[983] | 1 | #include <stdlib.h> |
---|
| 2 | #include "SimplificationMethod.h" |
---|
| 3 | #include <iostream> |
---|
| 4 | #include <fstream> |
---|
| 5 | |
---|
| 6 | using namespace std; |
---|
| 7 | using namespace Geometry; |
---|
| 8 | |
---|
| 9 | #include <gfx/geom/ProxGrid.h> |
---|
| 10 | #include "quadrics.h" |
---|
| 11 | |
---|
| 12 | //--------------------------------------------------------------------------- |
---|
| 13 | // Constructor |
---|
| 14 | //--------------------------------------------------------------------------- |
---|
| 15 | SimplificationMethod::SimplificationMethod(const Mesh *m) |
---|
| 16 | { |
---|
| 17 | objmesh = m; |
---|
| 18 | mGeoMesh = NULL; |
---|
| 19 | first_index_submesh = new unsigned int[objmesh->mSubMeshCount]; |
---|
| 20 | indexMeshLeaves = -1; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | //--------------------------------------------------------------------------- |
---|
| 24 | // Destructor |
---|
| 25 | //--------------------------------------------------------------------------- |
---|
| 26 | SimplificationMethod::~SimplificationMethod() |
---|
| 27 | { |
---|
| 28 | delete heap; |
---|
| 29 | delete [] first_index_submesh; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | //--------------------------------------------------------------------------- |
---|
| 33 | // Recalculate the cost of the vertices of an edge |
---|
| 34 | //--------------------------------------------------------------------------- |
---|
[1025] | 35 | void SimplificationMethod::compute_pair_info(simplif::pair_info *auxpair) |
---|
[983] | 36 | { |
---|
[1025] | 37 | simplif::Vertex *v0 = auxpair->v0; |
---|
| 38 | simplif::Vertex *v1 = auxpair->v1; |
---|
[983] | 39 | |
---|
[1025] | 40 | simplif::vert_info& v0_info = vertex_info(v0); |
---|
| 41 | simplif::vert_info& v1_info = vertex_info(v1); |
---|
[983] | 42 | |
---|
[1025] | 43 | simplif::Mat4 Q = v0_info.Q + v1_info.Q; |
---|
| 44 | simplif::real norm = v0_info.norm + v1_info.norm; |
---|
[983] | 45 | |
---|
[1025] | 46 | auxpair->cost = simplif::quadrix_pair_target(Q, v0, v1, auxpair->candidate); |
---|
[983] | 47 | |
---|
[1025] | 48 | if( simplif::will_weight_by_area ) |
---|
[983] | 49 | auxpair->cost /= norm; |
---|
| 50 | |
---|
[1025] | 51 | if( simplif::will_preserve_mesh_quality ) |
---|
[983] | 52 | auxpair->cost += pair_mesh_penalty(M0, v0, v1, auxpair->candidate); |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | // |
---|
| 56 | // NOTICE: In the heap we use the negative cost. That's because |
---|
| 57 | // the heap is implemented as a MAX heap. |
---|
| 58 | // |
---|
| 59 | if( auxpair->isInHeap() ) |
---|
| 60 | { |
---|
| 61 | heap->update(auxpair, (float)-auxpair->cost); |
---|
| 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
| 65 | heap->insert(auxpair, (float)-auxpair->cost); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | //--------------------------------------------------------------------------- |
---|
| 70 | // Reasign the new vertex to the adequate face |
---|
| 71 | //--------------------------------------------------------------------------- |
---|
[1025] | 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) |
---|
[983] | 73 | { |
---|
| 74 | int nmapped = 0; |
---|
| 75 | |
---|
| 76 | if( F.vertex(0) == v1 || F.vertex(0) == v2 ) |
---|
| 77 | { f1 = vnew; nmapped++; } |
---|
| 78 | else f1 = *F.vertex(0); |
---|
| 79 | |
---|
| 80 | if( F.vertex(1) == v1 || F.vertex(1) == v2 ) |
---|
| 81 | { f2 = vnew; nmapped++; } |
---|
| 82 | else f2 = *F.vertex(1); |
---|
| 83 | |
---|
| 84 | if( F.vertex(2) == v1 || F.vertex(2) == v2 ) |
---|
| 85 | { f3 = vnew; nmapped++; } |
---|
| 86 | else f3 = *F.vertex(2); |
---|
| 87 | |
---|
| 88 | return nmapped; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | #define MESH_INVERSION_PENALTY 1e9 |
---|
| 92 | |
---|
| 93 | //--------------------------------------------------------------------------- |
---|
| 94 | //--------------------------------------------------------------------------- |
---|
[1025] | 95 | simplif::real SimplificationMethod::pair_mesh_penalty(simplif::Model& M, simplif::Vertex *v1, simplif::Vertex *v2, simplif::Vec3& vnew) |
---|
[983] | 96 | { |
---|
[1025] | 97 | static simplif::face_buffer changed; |
---|
[983] | 98 | changed.reset(); |
---|
| 99 | |
---|
| 100 | M.contractionRegion(v1, v2, changed); |
---|
| 101 | |
---|
| 102 | // real Nsum = 0; |
---|
[1025] | 103 | simplif::real Nmin = 0; |
---|
[983] | 104 | |
---|
| 105 | for(int i=0; i<changed.length(); i++) |
---|
| 106 | { |
---|
[1025] | 107 | simplif::Face& F = *changed(i); |
---|
| 108 | simplif::Vec3 f1, f2, f3; |
---|
[983] | 109 | |
---|
| 110 | int nmapped = predict_face(F, v1, v2, vnew, f1, f2, f3); |
---|
| 111 | |
---|
| 112 | // Only consider non-degenerate faces |
---|
| 113 | if( nmapped < 2 ) |
---|
| 114 | { |
---|
[1025] | 115 | simplif::Plane Pnew(f1, f2, f3); |
---|
| 116 | simplif::real delta = Pnew.normal() * F.plane().normal(); |
---|
[983] | 117 | |
---|
| 118 | if( Nmin > delta ) Nmin = delta; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | //return (-Nmin) * MESH_INVERSION_PENALTY; |
---|
| 123 | if( Nmin < 0.0 ) |
---|
| 124 | return MESH_INVERSION_PENALTY; |
---|
| 125 | else |
---|
| 126 | return 0.0; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | //--------------------------------------------------------------------------- |
---|
| 130 | // Returns true if the givens vertices are a valid pair |
---|
| 131 | //--------------------------------------------------------------------------- |
---|
[1025] | 132 | bool SimplificationMethod::check_for_pair(simplif::Vertex *v0, simplif::Vertex *v1) |
---|
[983] | 133 | { |
---|
[1025] | 134 | const simplif::pair_buffer& pairs = vertex_info(v0).pairs; |
---|
[983] | 135 | |
---|
| 136 | for(int i=0; i<pairs.length(); i++) |
---|
| 137 | { |
---|
| 138 | if( pairs(i)->v0==v1 || pairs(i)->v1==v1 ) |
---|
| 139 | return true; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | //--------------------------------------------------------------------------- |
---|
| 146 | // Create a new pair with two given vertices |
---|
| 147 | //--------------------------------------------------------------------------- |
---|
[1025] | 148 | simplif::pair_info *SimplificationMethod::new_pair(simplif::Vertex *v0, simplif::Vertex *v1) |
---|
[983] | 149 | { |
---|
[1025] | 150 | simplif::vert_info& v0_info = vertex_info(v0); |
---|
| 151 | simplif::vert_info& v1_info = vertex_info(v1); |
---|
[983] | 152 | |
---|
[1025] | 153 | simplif::pair_info *pair = new simplif::pair_info(v0,v1); |
---|
[983] | 154 | v0_info.pairs.add(pair); |
---|
| 155 | v1_info.pairs.add(pair); |
---|
| 156 | |
---|
| 157 | return pair; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | //--------------------------------------------------------------------------- |
---|
| 161 | // Remove a given pair |
---|
| 162 | //--------------------------------------------------------------------------- |
---|
[1025] | 163 | void SimplificationMethod::delete_pair(simplif::pair_info *auxpair) |
---|
[983] | 164 | { |
---|
[1025] | 165 | simplif::vert_info& v0_info = vertex_info(auxpair->v0); |
---|
| 166 | simplif::vert_info& v1_info = vertex_info(auxpair->v1); |
---|
[983] | 167 | |
---|
| 168 | v0_info.pairs.remove(v0_info.pairs.find(auxpair)); |
---|
| 169 | v1_info.pairs.remove(v1_info.pairs.find(auxpair)); |
---|
| 170 | |
---|
| 171 | if( auxpair->isInHeap() ) |
---|
| 172 | heap->kill(auxpair->getHeapPos()); |
---|
| 173 | |
---|
| 174 | delete auxpair; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | //--------------------------------------------------------------------------- |
---|
| 178 | // Contract a given pair |
---|
| 179 | //--------------------------------------------------------------------------- |
---|
[1025] | 180 | void SimplificationMethod::do_contract(simplif::Model& m, simplif::pair_info *auxpair) |
---|
[983] | 181 | { |
---|
[1025] | 182 | simplif::Vertex *v0 = auxpair->v0; simplif::Vertex *v1 = auxpair->v1; |
---|
| 183 | simplif::vert_info& v0_info = vertex_info(v0); |
---|
| 184 | simplif::vert_info& v1_info = vertex_info(v1); |
---|
| 185 | simplif::Vec3 vnew = auxpair->candidate; |
---|
[983] | 186 | int i; |
---|
| 187 | |
---|
| 188 | // Make v0 be the new vertex |
---|
| 189 | v0_info.Q += v1_info.Q; |
---|
| 190 | v0_info.norm += v1_info.norm; |
---|
| 191 | |
---|
[1025] | 192 | simplif::Vec3 p(vnew); |
---|
| 193 | simplif::Vec3 v0antes(v0->operator[](simplif::X),v0->operator[](simplif::Y),v0->operator[](simplif::Z)); |
---|
[983] | 194 | |
---|
| 195 | // Find the edge to remove (before remove it (contract())) |
---|
[1025] | 196 | simplif::Edge *econ=NULL; |
---|
[983] | 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 |
---|
[1025] | 212 | static simplif::face_buffer changed; |
---|
[983] | 213 | changed.reset(); |
---|
| 214 | m.contract(v0, v1, vnew, changed); |
---|
| 215 | |
---|
| 216 | // Stores the simplification step |
---|
| 217 | MeshSimplificationSequence::Step simplifstep; |
---|
[1025] | 218 | simplif::Vec3 vdesp = p - v0antes; |
---|
[983] | 219 | |
---|
| 220 | // Stores the displacement in the simplification step |
---|
[1025] | 221 | simplifstep.x=(float)vdesp[simplif::X]; |
---|
| 222 | simplifstep.y = (float)vdesp[simplif::Y]; |
---|
| 223 | simplifstep.z = (float)vdesp[simplif::Z]; |
---|
[1007] | 224 | |
---|
| 225 | // Submeshes which pertains each vertex |
---|
| 226 | simplifstep.mV1 = v0->validID(); |
---|
[983] | 227 | |
---|
[1007] | 228 | // The simplification method returns as v1 the dead vertex. |
---|
| 229 | simplifstep.mV0 = v1->validID(); |
---|
[983] | 230 | |
---|
[1007] | 231 | // Number of triangles that are removed in this simplification step. |
---|
| 232 | int _numDelTris = 0; // 1 or 2 triangles can be removed. |
---|
| 233 | |
---|
| 234 | for (i = 0; i < changed.length(); i++) |
---|
[983] | 235 | { |
---|
[1007] | 236 | if (!changed(i)->isValid()) |
---|
| 237 | { |
---|
[983] | 238 | _numDelTris++; |
---|
[1007] | 239 | } |
---|
[983] | 240 | } |
---|
| 241 | |
---|
[1007] | 242 | int del_index = 0; |
---|
| 243 | |
---|
[983] | 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 | { |
---|
[1025] | 247 | simplif::Face *auxface = changed(i); |
---|
[1007] | 248 | |
---|
| 249 | if (auxface->isValid()) |
---|
[983] | 250 | { |
---|
| 251 | // Modified triangles |
---|
| 252 | simplifstep.mModfaces.push_back(auxface->validID()); |
---|
| 253 | } |
---|
[1007] | 254 | else |
---|
[983] | 255 | { |
---|
| 256 | // Removed triangles |
---|
| 257 | if (_numDelTris==1) |
---|
| 258 | { |
---|
| 259 | simplifstep.mT0=auxface->validID(); |
---|
| 260 | simplifstep.mT1=auxface->validID(); |
---|
| 261 | } |
---|
[1007] | 262 | else |
---|
[983] | 263 | { |
---|
[1007] | 264 | if(del_index==0) |
---|
[983] | 265 | { |
---|
| 266 | simplifstep.mT0=auxface->validID(); |
---|
| 267 | del_index++; |
---|
| 268 | } |
---|
[1007] | 269 | else |
---|
[983] | 270 | { |
---|
| 271 | simplifstep.mT1=auxface->validID(); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | decim_data.push_back(simplifstep); // Simplification sequence that make reference to the simplification method sequence |
---|
| 278 | |
---|
| 279 | #ifdef SUPPORT_VCOLOR |
---|
| 280 | // |
---|
| 281 | // If the vertices are colored, color the new vertex |
---|
| 282 | // using the average of the old colors. |
---|
| 283 | v0->props->color += v1->props->color; |
---|
| 284 | v0->props->color /= 2; |
---|
| 285 | #endif |
---|
| 286 | |
---|
| 287 | // Remove the pair that we just contracted |
---|
| 288 | delete_pair(auxpair); |
---|
| 289 | |
---|
| 290 | // Recalculate pairs associated with v0 |
---|
| 291 | for(i=0; i<v0_info.pairs.length(); i++) |
---|
| 292 | { |
---|
[1025] | 293 | simplif::pair_info *p = v0_info.pairs(i); |
---|
[983] | 294 | compute_pair_info(p); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // Process pairs associated with now dead vertex |
---|
[1025] | 298 | static simplif::pair_buffer condemned(6); // collect condemned pairs for execution |
---|
[983] | 299 | condemned.reset(); |
---|
| 300 | |
---|
| 301 | for(i=0; i<v1_info.pairs.length(); i++) |
---|
| 302 | { |
---|
[1025] | 303 | simplif::pair_info *p = v1_info.pairs(i); |
---|
[983] | 304 | |
---|
[1025] | 305 | simplif::Vertex *u; |
---|
[983] | 306 | if( p->v0 == v1 ) |
---|
| 307 | u = p->v1; |
---|
| 308 | else |
---|
| 309 | if( p->v1 == v1) |
---|
| 310 | u = p->v0; |
---|
| 311 | else |
---|
| 312 | 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; |
---|
| 318 | v0_info.pairs.add(p); |
---|
| 319 | compute_pair_info(p); |
---|
| 320 | } |
---|
| 321 | else |
---|
| 322 | condemned.add(p); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | for(i=0; i<condemned.length(); i++) |
---|
| 326 | // Do you have any last requests? |
---|
| 327 | delete_pair(condemned(i)); |
---|
| 328 | v1_info.pairs.reset(); // safety precaution |
---|
| 329 | |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | //--------------------------------------------------------------------------- |
---|
| 333 | //--------------------------------------------------------------------------- |
---|
[1025] | 334 | bool SimplificationMethod::decimate_quadric(simplif::Vertex *v, simplif::Mat4& Q) |
---|
[983] | 335 | { |
---|
| 336 | if( vinfo.length() > 0 ) |
---|
| 337 | { |
---|
| 338 | Q = vinfo(v->uniqID).Q; |
---|
| 339 | return true; |
---|
| 340 | } |
---|
| 341 | else |
---|
| 342 | return false; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | //--------------------------------------------------------------------------- |
---|
| 346 | // Extract a pair from the heap, concract it and remove it form the heap |
---|
| 347 | //--------------------------------------------------------------------------- |
---|
[1025] | 348 | void SimplificationMethod::decimate_contract(simplif::Model& m) |
---|
[983] | 349 | { |
---|
[1025] | 350 | simplif::heap_node *top; |
---|
| 351 | simplif::pair_info *pair; |
---|
[983] | 352 | |
---|
| 353 | for(;;) |
---|
| 354 | { |
---|
| 355 | top = heap->extract(); |
---|
| 356 | if( !top ) return; |
---|
[1025] | 357 | pair = (simplif::pair_info *)top->obj; |
---|
[983] | 358 | |
---|
| 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 | { |
---|
[1025] | 363 | simplif::Edge *econ = pair->v0->edgeUses()(i); |
---|
[983] | 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) |
---|
| 373 | 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 | //--------------------------------------------------------------------------- |
---|
[1025] | 385 | simplif::real SimplificationMethod::decimate_error(simplif::Vertex *v) |
---|
[983] | 386 | { |
---|
[1025] | 387 | simplif::vert_info& info = vertex_info(v); |
---|
[983] | 388 | |
---|
[1025] | 389 | simplif::real err = simplif::quadrix_evaluate_vertex(*v, info.Q); |
---|
[983] | 390 | |
---|
[1025] | 391 | if( simplif::will_weight_by_area ) |
---|
[983] | 392 | err /= info.norm; |
---|
| 393 | |
---|
| 394 | return err; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | //--------------------------------------------------------------------------- |
---|
| 398 | // Extract the minimum cost of the the valid nodes (not simplified) |
---|
| 399 | //--------------------------------------------------------------------------- |
---|
[1025] | 400 | simplif::real SimplificationMethod::decimate_min_error() |
---|
[983] | 401 | { |
---|
[1025] | 402 | simplif::heap_node *top; |
---|
| 403 | simplif::pair_info *pair; |
---|
[983] | 404 | |
---|
| 405 | for(;;) |
---|
| 406 | { |
---|
| 407 | top = heap->top(); |
---|
| 408 | if( !top ) return -1.0; |
---|
[1025] | 409 | pair = (simplif::pair_info *)top->obj; |
---|
[983] | 410 | |
---|
| 411 | if( pair->isValid() ) |
---|
| 412 | break; |
---|
| 413 | |
---|
| 414 | top = heap->extract(); |
---|
| 415 | delete_pair(pair); |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | return pair->cost; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | //--------------------------------------------------------------------------- |
---|
| 422 | // Returns the maximum error by vertex |
---|
| 423 | //--------------------------------------------------------------------------- |
---|
[1025] | 424 | simplif::real SimplificationMethod::decimate_max_error(simplif::Model& m) |
---|
[983] | 425 | { |
---|
[1025] | 426 | simplif::real max_err = 0; |
---|
[983] | 427 | |
---|
| 428 | for(int i=0; i<m.vertCount(); i++) |
---|
| 429 | if( m.vertex(i)->isValid() ) |
---|
| 430 | { |
---|
| 431 | max_err = MAX(max_err, decimate_error(m.vertex(i))); |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | return max_err; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | //--------------------------------------------------------------------------- |
---|
| 438 | // Initializations |
---|
| 439 | //--------------------------------------------------------------------------- |
---|
[1025] | 440 | void SimplificationMethod::decimate_init(simplif::Model& m, simplif::real limit) |
---|
[983] | 441 | { |
---|
| 442 | int i,j; |
---|
| 443 | |
---|
| 444 | vinfo.init(m.vertCount()); |
---|
| 445 | |
---|
[1025] | 446 | if( simplif::will_use_vertex_constraint ) |
---|
[983] | 447 | for(i=0; i<m.vertCount(); i++) |
---|
| 448 | { |
---|
[1025] | 449 | simplif::Vertex *v = m.vertex(i); |
---|
[983] | 450 | if( v->isValid() ) |
---|
[1025] | 451 | vertex_info(v).Q = simplif::quadrix_vertex_constraint(*v); |
---|
[983] | 452 | } |
---|
| 453 | |
---|
| 454 | for(i=0; i<m.faceCount(); i++) |
---|
| 455 | if( m.face(i)->isValid() ) |
---|
| 456 | { |
---|
[1025] | 457 | if( simplif::will_use_plane_constraint ) |
---|
[983] | 458 | { |
---|
[1025] | 459 | simplif::Mat4 Q = simplif::quadrix_plane_constraint(*m.face(i)); |
---|
| 460 | simplif::real norm = 0.0; |
---|
[983] | 461 | |
---|
[1025] | 462 | if( simplif::will_weight_by_area ) |
---|
[983] | 463 | { |
---|
| 464 | norm = m.face(i)->area(); |
---|
| 465 | Q *= norm; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | for(j=0; j<3; j++) |
---|
| 469 | { |
---|
| 470 | vertex_info(m.face(i)->vertex(j)).Q += Q; |
---|
| 471 | vertex_info(m.face(i)->vertex(j)).norm += norm; |
---|
| 472 | |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
[1025] | 477 | if( simplif::will_constrain_boundaries ) |
---|
[983] | 478 | { |
---|
| 479 | for(i=0; i<m.edgeCount(); i++) |
---|
[1025] | 480 | if( m.edge(i)->isValid() && simplif::check_for_discontinuity(m.edge(i)) ) |
---|
[983] | 481 | { |
---|
[1025] | 482 | simplif::Mat4 B = simplif::quadrix_discontinuity_constraint(m.edge(i)); |
---|
| 483 | simplif::real norm = 0.0; |
---|
[983] | 484 | |
---|
[1025] | 485 | if( simplif::will_weight_by_area ) |
---|
[983] | 486 | { |
---|
[1025] | 487 | norm = simplif::norm2(*m.edge(i)->org() - *m.edge(i)->dest()); |
---|
[983] | 488 | B *= norm; |
---|
| 489 | } |
---|
| 490 | |
---|
[1025] | 491 | B *= simplif::boundary_constraint_weight; |
---|
[983] | 492 | vertex_info(m.edge(i)->org()).Q += B; |
---|
| 493 | vertex_info(m.edge(i)->org()).norm += norm; |
---|
| 494 | vertex_info(m.edge(i)->dest()).Q += B; |
---|
| 495 | vertex_info(m.edge(i)->dest()).norm += norm; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
[1025] | 499 | heap = new simplif::Heap(m.validEdgeCount); |
---|
[983] | 500 | |
---|
| 501 | int pair_count = 0; |
---|
| 502 | |
---|
| 503 | for(i=0; i<m.edgeCount(); i++) |
---|
| 504 | if( m.edge(i)->isValid() ) |
---|
| 505 | { |
---|
[1025] | 506 | simplif::pair_info *pair = new_pair(m.edge(i)->org(), m.edge(i)->dest()); |
---|
[983] | 507 | //pointers_to_remove.push_back(pair); |
---|
| 508 | compute_pair_info(pair); |
---|
| 509 | pair_count++; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | if( limit<0 ) |
---|
| 513 | { |
---|
| 514 | limit = m.bounds.radius * 0.05; |
---|
| 515 | } |
---|
| 516 | proximity_limit = limit * limit; |
---|
| 517 | if( proximity_limit > 0 ) |
---|
| 518 | { |
---|
[1025] | 519 | simplif::ProxGrid grid(m.bounds.min, m.bounds.max, limit); |
---|
[983] | 520 | for(i=0; i<m.vertCount(); i++) |
---|
| 521 | grid.addPoint(m.vertex(i)); |
---|
| 522 | |
---|
[1025] | 523 | simplif::buffer<simplif::Vec3 *> nearby(32); |
---|
[983] | 524 | for(i=0; i<m.vertCount(); i++) |
---|
| 525 | { |
---|
| 526 | nearby.reset(); |
---|
| 527 | grid.proximalPoints(m.vertex(i), nearby); |
---|
| 528 | |
---|
| 529 | for(j=0; j<nearby.length(); j++) |
---|
| 530 | { |
---|
[1025] | 531 | simplif::Vertex *v1 = m.vertex(i); |
---|
| 532 | simplif::Vertex *v2 = (simplif::Vertex *)nearby(j); |
---|
[983] | 533 | |
---|
| 534 | if( v1->isValid() && v2->isValid() ) |
---|
| 535 | { |
---|
| 536 | #ifdef SAFETY |
---|
| 537 | assert(pair_is_valid(v1,v2)); |
---|
| 538 | #endif |
---|
| 539 | if( !check_for_pair(v1,v2) ) |
---|
| 540 | { |
---|
[1025] | 541 | simplif::pair_info *pair = new_pair(v1,v2); |
---|
[983] | 542 | compute_pair_info(pair); |
---|
| 543 | pair_count++; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | //--------------------------------------------------------------------------- |
---|
| 553 | // Initilizations |
---|
| 554 | //--------------------------------------------------------------------------- |
---|
[984] | 555 | void SimplificationMethod::simplifmethod_init(void) |
---|
[983] | 556 | { |
---|
| 557 | int i; |
---|
| 558 | |
---|
[984] | 559 | // Change mesh structure. |
---|
[983] | 560 | geomesh2simplifModel(); |
---|
| 561 | |
---|
| 562 | M0.bounds.complete(); |
---|
| 563 | |
---|
| 564 | initialVertCount = M0.vertCount(); |
---|
| 565 | initialEdgeCount = M0.edgeCount(); |
---|
| 566 | initialFaceCount = M0.faceCount(); |
---|
| 567 | |
---|
| 568 | // Get rid of degenerate faces |
---|
| 569 | for(i=0; i<M0.faceCount(); i++) |
---|
| 570 | if( !M0.face(i)->plane().isValid() ) |
---|
| 571 | M0.killFace(M0.face(i)); |
---|
| 572 | |
---|
| 573 | M0.removeDegeneracy(M0.allFaces()); |
---|
| 574 | |
---|
| 575 | // Get rid of unused vertices |
---|
| 576 | for(i=0; i<M0.vertCount(); i++) |
---|
| 577 | { |
---|
| 578 | if( M0.vertex(i)->edgeUses().length() == 0 ) |
---|
| 579 | M0.vertex(i)->kill(); |
---|
| 580 | } |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | //--------------------------------------------------------------------------- |
---|
| 584 | // Do the contractions till the required LOD (percentage option) |
---|
| 585 | //--------------------------------------------------------------------------- |
---|
[984] | 586 | void SimplificationMethod::simplifmethod_run(int finalfaces,TIPOFUNC upb) |
---|
[983] | 587 | { |
---|
[1025] | 588 | decimate_init(M0, simplif::pair_selection_tolerance); |
---|
[983] | 589 | unsigned int contador = 0; |
---|
| 590 | float percent; // simplification percentage. |
---|
| 591 | |
---|
| 592 | percent = (float)(2.0 * 100.0) / (M0.validFaceCount - finalfaces); |
---|
| 593 | |
---|
| 594 | // Update progress bar. |
---|
| 595 | if (upb) |
---|
| 596 | { |
---|
| 597 | upb(0.0); |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) |
---|
| 601 | { |
---|
| 602 | it->mModfaces.clear(); |
---|
| 603 | } |
---|
| 604 | decim_data.clear(); |
---|
| 605 | |
---|
[1025] | 606 | while( M0.validFaceCount > finalfaces/*simplif::face_target*/ |
---|
[983] | 607 | && M0.validFaceCount > 1 |
---|
[1025] | 608 | && decimate_min_error() < simplif::error_tolerance ) |
---|
[983] | 609 | { |
---|
| 610 | decimate_contract(M0); |
---|
| 611 | |
---|
| 612 | // Update progress bar. |
---|
| 613 | if (upb) |
---|
| 614 | { |
---|
| 615 | upb(percent); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | // Update progress bar. |
---|
| 620 | if (upb) |
---|
| 621 | { |
---|
| 622 | upb(100.0); |
---|
| 623 | } |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | //--------------------------------------------------------------------------- |
---|
| 627 | // Do the contractions till the required LOD (number of vertices option) |
---|
| 628 | //--------------------------------------------------------------------------- |
---|
[984] | 629 | void SimplificationMethod::simplifmethod_runv(int numvertices,TIPOFUNC upb) |
---|
[983] | 630 | { |
---|
[1025] | 631 | decimate_init(M0, simplif::pair_selection_tolerance); |
---|
[983] | 632 | unsigned int contador = 0; |
---|
| 633 | int previousValidVertCount; |
---|
| 634 | float percent; // Simplification percentage. |
---|
| 635 | |
---|
| 636 | previousValidVertCount = 0; |
---|
| 637 | |
---|
| 638 | percent = (float)(2.0 * 100.0) /(float)((M0.validFaceCount - numvertices / 3.0)); |
---|
| 639 | |
---|
| 640 | // Update progress bar. |
---|
| 641 | if (upb) |
---|
| 642 | { |
---|
| 643 | upb(0.0); |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | for (std::vector<MeshSimplificationSequence::Step>::iterator it= decim_data.begin(); it!=decim_data.end(); it++) |
---|
| 647 | { |
---|
| 648 | it->mModfaces.clear(); |
---|
| 649 | } |
---|
| 650 | decim_data.clear(); |
---|
| 651 | |
---|
[1025] | 652 | while( M0.validVertCount > numvertices /*simplif::face_target*/ |
---|
[983] | 653 | && |
---|
[1025] | 654 | decimate_min_error() < simplif::error_tolerance |
---|
[983] | 655 | && |
---|
| 656 | previousValidVertCount != M0.validVertCount) |
---|
| 657 | { |
---|
| 658 | previousValidVertCount = M0.validVertCount; |
---|
| 659 | |
---|
| 660 | decimate_contract(M0); |
---|
| 661 | |
---|
| 662 | // Update progress bar. |
---|
| 663 | if (upb) |
---|
| 664 | { |
---|
| 665 | upb(percent); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | // Update progress bar. |
---|
| 670 | if (upb) |
---|
| 671 | { |
---|
| 672 | upb(100.0); |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | //--------------------------------------------------------------------------- |
---|
| 677 | // Class to create a map without repeated vertices |
---|
| 678 | //--------------------------------------------------------------------------- |
---|
| 679 | class _float3_ |
---|
| 680 | { |
---|
| 681 | public: |
---|
| 682 | 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; } |
---|
| 686 | bool operator<(const _float3_ &f) const |
---|
| 687 | { |
---|
| 688 | if (x<f.x) return true; |
---|
| 689 | if (x>f.x) return false; |
---|
| 690 | if (y<f.y) return true; |
---|
| 691 | if (y>f.y) return false; |
---|
| 692 | if (z<f.z) return true; |
---|
| 693 | if (z>f.z) return false; |
---|
| 694 | return false; |
---|
| 695 | } |
---|
| 696 | }; |
---|
| 697 | |
---|
| 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 |
---|
[984] | 719 | if (objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS) |
---|
[983] | 720 | { |
---|
[984] | 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, |
---|
| 725 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, |
---|
| 726 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z); |
---|
[983] | 727 | |
---|
[984] | 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 |
---|
[1025] | 732 | simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, |
---|
[984] | 733 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, |
---|
| 734 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); |
---|
[1025] | 735 | M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, |
---|
[984] | 736 | objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, |
---|
| 737 | objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); |
---|
[983] | 738 | |
---|
[1025] | 739 | M0.in_TexCoord(simplif::Vec2(objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].x, |
---|
[984] | 740 | objmesh->mSubMesh[i].mVertexBuffer->mTexCoords[j].y)); |
---|
| 741 | vertices_map[auxvert] = num_final_verts; |
---|
| 742 | num_final_verts ++; |
---|
[983] | 743 | |
---|
[984] | 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 |
---|
[983] | 750 | |
---|
[984] | 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 |
---|
[983] | 760 | |
---|
[984] | 761 | // Debug |
---|
| 762 | num_repeated_verts++; |
---|
| 763 | } |
---|
| 764 | } |
---|
[983] | 765 | } |
---|
| 766 | else //the model has not texture information |
---|
[984] | 767 | { |
---|
| 768 | //For all the vertices of each submesh |
---|
| 769 | for (size_t j=0; j<objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; j++) |
---|
[983] | 770 | { |
---|
[984] | 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); |
---|
[983] | 774 | |
---|
[984] | 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 |
---|
[1025] | 779 | simplifindex=M0.in_Vertex(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].x, |
---|
[984] | 780 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].y, |
---|
| 781 | objmesh->mSubMesh[i].mVertexBuffer->mPosition[j].z)); |
---|
[1025] | 782 | M0.in_Normal(simplif::Vec3(objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].x, |
---|
[984] | 783 | objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].y, |
---|
| 784 | objmesh->mSubMesh[i].mVertexBuffer->mNormal[j].z)); |
---|
[1025] | 785 | M0.in_TexCoord(simplif::Vec2(0.0,0.0)); |
---|
[984] | 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 | } |
---|
[983] | 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 |
---|
| 931 | //--------------------------------------------------------------------------- |
---|
| 932 | MeshSimplificationSequence *SimplificationMethod::Decimate( float lod, |
---|
| 933 | int simpliftype, |
---|
| 934 | TIPOFUNC upb) |
---|
| 935 | { |
---|
[984] | 936 | simplifmethod_init(); |
---|
[983] | 937 | |
---|
| 938 | if (simpliftype == 0) |
---|
| 939 | { |
---|
| 940 | //Percentage option |
---|
[984] | 941 | simplifmethod_run((int)(number_of_triangles*lod),upb); |
---|
[983] | 942 | } |
---|
| 943 | else |
---|
| 944 | { |
---|
| 945 | //Number of vertices option |
---|
[984] | 946 | simplifmethod_runv((int)lod,upb); |
---|
[983] | 947 | } |
---|
| 948 | |
---|
| 949 | // Store unique vertices by submesh (without repetitions). |
---|
| 950 | typedef std::map<int,int> MAPAINDIND; |
---|
| 951 | |
---|
| 952 | // Store all the indices by submesh (with repetitions). |
---|
| 953 | typedef std::vector<int> REPINDLIST; |
---|
| 954 | |
---|
| 955 | MAPAINDIND *unique_verts_inds_by_geo = new MAPAINDIND[objmesh->mSubMeshCount]; |
---|
[984] | 956 | REPINDLIST *ver_inds_rep_by_geo = new REPINDLIST[objmesh->mSubMeshCount]; |
---|
[983] | 957 | |
---|
| 958 | // Index counter by submesh. |
---|
| 959 | int *inextvert = new int[objmesh->mSubMeshCount]; |
---|
| 960 | |
---|
| 961 | for (unsigned int i = 0; i < objmesh->mSubMeshCount; i++) |
---|
| 962 | { |
---|
| 963 | inextvert[i] = 0; |
---|
| 964 | } |
---|
| 965 | |
---|
| 966 | |
---|
| 967 | // Construct the model. |
---|
| 968 | for (int i = 0; i < M0.faceCount(); i++) |
---|
| 969 | { |
---|
| 970 | if (M0.face(i)->isValid()) |
---|
| 971 | { |
---|
[1025] | 972 | simplif::Face *auxface = M0.face(i); |
---|
[983] | 973 | |
---|
| 974 | // Determine to which submesh pertains the triangle. |
---|
| 975 | int igeo = auxface->igeo; |
---|
| 976 | |
---|
| 977 | // Insert to each submesh its triangles. |
---|
| 978 | std::map<int,int>::iterator findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(0)->validID()); |
---|
| 979 | if (findit == unique_verts_inds_by_geo[igeo].end()) |
---|
| 980 | { |
---|
| 981 | // If it is not added... add and map it. |
---|
| 982 | unique_verts_inds_by_geo[igeo][auxface->vertex(0)->validID()] = inextvert[igeo]; |
---|
| 983 | inextvert[igeo]++; |
---|
| 984 | } |
---|
| 985 | findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(1)->validID()); |
---|
| 986 | if (findit == unique_verts_inds_by_geo[igeo].end()) |
---|
| 987 | { |
---|
| 988 | // If it is not added... add and map it. |
---|
| 989 | unique_verts_inds_by_geo[igeo][auxface->vertex(1)->validID()] = inextvert[igeo]; |
---|
| 990 | inextvert[igeo]++; |
---|
| 991 | } |
---|
| 992 | findit = unique_verts_inds_by_geo[igeo].find(auxface->vertex(2)->validID()); |
---|
| 993 | if (findit == unique_verts_inds_by_geo[igeo].end()) |
---|
| 994 | { |
---|
| 995 | // If it is not added... add and map it. |
---|
| 996 | unique_verts_inds_by_geo[igeo][auxface->vertex(2)->validID()] = inextvert[igeo]; |
---|
| 997 | inextvert[igeo]++; |
---|
| 998 | } |
---|
| 999 | |
---|
| 1000 | // Total number of indices by submesh. |
---|
[984] | 1001 | ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(0)->validID()); |
---|
| 1002 | ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(1)->validID()); |
---|
| 1003 | ver_inds_rep_by_geo[igeo].push_back(auxface->vertex(2)->validID()); |
---|
[983] | 1004 | } |
---|
| 1005 | } |
---|
| 1006 | |
---|
| 1007 | // Output simplified mesh. |
---|
| 1008 | mGeoMesh = new Mesh(); |
---|
| 1009 | mGeoMesh->mVertexBuffer = new VertexBuffer(); |
---|
| 1010 | |
---|
| 1011 | mGeoMesh->mSubMeshCount = objmesh->mSubMeshCount; |
---|
[986] | 1012 | |
---|
[983] | 1013 | // Memory allocation for the submeshes. |
---|
| 1014 | mGeoMesh->mSubMesh = new SubMesh[objmesh->mSubMeshCount]; |
---|
| 1015 | |
---|
| 1016 | // Fill up bounding box settings. |
---|
| 1017 | mGeoMesh->mMeshBounds.maxX = objmesh->mMeshBounds.maxX; |
---|
| 1018 | mGeoMesh->mMeshBounds.maxY = objmesh->mMeshBounds.maxY; |
---|
| 1019 | mGeoMesh->mMeshBounds.maxZ = objmesh->mMeshBounds.maxZ; |
---|
| 1020 | mGeoMesh->mMeshBounds.minX = objmesh->mMeshBounds.minX; |
---|
| 1021 | mGeoMesh->mMeshBounds.minY = objmesh->mMeshBounds.minY; |
---|
| 1022 | mGeoMesh->mMeshBounds.minZ = objmesh->mMeshBounds.minZ; |
---|
| 1023 | mGeoMesh->mMeshBounds.radius = objmesh->mMeshBounds.radius; |
---|
| 1024 | mGeoMesh->mMeshBounds.scaleFactor = objmesh->mMeshBounds.scaleFactor; |
---|
| 1025 | |
---|
| 1026 | // Copy skeleton name. |
---|
| 1027 | if (objmesh->hasSkeleton) |
---|
| 1028 | { |
---|
| 1029 | mGeoMesh->hasSkeleton = true; |
---|
[986] | 1030 | |
---|
[983] | 1031 | strcpy(mGeoMesh->mSkeletonName,objmesh->mSkeletonName); |
---|
| 1032 | } |
---|
| 1033 | |
---|
| 1034 | // Copy mesh bones. |
---|
| 1035 | if (!objmesh->mBones.empty()) |
---|
| 1036 | { |
---|
[985] | 1037 | for (unsigned int j = 0; j < objmesh->mBones.size(); j++) |
---|
[983] | 1038 | { |
---|
| 1039 | mGeoMesh->mBones.push_back(objmesh->mBones[j]); |
---|
| 1040 | } |
---|
| 1041 | } |
---|
| 1042 | |
---|
| 1043 | bool copiedShared = false; |
---|
| 1044 | |
---|
| 1045 | for (size_t i = 0; i < objmesh->mSubMeshCount; i++) |
---|
| 1046 | { |
---|
| 1047 | mGeoMesh->mSubMesh[i].mStripCount = 0; |
---|
| 1048 | mGeoMesh->mSubMesh[i].mStrip = NULL; |
---|
| 1049 | |
---|
[986] | 1050 | mGeoMesh-> |
---|
| 1051 | mSubMesh[i].mSharedVertexBuffer = false; |
---|
| 1052 | |
---|
[983] | 1053 | strcpy( mGeoMesh->mSubMesh[i].mMaterialName, |
---|
[986] | 1054 | objmesh->mSubMesh[i].mMaterialName); |
---|
[983] | 1055 | |
---|
| 1056 | // Copy submesh bones. |
---|
| 1057 | if (!objmesh->mSubMesh[i].mBones.empty()) |
---|
| 1058 | { |
---|
[985] | 1059 | for (unsigned int j = 0; j < objmesh->mSubMesh[i].mBones.size(); j++) |
---|
[983] | 1060 | { |
---|
| 1061 | mGeoMesh->mSubMesh[i].mBones.push_back(objmesh-> |
---|
[986] | 1062 | mSubMesh[i].mBones[j]); |
---|
[983] | 1063 | } |
---|
| 1064 | } |
---|
| 1065 | |
---|
| 1066 | if (i != indexMeshLeaves) |
---|
| 1067 | { |
---|
| 1068 | // Indices vectors. |
---|
[984] | 1069 | mGeoMesh->mSubMesh[i].mIndexCount = ver_inds_rep_by_geo[i].size(); |
---|
[983] | 1070 | mGeoMesh->mSubMesh[i].mIndex = new Index[mGeoMesh-> |
---|
[986] | 1071 | mSubMesh[i].mIndexCount]; |
---|
[983] | 1072 | |
---|
| 1073 | // Store the indices. |
---|
| 1074 | for (unsigned int j = 0; j < mGeoMesh->mSubMesh[i].mIndexCount; j++) |
---|
| 1075 | { |
---|
| 1076 | // Obtain the indices that point at VertexBuffer. |
---|
[984] | 1077 | mGeoMesh->mSubMesh[i].mIndex[j] = unique_verts_inds_by_geo[i].operator [](ver_inds_rep_by_geo[i].operator [](j)); |
---|
[983] | 1078 | } |
---|
| 1079 | |
---|
| 1080 | // Copy the vertices. |
---|
| 1081 | if (mGeoMesh->mSubMesh[i].mSharedVertexBuffer) |
---|
| 1082 | { |
---|
| 1083 | // Shared vertices. |
---|
| 1084 | if (!copiedShared) |
---|
| 1085 | { |
---|
| 1086 | mGeoMesh->mVertexBuffer = new VertexBuffer(); |
---|
| 1087 | copiedShared = true; |
---|
| 1088 | } |
---|
| 1089 | |
---|
| 1090 | mGeoMesh->mSubMesh[i].mVertexBuffer = mGeoMesh->mVertexBuffer; |
---|
| 1091 | } |
---|
| 1092 | else |
---|
| 1093 | { |
---|
| 1094 | // There's no shared vertices. |
---|
| 1095 | mGeoMesh->mSubMesh[i].mVertexBuffer = new VertexBuffer(); |
---|
| 1096 | } |
---|
| 1097 | |
---|
| 1098 | mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount = unique_verts_inds_by_geo[i].size(); |
---|
| 1099 | mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh->mSubMesh[i].mVertexBuffer->mVertexInfo; |
---|
| 1100 | |
---|
| 1101 | // 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]; |
---|
| 1105 | |
---|
[986] | 1106 | for ( MAPAINDIND::iterator mapit = unique_verts_inds_by_geo[i].begin(); |
---|
| 1107 | mapit != unique_verts_inds_by_geo[i].end(); |
---|
| 1108 | mapit++) |
---|
[983] | 1109 | { |
---|
| 1110 | // Key and value. |
---|
| 1111 | int isrc = mapit->first; |
---|
| 1112 | int idst = mapit->second; |
---|
| 1113 | |
---|
| 1114 | // Vertex coordinate. |
---|
| 1115 | // 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; |
---|
| 1120 | |
---|
| 1121 | // 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; |
---|
| 1124 | |
---|
| 1125 | // Texture coordinates. |
---|
| 1126 | Vector2 v2((Real)M0.texcoord(isrc)(0), (Real)M0.texcoord(isrc)(1)); |
---|
| 1127 | mGeoMesh->mSubMesh[i].mVertexBuffer->mTexCoords[idst]=v2; |
---|
| 1128 | } |
---|
| 1129 | } |
---|
| 1130 | else |
---|
| 1131 | { |
---|
| 1132 | // Leaves mesh. |
---|
| 1133 | mGeoMesh->mSubMesh[i].mIndexCount=objmesh->mSubMesh[i].mIndexCount; |
---|
| 1134 | mGeoMesh->mSubMesh[i].mIndex=new Index[mGeoMesh->mSubMesh[i].mIndexCount]; |
---|
| 1135 | |
---|
| 1136 | // Copy the leaves submesh indexes. |
---|
[986] | 1137 | for (unsigned int j=0; j<mGeoMesh->mSubMesh[i].mIndexCount; j++) |
---|
[983] | 1138 | { |
---|
| 1139 | mGeoMesh->mSubMesh[i].mIndex[j]=objmesh->mSubMesh[i].mIndex[j]; |
---|
| 1140 | } |
---|
| 1141 | |
---|
| 1142 | // 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; |
---|
| 1146 | |
---|
| 1147 | // 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]; |
---|
[986] | 1151 | |
---|
[983] | 1152 | for ( unsigned int j = 0; |
---|
[986] | 1153 | j < mGeoMesh->mSubMesh[i].mVertexBuffer->mVertexCount; |
---|
| 1154 | j++) |
---|
[983] | 1155 | { |
---|
| 1156 | // 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; |
---|
| 1160 | |
---|
| 1161 | // 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; |
---|
| 1165 | |
---|
| 1166 | // 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; |
---|
| 1169 | } |
---|
| 1170 | } |
---|
| 1171 | } |
---|
| 1172 | |
---|
| 1173 | delete[] unique_verts_inds_by_geo; |
---|
[984] | 1174 | delete[] ver_inds_rep_by_geo; |
---|
[983] | 1175 | delete[] inextvert; |
---|
| 1176 | |
---|
| 1177 | // Store the simplification steps in MeshSimplificationSequence. |
---|
| 1178 | int acum = 0; |
---|
[986] | 1179 | |
---|
[983] | 1180 | for (size_t i = 0; i < objmesh->mSubMeshCount; i++) |
---|
| 1181 | { |
---|
| 1182 | if (objmesh->mSubMesh[i].mSharedVertexBuffer) |
---|
| 1183 | { |
---|
| 1184 | first_index_submesh[i] = 0; |
---|
| 1185 | } |
---|
| 1186 | else |
---|
| 1187 | { |
---|
| 1188 | first_index_submesh[i] = acum; |
---|
| 1189 | } |
---|
[986] | 1190 | |
---|
[983] | 1191 | acum += (int)objmesh->mSubMesh[i].mVertexBuffer->mVertexCount; |
---|
| 1192 | } |
---|
[986] | 1193 | |
---|
[983] | 1194 | MeshSimplificationSequence::Step stepaux, newstep; |
---|
| 1195 | vector<int> v0,v1,submesh0,submesh1; |
---|
| 1196 | MeshSimplificationSequence *msimpseq; |
---|
[986] | 1197 | |
---|
[983] | 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]; |
---|
[986] | 1207 | |
---|
[983] | 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; |
---|
[986] | 1219 | |
---|
[983] | 1220 | // mT0 y mT1 are unique triangles identifiers |
---|
| 1221 | // returned by the simplification method |
---|
| 1222 | newstep.mT0 = stepaux.mT0; |
---|
| 1223 | newstep.mT1 = stepaux.mT1; |
---|
[986] | 1224 | |
---|
[983] | 1225 | if (j == v0.size() - 1 && k == v1.size() - 1) |
---|
| 1226 | { |
---|
| 1227 | newstep.obligatorio = 0; |
---|
| 1228 | } |
---|
| 1229 | else |
---|
| 1230 | { |
---|
| 1231 | newstep.obligatorio = 1; |
---|
| 1232 | } |
---|
[986] | 1233 | |
---|
[983] | 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; |
---|
[986] | 1244 | |
---|
[983] | 1245 | newstep.mModfaces = vvacio; |
---|
| 1246 | } |
---|
| 1247 | |
---|
| 1248 | msimpseq->mSteps.push_back(newstep); |
---|
| 1249 | } |
---|
| 1250 | } |
---|
| 1251 | } |
---|
| 1252 | |
---|
| 1253 | // WriteOBJ(); |
---|
| 1254 | return msimpseq; |
---|
| 1255 | } |
---|
| 1256 | |
---|
| 1257 | //--------------------------------------------------------------------------- |
---|
| 1258 | // Set submesh leaves. |
---|
| 1259 | //--------------------------------------------------------------------------- |
---|
| 1260 | void SimplificationMethod::setMeshLeaves(int meshLeaves) |
---|
| 1261 | { |
---|
| 1262 | indexMeshLeaves = meshLeaves; |
---|
| 1263 | } |
---|
| 1264 | |
---|
| 1265 | // Gets mesh simplified. |
---|
| 1266 | Mesh * SimplificationMethod::GetMesh() |
---|
| 1267 | { |
---|
| 1268 | return mGeoMesh; |
---|
| 1269 | } |
---|
| 1270 | |
---|