Changeset 1526 for GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs
- Timestamp:
- 09/28/06 17:49:37 (18 years ago)
- Location:
- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs
- Files:
-
- 15 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjModel.cxx
r1025 r1526 17 17 int Model::in_Vertex(const Vec3& p) 18 18 { 19 20 21 19 Vertex *v = newVertex(p[X], p[Y], p[Z]); 20 bounds.addPoint(p); 21 return vertCount() - 1; 22 22 } 23 23 … … 36 36 int Model::in_Face(int a, int b, int c, int n1, int n2, int n3, int t1, int t2, int t3) 37 37 { 38 39 40 41 42 38 Vertex *v1 = vertices(a-1); 39 Vertex *v2 = vertices(b-1); 40 Vertex *v3 = vertices(c-1); 41 42 Face *t = newFace(v1, v2, v3); 43 43 44 44 t->normals[0] = n1; … … 55 55 int Model::miin_Face(int a, int b, int c) 56 56 { 57 Vertex *v1 = vertices(a-1);58 Vertex *v2 = vertices(b-1);59 Vertex *v3 = vertices(c-1);60 61 57 Vertex *v1 = vertices(a); 58 Vertex *v2 = vertices(b); 59 Vertex *v3 = vertices(c); 60 61 Face *t = newFace(v1, v2, v3); 62 62 63 63 t->normals[0] = a; … … 75 75 int Model::in_FColor(const Vec3& c) 76 76 { 77 Face *f = faces(faces.length()-1); 78 79 if( !f->props ) 80 f->props = new FProp; 81 82 f->props->color = c; 83 return 0; 77 Face *f = faces(faces.length()-1); 78 79 if( !f->props ) 80 { 81 f->props = new FProp; 82 } 83 84 f->props->color = c; 85 86 return 0; 84 87 } 85 88 #endif … … 88 91 int Model::in_VColor(const Vec3& c) 89 92 { 90 Vertex *v = vertices(vertices.length()-1); 91 92 if( !v->props ) 93 v->props = new VProp; 94 95 v->props->color = c; 96 return 0; 93 Vertex *v = vertices(vertices.length()-1); 94 95 if( !v->props ) 96 { 97 v->props = new VProp; 98 } 99 100 v->props->color = c; 101 return 0; 97 102 } 98 103 #endif … … 100 105 Vec3 Model::synthesizeNormal(Vertex *v) 101 106 { 102 Vec3 n(0,0,0); 103 int n_count = 0; 104 105 edge_buffer& edges = v->edgeUses(); 106 for(int i=0; i<edges.length(); i++) 107 { 108 face_buffer& faces = edges(i)->faceUses(); 109 110 for(int j=0; j<faces.length(); j++) 111 { 112 n += faces(j)->plane().normal(); 113 n_count++; 114 } 115 } 116 117 if( n_count ) 118 n /= (real)n_count; 119 else 120 { 107 Vec3 n(0,0,0); 108 int n_count = 0; 109 110 edge_buffer& edges = v->edgeUses(); 111 for(int i=0; i<edges.length(); i++) 112 { 113 face_buffer& faces = edges(i)->faceUses(); 114 115 for(int j=0; j<faces.length(); j++) 116 { 117 n += faces(j)->plane().normal(); 118 n_count++; 119 } 120 } 121 122 if( n_count ) 123 { 124 n /= (real)n_count; 125 } 126 else 127 { 121 128 std::cerr << "Vertex with no normals!!: " << v->uniqID; 122 129 std::cerr << " / " << v->tempID << std::endl; 123 124 130 } 131 return n; 125 132 } 126 133 … … 138 145 Vertex *Model::newVertex(real x, real y, real z) 139 146 { 140 141 142 143 144 /* if( logfile && selected_output&OUTPUT_MODEL_DEFN )145 146 */147 148 149 147 Vertex *v = new Vertex(x, y, z); 148 149 v->vID = v->uniqID = vertices.add(v); 150 151 /* if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 152 *logfile << "v " << x << " " << y << " " << z << std::endl; 153 */ 154 validVertCount++; 155 156 return v; 150 157 } 151 158 152 159 Edge *Model::newEdge(Vertex *a, Vertex *b) 153 160 { 154 155 156 157 158 159 160 161 161 Edge *e = new Edge(a, b); 162 163 e->uniqID = edges.add(e); 164 e->sym()->uniqID = e->uniqID; 165 166 validEdgeCount++; 167 168 return e; 162 169 } 163 170 164 171 static Edge *get_edge(Model *m, Vertex *org, Vertex *v) 165 172 { 166 edge_buffer& edge_uses = org->edgeUses(); 167 168 for(int i=0; i<edge_uses.length(); i++) 169 if( edge_uses(i)->dest() == v ) 170 return edge_uses(i); 171 172 Edge *e = m->newEdge(org, v); 173 174 return e; 175 } 176 173 edge_buffer& edge_uses = org->edgeUses(); 174 175 for (int i=0; i<edge_uses.length(); i++) 176 { 177 if ( edge_uses(i)->dest() == v ) 178 { 179 return edge_uses(i); 180 } 181 } 182 183 Edge *e = m->newEdge(org, v); 184 185 return e; 186 } 177 187 178 188 Face *Model::newFace(Vertex *v1, Vertex *v2, Vertex *v3) 179 189 { 180 181 182 183 184 185 186 187 188 /* if( logfile && selected_output&OUTPUT_MODEL_DEFN )189 *logfile << "f " << v1->uniqID+1 << " "190 << v2->uniqID+1 << " " << v3->uniqID+1 << std::endl;191 */192 193 194 190 Edge *e0 = get_edge(this, v1, v2); // v1->edgeTo(m, v2); 191 Edge *e1 = get_edge(this, v2, v3); // v2->edgeTo(m, v3); 192 Edge *e2 = get_edge(this, v3, v1); // v3->edgeTo(m, v1); 193 194 Face *t = new Face(e0, e1, e2); 195 196 t->uniqID = faces.add(t); 197 198 /* if( logfile && selected_output&OUTPUT_MODEL_DEFN ) 199 *logfile << "f " << v1->uniqID+1 << " " 200 << v2->uniqID+1 << " " << v3->uniqID+1 << std::endl; 201 */ 202 validFaceCount++; 203 204 return t; 195 205 } 196 206 … … 198 208 void Model::killVertex(Vertex *v) 199 209 { 200 201 202 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS )203 *logfile << "v- " << v->uniqID+1 << std::endl;204 */205 v->kill();206 validVertCount--;207 210 if( v->isValid() ) 211 { 212 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 213 *logfile << "v- " << v->uniqID+1 << std::endl; 214 */ 215 v->kill(); 216 validVertCount--; 217 } 208 218 } 209 219 210 220 void Model::killEdge(Edge *e) 211 221 { 212 213 214 e->kill();215 validEdgeCount--;216 222 if( e->isValid() ) 223 { 224 e->kill(); 225 validEdgeCount--; 226 } 217 227 } 218 228 219 229 void Model::killFace(Face *f) 220 230 { 221 222 223 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS )224 *logfile << "f- " << f->uniqID+1 << std::endl;225 */226 f->kill();227 validFaceCount--;228 231 if( f->isValid() ) 232 { 233 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 234 *logfile << "f- " << f->uniqID+1 << std::endl; 235 */ 236 f->kill(); 237 validFaceCount--; 238 } 229 239 } 230 240 231 241 void Model::reshapeVertex(Vertex *v, real x, real y, real z) 232 242 { 233 243 v->set(x, y, z); 234 244 235 245 #ifdef LOG_LOWLEVEL_OPS 236 if( logfile ) 237 *logfile << "v! " << v->uniqID+1 << " " 238 << x << " " << y << " " << z << endl; 246 if( logfile ) 247 { 248 *logfile << "v! " << v->uniqID+1 << " " 249 << x << " " << y << " " << z << endl; 250 } 239 251 #endif 240 252 } … … 242 254 void Model::remapVertex(Vertex *from, Vertex *to) 243 255 { 244 256 from->remapTo(to); 245 257 246 258 #ifdef LOG_LOWLEVEL_OPS 247 248 *logfile << "v> " << from->uniqID+1 << " " << to->uniqID+1 << endl;249 #endif 250 }251 252 259 if( logfile ) 260 { 261 *logfile << "v> " << from->uniqID+1 << " " << to->uniqID+1 << endl; 262 } 263 #endif 264 } 253 265 254 266 void Model::contract(Vertex *v1, … … 257 269 face_buffer& changed) 258 270 { 259 int i; 260 261 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 262 { 263 *logfile << "v% (" << v1->uniqID+1; 271 int i; 272 273 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 274 { 275 *logfile << "v% (" << v1->uniqID+1; 276 for(i=0; i<rest.length(); i++) 277 *logfile << " " << rest(i)->uniqID+1; 278 279 *logfile << ") " << to[X] << " " << to[Y] << " " << to[Z] << std::endl; 280 } 281 */ 282 283 // 284 // Collect all the faces that are going to be changed 285 // 286 contractionRegion(v1, rest, changed); 287 288 reshapeVertex(v1, to[X], to[Y], to[Z]); 289 264 290 for(i=0; i<rest.length(); i++) 265 *logfile << " " << rest(i)->uniqID+1; 266 267 *logfile << ") " << to[X] << " " << to[Y] << " " << to[Z] << std::endl; 268 } 269 */ 270 271 // 272 // Collect all the faces that are going to be changed 273 // 274 contractionRegion(v1, rest, changed); 275 276 reshapeVertex(v1, to[X], to[Y], to[Z]); 277 278 for(i=0; i<rest.length(); i++) 291 { 279 292 rest(i)->remapTo(v1); 280 281 removeDegeneracy(changed); 293 } 294 295 removeDegeneracy(changed); 282 296 } 283 297 284 298 void Model::maybeFixFace(Face *F) 285 299 { 286 287 300 // 301 // Invalid faces do not need to be fixed. 288 302 #ifdef SAFETY 289 290 #endif 291 292 293 294 295 296 297 298 299 // This triangle has been reduced to a point300 killEdge(e0);301 killEdge(e1);302 killEdge(e2);303 304 killFace(F);305 306 307 308 309 310 killEdge(e0);311 e1->remapTo(e2->sym());312 killFace(F);313 314 315 316 killEdge(e2);317 e0->remapTo(e1->sym());318 killFace(F);319 320 321 322 killEdge(e1);323 e0->remapTo(e2->sym());324 killFace(F);325 326 327 328 // This triangle remains non-degenerate329 /* // SUS: recalcular la normal en los triangulos cambiados no degenerados330 331 simplif::Vertex * auxv0 = F->vertex(0);332 simplif::Vertex * auxv1 = F->vertex(1);333 simplif::Vertex * auxv2 = F->vertex(2);334 simplif::Vec3 vd1(auxv1->operator[](simplif::X) - auxv0->operator[](simplif::X),335 336 337 simplif::Vec3 vd2(auxv2->operator[](simplif::X) - auxv0->operator[](simplif::X),338 339 340 341 simplif::unitize(vd1);342 simplif::unitize(vd2);343 344 simplif::Vec3 vn = vd1 ^ vd2;345 simplif::unitize(vn);346 303 assert( F->isValid() ); 304 #endif 305 306 Vertex *v0=F->vertex(0); Vertex *v1=F->vertex(1); Vertex *v2=F->vertex(2); 307 Edge *e0 = F->edge(0); Edge *e1 = F->edge(1); Edge *e2 = F->edge(2); 308 309 bool a=(v0 == v1), b=(v0 == v2), c=(v1 == v2); 310 311 if( a && c ) 312 { 313 // This triangle has been reduced to a point 314 killEdge(e0); 315 killEdge(e1); 316 killEdge(e2); 317 318 killFace(F); 319 } 320 // 321 // In the following 3 cases, the triangle has become an edge 322 else if( a ) 323 { 324 killEdge(e0); 325 e1->remapTo(e2->sym()); 326 killFace(F); 327 } 328 else if( b ) 329 { 330 killEdge(e2); 331 e0->remapTo(e1->sym()); 332 killFace(F); 333 } 334 else if( c ) 335 { 336 killEdge(e1); 337 e0->remapTo(e2->sym()); 338 killFace(F); 339 } 340 else 341 { 342 // This triangle remains non-degenerate 343 /* // SUS: recalcular la normal en los triangulos cambiados no degenerados 344 345 simplif::Vertex * auxv0 = F->vertex(0); 346 simplif::Vertex * auxv1 = F->vertex(1); 347 simplif::Vertex * auxv2 = F->vertex(2); 348 simplif::Vec3 vd1(auxv1->operator[](simplif::X) - auxv0->operator[](simplif::X), 349 auxv1->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 350 auxv1->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 351 simplif::Vec3 vd2(auxv2->operator[](simplif::X) - auxv0->operator[](simplif::X), 352 auxv2->operator[](simplif::Y) - auxv0->operator[](simplif::Y), 353 auxv2->operator[](simplif::Z) - auxv0->operator[](simplif::Z)); 354 355 simplif::unitize(vd1); 356 simplif::unitize(vd2); 357 358 simplif::Vec3 vn = vd1 ^ vd2; 359 simplif::unitize(vn); 360 347 361 //auxf->vertex_normals[0] = vn; 348 362 //auxf->vertex_normals[1] = vn; … … 358 372 F->vertex_normals[2][simplif::Z] = vn[simplif::Z];*/ 359 373 360 374 } 361 375 } 362 376 363 377 void Model::removeDegeneracy(face_buffer& changed) 364 378 { 365 for(int i=0; i<changed.length(); i++) 379 for(int i=0; i<changed.length(); i++) 380 { 366 381 maybeFixFace(changed(i)); 382 } 367 383 } 368 384 369 385 void Model::contractionRegion(Vertex *v1, 370 const vert_buffer& vertices, 371 face_buffer& changed) 372 { 373 changed.reset(); 374 375 // 376 // First, reset the marks on all reachable vertices 377 int i; 378 379 untagFaceLoop(v1); 380 for(i=0; i<vertices.length(); i++) 386 const vert_buffer& vertices, 387 face_buffer& changed) 388 { 389 changed.reset(); 390 391 // First, reset the marks on all reachable vertices 392 int i; 393 394 untagFaceLoop(v1); 395 396 for(i = 0; i < vertices.length(); i++) 397 { 381 398 untagFaceLoop(vertices(i)); 382 383 // 384 // Now, pick out all the unique reachable faces 385 collectFaceLoop(v1, changed); 386 for(i=0; i<vertices.length(); i++) 399 } 400 401 // Now, pick out all the unique reachable faces 402 collectFaceLoop(v1, changed); 403 404 for(i = 0; i < vertices.length(); i++) 405 { 387 406 collectFaceLoop(vertices(i), changed); 407 } 388 408 } 389 409 … … 391 411 void Model::contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed) 392 412 { 393 394 395 396 397 398 399 400 401 402 403 413 changed.reset(); 414 415 // 416 // Clear marks on all reachable faces 417 untagFaceLoop(v1); 418 untagFaceLoop(v2); 419 420 // 421 // Collect all the unique reachable faces 422 collectFaceLoop(v1, changed); 423 collectFaceLoop(v2, changed); 404 424 } 405 425 … … 408 428 { 409 429 #ifdef SAFETY 410 assert( v1 != v2); 411 #endif 412 413 /* if( logfile && selected_output&OUTPUT_CONTRACTIONS ) 414 *logfile << "v% (" << v1->uniqID+1 << " " << v2->uniqID+1 << ") " 415 << to[X] << " " 416 << to[Y] << " " 417 << to[Z] << std::endl; 418 */ 419 // 420 // Collect all the faces that are going to be changed 421 // 422 contractionRegion(v1, v2, changed); 423 424 reshapeVertex(v1, to[X], to[Y], to[Z]); 425 426 // 427 // Map v2 ---> v1. This accomplishes the topological change that we want. 428 v2->remapTo(v1); 429 430 removeDegeneracy(changed); 431 } 432 430 assert( v1 != v2); 431 #endif 432 // 433 // Collect all the faces that are going to be changed 434 // 435 contractionRegion(v1, v2, changed); 436 437 reshapeVertex(v1, to[X], to[Y], to[Z]); 438 439 // Map v2 ---> v1. This accomplishes the topological change that we want. 440 v2->remapTo(v1); 441 442 // std::cout << "Contract(QslimInds): " << v2->validID() << " --> " << v1->validID() << std::endl; 443 444 removeDegeneracy(changed); 445 } 433 446 434 447 /* … … 494 507 } 495 508 */ 509 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjModel.h
r1025 r1526 22 22 class Model : public SMF_Model 23 23 { 24 protected: 25 vert_buffer vertices; 26 edge_buffer edges; 27 face_buffer faces; 28 vec3_buffer normals; // SUS 29 vec2_buffer texcoords; 24 protected: 25 26 vert_buffer vertices; 27 edge_buffer edges; 28 face_buffer faces; 29 vec3_buffer normals; // SUS 30 vec2_buffer texcoords; 30 31 31 private:32 private: 32 33 33 void maybeFixFace(Face *);34 void maybeFixFace(Face *); 34 35 35 public: 36 Model() { 37 validVertCount = 0; 38 validEdgeCount = 0; 39 validFaceCount = 0; 40 } 36 public: 37 38 Model() 39 { 40 validVertCount = 0; 41 validEdgeCount = 0; 42 validFaceCount = 0; 43 } 41 44 42 ~Model(){ 43 for (int i=0; i<vertices.length(); i++) 44 delete vertices[i]; 45 for (int i=0; i<edges.length(); i++) 46 delete edges[i]; 47 for (int i=0; i<faces.length(); i++) 48 delete faces[i]; 49 } 45 ~Model() 46 { 47 for (int i=0; i<vertices.length(); i++) 48 { 49 delete vertices[i]; 50 } 50 51 51 Bounds bounds; 52 for (int i=0; i<edges.length(); i++) 53 { 54 delete edges[i]; 55 } 52 56 53 int validVertCount; 54 int validEdgeCount; 55 int validFaceCount; 57 for (int i=0; i<faces.length(); i++) 58 { 59 delete faces[i]; 60 } 61 } 56 62 57 // 58 // Basic model accessor functions 59 // 60 simplif::Vertex *vertex(int i) { return vertices(i); } 61 simplif::Vec3 & normal(int i){ return normals(i); } 62 simplif::Vec2 & texcoord(int i){ return texcoords(i); } 63 simplif::Edge *edge(int i) { return edges(i); } 64 simplif::Face *face(int i) { return faces(i); } 63 Bounds bounds; 65 64 66 int vertCount() { return vertices.length(); } 67 int normCount() { return normals.length(); } 68 int texcoordCount() { return texcoords.length(); } 69 int edgeCount() { return edges.length(); } 70 int faceCount() { return faces.length(); } 65 int validVertCount; 66 int validEdgeCount; 67 int validFaceCount; 71 68 72 vert_buffer& allVertices() { return vertices; } 73 edge_buffer& allEdges() { return edges; } 74 face_buffer& allFaces() { return faces; } 69 // 70 // Basic model accessor functions 71 // 72 simplif::Vertex *vertex(int i) { return vertices(i); } 73 simplif::Vec3 & normal(int i){ return normals(i); } 74 simplif::Vec2 & texcoord(int i){ return texcoords(i); } 75 simplif::Edge *edge(int i) { return edges(i); } 76 simplif::Face *face(int i) { return faces(i); } 75 77 76 // 77 // Simplification primitives 78 // 79 Vertex *newVertex(real x=0.0, real y=0.0, real z=0.0); 80 Edge *newEdge(Vertex *,Vertex *); 81 Face *newFace(Vertex *, Vertex *, Vertex *); 78 int vertCount() { return vertices.length(); } 79 int normCount() { return normals.length(); } 80 int texcoordCount() { return texcoords.length(); } 81 int edgeCount() { return edges.length(); } 82 int faceCount() { return faces.length(); } 82 83 83 void killVertex(Vertex *);84 void killEdge(Edge *);85 void killFace(Face *);84 vert_buffer& allVertices() { return vertices; } 85 edge_buffer& allEdges() { return edges; } 86 face_buffer& allFaces() { return faces; } 86 87 87 void reshapeVertex(Vertex *, real, real, real); 88 void remapVertex(Vertex *from, Vertex *to); 88 // 89 // Simplification primitives 90 // 91 Vertex *newVertex(real x=0.0, real y=0.0, real z=0.0); 92 Edge *newEdge(Vertex *,Vertex *); 93 Face *newFace(Vertex *, Vertex *, Vertex *); 89 94 90 void contract(Vertex *v1, Vertex *v2, const Vec3& to, 91 face_buffer& changed); 95 void killVertex(Vertex *); 96 void killEdge(Edge *); 97 void killFace(Face *); 92 98 93 void contract(Vertex *v1, 94 const vert_buffer& others, 95 const Vec3& to, 96 face_buffer& changed); 99 void reshapeVertex(Vertex *, real, real, real); 100 void remapVertex(Vertex *from, Vertex *to); 97 101 102 void contract(Vertex *v1, Vertex *v2, const Vec3& to, 103 face_buffer& changed); 98 104 99 // 100 // Simplification convenience procedures 101 // 102 void removeDegeneracy(face_buffer& changed); 103 void contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed); 104 void contractionRegion(Vertex *v1, 105 const vert_buffer& vertices, 106 face_buffer& changed); 105 void contract(Vertex *v1, 106 const vert_buffer& others, 107 const Vec3& to, 108 face_buffer& changed); 107 109 108 //109 // SMF reader functions110 //111 int in_Vertex(const Vec3&);112 int in_Normal(const Vec3&);113 int in_TexCoord(const Vec2&);114 int in_Face(int v1, int v2, int v3, int n1, int n2, int n3, int t1, int t2, int t3);115 int miin_Face(int v1, int v2, int v3);110 // 111 // Simplification convenience procedures 112 // 113 void removeDegeneracy(face_buffer& changed); 114 void contractionRegion(Vertex *v1, Vertex *v2, face_buffer& changed); 115 void contractionRegion(Vertex *v1, 116 const vert_buffer& vertices, 117 face_buffer& changed); 116 118 117 /* #ifdef SUPPORT_VCOLOR 118 int in_VColor(const Vec3&); 119 #endif 120 #ifdef SUPPORT_FCOLOR 121 int in_FColor(const Vec3&); 122 #endif 119 // 120 // SMF reader functions 121 // 122 int in_Vertex(const Vec3&); 123 int in_Normal(const Vec3&); 124 int in_TexCoord(const Vec2&); 125 126 int in_Face(int v1, int v2, int v3, 127 int n1, int n2, int n3, 128 int t1, int t2, int t3); 129 130 int miin_Face(int v1, int v2, int v3); 131 132 /* #ifdef SUPPORT_VCOLOR 133 int in_VColor(const Vec3&); 134 #endif 135 #ifdef SUPPORT_FCOLOR 136 int in_FColor(const Vec3&); 137 #endif 123 138 */ 124 //125 // Some random functions that are mostly temporary126 //139 // 140 // Some random functions that are mostly temporary 141 // 127 142 128 Vec3 synthesizeNormal(Vertex *);143 Vec3 synthesizeNormal(Vertex *); 129 144 }; 130 145 } 131 146 132 133 147 // NAUTILUS_ADJMODEL_INCLUDED 134 148 #endif 149 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjPrims.cxx
r1025 r1526 18 18 { 19 19 #ifdef SAFETY 20 21 #endif 22 23 20 assert( edge_uses.length() == 0 ); 21 #endif 22 markInvalid(); 23 edge_uses.reset(); 24 24 } 25 25 26 26 void Vertex::linkEdge(Edge *e) 27 27 { 28 28 edge_uses.add(e); 29 29 } 30 30 31 31 void Vertex::unlinkEdge(Edge *e) 32 32 { 33 34 35 #ifdef SAFETY 36 37 #endif 38 39 40 kill();33 int index = edge_uses.find(e); 34 35 #ifdef SAFETY 36 assert( index >= 0 ); 37 #endif 38 edge_uses.remove(index); 39 if( edge_uses.length() <= 0 ) 40 kill(); 41 41 } 42 42 … … 45 45 void Vertex::remapTo(Vertex *v) 46 46 { 47 if( v != this )48 49 for (int i=0; i<edge_uses.length(); i++)47 if ( v != this ) 48 { 49 for (int i=0; i<edge_uses.length(); i++) 50 50 { 51 51 assert( edge_uses(i)->org() == this ); … … 54 54 55 55 kill(); 56 } 57 } 58 56 } 57 } 59 58 60 59 Edge::Edge(Vertex *a, Vertex *b) 61 60 { 62 63 64 65 66 67 61 v1 = a; 62 v1->linkEdge(this); 63 64 face_uses = new buffer<Face *>(2); 65 66 twin = new Edge(this, b); 68 67 } 69 68 70 69 Edge::Edge(Edge *sibling, Vertex *org) 71 70 { 72 73 74 75 76 71 v1 = org; 72 v1->linkEdge(this); 73 74 face_uses = sibling->face_uses; 75 twin = sibling; 77 76 } 78 77 79 78 Edge::~Edge() 80 79 { 81 if( twin )82 83 face_uses->f_r_e_e();84 delete face_uses;85 86 twin->twin = NULL;87 delete twin;88 80 if ( twin ) 81 { 82 face_uses->f_r_e_e(); 83 delete face_uses; 84 85 twin->twin = NULL; 86 delete twin; 87 } 89 88 } 90 89 … … 92 91 { 93 92 #ifdef SAFETY 94 95 #endif 96 if( isValid() )97 98 org()->unlinkEdge(this);99 dest()->unlinkEdge(sym());100 markInvalid();101 twin->markInvalid();102 face_uses->reset();103 93 assert( face_uses->length() == 0 ); 94 #endif 95 if ( isValid() ) 96 { 97 org()->unlinkEdge(this); 98 dest()->unlinkEdge(sym()); 99 markInvalid(); 100 twin->markInvalid(); 101 face_uses->reset(); 102 } 104 103 } 105 104 106 105 void Edge::linkFace(Face *face) 107 106 { 108 107 face_uses->add(face); 109 108 } 110 109 111 110 void Edge::unlinkFace(Face *face) 112 111 { 113 114 115 #ifdef SAFETY 116 117 #endif 118 119 120 kill();112 int index = face_uses->find(face); 113 114 #ifdef SAFETY 115 assert( index>=0 ); 116 #endif 117 face_uses->remove(index); 118 if( face_uses->length() == 0 ) 119 kill(); 121 120 } 122 121 123 122 void Edge::remapTo(Edge *e) 124 123 { 125 if( e != this )126 127 for(int i=0; i<face_uses->length(); i++)128 {129 130 }131 132 // Disconnect from all faces and vertices133 kill();134 124 if ( e != this ) 125 { 126 for (int i=0; i<face_uses->length(); i++) 127 { 128 (*face_uses)(i)->remapEdge(this, e); 129 } 130 131 // Disconnect from all faces and vertices 132 kill(); 133 } 135 134 } 136 135 137 136 void Edge::remapEndpoint(Vertex *from, Vertex *to) 138 137 { 139 if( org()==from )140 138 if ( org() == from ) 139 { 141 140 v1 = to; 142 141 to->linkEdge(this); 143 144 else if( dest()==from )145 142 } 143 else if ( dest() == from ) 144 { 146 145 twin->v1 = to; 147 146 to->linkEdge(twin); 148 149 150 147 } 148 else 149 { 151 150 std::cerr << "WARNING remapEndpoint: Illegal endpoint." << std::endl; 152 153 154 155 156 157 for(int i=0; i<face_uses->length(); i++)158 151 } 152 153 // 154 // The cached Plane equations for the faces attached to us may 155 // no longer be valid (in general, chances are pretty slim that they're OK) 156 for (int i = 0; i < face_uses->length(); i++) 157 { 159 158 face_uses->ref(i)->invalidatePlane(); 160 } 161 } 162 159 } 160 } 163 161 164 162 Face::Face(Edge *e0, Edge *e1, Edge *e2) 165 163 : Face3(*e0->org(), *e1->org(), *e2->org()) 166 164 { 167 168 169 170 171 172 173 165 edges[0] = e0; 166 edges[1] = e1; 167 edges[2] = e2; 168 169 edges[0]->linkFace(this); 170 edges[1]->linkFace(this); 171 edges[2]->linkFace(this); 174 172 175 173 #ifdef SUPPORT_FCOLOR 176 174 props = NULL; 177 175 #endif 178 176 } … … 180 178 void Face::kill() 181 179 { 182 183 184 if( edge(0)->isValid() )185 186 187 if( edge(1)->isValid() )188 189 190 if( edge(2)->isValid() )191 192 193 markInvalid();194 180 if( isValid() ) 181 { 182 if( edge(0)->isValid() ) 183 edge(0)->unlinkFace(this); 184 185 if( edge(1)->isValid() ) 186 edge(1)->unlinkFace(this); 187 188 if( edge(2)->isValid() ) 189 edge(2)->unlinkFace(this); 190 191 markInvalid(); 192 } 195 193 } 196 194 197 195 void Face::remapEdge(Edge *from, Edge *to) 198 196 { 199 for(int i=0; i<3; i++) 200 { 201 if( edges[i] == from ) 202 { 203 edges[i] = to; 204 to->linkFace(this); 205 } 206 else if( edges[i] == from->sym() ) 207 { 208 edges[i] = to->sym(); 209 to->sym()->linkFace(this); 210 } 211 } 212 213 invalidatePlane(); 214 } 215 197 for (int i=0; i<3; i++) 198 { 199 if ( edges[i] == from ) 200 { 201 edges[i] = to; 202 to->linkFace(this); 203 } 204 else if ( edges[i] == from->sym() ) 205 { 206 edges[i] = to->sym(); 207 to->sym()->linkFace(this); 208 } 209 } 210 211 invalidatePlane(); 212 } 216 213 217 214 void simplif::untagFaceLoop(Vertex *v) 218 215 { 219 edge_buffer& edges = v->edgeUses(); 220 221 for(int j=0; j<edges.length(); j++) 222 { 223 face_buffer& faces = edges(j)->faceUses(); 224 for(int k=0; k<faces.length(); k++) 225 faces(k)->untag(); 226 } 216 edge_buffer& edges = v->edgeUses(); 217 218 for (int j = 0; j < edges.length(); j++) 219 { 220 face_buffer& faces = edges(j)->faceUses(); 221 222 for (int k = 0; k < faces.length(); k++) 223 { 224 faces(k)->untag(); 225 } 226 } 227 227 } 228 228 229 229 void simplif::collectFaceLoop(Vertex *v, face_buffer& loop) 230 230 { 231 232 233 234 235 face_buffer& faces = edges(j)->faceUses();236 for(int k=0; k<faces.length(); k++)237 238 239 loop.add(faces(k));240 faces(k)->tag();241 242 231 edge_buffer& edges = v->edgeUses(); 232 233 for(int j=0; j<edges.length(); j++) 234 { 235 face_buffer& faces = edges(j)->faceUses(); 236 for(int k=0; k<faces.length(); k++) 237 if( !faces(k)->isTagged() ) 238 { 239 loop.add(faces(k)); 240 faces(k)->tag(); 241 } 242 } 243 243 } 244 244 245 245 int simplif::classifyEdge(Edge *e) 246 246 { 247 248 249 250 251 247 int cls = e->faceUses().length(); 248 249 if( cls>3 ) cls=3; 250 251 return cls; 252 252 } 253 253 254 254 int simplif::classifyVertex(Vertex *v) 255 255 { 256 int border_count = 0; 257 const edge_buffer& edges = v->edgeUses(); 258 259 for(int i=0; i<edges.length(); i++) 260 if( classifyEdge(edges(i)) == 1 ) 261 border_count++; 262 263 if( border_count == edges.length() ) 264 return VERTEX_BORDER_ONLY; 265 else 266 return (border_count > 0); 267 } 256 int border_count = 0; 257 const edge_buffer& edges = v->edgeUses(); 258 259 for(int i=0; i<edges.length(); i++) 260 if( classifyEdge(edges(i)) == 1 ) 261 border_count++; 262 263 if( border_count == edges.length() ) 264 return VERTEX_BORDER_ONLY; 265 else 266 return (border_count > 0); 267 } 268 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/AdjPrims.h
r1025 r1526 27 27 class Face; 28 28 29 30 29 typedef buffer<Vertex *> vert_buffer; 31 30 typedef buffer<Edge *> edge_buffer; … … 37 36 extern void collectFaceLoop(Vertex *v, face_buffer& faces); 38 37 39 40 41 42 38 #define EDGE_BOGUS 0 39 #define EDGE_BORDER 1 40 #define EDGE_MANIFOLD 2 41 #define EDGE_NONMANIFOLD 3 43 42 extern int classifyEdge(Edge *); 44 43 45 46 47 44 #define VERTEX_INTERIOR 0 45 #define VERTEX_BORDER 1 46 #define VERTEX_BORDER_ONLY 2 48 47 extern int classifyVertex(Vertex *); 49 48 … … 52 51 // The actual class definitions 53 52 // 53 //////////////////////////////////////////////////////////////////////// 54 54 55 55 class VProp 56 56 { 57 public:58 Vec3 color;57 public: 58 Vec3 color; 59 59 }; 60 60 61 61 class FProp 62 62 { 63 public:64 Vec3 color;63 public: 64 Vec3 color; 65 65 }; 66 66 … … 68 68 { 69 69 edge_buffer edge_uses; 70 71 public:72 70 71 public: 72 #ifdef SUPPORT_VCOLOR 73 73 VProp *props; 74 74 #endif 75 75 76 76 Vertex(real x, real y, real z) : Vec3(x, y, z), edge_uses(6) { 77 78 props = NULL;79 #endif 77 #ifdef SUPPORT_VCOLOR 78 props = NULL; 79 #endif 80 80 } 81 81 // … … 92 92 void remapTo(Vertex *v); 93 93 94 unsigned int vID; 94 // SUS 95 int vID; // this can be -1 when the vertex becomes non-valid 95 96 }; 96 97 97 98 98 class Edge : public NPrim 99 99 { 100 private:100 private: 101 101 102 Vertex *v1;102 Vertex *v1; 103 103 104 face_buffer *face_uses;105 Edge *twin;104 face_buffer *face_uses; 105 Edge *twin; 106 106 107 Edge(Edge *twin, Vertex *org); // the twin constructor107 Edge(Edge *twin, Vertex *org); // the twin constructor 108 108 109 public: 110 Edge(Vertex *, Vertex *); 111 ~Edge(); 109 public: 112 110 113 // 114 // Fundamental Edge accessors 115 // 116 Vertex *org() { return v1; } 117 Vertex *dest() { return twin->v1; } 118 Edge *sym() { return twin; } 111 Edge(Vertex *, Vertex *); 112 ~Edge(); 119 113 120 // 121 // Standard methods for all objects 122 // 123 void kill(); 124 face_buffer& faceUses() { return *face_uses; } 114 // 115 // Fundamental Edge accessors 116 // 117 Vertex *org() { return v1; } 118 Vertex *dest() { return twin->v1; } 119 Edge *sym() { return twin; } 125 120 126 // 127 // Basic primitives for manipulating model topology 128 // 129 void linkFace(Face *); 130 void unlinkFace(Face *); 131 void remapEndpoint(Vertex *from, Vertex *to); 132 void remapTo(Edge *); 121 // 122 // Standard methods for all objects 123 // 124 void kill(); 125 face_buffer& faceUses() { return *face_uses; } 126 127 // 128 // Basic primitives for manipulating model topology 129 // 130 void linkFace(Face *); 131 void unlinkFace(Face *); 132 void remapEndpoint(Vertex *from, Vertex *to); 133 void remapTo(Edge *); 133 134 }; 134 135 … … 137 138 Edge *edges[3]; 138 139 139 140 public: 141 #ifdef SUPPORT_FCOLOR 140 public: 141 #ifdef SUPPORT_FCOLOR 142 142 FProp *props; 143 143 #endif 144 144 145 145 Face(Edge *, Edge *, Edge *); … … 148 148 // Basic Face accessors 149 149 // 150 const Vec3& vertexPos(int i) const { return *edges[i]->org(); } 151 void vertexPos(int, const Vec3&) { 150 const Vec3& vertexPos(int i) const{return *edges[i]->org();} 151 152 void vertexPos(int, const Vec3&) 153 { 152 154 fatal_error("Face: can't directly set vertex position."); 153 155 } 154 156 155 Vertex *vertex(int i) { return edges[i]->org(); } 156 const Vertex *vertex(int i) const { return edges[i]->org(); } 157 Edge *edge(int i) { return edges[i]; } 158 157 Vertex *vertex(int i){return edges[i]->org();} 158 const Vertex *vertex(int i) const{return edges[i]->org();} 159 Edge *edge(int i){return edges[i];} 159 160 160 161 // … … 173 174 } 174 175 175 176 176 // NAUTILUS_ADJPRIMS_INCLUDED 177 177 #endif 178 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/SimplificationMethod.cpp
r1025 r1526 6 6 using namespace std; 7 7 using namespace Geometry; 8 using namespace simplif; 8 9 9 10 #include <gfx/geom/ProxGrid.h> … … 13 14 // Constructor 14 15 //--------------------------------------------------------------------------- 15 SimplificationMethod::SimplificationMethod(const Mesh *m) 16 SimplificationMethod::SimplificationMethod(const Mesh *m) 16 17 { 17 18 objmesh = m; … … 19 20 first_index_submesh = new unsigned int[objmesh->mSubMeshCount]; 20 21 indexMeshLeaves = -1; 22 23 // Create simplification sequence. 24 msimpseq = new MeshSimplificationSequence(); 21 25 } 22 26 … … 41 45 simplif::vert_info& v1_info = vertex_info(v1); 42 46 43 simplif::Mat4 Q =v0_info.Q + v1_info.Q;44 simplif::real norm =v0_info.norm + v1_info.norm;47 simplif::Mat4 Q = v0_info.Q + v1_info.Q; 48 simplif::real norm = v0_info.norm + v1_info.norm; 45 49 46 50 auxpair->cost = simplif::quadrix_pair_target(Q, v0, v1, auxpair->candidate); 47 51 48 if( simplif::will_weight_by_area ) 52 if ( simplif::will_weight_by_area ) 53 { 49 54 auxpair->cost /= norm; 50 51 if( simplif::will_preserve_mesh_quality ) 55 } 56 57 if ( simplif::will_preserve_mesh_quality ) 58 { 52 59 auxpair->cost += pair_mesh_penalty(M0, v0, v1, auxpair->candidate); 53 60 } 54 61 55 62 // … … 57 64 // the heap is implemented as a MAX heap. 58 65 // 59 if ( auxpair->isInHeap() )66 if ( auxpair->isInHeap() ) 60 67 { 61 68 heap->update(auxpair, (float)-auxpair->cost); … … 70 77 // Reasign the new vertex to the adequate face 71 78 //--------------------------------------------------------------------------- 72 int SimplificationMethod::predict_face(simplif::Face& F, simplif::Vertex *v1, simplif::Vertex *v2, simplif::Vec3& vnew, simplif::Vec3& f1, simplif::Vec3& f2, simplif::Vec3& f3) 79 int SimplificationMethod::predict_face( simplif::Face& F, 80 simplif::Vertex *v1, 81 simplif::Vertex *v2, 82 simplif::Vec3& vnew, 83 simplif::Vec3& f1, 84 simplif::Vec3& f2, 85 simplif::Vec3& f3) 73 86 { 74 87 int nmapped = 0; 75 88 76 89 if( F.vertex(0) == v1 || F.vertex(0) == v2 ) 77 { f1 = vnew; nmapped++; } 78 else f1 = *F.vertex(0); 90 { 91 f1 = vnew; nmapped++; 92 } 93 else 94 { 95 f1 = *F.vertex(0); 96 } 79 97 80 98 if( F.vertex(1) == v1 || F.vertex(1) == v2 ) 81 { f2 = vnew; nmapped++; } 82 else f2 = *F.vertex(1); 99 { 100 f2 = vnew; nmapped++; 101 } 102 else 103 { 104 f2 = *F.vertex(1); 105 } 83 106 84 107 if( F.vertex(2) == v1 || F.vertex(2) == v2 ) 85 { f3 = vnew; nmapped++; } 86 else f3 = *F.vertex(2); 108 { 109 f3 = vnew; nmapped++; 110 } 111 else 112 { 113 f3 = *F.vertex(2); 114 } 87 115 88 116 return nmapped; … … 93 121 //--------------------------------------------------------------------------- 94 122 //--------------------------------------------------------------------------- 95 simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, simplif::Vertex *v2, simplif::Vec3& vnew) 123 simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, 124 simplif::Vertex *v2, 125 simplif::Vec3& vnew) 96 126 { 97 127 static simplif::face_buffer changed; 128 98 129 changed.reset(); 99 130 … … 103 134 simplif::real Nmin = 0; 104 135 105 for (int i=0; i<changed.length(); i++)136 for (int i = 0; i < changed.length(); i++) 106 137 { 107 138 simplif::Face& F = *changed(i); … … 114 145 { 115 146 simplif::Plane Pnew(f1, f2, f3); 147 116 148 simplif::real delta = Pnew.normal() * F.plane().normal(); 117 149 118 if( Nmin > delta ) Nmin = delta; 150 if( Nmin > delta ) 151 { 152 Nmin = delta; 153 } 119 154 } 120 155 } … … 122 157 //return (-Nmin) * MESH_INVERSION_PENALTY; 123 158 if( Nmin < 0.0 ) 159 { 124 160 return MESH_INVERSION_PENALTY; 161 } 125 162 else 163 { 126 164 return 0.0; 165 } 127 166 } 128 167 … … 130 169 // Returns true if the givens vertices are a valid pair 131 170 //--------------------------------------------------------------------------- 132 bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1) 171 bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, 172 simplif::Vertex *v1) 133 173 { 134 174 const simplif::pair_buffer& pairs = vertex_info(v0).pairs; … … 137 177 { 138 178 if( pairs(i)->v0==v1 || pairs(i)->v1==v1 ) 179 { 139 180 return true; 181 } 140 182 } 141 183 … … 146 188 // Create a new pair with two given vertices 147 189 //--------------------------------------------------------------------------- 148 simplif::pair_info *SimplificationMethod::new_pair(simplif::Vertex *v0, simplif::Vertex *v1) 190 simplif::pair_info *SimplificationMethod::new_pair( simplif::Vertex *v0, 191 simplif::Vertex *v1) 149 192 { 150 193 simplif::vert_info& v0_info = vertex_info(v0); … … 152 195 153 196 simplif::pair_info *pair = new simplif::pair_info(v0,v1); 197 154 198 v0_info.pairs.add(pair); 155 199 v1_info.pairs.add(pair); … … 169 213 v1_info.pairs.remove(v1_info.pairs.find(auxpair)); 170 214 171 if( auxpair->isInHeap() ) 215 if ( auxpair->isInHeap() ) 216 { 172 217 heap->kill(auxpair->getHeapPos()); 218 } 173 219 174 220 delete auxpair; … … 178 224 // Contract a given pair 179 225 //--------------------------------------------------------------------------- 180 void SimplificationMethod::do_contract(simplif::Model& m, simplif::pair_info *auxpair) 181 { 182 simplif::Vertex *v0 = auxpair->v0; simplif::Vertex *v1 = auxpair->v1; 226 void SimplificationMethod::do_contract( simplif::Model& m, 227 simplif::pair_info *auxpair) 228 { 229 simplif::Vertex *v0 = auxpair->v0; 230 simplif::Vertex *v1 = auxpair->v1; 183 231 simplif::vert_info& v0_info = vertex_info(v0); 184 232 simplif::vert_info& v1_info = vertex_info(v1); 185 simplif::Vec3 vnew = auxpair->candidate; 233 simplif::Vec3 vnew = auxpair->candidate; 234 186 235 int i; 236 237 // V0 must be always the alive vertex 238 // V1 must be always the removed vertex 239 if (vnew == (*v0)) 240 { 241 // Simplification step. 242 simplifstep.mV0 = v0->vID; 243 simplifstep.mV1 = v1->vID; 244 } 245 else 246 { 247 // Simplification step. 248 simplifstep.mV0 = v1->vID; 249 simplifstep.mV1 = v0->vID; 250 v0->vID = v1->vID; 251 } 187 252 188 253 // Make v0 be the new vertex 189 v0_info.Q 190 v0_info.norm 254 v0_info.Q += v1_info.Q; 255 v0_info.norm += v1_info.norm; 191 256 192 257 simplif::Vec3 p(vnew); 193 simplif::Vec3 v0antes(v0->operator[](simplif::X),v0->operator[](simplif::Y),v0->operator[](simplif::Z)); 194 195 // Find the edge to remove (before remove it (contract())) 196 simplif::Edge *econ=NULL; 197 bool finded = false; 198 for (i=0; i<v0->edgeUses().length(); i++) 199 { 200 econ = v0->edgeUses()(i); 201 if (v1->vID == econ->org()->vID || v1->vID == econ->dest()->vID) 202 { 203 finded = true; 204 break; 205 } 206 } 207 208 assert(finded); 209 210 211 // Perform the actual contraction 258 /* simplif::Vec3 v0antes(v0->operator[](simplif::X), 259 v0->operator[](simplif::Y), 260 v0->operator[](simplif::Z));*/ 261 262 // Perform the actual contraction. 212 263 static simplif::face_buffer changed; 264 213 265 changed.reset(); 214 266 m.contract(v0, v1, vnew, changed); 215 267 216 // Stores the simplification step 217 MeshSimplificationSequence::Step simplifstep; 218 simplif::Vec3 vdesp = p - v0antes; 219 220 // Stores the displacement in the simplification step 221 simplifstep.x=(float)vdesp[simplif::X]; 222 simplifstep.y = (float)vdesp[simplif::Y]; 223 simplifstep.z = (float)vdesp[simplif::Z]; 224 225 // Submeshes which pertains each vertex 226 simplifstep.mV1 = v0->validID(); 227 228 // The simplification method returns as v1 the dead vertex. 229 simplifstep.mV0 = v1->validID(); 268 std::cout << "Simp seq: " << simplifstep.mV1 << " --> " << simplifstep.mV0 << std::endl; 269 270 /* simplif::Vec3 vdesp = p - v0antes; 271 272 // Stores the displacement in the simplification step. 273 simplifstep.x = (float)vdesp[X]; 274 simplifstep.y = (float)vdesp[Y]; 275 simplifstep.z = (float)vdesp[Z];*/ 276 277 // Stores the displacement in the simplification step. 278 simplifstep.x = (float)0.0f; 279 simplifstep.y = (float)0.0f; 280 simplifstep.z = (float)0.0f; 230 281 231 282 // Number of triangles that are removed in this simplification step. … … 242 293 int del_index = 0; 243 294 244 // mModfaces y mT0, mT1 of simplifstep stores the triangles id's -> geomesh has not triangles id's 245 for (i=0; i<changed.length(); i++) 246 { 247 simplif::Face *auxface = changed(i); 295 // Clears faces list. 296 simplifstep.mModfaces.clear(); 297 298 // mModfaces y mT0, mT1 of simplifstep stores 299 // the triangles id's -> geomesh has not triangles id's. 300 for (i = 0; i < changed.length(); i++) 301 { 302 simplif::Face *auxface = changed(i); 248 303 249 304 if (auxface->isValid()) 250 305 { 251 // Modified triangles306 // Modified triangles. 252 307 simplifstep.mModfaces.push_back(auxface->validID()); 253 308 } 254 309 else 255 310 { 256 // Removed triangles257 if (_numDelTris ==1)258 { 259 simplifstep.mT0 =auxface->validID();260 simplifstep.mT1 =auxface->validID();311 // Removed triangles. 312 if (_numDelTris == 1) 313 { 314 simplifstep.mT0 = auxface->validID(); 315 simplifstep.mT1 = auxface->validID(); 261 316 } 262 317 else 263 318 { 264 if(del_index ==0)319 if(del_index == 0) 265 320 { 266 simplifstep.mT0 =auxface->validID();321 simplifstep.mT0 = auxface->validID(); 267 322 del_index++; 268 323 } 269 324 else 270 325 { 271 simplifstep.mT1 =auxface->validID();326 simplifstep.mT1 = auxface->validID(); 272 327 } 273 328 } … … 275 330 } 276 331 277 decim_data.push_back(simplifstep); // Simplification sequence that make reference to the simplification method sequence 332 // Simplification sequence that make reference to the 333 // simplification method sequence. 334 //decim_data.push_back(simplifstep); 335 336 // Adds new simplification step. 337 msimpseq->mSteps.push_back(simplifstep); 278 338 279 339 #ifdef SUPPORT_VCOLOR 280 // 281 // If the vertices are colored, color the new vertex 282 // using the average of the old colors. 340 // If the vertices are colored, color the new vertex 341 // using the average of the old colors. 283 342 v0->props->color += v1->props->color; 284 343 v0->props->color /= 2; 285 344 #endif 286 345 287 // Remove the pair that we just contracted346 // Remove the pair that we just contracted. 288 347 delete_pair(auxpair); 289 348 290 // Recalculate pairs associated with v0291 for (i=0; i<v0_info.pairs.length();i++)349 // Recalculate pairs associated with v0. 350 for (i = 0; i < v0_info.pairs.length(); i++) 292 351 { 293 352 simplif::pair_info *p = v0_info.pairs(i); … … 295 354 } 296 355 297 // Process pairs associated with now dead vertex 298 static simplif::pair_buffer condemned(6); // collect condemned pairs for execution 356 // Process pairs associated with now dead vertex. 357 // Collect condemned pairs for execution. 358 static simplif::pair_buffer condemned(6); 359 299 360 condemned.reset(); 300 361 301 for (i=0; i<v1_info.pairs.length(); i++)362 for (i = 0; i < v1_info.pairs.length(); i++) 302 363 { 303 364 simplif::pair_info *p = v1_info.pairs(i); 304 365 305 366 simplif::Vertex *u; 306 if( p->v0 == v1 ) 367 368 if( p->v0 == v1 ) 369 { 307 370 u = p->v1; 308 else 309 if( p->v1 == v1) 371 } 372 else 373 { 374 if( p->v1 == v1) 375 { 310 376 u = p->v0; 377 } 311 378 else 379 { 312 380 std::cerr << "YOW! This is a bogus pair." << endl; 313 314 if( !check_for_pair(u, v0) ) 315 { 316 p->v0 = v0; 317 p->v1 = u; 381 } 382 } 383 384 if ( !check_for_pair(u, v0) ) 385 { 386 p->v0 = v0; 387 p->v1 = u; 388 318 389 v0_info.pairs.add(p); 319 390 compute_pair_info(p); 320 391 } 321 392 else 393 { 322 394 condemned.add(p); 323 } 324 325 for(i=0; i<condemned.length(); i++) 395 } 396 } 397 398 for (i = 0; i < condemned.length(); i++) 399 { 326 400 // Do you have any last requests? 327 401 delete_pair(condemned(i)); 328 v1_info.pairs.reset(); // safety precaution 329 330 } 331 332 //--------------------------------------------------------------------------- 333 //--------------------------------------------------------------------------- 334 bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q) 402 } 403 404 // Safety precaution. 405 v1_info.pairs.reset(); 406 } 407 408 //--------------------------------------------------------------------------- 409 //--------------------------------------------------------------------------- 410 bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, 411 simplif::Mat4& Q) 335 412 { 336 413 if( vinfo.length() > 0 ) … … 340 417 } 341 418 else 419 { 342 420 return false; 421 } 343 422 } 344 423 … … 347 426 //--------------------------------------------------------------------------- 348 427 void SimplificationMethod::decimate_contract(simplif::Model& m) 428 { 429 simplif::heap_node *top; 430 simplif::pair_info *pair; 431 simplif::Vertex *v0; 432 simplif::Vertex *v1; 433 simplif::Vec3 candidate; 434 435 for (;;) 436 { 437 top = heap->extract(); 438 439 if (!top) 440 { 441 return; 442 } 443 444 pair = (simplif::pair_info *)top->obj; 445 446 // Remove all the vertices of the removed edges. 447 bool sharededge = false; 448 449 for (int i = 0; i < pair->v0->edgeUses().length(); i++) 450 { 451 simplif::Edge *econ = pair->v0->edgeUses()(i); 452 453 if (pair->v1->vID == econ->org()->vID 454 || 455 pair->v1->vID == econ->dest()->vID) 456 { 457 sharededge = true; 458 break; 459 } 460 } 461 462 if (pair->isValid() && sharededge) 463 { 464 break; 465 } 466 467 // Deletes repetitions of pair in heap. 468 delete_pair(pair); 469 } 470 471 // Copy pair. 472 v0 = new Vertex( pair->v0->operator[](0), 473 pair->v0->operator[](1), 474 pair->v0->operator[](2)); 475 v0->uniqID = pair->v0->uniqID; 476 477 v1 = new Vertex( pair->v1->operator[](0), 478 pair->v1->operator[](1), 479 pair->v1->operator[](2)); 480 481 v1->uniqID = pair->v1->uniqID; 482 483 candidate = pair->candidate; 484 485 // First contract. 486 simplifstep.obligatory = 0; 487 488 // Debug. 489 cout << "Contracting..." << endl; 490 491 // Contract main pair. 492 do_contract(m, pair); 493 494 // Twin contract and lonely vertex contract. 495 simplifstep.obligatory = 1; 496 497 // Attempt to maintain valid vertex information. 498 M0.validVertCount--; 499 500 // Find an edge of the same coordinates and different vertex identifiers. 501 removeTwinEdges(m,v0,v1,candidate); 502 503 // Move vertex of non twin edges. 504 contractLonelyVertices(m,v0,v1,candidate); 505 506 // Free Memory. 507 delete v0; 508 delete v1; 509 } 510 511 //--------------------------------------------------------------------------- 512 // Contract lonely vertex that has the same coords that the contracted one. 513 //--------------------------------------------------------------------------- 514 void SimplificationMethod::contractLonelyVertices( simplif::Model &m, simplif::Vertex *v0, 515 simplif::Vertex *v1, 516 simplif::Vec3 candidate) 517 { 518 bool lonely_vertex_found; 519 simplif::Vertex *vert; 520 simplif::Vertex *new_vert; 521 simplif::Vertex *lonely_vert; 522 simplif::Edge *edge; 523 simplif::heap_node *top; 524 simplif::pair_info *pair; 525 simplif::vert_info new_vert_info; 526 Geometry::GeoVertex vertex_added; 527 simplif::Vertex *take_bone_from_vert; 528 529 // Gets the vertex that is not the candidate. 530 if ((*v0) == candidate) 531 { 532 vert = v1; 533 take_bone_from_vert = v0; 534 } 535 else if ((*v1) == candidate) 536 { 537 vert = v0; 538 take_bone_from_vert = v1; 539 } 540 541 // For all vertex. 542 for (int i = 0; i < heap->getSize(); i++) 543 { 544 // Initialize lonely vertex flag. 545 lonely_vertex_found = false; 546 547 // Gets the current pair. 548 top = heap->getElement(i); 549 pair = (simplif::pair_info *)top->obj; 550 551 if (pair->v0->isValid() && (*vert) == (*pair->v0)) 552 { 553 lonely_vert = pair->v0; 554 lonely_vertex_found = true; 555 } 556 else if (pair->v1->isValid() && (*vert) == (*pair->v1)) 557 { 558 lonely_vert = pair->v1; 559 lonely_vertex_found = true; 560 } 561 562 // If a lonely vertex is found. Creates new vertex and contract. 563 if (lonely_vertex_found) 564 { 565 new_vert = m.newVertex(candidate[X],candidate[Y],candidate[Z]); 566 edge = m.newEdge(new_vert,lonely_vert); 567 568 // We assume here the there are the same number of vertices and texture coordinates and normals 569 570 // assign a texture coordinate for the vbertex 571 simplif::Vec2 newtexcoord = m.texcoord(lonely_vert->validID()); 572 m.in_TexCoord(newtexcoord); 573 574 // assign a normal coordinate for the vbertex 575 simplif::Vec3 newnormal = m.normal(lonely_vert->validID()); 576 m.in_Normal(newnormal); 577 578 // Adds new vertex information to simplification sequence. 579 vertex_added.id = new_vert->vID; 580 vertex_added.bonefrom = take_bone_from_vert->vID; 581 vertex_added.position = Vector3(candidate[X],candidate[Y],candidate[Z]); 582 583 simplif::Vec2 tc = m.texcoord(new_vert->validID()); 584 vertex_added.texcoord = Vector2(tc[X],tc[Y]); 585 586 simplif::Vec3 vn = m.normal(new_vert->validID()); 587 vertex_added.normal = Vector3(vn[X],vn[Y],vn[Z]); 588 589 msimpseq->mNewVertices.push_back(vertex_added); 590 591 pair = new_pair(new_vert,lonely_vert); 592 pair->candidate = candidate; 593 594 pair->notInHeap(); 595 /* vinfo(new_vert->validID()).Q = vinfo(lonely_vert->validID()).Q; 596 vinfo(new_vert->validID()).norm = vinfo(lonely_vert->validID()).norm;*/ 597 598 // Debug. 599 cout << "Contracting new pair..." << endl; 600 601 // Contract twin pair. 602 do_contract(m, pair); 603 M0.validVertCount--; 604 } 605 } 606 } 607 608 //--------------------------------------------------------------------------- 609 // Find a twin edge of the given one. 610 //--------------------------------------------------------------------------- 611 void SimplificationMethod::removeTwinEdges( simplif::Model &m, 612 simplif::Vertex *v0, 613 simplif::Vertex *v1, 614 simplif::Vec3 candidate) 615 { 616 bool twin_found; 617 simplif::heap_node *top; 618 simplif::pair_info *pair; 619 simplif::Vertex *aux_vert; 620 621 // Find a twin edge in heap. 622 for (int i = 0; i < heap->getSize(); i++) 623 { 624 // Initialize twin flag. 625 twin_found = false; 626 627 // Gets the current pair. 628 top = heap->getElement(i); 629 pair = (simplif::pair_info *)top->obj; 630 631 if (pair->v0->isValid()==false || pair->v1->isValid()==false) 632 continue; 633 634 if (v0->operator[](0) == pair->v0->operator[](0) 635 && 636 v0->operator[](1) == pair->v0->operator[](1) 637 && 638 v0->operator[](2) == pair->v0->operator[](2)) 639 { 640 if (v1->operator[](0) == pair->v1->operator[](0) 641 && 642 v1->operator[](1) == pair->v1->operator[](1) 643 && 644 v1->operator[](2) == pair->v1->operator[](2)) 645 { 646 // Debug. 647 cout << "Twin contracted (NO Swap)..." << endl; 648 649 // Active twin flag. 650 twin_found = true; 651 } 652 } 653 else 654 { 655 // If there is a twin edge in reverse order. 656 if (v0->operator[](0) == pair->v1->operator[](0) 657 && 658 v0->operator[](1) == pair->v1->operator[](1) 659 && 660 v0->operator[](2) == pair->v1->operator[](2)) 661 { 662 if (v1->operator[](0) == pair->v0->operator[](0) 663 && 664 v1->operator[](1) == pair->v0->operator[](1) 665 && 666 v1->operator[](2) == pair->v0->operator[](2)) 667 { 668 // Debug. 669 cout << "Twin contracted (Swap)..." << endl; 670 671 // Swap. 672 aux_vert = pair->v0; 673 pair->v0 = pair->v1; 674 pair->v1 = aux_vert; 675 676 // Active twin flag. 677 twin_found = true; 678 } 679 } 680 } 681 682 // If a twin edge has been found. 683 if (twin_found) 684 { 685 // Extract twin edge from heap. 686 top = heap->extract(i); 687 pair = (simplif::pair_info *)top->obj; 688 689 // Copy candidate. 690 pair->candidate = candidate; 691 692 // Contract twin pair. 693 do_contract(m, pair); 694 i--; 695 M0.validVertCount--; 696 } 697 } 698 } 699 700 //--------------------------------------------------------------------------- 701 //--------------------------------------------------------------------------- 702 simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) 703 { 704 simplif::vert_info& info = vertex_info(v); 705 706 simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); 707 708 if( simplif::will_weight_by_area ) 709 { 710 err /= info.norm; 711 } 712 713 return err; 714 } 715 716 //--------------------------------------------------------------------------- 717 // Extract the minimum cost of the the valid nodes (not simplified) 718 //--------------------------------------------------------------------------- 719 simplif::real SimplificationMethod::decimate_min_error() 349 720 { 350 721 simplif::heap_node *top; … … 353 724 for(;;) 354 725 { 355 top = heap->extract(); 356 if( !top ) return; 726 top = heap->top(); 727 728 if( !top ) 729 { 730 return -1.0; 731 } 732 357 733 pair = (simplif::pair_info *)top->obj; 358 734 359 //Remove all the vertices of the removed edges 360 bool sharededge= false; 361 for (int i=0; i<pair->v0->edgeUses().length(); i++) 362 { 363 simplif::Edge *econ = pair->v0->edgeUses()(i); 364 if (pair->v1->vID == econ->org()->vID || pair->v1->vID == econ->dest()->vID) 365 { 366 sharededge= true; 367 break; 368 } 369 370 } 371 372 if( pair->isValid() && sharededge) 735 if( pair->isValid() ) 736 { 373 737 break; 374 375 delete_pair(pair); 376 } 377 378 do_contract(m, pair); 379 380 M0.validVertCount--; // Attempt to maintain valid vertex information 381 } 382 383 //--------------------------------------------------------------------------- 384 //--------------------------------------------------------------------------- 385 simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) 386 { 387 simplif::vert_info& info = vertex_info(v); 388 389 simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); 390 391 if( simplif::will_weight_by_area ) 392 err /= info.norm; 393 394 return err; 395 } 396 397 //--------------------------------------------------------------------------- 398 // Extract the minimum cost of the the valid nodes (not simplified) 399 //--------------------------------------------------------------------------- 400 simplif::real SimplificationMethod::decimate_min_error() 401 { 402 simplif::heap_node *top; 403 simplif::pair_info *pair; 404 405 for(;;) 406 { 407 top = heap->top(); 408 if( !top ) return -1.0; 409 pair = (simplif::pair_info *)top->obj; 410 411 if( pair->isValid() ) 412 break; 738 } 413 739 414 740 top = heap->extract(); … … 442 768 int i,j; 443 769 444 vinfo.init(m.vertCount()); 445 446 if( simplif::will_use_vertex_constraint ) 447 for(i=0; i<m.vertCount(); i++) 448 { 449 simplif::Vertex *v = m.vertex(i); 450 if( v->isValid() ) 770 // Reserve memory for vert_info array. 771 vinfo.init(m.vertCount()*10); 772 773 if (simplif::will_use_vertex_constraint) 774 { 775 // For each vertex, calculate contraints. 776 for (i = 0; i < m.vertCount(); i++) 777 { 778 simplif::Vertex *v = m.vertex(i); 779 780 if (v->isValid()) 781 { 451 782 vertex_info(v).Q = simplif::quadrix_vertex_constraint(*v); 452 } 453 454 for(i=0; i<m.faceCount(); i++) 455 if( m.face(i)->isValid() ) 456 { 457 if( simplif::will_use_plane_constraint ) 783 } 784 } 785 } 786 787 // Sets the fill value of the vinfo buffer. 788 //vinfo.setFill(m.vertCount()); 789 790 // For each face, recalculate constraints. 791 for (i = 0; i < m.faceCount(); i++) 792 { 793 if (m.face(i)->isValid()) 794 { 795 if (simplif::will_use_plane_constraint) 458 796 { 459 797 simplif::Mat4 Q = simplif::quadrix_plane_constraint(*m.face(i)); 460 798 simplif::real norm = 0.0; 461 799 462 if ( simplif::will_weight_by_area)800 if (simplif::will_weight_by_area) 463 801 { 464 norm =m.face(i)->area();465 Q *=norm;802 norm = m.face(i)->area(); 803 Q *= norm; 466 804 } 467 805 468 for (j=0; j<3;j++)806 for (j = 0; j < 3; j++) 469 807 { 470 vertex_info(m.face(i)->vertex(j)).Q += Q; 471 vertex_info(m.face(i)->vertex(j)).norm += norm; 472 808 vertex_info(m.face(i)->vertex(j)).Q += Q; 809 vertex_info(m.face(i)->vertex(j)).norm += norm; 473 810 } 474 811 } 475 812 } 476 477 if( simplif::will_constrain_boundaries ) 478 { 479 for(i=0; i<m.edgeCount(); i++) 480 if( m.edge(i)->isValid() && simplif::check_for_discontinuity(m.edge(i)) ) 813 } 814 815 if (simplif::will_constrain_boundaries) 816 { 817 // For each edge, recalculate constraints. 818 for (i = 0; i < m.edgeCount(); i++) 819 { 820 if (m.edge(i)->isValid() && simplif::check_for_discontinuity(m.edge(i))) 481 821 { 482 822 simplif::Mat4 B = simplif::quadrix_discontinuity_constraint(m.edge(i)); 483 823 simplif::real norm = 0.0; 484 824 485 if ( simplif::will_weight_by_area)825 if (simplif::will_weight_by_area) 486 826 { 487 norm =simplif::norm2(*m.edge(i)->org() - *m.edge(i)->dest());488 B *=norm;827 norm = simplif::norm2(*m.edge(i)->org() - *m.edge(i)->dest()); 828 B *= norm; 489 829 } 490 830 … … 495 835 vertex_info(m.edge(i)->dest()).norm += norm; 496 836 } 497 } 498 837 } 838 } 839 840 // Create heap. 499 841 heap = new simplif::Heap(m.validEdgeCount); 500 842 501 843 int pair_count = 0; 502 844 503 for(i=0; i<m.edgeCount(); i++) 504 if( m.edge(i)->isValid() ) 845 // For each edge, add pairs to heap. 846 for (i = 0; i < m.edgeCount(); i++) 847 { 848 if (m.edge(i)->isValid()) 505 849 { 506 850 simplif::pair_info *pair = new_pair(m.edge(i)->org(), m.edge(i)->dest()); … … 509 853 pair_count++; 510 854 } 511 512 if( limit<0 ) 513 { 514 limit = m.bounds.radius * 0.05; 515 } 516 proximity_limit = limit * limit; 517 if( proximity_limit > 0 ) 855 } 856 857 if (limit < 0) 858 { 859 limit = m.bounds.radius * 0.05; 860 } 861 862 proximity_limit = limit * limit; 863 864 if (proximity_limit > 0) 518 865 { 519 866 simplif::ProxGrid grid(m.bounds.min, m.bounds.max, limit); 520 for(i=0; i<m.vertCount(); i++) 867 868 // Add points to grid. 869 for (i = 0; i < m.vertCount(); i++) 870 { 521 871 grid.addPoint(m.vertex(i)); 872 } 522 873 523 874 simplif::buffer<simplif::Vec3 *> nearby(32); 524 for(i=0; i<m.vertCount(); i++) 875 876 // For each vertex. 877 for (i = 0; i < m.vertCount(); i++) 525 878 { 526 879 nearby.reset(); 527 880 grid.proximalPoints(m.vertex(i), nearby); 528 881 529 for (j=0; j<nearby.length();j++)882 for (j = 0; j < nearby.length(); j++) 530 883 { 531 884 simplif::Vertex *v1 = m.vertex(i); 532 885 simplif::Vertex *v2 = (simplif::Vertex *)nearby(j); 533 886 534 if ( v1->isValid() && v2->isValid())887 if (v1->isValid() && v2->isValid()) 535 888 { 536 889 #ifdef SAFETY 537 890 assert(pair_is_valid(v1,v2)); 538 891 #endif 539 if ( !check_for_pair(v1,v2))892 if (!check_for_pair(v1,v2)) 540 893 { 541 894 simplif::pair_info *pair = new_pair(v1,v2); … … 544 897 } 545 898 } 546 547 899 } 548 900 } … … 555 907 void SimplificationMethod::simplifmethod_init(void) 556 908 { 557 int i;558 559 909 // Change mesh structure. 560 ge omesh2simplifModel();561 910 generateSimplifModel(); 911 562 912 M0.bounds.complete(); 563 913 … … 566 916 initialFaceCount = M0.faceCount(); 567 917 568 // Get rid of degenerate faces 569 for(i=0; i<M0.faceCount(); i++) 570 if( !M0.face(i)->plane().isValid() ) 918 // Get rid of degenerate faces. 919 for (int i = 0; i < M0.faceCount(); i++) 920 { 921 if (!M0.face(i)->plane().isValid()) 922 { 571 923 M0.killFace(M0.face(i)); 924 } 925 } 572 926 573 927 M0.removeDegeneracy(M0.allFaces()); 574 928 575 // Get rid of unused vertices 576 for(i=0; i<M0.vertCount(); i++) 577 { 578 if( M0.vertex(i)->edgeUses().length() == 0 ) 929 // Get rid of unused vertices. 930 for (int i = 0; i < M0.vertCount(); i++) 931 { 932 if (M0.vertex(i)->edgeUses().length() == 0) 933 { 579 934 M0.vertex(i)->kill(); 935 } 580 936 } 581 937 } … … 598 954 } 599 955 600 for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) 956 /* 957 for ( std::vector<MeshSimplificationSequence::Step>::iterator 958 it = decim_data.begin(); 959 it != decim_data.end(); 960 it++) 601 961 { 602 962 it->mModfaces.clear(); 603 963 } 964 604 965 decim_data.clear(); 605 606 while( M0.validFaceCount > finalfaces/*simplif::face_target*/ 607 && M0.validFaceCount > 1 608 && decimate_min_error() < simplif::error_tolerance ) 966 */ 967 968 // Debug. 969 cout << "M0.validFaceCount: " << M0.validFaceCount << endl; 970 cout << "finalfaces: " << finalfaces << endl; 971 972 while ( M0.validFaceCount > finalfaces/*simplif::face_target*/ 973 && M0.validFaceCount > 1 974 && decimate_min_error() < simplif::error_tolerance ) 609 975 { 610 976 decimate_contract(M0); … … 636 1002 previousValidVertCount = 0; 637 1003 638 percent = (float)(2.0 * 100.0) /(float)((M0.validFaceCount - numvertices / 3.0)); 1004 percent = (float)(2.0 * 100.0) 1005 / 1006 (float)((M0.validFaceCount - numvertices / 3.0)); 639 1007 640 1008 // Update progress bar. … … 644 1012 } 645 1013 646 for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) 1014 /* 1015 for ( std::vector<MeshSimplificationSequence::Step>::iterator 1016 it = decim_data.begin(); 1017 it != decim_data.end(); 1018 it++) 647 1019 { 648 1020 it->mModfaces.clear(); 649 1021 } 1022 650 1023 decim_data.clear(); 1024 */ 1025 1026 // Debug. 1027 cout << "M0.validVertCount: " << M0.validVertCount << endl; 1028 cout << "numvertices: " << numvertices << endl; 651 1029 652 1030 while( M0.validVertCount > numvertices /*simplif::face_target*/ … … 680 1058 { 681 1059 public: 1060 682 1061 float x,y,z; 683 _float3_(float x=0.0f, float y=0.0f, float z=0.0f){ this->x = x; this->y = y; this->z = z; } 684 _float3_(const _float3_ &f){ x=f.x; y=f.y; z=f.z; } 685 _float3_ & operator=(const _float3_ &f){ x=f.x; y=f.y; z=f.z; return *this; } 1062 1063 _float3_(float x=0.0f, float y=0.0f, float z=0.0f) 1064 { 1065 this->x = x; this->y = y; this->z = z; 1066 } 1067 1068 _float3_(const _float3_ &f) 1069 { 1070 x=f.x; y=f.y; z=f.z; 1071 } 1072 1073 _float3_ & operator=(const _float3_ &f) 1074 { 1075 x=f.x; y=f.y; z=f.z; return *this; 1076 } 1077 686 1078 bool operator<(const _float3_ &f) const 687 1079 { … … 696 1088 }; 697 1089 698 699 //--------------------------------------------------------------------------- 700 //Unify the model to work with it 701 //The vertices that have the same space coordinates are stored as an unique vertex 702 //in order to avoid holes in the simplification 703 //The necessary information to store correctly the decimation data is stored 704 //--------------------------------------------------------------------------- 705 void SimplificationMethod::geomesh2simplifModel(void) 706 { 707 std::map<_float3_,int> vertices_map; //Unique vertices (without repetitions) from all the submeshes 708 unsigned int num_final_verts = 0; //Number of unique vertices from all the submeshes 709 710 int num_final_verts2=0; 711 int num_repeated_verts=0; 712 713 int simplifindex; 714 for (size_t i=0; i<objmesh->mSubMeshCount; i++) //For all the submeshes 715 { 716 if (i != indexMeshLeaves) 717 { 718 //If the model has texture information 719 if (objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS) 720 { 721 //For all the vertices of each submesh 722 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++) 723 { 724 _float3_ auxvert(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 1090 //--------------------------------------------------------------------------- 1091 // Generate simplification model. 1092 //--------------------------------------------------------------------------- 1093 void SimplificationMethod::generateSimplifModel(void) 1094 { 1095 int face_index = 0; 1096 1097 // For each submesh. 1098 bool added_vertices = false; 1099 for (size_t i = 0; i < objmesh->mSubMeshCount; i++) 1100 { 1101 // If is not a tree leaves submesh. 1102 if (i != indexMeshLeaves) 1103 { 1104 // For all the vertices of each submesh. 1105 for ( size_t j = 0; 1106 j < objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1107 j++) 1108 { 1109 // If the vertex is not in vertices_map, then is added. 1110 M0.in_Vertex(simplif::Vec3( 1111 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 725 1112 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 726 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z); 727 728 std::map<_float3_,int>::iterator mit = vertices_map.find(auxvert); 729 if (mit==vertices_map.end()) 730 { 731 //If the vertex is not in vertices_map, then is added 732 simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 733 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 734 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 735 M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, 736 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 737 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 738 739 M0.in_TexCoord(simplif::Vec2(objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x, 740 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); 741 vertices_map[auxvert] = num_final_verts; 742 num_final_verts ++; 743 744 std::vector<int> v; 745 submeshmap[simplifindex]=v; 746 submeshmap[simplifindex].push_back((int)i); //Add the index of the mesh that contains the vertex 747 std::vector<int> v2; 748 vertexbuffermap[simplifindex]=v2; 749 vertexbuffermap[simplifindex].push_back((int)j); //Add the index of vertex in the submesh 750 751 // Debug 752 num_final_verts2++; 753 } 754 else 755 { 756 //the vertex is already in vertices_map, so store the reference of the first one with the same space coodinates 757 simplifindex = mit->second; 758 submeshmap[simplifindex].push_back((int)i); //Submeshes that contains the vertex 759 vertexbuffermap[simplifindex].push_back((int)j); //Indices of the vertex buffers that contains the vertex 760 761 // Debug 762 num_repeated_verts++; 763 } 764 } 765 } 766 else //the model has not texture information 767 { 768 //For all the vertices of each submesh 769 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++) 770 { 771 _float3_ auxvert(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 772 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 773 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z); 774 775 std::map<_float3_,int>::iterator mit = vertices_map.find(auxvert); 776 if (mit==vertices_map.end()) 777 { 778 //If the vertex is not in vertices_map, then is added 779 simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 780 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 781 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 782 M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, 783 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 784 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 785 M0.in_TexCoord(simplif::Vec2(0.0,0.0)); 786 vertices_map[auxvert] = num_final_verts; 787 num_final_verts ++; 788 789 std::vector<int> v; 790 submeshmap[simplifindex]=v; 791 submeshmap[simplifindex].push_back((int)i); //Add the index of the mesh that contains the vertex 792 std::vector<int> v2; 793 vertexbuffermap[simplifindex]=v2; 794 vertexbuffermap[simplifindex].push_back((int)j); //Add the index of vertex in the submesh 795 } 796 else 797 { 798 //the vertex is already in vertices_map, so store the reference of the first one with the same space coodinates 799 simplifindex = mit->second; 800 submeshmap[simplifindex].push_back((int)i); //Submeshes that contains the vertex 801 vertexbuffermap[simplifindex].push_back((int)j); //Indices of the vertex buffers that contains the vertex 802 } 803 } 804 } 805 if (objmesh->mSubMesh[i].mSharedVertexBuffer) 806 { 807 printf("Shared break"); 808 break; 809 } 810 } 811 } 812 813 //Create the faces 814 unsigned int base_index=0; 815 816 number_of_triangles=0; //Number of triangles of the model 817 int face_index=0; 818 819 int index_in_vertices_map0,index_in_vertices_map1,index_in_vertices_map2; 820 for (size_t i=0; i<objmesh->mSubMeshCount; i++) //For all the submeshes 821 { 822 if (i!=indexMeshLeaves) 823 { 824 for (Index j=0; j<objmesh->mSubMesh[i].mIndexCount; j+=3) 825 { 826 //+1 is required because the first index in the simplification method is 1 827 //Index of the vertex in vertices_map 828 Index indice0=objmesh->mSubMesh[i].mIndex[j]; 829 _float3_ auxvert0(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].x, 830 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].y, 831 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice0].z); 832 index_in_vertices_map0=vertices_map[auxvert0] + 1; 833 834 Index indice1=objmesh->mSubMesh[i].mIndex[j+1]; 835 _float3_ auxvert1(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].x, 836 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].y, 837 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice1].z); 838 index_in_vertices_map1=vertices_map[auxvert1] + 1; 839 840 Index indice2=objmesh->mSubMesh[i].mIndex[j+2]; 841 _float3_ auxvert2(objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].x, 842 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].y, 843 objmesh->mSubMesh[i].mVertexBuffer->mPosition[indice2].z); 844 index_in_vertices_map2=vertices_map[auxvert2] + 1; 845 846 //Create a triangle with its indices in vertices_map 847 face_index=M0.miin_Face(index_in_vertices_map0,index_in_vertices_map1,index_in_vertices_map2); 848 849 //igeo allows to identify the submesh that contains the triangle 850 M0.face(face_index)->igeo=(int)i; 851 } 852 } 853 } 854 number_of_triangles=face_index; 855 } 856 857 858 //--------------------------------------------------------------------------- 859 //Wrties a file with the vertices, triangles and the simplification sequence 860 //--------------------------------------------------------------------------- 861 void SimplificationMethod::WriteOBJ(void) 862 { 863 MeshSimplificationSequence::Step stepaux; 864 vector<int> v0,v1,submesh0,submesh1; 865 std::ofstream obj("salida.lod"); 866 obj << "begin" << std::endl; 867 //vertices 868 for (size_t i=0; i<objmesh->mSubMeshCount; i++) 869 { 870 for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++) 871 { 872 obj << "v " << objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x << " " << 873 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y << " " << 874 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z << " " << std::endl; 875 } 876 } 877 878 879 //faces 880 for (size_t i=0; i<objmesh->mSubMeshCount; i++) 881 { 882 for (size_t j=0; j<objmesh->mSubMesh[i].mIndexCount; j=j+3) 883 { 884 obj << "f " << objmesh->mSubMesh[i].mIndex[j]+1 << " " << 885 objmesh->mSubMesh[i].mIndex[j+1]+1 << " " << 886 objmesh->mSubMesh[i].mIndex[j+2]+1 << std::endl; 887 } 888 } 889 890 obj << "end" << std::endl; 891 892 for (unsigned i=0; i<decim_data.size(); i++) 893 { 894 stepaux = decim_data.operator [](i); 895 v0=vertexbuffermap[stepaux.mV0]; //vertexbuffer index 896 submesh0=submeshmap[stepaux.mV0]; //displacement for each submesh 897 v1=vertexbuffermap[stepaux.mV1]; 898 submesh1=submeshmap[stepaux.mV1]; 899 for (unsigned j=0; j<v0.size(); j++) 900 { 901 for (unsigned k=0; k<v1.size(); k++) 902 { 903 //obtain the index of the VertexBuffer of each submesh and the displacemente by submesh 904 obj << "v% " << v1.operator [](k)+first_index_submesh[submesh1.operator [](k)] << 905 " " << v0.operator [](j)+first_index_submesh[submesh0.operator [](j)] << " " << 906 stepaux.x << " " << stepaux.y << " " << stepaux.z << " " << stepaux.mT0 << " " << 907 stepaux.mT1 << " & "; 908 if (stepaux.mModfaces.size()>0) 909 { 910 unsigned int ii=0; 911 for (ii=0; ii<stepaux.mModfaces.size()-1; ii++) 912 { 913 obj << stepaux.mModfaces.operator [](ii) << " "; 914 } 915 obj << stepaux.mModfaces.operator [](ii) << std::endl; 916 } 917 else 918 { 919 obj << std::endl; 920 } 921 } 922 } 923 } 924 obj.close(); 925 } 926 927 //--------------------------------------------------------------------------- 928 //simplifmethod_init is executed to do some initalizations 929 //simplifmethod_run is executed to do the decimation 930 //Then, the simpified model is created and the decimation information is returned 1113 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 1114 1115 M0.in_Normal(simplif::Vec3( 1116 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, 1117 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 1118 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 1119 1120 M0.in_TexCoord(simplif::Vec2( 1121 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x, 1122 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); 1123 1124 added_vertices = true; 1125 } 1126 } 1127 1128 // If is shared vertex buffer object. 1129 if (objmesh->mSubMesh[i].mSharedVertexBuffer && added_vertices) 1130 { 1131 printf("Shared break"); 1132 break; 1133 } 1134 } 1135 1136 // For each submesh. 1137 for (size_t i = 0; i < objmesh->mSubMeshCount; i++) 1138 { 1139 if (i != indexMeshLeaves) 1140 { 1141 for (Index j = 0; j < objmesh->mSubMesh[i].mIndexCount; j += 3) 1142 { 1143 // +1 is required because the first index in the 1144 // simplification method is 1 1145 // Index of the vertex in vertices_map. 1146 Index index0 = objmesh->mSubMesh[i].mIndex[j]; 1147 1148 Index index1 = objmesh->mSubMesh[i].mIndex[j + 1]; 1149 1150 Index index2 = objmesh->mSubMesh[i].mIndex[j + 2]; 1151 1152 // Create a triangle with its indices in vertices_map. 1153 face_index = M0.miin_Face( 1154 index0, 1155 index1, 1156 index2); 1157 1158 // igeo allows to identify the submesh that contains the triangle. 1159 M0.face(face_index)->igeo = (int)i; 1160 } 1161 } 1162 } 1163 number_of_triangles = face_index; 1164 } 1165 1166 //--------------------------------------------------------------------------- 1167 // simplifmethod_init is executed to do some initalizations 1168 // simplifmethod_run is executed to do the decimation 1169 // Then, the simpified model is created and the decimation 1170 // information is returned. 931 1171 //--------------------------------------------------------------------------- 932 1172 MeshSimplificationSequence *SimplificationMethod::Decimate( float lod, … … 938 1178 if (simpliftype == 0) 939 1179 { 940 // Percentage option1180 // Percentage option. 941 1181 simplifmethod_run((int)(number_of_triangles*lod),upb); 942 1182 } 943 1183 else 944 1184 { 945 // Number of vertices option1185 // Number of vertices option. 946 1186 simplifmethod_runv((int)lod,upb); 947 1187 } … … 953 1193 typedef std::vector<int> REPINDLIST; 954 1194 955 MAPAINDIND *unique_verts_inds_by_geo = new MAPAINDIND[objmesh->mSubMeshCount]; 956 REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[objmesh->mSubMeshCount]; 1195 MAPAINDIND *unique_verts_inds_by_geo = 1196 new MAPAINDIND[objmesh->mSubMeshCount]; 1197 1198 REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[objmesh->mSubMeshCount]; 957 1199 958 1200 // Index counter by submesh. … … 964 1206 } 965 1207 966 967 1208 // Construct the model. 1209 std::cout << "M0.faceCount(): " << M0.faceCount() << std::endl; 968 1210 for (int i = 0; i < M0.faceCount(); i++) 969 1211 { … … 976 1218 977 1219 // Insert to each submesh its triangles. 978 std::map<int,int>::iterator findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); 1220 std::map<int,int>::iterator findit = 1221 unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); 1222 979 1223 if (findit == unique_verts_inds_by_geo[igeo].end()) 980 1224 { 981 1225 // If it is not added... add and map it. 982 unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = inextvert[igeo]; 1226 unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = 1227 inextvert[igeo]; 1228 983 1229 inextvert[igeo]++; 984 } 985 findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); 1230 } 1231 1232 findit = 1233 unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); 1234 986 1235 if (findit == unique_verts_inds_by_geo[igeo].end()) 987 1236 { 988 1237 // If it is not added... add and map it. 989 unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = inextvert[igeo]; 1238 unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = 1239 inextvert[igeo]; 990 1240 inextvert[igeo]++; 991 } 992 findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); 1241 } 1242 1243 findit = 1244 unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); 1245 993 1246 if (findit == unique_verts_inds_by_geo[igeo].end()) 994 1247 { 995 1248 // If it is not added... add and map it. 996 unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] = inextvert[igeo]; 1249 unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] = 1250 inextvert[igeo]; 997 1251 inextvert[igeo]++; 998 } 1252 } 999 1253 1000 1254 // Total number of indices by submesh. … … 1043 1297 bool copiedShared = false; 1044 1298 1299 // For each submesh. 1045 1300 for (size_t i = 0; i < objmesh->mSubMeshCount; i++) 1046 1301 { 1047 mGeoMesh->mSubMesh[i].mStripCount 1048 mGeoMesh->mSubMesh[i].mStrip 1302 mGeoMesh->mSubMesh[i].mStripCount = 0; 1303 mGeoMesh->mSubMesh[i].mStrip = NULL; 1049 1304 1050 1305 mGeoMesh-> … … 1060 1315 { 1061 1316 mGeoMesh->mSubMesh[i].mBones.push_back(objmesh-> 1062 mSubMesh[i].mBones[j]);1317 mSubMesh[i].mBones[j]); 1063 1318 } 1064 1319 } … … 1068 1323 // Indices vectors. 1069 1324 mGeoMesh->mSubMesh[i].mIndexCount = ver_inds_rep_by_geo[i].size(); 1325 1070 1326 mGeoMesh->mSubMesh[i].mIndex = new Index[mGeoMesh-> 1071 mSubMesh[i].mIndexCount];1327 mSubMesh[i].mIndexCount]; 1072 1328 1073 1329 // Store the indices. … … 1075 1331 { 1076 1332 // Obtain the indices that point at VertexBuffer. 1077 mGeoMesh->mSubMesh[i].mIndex[j] = unique_verts_inds_by_geo[i].operator [](ver_inds_rep_by_geo[i].operator [](j)); 1333 mGeoMesh->mSubMesh[i].mIndex[j] = 1334 unique_verts_inds_by_geo[i]. 1335 operator [](ver_inds_rep_by_geo[i]. 1336 operator [](j)); 1078 1337 } 1079 1338 … … 1096 1355 } 1097 1356 1098 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = unique_verts_inds_by_geo[i].size(); 1099 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1357 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = 1358 unique_verts_inds_by_geo[i].size(); 1359 1360 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = 1361 objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1100 1362 1101 1363 // Allocate memory for position, normal and texutre coordinates. 1102 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition = new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1103 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal = new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1104 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1364 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition = 1365 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1366 1367 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal = 1368 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1369 1370 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 1371 new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1105 1372 1106 1373 for ( MAPAINDIND::iterator mapit = unique_verts_inds_by_geo[i].begin(); … … 1114 1381 // Vertex coordinate. 1115 1382 // Construction of the submeshes. 1116 Vector3 v3((Real)M0.vertex(isrc)->operator [](0), 1117 (Real)M0.vertex(isrc)->operator [](1), 1118 (Real)M0.vertex(isrc)->operator [](2)); 1119 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[idst]=v3; 1383 Vector3 v3( (Real)M0.vertex(isrc)->operator [](0), 1384 (Real)M0.vertex(isrc)->operator [](1), 1385 (Real)M0.vertex(isrc)->operator [](2)); 1386 1387 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[idst] = v3; 1120 1388 1121 1389 // Normal coordinates. 1122 Vector3 v3n((Real)M0.normal(isrc)(0), (Real)M0.normal(isrc)(1), (Real)M0.normal(isrc)(2)); 1123 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[idst]=v3n; 1390 Vector3 v3n( (Real)M0.normal(isrc)(0), 1391 (Real)M0.normal(isrc)(1), 1392 (Real)M0.normal(isrc)(2)); 1393 1394 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[idst] = v3n; 1124 1395 1125 1396 // Texture coordinates. 1126 Vector2 v2((Real)M0.texcoord(isrc)(0), (Real)M0.texcoord(isrc)(1)); 1127 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst]=v2; 1397 Vector2 v2( (Real)M0.texcoord(isrc)(0), 1398 (Real)M0.texcoord(isrc)(1)); 1399 1400 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst] = v2; 1128 1401 } 1129 1402 } … … 1131 1404 { 1132 1405 // Leaves mesh. 1133 mGeoMesh->mSubMesh[i].mIndexCount=objmesh->mSubMesh[i].mIndexCount; 1134 mGeoMesh->mSubMesh[i].mIndex=new Index[mGeoMesh->mSubMesh[i].mIndexCount]; 1406 mGeoMesh->mSubMesh[i].mIndexCount = objmesh->mSubMesh[i].mIndexCount; 1407 1408 mGeoMesh->mSubMesh[i].mIndex = new Index[mGeoMesh->mSubMesh[i].mIndexCount]; 1135 1409 1136 1410 // Copy the leaves submesh indexes. 1137 for (unsigned int j=0; j<mGeoMesh->mSubMesh[i].mIndexCount;j++)1411 for (unsigned int j = 0; j < mGeoMesh->mSubMesh[i].mIndexCount; j++) 1138 1412 { 1139 mGeoMesh->mSubMesh[i].mIndex[j] =objmesh->mSubMesh[i].mIndex[j];1413 mGeoMesh->mSubMesh[i].mIndex[j] = objmesh->mSubMesh[i].mIndex[j]; 1140 1414 } 1141 1415 1142 1416 // Copy the leaves submesh vertices. 1143 mGeoMesh->mSubMesh[i].mVertexBuffer=new VertexBuffer(); 1144 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount=objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1145 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo=objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1417 mGeoMesh->mSubMesh[i].mVertexBuffer = new VertexBuffer(); 1418 1419 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = 1420 objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1421 1422 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = 1423 objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1146 1424 1147 1425 // Allocate memory for position, normal and texture coordinates. 1148 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition=new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1149 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal=new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1150 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords=new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1426 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition = 1427 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1428 1429 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal = 1430 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1431 1432 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 1433 new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1151 1434 1152 1435 for ( unsigned int j = 0; … … 1155 1438 { 1156 1439 // Position. 1157 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 1158 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 1159 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z=objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 1440 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x = 1441 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 1442 1443 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y = 1444 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 1445 1446 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z = 1447 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 1160 1448 1161 1449 // Normals. 1162 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x; 1163 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y; 1164 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z=objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z; 1450 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x = 1451 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x; 1452 1453 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y = 1454 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y; 1455 1456 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z = 1457 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z; 1165 1458 1166 1459 // Textures. 1167 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x=objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x; 1168 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y=objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 1460 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x = 1461 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x; 1462 1463 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y = 1464 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 1169 1465 } 1170 1466 } … … 1176 1472 1177 1473 // Store the simplification steps in MeshSimplificationSequence. 1178 int 1474 int acum = 0; 1179 1475 1180 1476 for (size_t i = 0; i < objmesh->mSubMeshCount; i++) … … 1192 1488 } 1193 1489 1194 MeshSimplificationSequence::Step stepaux, newstep; 1195 vector<int> v0,v1,submesh0,submesh1; 1196 MeshSimplificationSequence *msimpseq; 1197 1198 msimpseq = new MeshSimplificationSequence(); 1199 1200 for (unsigned i = 0; i < decim_data.size(); i++) 1201 { 1202 stepaux = decim_data.operator [](i); 1203 v0 = vertexbuffermap[stepaux.mV0]; // vertexbuffer index 1204 submesh0 = submeshmap[stepaux.mV0]; // displacement by submesh 1205 v1 = vertexbuffermap[stepaux.mV1]; 1206 submesh1 = submeshmap[stepaux.mV1]; 1207 1208 for (unsigned j = 0; j < v0.size(); j++) 1209 { 1210 for (unsigned k = 0; k < v1.size(); k++) 1211 { 1212 // Obtain the submesh index in VertexBuffer 1213 // and the displacement by submesh 1214 newstep.mV0 = v0.operator [](j)+first_index_submesh[submesh0.operator [](j)]; 1215 newstep.mV1 = v1.operator [](k)+first_index_submesh[submesh1.operator [](k)]; 1216 newstep.x = stepaux.x; 1217 newstep.y = stepaux.y; 1218 newstep.z = stepaux.z; 1219 1220 // mT0 y mT1 are unique triangles identifiers 1221 // returned by the simplification method 1222 newstep.mT0 = stepaux.mT0; 1223 newstep.mT1 = stepaux.mT1; 1224 1225 if (j == v0.size() - 1 && k == v1.size() - 1) 1226 { 1227 newstep.obligatorio = 0; 1228 } 1229 else 1230 { 1231 newstep.obligatorio = 1; 1232 } 1233 1234 if (j == 0 && k == 0) 1235 { 1236 // First simplification step. 1237 // unique triangle identifiers 1238 // returned by the simplification method. 1239 newstep.mModfaces = stepaux.mModfaces; 1240 } 1241 else 1242 { 1243 std::vector<Index> vvacio; 1244 1245 newstep.mModfaces = vvacio; 1246 } 1247 1248 msimpseq->mSteps.push_back(newstep); 1249 } 1250 } 1251 } 1252 1253 // WriteOBJ(); 1490 vector<int> v0; 1491 vector<int> v1; 1492 vector<int> submesh0; 1493 vector<int> submesh1; 1494 1254 1495 return msimpseq; 1255 1496 } -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/SimplificationMethod.h
r1025 r1526 9 9 #include <GeoMeshSimplifier.h> 10 10 11 using namespace Geometry; 12 11 13 class SimplificationMethod 12 14 { 13 private: 14 Geometry::Mesh *mGeoMesh; 15 void compute_pair_info(simplif::pair_info*); 16 simplif::Model M0; 17 simplif::array<simplif::vert_info> vinfo; 18 simplif::Heap *heap; 19 simplif::real proximity_limit; // distance threshold squared 20 int initialVertCount; 21 int initialEdgeCount; 22 int initialFaceCount; 15 private: 23 16 24 inline simplif::vert_info& vertex_info(simplif::Vertex *v) 25 { 26 return vinfo(v->validID()); 27 } 28 29 simplif::real pair_mesh_penalty(simplif::Model& M, 30 simplif::Vertex *v1, 31 simplif::Vertex *v2, 32 simplif::Vec3& vnew); 33 34 int predict_face(simplif::Face& F, 35 simplif::Vertex *v1, 36 simplif::Vertex *v2, 37 simplif::Vec3& vnew, 38 simplif::Vec3& f1, 39 simplif::Vec3& f2, 40 simplif::Vec3& f3); 41 42 bool check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1); 43 44 simplif::pair_info *new_pair(simplif::Vertex *v0, simplif::Vertex *v1); 45 46 void delete_pair(simplif::pair_info *pair); 47 48 void do_contract(simplif::Model& m, simplif::pair_info *pair); 49 50 bool decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q); 51 52 void decimate_contract(simplif::Model& m); 53 54 simplif::real decimate_error(simplif::Vertex *v); 55 56 simplif::real decimate_min_error(); 57 58 simplif::real decimate_max_error(simplif::Model& m); 59 60 void decimate_init(simplif::Model& m, simplif::real limit); 61 62 bool pair_is_valid(simplif::Vertex *u, simplif::Vertex *v); 63 64 void simplifmethod_run(int, Geometry::TIPOFUNC upb=0); 65 66 void simplifmethod_runv(int, Geometry::TIPOFUNC upb=0); 67 68 void simplifmethod_init(void); 17 Geometry::Mesh *mGeoMesh; 69 18 70 // To map the mesh with de simplification method structure. 71 // Submeshes which pertains each vertex. 72 std::map<int,std::vector<int> > submeshmap; 73 74 // Vertices of the VertexBuffer that point 75 // at this vertex of the simplification method. 76 std::map<int,std::vector<int> > vertexbuffermap; 19 void compute_pair_info(simplif::pair_info*); 77 20 78 // Simplification sequence of the simplification method. 79 std::vector<Geometry::MeshSimplificationSequence::Step> decim_data; 80 81 std::string meshName; 82 83 void WriteOBJ(void); 84 85 unsigned int *first_index_submesh; 86 87 //std::vector<simplif::pair_info *> pointers_to_remove; 21 simplif::Model M0; 88 22 89 int indexMeshLeaves; 90 91 public: 92 93 const Geometry::Mesh *objmesh; 94 95 // Total number of triangles of all the submeshes. 96 int number_of_triangles; 97 98 SimplificationMethod(const Geometry::Mesh *m); 99 100 void geomesh2simplifModel(void); 101 102 Geometry::MeshSimplificationSequence *Decimate( float lod, 103 int simpliftype, 104 Geometry::TIPOFUNC upb=0); 105 106 void setMeshLeaves(int meshLeaves); 23 simplif::array<simplif::vert_info> vinfo; 107 24 108 /// Gets mesh simplified. 109 Geometry::Mesh * GetMesh(); 110 111 ///Destructor 112 ~SimplificationMethod(); 25 simplif::Heap *heap; 26 simplif::real proximity_limit; // distance threshold squared 27 28 int initialVertCount; 29 int initialEdgeCount; 30 int initialFaceCount; 31 32 // Simplification sequence. 33 MeshSimplificationSequence *msimpseq; 34 35 // Stores the simplification step. 36 MeshSimplificationSequence::Step simplifstep; 37 38 inline simplif::vert_info& vertex_info(simplif::Vertex *v) 39 { 40 return vinfo(v->validID()); 41 } 42 43 simplif::real pair_mesh_penalty(simplif::Model& M, 44 simplif::Vertex *v1, 45 simplif::Vertex *v2, 46 simplif::Vec3& vnew); 47 48 int predict_face(simplif::Face& F, 49 simplif::Vertex *v1, 50 simplif::Vertex *v2, 51 simplif::Vec3& vnew, 52 simplif::Vec3& f1, 53 simplif::Vec3& f2, 54 simplif::Vec3& f3); 55 56 bool check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1); 57 58 simplif::pair_info *new_pair(simplif::Vertex *v0, simplif::Vertex *v1); 59 60 void delete_pair(simplif::pair_info *pair); 61 62 void do_contract(simplif::Model& m, simplif::pair_info *pair); 63 64 bool decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q); 65 66 void decimate_contract(simplif::Model& m); 67 68 simplif::real decimate_error(simplif::Vertex *v); 69 70 simplif::real decimate_min_error(); 71 72 simplif::real decimate_max_error(simplif::Model& m); 73 74 void decimate_init(simplif::Model& m, simplif::real limit); 75 76 bool pair_is_valid(simplif::Vertex *u, simplif::Vertex *v); 77 78 void simplifmethod_run(int, Geometry::TIPOFUNC upb=0); 79 80 void simplifmethod_runv(int, Geometry::TIPOFUNC upb=0); 81 82 void simplifmethod_init(void); 83 84 // To map the mesh with de simplification method structure. 85 // Submeshes which pertains each vertex. 86 std::map< int, std::vector<int> > submeshmap; 87 88 // Vertices of the VertexBuffer that point 89 // at this vertex of the simplification method. 90 std::map< int, std::vector<int> > vertexbuffermap; 91 92 // Simplification sequence of the simplification method. 93 std::vector<Geometry::MeshSimplificationSequence::Step> decim_data; 94 95 std::string meshName; 96 97 unsigned int *first_index_submesh; 98 99 //std::vector<simplif::pair_info *> pointers_to_remove; 100 101 int indexMeshLeaves; 102 103 // Contract lonely vertices that have same coords than the contracted one. 104 void contractLonelyVertices( simplif::Model &m, 105 simplif::Vertex *v0, 106 simplif::Vertex *v1, 107 simplif::Vec3 candidate); 108 109 // Remove twin edges. 110 void removeTwinEdges(simplif::Model &m, 111 simplif::Vertex *v0, 112 simplif::Vertex *v1, 113 simplif::Vec3 candidate); 114 115 public: 116 117 const Geometry::Mesh *objmesh; 118 119 // Total number of triangles of all the submeshes. 120 int number_of_triangles; 121 122 SimplificationMethod(const Geometry::Mesh *m); 123 124 void generateSimplifModel(void); 125 126 Geometry::MeshSimplificationSequence *Decimate( float lod, 127 int simpliftype, 128 Geometry::TIPOFUNC upb=0); 129 130 void setMeshLeaves(int meshLeaves); 131 132 /// Gets mesh simplified. 133 Geometry::Mesh * GetMesh(); 134 135 /// Destructor. 136 ~SimplificationMethod(); 113 137 }; 114 138 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/avars.cxx
r1025 r1526 19 19 bool simplif::will_preserve_mesh_quality = true; 20 20 bool simplif::will_constrain_boundaries = true; 21 simplif::real simplif::boundary_constraint_weight = 1 .0;21 simplif::real simplif::boundary_constraint_weight = 10.0; 22 22 23 23 bool simplif::will_weight_by_area = false; -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.cxx
r1025 r1526 85 85 } 86 86 87 88 89 90 87 //////////////////////////////////////////////////////////////////////// 91 88 // -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/geom/3D.h
r1025 r1526 63 63 }; 64 64 65 66 65 // 67 66 // A triangular face in 3D (ie. a 2-simplex in E3) … … 94 93 } 95 94 96 97 98 95 // GFXGEOM_3D_INCLUDED 99 96 #endif -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Array.h
r1025 r1526 8 8 { 9 9 template<class T> 10 class array { 11 protected: 12 T *data; 13 int len; 14 public: 15 array() { data=NULL; len=0; } 16 array(int l) { init(l); } 17 ~array() { f_r_e_e(); } 10 class array 11 { 12 protected: 13 T *data; 14 int len; 15 public: 16 array() { data=NULL; len=0; } 17 array(int l) { init(l); } 18 ~array() { f_r_e_e(); } 18 19 20 inline void init(int l); 21 inline void f_r_e_e(); 22 inline void resize(int l); 19 23 20 inline void init(int l);21 inline void f_r_e_e();22 inline void resize(int l);24 inline T& ref(int i); 25 inline T& operator[](int i) { return data[i]; } 26 inline T& operator()(int i) { return ref(i); } 23 27 24 inline T& ref(int i);25 inline T& operator[](int i){ return data[i]; }26 inline T& operator()(int i){ return ref(i); }28 inline const T& ref(int i) const; 29 inline const T& operator[](int i) const { return data[i]; } 30 inline const T& operator()(int i) const { return ref(i); } 27 31 28 inline const T& ref(int i) const; 29 inline const T& operator[](int i) const { return data[i]; } 30 inline const T& operator()(int i) const { return ref(i); } 31 32 33 inline int length() const { return len; } 34 inline int maxLength() const { return len; } 35 }; 32 inline int length() const { return len; } 33 inline int maxLength() const { return len; } 34 }; 36 35 37 36 template<class T> 38 inline void array<T>::init(int l)39 {40 data = new T[l];41 len = l;42 }37 inline void array<T>::init(int l) 38 { 39 data = new T[l]; 40 len = l; 41 } 43 42 44 43 template<class T> 45 inline void array<T>::f_r_e_e() 46 { 47 if( data ) 44 inline void array<T>::f_r_e_e() 48 45 { 49 delete[] data; 50 data = NULL; 46 if( data ) 47 { 48 delete[] data; 49 data = NULL; 50 } 51 51 } 52 }53 52 54 53 template<class T> 55 inline T& array<T>::ref(int i)56 {57 58 assert( data );59 assert( i>=0 && i<len );60 61 return data[i];62 }54 inline T& array<T>::ref(int i) 55 { 56 #ifdef SAFETY 57 assert( data ); 58 assert( i>=0 && i<len ); 59 #endif 60 return data[i]; 61 } 63 62 64 63 template<class T> 65 inline const T& array<T>::ref(int i) const66 {67 68 assert( data );69 assert( i>=0 && i<len );70 71 return data[i];72 }64 inline const T& array<T>::ref(int i) const 65 { 66 #ifdef SAFETY 67 assert( data ); 68 assert( i>=0 && i<len ); 69 #endif 70 return data[i]; 71 } 73 72 74 73 template<class T> 75 inline void array<T>::resize(int l)76 {77 T *old = data;78 data = new T[l];79 data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T));80 len = l;81 delete[] old;82 }74 inline void array<T>::resize(int l) 75 { 76 T *old = data; 77 data = new T[l]; 78 data = (T *)memcpy(data,old,MIN(len,l)*sizeof(T)); 79 len = l; 80 delete[] old; 81 } 83 82 } 84 83 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Buffer.h
r1025 r1526 25 25 inline int length() const { return fill; } 26 26 inline int maxLength() const { return len; } 27 inline void setFill(int l){fill = l;} 27 28 }; 28 29 29 30 30 template<class T> -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/Heap.h
r1025 r1526 16 16 class Heapable 17 17 { 18 private: 19 int token; 18 private: 20 19 21 public: 22 Heapable() { notInHeap(); } 20 int token; 23 21 24 inline int isInHeap() { return token!=NOT_IN_HEAP; } 25 inline void notInHeap() { token = NOT_IN_HEAP; } 26 inline int getHeapPos() { return token; } 27 inline void setHeapPos(int t) { token=t; } 22 public: 23 24 Heapable() { notInHeap(); } 25 26 inline int isInHeap() { return token!=NOT_IN_HEAP; } 27 inline void notInHeap() { token = NOT_IN_HEAP; } 28 inline int getHeapPos() { return token; } 29 inline void setHeapPos(int t) { token=t; } 28 30 }; 29 31 32 class heap_node 33 { 34 public: 35 float import; 36 Heapable *obj; 30 37 31 class heap_node { 32 public: 33 float import; 34 Heapable *obj; 35 36 heap_node() { obj=NULL; import=0.0; } 37 heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 38 heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 38 heap_node() { obj=NULL; import=0.0; } 39 heap_node(Heapable *t, float i=0.0) { obj=t; import=i; } 40 heap_node(const heap_node& h) { import=h.import; obj=h.obj; } 39 41 }; 40 42 41 42 43 class Heap : public array<heap_node> { 44 43 class Heap : public array<heap_node> 44 { 45 45 // 46 46 // The actual size of the heap. array::length() … … 57 57 void downheap(int i); 58 58 59 public:59 public: 60 60 61 61 Heap() { size=0; } 62 62 Heap(int s) : array<heap_node>(s) { size=0; } 63 64 63 65 64 void insert(Heapable *, float); … … 67 66 68 67 heap_node *extract(); 68 69 // Overhead method. 70 heap_node *extract(int i); 71 69 72 heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); } 70 73 heap_node *kill(int i); 74 75 // Gets heap size. 76 int getSize(){return size;} 77 78 // Gets element at position i. 79 heap_node *getElement(int i){return &ref(i);} 80 71 81 }; 72 82 73 83 } 74 84 75 76 85 // GFXTOOLS_HEAP_INCLUDED 77 86 #endif 87 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/tools/heap.cxx
r1025 r1526 7 7 void Heap::swap(int i,int j) 8 8 { 9 9 heap_node tmp = ref(i); 10 10 11 12 11 ref(i) = ref(j); 12 ref(j) = tmp; 13 13 14 15 14 ref(i).obj->setHeapPos(i); 15 ref(j).obj->setHeapPos(j); 16 16 } 17 17 18 18 void Heap::upheap(int i) 19 19 { 20 20 if( i==0 ) return; 21 21 22 if( ref(i).import > ref(parent(i)).import ) { 23 swap(i,parent(i)); 24 upheap(parent(i)); 25 } 22 if( ref(i).import > ref(parent(i)).import ) 23 { 24 swap(i,parent(i)); 25 upheap(parent(i)); 26 } 26 27 } 27 28 28 29 void Heap::downheap(int i) 29 30 { 30 31 if (i>=size) return; // perhaps just extracted the last 31 32 32 33 34 33 int largest = i, 34 l = left(i), 35 r = right(i); 35 36 36 37 37 if( l<size && ref(l).import > ref(largest).import ) largest = l; 38 if( r<size && ref(r).import > ref(largest).import ) largest = r; 38 39 39 40 41 42 40 if( largest != i ) { 41 swap(i,largest); 42 downheap(largest); 43 } 43 44 } 44 45 46 45 47 46 void Heap::insert(Heapable *t,float v) 48 47 { 49 50 51 std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl;52 resize(2*size);53 48 if( size == maxLength() ) 49 { 50 std::cerr << "NOTE: Growing heap from " << size << " to " << 2*size << std::endl; 51 resize(2*size); 52 } 54 53 55 54 int i = size++; 56 55 57 58 56 ref(i).obj = t; 57 ref(i).import = v; 59 58 60 59 ref(i).obj->setHeapPos(i); 61 60 62 61 upheap(i); 63 62 } 64 63 65 64 void Heap::update(Heapable *t,float v) 66 65 { 67 66 int i = t->getHeapPos(); 68 67 69 70 68 if( i >= size ) 69 { 71 70 std::cerr << "WARNING: Attempting to update past end of heap!" << std::endl; 72 71 return; 73 74 75 72 } 73 else if( i == NOT_IN_HEAP ) 74 { 76 75 std::cerr << "WARNING: Attempting to update object not in heap!" << std::endl; 77 76 return; 78 77 } 79 78 80 81 79 float old=ref(i).import; 80 ref(i).import = v; 82 81 83 84 85 86 82 if( v<old ) 83 downheap(i); 84 else 85 upheap(i); 87 86 } 88 89 90 87 91 88 heap_node *Heap::extract() 92 89 { 93 90 if( size<1 ) return 0; 94 91 95 96 92 swap(0,size-1); 93 size--; 97 94 98 95 downheap(0); 99 96 100 97 ref(size).obj->notInHeap(); 101 98 102 return &ref(size); 99 return &ref(size); 100 } 101 102 // Overhead method. 103 heap_node *Heap::extract(int i) 104 { 105 if( size<1 ) return 0; 106 107 swap(i,size-1); 108 size--; 109 110 downheap(i); 111 112 ref(size).obj->notInHeap(); 113 114 return &ref(size); 103 115 } 104 116 105 117 heap_node *Heap::kill(int i) 106 118 { 107 119 if( i>=size ) 108 120 std::cerr << "WARNING: Attempt to delete invalid heap node." << std::endl; 109 121 110 111 112 122 swap(i, size-1); 123 size--; 124 ref(size).obj->notInHeap(); 113 125 114 126 if( ref(i).import < ref(size).import ) 115 127 downheap(i); 116 128 else 117 129 upheap(i); 118 130 131 return &ref(size); 132 } 119 133 120 return &ref(size);121 } -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/Leaf.cpp
r1026 r1526 10 10 vertsLeaf[0] = vertsLeaf[1] = vertsLeaf[2] = vertsLeaf[3] =0; 11 11 center[0] = center[1] = center[2] = 0; 12 normal[0] = normal[1] = normal[2] = 0;12 // normal[0] = normal[1] = normal[2] = 0; 13 13 leafNear=-1; 14 14 parentLeafCount = 1; … … 40 40 for ( int i=0;i<3;i++){ 41 41 center[i] = aLeaf.center[i]; 42 normal[i] = aLeaf.normal[i];42 // normal[i] = aLeaf.normal[i]; 43 43 } 44 44 for (i = 0L; i < 4; i++) … … 53 53 54 54 55 RuntimeLeaf::RuntimeLeaf(void) 56 { 57 vertsLeaf[0] = vertsLeaf[1] = vertsLeaf[2] = vertsLeaf[3] = 0; 58 parent = root = childLeft = childRight = -1; 59 } 55 60 56 61 //-------------------------------------------------------------------------------------------------------------------------------- 57 // Destructor. We must deallocate the memory allocated for pointers to vertices and edges62 // Copy constructor 58 63 //-------------------------------------------------------------------------------------------------------------------------------- 59 Leaf::~Leaf (void)64 RuntimeLeaf::RuntimeLeaf (const RuntimeLeaf& aLeaf) 60 65 { 66 for (int i = 0L; i < 4; i++) 67 vertsLeaf[i] = aLeaf.vertsLeaf[i]; 68 parent = aLeaf.parent; 69 childLeft = aLeaf.childLeft; 70 childRight = aLeaf.childRight; 71 root = aLeaf.root; 61 72 } 62 73 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/VertexData.cpp
r1105 r1526 1 1 #include "VertexData.h" 2 2 3 Geometry::DefaultVertexData::DefaultVertexData(unsigned int numv):Geometry::VertexData(numv) 3 /* 4 void Geometry::DefaultIndexData::Init(unsigned int numi) 4 5 { 5 v = new float[numv*3]; 6 n = new float[numv*3]; 7 t = new float[numv*2]; 8 } 9 Geometry::DefaultVertexData::~DefaultVertexData(void) 10 { 11 /* delete[] v; 12 delete[] n; 13 delete[] t;*/ 14 } 15 16 Geometry::VertexData *Geometry::DefaultVertexDataCreator(unsigned int numv) 17 { 18 return new DefaultVertexData(numv); 19 } 20 21 22 Geometry::DefaultIndexData::DefaultIndexData(unsigned int numi):Geometry::IndexData(numi) 23 { 24 indices = new unsigned int[numi]; 6 indices = new unsigned int[numi]; 25 7 } 26 8 Geometry::DefaultIndexData::~DefaultIndexData(void) 27 9 { 28 /* delete[] indices;*/29 10 } 30 11 31 12 Geometry::IndexData *Geometry::DefaultIndexDataCreator(unsigned int numi) 32 13 { 33 return new DefaultIndexData(numi); 14 IndexData *ret = new DefaultIndexData(); 15 // ret->Init(numi); 16 return ret; 34 17 } 35 18 36 37 /*Geometry::DefaultMultiIndexData::DefaultMultiIndexData(unsigned int numprims, unsigned int *numi):Geometry::MultiIndexData(numprims,numi)38 {39 indices = new unsigned int*[numprims];40 for (unsigned int i=0; i<numprims; i++)41 indices[i]=new unsigned int[numi[i]];42 }43 Geometry::DefaultMultiIndexData::~DefaultMultiIndexData(void)44 {45 for (unsigned int i=0; i<GetNumPrims(); i++)46 delete[] indices[i];47 delete[] indices;48 }49 50 Geometry::MultiIndexData *Geometry::DefaultMultiIndexDataCreator(unsigned int numprims, unsigned int *numi)51 {52 return new DefaultMultiIndexData(numprims,numi);53 }54 19 */ -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/foliage.cpp
r1083 r1526 10 10 // Parameters --> None 11 11 //-------------------------------------------------------------------------------------------------------------------------------- 12 Foliage::Foliage(const Geometry::SubMesh *leavesSubMesh, const Geometry::TreeSimplificationSequence * simpSeq, Geometry::CREATEVERTEXDATAFUNC vdfun, Geometry::CREATEINDEXDATAFUNC idfun): 12 Foliage::Foliage(int leavessubmeshID, 13 const Geometry::SubMesh *leavesSubMesh, 14 const Geometry::TreeSimplificationSequence * simpSeq/*, 15 Geometry::CREATEVERTEXDATAFUNC vdfun*/): 13 16 Acth(NULL), 14 create_vertex_data_func(vdfun==NULL?Geometry::DefaultVertexDataCreator:vdfun),15 create_index_data_func(idfun==NULL?Geometry::DefaultIndexDataCreator:idfun),16 vertexdata(NULL),Leaves(NULL), MinDet(NULL)17 //create_vertex_data_func(vdfun==NULL?Geometry::DefaultVertexDataCreator:vdfun), 18 //create_index_data_func(idfun==NULL?Geometry::DefaultIndexDataCreator:idfun), 19 /*vertexdata(NULL),*/ Leaves(NULL), MinDet(NULL) 17 20 { 18 21 begin = final = -1; 19 20 ReadVertices(leavesSubMesh); 22 // indexdata=NULL; 23 24 // ReadVertices(leavesSubMesh); 25 int countv= int(leavesSubMesh->mVertexBuffer->mVertexCount); 26 Leaves = new RuntimeLeaf[countv*2]; 27 TotalVerts = countv; 28 21 29 ReadLeafs(leavesSubMesh); 22 30 if (!ReadSimpSeq(simpSeq)) exit(1); 23 31 FillRoot(); 24 CalculateTexCoordsAndNorms();25 26 indexdata->SetNumValidIndices(0);32 // CalculateTexCoordsAndNorms(); 33 34 // indexdata->SetNumValidIndices(0); 27 35 28 36 int h=0; … … 44 52 final = leafCount-1; 45 53 active_leaf_count = leafCount; 54 leavesSubMeshID=leavessubmeshID; 46 55 } 47 56 … … 51 60 Foliage::~Foliage (void) 52 61 { 53 if (vertexdata) delete vertexdata;54 if (indexdata) delete indexdata;62 // if (vertexdata) delete vertexdata; 63 // if (indexdata) delete indexdata; 55 64 delete[] Leaves; 56 65 delete MinDet; … … 222 231 223 232 } 224 233 /* 225 234 void Foliage::ReadVertices(const Geometry::SubMesh *submesh) 226 235 { … … 228 237 vertexdata = create_vertex_data_func(2*countv); 229 238 Leaves = new Leaf[countv*2]; 230 indexdata = create_index_data_func(countv*2*3); // 3 indices x 2 triangulos x hoja239 //indexdata = create_index_data_func(countv*2*3); // 3 indices x 2 triangulos x hoja 231 240 232 241 vertexdata->Begin(); … … 249 258 float twox, twoy, twoz; 250 259 float threex, threey, threez; 251 252 /* Vertices[aHoja.vertsLeaf[0]].GetCoordinates (onex, oney, onez);253 Vertices[aHoja.vertsLeaf[1]].GetCoordinates(twox, twoy, twoz);254 Vertices[aHoja.vertsLeaf[2]].GetCoordinates (threex, threey, threez);*/255 260 256 261 vertexdata->GetVertexCoord(aleaf.vertsLeaf[0],onex,oney,onez); … … 286 291 res[2]=v[2]/module; 287 292 } 288 293 */ 289 294 void Foliage::ReadLeafs(const Geometry::SubMesh *submesh) 290 295 { … … 297 302 Leaves[h].vertsLeaf[2] = submesh->mIndex[h*6+2]; 298 303 Leaves[h].vertsLeaf[3] = submesh->mIndex[h*6+5]; 299 Leaves[h].visible = 0;300 301 GetNormalH ( Leaves[h]);304 // Leaves[h].visible = 0; 305 306 // GetNormalH ( Leaves[h]); 302 307 } 303 308 } … … 316 321 Leaves[tn].vertsLeaf[3] = it->mNewQuad[3]; 317 322 318 Leaves[tn].visible = 0;319 320 GetNormalH (Leaves[tn]);323 // Leaves[tn].visible = 0; 324 325 // GetNormalH (Leaves[tn]); 321 326 322 327 tv1 = it->mV0/2; … … 433 438 } 434 439 435 void Foliage::CalculateTexCoordsAndNorms(void)440 /*void Foliage::CalculateTexCoordsAndNorms(void) 436 441 { 437 442 vertexdata->Begin(); … … 453 458 vertexdata->End(); 454 459 } 455 460 */ 456 461 Foliage::Foliage(const Foliage *ar) 457 462 { … … 465 470 TotalVerts=ar->TotalVerts; 466 471 467 create_vertex_data_func=ar->create_vertex_data_func;468 create_index_data_func=ar->create_index_data_func;472 /* create_vertex_data_func=ar->create_vertex_data_func; 473 // create_index_data_func=ar->create_index_data_func; 469 474 vertexdata=create_vertex_data_func(ar->vertexdata->GetNumVertices()); 470 475 vertexdata->Begin(); … … 477 482 vertexdata->SetVertexNormal(i,va,vb,vc); 478 483 } 479 vertexdata->End(); 480 indexdata=create_index_data_func(ar->indexdata->GetNumMaxIndices());481 indexdata->Begin( );484 vertexdata->End();*/ 485 /* indexdata=create_index_data_func(ar->indexdata->GetNumMaxIndices()); 486 indexdata->Begin(ar->leavesSubMeshID,indexdata->GetNumMaxIndices()); 482 487 for (unsigned int i=0; i<indexdata->GetNumMaxIndices(); i++) 483 488 indexdata->SetIndex(i,ar->indexdata->GetIndex(i)); 484 indexdata->End(); 485 486 Leaves=new Leaf[vertexdata->GetNumVertices()];489 indexdata->End();*/ 490 491 Leaves=new RuntimeLeaf[TotalVerts]; 487 492 // for (unsigned int i=0; i<vertexdata->GetNumVertices(); i++) 488 493 // Leaves[i]=ar->Leaves[i]; 489 memcpy(Leaves,ar->Leaves,sizeof(Leaf)* vertexdata->GetNumVertices());494 memcpy(Leaves,ar->Leaves,sizeof(Leaf)*TotalVerts); 490 495 491 496 // esto no sé si devería haber akí 492 indexdata->SetNumValidIndices(0);497 // indexdata->SetNumValidIndices(0); 493 498 494 499 int h=0; -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/foliage.h
r1083 r1526 26 26 int begin, final; 27 27 int active_leaf_count; 28 int leavesSubMeshID; 28 29 29 Foliage (const Geometry::SubMesh *, const Geometry::TreeSimplificationSequence *, Geometry::CREATEVERTEXDATAFUNC vdfun=NULL, Geometry::CREATEINDEXDATAFUNC idfun=NULL); 30 Foliage ( int leavesSubMeshID, 31 const Geometry::SubMesh *, 32 const Geometry::TreeSimplificationSequence * ); 33 30 34 Foliage (const Foliage *); 31 35 virtual ~Foliage (void); // Destructor … … 33 37 void CalculateLOD(int nhojas); 34 38 35 Geometry::VertexData *vertexdata; 36 Geometry::IndexData *indexdata; 37 38 Leaf *Leaves; 39 RuntimeLeaf *Leaves; 39 40 ActiveLeafNode *MinDet; // first active leaf 40 41 int leafCount; … … 45 46 46 47 private: 47 Geometry::CREATEVERTEXDATAFUNC create_vertex_data_func;48 Geometry::CREATEINDEXDATAFUNC create_index_data_func;49 48 50 49 bool IsActive( int num) const; … … 59 58 bool ReadSimpSeq(const Geometry::TreeSimplificationSequence *); /// returns true when successful 60 59 void FillRoot(void); 61 62 void GetNormalH (Leaf&);63 64 void CrossProduct(const float *v1, const float *v2, float *res);65 void Normalize(const float *v, float *res);66 // void CalculaNormalesVertice(void);67 void CalculateTexCoordsAndNorms(void);68 69 60 }; 70 61 -
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/simplif.h
r1025 r1526 12 12 class pair_info : public Heapable 13 13 { 14 public:15 Vertex *v0, *v1;14 public: 15 Vertex *v0, *v1; 16 16 17 Vec3 candidate;18 real cost;17 Vec3 candidate; 18 real cost; 19 19 20 pair_info(Vertex *a,Vertex *b) { v0=a; v1=b; cost=HUGE; }20 pair_info(Vertex *a,Vertex *b) { v0=a; v1=b; cost=HUGE; } 21 21 22 bool isValid() { return v0->isValid() && v1->isValid(); }22 bool isValid() { return v0->isValid() && v1->isValid(); } 23 23 }; 24 24 … … 27 27 class vert_info 28 28 { 29 public:29 public: 30 30 31 pair_buffer pairs;31 pair_buffer pairs; 32 32 33 Mat4 Q;34 real norm;33 Mat4 Q; 34 real norm; 35 35 36 vert_info() : Q(Mat4::zero) { pairs.init(2); norm=0.0; }36 vert_info() : Q(Mat4::zero) { pairs.init(2); norm=0.0; } 37 37 }; 38 38 } 39
Note: See TracChangeset
for help on using the changeset viewer.