-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathModelIO.swift
330 lines (287 loc) · 14.2 KB
/
ModelIO.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// UNSUPPORTED: OS=watchos
// REQUIRES: objc_interop
// Currently crashes on iphonesimulator.
// rdar://35490456
// UNSUPPORTED: OS=ios
// UNSUPPORTED: OS=tvos
import StdlibUnittest
import Foundation
import ModelIO
var ModelIOTests = TestSuite("ModelIO")
if #available(OSX 10.13, iOS 11.0, tvOS 11.0, *) {
ModelIOTests.test("MDLAnimatedScalar/accessors") {
let animatedVal = MDLAnimatedScalar()
let testCount = 10
let testTimeVal = 5.0
let testFloatVal:Float = 1.0
let testDoubleVal = Double(testFloatVal)
let fArray = [Float](repeating: testFloatVal, count: testCount)
let dArray = [Double](repeating: testDoubleVal, count: testCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(floatArray: fArray, atTimes: times)
let floats = animatedVal.floatArray
animatedVal.reset(doubleArray: dArray, atTimes: times)
let doubles = animatedVal.doubleArray
times = animatedVal.times
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
expectEqual(floats[idx], testFloatVal)
expectEqual(doubles[idx], testDoubleVal)
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedVector2/accessors") {
let animatedVal = MDLAnimatedVector2()
let testCount = 10
let testTimeVal = 5.0
let testFloatVal = float2(1.0, 2.0)
let testDoubleVal = double2(Double(testFloatVal.x), Double(testFloatVal.y))
let fArray = [float2](repeating: testFloatVal, count: testCount)
let dArray = [double2](repeating: testDoubleVal, count: testCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(float2Array: fArray, atTimes: times)
let floats = animatedVal.float2Array
animatedVal.reset(double2Array: dArray, atTimes: times)
let doubles = animatedVal.double2Array
times = animatedVal.times
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
expectEqual(floats[idx].x, testFloatVal.x)
expectEqual(floats[idx].y, testFloatVal.y)
expectEqual(doubles[idx].x, testDoubleVal.x)
expectEqual(doubles[idx].y, testDoubleVal.y)
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedVector3/accessors") {
let animatedVal = MDLAnimatedVector3()
let testCount = 10
let testTimeVal = 5.0
let testFloatVal = float3(1.0, 2.0, 3.0)
let testDoubleVal = double3(Double(testFloatVal.x), Double(testFloatVal.y), Double(testFloatVal.z))
let fArray = [float3](repeating: testFloatVal, count: testCount)
let dArray = [double3](repeating: testDoubleVal, count: testCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(float3Array: fArray, atTimes: times)
let floats = animatedVal.float3Array
animatedVal.reset(double3Array: dArray, atTimes: times)
let doubles = animatedVal.double3Array
times = animatedVal.times
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
expectEqual(floats[idx].x, testFloatVal.x)
expectEqual(floats[idx].y, testFloatVal.y)
expectEqual(floats[idx].z, testFloatVal.z)
expectEqual(doubles[idx].x, testDoubleVal.x)
expectEqual(doubles[idx].y, testDoubleVal.y)
expectEqual(doubles[idx].z, testDoubleVal.z)
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedVector4/accessors") {
let animatedVal = MDLAnimatedVector4()
let testCount = 10
let testTimeVal = 5.0
let testFloatVal = float4(1.0, 2.0, 3.0, 4.0)
let testDoubleVal = double4(Double(testFloatVal.x), Double(testFloatVal.y), Double(testFloatVal.z), Double(testFloatVal.w))
let fArray = [float4](repeating: testFloatVal, count: testCount)
let dArray = [double4](repeating: testDoubleVal, count: testCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(float4Array: fArray, atTimes: times)
let floats = animatedVal.float4Array
animatedVal.reset(double4Array: dArray, atTimes: times)
let doubles = animatedVal.double4Array
times = animatedVal.times
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
expectEqual(floats[idx].x, testFloatVal.x)
expectEqual(floats[idx].y, testFloatVal.y)
expectEqual(floats[idx].z, testFloatVal.z)
expectEqual(floats[idx].w, testFloatVal.w)
expectEqual(doubles[idx].x, testDoubleVal.x)
expectEqual(doubles[idx].y, testDoubleVal.y)
expectEqual(doubles[idx].z, testDoubleVal.z)
expectEqual(doubles[idx].w, testDoubleVal.w)
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedMatrix4x4/accessors") {
let animatedVal = MDLAnimatedMatrix4x4()
let testCount = 10
let testTimeVal = 5.0
let testFloatVal = matrix_identity_float4x4
let testDoubleVal = matrix_identity_double4x4
let fArray = [float4x4](repeating: testFloatVal, count: testCount)
let dArray = [double4x4](repeating: testDoubleVal, count: testCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(float4x4Array: fArray, atTimes: times)
let floats = animatedVal.float4x4Array
animatedVal.reset(double4Array: dArray, atTimes: times)
let doubles = animatedVal.double4x4Array
times = animatedVal.times
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
for matIdx in 0..<4 {
expectEqual(floats[idx][matIdx].x, testFloatVal[matIdx].x)
expectEqual(floats[idx][matIdx].y, testFloatVal[matIdx].y)
expectEqual(floats[idx][matIdx].z, testFloatVal[matIdx].z)
expectEqual(floats[idx][matIdx].w, testFloatVal[matIdx].w)
expectEqual(doubles[idx][matIdx].x, testDoubleVal[matIdx].x)
expectEqual(doubles[idx][matIdx].y, testDoubleVal[matIdx].y)
expectEqual(doubles[idx][matIdx].z, testDoubleVal[matIdx].z)
expectEqual(doubles[idx][matIdx].w, testDoubleVal[matIdx].w)
}
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLMatrix4x4Array/accessors") {
let testCount = 10
let matrixArray = MDLMatrix4x4Array(elementCount: testCount)
let testFloatVal = float4x4()
let testDoubleVal = double4x4()
let fArray = [float4x4](repeating: testFloatVal, count: testCount)
let dArray = [double4x4](repeating: testDoubleVal, count: testCount)
matrixArray.float4x4Array = fArray
let floats = matrixArray.float4x4Array
matrixArray.double4x4Array = dArray
let doubles = matrixArray.double4x4Array
expectEqual(floats.count, testCount)
expectEqual(doubles.count, testCount)
for idx in 0..<testCount {
for matIdx in 0..<4 {
expectEqual(floats[idx][matIdx].x, testFloatVal[matIdx].x)
expectEqual(floats[idx][matIdx].y, testFloatVal[matIdx].y)
expectEqual(floats[idx][matIdx].z, testFloatVal[matIdx].z)
expectEqual(floats[idx][matIdx].w, testFloatVal[matIdx].w)
expectEqual(doubles[idx][matIdx].x, testDoubleVal[matIdx].x)
expectEqual(doubles[idx][matIdx].y, testDoubleVal[matIdx].y)
expectEqual(doubles[idx][matIdx].z, testDoubleVal[matIdx].z)
expectEqual(doubles[idx][matIdx].w, testDoubleVal[matIdx].w)
}
}
}
ModelIOTests.test("MDLAnimatedScalarArray/accessors") {
let elementCount = 10
let animatedVal = MDLAnimatedScalarArray(elementCount: elementCount)
let subCount = 2
let testCount = 10
let totalCount = elementCount * testCount
let testTimeVal = 5.0
let testFloatVal:Float = 10.0
let testSubFloatVal:Float = 5.0
let testDoubleVal = Double(testFloatVal)
let testSubDoubleVal = Double(testSubFloatVal)
let fArray = [Float](repeating: testFloatVal, count: totalCount)
let _ = [Float](repeating: testSubFloatVal, count: subCount)
let dArray = [Double](repeating: testDoubleVal, count: totalCount)
let _ = [Double](repeating: testSubDoubleVal, count: subCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(floatArray: fArray, atTimes: times)
let floats = animatedVal.floatArray
// reset is currently appending instead of resetting the time sampled data
//animatedVal.reset(doubleArray: dArray, atTimes: times)
let doubles = animatedVal.doubleArray
let sampledFloatArray = animatedVal.floatArray(atTime: 5.0)
let sampledDoubleArray = animatedVal.doubleArray(atTime: 5.0)
times = animatedVal.times
expectEqual(floats.count, totalCount)
expectEqual(doubles.count, totalCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
// -- test a sampled time
expectEqual(sampledFloatArray[idx], testFloatVal)
expectEqual(sampledDoubleArray[idx], testDoubleVal)
// -- for each time test the arrays
for arrIdx in 0..<elementCount {
expectEqual(floats[idx * elementCount + arrIdx], testFloatVal)
expectEqual(doubles[idx * elementCount + arrIdx], testDoubleVal)
}
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedQuaternionArray/accessors") {
let elementCount = 10
let testCount = 10
let totalCount = elementCount * testCount
let animatedVal = MDLAnimatedQuaternionArray(elementCount: elementCount)
let testTimeVal = 5.0;
let testFloatVal = simd_quatf(ix: 1.0, iy: 2.0, iz: 3.0, r: 4.0)
let testDoubleVal = simd_quatd(ix: 1.0, iy: 2.0, iz: 3.0, r: 4.0)
let fArray = [simd_quatf](repeating: testFloatVal, count: totalCount)
let dArray = [simd_quatd](repeating: testDoubleVal, count: totalCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(floatQuaternionArray: fArray, atTimes: times)
let quatFloats = animatedVal.floatQuaternionArray
// reset is appending instead of reseting the time sampled data
//animatedVal.reset(doubleQuaternionArray: dArray, atTimes: times)
let quatDoubles = animatedVal.doubleQuaternionArray
let sampledFloatQuaternionArray = animatedVal.floatQuaternionArray(atTime: 5.0)
let sampledDoubleQuaternionArray = animatedVal.doubleQuaternionArray(atTime: 5.0)
times = animatedVal.times
expectEqual(quatFloats.count, totalCount)
expectEqual(quatDoubles.count, totalCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
// -- test a sampled time
// -- data gets swizzled somewhere between getting and setting so we just
// -- check to make sure we can at least access the data
sampledFloatQuaternionArray[idx]
sampledDoubleQuaternionArray[idx]
/*expectEqual(sampledFloatQuaternionArray[idx], testFloatVal)
expectEqual(sampledDoubleQuaternionArray[idx], testDoubleVal)
// -- for each time test the arrays
for arrIdx in 0..<elementCount {
expectEqual(quatFloats[idx * elementCount + arrIdx], testFloatVal)
expectEqual(quatDoubles[idx * elementCount + arrIdx], testDoubleVal)
}*/
expectEqual(times[idx], testTimeVal)
}
}
ModelIOTests.test("MDLAnimatedVector3Array/accessors") {
let elementCount = 10
let animatedVal = MDLAnimatedVector3Array(elementCount: elementCount)
let testCount = 10
let totalCount = elementCount * testCount
let testTimeVal = 5.0
let testFloatVal = float3(1.0, 2.0, 3.0)
let testDoubleVal = double3(1.0, 2.0, 3.0)
let fArray = [float3](repeating: testFloatVal, count: totalCount)
let dArray = [double3](repeating: testDoubleVal, count: totalCount)
var times = [TimeInterval](repeating: testTimeVal, count: testCount)
animatedVal.reset(float3Array: fArray, atTimes: times)
let vector3Floats = animatedVal.float3Array
// reset is appending instead reseting the time sampled data
//animatedVal.reset(double3Array: dArray, atTimes: times)
let vector3Doubles = animatedVal.double3Array
let sampledFloatVector3Array = animatedVal.float3Array(atTime: 5.0)
let sampledDoubleVector3Array = animatedVal.double3Array(atTime: 5.0)
times = animatedVal.times
expectEqual(vector3Floats.count, totalCount)
expectEqual(vector3Doubles.count, totalCount)
expectEqual(times.count, testCount)
for idx in 0..<testCount {
// -- test a sampled time
expectEqual(sampledFloatVector3Array[idx], testFloatVal)
expectEqual(sampledDoubleVector3Array[idx], testDoubleVal)
// -- for each time test the arrays
for arrIdx in 0..<elementCount {
expectEqual(vector3Floats[idx * elementCount + arrIdx], testFloatVal)
expectEqual(vector3Doubles[idx * elementCount + arrIdx], testDoubleVal)
}
expectEqual(times[idx], testTimeVal)
}
}
}
runAllTests()