1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#include <vcg/complex/algorithms/bitquad_support.h>
/** BIT-QUAD creation support:
a collection of methods that,
starting from a triangular mesh, will create your quad-pure or quad-domainant mesh.
They all require:
- per face Q, and FF connectivity, 2-manyfold meshes,
- and tri- or quad- meshes (no penta, etc) (if in need, use MakeBitTriOnly)
[ list of available methods: ]
void MakePureByRefine(Mesh &m)
- adds a vertex for each tri or quad present
- thus, miminal complexity increase is the mesh is quad-dominant already
- old non-border edges are made faux
- never fails
void MakePureByCatmullClark(MeshType &m)
- adds a vertex in each (non-faux) edge.
- twice complexity increase w.r.t. "ByRefine" method.
- preserves edges: old edges are still edges
- never fails
bool MakePureByFlip(MeshType &m [, int maxdist] )
- does not increase # vertices, just flips edges
- call in a loop until it returns true (temporary hack)
- fails if number of triangle is odd (only happens in open meshes)
- add "StepByStep" to method name if you want it to make a single step (debugging purposes)
bool MakeTriEvenBySplit(MeshType& m)
bool MakeTriEvenByDelete(MeshType& m)
- two simple variants that either delete or split *at most one* border face
so that the number of tris will be made even. Return true if it did it.
- useful to use the previous method, when mesh is still all triangle
void MakeDominant(MeshType &m, int level)
- just merges traingle pairs into quads, trying its best
- various heuristic available, see descr. for parameter "level"
- provides good starting point for make-Quad-Only methods
- uses an ad-hoc measure for "quad quality" (which is hard-wired, for now)
void MakeBitTriOnly(MeshType &m)
- inverse process: returns to tri-only mesh
int SplitNonFlatQuads(MeshType &m, ScalarType toleranceDeg=0){
- as above, but splits only non flat quads
TESTING METHODS:
bool IsTriOnly(const MeshType &m); // only triangles
bool IsQuadOnly(const MeshType &m); // only quads
bool IsTriQuadOnly(const MeshType &m); // only quads or triangles
(more info in comments before each method)
*/
#ifndef VCG_BITQUAD_CRE
#define VCG_BITQUAD_CRE
namespace vcg{namespace tri{
template <class _MeshType,
class Interpolator = GeometricInterpolator<typename _MeshType::VertexType> >
class BitQuadCreation{
public:
typedef _MeshType MeshType;
typedef typename MeshType::ScalarType ScalarType;
typedef typename MeshType::CoordType CoordType;
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FaceType* FaceTypeP;
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::FaceIterator FaceIterator;
typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::ConstFaceIterator ConstFaceIterator;
typedef BitQuad<MeshType> BQ; // static class to make basic quad operations
// helper function:
// given a triangle, merge it with its best neightboord to form a quad
template <bool override>
static void selectBestDiag(FaceType *fi){
if (!override) {
if (fi->IsAnyF()) return;
}
// select which edge to make faux (if any)...
int whichEdge = -1;
ScalarType bestScore = fi->Q();
whichEdge=-1;
for (int k=0; k<3; k++){
// todo: check creases? (continue if edge k is a crease)
if (!override) {
if (fi->FFp(k)->IsAnyF()) continue;
}
if (fi->FFp(k)==fi) continue; // never make a border faux
ScalarType score = BQ::quadQuality( &*fi, k );
if (override) {
// don't override anyway iff other face has a better match
if (score < fi->FFp(k)->Q()) continue;
}
if (score>bestScore) {
bestScore = score;
whichEdge = k;
}
}
// ...and make it faux
if (whichEdge>=0) {
//if (override && fi->FFp(whichEdge)->IsAnyF()) {
// new score is the average of both scores
// fi->Q() = fi->FFp(whichEdge)->Q() = ( bestScore + fi->FFp(whichEdge)->Q() ) /2;
//} else {
//}
if (override) {
// clear any faux edge of the other face
for (int k=0; k<3; k++)
if (fi->FFp(whichEdge)->IsF(k)) {
fi->FFp(whichEdge)->ClearF(k);
fi->FFp(whichEdge)->FFp(k)->ClearF( fi->FFp(whichEdge)->FFi(k) );
fi->FFp(whichEdge)->FFp(k)->Q()=0.0; // other face's ex-buddy is now single and sad :(
}
// clear all faux edges of this face...
for (int k=0; k<3; k++)
if (fi->IsF(k)) {
fi->ClearF(k);
fi->FFp(k)->ClearF( fi->FFi(k) );
fi->FFp(k)->Q()= 0.0; // my ex-buddy is now sad
}
}
// set (new?) quad
fi->SetF(whichEdge);
fi->FFp(whichEdge)->SetF( fi->FFi(whichEdge) );
fi->Q() = fi->FFp(whichEdge)->Q() = bestScore;
}
}
// helper funcion:
// a pass though all triangles to merge triangle pairs into quads
template <bool override> // override previous decisions?
static void MakeDominantPass(MeshType &m){
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
selectBestDiag<override>(&(*fi));
}
}
/**
* This function split a face along the specified border edge it does not compute any property of the new vertex. It only do the topological work.
* @param edge Index of the edge
*/
// sideF
// sideF V2(e) ------------- v2
// V0 -------------V2 V2(e) \ /
// | / | \ \ newF /
// | / | \ \ / e
// | f / | \ \ /
// | / e | f V1(e)=newV =
// | / | /
// | / | /
// | / | /
// V1 V0(e)
//
static std::pair<typename MeshType::FaceType *, typename MeshType::VertexType *> FaceSplitBorderEdge(MeshType &m, typename MeshType::FaceType &f, int edge, typename MeshType::FaceType *newFace, typename MeshType::VertexType *newVert )
{
typename MeshType::FaceType *sideFFp;
int sideFFi;
assert(tri::HasFFAdjacency(m));
assert(face::IsBorder(f,edge));
//qDebug("OldFacePRE %i %i %i",tri::Index(m,f.V(0)),tri::Index(m,f.V(1)),tri::Index(m,f.V(2)));
if(newFace==0) newFace=&*tri::Allocator<MeshType>::AddFaces(m,1);
if(newVert==0) {
newVert=&*tri::Allocator<MeshType>::AddVertices(m,1);
newVert->P()=(f.P0(edge)+f.P1(edge))/2.0;
}
newFace->V0(edge)=newVert;
newFace->V1(edge)=f.V1(edge);
newFace->V2(edge)=f.V2(edge);
f.V1(edge)=newVert;
//qDebug("NewFace %i %i %i",tri::Index(m,newFace->V(0)),tri::Index(m,newFace->V(1)),tri::Index(m,newFace->V(2)));
//qDebug("OldFace %i %i %i",tri::Index(m,f.V(0)),tri::Index(m,f.V(1)),tri::Index(m,f.V(2)));
// Topology
newFace->FFp((edge+2)%3) = &f;
newFace->FFi((edge+2)%3) = (edge+1)%3;
newFace->FFp((edge+0)%3) = newFace;
newFace->FFi((edge+0)%3) = (edge+0)%3;
newFace->FFp((edge+1)%3) = f.FFp((edge+1)%3);
newFace->FFi((edge+1)%3) = f.FFi((edge+1)%3);
sideFFp = f.FFp((edge+1)%3);
sideFFi = f.FFi((edge+1)%3);
f.FFp((edge+1)%3) = newFace;
f.FFi((edge+1)%3) = (edge+2)%3;
sideFFp->FFp(sideFFi)=newFace;
sideFFp->FFi(sideFFi)=(edge+1)%3;
assert(face::IsBorder(f,edge));
assert(face::IsBorder(*newFace,edge));
return std::make_pair(newFace,newVert);
}
// make tri count even by splitting a single triangle...
//
// V0 -------V2 V0 --------V2
// | / | \ Fnew /
// | / | Vnew
// | / | /
// | / | /
// V1 V1
//
static bool MakeTriEvenBySplit(MeshType& m){
if (m.fn%2==0) return false; // it's already Even
// Search for a triangle on the border
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++)
{
if(!(*fi).IsD())
{
for (int k=0; k<3; k++) {
if (face::IsBorder(*fi,k)){
// We have found a face with a border
int index=tri::Index(m,*fi);
VertexIterator vnew=tri::Allocator<MeshType>::AddVertices(m,1);
(*vnew).P()=((*fi).P0(k)+(*fi).P1(k))/2.0;
FaceIterator fnew=tri::Allocator<MeshType>::AddFaces(m,1);
FaceSplitBorderEdge(m,m.face[index],k,&*fnew,&*vnew);
return true;
}
}
}
}
return true;
}
// make tri count even by delete...
static bool MakeTriEvenByDelete(MeshType& m)
{
if (m.fn%2==0) return false; // it's already Even
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) {
for (int k=0; k<3; k++) {
if (face::IsBorder(*fi,k) ) {
FFDetachManifold(*fi,(k+1)%3);
FFDetachManifold(*fi,(k+2)%3);
Allocator<MeshType>::DeleteFace(m,*fi);
return true;
}
}
}
assert(0); // no border face found? then how could the number of tri be Odd?
return true;
}
/*
Splits any quad that makes an angle steeper than given degrees
*/
static int SplitNonFlatQuads(MeshType &m, ScalarType deg=0){
int res=0;
float th = math::Cos(math::ToRad(deg));
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (fi->IsAnyF()) {
int faux = BQ::FauxIndex(&*fi);
FaceType *fb = fi->FFp(faux);
if (fb->N()*fi->N()<th) {
fi->ClearF(faux);
fb->ClearF(fi->FFi(faux));
res++;
}
}
}
return res;
}
/**
Given a mesh, makes it bit trianglular (makes all edges NOT faux)
*/
static void MakeBitTriOnly(MeshType &m){
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) {
fi->ClearAllF();
}
}
/** given a quad-and-tree mesh, enforces the "faux edge is 2nd edge" convention.
* Requires (and updates): FV and FF structure
* Updates: faux flags
* Updates: per wedge attributes, if any
* Other connectivity structures, and per edge and per wedge flags are ignored
*/
static bool MakeBitTriQuadConventional(MeshType &/*m*/){
assert(0); // todo
return false;
}
/* returns true if mesh is a "conventional" quad mesh.
I.e. if it is all quads, with third edge faux for all triangles*/
static bool IsBitTriQuadConventional(const MeshType &m){
for (ConstFaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (fi->IsAnyF())
if ( (fi->Flags() & FaceType::FAUX012 ) != FaceType::FAUX2 ) {
return false;
}
}
return true;
}
/* returns true if mesh is a pure tri-mesh. (no faux edges) */
static bool IsTriOnly(const MeshType &m){
for (ConstFaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (fi->IsAnyF()) return false;
}
return true;
}
/* returns true if mesh is a pure quad-mesh. */
static bool IsQuadOnly(const MeshType &m){
for (ConstFaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
int count = 0;
if (fi->IsF(0)) count++;
if (fi->IsF(1)) count++;
if (fi->IsF(2)) count++;
if (count!=1) return false;
}
return true;
}
/* returns true if mesh has only tris and quads (no penta etc) */
static bool IsTriQuadOnly(const MeshType &m){
for (ConstFaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
int count = 0;
if (fi->IsF(0)) count++;
if (fi->IsF(1)) count++;
if (fi->IsF(2)) count++;
if (count>1) return false;
}
return true;
}
static void CopyTopology(FaceType *fnew, FaceType * fold)
{
fnew->FFp(0)=fold->FFp(0); fnew->FFi(0)=fold->FFi(0);
fnew->FFp(1)=fold->FFp(1); fnew->FFi(1)=fold->FFi(1);
fnew->FFp(2)=fold->FFp(2); fnew->FFi(2)=fold->FFi(2);
fnew->V(0) = fold->V(0);
fnew->V(1) = fold->V(1);
fnew->V(2) = fold->V(2);
}
/**
makes any mesh quad only by refining it so that a quad is created over all
previous diags
requires that the mesh is made only of quads and tris.
*/
static void MakePureByRefine(MeshType &m){
// todo: update VF connectivity if present
int ev = 0; // EXTRA vertices (times 2)
int ef = 0; // EXTRA faces
// first pass: count triangles to be added
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
int k=0;
if (face::IsBorder(*fi,0)) k++;
if (face::IsBorder(*fi,1)) k++;
if (face::IsBorder(*fi,2)) k++;
if (!fi->IsAnyF()) {
// it's a triangle
if (k==0) // add a vertex in the center of the face, splitting it in 3
{ ev+=2; ef+=2; }
if (k==1) // add a vertex in the border edge, splitting it in 2
{ }
if (k==2) // do nothing, just mark the non border edge as faux
{ }
if (k==3) // disconnected single triangle (all borders): make one edge as faus
{ }
}
else {
// assuming is a quad (not a penta, etc), i.e. only one faux
// add a vertex in the center of the faux edge, splitting the face in 2
ev+=1; ef+=1;
assert(k!=3);
}
}
assert(ev%2==0); // should be even by now
ev/=2; // I was counting each of them twice
//int originalFaceNum = m.fn;
FaceIterator nfi = tri::Allocator<MeshType>::AddFaces(m,ef);
VertexIterator nvi = tri::Allocator<MeshType>::AddVertices(m,ev);
tri::UpdateFlags<MeshType>::FaceClearV(m);
// second pass: add faces and vertices
int nsplit=0; // spits to be done on border in the third pass
for (FaceIterator fi = m.face.begin(), fend = nfi; fi!=fend; fi++) if (!fi->IsD() && !fi->IsV() ) {
fi->SetV();
if (!fi->IsAnyF()) {
// it's a triangle
int k=0; // number of borders
if (face::IsBorder(*fi,0)) k++;
if (face::IsBorder(*fi,1)) k++;
if (face::IsBorder(*fi,2)) k++;
if (k==0) // add a vertex in the center of the face, splitting it in 3
{
assert(nvi!=m.vert.end());
VertexType *nv = &*nvi; nvi++;
//*nv = *fi->V0( 0 ); // lazy: copy everything from the old vertex
nv->ImportData(*(fi->V0( 0 ))); // lazy: copy everything from the old vertex
nv->P() = ( fi->V(0)->P() + fi->V(1)->P() + fi->V(2)->P() ) /3.0;
FaceType *fa = &*fi;
FaceType *fb = &*nfi; nfi++;
FaceType *fc = &*nfi; nfi++;
fb->ImportData(*fa); CopyTopology(fb,fa);
fc->ImportData(*fa); CopyTopology(fc,fa);
fa->V(0) = nv;
fb->V(1) = nv;
fc->V(2) = nv;
fb->FFp(2)=fa->FFp(2); fb->FFi(2)=fa->FFi(2);
fc->FFp(0)=fa->FFp(0); fc->FFi(0)=fa->FFi(0);
assert( fa->FFp(1)->FFp(fa->FFi(1)) == fa );
/* */fb->FFp(2)->FFp(fb->FFi(2)) = fb;
/* */fc->FFp(0)->FFp(fc->FFi(0)) = fc;
fa->FFp(0) = fc; fa->FFp(2) = fb; fa->FFi(0) = fa->FFi(2) = 1;
fb->FFp(1) = fa; fb->FFp(0) = fc; fb->FFi(0) = fb->FFi(1) = 2;
fc->FFp(1) = fa; fc->FFp(2) = fb; fc->FFi(1) = fc->FFi(2) = 0;
if (fb->FFp(2)==fa) fb->FFp(2)=fb; // recover border status
if (fc->FFp(0)==fa) fc->FFp(0)=fc;
fa->ClearAllF();
fb->ClearAllF();
fc->ClearAllF();
fa->SetF(1);
fb->SetF(2);
fc->SetF(0);
fa->SetV();fb->SetV();fc->SetV();
}
if (k==1) { // make a border face faux, anf other two as well
fi->SetF(0);
fi->SetF(1);
fi->SetF(2);
nsplit++;
}
if (k==2) // do nothing, just mark the non border edge as faux
{
fi->ClearAllF();
for (int w=0; w<3; w++) if (fi->FFp(w) != &*fi) fi->SetF(w);
}
if (k==3) // disconnected single triangle (all borders): use catmull-clark (tree vertices, split it in 6
{
fi->ClearAllF();
fi->SetF(2);
nsplit++;
}
}
else {
// assuming is a part of quad (not a penta, etc), i.e. only one faux
FaceType *fa = &*fi;
int ea2 = BQ::FauxIndex(fa); // index of the only faux edge
FaceType *fb = fa->FFp(ea2);
int eb2 = fa->FFi(ea2);
assert(fb->FFp(eb2)==fa) ;
assert(fa->IsF(ea2));
//assert(fb->IsF(eb2)); // reciprocal faux edge
int ea0 = (ea2+1) %3;
int ea1 = (ea2+2) %3;
int eb0 = (eb2+1) %3;
int eb1 = (eb2+2) %3;
// create new vert in center of faux edge
assert(nvi!=m.vert.end());
VertexType *nv = &*nvi; nvi++;
// *nv = * fa->V0( ea2 );
nv->ImportData(*(fa->V0( ea2 ) )); // lazy: copy everything from the old vertex
//nv->P() = ( fa->V(ea2)->P() + fa->V(ea0)->P() ) /2.0;
Interpolator::Apply(*(fa->V(ea2)),*(fa->V(ea0)),0.5,*nv);
// split faces: add 2 faces (one per side)
assert(nfi!=m.face.end());
FaceType *fc = &*nfi; nfi++;
assert(nfi!=m.face.end());
FaceType *fd = &*nfi; nfi++;
fc->ImportData(*fa ); CopyTopology(fc,fa); // lazy: copy everything from the old vertex
fd->ImportData(*fb ); CopyTopology(fd,fb);// lazy: copy everything from the old vertex
fa->V(ea2) = fc->V(ea0) =
fb->V(eb2) = fd->V(eb0) = nv ;
fa->FFp(ea1)->FFp( fa->FFi(ea1) ) = fc;
fb->FFp(eb1)->FFp( fb->FFi(eb1) ) = fd;
fa->FFp(ea1) = fc ; fa->FFp(ea2) = fd;
fa->FFi(ea1) = ea0; fa->FFi(ea2) = eb2;
fb->FFp(eb1) = fd ; fb->FFp(eb2) = fc;
fb->FFi(eb1) = eb0; fb->FFi(eb2) = ea2;
fc->FFp(ea0) = fa ; fc->FFp(ea2) = fb;
fc->FFi(ea0) = ea1; fc->FFi(ea2) = eb2;
fd->FFp(eb0) = fb ; fd->FFp(eb2) = fa;
fd->FFi(eb0) = eb1; fd->FFi(eb2) = ea2;
// detect boundaries
bool ba = fa->FFp(ea0)==fa;
bool bc = fc->FFp(ea1)==fa;
bool bb = fb->FFp(eb0)==fb;
bool bd = fd->FFp(eb1)==fb;
if (bc) fc->FFp(ea1)=fc; // repristinate boundary status
if (bd) fd->FFp(eb1)=fd; // of new faces
fa->SetV();
fb->SetV();
fc->SetV();
fd->SetV();
fa->ClearAllF();
fb->ClearAllF();
fc->ClearAllF();
fd->ClearAllF();
fa->SetF( ea0 );
fb->SetF( eb0 );
fc->SetF( ea1 );
fd->SetF( eb1 );
// fix faux mesh boundary... if two any consecutive, merge it in a quad
if (ba&&bc) {
fa->ClearAllF(); fa->SetF(ea1);
fc->ClearAllF(); fc->SetF(ea0);
ba = bc = false;
}
if (bc&&bb) {
fc->ClearAllF(); fc->SetF(ea2);
fb->ClearAllF(); fb->SetF(eb2);
bc = bb = false;
}
if (bb&&bd) {
fb->ClearAllF(); fb->SetF(eb1);
fd->ClearAllF(); fd->SetF(eb0);
bb = bd = false;
}
if (bd&&ba) {
fd->ClearAllF(); fd->SetF(eb2);
fa->ClearAllF(); fa->SetF(ea2);
bd = ba = false;
}
// remaninig boudaries will be fixed by splitting in the last pass
if (ba) nsplit++;
if (bb) nsplit++;
if (bc) nsplit++;
if (bd) nsplit++;
}
}
assert(nfi==m.face.end());
assert(nvi==m.vert.end());
// now and there are no tris left, but there can be faces with ONE edge border & faux ()
// last pass: add vertex on faux border faces... (if any)
if (nsplit>0) {
FaceIterator nfi = tri::Allocator<MeshType>::AddFaces(m,nsplit);
VertexIterator nvi = tri::Allocator<MeshType>::AddVertices(m,nsplit);
for (FaceIterator fi = m.face.begin(), fend = nfi; fi!=fend; fi++) if (!fi->IsD()) {
FaceType* fa = &*fi;
int ea2 = -1; // border and faux face (if any)
if (fa->FFp(0)==fa && fa->IsF(0) ) ea2=0;
if (fa->FFp(1)==fa && fa->IsF(1) ) ea2=1;
if (fa->FFp(2)==fa && fa->IsF(2) ) ea2=2;
if (ea2 != -1) { // ea2 edge is naughty (border AND faux)
int ea0 = (ea2+1) %3;
int ea1 = (ea2+2) %3;
// create new vert in center of faux edge
VertexType *nv = &*nvi; nvi++;
//*nv = * fa->V0( ea2 );
nv->ImportData(*(fa->V0( ea2 ) )); // lazy: copy everything from the old vertex
nv->P() = ( fa->V(ea2)->P() + fa->V(ea0)->P() ) /2.0;
Interpolator::Apply(*(fa->V(ea2)),*(fa->V(ea0)),0.5,*nv);
// split face: add 1 face
FaceType *fc = &*nfi; nfi++;
fc->ImportData(*fa);CopyTopology(fc,fa); // lazy: copy everything from the old vertex
fa->V(ea2) = fc->V(ea0) = nv ;
fc->FFp(ea2) = fc;
fa->FFp(ea1)->FFp( fa->FFi(ea1) ) = fc;
fa->FFp(ea1) = fc ;
fa->FFi(ea1) = ea0;
fc->FFp(ea0) = fa ; fc->FFp(ea2) = fc;
fc->FFi(ea0) = ea1;
if (fc->FFp(ea1)==fa) fc->FFp(ea1)=fc; // recover border status
assert(fa->IsF(ea0) == fa->IsF(ea1) );
bool b = fa->IsF(ea1);
fa->ClearAllF();
fc->ClearAllF();
if (b) {
fa->SetF( ea0 );
fc->SetF( ea1 );
} else {
fa->SetF( ea1 );
fc->SetF( ea0 );
}
}
}
}
}
// uses Catmull Clark to enforce quad only meshes
// each old edge (but not faux) is split in two.
static void MakePureByCatmullClark(MeshType &m){
MakePureByRefine(m);
MakePureByRefine(m);
// done
}
// Helper funcion:
// marks edge distance froma a given face.
// Stops at maxDist or at the distance when a triangle is found
static FaceType * MarkEdgeDistance(MeshType &m, FaceType *startF, int maxDist){
assert(tri::HasPerFaceQuality(m));
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
fi->Q()=maxDist;
}
FaceType * firstTriangleFound = NULL;
startF->Q() = 0;
std::vector<FaceType*> stack;
int stackPos=0;
stack.push_back(startF);
while ( stackPos<int(stack.size())) {
FaceType *f = stack[stackPos++];
for (int k=0; k<3; k++) {
assert(FFCorrectness(*f,k));
FaceType *fk = f->FFp(k);
int fq = int(f->Q()) + ( ! f->IsF(k) );
if (fk->Q()> fq && fq <= maxDist) {
if (!fk->IsAnyF()) { firstTriangleFound = fk; maxDist = fq;}
fk->Q() = fq;
stack.push_back(fk);
}
}
}
return firstTriangleFound;
}
/*
given a tri-quad mesh,
uses edge rotates to make a tri move toward another tri and to merges them into a quad.
Retunrs number of surviving triangles (0, or 1), or -1 if not done yet.
StepbyStep: makes just one step!
use it in a loop as long as it returns 0 or 1.
maxdist is the maximal edge distance where to look for a companion triangle
*/
static int MakePureByFlipStepByStep(MeshType &m, int maxdist=10000, int restart=false){
static FaceType *ta, *tb; // faces to be matched into a quad
static int step = 0; // hack
if (restart) { step=0; return false; }
if (step==0) {
// find a triangular face ta
ta = NULL;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (!fi->IsAnyF()) { ta=&*fi; break; }
}
if (!ta) return 0; // success: no triangle left (done?)
tb = MarkEdgeDistance(m,ta,maxdist);
if (!tb) return 1; // fail: no matching triagle found (increase maxdist?)
step=1;
} else {
int marriageEdge=-1;
bool done = false;
while (!done) {
int bestScore = int(tb->Q());
int edge = -1;
bool mustDoFlip;
// select which edge to use
for (int k=0; k<3; k++) {
if (tb->FFp(k) == tb) continue; // border
FaceType* tbk = tb->FFp(k);
if (!tbk->IsAnyF()) {done=true; marriageEdge=k; break; } // found my match
int back = tb->FFi(k);
int faux = BQ::FauxIndex(tbk);
int other = 3-back-faux;
int scoreA = int(tbk->FFp(other)->Q());
FaceType* tbh = tbk->FFp(faux);
int fauxh = BQ::FauxIndex(tbh);
int scoreB = int(tbh->FFp( (fauxh+1)%3 )->Q());
int scoreC = int(tbh->FFp( (fauxh+2)%3 )->Q());
int scoreABC = std::min( scoreC, std::min( scoreA, scoreB ) );
if (scoreABC<bestScore) {
bestScore = scoreABC;
edge = k;
mustDoFlip = !(scoreB == scoreABC || scoreC == scoreABC);
}
}
if (done) break;
// use that edge to proceed
if (mustDoFlip) {
BQ::FlipDiag( *(tb->FFp(edge)) );
}
FaceType* next = tb->FFp(edge)->FFp( BQ::FauxIndex(tb->FFp(edge)) );
// create new edge
next->ClearAllF();
tb->FFp(edge)->ClearAllF();
// dissolve old edge
tb->SetF(edge);
tb->FFp(edge)->SetF( tb->FFi(edge) );
tb->FFp(edge)->Q() = tb->Q();
tb = next;
break;
}
if (marriageEdge!=-1) {
// consume the marriage (two tris = one quad)
assert(!(tb->IsAnyF()));
assert(!(tb->FFp(marriageEdge)->IsAnyF()));
tb->SetF(marriageEdge);
tb->FFp(marriageEdge)->SetF(tb->FFi(marriageEdge));
step=0;
}
}
return -1; // not done yet
}
/*
given a tri-quad mesh,
uses edge rotates to make a tri move toward another tri and to merges them into a quad.
- maxdist is the maximal edge distance where to look for a companion triangle
- retunrs true if all triangles are merged (always, unless they are odd, or maxdist not enough).
*/
static bool MakePureByFlip(MeshType &m, int maxdist=10000)
{
MakePureByFlipStepByStep(m, maxdist, true); // restart
int res=-1;
while (res==-1) res = MakePureByFlipStepByStep(m, maxdist);
return res==0;
}
/**
given a triangle mesh, makes it quad dominant by merging triangle pairs into quads
various euristics:
level = 0: maximally greedy. Leaves fewest triangles
level = 1: smarter: leaves more triangles, but makes better quality quads
level = 2: even more so (marginally)
*/
static void MakeDominant(MeshType &m, int level){
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) {
fi->ClearAllF();
fi->Q() = 0;
}
MakeDominantPass<false> (m);
if (level>0) MakeDominantPass<true> (m);
if (level>1) MakeDominantPass<true> (m);
if (level>0) MakeDominantPass<false> (m);
}
};
}} // end namespace vcg::tri
#endif
|