-
Notifications
You must be signed in to change notification settings - Fork 0
/
csg.c
286 lines (251 loc) · 10.3 KB
/
csg.c
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
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "csg.h"
CSGSets *CSGPrimitiveSetsCreate() {
CSGSets *sets = (CSGSets *)calloc(1, sizeof(CSGSets));
sets->type = CSG_Sets;
sets->primitives = LinkedListCreate(NULL);
return sets;
}
CSGPrimitive *CSGPrimitiveSetsAppend(CSGSets *sets, CSGPrimitive *primitive) {
if (sets->primitives[0].ptr == NULL) {
sets->primitives[0].ptr = primitive;
} else {
LinkedListAppend(sets->primitives, primitive);
}
return primitive;
}
bool CSGPrimitiveSetsDestroy(CSGSets *sets) {
LinkedListDestroy(sets->primitives, true, NULL);
free(sets);
return true;
}
CSGTriangle *CSGPrimitiveTriangleCreate(Vector vertexes[3], Vector surfaceNormal) {
CSGTriangle *triangle = (CSGTriangle *)calloc(1, sizeof(CSGTriangle));
*triangle = (CSGTriangle){CSG_Triangle, .vertexes = {vertexes[0], vertexes[1], vertexes[2]}, .surfaceNormal = surfaceNormal};
return triangle;
}
CSGPlane *CSGPrimitivePlaneCreate(Vector vertexes[4], Vector surfaceNormal) {
CSGPlane *plane = (CSGPlane *)calloc(1, sizeof(CSGPlane));
*plane = (CSGPlane){CSG_Plane, .vertexes = {vertexes[0], vertexes[1], vertexes[2], vertexes[3]}, .surfaceNormal = surfaceNormal};
return plane;
}
CSGCube *CSGPrimitiveCubeCreate(Real size) {
CSGCube *cube = (CSGCube *)calloc(1, sizeof(CSGCube));
*cube = (CSGCube){CSG_Cube, size};
return cube;
}
CSGCylinder *CSGPrimitiveCylinderCreate(Real radius, Real height, uint64_t partition) {
if (partition < 3) {
#ifndef NDEBUG
fprintf(stderr, "%s: \"partition\" must be more than 3. (%ld >= 3) \n", __FUNCTION_NAME__, partition);
#endif
return NULL;
}
CSGCylinder *cylinder = (CSGCylinder *)calloc(1, sizeof(CSGCylinder));
*cylinder = (CSGCylinder){CSG_Cylinder, radius, height, partition};
return cylinder;
}
CSGTriangular *CSGPrimitiveTriangularCreate(Real radius, Real height, uint64_t partition) {
if (partition < 3) {
#ifndef NDEBUG
fprintf(stderr, "%s: \"partition\" must be more than 3. (%ld >= 3) \n", __FUNCTION_NAME__, partition);
#endif
return NULL;
}
CSGTriangular *triangular = (CSGCylinder *)calloc(1, sizeof(CSGCylinder));
*triangular = (CSGTriangular){CSG_Triangular, radius, height, partition};
return triangular;
}
CSGBall *CSGPrimitiveBallCreate(Real radius, uint64_t partition) {
if (partition < 3) {
#ifndef NDEBUG
fprintf(stderr, "%s: \"partition\" must be more than 3. (%ld >= 3) \n", __FUNCTION_NAME__, partition);
#endif
return NULL;
}
CSGBall *ball = (CSGBall *)calloc(1, sizeof(CSGBall));
*ball = (CSGBall){CSG_Ball, radius, partition};
return ball;
}
void CSGPrimitivePlaneReduce(CSGSets *sets, const CSGPlane *plane) {
Vector triangleVertex1[3] = {plane->vertexes[0], plane->vertexes[1], plane->vertexes[2]};
Vector triangleVertex2[3] = {plane->vertexes[2], plane->vertexes[3], plane->vertexes[0]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(triangleVertex1, plane->surfaceNormal));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(triangleVertex2, plane->surfaceNormal));
};
void CSGPrimitiveCubeReduce(CSGSets *sets, const CSGCube *cube) {
Real s = cube->size;
Vector bottomFace[4] = {V(0, 0, 0), V(s, 0, 0), V(s, 0, s), V(0, 0, s)};
Vector topFace[4] = {V(0, s, 0), V(0, s, s), V(s, s, s), V(s, s, 0)};
Vector rightSideFace[4] = {V(s, 0, 0), V(s, s, 0), V(s, s, s), V(s, 0, s)};
Vector leftSideFace[4] = {V(0, 0, s), V(0, s, s), V(0, s, 0), V(0, 0, 0)};
Vector frontFace[4] = {V(0, 0, s), V(s, 0, s), V(s, s, s), V(0, s, s)};
Vector backFace[4] = {V(0, 0, 0), V(0, s, 0), V(s, s, 0), V(s, 0, 0)};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(bottomFace, V(0, -1, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(topFace, V(0, 1, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(rightSideFace, V(1, 0, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(leftSideFace, V(-1, 0, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(frontFace, V(0, 0, 1)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(backFace, V(0, 0, -1)));
};
void CSGPrimitiveCylinderReduce(CSGSets *sets, const CSGCylinder *cylinder) {
uint64_t p = cylinder->partition;
Real h = cylinder->height;
Real r = cylinder->radius;
Vector bottomCenter = V0;
Vector topCenter = V(0, cylinder->height, 0);
Vector *bottoms = (Vector *)calloc(p, sizeof(Vector));
Vector *tops = (Vector *)calloc(p, sizeof(Vector));
for (uint64_t j = 0; j < p; ++j) {
Real x = 2 * M_PI * j / p;
Real _sin = sinl(x) * r, _cos = cosl(x) * r;
bottoms[j] = V(_sin, 0, _cos);
tops[j] = V(_sin, h, _cos);
}
for (uint64_t i = 0; i < p; ++i) {
Vector vb[3] = {bottoms[i], bottomCenter, bottoms[(i + 1) % p]};
Vector vt[3] = {tops[(i + 1) % p], topCenter, tops[i]};
Vector backFace[4] = {tops[(i + 1) % p], tops[i], bottoms[i], bottoms[(i + 1) % p]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(vb, V(0, -1, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(vt, V(0, 1, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(backFace, VectorTriangleNormal(backFace[0], backFace[1], backFace[2])));
}
free(tops);
free(bottoms);
};
void CSGPrimitiveTriangularReduce(CSGSets *sets, const CSGTriangular *triangular) {
uint64_t p = triangular->partition;
Real r = triangular->radius;
Vector bottomCenter = V0;
Vector topCenter = V(0, triangular->height, 0);
Vector *bottoms = (Vector *)calloc(p, sizeof(Vector));
for (uint64_t j = 0; j < p; ++j) {
Real x = 2 * M_PI * j / p;
Real _sin = sinl(x) * r, _cos = cosl(x) * r;
bottoms[j] = V(_sin, 0, _cos);
}
for (uint64_t i = 0; i < p; ++i) {
Vector vb[3] = {bottoms[i], bottomCenter, bottoms[(i + 1) % p]};
Vector backFace[3] = {bottoms[(i + 1) % p], topCenter, bottoms[i]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(vb, V(0, -1, 0)));
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(backFace, VectorTriangleNormal(backFace[0], backFace[1], backFace[2])));
}
free(bottoms);
};
void CSGPrimitiveBallReduce(CSGSets *sets, const CSGBall *ball) {
uint64_t p = ball->partition;
Real r = ball->radius;
Real h = r * 2;
uint64_t roundIndex;
Vector *bottoms = (Vector *)calloc(p, sizeof(Vector));
Vector *tops = (Vector *)calloc(p, sizeof(Vector));
for (uint64_t hemisphereIndex = 0; hemisphereIndex < p; ++hemisphereIndex) {
for (roundIndex = 0; roundIndex < p; ++roundIndex) {
Real x, y, z, rad, radSurface;
rad = 2 * M_PI * roundIndex / p;
radSurface = M_PI * hemisphereIndex / p;
x = sinl(rad) * sinl(radSurface) * r;
y = (cosl(radSurface) + 1) / 2 * h;
z = cosl(rad) * sinl(radSurface) * r;
tops[roundIndex] = V(x, y, z);
radSurface = M_PI * (Real)(hemisphereIndex + 1) / p;
x = sinl(rad) * sinl(radSurface) * r;
y = (cosl(radSurface) + 1) / 2 * h;
z = cosl(rad) * sinl(radSurface) * r;
bottoms[roundIndex] = V(x, y, z);
}
for (roundIndex = 0; roundIndex < p; ++roundIndex) {
// Calculating surface normal, four points must be on same plane.
if (hemisphereIndex == 0) { // top
Vector triangle[3] = {bottoms[(roundIndex + 1) % p], V(0, 2 * r, 0), bottoms[roundIndex]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(triangle, VectorTriangleNormal(triangle[0], triangle[1], triangle[2])));
} else if (hemisphereIndex == p - 1) { // bottom
Vector triangle[3] = {tops[(roundIndex + 1) % p], V(0, 0, 0), tops[roundIndex]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitiveTriangleCreate(triangle, VectorTriangleNormal(triangle[0], triangle[1], triangle[2])));
} else { // rest of ball polygons
Vector plain[4] = {tops[(roundIndex + 1) % p], tops[roundIndex], bottoms[roundIndex], bottoms[(roundIndex + 1) % p]};
CSGPrimitiveSetsAppend(sets, (CSGPrimitive *)CSGPrimitivePlaneCreate(plain, VectorTriangleNormal(plain[0], plain[1], plain[2])));
}
}
}
free(tops);
free(bottoms);
};
/**
*
* @param sets
* @param primitive
* @return Is CSGSets modified in this function?
*/
bool CSGPrimitiveReduce(CSGSets *sets, const CSGPrimitive *primitive) {
switch (primitive->type) {
case CSG_Plane:
CSGPrimitivePlaneReduce(sets, (const CSGPlane *)primitive);
break;
case CSG_Cube:
CSGPrimitiveCubeReduce(sets, (const CSGCube *)primitive);
break;
case CSG_Cylinder:
CSGPrimitiveCylinderReduce(sets, (const CSGCylinder *)primitive);
break;
case CSG_Triangular:
CSGPrimitiveTriangularReduce(sets, (const CSGTriangular *)primitive);
break;
case CSG_Ball:
CSGPrimitiveBallReduce(sets, (const CSGBall *)primitive);
break;
case CSG_Sets:
case CSG_Triangle:
return false;
}
return true;
}
// FIXME: unnecessary loop
void CSGPrimitiveSetsReduce(CSGSets *sets) {
LinkedList *prev = NULL, *cur = sets->primitives;
while (cur != NULL) {
if (CSGPrimitiveReduce(sets, cur->ptr) == true) {
LinkedList *next = cur->next;
// free reduced primitive
free(cur->ptr);
if (next != NULL) {
// copy next node
cur->ptr = next->ptr;
cur->next = next->next;
// free (old) next node
free(next);
// decrement root length
--sets->primitives->length;
} else if (prev != NULL) {
--sets->primitives->length;
prev->next = NULL;
}
cur = sets->primitives;
} else {
prev = cur;
cur = cur->next;
}
}
}
Polygon *CSGPrimitiveSetsPolygon(const CSGSets *sets) {
uint64_t triangle = sets->primitives->length;
Polygon *polygon = (Polygon *)calloc(1, sizeof(Polygon));
polygon->triangle = triangle;
polygon->triangles = (Triangle *)calloc(triangle, sizeof(Triangle));
uint64_t triangleIndex = 0;
LinkedList *cur = sets->primitives;
while (cur != NULL) {
CSGTriangle *s = cur->ptr;
assert(s->type == CSG_Triangle);
Triangle *d = &polygon->triangles[triangleIndex];
memcpy(d->vertexes, s->vertexes, sizeof(Vector) * 3);
d->surfaceNormal = s->surfaceNormal;
++triangleIndex;
cur = cur->next;
}
return polygon;
}