Skip to content

Commit 2115cd8

Browse files
committed
treasure_smooth.dae bug hunting in progress
1 parent 2bfa1ae commit 2115cd8

File tree

3 files changed

+120
-60
lines changed

3 files changed

+120
-60
lines changed

src/main/kotlin/assimp/format/collada/ColladaLoader.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class ColladaLoader : BaseImporter() {
8888
mFileName = file
8989
// parse the input file
9090
val parser = ColladaParser(ioSystem.open(file))
91-
if (parser.mRootNode == null) throw Error("Collada: File came out empty. Something is wrong here.")
91+
if (parser.mRootNode == null)
92+
throw Error("Collada: File came out empty. Something is wrong here.")
9293
// create the materials first, for the meshes to find
9394
buildMaterials(parser)
9495
// build the node hierarchy from it
@@ -301,7 +302,7 @@ class ColladaLoader : BaseImporter() {
301302

302303
if (srcMesh == null) {
303304
logger.warn { "Collada: Unable to find geometry for ID \"${mid.mMeshOrController}\". Skipping." }
304-
return
305+
return@forEach
305306
}
306307
}
307308
// else ID found in the mesh library -> direct reference to an unskinned mesh
@@ -314,7 +315,7 @@ class ColladaLoader : BaseImporter() {
314315
val submesh = srcMesh!!.mSubMeshes[sm]
315316

316317
if (submesh.mNumFaces == 0)
317-
return
318+
return@forEach
318319

319320
// find material assigned to this submesh
320321
var meshMaterial = ""
@@ -351,8 +352,8 @@ class ColladaLoader : BaseImporter() {
351352

352353
// if we already have the mesh at the library, just add its index to the node's array
353354
mMeshIndexByID[index]?.let {
354-
newMeshRefs.add(it)
355-
return
355+
newMeshRefs += it
356+
return@forEach
356357
}
357358

358359
// else we have to add the mesh to the collection and store its newly assigned index at the node

src/main/kotlin/assimp/format/collada/ColladaParser.kt

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import glm_.vec3.Vec3
88
import java.io.File
99
import java.io.FileReader
1010
import java.net.URI
11+
import java.util.*
1112
import javax.xml.namespace.QName
1213
import javax.xml.stream.XMLEventReader
1314
import javax.xml.stream.XMLInputFactory
@@ -23,17 +24,17 @@ import assimp.format.collada.PrimitiveType as Pt
2324
* Created by elect on 23/01/2017.
2425
*/
2526

26-
typealias DataLibrary = HashMap<String, Data>
27-
typealias AccessorLibrary = HashMap<String, Accessor>
28-
typealias MeshLibrary = HashMap<String, Mesh>
29-
typealias NodeLibrary = HashMap<String, Node>
30-
typealias ImageLibrary = HashMap<String, Image>
31-
typealias EffectLibrary = HashMap<String, Effect>
32-
typealias MaterialLibrary = HashMap<String, Material>
33-
typealias LightLibrary = HashMap<String, Light>
34-
typealias CameraLibrary = HashMap<String, Camera>
35-
typealias ControllerLibrary = HashMap<String, Controller>
36-
typealias AnimationLibrary = HashMap<String, Animation>
27+
typealias DataLibrary = SortedMap<String, Data>
28+
typealias AccessorLibrary = SortedMap<String, Accessor>
29+
typealias MeshLibrary = SortedMap<String, Mesh>
30+
typealias NodeLibrary = SortedMap<String, Node>
31+
typealias ImageLibrary = SortedMap<String, Image>
32+
typealias EffectLibrary = SortedMap<String, Effect>
33+
typealias MaterialLibrary = SortedMap<String, Material>
34+
typealias LightLibrary = SortedMap<String, Light>
35+
typealias CameraLibrary = SortedMap<String, Camera>
36+
typealias ControllerLibrary = SortedMap<String, Controller>
37+
typealias AnimationLibrary = SortedMap<String, Animation>
3738
typealias AnimationClipLibrary = ArrayList<Pair<String, ArrayList<String>>>
3839

3940
typealias ChannelMap = MutableMap<String, AnimationChannel>
@@ -54,27 +55,27 @@ class ColladaParser(pFile: IOStream) {
5455
lateinit var endElement: EndElement
5556
/** All data arrays found in the file by ID. Might be referred to by actually everyone. Collada, you are a steaming
5657
* pile of indirection. */
57-
var mDataLibrary: DataLibrary = HashMap()
58+
var mDataLibrary: DataLibrary = sortedMapOf()
5859
/** Same for accessors which define how the data in a data array is accessed. */
59-
var mAccessorLibrary: AccessorLibrary = HashMap()
60+
var mAccessorLibrary: AccessorLibrary = sortedMapOf()
6061
/** Mesh library: mesh by ID */
61-
var mMeshLibrary: MeshLibrary = HashMap()
62+
var mMeshLibrary: MeshLibrary = sortedMapOf()
6263
/** node library: root node of the hierarchy part by ID */
63-
var mNodeLibrary: NodeLibrary = HashMap()
64+
var mNodeLibrary: NodeLibrary = sortedMapOf()
6465
/** Image library: stores texture properties by ID */
65-
var mImageLibrary: ImageLibrary = HashMap()
66+
var mImageLibrary: ImageLibrary = sortedMapOf()
6667
/** Effect library: surface attributes by ID */
67-
var mEffectLibrary: EffectLibrary = HashMap()
68+
var mEffectLibrary: EffectLibrary = sortedMapOf()
6869
/** Material library: surface material by ID */
69-
var mMaterialLibrary: MaterialLibrary = HashMap()
70+
var mMaterialLibrary: MaterialLibrary = sortedMapOf()
7071
/** Light library: surface light by ID */
71-
var mLightLibrary: LightLibrary = HashMap()
72+
var mLightLibrary: LightLibrary = sortedMapOf()
7273
/** Camera library: surface material by ID */
73-
var mCameraLibrary: CameraLibrary = HashMap()
74+
var mCameraLibrary: CameraLibrary = sortedMapOf()
7475
/** Controller library: joint controllers by ID */
75-
var mControllerLibrary: ControllerLibrary = HashMap()
76+
var mControllerLibrary: ControllerLibrary = sortedMapOf()
7677
/** Animation library: animation references by ID */
77-
var mAnimationLibrary: AnimationLibrary = HashMap()
78+
var mAnimationLibrary: AnimationLibrary = sortedMapOf()
7879
/** Animation clip library: clip animation references by ID */
7980
var mAnimationClipLibrary: AnimationClipLibrary = ArrayList()
8081
/** Pointer to the root node. Don't delete, it just points to one of the nodes in the node library. */
@@ -1828,6 +1829,6 @@ class ColladaParser(pFile: IOStream) {
18281829
}
18291830

18301831
/** Finds the item in the given library by its reference, throws if not found */
1831-
fun <T> resolveLibraryReference(pLibrary: HashMap<String, T>, pURL: String): T =
1832+
fun <T> resolveLibraryReference(pLibrary: SortedMap<String, T>, pURL: String): T =
18321833
pLibrary[pURL] ?: throw Exception("Unable to resolve library reference \"$pURL\".")
18331834
}

src/test/kotlin/assimp/collada/treasure_smooth.kt

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package assimp.collada
22

3-
import assimp.AiShadingMode
3+
import assimp.AiPrimitiveType
44
import assimp.Importer
55
import glm_.mat4x4.Mat4
66
import glm_.vec3.Vec3
7-
import io.kotlintest.shouldBe
7+
import io.kotlintest.matchers.shouldBe
88
import java.net.URL
99

1010
object treasure_smooth {
@@ -14,36 +14,94 @@ object treasure_smooth {
1414
with(Importer().readFile(fileName)!!) {
1515

1616
flags shouldBe 0
17-
// with(rootNode) {
18-
// name shouldBe "VisualSceneNode"
19-
// transformation shouldBe Mat4()
20-
// parent shouldBe null
21-
// numChildren shouldBe 1
22-
//
23-
// with(children[0]) {
24-
// name shouldBe "Mesh_Object"
25-
// transformation shouldBe Mat4()
26-
// (parent === rootNode) shouldBe true
27-
// numChildren shouldBe 0
28-
// children.isEmpty() shouldBe true
29-
// numMeshes shouldBe 1
30-
// meshes[0] shouldBe 0
31-
// metaData.isEmpty() shouldBe true
32-
// }
33-
// }
34-
//
35-
// numMeshes shouldBe 1
36-
// with(meshes[0]) {
37-
// primitiveTypes shouldBe 8
38-
// numVertices shouldBe 66
39-
// numFaces shouldBe 1
40-
// vertices[0] shouldBe Vec3(-1.14600003f, 2.25515008f, 3.07623005f)
41-
// vertices[32] shouldBe Vec3(-1.14600003f,2.40000010f,3.03749990f)
42-
// vertices[65] shouldBe Vec3(-1.14600003f, 2.40000010f, 3.0905f)
43-
// faces[0].forEachIndexed { i, it -> it shouldBe i }
44-
// name shouldBe "Mesh_Object"
45-
// }
46-
//
17+
with(rootNode) {
18+
name shouldBe "Scene"
19+
transformation shouldBe Mat4(
20+
1f, 0f, 0f, 0f,
21+
0f, 0f, -1f, 0f,
22+
0f, 1f, 0f, 0f,
23+
0f, 0f, 0f, 1f)
24+
parent shouldBe null
25+
numChildren shouldBe 68
26+
27+
with(children[0]) {
28+
name shouldBe "Plane"
29+
transformation shouldBe Mat4()
30+
(parent === rootNode) shouldBe true
31+
numChildren shouldBe 0
32+
children.isEmpty() shouldBe true
33+
numMeshes shouldBe 2
34+
meshes[0] shouldBe 0
35+
meshes[1] shouldBe 1
36+
metaData.isEmpty() shouldBe true
37+
}
38+
39+
with(children[34]) {
40+
name shouldBe "Cube_015"
41+
transformation shouldBe Mat4(
42+
0.122561797f, 0.0853303894f, -0.0305411592f, 1.10377395f,
43+
-0.0906103402f, 0.116471097f, -0.0382059515f, -4.13784218f,
44+
0.00194863102f, 0.0488739200f, 0.144371003f, 0.766323626f,
45+
0f, 0f, 0f, 1f)
46+
(parent === rootNode) shouldBe true
47+
numChildren shouldBe 0
48+
children.isEmpty() shouldBe true
49+
numMeshes shouldBe 1
50+
meshes[0] shouldBe 48
51+
metaData.isEmpty() shouldBe true
52+
}
53+
54+
with(children[67]) {
55+
name shouldBe "Cylinder_027"
56+
transformation shouldBe Mat4(
57+
0.0158306006f, -0.0440383293f, 0.0162486192f, -0.724227428f,
58+
0.0467648916f, 0.0133153200f, -0.00947351381f, -2.80546498f,
59+
0.00405431492f, 0.0183664802f, 0.0458283201f, 0.661597490f,
60+
0f, 0f, 0f, 1f)
61+
(parent === rootNode) shouldBe true
62+
numChildren shouldBe 0
63+
children.isEmpty() shouldBe true
64+
numMeshes shouldBe 2
65+
meshes[0] shouldBe 84
66+
meshes[1] shouldBe 85
67+
metaData.isEmpty() shouldBe true
68+
}
69+
}
70+
71+
numMeshes shouldBe 86
72+
with(meshes[0]) {
73+
primitiveTypes shouldBe AiPrimitiveType.TRIANGLE.i
74+
numVertices shouldBe 1554
75+
numFaces shouldBe 518
76+
vertices[0] shouldBe Vec3(7.50098801f, 6.56336403f, 0f)
77+
vertices[776] shouldBe Vec3(-1.62673700f, -3.29218197f, 0.503621578f)
78+
vertices[1553] shouldBe Vec3(-1.15598500f, -3.56582594f, 0.488107890f)
79+
normals[0] shouldBe Vec3(0.00296032405f, 0.000976616982f, 0.999995172f)
80+
normals[776] shouldBe Vec3(0.600491107f, 0.295148909f, 0.743167281f)
81+
normals[1553] shouldBe Vec3(0.254105508f, 0.335226387f, 0.907223225f)
82+
for(i in 0 until numFaces)
83+
for(j in 0..2)
84+
faces[i][j] shouldBe i * 3 + j
85+
name shouldBe "Plane"
86+
}
87+
88+
with(meshes[1]) {
89+
primitiveTypes shouldBe AiPrimitiveType.TRIANGLE.i
90+
numVertices shouldBe 342
91+
numFaces shouldBe 114
92+
vertices.isNotEmpty() shouldBe true
93+
// vertices[0] shouldBe Vec3(7.50098801f, 6.56336403f, 0f)
94+
// vertices[776] shouldBe Vec3(-1.62673700f, -3.29218197f, 0.503621578f)
95+
// vertices[1553] shouldBe Vec3(-1.15598500f, -3.56582594f, 0.488107890f)
96+
// normals[0] shouldBe Vec3(0.00296032405f, 0.000976616982f, 0.999995172f)
97+
// normals[776] shouldBe Vec3(0.600491107f, 0.295148909f, 0.743167281f)
98+
// normals[1553] shouldBe Vec3(0.254105508f, 0.335226387f, 0.907223225f)
99+
// for(i in 0 until numFaces)
100+
// for(j in 0..2)
101+
// faces[i][j] shouldBe i * 3 + j
102+
// name shouldBe "Plane"
103+
}
104+
47105
// numMaterials shouldBe 1
48106
// with(materials[0]) {
49107
// name shouldBe "test_Smoothing"

0 commit comments

Comments
 (0)