-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcsUnitTest.cpp
234 lines (205 loc) · 7.31 KB
/
lcsUnitTest.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
/**********************************************
File : lcsUnitTest.cpp
Author : Mingcheng Chen
Last Update : October 23rd, 2013
***********************************************/
#include "lcsUnitTest.h"
#include "lcsUtility.h"
////////////////////////////////////////////////
bool CheckPlane(const lcs::Vector &p1, const lcs::Vector &p2, const lcs::Vector &p3,
const lcs::Tetrahedron &tetrahedron,
double localMinX, double localMinY, double localMinZ,
double blockSize, double epsilon) {
/// DEBUG ///
//printf("check plane = %.20lf\n", Cross(p1 - p2, p1 - p3).Length());
if (!lcs::Sign(Cross(p1 - p2, p1 - p3).Length(), 100 * epsilon)) return false;
bool tetPos = 0, tetNeg = 0, blkPos = 0, blkNeg = 0;
for (int i = 0; i < 4; i++) {
lcs::Vector point = tetrahedron.GetVertex(i);
double directedVolume = lcs::Mixed(p1 - point, p2 - point, p3 - point);
/// DEBUG ///
//printf("%.20lf\n", directedVolume);
if (lcs::Sign(directedVolume, epsilon) < 0) tetNeg = 1;
if (lcs::Sign(directedVolume, epsilon) > 0) tetPos = 1;
if (tetNeg * tetPos) return false;
}
for (int i = 0; i < 8; i++) {
lcs::Vector point(localMinX, localMinY, localMinZ);
if (i & 1) point.SetX(point.GetX() + blockSize);
if (i & 2) point.SetY(point.GetY() + blockSize);
if (i & 4) point.SetZ(point.GetZ() + blockSize);
double directedVolume = lcs::Mixed(p1 - point, p2 - point, p3 - point);
/// DEBUG ///
//printf("%.20lf\n", directedVolume);
if (lcs::Sign(directedVolume, epsilon) < 0) blkNeg = 1;
if (lcs::Sign(directedVolume, epsilon) > 0) blkPos = 1;
if (blkNeg * blkPos) return false;
}
if (tetNeg && blkNeg || tetPos && blkPos) return false;
if (tetNeg + tetPos == 0 || blkNeg + blkPos == 0) {
printf("Special Case!!!\n");
return false;
}
return true;
}
bool TetrahedronContainsPoint(const lcs::Tetrahedron &tet, const lcs::Vector &pt, double eps) {
double coordinates[4];
tet.CalculateNaturalCoordinates(pt, coordinates);
for (int i = 0; i < 4; i++)
if (lcs::Sign(coordinates[i], eps) < 0) return false;
return true;
}
////////////////////////////////////////////////
void lcs::UnitTestForTetBlkIntersection(lcs::TetrahedralGrid *grid, double blockSize,
double globalMinX, double globalMinY, double globalMinZ,
int numOfBlocksInY, int numOfBlocksInZ,
int *queryTetrahedron, int *queryBlock,
char *queryResults,
int numOfQueries,
double epsilon) {
printf("Unit test for tetrahedron-block intersection ... ");
for (int i = 0; i < numOfQueries; i++) {
int tetID = queryTetrahedron[i];
int blkID = queryBlock[i];
/*
if (tetID == 6825504 && blkID == 33) {
printf("tetID = %d, blkID = %d\n", tetID, blkID);
} else continue;
*/
lcs::Tetrahedron tet = grid->GetTetrahedron(tetID);
/// DEBUG ///
tet.Output();
int x, y, z;
z = blkID % numOfBlocksInZ;
int temp = blkID / numOfBlocksInZ;
y = temp % numOfBlocksInY;
x = temp / numOfBlocksInY;
double localMinX = globalMinX + x * blockSize;
double localMinY = globalMinY + y * blockSize;
double localMinZ = globalMinZ + z * blockSize;
printf("%lf, %lf, %lf: %lf\n", localMinX, localMinY, localMinZ, blockSize);
// Test tetrahedral edge and block point
bool flag = 0;
for (int tetEdgeID = 0; !flag && tetEdgeID < 6; tetEdgeID++) {
Vector p1, p2;
switch (tetEdgeID) {
case 0: {
p1 = tet.GetVertex(0);
p2 = tet.GetVertex(1);
} break;
case 1: {
p1 = tet.GetVertex(0);
p2 = tet.GetVertex(2);
} break;
case 2: {
p1 = tet.GetVertex(0);
p2 = tet.GetVertex(3);
} break;
case 3: {
p1 = tet.GetVertex(1);
p2 = tet.GetVertex(2);
} break;
case 4: {
p1 = tet.GetVertex(1);
p2 = tet.GetVertex(3);
} break;
case 5: {
p1 = tet.GetVertex(2);
p2 = tet.GetVertex(3);
} break;
}
for (int dx = 0; !flag && dx <= 1; dx++)
for (int dy = 0; !flag && dy <= 1; dy++)
for (int dz = 0; dz <= 1; dz++) {
Vector p3 = Vector(localMinX, localMinY, localMinZ) + Vector(dx, dy, dz) * blockSize;
if (CheckPlane(p1, p2, p3, tet, localMinX, localMinY, localMinZ, blockSize, epsilon)) {
flag = 1;
p1.Output();
p2.Output();
p3.Output();
printf("\n");
printf("tetrahedral edge and block point: %d, %d %d %d\n", tetEdgeID, dx, dy, dz);
break;
}
}
}
// Test tetrahedral point and block edge
for (int x1 = 0; !flag && x1 <= 1; x1++)
for (int y1 = 0; !flag && y1 <= 1; y1++)
for (int z1 = 0; !flag && z1 <= 1; z1++) {
Vector p1(localMinX + x1 * blockSize, localMinY + y1 * blockSize, localMinZ + z1 *blockSize);
for (int k = 0; !flag && k < 3; k++) {
int x2 = x1, y2 = y1, z2 = z1;
if (k == 0)
if (x1 == 0) x2++;
else continue;
if (k == 1)
if (y1 == 0) y2++;
else continue;
if (k == 2)
if (z1 == 0) z2++;
else continue;
Vector p2(localMinX + x2 * blockSize, localMinY + y2 * blockSize, localMinZ + z2 * blockSize);
for (int j = 0; j < 4; j++) {
Vector p3 = tet.GetVertex(j);
if (CheckPlane(p1, p2, p3, tet, localMinX, localMinY, localMinZ, blockSize, epsilon)) {
flag = 1;
break;
}
}
}
}
bool result = !flag;
if (result != queryResults[i]) {
char error[100];
sprintf(error, "Query %d has incorrect result.\ntet = %d, blk = %d\nkernel result: %d, CPU result: %d",
i + 1, queryTetrahedron[i], queryBlock[i], queryResults[i], result);
lcs::Error(error);
}
}
printf("Passed\n");
}
void lcs::UnitTestForInitialCellLocations(lcs::TetrahedralGrid *grid,
int xRes, int yRes, int zRes,
double minX, double minY, double minZ,
double dx, double dy, double dz,
int *initialCellLocations,
double epsilon) {
printf("Unit test for initial cell location ");
int numOfGridPoints = (xRes + 1) * (yRes + 1) * (zRes + 1);
int numOfMinusOnes = 0, numOfLocations = 0;
for (int i = 0; i < numOfGridPoints; i++)
if (initialCellLocations[i] == -1) numOfMinusOnes++;
else numOfLocations++;
printf("(outside: %d, inside: %d) ... ", numOfMinusOnes, numOfLocations);
int idx = -1;
for (int i = 0; i <= xRes; i++)
for (int j = 0; j <= yRes; j++)
for (int k = 0; k <= zRes; k++) {
idx++;
lcs::Vector point(minX + dx * i, minY + dy * j, minZ + dz * k);
if (initialCellLocations[idx] != -1) {
lcs::Tetrahedron tet = grid->GetTetrahedron(initialCellLocations[idx]);
if (!TetrahedronContainsPoint(tet, point, epsilon)) {
char str[100];
sprintf(str, "Tetrahedron %d does not contain grid point %d (%d, %d, %d)",
initialCellLocations[idx] + 1, idx, i, j, k);
lcs::Error(str);
}
continue;
}
/// DEBUG ///
/*
for (int tetID = 0; tetID < grid->GetNumOfCells(); tetID++) {
lcs::Tetrahedron tet = grid->GetTetrahedron(tetID);
if (TetrahedronContainsPoint(tet, point, epsilon)) {
char str[100];
sprintf(str, "Tetrahedron %d contains grid point %d (%d, %d, %d)",
tetID, idx, i, j, k);
lcs::Error(str);
}
}
*/
}
printf("Passed\n");
}