ldtypes.cpp

changeset 62
915fc477cb6a
parent 57
6e89589f1fe8
child 63
aa40ce18f869
equal deleted inserted replaced
61:109b07334fa0 62:915fc477cb6a
104 104
105 LDVertex::LDVertex () { 105 LDVertex::LDVertex () {
106 commonInit (); 106 commonInit ();
107 } 107 }
108 108
109 // =============================================================================
110 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
111 // =============================================================================
109 ulong LDObject::getIndex () { 112 ulong LDObject::getIndex () {
110 if (!g_CurrentFile) 113 if (!g_CurrentFile)
111 return -1u; 114 return -1u;
112 115
113 // TODO: shouldn't rely on g_CurrentFile 116 // TODO: shouldn't rely on g_CurrentFile
181 184
182 str LDEmpty::getContents () { 185 str LDEmpty::getContents () {
183 return str (); 186 return str ();
184 } 187 }
185 188
189 // =============================================================================
190 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
191 // =============================================================================
186 void LDQuad::splitToTriangles () { 192 void LDQuad::splitToTriangles () {
187 // Find the index of this quad 193 // Find the index of this quad
188 ulong ulIndex; 194 ulong ulIndex;
189 for (ulIndex = 0; ulIndex < g_CurrentFile->objects.size(); ++ulIndex) 195 for (ulIndex = 0; ulIndex < g_CurrentFile->objects.size(); ++ulIndex)
190 if (g_CurrentFile->objects[ulIndex] == this) 196 if (g_CurrentFile->objects[ulIndex] == this)
225 231
226 // Delete this quad now, it has been split. 232 // Delete this quad now, it has been split.
227 delete this; 233 delete this;
228 } 234 }
229 235
236 // =============================================================================
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
238 // =============================================================================
230 void LDObject::replace (LDObject* replacement) { 239 void LDObject::replace (LDObject* replacement) {
231 // Replace all instances of the old object with the new object 240 // Replace all instances of the old object with the new object
232 for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) { 241 for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
233 if (g_CurrentFile->objects[i] == this) 242 if (g_CurrentFile->objects[i] == this)
234 g_CurrentFile->objects[i] = replacement; 243 g_CurrentFile->objects[i] = replacement;
253 LDLine::~LDLine () {} 262 LDLine::~LDLine () {}
254 LDQuad::~LDQuad () {} 263 LDQuad::~LDQuad () {}
255 LDSubfile::~LDSubfile () {} 264 LDSubfile::~LDSubfile () {}
256 LDTriangle::~LDTriangle () {} 265 LDTriangle::~LDTriangle () {}
257 LDVertex::~LDVertex () {} 266 LDVertex::~LDVertex () {}
267
268 #define ADD_TYPE(T,N) \
269 case OBJ_##T: \
270 { \
271 LD##T* newobj = static_cast<LD##T*> (obj)->makeClone (); \
272 for (short i = 0; i < N; ++i) \
273 newobj->vaCoords[i].transform (matrix, pos); \
274 \
275 objs.push_back (newobj); \
276 } \
277 break;
278
279 // =============================================================================
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
281 // =============================================================================
282 static uint g_uTabs = 0;
283 vector<LDObject*> LDSubfile::inlineContents (double* matrix, vertex pos, bool bCache) {
284 // If we have this cached, just return that.
285 if (objCache.size ())
286 return objCache;
287
288 vector<LDObject*> objs;
289
290 for (ulong i = 0; i < pFile->objects.size(); ++i) {
291 LDObject* obj = pFile->objects[i];
292
293 switch (obj->getType()) {
294 case OBJ_Comment:
295 case OBJ_Empty:
296 case OBJ_Gibberish:
297 case OBJ_Unidentified:
298 case OBJ_Vertex:
299 break; // Skip non-essentials
300
301 ADD_TYPE (Line, 2)
302 ADD_TYPE (Triangle, 3)
303 ADD_TYPE (Quad, 4)
304 ADD_TYPE (CondLine, 4)
305
306 case OBJ_Subfile:
307 // Got another sub-file reference, inline it.
308 LDSubfile* ref = static_cast<LDSubfile*> (obj);
309
310 double faNewMatrix[9];
311
312 for (short i = 0; i < 9; ++i)
313 faNewMatrix[i] = matrix[i] * ref->faMatrix[i];
314
315 vertex vNewPos = ref->vPosition;
316 vNewPos.transform (matrix, pos);
317
318 // Only cache immediate subfiles, this is not one. Yay recursion!
319 g_uTabs++;
320 vector<LDObject*> otherobjs = ref->inlineContents (faNewMatrix, vNewPos, false);
321 g_uTabs--;
322
323 for (ulong i = 0; i < otherobjs.size(); ++i)
324 objs.push_back (otherobjs[i]);
325 break;
326 }
327 }
328
329 // If we cache this stuff, keep it around
330 if (bCache)
331 objCache = objs;
332
333 return objs;
334 }

mercurial