Skip to content

Commit c224cf4

Browse files
committed
Fixed obj crash on line with empty groupName
1 parent 0c7d00d commit c224cf4

File tree

3 files changed

+174
-8
lines changed

3 files changed

+174
-8
lines changed

src/main/kotlin/assimp/format/obj/ObjFileParser.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
3939
fun parseFile(streamBuffer: BufferedReader) {
4040

4141

42-
var line : String? = ""
43-
do{
42+
var line: String? = ""
43+
do {
4444
//Support for continuationToken
45-
line = ObjTools.getNextDataLine(streamBuffer,"\\")
45+
line = ObjTools.getNextDataLine(streamBuffer, "\\")
4646

4747
//End of stream
4848
if (line == null)
@@ -94,7 +94,7 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
9494
// Parse object name
9595
'o' -> getObjectName(line)
9696
}
97-
}while(line != null)
97+
} while (line != null)
9898
}
9999

100100
// -------------------------------------------------------------------
@@ -129,7 +129,7 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
129129
hasNormal = true
130130
}
131131
else -> {
132-
logger.error { "OBJ: Not supported token in face description detected --> "+ line }
132+
logger.error { "OBJ: Not supported token in face description detected --> " + line }
133133
skip = true
134134
}
135135
}
@@ -141,7 +141,7 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
141141
hasNormal = true
142142
}
143143
else -> {
144-
logger.error { "OBJ: Not supported token in face description detected -- >"+ line }
144+
logger.error { "OBJ: Not supported token in face description detected -- >" + line }
145145
skip = true
146146
}
147147
}
@@ -256,7 +256,7 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
256256
if (words.size < 2) throw Error("File name of the material is absent.")
257257

258258
// get the name of the mat file with spaces
259-
var filename = ObjTools.getNameWithSpace(words,1)
259+
var filename = ObjTools.getNameWithSpace(words, 1)
260260

