00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00053 #include "../include/IPSA/TriangleExtractor.h"
00054 #include "../include/IPSA/VertexStorage.h"
00055
00056 #include <Inventor/SbLinear.h>
00057 #include <Inventor/SoPrimitiveVertex.h>
00058 #include <Inventor/actions/SoCallbackAction.h>
00059 #include <Inventor/nodes/SoCoordinate3.h>
00060 #include <Inventor/nodes/SoIndexedFaceSet.h>
00061 #include <Inventor/nodes/SoSeparator.h>
00062 #include <Inventor/nodes/SoTranslation.h>
00063
00064 #include <iostream>
00065
00072 TriangleExtractor::TriangleExtractor(SoNode* object )
00073 {
00074 this->result = new TriangleCallbackResult();
00075 this->ivModel = object;
00076 }
00077
00082 TriangleExtractor::~TriangleExtractor()
00083 {
00084 delete result;
00085 }
00086
00098 void TriangleExtractor::execute(const SbVec3f& transvec)
00099 {
00100
00101 SoSeparator* colModel = new SoSeparator;
00102 SoTranslation* t = new SoTranslation;
00103 t->translation.setValue(transvec);
00104 SoComplexity* complexity = new SoComplexity();
00105 complexity->value = 0.3f;
00106 colModel->addChild( t );
00107 colModel->addChild( complexity );
00108 colModel->addChild( ivModel );
00109 colModel->ref();
00110
00111 SoCallbackAction cbAction;
00112 cbAction.addTriangleCallback( SoShape::getClassTypeId(), &TriangleExtractor::TriangleCB, this->result );
00113 cbAction.apply( colModel );
00114
00115 colModel->unref();
00116 }
00117
00124 VertexStorage<odeVertexType>* TriangleExtractor::getResult()
00125 {
00126
00127
00128
00129 int indexCount = this->result->ifs->coordIndex.getNum() * 3 / 4;
00130 VertexStorage<odeVertexType>* newVertexStorage =
00131 new VertexStorage<odeVertexType>(this->result->coord3idx, indexCount);
00132
00133
00134 SoMFVec3f vertices = result->coord3->point;
00135 for (int i = 0; i < result->coord3idx; i++)
00136 {
00137 SbVec3f vertex = vertices[i];
00138 odeVertexType triangle;
00139 triangle[0] = vertex[0];
00140 triangle[1] = vertex[1];
00141 triangle[2] = vertex[2];
00142 newVertexStorage->appendVertex( triangle );
00143 }
00144
00145
00146 SoMFInt32 coordIndex = result->ifs->coordIndex;
00147 for (int i = 0; i < coordIndex.getNum(); i += 4)
00148 {
00149 newVertexStorage->appendVertexIndices( coordIndex[i + 0],
00150 coordIndex[i + 1],
00151 coordIndex[i + 2] );
00152 }
00153 return newVertexStorage;
00154 }
00155
00167 void TriangleExtractor::TriangleCB(void* data, SoCallbackAction* action,
00168 const SoPrimitiveVertex* v1,
00169 const SoPrimitiveVertex* v2,
00170 const SoPrimitiveVertex* v3)
00171 {
00172 TriangleCallbackResult* tcr = static_cast<TriangleCallbackResult*> (data);
00173 if (NULL == tcr)
00174 return;
00175
00176 SbMatrix mm = action->getModelMatrix();
00177 SbVec3f triangle[3];
00178
00179 if ( (v1->getPoint() == v2->getPoint()) ||
00180 (v1->getPoint() == v3->getPoint()) ||
00181 (v2->getPoint() == v3->getPoint()) )
00182 {
00183 printf("-- Vertex (%d) is degenerated, skipping.\n", tcr->coord3idx);
00184 return;
00185 }
00186
00187
00188
00189 mm.multVecMatrix(v1->getPoint(), triangle[0]);
00190 mm.multVecMatrix(v2->getPoint(), triangle[1]);
00191 mm.multVecMatrix(v3->getPoint(), triangle[2]);
00192
00193 int32_t indices[] = { 0, 0, 0, -1};
00194 for ( int i = 0; i < 3; i++ )
00195 {
00196
00197
00198 int index = tcr->coord3->point.find( triangle[i], TRUE );
00199 if ( index == -1 )
00200 {
00201 indices[i] = tcr->coord3idx;
00202 tcr->coord3idx++;
00203 }
00204 else
00205 {
00206 indices[i] = index;
00207 }
00208 }
00209
00210 int oldsize = tcr->ifs->coordIndex.getNum();
00211 tcr->ifs->coordIndex.setNum(oldsize + 4);
00212 tcr->ifs->coordIndex.setValues(oldsize, 4, indices);
00213
00214
00215
00216
00217
00218
00219 }