-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiritSkel.cpp
executable file
·263 lines (231 loc) · 12.4 KB
/
iritSkel.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
#include "vox_iritSkel.h"
#include "vox_math.h"
#include "vox_types.h"
#include "voxelizer.h"
#include <array>
#include <vector>
/*****************************************************************************
* Skeleton for an interface to a parser to read IRIT data files. *
******************************************************************************
* (C) Gershon Elber, Technion, Israel Institute of Technology *
******************************************************************************
* Written by: Gershon Elber Ver 1.0, Feb 2002 * Minimal changes made by Amit
*Mano November 2008 *
******************************************************************************/
IPFreeformConvStateStruct CGSkelFFCState = { FALSE, /* Talkative */
FALSE, /* DumpObjsAsPolylines */
TRUE, /* DrawFFGeom */
FALSE, /* DrawFFMesh */
{ 10, 10, 10 }, /* 10 isocurves peru/v/w direction. */
100, /* 100 point samples along a curve. */
SYMB_CRV_APPROX_UNIFORM, /* CrvApproxMethod */
FALSE, /* ShowIntrnal */
FALSE, /* CubicCrvsAprox */
20, /* Polygonal FineNess */
FALSE, /* ComputeUV */
TRUE, /* ComputeNrml */
FALSE, /* FourPerFlat */
0, /* OptimalPolygons */
FALSE, /* BBoxGrid */
TRUE, /* LinearOnePolyFlag */
FALSE };
// CGSkelProcessIritDataFiles(argv + 1, argc - 1);
/*****************************************************************************
* DESCRIPTION: *
* Main module of skeleton - Read command line and do what is needed... *
* *
* PARAMETERS: *
* FileNames: Files to open and read, as a vector of strings. *
* NumFiles: Length of the FileNames vector.
**
* *
* RETURN VALUE: *
* bool: false - fail, true - success. *
*****************************************************************************/
bool
CGSkelProcessIritDataFiles(const char* file_name)
{
IPObjectStruct* PObjects;
IrtHmgnMatType CrntViewMat;
/* Get the data files: */
IPSetFlattenObjects(FALSE);
if ((PObjects = IPGetDataFiles(&file_name, 1 /*NumFiles*/, TRUE, FALSE)) == NULL) return false;
PObjects = IPResolveInstances(PObjects);
if (IPWasPrspMat)
MatMultTwo4by4(CrntViewMat, IPViewMat, IPPrspMat);
else
IRIT_GEN_COPY(CrntViewMat, IPViewMat, sizeof(IrtHmgnMatType));
/* Here some useful parameters to play with in tesselating freeforms: */
CGSkelFFCState.FineNess = 20; /* Res. of tesselation, larger is finer. */
CGSkelFFCState.FourPerFlat = TRUE; /* 4 poly per ~flat patch, 2 otherwise.*/
CGSkelFFCState.LinearOnePolyFlag = TRUE; /* Linear srf gen. one poly. */
/* Traverse ALL the parsed data, recursively. */
IPTraverseObjListHierarchy(PObjects, CrntViewMat, CGSkelDumpOneTraversedObject);
return true;
}
/*****************************************************************************
* DESCRIPTION: *
* Call back function of IPTraverseObjListHierarchy. Called on every non *
* list object found in hierarchy. *
* *
* PARAMETERS: *
* PObj: Non list object to handle. *
* Mat: Transformation matrix to apply to this object. *
* Data: Additional data. *
* *
* RETURN VALUE: *
* void *
*****************************************************************************/
void
CGSkelDumpOneTraversedObject(IPObjectStruct* PObj, IrtHmgnMatType Mat, void* Data)
{
IPObjectStruct* PObjs;
if (IP_IS_FFGEOM_OBJ(PObj))
PObjs = IPConvertFreeForm(PObj, &CGSkelFFCState);
else
PObjs = PObj;
for (PObj = PObjs; PObj != NULL; PObj = PObj->Pnext)
if (!CGSkelStoreData(PObj)) exit(1);
}
using Triangle = std::array<std::array<double, 3>, 3>;
void
save_triangles_convex(std::vector<Triangle>& triangles, IPPolygonStruct* poly)
{
extern double scene_aabb_min[3];
extern double scene_aabb_max[3];
IPVertexStruct* PVertex;
Triangle t;
int vi = 0;
PVertex = poly->PVertex;
do {
for (int j = 0; j < 3; j++) {
double x = PVertex->Coord[j];
scene_aabb_max[j] = std::max(scene_aabb_max[j], x);
scene_aabb_min[j] = std::min(scene_aabb_min[j], x);
if (vi < 3)
t[vi][j] = x;
else {
t[1][j] = t[2][j];
t[2][j] = x;
}
}
vi++;
if (vi >= 3) triangles.push_back(t);
PVertex = PVertex->Pnext;
} while (PVertex != poly->PVertex && PVertex != NULL);
}
extern double GMPolyObjectVolume(IPObjectStruct* PObj);
extern void GMVrtxListToCircOrLin(IPPolygonStruct* Pls, int DoCirc);
bool
CGSkelStoreData(IPObjectStruct* PObj)
{
extern std::vector<std::vector<Triangle>> meshes;
extern Voxelizer::FillType fill;
extern std::vector<unsigned char> is_flipped;
if (PObj->ObjType != IP_OBJ_POLY) return true;
meshes.push_back({});
size_t mesh_idx = meshes.size() - 1;
std::vector<Triangle>& mesh = meshes[mesh_idx];
IPPolygonStruct* poly = PObj->U.Pl;
IPSetPolyListCirc(TRUE);
GMVrtxListToCircOrLin(poly, 1);
is_flipped.push_back(GMPolyObjectVolume(PObj) > 0.0 ? 1 : 0);
IPSetPolyListCirc(FALSE);
GMVrtxListToCircOrLin(poly, 0);
for (; poly != NULL; poly = poly->Pnext) {
if (poly->PVertex == NULL) return false;
save_triangles_convex(mesh, poly);
}
return true;
}
/*****************************************************************************
* DESCRIPTION: *
* Returns the color of an object. *
* *
* PARAMETERS: *
* PObj: Object to get its color. *
* RGB: as 3 floats in the domain [0, 1]. *
* *
* RETURN VALUE: *
* int: TRUE if object has color, FALSE otherwise. *
*****************************************************************************/
int
CGSkelGetObjectColor(IPObjectStruct* PObj, double RGB[3])
{
static int TransColorTable[][4] = {
{ /* BLACK */ 0, 0, 0, 0 }, { /* BLUE */ 1, 0, 0, 255 },
{ /* GREEN */ 2, 0, 255, 0 }, { /* CYAN */ 3, 0, 255, 255 },
{ /* RED */ 4, 255, 0, 0 }, { /* MAGENTA */ 5, 255, 0, 255 },
{ /* BROWN */ 6, 50, 0, 0 }, { /* LIGHTGRAY */ 7, 127, 127, 127 },
{ /* DARKGRAY */ 8, 63, 63, 63 }, { /* LIGHTBLUE */ 9, 0, 0, 255 },
{ /* LIGHTGREEN */ 10, 0, 255, 0 }, { /* LIGHTCYAN */ 11, 0, 255, 255 },
{ /* LIGHTRED */ 12, 255, 0, 0 }, { /* LIGHTMAGENTA */ 13, 255, 0, 255 },
{ /* YELLOW */ 14, 255, 255, 0 }, { /* WHITE */ 15, 255, 255, 255 },
{ /* BROWN */ 20, 50, 0, 0 }, { /* DARKGRAY */ 56, 63, 63, 63 },
{ /* LIGHTBLUE */ 57, 0, 0, 255 }, { /* LIGHTGREEN */ 58, 0, 255, 0 },
{ /* LIGHTCYAN */ 59, 0, 255, 255 }, { /* LIGHTRED */ 60, 255, 0, 0 },
{ /* LIGHTMAGENTA */ 61, 255, 0, 255 }, { /* YELLOW */ 62, 255, 255, 0 },
{ /* WHITE */ 63, 255, 255, 255 }, { -1, 0, 0, 0 }
};
int i, j, Color, RGBIColor[3];
if (AttrGetObjectRGBColor(PObj, &RGBIColor[0], &RGBIColor[1], &RGBIColor[2])) {
for (i = 0; i < 3; i++) RGB[i] = RGBIColor[i] / 255.0;
return TRUE;
}
else if ((Color = AttrGetObjectColor(PObj)) != IP_ATTR_NO_COLOR) {
for (i = 0; TransColorTable[i][0] >= 0; i++) {
if (TransColorTable[i][0] == Color) {
for (j = 0; j < 3; j++) RGB[j] = TransColorTable[i][j + 1] / 255.0;
return TRUE;
}
}
}
return FALSE;
}
/*****************************************************************************
* DESCRIPTION: *
* Returns the volumetric texture of an object, if any. *
* *
* PARAMETERS: *
* PObj: Object to get its volumetric texture. *
* *
* RETURN VALUE: *
* char *: Name of volumetric texture map to apply, NULL if none. *
*****************************************************************************/
const char*
CGSkelGetObjectTexture(IPObjectStruct* PObj)
{
return AttrGetObjectStrAttrib(PObj, "texture");
}
/*****************************************************************************
* DESCRIPTION: *
* Returns the parametric texture of an object, if any. *
* *
* PARAMETERS: *
* PObj: Object to get its parametric texture. *
* *
* RETURN VALUE: *
* char *: Name of parametric texture map to apply, NULL if none. *
*****************************************************************************/
const char*
CGSkelGetObjectPTexture(IPObjectStruct* PObj)
{
return AttrGetObjectStrAttrib(PObj, "ptexture");
}
/*****************************************************************************
* DESCRIPTION: *
* Returns the transparency level of an object, if any. *
* *
* PARAMETERS: *
* PObj: Object to get its volumetric texture. *
* Transp: Transparency level between zero and one. *
* *
* RETURN VALUE: *
* int: TRUE if object has transparency, FALSE otherwise. *
*****************************************************************************/
int
CGSkelGetObjectTransp(IPObjectStruct* PObj, double* Transp)
{
*Transp = AttrGetObjectRealAttrib(PObj, "transp");
return !IP_ATTR_IS_BAD_REAL(*Transp);
}