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
|
/****************************************************************************
* 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. *
* *
****************************************************************************/
#ifndef _BITQUAD_OPTIMIZATION
#define _BITQUAD_OPTIMIZATION
namespace vcg{namespace tri{
template <class BQ>
class BitQuadOptimization{
typedef typename BQ::MeshType MeshType;
typedef typename BQ::Pos Pos;
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 BitQuad<MeshType> BQ; // static class to make basic quad operatins
public:
// helper function: mark a quadface, setting Q at 0, and neight at .75, 0.5...
static void MarkFace(FaceType* f, MeshType &m){
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
fi->Q() = 1;
}
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) f->FFp(i)->FFp(j)->Q() = 0.75;
}
for (int i=0; i<3; i++) {
f->FFp(i)->Q() = 0.50;
}
f->Q() = 0;
}
// helper function: mark a quadface, setting Q at 0, and neight at .75, 0.5...
static void MarkVertex(FaceType* f, int wedge, MeshType &m){
VertexType *v = f->V(wedge);
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (fi->V0(0)==v || fi->V1(0)==v ||fi->V2(0)==v ) fi->Q() = 0;
// else fi->Q() = 1;
}
}
static bool MarkSmallestEdge(MeshType &m, bool perform)
{
ScalarType min = std::numeric_limits<ScalarType>::max();
FaceType *fa=NULL; int w=0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD())
for (int k=0; k<3; k++) {
FaceType *f=&*fi;
if (f->IsF(k)) continue;
if (f->FFp(k) == f ) continue; // skip borders
ScalarType score;
score = (f->P0(k) - f->P1(k)).Norm();
if (score<min) {
min=score;
fa = f;
w = k;
}
}
if (fa) {
if (perform) {
return BQ::CollapseEdge(*fa,w,m);
} else {
fa->Q()=0.0;
fa->FFp(w)->Q()=0.0;
return true;
}
}
return false;
}
static ScalarType Importance(const CoordType &/*p*/){
//return ::proceduralImportance(p);
return 1;
}
// returns: 0 if fail. 1 if edge. 2 if diag.
static int MarkSmallestEdgeOrDiag(MeshType &m, ScalarType edgeMult, bool perform, Pos* affected=NULL)
{
ScalarType min = std::numeric_limits<ScalarType>::max();
FaceType *fa=NULL; int w=0; bool counterDiag = false;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD())
for (int k=0; k<3; k++) {
FaceType *f=&*fi;
if (f->FFp(k) >= f ) continue; // skip borders (==), and do it one per edge
ScalarType score;
score = (f->P0(k) - f->P1(k)).Norm();
ScalarType imp = Importance( (f->P0(k) + f->P1(k))/2 );
score /= imp;
if (!f->IsF(k)) score*=edgeMult; // edges are supposed to be smaller!
if (score<min) {
min=score;
fa = f;
w = k;
counterDiag=false;
}
if (f->IsF(k)) { // for diag faces, test counterdiag too
score = BQ::CounterDiag(f).Norm();
score /= imp;
if (score<min) {
min=score;
fa = f;
w = k;
counterDiag=true;
}
}
}
if (fa) {
if (perform) {
if (fa->IsF(w)) {
if (counterDiag) {
if (BQ::CollapseCounterDiag(*fa, BQ::PosOnDiag(*fa,true), m , affected)) return 2;
} else {
if (BQ::CollapseDiag(*fa, BQ::PosOnDiag(*fa,false), m ,affected)) return 2;
}
} else {
if (BQ::CollapseEdge(*fa,w,m, affected)) return 1;
}
} else {
fa->Q()=0.0;
fa->FFp(w)->Q()=0.0;
if (fa->IsF(w)) return 2; else return 1;
}
}
return 0;
}
static void MarkSmallestDiag(MeshType &m)
{
ScalarType min = std::numeric_limits<ScalarType>::max();
FaceType *fa=NULL;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
FaceType *f=&*fi;
ScalarType score;
score = BQ::Diag(f).Norm();
if (score<min) {
min=score;
fa = f;
}
score = BQ::CounterDiag(f).Norm();
if (score<min) {
min=score;
fa = f;
}
}
if (fa) {
fa->Q()=0.0;
fa->FFp(BQ::FauxIndex(fa))->Q()=0.0;
}
}
static bool IdentifyAndCollapseSmallestDiag(MeshType &m){
ScalarType min = std::numeric_limits<ScalarType>::max();
FaceType *fa=NULL; bool flip;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
FaceType *f=&*fi;
ScalarType score;
score = BQ::Diag(f).Norm();
if (score<min) {
min=score;
fa = f;
flip = false;
}
score = BQ::CounterDiag(f).Norm();
if (score<min) {
min=score;
fa = f;
flip = true;
}
}
if (!fa) return false;
if (BQ::TestAndRemoveDoublet(*fa,0,m)) { return true; }
if (BQ::TestAndRemoveDoublet(*fa,1,m)) { return true; }
if (BQ::TestAndRemoveDoublet(*fa,2,m)) { return true; }
int k = BQ::FauxIndex(fa);
if (BQ::TestAndRemoveDoublet( *fa->FFp(k),(fa->FFi(k)+2)%3, m )) return true;
if (flip) {
if (!BQ::CheckFlipDiag(*fa) ) {
// I can't collapse (why?)
MarkFace(fa,m);
return false;
} else
BQ::CollapseCounterDiag(*fa, BQ::PosOnDiag(*fa,true), m );
}
else {
BQ::CollapseDiag(*fa, BQ::PosOnDiag(*fa,false), m );
}
return true;
}
/*
seeks and removes all doublets (a pair of quads sharing two consecutive edges)
by merging them into a single quad (thus removing one vertex and two tri faces)-
Returns number of removed Doublets
*/
static int RemoveDoublets(MeshType &m, Pos *p=NULL)
{
int res=0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
fi->Q()=1;
for (int k=0; k<3; k++) {
if ( BQ::IsDoublet(*fi,k) ){
res++;
BQ::RemoveDoublet(*fi,k,m,p);
if (fi->IsD()) break; // break wedge circle, if face disappeard
if (p) return res;
}
}
}
return res;
}
/*
marks (Quality=0) and approx. counts profitable vertex rotations
(vertex rotations which make edge shorter
*/
template <bool perform>
static int MarkVertexRotations(MeshType &m, Pos *affected=NULL)
{
int res=0;
for (VertexIterator vi = m.vert.begin(); vi!=m.vert.end(); vi++) if (!vi->IsD()) vi->ClearV();
if (!perform)
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) fi->Q()=1.0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
for (int k=0; k<3; k++) {
if (fi->V(k)->IsV()) continue;
if (BQ::TestVertexRotation(*fi,k)) {
res++;
fi->V(k)->SetV();
if (!perform) {
res++; MarkVertex(&*fi, k, m); //fi->Q()=0;
}
else {
if (BQ::RotateVertex(*fi, k, m, affected)) res++; //fi->Q()=0;
if (affected) return res; // uncomment for only one rotation
}
}
}
}
return res;
}
// mark (and count) all edges that are worth rotating
// if perform == true, actually rotate them
template <bool perform>
static int MarkEdgeRotations(MeshType &m, Pos *p=NULL)
{
int count = 0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) fi->Q()=1;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
//if (count>0) break;
for (int k=0; k<3; k++) {
if (fi->IsF(k)) continue;
if (fi->FFp(k)<= &*fi) continue; // only once per real (non faux) edge, and only for non border ones
int best = BQ::TestEdgeRotation(*fi, k);
if (perform) {
if (best==+1) if (BQ::template RotateEdge< true>(*fi, k, m, p)) count++;
if (best==-1) if (BQ::template RotateEdge<false>(*fi, k, m, p)) count++;
if (p) if (count>0) return count;
}
else {
if (best!=0) { fi->Q()=0; fi->FFp(k)->Q()=0; count++; }
}
}
}
return count;
}
/*
marks (Quality=0) and approx. counts doublets (a pair of quads sharing two consecutive edges)
*/
static int MarkDoublets(MeshType &m)
{
int res=0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
fi->Q()=1;
for (int k=0; k<3; k++) {
if ( BQ::IsDoublet(*fi,k) ){
res++;
if (fi->IsF((k+1)%3)) res++; // counts for a quad
fi->Q()=0;
}
}
}
assert (res%2==0);
return res/4; // return doublet pairs (approx, as a quad could be a part of many pairs)
}
/*
marks (Quality=0) and counts singlets (vertex B in an A-B-A-C quad)
*/
static int MarkSinglets(MeshType &m)
{
int res=0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
fi->Q()=1;
for (int k=0; k<3; k++) {
if ( BQ::IsSinglet(*fi,k) ){
res++;
fi->Q()=0;
}
}
}
assert (res%2==0);
return res/2; // return number of singlet pairs
}
/*
deletes singlets, reutrns number of
*/
static int RemoveSinglets(MeshType &m, Pos *p=NULL)
{
int res=0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
for (int k=0; k<3; k++) {
if ( BQ::IsSinglet(*fi,k) ){
res++;
BQ::RemoveSinglet(*fi,k,m, p);
if (p) return res;
break;
}
}
}
return res; // return singlet pairs (approx, as a quad could be a part of many pairs)
}
/* returns average quad quality, and assigns it to triangle quality
*/
static ScalarType MeasureQuality(MeshType &m)
{
assert(MeshType::HasPerFaceFlags());
ScalarType res = 0;
int div = 0;
for (FaceIterator fi = m.face.begin(); fi!=m.face.end(); fi++) if (!fi->IsD()) {
if (fi->IsAnyF()) {
ScalarType q = BQ::quadQuality( &*fi, BQ::FauxIndex(&*fi) );
if (MeshType::HasPerFaceQuality()) fi->Q() = q;
res += q;
div++;
}
}
if (!div) return 0; else return res / div;
}
};
}} // end namespace vcg::tri
#endif
|