261261
val pFile = file.parentPath() + "/" + filename //windows can just suck it
262262
println(pFile)
@@ -282,7 +282,7 @@ class ObjFileParser(private val file: IOStream, val ioSystem: IOSystem) {
282282
// Getter for a group name.
283283
fun getGroupName(line: String) {
284284

285-
val groupName = line.split("\\s+".toRegex())[1]
285+
val groupName = line.split("\\s+".toRegex()).getOrElse(1) { "" }
286286
// Change active group, if necessary
287287
if (m_pModel.m_strActiveGroup != groupName) {
288288

src/test/kotlin/assimp/obj/cube.kt

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package assimp.obj
2+
3+
import assimp.*
4+
import glm_.mat4x4.Mat4
5+
import glm_.vec3.Vec3
6+
import io.kotlintest.shouldBe
7+
import uno.kotlin.uri
8+
import uno.kotlin.url
9+
10+
/**
11+
* Created by elect on 16/11/2016.
12+
*/
13+
14+
object cube {
15+
16+
operator fun invoke(fileName: String) {
17+
18+
with(Importer().readFile(getResource(fileName))!!) {
19+
20+
// with(rootNode) {
21+
//
22+
// name shouldBe "box.obj"
23+
// transformation shouldBe Mat4()
24+
// numChildren shouldBe 1
25+
//
26+
// with(children[0]) {
27+
//
28+
// name shouldBe "1"
29+
// transformation shouldBe Mat4()
30+
// numChildren shouldBe 0
31+
// numMeshes shouldBe 1
32+
// meshes[0] shouldBe 0
33+
// }
34+
// numMeshes shouldBe 0
35+
// }
36+
// numMeshes shouldBe 1
37+
// with(meshes[0]) {
38+
// primitiveTypes shouldBe AiPrimitiveType.POLYGON.i
39+
// numVertices shouldBe 24
40+
// numFaces shouldBe 6
41+
//
42+
// vertices[0] shouldBe Vec3(-0.5, +0.5, +0.5)
43+
// vertices[5] shouldBe Vec3(+0.5, -0.5, -0.5)
44+
// vertices[10] shouldBe Vec3(+0.5, -0.5, -0.5)
45+
// vertices[15] shouldBe Vec3(-0.5, +0.5, +0.5)
46+
// vertices[20] shouldBe Vec3(+0.5, -0.5, -0.5)
47+
// vertices[23] shouldBe Vec3(+0.5, -0.5, +0.5)
48+
//
49+
// var i = 0
50+
// faces.forEach {
51+
// it.size shouldBe 4
52+
// it shouldBe mutableListOf(i++, i++, i++, i++)
53+
// }
54+
// }
55+
// with(materials[0]) {
56+
// name shouldBe AI_DEFAULT_MATERIAL_NAME
57+
// shadingModel shouldBe AiShadingMode.gouraud
58+
// with(color!!) {
59+
// ambient shouldBe Vec3()
60+
// diffuse shouldBe Vec3(0.6)
61+
// specular shouldBe Vec3()
62+
// emissive shouldBe Vec3()
63+
// shininess shouldBe 0f
64+
// opacity shouldBe 1f
65+
// refracti shouldBe 1f
66+
// }
67+
// }
68+
// }
69+
// }
70+
71+
72+
// val concavePolygon = "concave_polygon.obj"
73+
//
74+
// concavePolygon
75+
// {
76+
//
77+
// with(Importer().readFile(obj + concavePolygon)!!) {
78+
//
79+
// with(rootNode) {
80+
//
81+
// name shouldBe "concave_polygon.obj"
82+
// transformation shouldBe Mat4()
83+
// numChildren shouldBe 2
84+
//
85+
// with(children[0]) {
86+
//
87+
// name shouldBe "concave_test.obj"
88+
// transformation shouldBe Mat4()
89+
// parent === rootNode
90+
// numChildren shouldBe 0
91+
// numMeshes shouldBe 0
92+
// }
93+
// with(children[1]) {
94+
//
95+
// name shouldBe "default"
96+
// transformation shouldBe Mat4()
97+
// parent === rootNode
98+
// numChildren shouldBe 0
99+
// numMeshes shouldBe 1
100+
// meshes[0] shouldBe 0
101+
// }
102+
// }
103+
// with(meshes[0]) {
104+
//
105+
// primitiveTypes shouldBe AiPrimitiveType.POLYGON.i
106+
// numVertices shouldBe 66
107+
// numFaces shouldBe 1
108+
//
109+
// vertices[0] shouldBe Vec3(-1.14600003, 2.25515008, 3.07623005)
110+
// vertices[10] shouldBe Vec3(-1.14600003, 1.78262997, 1.93549001)
111+
// vertices[20] shouldBe Vec3(-1.14600003, 3.01736999, 1.93549001)
112+
// vertices[30] shouldBe Vec3(-1.14600003, 2.54485, 3.07623005)
113+
// vertices[40] shouldBe Vec3(-1.14600003, 3.08750010, 2.34999990)
114+
// vertices[50] shouldBe Vec3(-1.14600003, 2.13690996, 1.71483)
115+
// vertices[60] shouldBe Vec3(-1.14600003, 1.91386, 2.83613992)
116+
// vertices[65] shouldBe Vec3(-1.14600003, 2.40000010, 3.0905)
117+
//
118+
// normals.forEach { it shouldBe Vec3(1, 0, -0.0) }
119+
// var i = 0
120+
// faces[0].forEach { it shouldBe i++ }
121+
//
122+
// materialIndex shouldBe 1
123+
//
124+
// name shouldBe "default"
125+
// }
126+
// numMaterials shouldBe 2
127+
//
128+
// with(materials[0]) {
129+
//
130+
// name shouldBe "DefaultMaterial"
131+
//
132+
// shadingModel shouldBe AiShadingMode.gouraud
133+
//
134+
// with(color!!) {
135+
//
136+
// ambient!! shouldBe Vec3(0)
137+
// diffuse!! shouldBe Vec3(0.600000024)
138+
// specular!! shouldBe Vec3(0)
139+
// emissive!! shouldBe Vec3(0)
140+
// shininess!! shouldBe 0f
141+
// opacity!! shouldBe 1f
142+
// refracti!! shouldBe 1f
143+
// }
144+
// }
145+
//
146+
// with(materials[1]) {
147+
//
148+
// name shouldBe "test"
149+
//
150+
// shadingModel shouldBe AiShadingMode.gouraud
151+
//
152+
// with(color!!) {
153+
//
154+
// ambient!! shouldBe Vec3(0)
155+
// diffuse!! shouldBe Vec3(0.141176000, 0.184313998, 0.411765009)
156+
// specular!! shouldBe Vec3(0)
157+
// emissive!! shouldBe Vec3(0)
158+
// shininess!! shouldBe 400f
159+
// opacity!! shouldBe 1f
160+
// refracti!! shouldBe 1f
161+
// }
162+
// }
163+
}
164+
}
165+
}

src/test/kotlin/assimp/obj/obj.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class obj : StringSpec() {
88
val path = "$models/OBJ/"
99

1010
init {
11+
"cube"{ cube(path + "cube.obj") }
1112
"wall"{ wall(path + "wall.obj") }
1213
"box"{ box(path + "box.obj") }
1314
"spider"{ spider(path + "spider.obj") }

0 commit comments

Comments
 (0)