Changeset 2088 for GTP/trunk/Lib/Geom/shared/GTGeometry/src
- Timestamp:
- 02/05/07 13:28:39 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/SimplificationMethod.cpp
r1600 r2088 3 3 #include <iostream> 4 4 #include <fstream> 5 #include <gfx/geom/ProxGrid.h> 6 #include "quadrics.h" 5 7 6 8 using namespace std; … … 8 10 using namespace simplif; 9 11 10 #include <gfx/geom/ProxGrid.h> 11 #include "quadrics.h" 12 13 //--------------------------------------------------------------------------- 12 //------------------------------------------------------------------------- 14 13 // Constructor 15 //------------------------------------------------------------------------- --14 //------------------------------------------------------------------------- 16 15 SimplificationMethod::SimplificationMethod(const Mesh *m) 17 16 { 18 objmesh = m; 17 mInitialMesh = new Mesh(); 18 *mInitialMesh = *m; 19 19 mGeoMesh = NULL; 20 first_index_submesh = new unsigned int[ objmesh->mSubMeshCount];20 first_index_submesh = new unsigned int[mInitialMesh->mSubMeshCount]; 21 21 indexMeshLeaves = -1; 22 22 … … 25 25 } 26 26 27 //------------------------------------------------------------------------- --27 //------------------------------------------------------------------------- 28 28 // Destructor 29 //------------------------------------------------------------------------- --29 //------------------------------------------------------------------------- 30 30 SimplificationMethod::~SimplificationMethod() 31 31 { 32 delete heap; 33 delete [] first_index_submesh; 34 } 35 36 //--------------------------------------------------------------------------- 32 delete heap; 33 delete [] first_index_submesh; 34 35 delete mInitialMesh; 36 } 37 38 //------------------------------------------------------------------------- 37 39 // Recalculate the cost of the vertices of an edge 38 //------------------------------------------------------------------------- --40 //------------------------------------------------------------------------- 39 41 void SimplificationMethod::compute_pair_info(simplif::pair_info *auxpair) 40 42 { … … 48 50 simplif::real norm = v0_info.norm + v1_info.norm; 49 51 50 auxpair->cost = simplif::quadrix_pair_target(Q, v0, v1, auxpair->candidate); 52 auxpair->cost = simplif::quadrix_pair_target( Q, 53 v0, 54 v1, 55 auxpair->candidate); 51 56 52 57 if ( simplif::will_weight_by_area ) … … 74 79 } 75 80 76 //------------------------------------------------------------------------- --81 //------------------------------------------------------------------------- 77 82 // Reasign the new vertex to the adequate face 78 //------------------------------------------------------------------------- --83 //------------------------------------------------------------------------- 79 84 int SimplificationMethod::predict_face( simplif::Face& F, 80 85 simplif::Vertex *v1, … … 87 92 int nmapped = 0; 88 93 89 if ( F.vertex(0) == v1 || F.vertex(0) == v2 )94 if ( F.vertex(0) == v1 || F.vertex(0) == v2 ) 90 95 { 91 96 f1 = vnew; nmapped++; … … 96 101 } 97 102 98 if ( F.vertex(1) == v1 || F.vertex(1) == v2 )103 if ( F.vertex(1) == v1 || F.vertex(1) == v2 ) 99 104 { 100 105 f2 = vnew; nmapped++; … … 105 110 } 106 111 107 if ( F.vertex(2) == v1 || F.vertex(2) == v2 )112 if ( F.vertex(2) == v1 || F.vertex(2) == v2 ) 108 113 { 109 114 f3 = vnew; nmapped++; … … 119 124 #define MESH_INVERSION_PENALTY 1e9 120 125 121 //--------------------------------------------------------------------------- 122 //--------------------------------------------------------------------------- 123 simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, 124 simplif::Vertex *v2, 125 simplif::Vec3& vnew) 126 //------------------------------------------------------------------------- 127 //------------------------------------------------------------------------- 128 simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, 129 simplif::Vertex *v1, 130 simplif::Vertex *v2, 131 simplif::Vec3& vnew) 126 132 { 127 133 static simplif::face_buffer changed; … … 166 172 } 167 173 168 //------------------------------------------------------------------------- --174 //------------------------------------------------------------------------- 169 175 // Returns true if the givens vertices are a valid pair 170 //------------------------------------------------------------------------- --176 //------------------------------------------------------------------------- 171 177 bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, 172 178 simplif::Vertex *v1) … … 185 191 } 186 192 187 //------------------------------------------------------------------------- --193 //------------------------------------------------------------------------- 188 194 // Create a new pair with two given vertices 189 //------------------------------------------------------------------------- --195 //------------------------------------------------------------------------- 190 196 simplif::pair_info *SimplificationMethod::new_pair( simplif::Vertex *v0, 191 197 simplif::Vertex *v1) … … 202 208 } 203 209 204 //------------------------------------------------------------------------- --210 //------------------------------------------------------------------------- 205 211 // Remove a given pair 206 //------------------------------------------------------------------------- --212 //------------------------------------------------------------------------- 207 213 void SimplificationMethod::delete_pair(simplif::pair_info *auxpair) 208 214 { … … 221 227 } 222 228 223 //------------------------------------------------------------------------- --229 //------------------------------------------------------------------------- 224 230 // Contract a given pair 225 //------------------------------------------------------------------------- --231 //------------------------------------------------------------------------- 226 232 void SimplificationMethod::do_contract( simplif::Model& m, 227 233 simplif::pair_info *auxpair) 228 234 { 229 simplif::Vertex *v0 = auxpair->v0; 230 simplif::Vertex *v1 = auxpair->v1; 231 simplif::vert_info& v0_info = vertex_info(v0); 232 simplif::vert_info& v1_info = vertex_info(v1); 233 simplif::Vec3 vnew = auxpair->candidate; 234 235 int i; 235 simplif::Vertex *v0 = auxpair->v0; 236 simplif::Vertex *v1 = auxpair->v1; 237 simplif::Vec3 vnew = auxpair->candidate; 238 float x,y,z; 239 simplif::Vertex *aux_vert; 240 241 _float3_ removed_vert; 242 int i; 243 vertex_map_str lb; 244 vertex_map_str ub; 236 245 237 246 // V0 must be always the alive vertex … … 239 248 if (vnew == (*v0)) 240 249 { 250 // Coords of the removed vertex. 251 x = (*v1)[X]; 252 y = (*v1)[Y]; 253 z = (*v1)[Z]; 254 255 aux_vert = v1; 256 } 257 else 258 { 259 // Coords of the removed vertex. 260 x = (*v0)[X]; 261 y = (*v0)[Y]; 262 z = (*v0)[Z]; 263 241 264 // 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 } 252 253 // Make v0 be the new vertex 254 v0_info.Q += v1_info.Q; 265 aux_vert = v0; 266 v0 = v1; 267 v1 = aux_vert; 268 } 269 270 simplif::vert_info& v0_info = vertex_info(v0); 271 simplif::vert_info& v1_info = vertex_info(v1); 272 273 // Simplification step. 274 simplifstep.mV0 = v0->vID; 275 simplifstep.mV1 = v1->vID; 276 277 // Position of the vertex removed. 278 removed_vert = _float3_(x,y,z); 279 280 // If position of the removed vertex is found. 281 if (vertexMultimap.end() != vertexMultimap.find(removed_vert)) 282 { 283 lb = vertexMultimap.lower_bound(removed_vert); 284 ub = vertexMultimap.upper_bound(removed_vert); 285 286 // For each vertex. 287 while (lb != ub) 288 { 289 // If removed vertex is found. 290 if ((*lb).second == aux_vert->vID) 291 { 292 // Debug. 293 cout << "to remove: " 294 << (*lb).second 295 << endl; 296 297 // Delete vertex that disappears. 298 vertexMultimap.erase(lb); 299 300 // Break while. 301 lb = ub; 302 } 303 else 304 { 305 // Next iteration. 306 lb++; 307 } 308 } 309 } 310 311 // Make v0 be the new vertex 312 v0_info.Q += v1_info.Q; 255 313 v0_info.norm += v1_info.norm; 256 314 257 315 simplif::Vec3 p(vnew); 258 /* simplif::Vec3 v0antes(v0->operator[](simplif::X),259 v0->operator[](simplif::Y),260 v0->operator[](simplif::Z));*/261 316 262 317 // Perform the actual contraction. … … 265 320 changed.reset(); 266 321 m.contract(v0, v1, vnew, changed); 267 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 322 277 323 // Stores the displacement in the simplification step. … … 360 406 condemned.reset(); 361 407 408 // Recalculate pairs associated with v1. 362 409 for (i = 0; i < v1_info.pairs.length(); i++) 363 410 { … … 366 413 simplif::Vertex *u; 367 414 368 if ( p->v0 == v1 )415 if ( p->v0 == v1 ) 369 416 { 370 417 u = p->v1; … … 372 419 else 373 420 { 374 if ( p->v1 == v1)421 if ( p->v1 == v1) 375 422 { 376 423 u = p->v0; … … 406 453 } 407 454 408 //------------------------------------------------------------------------- --409 //------------------------------------------------------------------------- --455 //------------------------------------------------------------------------- 456 //------------------------------------------------------------------------- 410 457 bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, 411 458 simplif::Mat4& Q) … … 422 469 } 423 470 424 //------------------------------------------------------------------------- --425 // Extract a pair from the heap, concract it and remove it f orm the heap426 //------------------------------------------------------------------------- --471 //------------------------------------------------------------------------- 472 // Extract a pair from the heap, concract it and remove it from the heap. 473 //------------------------------------------------------------------------- 427 474 void SimplificationMethod::decimate_contract(simplif::Model& m) 428 475 { 429 476 simplif::heap_node *top; 430 477 simplif::pair_info *pair; 431 simplif::Vertex *v0; 432 simplif::Vertex *v1; 433 simplif::Vec3 candidate; 478 simplif::Vertex *v0; 479 simplif::Vertex *v1; 480 simplif::Vec3 candidate; 481 _float3_ f0; 482 _float3_ f1; 434 483 435 484 for (;;) … … 460 509 } 461 510 462 if (pair->isValid() && sharededge) 511 // Coordinates of the pair. 512 f0 = _float3_((*pair->v0)[X],(*pair->v0)[Y],(*pair->v0)[Z]); 513 f1 = _float3_((*pair->v1)[X],(*pair->v1)[Y],(*pair->v1)[Z]); 514 515 if (pair->isValid() 516 && 517 sharededge 518 && 519 (vertexMultimap.find(f0) != vertexMultimap.end()) 520 && 521 (vertexMultimap.find(f1) != vertexMultimap.end())) 463 522 { 464 523 break; 465 524 } 525 526 // Debug. 527 cout << "Deleting pair (" 528 << pair->v0->vID 529 << "--" 530 << pair->v1->vID 531 << ")" 532 << endl; 466 533 467 534 // Deletes repetitions of pair in heap. … … 470 537 471 538 // Copy pair. 539 <<<<<<< .mine 540 v0 = new Vertex( (*pair->v0)[X], 541 (*pair->v0)[Y], 542 (*pair->v0)[Z]); 543 544 v0->vID = pair->v0->vID; 545 ======= 472 546 v0 = new Vertex( pair->v0->operator[](0), 473 547 pair->v0->operator[](1), … … 475 549 v0->vID = pair->v0->vID; 476 550 v0->uniqID = pair->v0->uniqID; 551 >>>>>>> .r2086 477 552 478 v1 = new Vertex( pair->v1->operator[](0),479 pair->v1->operator[](1),480 pair->v1->operator[](2));553 v1 = new Vertex( (*pair->v1)[X], 554 (*pair->v1)[Y], 555 (*pair->v1)[Z]); 481 556 v1->vID = pair->v1->vID; 482 v1-> uniqID = pair->v1->uniqID;557 v1->vID = pair->v1->vID; 483 558 484 559 candidate = pair->candidate; … … 488 563 489 564 // Debug. 490 cout << "Contracting..." << endl; 565 cout << "Contracting... " 566 << endl; 491 567 492 568 // Contract main pair. … … 499 575 M0.validVertCount--; 500 576 501 // Find an edge of the same coordinates and different vertex identifiers. 502 removeTwinEdges(m,v0,v1,candidate); 503 504 // Move vertex of non twin edges. 505 contractLonelyVertices(m,v0,v1,candidate); 577 // Find twin vertices to the contracted one and contract them. 578 removeTwinVertices(m,v0,v1,candidate); 506 579 507 580 // Free Memory. … … 510 583 } 511 584 512 //--------------------------------------------------------------------------- 513 // Contract lonely vertex that has the same coords that the contracted one. 514 //--------------------------------------------------------------------------- 515 void SimplificationMethod::contractLonelyVertices( simplif::Model &m, simplif::Vertex *v0, 516 simplif::Vertex *v1, 517 simplif::Vec3 candidate) 518 { 519 bool lonely_vertex_found; 520 simplif::Vertex *vert; 521 simplif::Vertex *new_vert; 522 simplif::Vertex *lonely_vert; 523 simplif::Edge *edge; 524 simplif::heap_node *top; 585 //------------------------------------------------------------------------- 586 // Find twin vertices to the contracted one and contract them. 587 //------------------------------------------------------------------------- 588 void SimplificationMethod::removeTwinVertices(simplif::Model &m, 589 simplif::Vertex *v0, 590 simplif::Vertex *v1, 591 simplif::Vec3 candidate) 592 { 593 bool twin_found; 594 simplif::Edge *edge; 525 595 simplif::pair_info *pair; 596 simplif::Vertex *vert; 597 simplif::Vertex *lonely_vert; 598 simplif::Vertex *new_vert; 599 vertex_map_str lb; 600 vertex_map_str ub; 601 vertex_map_str it; 602 _float3_ f0; 603 _float3_ f1; 604 simplif::Edge *econ; 526 605 simplif::vert_info new_vert_info; 527 606 Geometry::GeoVertex vertex_added; 528 simplif::Vertex *take_bone_from_vert; 529 530 // Gets the vertex that is not the candidate. 531 if ((*v0) == candidate) 532 { 533 vert = v1; 607 simplif::Vertex *take_bone_from_vert; 608 float x,y,z; 609 610 if ((*v0) != (*v1)) 611 { 612 // Gets the vertex that is not the candidate. 613 if ((*v1) == candidate) 614 { 615 vert = v0; 616 v0 = v1; 617 v1 = vert; 618 } 619 534 620 take_bone_from_vert = v0; 535 } 536 else if ((*v1) == candidate) 537 { 538 vert = v0; 539 take_bone_from_vert = v1; 540 } 541 542 // For all vertex. 543 for (int i = 0; i < heap->getSize(); i++) 544 { 545 // Initialize lonely vertex flag. 546 lonely_vertex_found = false; 547 548 // Gets the current pair. 549 top = heap->getElement(i); 550 pair = (simplif::pair_info *)top->obj; 551 552 if (pair->v0->isValid() && (*vert) == (*pair->v0)) 553 { 554 lonely_vert = pair->v0; 555 lonely_vertex_found = true; 556 } 557 else if (pair->v1->isValid() && (*vert) == (*pair->v1)) 558 { 559 lonely_vert = pair->v1; 560 lonely_vertex_found = true; 561 } 562 563 // If a lonely vertex is found. Creates new vertex and contract. 564 if (lonely_vertex_found) 565 { 566 new_vert = m.newVertex(candidate[X],candidate[Y],candidate[Z]); 567 edge = m.newEdge(new_vert,lonely_vert); 568 569 // We assume here the there are the same number of vertices and texture coordinates and normals 570 571 // assign a texture coordinate for the vbertex 572 simplif::Vec2 newtexcoord = m.texcoord(lonely_vert->validID()); 573 m.in_TexCoord(newtexcoord); 574 575 // assign a normal coordinate for the vbertex 576 simplif::Vec3 newnormal = m.normal(lonely_vert->validID()); 577 m.in_Normal(newnormal); 578 579 // Adds new vertex information to simplification sequence. 580 vertex_added.id = new_vert->vID; 581 vertex_added.bonefrom = take_bone_from_vert->vID; 582 vertex_added.position = Vector3(candidate[X],candidate[Y],candidate[Z]); 583 584 simplif::Vec2 tc = m.texcoord(new_vert->validID()); 585 vertex_added.texcoord = Vector2(tc[X],tc[Y]); 586 587 simplif::Vec3 vn = m.normal(new_vert->validID()); 588 vertex_added.normal = Vector3(vn[X],vn[Y],vn[Z]); 589 590 msimpseq->mNewVertices.push_back(vertex_added); 591 592 pair = new_pair(new_vert,lonely_vert); 593 pair->candidate = candidate; 594 595 pair->notInHeap(); 596 /* vinfo(new_vert->validID()).Q = vinfo(lonely_vert->validID()).Q; 597 vinfo(new_vert->validID()).norm = vinfo(lonely_vert->validID()).norm;*/ 598 599 // Debug. 600 cout << "Contracting new pair..." << endl; 601 602 // Contract twin pair. 603 do_contract(m, pair); 604 M0.validVertCount--; 605 } 606 } 607 } 608 609 //--------------------------------------------------------------------------- 610 // Find a twin edge of the given one. 611 //--------------------------------------------------------------------------- 612 void SimplificationMethod::removeTwinEdges( simplif::Model &m, 613 simplif::Vertex *v0, 614 simplif::Vertex *v1, 615 simplif::Vec3 candidate) 616 { 617 bool twin_found; 618 simplif::heap_node *top; 619 simplif::pair_info *pair; 620 simplif::Vertex *aux_vert; 621 622 // Find a twin edge in heap. 623 for (int i = 0; i < heap->getSize(); i++) 624 { 625 // Initialize twin flag. 626 twin_found = false; 627 628 // Gets the current pair. 629 top = heap->getElement(i); 630 pair = (simplif::pair_info *)top->obj; 631 632 if (pair->v0->isValid()==false || pair->v1->isValid()==false) 633 continue; 634 635 if (v0->operator[](0) == pair->v0->operator[](0) 636 && 637 v0->operator[](1) == pair->v0->operator[](1) 638 && 639 v0->operator[](2) == pair->v0->operator[](2)) 640 { 641 if (v1->operator[](0) == pair->v1->operator[](0) 642 && 643 v1->operator[](1) == pair->v1->operator[](1) 644 && 645 v1->operator[](2) == pair->v1->operator[](2)) 621 622 f0 = _float3_((*v0)[X],(*v0)[Y],(*v0)[Z]); 623 f1 = _float3_((*v1)[X],(*v1)[Y],(*v1)[Z]); 624 625 while ((it = vertexMultimap.find(f1)) != vertexMultimap.end()) 626 { 627 twin_found = false; 628 629 lonely_vert = m.vertex((*it).second); 630 631 for (int i = 0; i < lonely_vert->edgeUses().length(); i++) 632 { 633 econ = lonely_vert->edgeUses()(i); 634 635 if (((*econ->org()) == (*v0)) 636 || 637 ((*econ->dest()) == (*v0))) 638 { 639 lb = vertexMultimap.lower_bound(f0); 640 ub = vertexMultimap.upper_bound(f0); 641 642 // For each vertex. 643 while (lb != ub) 644 { 645 // If removed vertex is found. 646 if (((*lb).second == econ->org()->vID) 647 || 648 ((*lb).second == econ->dest()->vID)) 649 { 650 twin_found = true; 651 652 // Break while. 653 lb = ub; 654 } 655 else 656 { 657 // Next iteration. 658 lb++; 659 } 660 } 661 662 break; 663 } 664 } 665 666 // If a twin edge has been found. 667 if (twin_found) 646 668 { 647 669 // Debug. 648 cout << "Twin contracted (NO Swap)..." << endl; 649 650 // Active twin flag. 651 twin_found = true; 652 } 653 } 654 else 655 { 656 // If there is a twin edge in reverse order. 657 if (v0->operator[](0) == pair->v1->operator[](0) 658 && 659 v0->operator[](1) == pair->v1->operator[](1) 660 && 661 v0->operator[](2) == pair->v1->operator[](2)) 662 { 663 if (v1->operator[](0) == pair->v0->operator[](0) 664 && 665 v1->operator[](1) == pair->v0->operator[](1) 666 && 667 v1->operator[](2) == pair->v0->operator[](2)) 668 { 669 // Debug. 670 cout << "Twin contracted (Swap)..." << endl; 671 672 // Swap. 673 aux_vert = pair->v0; 674 pair->v0 = pair->v1; 675 pair->v1 = aux_vert; 676 677 // Active twin flag. 678 twin_found = true; 679 } 680 } 681 } 682 670 cout << "Twin Contracted..." << endl; 671 672 // Extract twin edge from heap. 673 pair = new_pair(econ->org(),econ->dest()); 674 675 // Copy candidate. 676 pair->candidate = candidate; 677 678 pair->notInHeap(); 679 680 // Contract twin pair. 681 do_contract(m, pair); 682 M0.validVertCount--; 683 } 684 else 685 { 686 // Debug. 687 cout << "Contracting new pair..." << endl; 688 689 new_vert = m.newVertex(candidate[X],candidate[Y],candidate[Z]); 690 691 // Add new vert to multimap. 692 x = (*new_vert)[X]; 693 y = (*new_vert)[Y]; 694 z = (*new_vert)[Z]; 695 696 // Adds new vertex to multimap. 697 vertexMultimap.insert(vertex_pair(_float3_(x,y,z),new_vert->vID)); 698 699 // Creates new edge. 700 edge = m.newEdge(new_vert,lonely_vert); 701 702 // We assume here there are the same number of vertices 703 // and texture coordinates and normals. 704 705 // Assign a texture coordinate for the vertex. 706 simplif::Vec2 newtexcoord = m.texcoord(lonely_vert->vID); 707 m.in_TexCoord(newtexcoord); 708 709 // Assign a normal coordinate for the vbertex. 710 simplif::Vec3 newnormal = m.normal(lonely_vert->vID); 711 m.in_Normal(newnormal); 712 713 // Adds new vertex information to simplification sequence. 714 vertex_added.id = new_vert->vID; 715 716 vertex_added.bonefrom = take_bone_from_vert->vID; 717 718 <<<<<<< .mine 719 vertex_added.position = Vector3(candidate[X], 720 candidate[Y], 721 candidate[Z]); 722 ======= 683 723 // If a twin edge has been found. 684 724 if (twin_found) 685 725 { 686 726 heap->kill(i); 687 688 // Copy candidate. 689 pair->candidate = candidate; 690 691 // Contract twin pair. 692 do_contract(m, pair); 693 i--; 694 M0.validVertCount--; 695 } 696 } 697 } 698 699 //--------------------------------------------------------------------------- 700 //--------------------------------------------------------------------------- 727 >>>>>>> .r2086 728 729 simplif::Vec2 tc = m.texcoord(new_vert->vID); 730 vertex_added.texcoord = Vector2(tc[X],tc[Y]); 731 732 simplif::Vec3 vn = m.normal(new_vert->vID); 733 vertex_added.normal = Vector3(vn[X],vn[Y],vn[Z]); 734 735 msimpseq->mNewVertices.push_back(vertex_added); 736 737 pair = new_pair(new_vert,lonely_vert); 738 pair->candidate = candidate; 739 pair->notInHeap(); 740 741 // Contract new pair. 742 do_contract(m, pair); 743 M0.validVertCount--; 744 } 745 } 746 } 747 } 748 749 //------------------------------------------------------------------------- 750 //------------------------------------------------------------------------- 701 751 simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) 702 752 { … … 705 755 simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); 706 756 707 if ( simplif::will_weight_by_area )757 if ( simplif::will_weight_by_area ) 708 758 { 709 759 err /= info.norm; … … 713 763 } 714 764 715 //------------------------------------------------------------------------- --765 //------------------------------------------------------------------------- 716 766 // Extract the minimum cost of the the valid nodes (not simplified) 717 //------------------------------------------------------------------------- --767 //------------------------------------------------------------------------- 718 768 simplif::real SimplificationMethod::decimate_min_error() 719 769 { … … 744 794 } 745 795 746 //------------------------------------------------------------------------- --796 //------------------------------------------------------------------------- 747 797 // Returns the maximum error by vertex 748 //------------------------------------------------------------------------- --798 //------------------------------------------------------------------------- 749 799 simplif::real SimplificationMethod::decimate_max_error(simplif::Model& m) 750 800 { … … 760 810 } 761 811 762 //------------------------------------------------------------------------- --812 //------------------------------------------------------------------------- 763 813 // Initializations 764 //--------------------------------------------------------------------------- 765 void SimplificationMethod::decimate_init(simplif::Model& m, simplif::real limit) 814 //------------------------------------------------------------------------- 815 void SimplificationMethod::decimate_init( simplif::Model &m, 816 simplif::real limit) 766 817 { 767 818 int i,j; … … 783 834 } 784 835 } 785 786 // Sets the fill value of the vinfo buffer.787 //vinfo.setFill(m.vertCount());788 836 789 837 // For each face, recalculate constraints. … … 849 897 simplif::pair_info *pair = new_pair(m.edge(i)->org(), m.edge(i)->dest()); 850 898 //pointers_to_remove.push_back(pair); 899 851 900 compute_pair_info(pair); 852 901 pair_count++; … … 901 950 } 902 951 903 //------------------------------------------------------------------------- --952 //------------------------------------------------------------------------- 904 953 // Initilizations 905 //------------------------------------------------------------------------- --954 //------------------------------------------------------------------------- 906 955 void SimplificationMethod::simplifmethod_init(void) 907 956 { … … 936 985 } 937 986 938 //------------------------------------------------------------------------- --987 //------------------------------------------------------------------------- 939 988 // Do the contractions till the required LOD (percentage option) 940 //------------------------------------------------------------------------- --989 //------------------------------------------------------------------------- 941 990 void SimplificationMethod::simplifmethod_run(int finalfaces,TIPOFUNC upb) 942 991 { … … 989 1038 } 990 1039 991 //------------------------------------------------------------------------- --1040 //------------------------------------------------------------------------- 992 1041 // Do the contractions till the required LOD (number of vertices option) 993 //------------------------------------------------------------------------- --1042 //------------------------------------------------------------------------- 994 1043 void SimplificationMethod::simplifmethod_runv(int numvertices,TIPOFUNC upb) 995 1044 { … … 1051 1100 } 1052 1101 1053 //--------------------------------------------------------------------------- 1054 // Class to create a map without repeated vertices 1055 //--------------------------------------------------------------------------- 1056 class _float3_ 1057 { 1058 public: 1059 1060 float x,y,z; 1061 1062 _float3_(float x=0.0f, float y=0.0f, float z=0.0f) 1063 { 1064 this->x = x; this->y = y; this->z = z; 1065 } 1066 1067 _float3_(const _float3_ &f) 1068 { 1069 x=f.x; y=f.y; z=f.z; 1070 } 1071 1072 _float3_ & operator=(const _float3_ &f) 1073 { 1074 x=f.x; y=f.y; z=f.z; return *this; 1075 } 1076 1077 bool operator<(const _float3_ &f) const 1078 { 1079 if (x<f.x) return true; 1080 if (x>f.x) return false; 1081 if (y<f.y) return true; 1082 if (y>f.y) return false; 1083 if (z<f.z) return true; 1084 if (z>f.z) return false; 1085 return false; 1086 } 1087 }; 1088 1089 //--------------------------------------------------------------------------- 1102 //------------------------------------------------------------------------- 1090 1103 // Generate simplification model. 1091 //------------------------------------------------------------------------- --1104 //------------------------------------------------------------------------- 1092 1105 void SimplificationMethod::generateSimplifModel(void) 1093 1106 { 1094 int face_index = 0; 1107 int face_index = 0; 1108 int vertID; 1109 float x,y,z; 1095 1110 1096 1111 // For each submesh. 1097 1112 bool added_vertices = false; 1098 for (size_t i = 0; i < objmesh->mSubMeshCount; i++)1113 for (size_t i = 0; i < mInitialMesh->mSubMeshCount; i++) 1099 1114 { 1100 1115 // If is not a tree leaves submesh. … … 1103 1118 // For all the vertices of each submesh. 1104 1119 for ( size_t j = 0; 1105 j < objmesh->mSubMesh[i].mVertexBuffer->mVertexCount;1120 j < mInitialMesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1106 1121 j++) 1107 1122 { 1123 x = mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 1124 y = mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 1125 z = mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 1126 1108 1127 // If the vertex is not in vertices_map, then is added. 1109 M0.in_Vertex(simplif::Vec3(1110 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x,1111 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y,1112 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z));1128 vertID = M0.in_Vertex(simplif::Vec3( 1129 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, 1130 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, 1131 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); 1113 1132 1114 1133 M0.in_Normal(simplif::Vec3( 1115 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x,1116 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y,1117 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z));1134 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, 1135 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, 1136 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); 1118 1137 1119 1138 M0.in_TexCoord(simplif::Vec2( 1120 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x,1121 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y));1139 mInitialMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x, 1140 mInitialMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); 1122 1141 1123 1142 added_vertices = true; 1124 } 1125 } 1126 1143 1144 // Add vertex to multimap. 1145 vertexMultimap.insert(vertex_pair(_float3_(x,y,z),vertID)); 1146 } 1147 } 1148 1127 1149 // If is shared vertex buffer object. 1128 if ( objmesh->mSubMesh[i].mSharedVertexBuffer && added_vertices)1150 if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer && added_vertices) 1129 1151 { 1130 1152 printf("Shared break"); … … 1134 1156 1135 1157 // For each submesh. 1136 for (size_t i = 0; i < objmesh->mSubMeshCount; i++)1158 for (size_t i = 0; i < mInitialMesh->mSubMeshCount; i++) 1137 1159 { 1138 1160 if (i != indexMeshLeaves) 1139 1161 { 1140 for (Index j = 0; j < objmesh->mSubMesh[i].mIndexCount; j += 3) 1162 for ( Geometry::Index j = 0; 1163 j < mInitialMesh->mSubMesh[i].mIndexCount; 1164 j += 3) 1141 1165 { 1142 1166 // +1 is required because the first index in the 1143 1167 // simplification method is 1 1144 1168 // Index of the vertex in vertices_map. 1145 Index index0 = objmesh->mSubMesh[i].mIndex[j];1169 Geometry::Index index0 = mInitialMesh->mSubMesh[i].mIndex[j]; 1146 1170 1147 Index index1 = objmesh->mSubMesh[i].mIndex[j + 1];1171 Geometry::Index index1 = mInitialMesh->mSubMesh[i].mIndex[j + 1]; 1148 1172 1149 Index index2 = objmesh->mSubMesh[i].mIndex[j + 2];1173 Geometry::Index index2 = mInitialMesh->mSubMesh[i].mIndex[j + 2]; 1150 1174 1151 1175 // Create a triangle with its indices in vertices_map. … … 1163 1187 } 1164 1188 1165 //------------------------------------------------------------------------- --1189 //------------------------------------------------------------------------- 1166 1190 // simplifmethod_init is executed to do some initalizations 1167 1191 // simplifmethod_run is executed to do the decimation 1168 1192 // Then, the simpified model is created and the decimation 1169 1193 // information is returned. 1170 //------------------------------------------------------------------------- --1194 //------------------------------------------------------------------------- 1171 1195 MeshSimplificationSequence *SimplificationMethod::Decimate( float lod, 1172 1196 int simpliftype, 1173 1197 TIPOFUNC upb) 1174 1198 { 1199 bool shared_vertex; 1200 unsigned int vertex_count; 1201 std::map<int,int>::iterator findit; 1202 // Initialize vertex count. 1203 vertex_count = 0; 1204 1205 // Initialize shared vertex buffer flag. 1206 shared_vertex = false; 1207 1208 // Determines if the mesh have shared vertex buffer. 1209 for (int i = 0; i < mInitialMesh->mSubMeshCount; i++) 1210 { 1211 if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) 1212 { 1213 shared_vertex = true; 1214 break; 1215 } 1216 } 1217 1175 1218 simplifmethod_init(); 1176 1219 … … 1186 1229 } 1187 1230 1188 // Store unique vertices by submesh (without repetitions).1189 typedef std::map<int,int> MAPAINDIND;1190 1191 // Store all the indices by submesh (with repetitions).1192 typedef std::vector<int> REPINDLIST;1193 1194 1231 MAPAINDIND *unique_verts_inds_by_geo = 1195 new MAPAINDIND[objmesh->mSubMeshCount]; 1196 1197 REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[objmesh->mSubMeshCount]; 1232 new MAPAINDIND[mInitialMesh->mSubMeshCount]; 1233 1234 MAPAINDIND shared_vertex_map; 1235 1236 REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[mInitialMesh->mSubMeshCount]; 1198 1237 1199 1238 // Index counter by submesh. 1200 int *inextvert = new int[ objmesh->mSubMeshCount];1201 1202 for (unsigned int i = 0; i < objmesh->mSubMeshCount; i++)1239 int *inextvert = new int[mInitialMesh->mSubMeshCount]; 1240 1241 for (unsigned int i = 0; i < mInitialMesh->mSubMeshCount; i++) 1203 1242 { 1204 1243 inextvert[i] = 0; … … 1207 1246 // Construct the model. 1208 1247 std::cout << "M0.faceCount(): " << M0.faceCount() << std::endl; 1248 1249 // For each face. 1209 1250 for (int i = 0; i < M0.faceCount(); i++) 1210 1251 { … … 1216 1257 int igeo = auxface->igeo; 1217 1258 1218 // Insert to each submesh its triangles. 1219 std::map<int,int>::iterator findit = 1220 unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); 1221 1222 if (findit == unique_verts_inds_by_geo[igeo].end()) 1223 { 1224 // If it is not added... add and map it. 1225 unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = 1226 inextvert[igeo]; 1227 1228 inextvert[igeo]++; 1229 } 1230 1231 findit = 1232 unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); 1233 1234 if (findit == unique_verts_inds_by_geo[igeo].end()) 1235 { 1236 // If it is not added... add and map it. 1237 unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = 1238 inextvert[igeo]; 1239 inextvert[igeo]++; 1240 } 1241 1242 findit = 1243 unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); 1244 1245 if (findit == unique_verts_inds_by_geo[igeo].end()) 1246 { 1247 // If it is not added... add and map it. 1248 unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] = 1249 inextvert[igeo]; 1250 inextvert[igeo]++; 1251 } 1252 1253 // Total number of indices by submesh. 1254 ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(0)->validID()); 1255 ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(1)->validID()); 1256 ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(2)->validID()); 1259 // For each index of the face. 1260 for (size_t j = 0; j < 3; j++) 1261 { 1262 if (shared_vertex) 1263 { 1264 // Insert to each submesh its triangles. 1265 findit = shared_vertex_map.find(auxface->vertex(j)->vID); 1266 1267 if (findit == shared_vertex_map.end()) 1268 { 1269 // If it is not added... add and map it. 1270 shared_vertex_map[auxface->vertex(j)->vID] = vertex_count; 1271 1272 vertex_count++; 1273 } 1274 } 1275 else 1276 { 1277 // Insert to each submesh its triangles. 1278 findit = unique_verts_inds_by_geo[igeo]. 1279 find(auxface->vertex(j)->vID); 1280 1281 if (findit == unique_verts_inds_by_geo[igeo].end()) 1282 { 1283 // If it is not added... add and map it. 1284 unique_verts_inds_by_geo[igeo][auxface->vertex(j)->vID] = 1285 inextvert[igeo]; 1286 1287 inextvert[igeo]++; 1288 } 1289 } 1290 1291 // Total number of indices by submesh. 1292 ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(j)->vID); 1293 } 1257 1294 } 1258 1295 } … … 1262 1299 mGeoMesh->mVertexBuffer = new VertexBuffer(); 1263 1300 1264 mGeoMesh->mSubMeshCount = objmesh->mSubMeshCount;1301 mGeoMesh->mSubMeshCount = mInitialMesh->mSubMeshCount; 1265 1302 1266 1303 // Memory allocation for the submeshes. 1267 mGeoMesh->mSubMesh = new SubMesh[ objmesh->mSubMeshCount];1304 mGeoMesh->mSubMesh = new SubMesh[mInitialMesh->mSubMeshCount]; 1268 1305 1269 1306 // Fill up bounding box settings. 1270 mGeoMesh->mMeshBounds.maxX = objmesh->mMeshBounds.maxX;1271 mGeoMesh->mMeshBounds.maxY = objmesh->mMeshBounds.maxY;1272 mGeoMesh->mMeshBounds.maxZ = objmesh->mMeshBounds.maxZ;1273 mGeoMesh->mMeshBounds.minX = objmesh->mMeshBounds.minX;1274 mGeoMesh->mMeshBounds.minY = objmesh->mMeshBounds.minY;1275 mGeoMesh->mMeshBounds.minZ = objmesh->mMeshBounds.minZ;1276 mGeoMesh->mMeshBounds.radius = objmesh->mMeshBounds.radius;1277 mGeoMesh->mMeshBounds.scaleFactor = objmesh->mMeshBounds.scaleFactor;1307 mGeoMesh->mMeshBounds.maxX = mInitialMesh->mMeshBounds.maxX; 1308 mGeoMesh->mMeshBounds.maxY = mInitialMesh->mMeshBounds.maxY; 1309 mGeoMesh->mMeshBounds.maxZ = mInitialMesh->mMeshBounds.maxZ; 1310 mGeoMesh->mMeshBounds.minX = mInitialMesh->mMeshBounds.minX; 1311 mGeoMesh->mMeshBounds.minY = mInitialMesh->mMeshBounds.minY; 1312 mGeoMesh->mMeshBounds.minZ = mInitialMesh->mMeshBounds.minZ; 1313 mGeoMesh->mMeshBounds.radius = mInitialMesh->mMeshBounds.radius; 1314 mGeoMesh->mMeshBounds.scaleFactor = mInitialMesh->mMeshBounds.scaleFactor; 1278 1315 1279 1316 // Copy skeleton name. 1280 if ( objmesh->hasSkeleton)1317 if (mInitialMesh->hasSkeleton) 1281 1318 { 1282 1319 mGeoMesh->hasSkeleton = true; 1283 1320 1284 strcpy(mGeoMesh->mSkeletonName,objmesh->mSkeletonName); 1285 } 1286 1287 // Copy mesh bones. 1288 if (!objmesh->mBones.empty()) 1289 { 1290 for (unsigned int j = 0; j < objmesh->mBones.size(); j++) 1291 { 1292 mGeoMesh->mBones.push_back(objmesh->mBones[j]); 1293 } 1294 } 1295 1321 strcpy(mGeoMesh->mSkeletonName,mInitialMesh->mSkeletonName); 1322 } 1323 1324 // If the mesh has shared vertex. 1325 if (shared_vertex) 1326 { 1327 fillUpSharedVertices(shared_vertex_map,ver_inds_rep_by_geo); 1328 } 1329 else 1330 { 1331 fillUpVertices(unique_verts_inds_by_geo,ver_inds_rep_by_geo); 1332 } 1333 1334 //------------------------// 1335 // Bones reassignment. // 1336 //------------------------// 1337 VertexBoneAssignment bone; 1338 bool vertex_found; 1339 MAPAINDIND::iterator it; 1340 1341 addNewBoneAssignments(); 1342 1343 // For each bone assignment. 1344 for (unsigned int i = 0; i < mInitialMesh->mBones.size(); i++) 1345 { 1346 bone.vertexIndex = mInitialMesh->mBones[i].vertexIndex; 1347 bone.boneIndex = mInitialMesh->mBones[i].boneIndex; 1348 bone.weight = mInitialMesh->mBones[i].weight; 1349 1350 vertex_found = false; 1351 1352 // If the vertex is found in the simplification model. 1353 if ((it = shared_vertex_map.find(bone.vertexIndex)) 1354 != 1355 shared_vertex_map.end()) 1356 { 1357 bone.vertexIndex = (*it).second; 1358 1359 mGeoMesh->mBones.push_back(bone); 1360 vertex_found = true; 1361 } 1362 } 1363 //------------------------------------------------------------// 1364 1365 delete[] unique_verts_inds_by_geo; 1366 delete[] ver_inds_rep_by_geo; 1367 delete[] inextvert; 1368 1369 // Store the simplification steps in MeshSimplificationSequence. 1370 int acum = 0; 1371 1372 for (size_t i = 0; i < mInitialMesh->mSubMeshCount; i++) 1373 { 1374 if (mInitialMesh->mSubMesh[i].mSharedVertexBuffer) 1375 { 1376 first_index_submesh[i] = 0; 1377 } 1378 else 1379 { 1380 first_index_submesh[i] = acum; 1381 } 1382 1383 acum += (int)mInitialMesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1384 } 1385 1386 vector<int> v0; 1387 vector<int> v1; 1388 vector<int> submesh0; 1389 vector<int> submesh1; 1390 1391 return msimpseq; 1392 } 1393 1394 //------------------------------------------------------------------------- 1395 // Add bone assignments for new bones. 1396 //------------------------------------------------------------------------- 1397 void SimplificationMethod::addNewBoneAssignments() 1398 { 1399 size_t vertex_id; 1400 size_t bones_count; 1401 VertexBoneAssignment bone; 1402 vector<VertexBoneAssignment>::iterator it; 1403 1404 // Copy new vertices. 1405 for (unsigned int i = 0; i < msimpseq->mNewVertices.size(); i++) 1406 { 1407 vertex_id = msimpseq->mNewVertices[i].id; 1408 1409 // Initialize number of bones. 1410 bones_count = mInitialMesh->mBones.size(); 1411 1412 // check if my twin-vertex-bone has a bone assignment 1413 // we check only the GeoMesh bones because the lodstrips 1414 // only works for sharedvertex bones. 1415 for (int j = 0; j < bones_count; j++) 1416 { 1417 it = mInitialMesh->mBones.begin() + j; 1418 1419 if (it->vertexIndex == msimpseq->mNewVertices[i].bonefrom) 1420 { 1421 bone.vertexIndex = vertex_id; 1422 bone.boneIndex = it->boneIndex; 1423 bone.weight = it->weight; 1424 1425 mInitialMesh->mBones.push_back(bone); 1426 bones_count++; 1427 } 1428 } 1429 } 1430 } 1431 1432 //------------------------------------------------------------------------- 1433 // Erase submeshes that have not indices. 1434 //------------------------------------------------------------------------- 1435 void SimplificationMethod::eraseVoidSubMeshes(Mesh *geoMesh) 1436 { 1437 SubMesh *geosubmesh; 1438 size_t valid_submesh_count; 1439 size_t submesh; 1440 1441 valid_submesh_count = 0; 1442 1443 // For each submesh. 1444 for (size_t i = 0; i < geoMesh->mSubMeshCount; i++) 1445 { 1446 // Debug. 1447 cout << "Indices of submesh " 1448 << i 1449 << ": " 1450 << geoMesh->mSubMesh[i].mIndexCount 1451 << endl; 1452 1453 if ((geoMesh->mSubMesh[i].mIndexCount > 0) 1454 || 1455 (indexMeshLeaves == i)) 1456 { 1457 valid_submesh_count++; 1458 } 1459 } 1460 1461 // Reassign submesh count. 1462 geoMesh->mSubMeshCount = valid_submesh_count; 1463 1464 // Reserve memory for valid submeshes. 1465 geosubmesh = new SubMesh[valid_submesh_count]; 1466 1467 submesh = 0; 1468 1469 // For each submesh. 1470 for (size_t i = 0; i < geoMesh->mSubMeshCount; i++) 1471 { 1472 // If leaves submesh is found. 1473 if (indexMeshLeaves == i) 1474 { 1475 geosubmesh[i].mStripCount = 0; 1476 geosubmesh[i].mStrip = NULL; 1477 1478 geosubmesh[i].mSharedVertexBuffer = false; 1479 1480 strcpy( geosubmesh[i].mMaterialName, 1481 mInitialMesh->mSubMesh[i].mMaterialName); 1482 1483 // Copy submesh bones. 1484 if (!mInitialMesh->mSubMesh[i].mBones.empty()) 1485 { 1486 for ( size_t j = 0; 1487 j < mInitialMesh->mSubMesh[i].mBones.size(); 1488 j++) 1489 { 1490 geosubmesh[i].mBones.push_back(mInitialMesh-> 1491 mSubMesh[i].mBones[j]); 1492 } 1493 } 1494 1495 // Leaves mesh. 1496 geosubmesh[i].mIndexCount = 1497 mInitialMesh->mSubMesh[i].mIndexCount; 1498 1499 geosubmesh[i].mIndex = 1500 new Geometry::Index[geosubmesh[i].mIndexCount]; 1501 1502 memcpy( geosubmesh[i].mIndex, 1503 mInitialMesh->mSubMesh[i].mIndex, 1504 mInitialMesh->mSubMesh[i].mIndexCount * sizeof(Index)); 1505 1506 // Copy the leaves submesh vertices. 1507 geosubmesh[i].mVertexBuffer = 1508 mInitialMesh->mSubMesh[i].mVertexBuffer->Clone(); 1509 1510 // Next valid submesh. 1511 submesh++; 1512 } 1513 else if (geoMesh->mSubMesh[i].mIndexCount > 0) 1514 { 1515 geosubmesh[submesh].mSharedVertexBuffer = 1516 geoMesh->mSubMesh[i].mSharedVertexBuffer; 1517 1518 geosubmesh[submesh].mVertexBuffer = geoMesh->mVertexBuffer; 1519 1520 geosubmesh[submesh].mType = geoMesh->mSubMesh[i].mType; 1521 1522 geosubmesh[submesh].mStripCount = geoMesh->mSubMesh[i].mStripCount; 1523 1524 geosubmesh[submesh].mIndexCount = geoMesh->mSubMesh[i].mIndexCount; 1525 1526 // Reserve memory for indices. 1527 geosubmesh[submesh].mIndex = 1528 new Index[geosubmesh[submesh].mIndexCount]; 1529 1530 // Copy indices. 1531 memcpy( geosubmesh[submesh].mIndex, 1532 geoMesh->mSubMesh[i].mIndex, 1533 geoMesh->mSubMesh[i].mIndexCount * sizeof(Index)); 1534 1535 // Reserve memory for array of strips. 1536 geosubmesh[submesh].mStrip = 1537 new Index*[geoMesh->mSubMesh[submesh].mStripCount]; 1538 1539 // Copy strip list of the submesh. 1540 for (size_t j = 0; j < geoMesh->mSubMesh[i].mStripCount; j++) 1541 { 1542 geosubmesh[submesh].mStrip[j] = geoMesh->mSubMesh[i].mStrip[j]; 1543 } 1544 1545 strcpy( geosubmesh[submesh].mMaterialName, 1546 geoMesh->mSubMesh[i].mMaterialName); 1547 1548 for (size_t j = 0; j < geoMesh->mSubMesh[i].mBones.size(); j++) 1549 { 1550 geosubmesh[submesh].mBones 1551 .push_back(geoMesh->mSubMesh[i].mBones[j]); 1552 } 1553 1554 // Next valid submesh. 1555 submesh++; 1556 } 1557 } 1558 1559 // Delete submeshes. 1560 delete []geoMesh->mSubMesh; 1561 1562 geoMesh->mSubMesh = geosubmesh; 1563 } 1564 1565 //------------------------------------------------------------------------- 1566 // Fill up vertices. 1567 //------------------------------------------------------------------------- 1568 void SimplificationMethod::fillUpVertices( 1569 MAPAINDIND *unique_verts_inds_by_geo, 1570 REPINDLIST *ver_inds_rep_by_geo) 1571 { 1296 1572 bool copiedShared = false; 1297 1573 1298 1574 // For each submesh. 1299 for (size_t i = 0; i < objmesh->mSubMeshCount; i++)1575 for (size_t i = 0; i < mInitialMesh->mSubMeshCount; i++) 1300 1576 { 1301 1577 mGeoMesh->mSubMesh[i].mStripCount = 0; … … 1306 1582 1307 1583 strcpy( mGeoMesh->mSubMesh[i].mMaterialName, 1308 objmesh->mSubMesh[i].mMaterialName);1584 mInitialMesh->mSubMesh[i].mMaterialName); 1309 1585 1310 1586 // Copy submesh bones. 1311 if (!objmesh->mSubMesh[i].mBones.empty()) 1312 { 1313 for (unsigned int j = 0; j < objmesh->mSubMesh[i].mBones.size(); j++) 1314 { 1315 mGeoMesh->mSubMesh[i].mBones.push_back(objmesh-> 1316 mSubMesh[i].mBones[j]); 1587 if (!mInitialMesh->mSubMesh[i].mBones.empty()) 1588 { 1589 for ( unsigned int j = 0; 1590 j < mInitialMesh->mSubMesh[i].mBones.size(); 1591 j++) 1592 { 1593 mGeoMesh->mSubMesh[i].mBones.push_back(mInitialMesh-> 1594 mSubMesh[i].mBones[j]); 1317 1595 } 1318 1596 } … … 1322 1600 // Indices vectors. 1323 1601 mGeoMesh->mSubMesh[i].mIndexCount = ver_inds_rep_by_geo[i].size(); 1324 1325 mGeoMesh->mSubMesh[i].mIndex = new Index[mGeoMesh->1326 1602 1603 mGeoMesh->mSubMesh[i].mIndex = new Geometry::Index[mGeoMesh-> 1604 mSubMesh[i].mIndexCount]; 1327 1605 1328 1606 // Store the indices. … … 1331 1609 // Obtain the indices that point at VertexBuffer. 1332 1610 mGeoMesh->mSubMesh[i].mIndex[j] = 1333 unique_verts_inds_by_geo[i]. 1334 operator [](ver_inds_rep_by_geo[i]. 1335 operator [](j)); 1611 unique_verts_inds_by_geo[i][ver_inds_rep_by_geo[i][j]]; 1336 1612 } 1337 1613 … … 1355 1631 1356 1632 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = 1357 1358 1633 unique_verts_inds_by_geo[i].size(); 1634 1359 1635 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = 1360 objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo;1636 mInitialMesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1361 1637 1362 1638 // Allocate memory for position, normal and texutre coordinates. 1363 1639 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition = 1364 1365 1640 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1641 1366 1642 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal = 1367 1368 1643 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1644 1369 1645 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 1370 1371 1372 for ( 1646 new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1647 1648 for (MAPAINDIND::iterator mapit = unique_verts_inds_by_geo[i].begin(); 1373 1649 mapit != unique_verts_inds_by_geo[i].end(); 1374 1650 mapit++) … … 1381 1657 // Construction of the submeshes. 1382 1658 Vector3 v3( (Real)M0.vertex(isrc)->operator [](0), 1383 1384 1385 1659 (Real)M0.vertex(isrc)->operator [](1), 1660 (Real)M0.vertex(isrc)->operator [](2)); 1661 1386 1662 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[idst] = v3; 1387 1663 1388 1664 // Normal coordinates. 1389 1665 Vector3 v3n( (Real)M0.normal(isrc)(0), 1390 1391 1392 1666 (Real)M0.normal(isrc)(1), 1667 (Real)M0.normal(isrc)(2)); 1668 1393 1669 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[idst] = v3n; 1394 1670 1395 1671 // Texture coordinates. 1396 1672 Vector2 v2( (Real)M0.texcoord(isrc)(0), 1397 1398 1673 (Real)M0.texcoord(isrc)(1)); 1674 1399 1675 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst] = v2; 1400 1676 } 1401 1677 } 1678 // Leaves mesh. 1402 1679 else 1403 1680 { 1404 // Leaves mesh. 1405 mGeoMesh->mSubMesh[i].mIndexCount = objmesh->mSubMesh[i].mIndexCount; 1406 1407 mGeoMesh->mSubMesh[i].mIndex = new Index[mGeoMesh->mSubMesh[i].mIndexCount]; 1681 mGeoMesh->mSubMesh[i].mIndexCount = 1682 mInitialMesh->mSubMesh[i].mIndexCount; 1683 1684 mGeoMesh->mSubMesh[i].mIndex = 1685 new Geometry::Index[mGeoMesh->mSubMesh[i].mIndexCount]; 1408 1686 1409 1687 // Copy the leaves submesh indexes. 1410 for (unsigned int j = 0; j < mGeoMesh->mSubMesh[i].mIndexCount; j++) 1411 { 1412 mGeoMesh->mSubMesh[i].mIndex[j] = objmesh->mSubMesh[i].mIndex[j]; 1688 for ( unsigned int j = 0; 1689 j < mGeoMesh->mSubMesh[i].mIndexCount; 1690 j++) 1691 { 1692 mGeoMesh->mSubMesh[i].mIndex[j] = mInitialMesh->mSubMesh[i].mIndex[j]; 1413 1693 } 1414 1694 1415 1695 // Copy the leaves submesh vertices. 1416 1696 mGeoMesh->mSubMesh[i].mVertexBuffer = new VertexBuffer(); 1417 1697 1418 1698 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = 1419 objmesh->mSubMesh[i].mVertexBuffer->mVertexCount;1420 1699 mInitialMesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1700 1421 1701 mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = 1422 objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo;1702 mInitialMesh->mSubMesh[i].mVertexBuffer->mVertexInfo; 1423 1703 1424 1704 // Allocate memory for position, normal and texture coordinates. 1425 1705 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition = 1426 1427 1706 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1707 1428 1708 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal = 1429 1430 1709 new Vector3[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1710 1431 1711 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords = 1432 1712 new Vector2[mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount]; 1433 1713 1434 1714 for ( unsigned int j = 0; … … 1438 1718 // Position. 1439 1719 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x = 1440 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x;1441 1720 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].x; 1721 1442 1722 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y = 1443 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y;1444 1723 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].y; 1724 1445 1725 mGeoMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z = 1446 objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z;1726 mInitialMesh->mSubMesh[i].mVertexBuffer->mPosition[j].z; 1447 1727 1448 1728 // Normals. 1449 1729 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x = 1450 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x;1451 1730 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].x; 1731 1452 1732 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y = 1453 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y;1454 1733 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].y; 1734 1455 1735 mGeoMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z = 1456 objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z;1736 mInitialMesh->mSubMesh[i].mVertexBuffer->mNormal[j].z; 1457 1737 1458 1738 // Textures. 1459 1739 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x = 1460 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x;1461 1740 mInitialMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x; 1741 1462 1742 mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y = 1463 objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 1464 } 1465 } 1466 } 1467 1468 delete[] unique_verts_inds_by_geo; 1469 delete[] ver_inds_rep_by_geo; 1470 delete[] inextvert; 1471 1472 // Store the simplification steps in MeshSimplificationSequence. 1473 int acum = 0; 1474 1475 for (size_t i = 0; i < objmesh->mSubMeshCount; i++) 1476 { 1477 if (objmesh->mSubMesh[i].mSharedVertexBuffer) 1478 { 1479 first_index_submesh[i] = 0; 1480 } 1481 else 1482 { 1483 first_index_submesh[i] = acum; 1484 } 1485 1486 acum += (int)objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; 1487 } 1488 1489 vector<int> v0; 1490 vector<int> v1; 1491 vector<int> submesh0; 1492 vector<int> submesh1; 1493 1494 return msimpseq; 1495 } 1496 1497 //--------------------------------------------------------------------------- 1743 mInitialMesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y; 1744 } 1745 } 1746 } 1747 } 1748 1749 //------------------------------------------------------------------------- 1750 // Fill up shared vertices. 1751 //------------------------------------------------------------------------- 1752 void SimplificationMethod::fillUpSharedVertices( 1753 MAPAINDIND &shared_vertex_map, 1754 REPINDLIST *ver_inds_rep_by_geo) 1755 { 1756 mGeoMesh->mVertexBuffer = new VertexBuffer(); 1757 1758 // For each submesh. 1759 for (size_t i = 0; i < mInitialMesh->mSubMeshCount; i++) 1760 { 1761 mGeoMesh-> 1762 mSubMesh[i].mSharedVertexBuffer = mInitialMesh-> 1763 mSubMesh[i].mSharedVertexBuffer; 1764 1765 // Reassign submesh vertex buffers. 1766 mGeoMesh->mSubMesh[i].mVertexBuffer = mGeoMesh->mVertexBuffer; 1767 1768 mGeoMesh->mSubMesh[i].mStripCount = 0; 1769 mGeoMesh->mSubMesh[i].mStrip = NULL; 1770 1771 strcpy( mGeoMesh->mSubMesh[i].mMaterialName, 1772 mInitialMesh->mSubMesh[i].mMaterialName); 1773 1774 // Indices vectors. 1775 mGeoMesh->mSubMesh[i].mIndexCount = ver_inds_rep_by_geo[i].size(); 1776 1777 mGeoMesh->mSubMesh[i].mIndex = new Geometry::Index[mGeoMesh-> 1778 mSubMesh[i].mIndexCount]; 1779 1780 // Store the indices. 1781 for (unsigned int j = 0; j < mGeoMesh->mSubMesh[i].mIndexCount; j++) 1782 { 1783 // Obtain the indices that point at VertexBuffer. 1784 mGeoMesh->mSubMesh[i].mIndex[j] = 1785 shared_vertex_map[ver_inds_rep_by_geo[i][j]]; 1786 } 1787 } 1788 1789 mGeoMesh->mVertexBuffer->mVertexCount = shared_vertex_map.size(); 1790 1791 mGeoMesh->mVertexBuffer->mVertexInfo = 1792 mInitialMesh->mVertexBuffer->mVertexInfo; 1793 1794 // Allocate memory for position, normal and texutre coordinates. 1795 mGeoMesh->mVertexBuffer->mPosition = 1796 new Vector3[mGeoMesh->mVertexBuffer->mVertexCount]; 1797 1798 mGeoMesh->mVertexBuffer->mNormal = 1799 new Vector3[mGeoMesh->mVertexBuffer->mVertexCount]; 1800 1801 mGeoMesh->mVertexBuffer->mTexCoords = 1802 new Vector2[mGeoMesh->mVertexBuffer->mVertexCount]; 1803 1804 for (MAPAINDIND::iterator mapit = shared_vertex_map.begin(); 1805 mapit != shared_vertex_map.end(); 1806 mapit++) 1807 { 1808 // Key and value. 1809 int isrc = mapit->first; 1810 int idst = mapit->second; 1811 1812 // Vertex coordinate. 1813 // Construction of the submeshes. 1814 Vector3 v3( (Real)M0.vertex(isrc)->operator [](0), 1815 (Real)M0.vertex(isrc)->operator [](1), 1816 (Real)M0.vertex(isrc)->operator [](2)); 1817 1818 mGeoMesh->mVertexBuffer->mPosition[idst] = v3; 1819 1820 // Normal coordinates. 1821 Vector3 v3n((Real)M0.normal(isrc)(0), 1822 (Real)M0.normal(isrc)(1), 1823 (Real)M0.normal(isrc)(2)); 1824 1825 mGeoMesh->mVertexBuffer->mNormal[idst] = v3n; 1826 1827 // Texture coordinates. 1828 Vector2 v2( (Real)M0.texcoord(isrc)(0), 1829 (Real)M0.texcoord(isrc)(1)); 1830 1831 mGeoMesh->mVertexBuffer->mTexCoords[idst] = v2; 1832 } 1833 } 1834 1835 //------------------------------------------------------------------------- 1498 1836 // Set submesh leaves. 1499 //------------------------------------------------------------------------- --1837 //------------------------------------------------------------------------- 1500 1838 void SimplificationMethod::setMeshLeaves(int meshLeaves) 1501 1839 { … … 1506 1844 Mesh * SimplificationMethod::GetMesh() 1507 1845 { 1846 // Delete void submeshes. 1847 eraseVoidSubMeshes(mGeoMesh); 1848 1508 1849 return mGeoMesh; 1509 1850 }
Note: See TracChangeset
for help on using the changeset viewer.