forked from juj/MathGeoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangleMesh.cpp
480 lines (418 loc) · 12.2 KB
/
TriangleMesh.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* Copyright Jukka Jylänki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/** @file TriangleMesh.cpp
@author Jukka Jylänki
@brief Implementation for the TriangleMesh geometry object. */
#include "TriangleMesh.h"
#include <stdlib.h>
#include <string.h>
#include "../Math/float3.h"
#include "Triangle.h"
#include "Ray.h"
#include "Polyhedron.h"
#include "../MathGeoLibFwd.h"
#include "../Math/MathConstants.h"
#include "../Math/myassert.h"
#include "../../tests/SystemInfo.h"
#include <vector>
#include "../Math/SSEMath.h"
// If defined, we preprocess our TriangleMesh data structure to contain (v0, v1-v0, v2-v0)
// instead of (v0, v1, v2) triplets for faster ray-triangle mesh intersection.
#define SOA_HAS_EDGES
MATH_BEGIN_NAMESPACE
enum SIMDCapability
{
SIMD_NONE,
SIMD_SSE,
SIMD_SSE2,
// SIMD_SSE3,
// SIMD_SSSE3,
// SIMD_SSE4,
SIMD_SSE41,
// SIMD_SSE42,
SIMD_AVX
};
SIMDCapability DetectSIMDCapability()
{
#ifdef WIN32 ///\todo SIMD detection for other x86 platforms.
#ifdef MATH_SSE
int CPUInfo[4] = {-1};
unsigned nIds;//, nExIds, i;
int nFeatureInfo = 0;
// bool bSSE3Instructions = false;
// bool bSupplementalSSE3 = false;
// bool bCMPXCHG16B = false;
#ifdef MATH_SSE41
bool bSSE41Extensions = false;
#endif
// bool bSSE42Extensions = false;
// bool bPOPCNT = false;
// bool bLAHF_SAHFAvailable = false;
// bool bCmpLegacy = false;
// bool bLZCNT = false;
// bool bSSE4A = false;
// bool bMisalignedSSE = false;
// bool bPREFETCH = false;
// bool bMMXExtensions = false;
// bool b3DNowExt = false;
// bool b3DNow = false;
// bool bFP128 = false;
#ifdef MATH_AVX
bool hasAVX = false;
#endif
// bool bMOVOptimization = false;
CpuId(CPUInfo, 0);
nIds = CPUInfo[0];
// Get the information associated with each valid Id
// for (i=0; i<=nIds; ++i)
if (nIds >= 1)
{
// __cpuid(CPUInfo, i);
CpuId(CPUInfo, 1);
// Interpret CPU feature information.
// if (i == 1)
{
// bSSE3Instructions = (CPUInfo[2] & 0x1) || false;
// bSupplementalSSE3 = (CPUInfo[2] & 0x200) || false;
// bCMPXCHG16B= (CPUInfo[2] & 0x2000) || false;
// bSSE41Extensions = (CPUInfo[2] & 0x80000) || false;
// bSSE42Extensions = (CPUInfo[2] & 0x100000) || false;
// bPOPCNT= (CPUInfo[2] & 0x800000) || false;
#ifdef MATH_AVX
hasAVX = (CPUInfo[2] & 0x10000000) || false;
#endif
nFeatureInfo = CPUInfo[3];
}
}
// const bool hasMMX = (nFeatureInfo & (1 << 23)) != 0;
// Calling __cpuid with 0x80000000 as the InfoType argument
// gets the number of valid extended IDs.
// __cpuid(CPUInfo, 0x80000000);
// nExIds = CPUInfo[0];
/*
// Get the information associated with each extended ID.
for (i=0x80000000; i<=nExIds; ++i)
{
__cpuid(CPUInfo, i);
if (i == 0x80000001)
{
bLAHF_SAHFAvailable = (CPUInfo[2] & 0x1) || false;
bCmpLegacy = (CPUInfo[2] & 0x2) || false;
bLZCNT = (CPUInfo[2] & 0x20) || false;
bSSE4A = (CPUInfo[2] & 0x40) || false;
bMisalignedSSE = (CPUInfo[2] & 0x80) || false;
bPREFETCH = (CPUInfo[2] & 0x100) || false;
bMMXExtensions = (CPUInfo[3] & 0x40000) || false;
b3DNowExt = (CPUInfo[3] & 0x40000000) || false;
b3DNow = (CPUInfo[3] & 0x80000000) || false;
}
if (i == 0x8000001A)
{
bFP128 = (CPUInfo[0] & 0x1) || false;
bMOVOptimization = (CPUInfo[0] & 0x2) || false;
}
}
*/
#ifdef MATH_AVX
if (hasAVX)
return SIMD_AVX;
#endif
#ifdef MATH_SSE41
if (bSSE41Extensions)
return SIMD_SSE41;
#endif
#ifdef MATH_SSE2
const bool hasSSE2 = (nFeatureInfo & (1 << 26)) != 0;
if (hasSSE2)
return SIMD_SSE2;
#endif
#ifdef MATH_SSE
const bool hasSSE = (nFeatureInfo & (1 << 25)) != 0;
if (hasSSE)
return SIMD_SSE;
#endif
#endif // ~ MATH_SSE not defined.
#endif
return SIMD_NONE;
}
const int simdCapability = DetectSIMDCapability();
TriangleMesh::TriangleMesh()
:data(0), numTriangles(0), vertexSizeBytes(0)
#ifdef _DEBUG
, vertexDataLayout(0)
#endif
{
}
TriangleMesh::~TriangleMesh()
{
AlignedFree(data);
}
TriangleMesh::TriangleMesh(const TriangleMesh &rhs)
:data(0), numTriangles(0), vertexSizeBytes(0)
#ifdef _DEBUG
, vertexDataLayout(0)
#endif
{
*this = rhs;
}
TriangleMesh &TriangleMesh::operator =(const TriangleMesh &rhs)
{
if (this == &rhs)
return *this;
#ifdef _DEBUG
vertexDataLayout = rhs.vertexDataLayout;
#endif
ReallocVertexBuffer(rhs.numTriangles, rhs.vertexSizeBytes);
memcpy(data, rhs.data, numTriangles*3*vertexSizeBytes);
return *this;
}
void TriangleMesh::Set(const Polyhedron &polyhedron)
{
TriangleArray tris = polyhedron.Triangulate();
if (!tris.empty())
{
int alignment = (simdCapability == SIMD_AVX) ? 8 : ((simdCapability == SIMD_SSE41 || simdCapability == SIMD_SSE2) ? 4 : 1);
vec degen = POINT_VEC_SCALAR(-FLOAT_INF);
Triangle degent(degen, degen, degen);
while(tris.size() % alignment != 0)
tris.push_back(degent);
Set((Triangle*)&tris[0], (int)tris.size());
}
}
void TriangleMesh::Set(const float *triangleMesh, int numTris, int vtxSizeBytes)
{
#ifndef MATH_AUTOMATIC_SSE // TODO: Restore support for this when MATH_AUTOMATIC_SSE is defined!
if (simdCapability == SIMD_AVX)
SetSoA8(triangleMesh, numTriangles, vertexSizeBytes);
else if (simdCapability == SIMD_SSE41 || simdCapability == SIMD_SSE2)
SetSoA4(triangleMesh, numTriangles, vertexSizeBytes);
else
#endif
SetAoS(triangleMesh, numTris, vtxSizeBytes);
}
float TriangleMesh::IntersectRay(const Ray &ray) const
{
#ifndef MATH_AUTOMATIC_SSE // TODO: Restore support for this when MATH_AUTOMATIC_SSE is defined!
#ifdef MATH_AVX
if (simdCapability == SIMD_AVX)
return IntersectRay_AVX(ray);
#endif
#ifdef MATH_SSE41
if (simdCapability == SIMD_SSE41)
return IntersectRay_SSE41(ray);
#endif
#ifdef MATH_SSE2
if (simdCapability == SIMD_SSE2)
return IntersectRay_SSE2(ray);
#endif
#endif
int triangleIndex;
float u, v;
return IntersectRay_TriangleIndex_UV_CPP(ray, triangleIndex, u, v);
}
float TriangleMesh::IntersectRay_TriangleIndex(const Ray &ray, int &outTriangleIndex) const
{
#ifndef MATH_AUTOMATIC_SSE // TODO: Restore support for this when MATH_AUTOMATIC_SSE is defined!
#ifdef MATH_AVX
if (simdCapability == SIMD_AVX)
return IntersectRay_TriangleIndex_AVX(ray, outTriangleIndex);
#endif
#ifdef MATH_SSE41
if (simdCapability == SIMD_SSE41)
return IntersectRay_TriangleIndex_SSE41(ray, outTriangleIndex);
#endif
#ifdef MATH_SSE2
if (simdCapability == SIMD_SSE2)
return IntersectRay_TriangleIndex_SSE2(ray, outTriangleIndex);
#endif
#endif
float u, v;
return IntersectRay_TriangleIndex_UV_CPP(ray, outTriangleIndex, u, v);
}
float TriangleMesh::IntersectRay_TriangleIndex_UV(const Ray &ray, int &outTriangleIndex, float &outU, float &outV) const
{
#ifndef MATH_AUTOMATIC_SSE // TODO: Restore support for this when MATH_AUTOMATIC_SSE is defined!
#ifdef MATH_AVX
if (simdCapability == SIMD_AVX)
return IntersectRay_TriangleIndex_UV_AVX(ray, outTriangleIndex, outU, outV);
#endif
#ifdef MATH_SSE41
if (simdCapability == SIMD_SSE41)
return IntersectRay_TriangleIndex_UV_SSE41(ray, outTriangleIndex, outU, outV);
#endif
#ifdef MATH_SSE2
if (simdCapability == SIMD_SSE2)
return IntersectRay_TriangleIndex_UV_SSE2(ray, outTriangleIndex, outU, outV);
#endif
#endif
return IntersectRay_TriangleIndex_UV_CPP(ray, outTriangleIndex, outU, outV);
}
void TriangleMesh::ReallocVertexBuffer(int numTris, int vertexSizeBytes_)
{
AlignedFree(data);
vertexSizeBytes = vertexSizeBytes_;
data = (float*)AlignedMalloc(numTris * 3 * vertexSizeBytes, 32);
numTriangles = numTris;
}
void TriangleMesh::SetAoS(const float *vertexData, int numTris, int vtxSizeBytes)
{
ReallocVertexBuffer(numTris, vtxSizeBytes);
#ifdef _DEBUG
vertexDataLayout = 0; // AoS
#endif
memcpy(data, vertexData, numTris * 3 * vtxSizeBytes);
}
void TriangleMesh::SetSoA4(const float *vertexData, int numTris, int vtxSizeBytes)
{
ReallocVertexBuffer(numTris, 3*sizeof(float));
#ifdef _DEBUG
vertexDataLayout = 1; // SoA4
#endif
assert(vtxSizeBytes % 4 == 0);
int vertexSizeFloats = vtxSizeBytes / 4;
int triangleSizeFloats = vertexSizeFloats * 3;
assert(numTris % 4 == 0); // We must have an evenly divisible amount of triangles, so that the SoA swizzling succeeds.
// From (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz)
// To xxxx yyyy zzzz xxxx yyyy zzzz xxxx yyyy zzzz
float *o = data;
for(int i = 0; i + 4 <= numTris; i += 4) // 4 triangles at a time
{
for (int j = 0; j < 3; ++j) // v0,v1,v2
{
const float *src = vertexData;
for (int k = 0; k < 3; ++k) // x,y,z
{
*o++ = src[0];
*o++ = src[triangleSizeFloats];
*o++ = src[2 * triangleSizeFloats];
*o++ = src[3 * triangleSizeFloats];
++src;
}
vertexData += vertexSizeFloats;
}
vertexData += 3 * triangleSizeFloats;
}
#ifdef SOA_HAS_EDGES
o = data;
for(int i = 0; i + 4 <= numTris; i += 4)
{
for(int j = 12; j < 24; ++j)
o[j] -= o[j-12];
for(int j = 24; j < 36; ++j)
o[j] -= o[j-24];
o += 36;
}
#endif
}
void TriangleMesh::SetSoA8(const float *vertexData, int numTris, int vtxSizeBytes)
{
ReallocVertexBuffer(numTris, 3*sizeof(float));
#ifdef _DEBUG
vertexDataLayout = 2; // SoA8
#endif
assert(vtxSizeBytes % 4 == 0);
int vertexSizeFloats = vtxSizeBytes / 4;
int triangleSizeFloats = vertexSizeFloats * 3;
assert(numTris % 8 == 0); // We must have an evenly divisible amount of triangles, so that the SoA swizzling succeeds.
// From (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz) (xyz xyz xyz)
// To xxxxxxxx yyyyyyyy zzzzzzzz xxxxxxxx yyyyyyyy zzzzzzzz xxxxxxxx yyyyyyyy zzzzzzzz
float *o = data;
for(int i = 0; i + 8 <= numTris; i += 8) // 8 triangles at a time.
{
for (int j = 0; j < 3; ++j) // v0, v1, v2
{
const float *src = vertexData;
for (int k = 0; k < 3; ++k) // x,y,z
{
*o++ = src[0];
*o++ = src[triangleSizeFloats];
*o++ = src[2 * triangleSizeFloats];
*o++ = src[3 * triangleSizeFloats];
*o++ = src[4 * triangleSizeFloats];
*o++ = src[5 * triangleSizeFloats];
*o++ = src[6 * triangleSizeFloats];
*o++ = src[7 * triangleSizeFloats];
++src;
}
vertexData += vertexSizeFloats;
}
vertexData += 7 * triangleSizeFloats;
}
#ifdef SOA_HAS_EDGES
o = data;
for(int i = 0; i + 8 <= numTris; i += 8)
{
for(int j = 24; j < 48; ++j)
o[j] -= o[j-24];
for(int j = 48; j < 72; ++j)
o[j] -= o[j-48];
o += 72;
}
#endif
}
float TriangleMesh::IntersectRay_TriangleIndex_UV_CPP(const Ray &ray, int &outTriangleIndex, float &outU, float &outV) const
{
assert(sizeof(float3) == 3*sizeof(float));
assert(sizeof(Triangle) == 3*sizeof(vec));
#ifdef _DEBUG
assert(vertexDataLayout == 0); // Must be AoS structured!
#endif
float nearestD = FLOAT_INF;
const Triangle *tris = reinterpret_cast<const Triangle*>(data);
for(int i = 0; i < numTriangles; ++i)
{
float u, v;
float d = Triangle::IntersectLineTri(ray.pos, ray.dir, tris->a, tris->b, tris->c, u, v);
if (d >= 0.f && d < nearestD)
{
nearestD = d;
outU = u;
outV = v;
outTriangleIndex = i;
}
++tris;
}
return nearestD;
}
MATH_END_NAMESPACE
#ifdef MATH_SSE2
#define MATH_GEN_SSE2
#include "TriangleMesh_IntersectRay_SSE.inl"
#define MATH_GEN_SSE2
#define MATH_GEN_TRIANGLEINDEX
#include "TriangleMesh_IntersectRay_SSE.inl"
#define MATH_GEN_SSE2
#define MATH_GEN_TRIANGLEINDEX
#define MATH_GEN_UV
#include "TriangleMesh_IntersectRay_SSE.inl"
#endif
#ifdef MATH_SSE41
#define MATH_GEN_SSE41
#include "TriangleMesh_IntersectRay_SSE.inl"
#define MATH_GEN_SSE41
#define MATH_GEN_TRIANGLEINDEX
#include "TriangleMesh_IntersectRay_SSE.inl"
#define MATH_GEN_SSE41
#define MATH_GEN_TRIANGLEINDEX
#define MATH_GEN_UV
#include "TriangleMesh_IntersectRay_SSE.inl"
#endif
#ifdef MATH_AVX
#define MATH_GEN_AVX
#include "TriangleMesh_IntersectRay_AVX.inl"
#define MATH_GEN_AVX
#define MATH_GEN_TRIANGLEINDEX
#include "TriangleMesh_IntersectRay_AVX.inl"
#define MATH_GEN_AVX
#define MATH_GEN_TRIANGLEINDEX
#define MATH_GEN_UV
#include "TriangleMesh_IntersectRay_AVX.inl"
#endif