changed: gcc8 base update
[opensg.git] / Source / System / NodeCores / Drawables / Geometry / Util / OSGFastTriangleIterator.cpp
bloba83891d2d6e70f71c5b73f74bd893154a8497825
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2011 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
30 #include "OSGFastTriangleIterator.h"
32 OSG_BEGIN_NAMESPACE
34 FastTriangleIterator::FastTriangleIterator(void) :
35 Inherited (),
36 _triIndex (0),
37 _actPrimIndex(0),
38 _triPntIndex ()
40 _triPntIndex[0] = 0;
41 _triPntIndex[1] = 0;
42 _triPntIndex[2] = 0;
45 FastTriangleIterator::FastTriangleIterator(const FastTriangleIterator &source) :
46 Inherited (source ),
47 _triIndex (source._triIndex ),
48 _actPrimIndex(source._actPrimIndex),
49 _triPntIndex ()
51 _triPntIndex[0] = source._triPntIndex[0];
52 _triPntIndex[1] = source._triPntIndex[1];
53 _triPntIndex[2] = source._triPntIndex[2];
56 FastTriangleIterator::FastTriangleIterator(const Geometry *geo) :
57 Inherited (),
58 _triIndex (0),
59 _actPrimIndex(0),
60 _triPntIndex ()
62 _triPntIndex[0] = 0;
63 _triPntIndex[1] = 0;
64 _triPntIndex[2] = 0;
66 setGeo(geo);
69 FastTriangleIterator::FastTriangleIterator(const Node *node) :
70 Inherited (),
71 _triIndex (0),
72 _actPrimIndex(0),
73 _triPntIndex ()
75 _triPntIndex[0] = 0;
76 _triPntIndex[1] = 0;
77 _triPntIndex[2] = 0;
79 setGeo(node);
82 /* virtual */
83 FastTriangleIterator::~FastTriangleIterator(void)
87 /* virtual */ void
88 FastTriangleIterator::setToBegin(void)
90 Inherited::setToBegin();
91 _triIndex = 0;
93 startPrim();
96 /* virtual */ void
97 FastTriangleIterator::setToEnd(void)
99 Inherited::setToEnd();
100 _actPrimIndex = 0;
103 FastTriangleIterator &
104 FastTriangleIterator::operator++(void)
106 if(isAtEnd())
107 return *this;
109 ++_triIndex;
111 if(_actPrimIndex >= getLength())
113 ++(static_cast<Inherited&>(*this));
114 startPrim();
116 return *this;
119 switch(getType())
121 case GL_TRIANGLES:
122 _triPntIndex[0] = _actPrimIndex++;
123 _triPntIndex[1] = _actPrimIndex++;
124 _triPntIndex[2] = _actPrimIndex++;
125 break;
127 case GL_QUAD_STRIP:
128 case GL_TRIANGLE_STRIP:
130 if(_actPrimIndex & 1)
132 _triPntIndex[0] = _triPntIndex[2];
134 else
136 _triPntIndex[1] = _triPntIndex[2];
138 _triPntIndex[2] = _actPrimIndex++;
140 break;
142 case GL_POLYGON:
143 case GL_TRIANGLE_FAN:
144 _triPntIndex[1] = _triPntIndex[2];
145 _triPntIndex[2] = _actPrimIndex++;
146 break;
148 case GL_QUADS:
149 if(_actPrimIndex & 1)
151 _triPntIndex[1] = _triPntIndex[2];
152 _triPntIndex[2] = _actPrimIndex++;
154 else
156 _triPntIndex[0] = _actPrimIndex++;
157 _triPntIndex[1] = _actPrimIndex++;
158 _triPntIndex[2] = _actPrimIndex++;
160 break;
162 default:
164 SWARNING << "TriangleIterator::++: encountered "
165 << "unknown primitive type "
166 << getType() << ", ignoring!" << std::endl;
167 startPrim();
169 break;
172 return *this;
175 FastTriangleIterator &
176 FastTriangleIterator::seek(Int32 index)
178 if(index < getIndex())
179 setToBegin();
181 if(index > getIndex())
183 UInt32 tri = getTriInActPrim ();
184 UInt32 numTris = getTrisInActPrim();
186 if(tri >= numTris)
188 ++(static_cast<Inherited&>(*this));
189 startPrim();
191 tri = getTriInActPrim ();
192 numTris = getTrisInActPrim();
195 Int32 leftTris = numTris - tri - 1;
197 // skip whole primitives
198 while(index > _triIndex + leftTris)
200 _triIndex += leftTris + 1;
201 ++(static_cast<Inherited&>(*this));
202 startPrim();
204 tri = 0;
205 leftTris = getTrisInActPrim() - 1;
208 tri += index - getIndex();
209 _triIndex = index;
211 // setup members
212 if(!isAtEnd() && tri > 0)
214 switch(getType())
216 case GL_TRIANGLES:
217 _actPrimIndex = 3 * tri + 3;
218 _triPntIndex[0] = _actPrimIndex - 3;
219 _triPntIndex[1] = _actPrimIndex - 2;
220 _triPntIndex[2] = _actPrimIndex - 1;
221 break;
223 case GL_QUADS:
224 if(tri % 2)
226 _actPrimIndex = 2 * tri + 2;
227 _triPntIndex[0] = _actPrimIndex - 4;
228 _triPntIndex[1] = _actPrimIndex - 2;
229 _triPntIndex[2] = _actPrimIndex - 1;
231 else
233 _actPrimIndex = 2 * tri + 3;
234 _triPntIndex[0] = _actPrimIndex - 3;
235 _triPntIndex[1] = _actPrimIndex - 2;
236 _triPntIndex[2] = _actPrimIndex - 1;
238 break;
240 case GL_QUAD_STRIP:
241 case GL_TRIANGLE_STRIP:
242 if(tri % 2)
244 _actPrimIndex = tri + 3;
245 _triPntIndex[0] = _actPrimIndex - 2;
246 _triPntIndex[1] = _actPrimIndex - 3;
247 _triPntIndex[2] = _actPrimIndex - 1;
249 else
251 _actPrimIndex = tri + 3;
252 _triPntIndex[0] = _actPrimIndex - 3;
253 _triPntIndex[1] = _actPrimIndex - 2;
254 _triPntIndex[2] = _actPrimIndex - 1;
256 break;
258 case GL_TRIANGLE_FAN:
259 case GL_POLYGON:
260 _actPrimIndex = tri + 3;
261 _triPntIndex[0] = 0;
262 _triPntIndex[1] = _actPrimIndex - 2;
263 _triPntIndex[2] = _actPrimIndex - 1;
264 break;
266 default:
268 SWARNING << "FastTriangleIterator::seek: "
269 << "encountered unknown primitive type "
270 << getType() << ", ignoring!"
271 << std::endl;
273 break;
278 return *this;
281 void
282 FastTriangleIterator::startPrim(void)
284 if(isAtEnd())
285 return;
287 _triPntIndex[0] = 0;
288 _triPntIndex[1] = 1;
289 _triPntIndex[2] = 2;
290 _actPrimIndex = 3;
292 while(! isAtEnd())
294 switch(getType())
296 case GL_POINTS: // non-polygon types: ignored
297 case GL_LINES:
298 case GL_LINE_STRIP:
299 case GL_LINE_LOOP:
300 break;
302 case GL_TRIANGLES: // polygon types
303 case GL_TRIANGLE_STRIP:
304 case GL_TRIANGLE_FAN:
305 case GL_QUADS:
306 case GL_QUAD_STRIP:
307 case GL_POLYGON:
308 if(getLength() >= 3)
309 return;
310 break;
312 default:
314 SWARNING << "FastTriangleIterator::startPrim: "
315 << "encountered unknown primitive type "
316 << getType() << ", ignoring!"
317 << std::endl;
319 break;
322 ++(static_cast<Inherited&>(*this));
326 UInt32
327 FastTriangleIterator::getTrisInActPrim(void) const
329 UInt32 returnValue = 0;
331 switch(getType())
333 case GL_TRIANGLES:
334 assert(getLength() % 3 == 0);
335 returnValue = getLength() / 3;
336 break;
338 case GL_QUADS:
339 assert(getLength() % 4 == 0);
340 returnValue = getLength() / 2;
341 break;
343 case GL_QUAD_STRIP:
344 case GL_TRIANGLE_STRIP:
345 case GL_TRIANGLE_FAN:
346 case GL_POLYGON:
347 assert(getLength() >= 3);
348 returnValue = getLength() - 2;
349 break;
351 default:
353 SWARNING << "FastTriangleIterator::getTrisInActPrim: "
354 << "encountered unknown primitive type "
355 << getType() << ", ignoring!"
356 << std::endl;
358 break;
361 return returnValue;
364 UInt32
365 FastTriangleIterator::getTriInActPrim(void) const
367 UInt32 returnValue = 0;
369 switch(getType())
371 case GL_TRIANGLES:
372 assert(_actPrimIndex % 3 == 0);
373 returnValue = _actPrimIndex / 3 - 1;
374 break;
376 case GL_QUADS:
377 if(_actPrimIndex % 1)
379 returnValue = (_actPrimIndex - 3) / 2;
381 else
383 returnValue = (_actPrimIndex - 2) / 2;
385 break;
387 case GL_QUAD_STRIP:
388 case GL_TRIANGLE_STRIP:
389 case GL_TRIANGLE_FAN:
390 case GL_POLYGON:
391 returnValue = _actPrimIndex - 3;
392 break;
394 default:
396 SWARNING << "FastTriangleIterator::getTriInActPrim: "
397 << "encountered unknown primitive type "
398 << getType() << ", ignoring!"
399 << std::endl;
401 break;
404 return returnValue;
407 OSG_END_NAMESPACE