00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGFaceIterator.h"
00049
00050 OSG_USING_NAMESPACE
00051
00052
00053
00054
00055
00056
00067 #if !defined(OSG_DO_DOC) || defined(OSG_DOC_DEV)
00068
00090 #endif // only include in dev docs
00091
00092
00093
00094
00095
00096
00097 char FaceIterator::cvsid[] = "@(#)$Id: OSGFaceIterator.cpp,v 1.13 2001/11/01 09:03:28 vossg Exp $";
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 FaceIterator::FaceIterator(void) : PrimitiveIterator(),
00110 _faceIndex(0), _actPrimIndex(), _facePntIndex()
00111 {
00112 }
00113
00114 FaceIterator::FaceIterator(const FaceIterator &source) :
00115 PrimitiveIterator(source),
00116 _faceIndex(source._faceIndex),
00117 _actPrimIndex(source._actPrimIndex),
00118 _facePntIndex()
00119 {
00120 _facePntIndex[0] = source._facePntIndex[0];
00121 _facePntIndex[1] = source._facePntIndex[1];
00122 _facePntIndex[2] = source._facePntIndex[2];
00123 _facePntIndex[3] = source._facePntIndex[3];
00124 }
00125
00131 FaceIterator::FaceIterator(const NodePtr& geo) : PrimitiveIterator(),
00132 _faceIndex(0), _actPrimIndex(), _facePntIndex()
00133 {
00134 setGeo(geo);
00135 }
00136
00142 FaceIterator::FaceIterator(const GeometryPtr& geo) : PrimitiveIterator(),
00143 _faceIndex(0), _actPrimIndex(), _facePntIndex()
00144 {
00145 setGeo(geo);
00146 }
00147
00148
00149 FaceIterator::~FaceIterator(void)
00150 {
00151 }
00152
00153
00154
00164 void FaceIterator::operator++()
00165 {
00166
00167 if(isAtEnd())
00168 return;
00169
00170 ++_faceIndex;
00171
00172
00173 if(_actPrimIndex >= PrimitiveIterator::getLength())
00174 {
00175 ++(static_cast<PrimitiveIterator&>(*this));
00176
00177 startPrim();
00178
00179 return;
00180 }
00181
00182 switch(getType())
00183 {
00184 case GL_TRIANGLES: _facePntIndex[0] = _actPrimIndex++;
00185 _facePntIndex[1] = _actPrimIndex++;
00186 _facePntIndex[2] = _actPrimIndex++;
00187 _facePntIndex[3] = -1;
00188 break;
00189 case GL_QUAD_STRIP: _facePntIndex[0] = _facePntIndex[3];
00190 _facePntIndex[1] = _facePntIndex[2];
00191 _facePntIndex[3] = _actPrimIndex++;
00192 _facePntIndex[2] = _actPrimIndex++;
00193 break;
00194 case GL_TRIANGLE_STRIP: if(_actPrimIndex & 1)
00195 {
00196 _facePntIndex[0] = _facePntIndex[2];
00197 }
00198 else
00199 {
00200 _facePntIndex[1] = _facePntIndex[2];
00201 }
00202 _facePntIndex[2] = _actPrimIndex++;
00203
00204 if(getPositionIndex(0) == getPositionIndex(1) ||
00205 getPositionIndex(0) == getPositionIndex(2) ||
00206 getPositionIndex(1) == getPositionIndex(2))
00207 {
00208 --_faceIndex;
00209 ++(*this);
00210 }
00211
00212 break;
00213 case GL_POLYGON:
00214 case GL_TRIANGLE_FAN: _facePntIndex[1] = _facePntIndex[2];
00215 _facePntIndex[2] = _actPrimIndex++;
00216 break;
00217 case GL_QUADS: _facePntIndex[0] = _actPrimIndex++;
00218 _facePntIndex[1] = _actPrimIndex++;
00219 _facePntIndex[2] = _actPrimIndex++;
00220 _facePntIndex[3] = _actPrimIndex++;
00221 break;
00222 default: SWARNING << "FaceIterator::++: encountered "
00223 << "unknown primitive type "
00224 << getType()
00225 << ", ignoring!" << std::endl;
00226 startPrim();
00227 break;
00228 }
00229 }
00230
00231
00236 void FaceIterator::startPrim(void)
00237 {
00238
00239 if(isAtEnd())
00240 return;
00241
00242 _facePntIndex[0] = 0;
00243 _facePntIndex[1] = 1;
00244 _facePntIndex[2] = 2;
00245 _facePntIndex[3] = -1;
00246 _actPrimIndex = 3;
00247
00248
00249 while(! isAtEnd())
00250 {
00251 switch(getType())
00252 {
00253 case GL_POINTS:
00254 case GL_LINES:
00255 case GL_LINE_STRIP:
00256 case GL_LINE_LOOP:
00257 break;
00258 case GL_TRIANGLES:
00259 case GL_TRIANGLE_STRIP:
00260 case GL_TRIANGLE_FAN:
00261 if(PrimitiveIterator::getLength() >= 3)
00262 return;
00263 break;
00264 case GL_POLYGON: switch(PrimitiveIterator::getLength())
00265 {
00266 case 0:
00267 case 1:
00268 case 2:
00269 break;
00270 case 4:
00271 _facePntIndex[3] = _actPrimIndex++;
00272 return;
00273 default:
00274 return;
00275 }
00276 break;
00277 case GL_QUADS: if(PrimitiveIterator::getLength() >= 4)
00278 {
00279 _facePntIndex[3] = _actPrimIndex++;
00280 return;
00281 }
00282 break;
00283 case GL_QUAD_STRIP: if(PrimitiveIterator::getLength() >= 4)
00284 {
00285 _facePntIndex[3] = _facePntIndex[2];
00286 _facePntIndex[2] = _actPrimIndex++;
00287 return;
00288 }
00289 break;
00290 default: SWARNING << "FaceIterator::startPrim: encountered "
00291 << "unknown primitive type "
00292 << getType()
00293 << ", ignoring!" << std::endl;
00294 break;
00295 }
00296
00297 ++(static_cast<PrimitiveIterator&>(*this));
00298 }
00299 }
00300
00307 void FaceIterator::seek(Int32 index)
00308 {
00309 setToBegin();
00310
00311 while(getIndex() != index)
00312 ++(*this);
00313 }
00314
00319 void FaceIterator::setToBegin(void)
00320 {
00321 PrimitiveIterator::setToBegin();
00322 _faceIndex = 0;
00323 startPrim();
00324 }
00325
00330 void FaceIterator::setToEnd(void)
00331 {
00332 PrimitiveIterator::setToEnd();
00333 _actPrimIndex = 0;
00334 }
00335
00336
00337
00338 FaceIterator& FaceIterator::operator =(const FaceIterator &source)
00339 {
00340 if(this == &source)
00341 return *this;
00342
00343 *static_cast<Inherited *>(this) = source;
00344
00345 this->_faceIndex = source._faceIndex;
00346 this->_actPrimIndex = source._actPrimIndex;
00347 this->_facePntIndex[0] = source._facePntIndex[0];
00348 this->_facePntIndex[1] = source._facePntIndex[1];
00349 this->_facePntIndex[2] = source._facePntIndex[2];
00350 this->_facePntIndex[3] = source._facePntIndex[3];
00351
00352 return *this;
00353 }
00354
00355
00356
00357 bool FaceIterator::operator <(const FaceIterator &other) const
00358 {
00359 return
00360 (*static_cast<const Inherited *>(this) < other) ||
00361 ( (*static_cast<const Inherited *>(this) == other) &&
00362 _actPrimIndex < other._actPrimIndex);
00363 }
00364
00365 bool FaceIterator::operator ==(const FaceIterator &other) const
00366 {
00367 if(isAtEnd() && other.isAtEnd())
00368 return true;
00369
00370 if(isAtEnd() || other.isAtEnd())
00371 return false;
00372
00373 return
00374 (*static_cast<const Inherited *>(this) == other ) &&
00375 _actPrimIndex == other._actPrimIndex;
00376 }
00377
00378 bool FaceIterator::operator !=(const FaceIterator &other) const
00379 {
00380 return !(*this == other);
00381 }