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
00044 #include "OSGConfig.h"
00045 #include "OSGLog.h"
00046 #include "OSGExtrusionGeometry.h"
00047
00048 #include <sstream>
00049
00050
00051
00052
00053 #define DEBUG_PRIMITIVE_STORE
00054
00055 OSG_BEGIN_NAMESPACE
00056
00057
00058
00059
00060
00061
00062
00063
00064 template<class type>
00065 inline bool ExtrusionSurface::vecless<type>::operator () (const type &a,
00066 const type &b) const
00067 {
00068 UInt32 i;
00069 bool ret = false;
00070 for(i = 0; i < type::_iSize; i++)
00071 {
00072 if(osgabs(a[i] - b[i]) < Eps)
00073 continue;
00074 if(a[i] > b[i])
00075 {
00076 ret = false;
00077 break;
00078 }
00079 ret = true;
00080 break;
00081 }
00082 return ret;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 inline ExtrusionSurface::Pnt3fConstItPair
00100 ExtrusionSurface::getPrevAndNextIt(const Pnt3fConstIt &pIt)
00101 {
00102 Pnt3fConstIt prevIt, nextIt;
00103 bool firstPoint = (pIt == _spine.begin()) ? true : false;
00104 bool lastPoint = (pIt == _spine.end() - 1) ? true : false;
00105
00106 if(!firstPoint && !lastPoint)
00107 {
00108 prevIt = pIt - 1;
00109 nextIt = pIt + 1;
00110 }
00111 else
00112 {
00113 if(_spineClosed)
00114 {
00115 prevIt = _spine.end() - 2;
00116 nextIt = _spine.begin() + 1;
00117 }
00118 else
00119 {
00120 if(firstPoint)
00121 {
00122 prevIt = _spine.begin();
00123 nextIt = _spine.begin() + 1;
00124 }
00125 else
00126 {
00127 prevIt = _spine.end() - 2;
00128 nextIt = _spine.end() - 1;
00129 }
00130 }
00131 }
00132 return Pnt3fConstItPair(prevIt, nextIt);
00133 }
00134
00135
00136
00137
00138
00139
00140 inline Vec3f ExtrusionSurface::calcNonUnitYAxis(const Pnt3fConstIt &pIt)
00141 {
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 Pnt3fConstItPair prevAndNextIt = getPrevAndNextIt(pIt);
00156 Vec3f yAxis = *(prevAndNextIt.second) - *(prevAndNextIt.first);
00157 return yAxis;
00158 }
00159
00160
00161
00162
00163
00164
00165 inline Vec3f ExtrusionSurface::calcNonUnitZAxis(const Pnt3fConstIt &pIt)
00166 {
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 Pnt3fConstItPair prevAndNextIt = getPrevAndNextIt(pIt);
00188 Vec3f pToNext, pToPrev;
00189 pToNext = *(prevAndNextIt.second) - *pIt;
00190 pToPrev = *(prevAndNextIt.first) - *pIt;
00191
00192
00193
00194 Vec3f zAxis = pToNext.cross(pToPrev);
00195 return zAxis;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205 inline Vec3f ExtrusionSurface::calcXAxis(const MatrixConstIt &mIt)
00206 {
00207 Vec3f yAxis((*mIt)[1]);
00208 Vec3f zAxis((*mIt)[2]);
00209
00210 Vec3f xAxis = yAxis.cross(zAxis);
00211 xAxis.normalize();
00212
00213 return xAxis;
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 template <class VectorTypeT>
00229 inline VectorTypeT
00230 apply4PtScheme(const typename std::vector<VectorTypeT>::const_iterator &a,
00231 const typename std::vector<VectorTypeT>::const_iterator &b,
00232 const typename std::vector<VectorTypeT>::const_iterator &c,
00233 const typename std::vector<VectorTypeT>::const_iterator &d)
00234 {
00235
00236 #ifdef DEBUG_SUBDIVISION
00237 SLOG << "4ptScheme: interior: "
00238 << "( " << *a << " ) "
00239 << "(_" << *b << "_) "
00240 << "( " << *c << " ) "
00241 << "( " << *d << " ) "
00242 << endl;
00243 #endif
00244
00245
00246 return (-1.f/16.f) * (*a)
00247 + ( 9.f/16.f) * (*b)
00248 + ( 9.f/16.f) * (*c)
00249 + (-1.f/16.f) * (*d);
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259 template <class VectorTypeT>
00260 inline VectorTypeT
00261 apply3PtScheme(const typename std::vector<VectorTypeT>::const_iterator &a,
00262 const typename std::vector<VectorTypeT>::const_iterator &b,
00263 const typename std::vector<VectorTypeT>::const_iterator &c)
00264 {
00265
00266 #ifdef DEBUG_SUBDIVISION
00267 SLOG << "3PtScheme: exterior"
00268 << "( " << *a << " ) "
00269 << "(_" << *b << "_) "
00270 << "( " << *c << " ) "
00271 << endl;
00272 #endif
00273
00274
00275 return ( 3.f/8.f) * (*a)
00276 + ( 6.f/8.f) * (*b)
00277 + (-1.f/8.f) * (*c);
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 inline Vec3f ExtrusionSurface::calcTriangleFaceNormal(const Pnt3f &a,
00293 const Pnt3f &b,
00294 const Pnt3f &c)
00295 {
00296 Vec3f d1 = b - a;
00297 Vec3f d2 = c - a;
00298 Vec3f faceNormal = _ccw ? d1.cross(d2) : d2.cross(d1);
00299
00300 return faceNormal;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310 inline Vec3f ExtrusionSurface::calcQuadFaceNormal(const Pnt3f &a,
00311 const Pnt3f &b,
00312 const Pnt3f &c,
00313 const Pnt3f &d)
00314 {
00315 Vec3f faceNormal = calcTriangleFaceNormal(a, b, d);
00316 if(faceNormal.length() > Eps)
00317 {
00318 faceNormal.normalize();
00319 return faceNormal;
00320 }
00321
00322 faceNormal = calcTriangleFaceNormal(b, c, a);
00323 if(faceNormal.length() > Eps)
00324 {
00325 faceNormal.normalize();
00326 return faceNormal;
00327 }
00328
00329 faceNormal = calcTriangleFaceNormal(c, d, b);
00330 if(faceNormal.length() > Eps)
00331 {
00332 faceNormal.normalize();
00333 return faceNormal;
00334 }
00335
00336
00337 return Vec3f(0.f, 0.f, 0.f);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 inline void ExtrusionSurface::calcVertexNormal(Vertex *vPtr, UInt32 quadrant)
00355 {
00356 assert(quadrant < 4);
00357
00358 Vec3f refFN = vPtr->adjFaceNormals[quadrant];
00359
00360
00361 vPtr->normal = refFN;
00362
00363 if(_creaseAngle < Eps)
00364 {
00365
00366 return;
00367 }
00368
00369
00370 for(UInt32 k = 0; k < 4; k++)
00371 {
00372 if(k != quadrant)
00373 {
00374 Vec3f fN = vPtr->adjFaceNormals[k];
00375 if(refFN.enclosedAngle(fN) - Eps < _creaseAngle + Eps)
00376 {
00377 vPtr->normal += fN;
00378 }
00379 }
00380 }
00381
00382 vPtr->normal.normalize();
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 inline void ExtrusionSurface::storeVertex(const Vertex &vertex,
00405 const GeoIndicesUI32Ptr indicesPtr)
00406 {
00407 assert(indicesPtr != NullFC);
00408
00409 #ifdef DEBUG_VERTEX_STORE
00410 std::ostringstream strout;
00411 strout << "OSGExtrusion:storeVertex:Storing:" << std::endl;
00412 strout << "\t[ (Pos: " << vertex.position << ")" << std::endl;
00413 #endif
00414
00415
00416 store(vertex.position, _posMap, indicesPtr);
00417
00418 if(_createNormals)
00419 {
00420 #ifdef DEBUG_VERTEX_STORE
00421 strout << "\t (Normal:" << vertex.normal << ")" << std::endl;
00422 #endif
00423
00424 store(vertex.normal, _normalMap, indicesPtr);
00425 }
00426
00427 if(_createTexCoords)
00428 {
00429 #ifdef DEBUG_VERTEX_STORE
00430 strout << "\t (TexCoord:" << vertex.texCoord << ")" << std::endl;
00431 #endif
00432
00433 store(vertex.texCoord, _texCoordMap, indicesPtr);
00434 }
00435
00436
00437 _vertexCount++;
00438
00439 #ifdef DEBUG_VERTEX_STORE
00440 strout << "\t (vc:" << _vertexCount << "/tvc:" << _totalVertexCount << ")]";
00441 SLOG << strout.str() << std::endl;
00442 #endif
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452 inline void ExtrusionSurface::storePrimitive(GLenum type,
00453 GeoPLengthsUI32Ptr lensPtr,
00454 GeoPTypesUI8Ptr typesPtr)
00455 {
00456 assert(lensPtr != NullFC);
00457 assert(typesPtr != NullFC);
00458
00459 #ifdef DEBUG_PRIMITIVE_STORE
00460 switch(type)
00461 {
00462 case GL_TRIANGLE_STRIP:
00463 assert(_vertexCount > 2);
00464 assert(_vertexCount % 2 == 0);
00465 break;
00466
00467 case GL_TRIANGLE_FAN:
00468 assert(_vertexCount > 2);
00469 break;
00470
00471 case GL_QUADS:
00472 assert(_vertexCount == 4);
00473 break;
00474
00475 case GL_LINES:
00476 assert(_vertexCount == 2);
00477 break;
00478 };
00479 #endif
00480
00481 if(_vertexCount != 0)
00482 {
00483 lensPtr->push_back(_vertexCount);
00484 typesPtr->push_back(type);
00485 _totalVertexCount += _vertexCount;
00486
00487
00488 _vertexCount = 0;
00489
00490
00491 _primitiveCount++;
00492 }
00493 else
00494 {
00495 FFATAL(("OSGExtrusion::storePrimitive: Tried to store empty primitive\n"));
00496 }
00497 }
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 template <typename PType>
00509 inline UInt32
00510 ExtrusionSurface::store(PType property,
00511 std::map<PType, UInt32, vecless<PType> > &pIndexMap)
00512 {
00513 typedef typename std::map<PType,
00514 UInt32,
00515 vecless<PType> >::iterator PTypeMapIterator;
00516 typedef typename std::map<PType,
00517 UInt32,
00518 vecless<PType> >::value_type PTypeMapValueType;
00519
00520 UInt32 newIndex = pIndexMap.size();
00521 const std::pair<PTypeMapIterator, bool>& result
00522 = pIndexMap.insert(PTypeMapValueType(property, newIndex));
00523
00524 #ifdef DEBUG_PROPERTY_STORE
00525 std::ostringstream strout;
00526 strout << "OSGExtrusion:store: Storing property:" << std::endl;
00527 strout << "\t"
00528 << result.first->first << " -> " << result.first->second
00529 << " (new insert: " << (result.second ? "TRUE" : "FALSE") << ")";
00530 SLOG << strout.str() << std::endl;
00531 #endif
00532
00533
00534 return result.first->second;
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544 template <typename PType>
00545 inline void
00546 ExtrusionSurface::store(PType property,
00547 std::map<PType, UInt32, vecless<PType> > &propertyIndexMap,
00548 GeoIndicesUI32Ptr indicesPtr)
00549 {
00550 assert(indicesPtr != NullFC);
00551 indicesPtr->push_back(store(property, propertyIndexMap));
00552 }
00553
00554
00555 OSG_END_NAMESPACE
00556
00557
00558 #define OSGEXTRUSIONGEOMETRY_INLINE_CVSID "@(#)$Id: OSGExtrusionGeometry.inl,v 1.2 2005/06/02 14:32:16 vossg Exp $"
00559
00560
00561 #ifdef DEBUG_SUBDIVISION
00562 #undef DEBUG_SUBDIVISION
00563 #endif
00564
00565 #ifdef DEBUG_VERTEX_STORE
00566 #undef DEBUG_VERTEX_STORE
00567 #endif
00568
00569 #ifdef DEBUG_PROPERTY_STORE
00570 #undef DEBUG_PROPERTY_STORE
00571 #endif
00572
00573 #ifdef DEBUG_PRIMITIVE_STORE
00574 #undef DEBUG_PRIMITIVE_STORE
00575 #endif