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
|
#ifndef VCG_PIVOT_H
#define VCG_PIVOT_H
#include <vector>
#include <list>
#include "vcg/space/index/grid_static_ptr.h"
#include "vcg/complex/algorithms/closest.h"
namespace vcg {
namespace tri {
struct Hinge {
int v0, v1, v2; //v0, v1 represent the Hinge, v2 the other vertex in the face
//this Hinge belongs to
int face; //corresponding face
Point3f center; //center of the sphere touching the face
int count; //test delay touch Hinges.
//the loops in the front are mantained as a double linked list
std::list<Hinge>::iterator next;
std::list<Hinge>::iterator previous;
Hinge() {}
Hinge(int _v0, int _v1, int _v2, int _face, Point3f &_center):
v0(_v0), v1(_v1), v2(_v2),
face(_face), center(_center), count(0) {
assert(v0 != v1 && v1 != v2 && v0 != v2);
}
};
template <class MESH>
class Pivot {
public:
// typedef CMesh MESH;
typedef GridStaticPtr<typename MESH::VertexType, typename MESH::ScalarType > StaticGrid;
float radius; //default 1 (not meaningful
float mindist; //minimum distance between points in the mesh (% of radius)
float crease; // -0.5
Box3f box;
MESH &mesh;
StaticGrid grid;
/* front Hinges of the mesh:
to expand the front we get the first Hinge
if an Hinge cannot create a new triangle it is marked dead and moved
to the end of the list
the new Hinges are inserted to the back (before dead_begin) */
std::list<Hinge> front;
std::list<Hinge> deads;
std::vector<int> nb; //number of fronts a vertex is into,
//this is used for the Visited and Border flags
//but adding topology may not be needed anymode
Pivot(MESH &_mesh, float _radius, float _mindist = 0.05, float _crease = -0.5):
mesh(_mesh), radius(_radius), mindist(_mindist), crease(_crease) {
//Compute bounding box. (this may be passed as a parameter?
for(int i = 0; i < mesh.vert.size(); i++)
box.Add(mesh.vert[i].P());
/* we need to enlarge the grid to allow queries from little outside of the box
Someone is a bit lazy... */
box.Offset(4*radius);
grid.Set(mesh.vert.begin(), mesh.vert.end(), box);
nb.clear();
nb.resize(mesh.vert.size(), 0);
for(int i = 0; i < mesh.vert.size(); i++)
mesh.vert[i].ClearFlags();
}
/* select a vertex at random, a small group of nearby vertices and looks
for a sphere that touches 3 and contains none.
Use the center of the box to get a sphere inside (or outside) the model
You may be unlucky... */
bool seed(bool outside = true, int start = -1) {
//pick a random point (well...)
if(start == -1) start = rand()%mesh.vert.size();
//get a sphere of neighbours
std::vector<int> targets;
std::vector<float> dists;
int n = getInSphere(mesh.vert[start].P(), 2*radius, targets, dists);
if(n < 3) {
//bad luck. we should call seed again (assuming random pick) up to
//some maximum tries. im lazy.
return false;
}
int v0, v1, v2;
bool found = false;
//find a triplet that does not contains any other point
Point3f center;
for(int i = 0; i < n; i++) {
v0 = targets[i];
CVertex &vv0 = mesh.vert[v0];
if(vv0.IsD() || vv0.IsB() || vv0.IsV()) continue;
Point3f &p0 = mesh.vert[v0].P();
Point3f out = (p0 - box.Center());
if(!outside) out = -out;
for(int k = i+1; k < n; k++) {
v1 = targets[k];
CVertex &vv1 = mesh.vert[v1];
if(vv1.IsD() || vv1.IsB() || vv1.IsV()) continue;
Point3f &p1 = mesh.vert[v1].P();
if((p1 - p0).Norm() < mindist*radius) continue;
for(int j = k+1; j < n; j++) {
v2 = targets[j];
CVertex &vv2 = mesh.vert[v2];
if(vv2.IsD() || vv2.IsB() || vv2.IsV()) continue;
Point3f &p2 = mesh.vert[v2].P();
if((p2 - p0).Norm() < mindist*radius) continue;
if((p2 - p1).Norm() < mindist*radius) continue;
Point3f normal = (p1 - p0)^(p2 - p0);
//check normal pointing inside
if(normal * out < 0) continue;
if(!findSphere(p0, p1, p2, center)) continue;
bool failed = false;
//check no other point inside
for(int t = 0; t < n; t++) {
Point3f &p = mesh.vert[targets[t]].P();
if((center - p).Norm() <= radius) {
failed = true;
break;
}
}
if(failed) continue;
found = true;
i = k = j = n;
}
}
}
if(!found) //see bad luck above
return false;
assert(!front.size());
//TODO: should i check the Hinges too?
addFace(v0, v1, v2);
//create the border of the first face
std::list<Hinge>::iterator e = front.end();
std::list<Hinge>::iterator last;
for(int i = 0; i < 3; i++) {
int v0 = (int)(mesh.face.back().V0(i));
int v1 = (int)(mesh.face.back().V1(i));
int v2 = (int)(mesh.face.back().V2(i));
nb[v0] = 1;
assert(!mesh.vert[v0].IsB());
mesh.vert[v0].SetB();
Hinge Hinge(v0, v1, v2, 0, center);
Hinge.previous = e;
e = front.insert(front.begin(), Hinge);
if(i == 0) last = e;
(*Hinge.previous).next = e;
cluster(v0);
}
//connect last and first
(*e).next = last;
(*last).previous = e;
return true;
}
/* expand the front adding 1 face. Return false on failure (id when
all Hinges are dead returns:
1: added a face
0: added nothing
-1: finished */
int addFace() {
//We try to seed again
if(!mesh.face.size()) {
for(int i = 0; i < 100; i++)
if(seed()) return 1;
return -1;
}
if(!front.size()) {
//maybe there are unconnected parts of the mesh:
//find a non D, V, B point and try to seed if failed D it.
for(int i = 0; i < mesh.vert.size();i ++) {
CVertex &v = mesh.vert[i];
if(v.IsD() || v.IsV() || v.IsB()) continue;
if(seed(true, i)) return 1;
v.SetD();
}
return -1;
}
std::list<Hinge>::iterator ei = front.begin();
Hinge &e = *ei;
Hinge &previous = *e.previous;
Hinge &next = *e.next;
int v0 = e.v0, v1 = e.v1;
//last triangle missing. or it is the first?
if(0 &&next.next == e.previous) {
int v[3] = { previous.v0, next.v0, e.v0 };
int c[3] = { 0, 0, 0 };
for(int k = 0; k < 3; k++) {
int vert = v[k];
nb[vert]--;
if(nb[vert] == 0) {
mesh.vert[vert].SetV();
mesh.vert[vert].ClearB();
}
}
assert(previous.previous == e.next);
addFace(previous.v0, next.v0, e.v0);
front.erase(e.previous);
front.erase(e.next);
front.erase(ei);
return 1;
}
int v2;
Point3f center;
std::vector<int> targets;
bool success = pivot(e, v2, center, targets);
//if no pivoting move this thing to the end and try again
//or we are trying to connect to the inside of the mesh. BAD.
if(!success || mesh.vert[v2].IsV()) {
killHinge(ei);
return 0;
}
//does v2 belongs to a front? (and which?)
std::list<Hinge>::iterator touch = touches(v2, ei);
assert(v2 != v0 && v2 != v1);
int fn = mesh.face.size();
if(touch != front.end()) {
if(!checkHinge(v0, v2) || !checkHinge(v2, v1)) {
killHinge(ei);
return 0;
}
if(v2 == previous.v0) {
/*touching previous Hinge (we reuse previous)
next
------->v2 -----> v1------>
\ /
\ /
previous \ / e
\ /
v0 */
detach(v0);
previous.v1 = v1;
previous.v2 = v0;
previous.face = fn;
previous.center = center;
previous.next = e.next;
next.previous = e.previous;
moveBack(e.previous);
//this checks if we can glue something to e.previous
trovamiunnome(e.previous);
front.erase(ei);
} else if(v2 == next.v1) {
/*touching previous Hinge (we reuse next)
previous
------->v0 -----> v2------>
\ /
\ /
\ / next
\ /
v1 */
detach(v1);
next.v0 = v0;
next.v2 = v1;
next.face = fn;
next.center = center;
next.previous = e.previous;
previous.next = e.next;
// moveBack(e.next);
//this checks if we can glue something to e.previous
trovamiunnome(e.next);
front.erase(ei);
} else {
/* this code would delay the joining Hinge to avoid bad situations not used but..
if(e.count < 2) {
e.count++;
moveBack(ei);
return true;
}*/
//touching some loop: split (or merge it is local does not matter.
//like this
/*
left right
<--------v2-<------
/|\
/ \
up / \ down
/ \
/ V
----v0 - - - > v1---------
e */
std::list<Hinge>::iterator left = touch;
std::list<Hinge>::iterator right = (*touch).previous;
std::list<Hinge>::iterator up = ei;
if(v1 == (*right).v0 || v0 == (*left).v1) {
// cout << "Bad join.\n";
killHinge(ei);
return 0;
}
nb[v2]++;
std::list<Hinge>::iterator down = newHinge(Hinge(v2, v1, v0, fn, center));
(*right).next = down;
(*down).previous = right;
(*down).next = e.next;
next.previous = down;
(*left).previous = up;
(*up).next = left;
(*up).v2 = v1;
(*up).v1 = v2;
(*up).face = fn;
(*up).center = center;
moveBack(ei);
}
} else {
assert(!mesh.vert[v2].IsB()); //fatal error! a new point is already a border?
/* adding a new vertex
v2
/|\
/ \
up / \ down
/ \
/ V
----v0 - - - > v1--------- */
cluster(v2);
nb[v2]++;
mesh.vert[v2].SetB();
std::list<Hinge>::iterator down = newHinge(Hinge(v2, v1, v0, fn, center));
(*down).previous = ei;
(*down).next = e.next;
next.previous = down;
e.v2 = v1;
e.v1 = v2;
e.face = fn;
e.center = center;
e.next = down;
moveBack(ei);
}
addFace(v0, v2, v1);
return 1;
}
/* return new vertex and the center of the new sphere pivoting from Hinge
if the vertex belongs to another Hinge, touch points to it. */
bool pivot(Hinge &Hinge, int &candidate, Point3f &end_pivot, std::vector<int> &targets) {
Point3f v0 = mesh.vert[Hinge.v0].P();
Point3f v1 = mesh.vert[Hinge.v1].P();
Point3f v2 = mesh.vert[Hinge.v2].P();
/* TODO why using the face normals everything goes wrong? should be
exactly the same................................................
Check if the e.face is correct.
Point3f &normal = mesh.face[Hinge.face].N();
*/
Point3f normal = ((v1 - v0)^(v2 - v0)).Normalize();
Point3f middle = (v0 + v1)/2;
Point3f start_pivot = Hinge.center - middle;
Point3f axis = (v1 - v0);
float axis_len = axis.SquaredNorm();
if(axis_len > 4*radius*radius) return false;
axis.Normalize();
// r is the radius of the thorus of all possible spheres passing throug v0 and v1
float r = sqrt(radius*radius - axis_len/4);
std::vector<float> dists;
getInSphere(middle, r + radius, targets, dists);
if(targets.size() == 0) return false; //this really would be strange but one never knows.
candidate = -1;
float minangle = 0;
Point3f center; //to be computed for each sample
for(int i = 0; i < targets.size(); i++) {
int id = targets[i];
if(id == Hinge.v0 || id == Hinge.v1 || id == Hinge.v2) continue;
if(mesh.vert[id].IsD()) {
continue;
}
Point3f p = mesh.vert[id].P();
/* Prevent 360 Hinges, also often reject ~ 50% points */
Point3f n = ((p - v0)^(v1 - v0)).Normalize();
if(n * normal < -0.5) {
continue;
}
/* Find the sphere through v0, p, v1 (store center on end_pivot */
if(!findSphere(v0, p, v1, center)) {
continue;
}
/* Angle between old center and new center */
float alpha = angle(start_pivot, center - middle, axis);
/* adding a small bias to already chosen vertices.
doesn't solve numerical problems, but helps. */
if(mesh.vert[id].IsB()) alpha -= 0.001;
/* Sometimes alpha might be little less then M_PI while it should be 0,
by numerical errors: happens for example pivoting
on the diagonal of a square. */
if(alpha > 2*M_PI - 0.8) {
// Angle between old center and new *point*
//TODO is this really overshooting? shouldbe enough to alpha -= 2*M_PI
Point3f proj = p - axis * (axis * p - axis * middle);
float beta = angle(start_pivot, proj - middle, axis);
if(alpha > beta) alpha -= 2*M_PI;
}
if(candidate == -1 || alpha < minangle) {
candidate = id;
minangle = alpha;
end_pivot = center;
}
}
//found no point suitable.
if(candidate == -1) {
return false;
}
assert(candidate != Hinge.v0 && candidate != Hinge.v1);
return true;
}
private:
//front management:
//Add a new Hinge to the back of the queue
std::list<Hinge>::iterator newHinge(Hinge e) {
return front.insert(front.end(), e);
}
//move an Hinge among the dead ones
void killHinge(std::list<Hinge>::iterator e) {
deads.splice(deads.end(), front, e);
}
//move an Hinge to the back of the queue
void moveBack(std::list<Hinge>::iterator e) {
front.splice(front.end(), front, e);
}
void moveFront(std::list<Hinge>::iterator e) {
front.splice(front.begin(), front, e);
}
bool checkHinge(int v0, int v1) {
int tot = 0;
//HACK to speed up things until i can use a seach structure
int i = mesh.face.size() - 2*(front.size());
// i = 0;
if(i < 0) i = 0;
for(; i < mesh.face.size(); i++) {
CFace &f = mesh.face[i];
for(int k = 0; k < 3; k++) {
if(v1== (int)f.V(k) && v0 == (int)f.V((k+1)%3)) ++tot;
else if(v0 == (int)f.V(k) && v1 == (int)f.V((k+1)%3)) { //orientation non constistent
return false;
}
}
if(tot >= 2) { //non manifold
return false;
}
}
return true;
}
void Pivot::cluster(int v) {
/* clean up too close points */
std::vector<int> targets;
std::vector<float> dists;
getInSphere(mesh.vert[v].P(), mindist*radius, targets, dists);
for(int i = 0; i < targets.size(); i++) {
int id = targets[i];
if(id == v) continue;
CVertex &v = mesh.vert[id];
if(v.IsD() || v.IsV() || v.IsB()) continue;
v.SetD();
}
}
void Pivot::trovamiunnome(std::list<Hinge>::iterator e) {
if(glue((*e).previous, e)) return;
glue(e, (*e).next);
}
//glue toghether a and b (where a.next = b
bool Pivot::glue(std::list<Hinge>::iterator a, std::list<Hinge>::iterator b) {
if((*a).v0 != (*b).v1) return false;
std::list<Hinge>::iterator previous = (*a).previous;
std::list<Hinge>::iterator next = (*b).next;
(*previous).next = next;
(*next).previous = previous;
detach((*a).v1);
detach((*a).v0);
front.erase(a);
front.erase(b);
return true;
}
void Pivot::detach(int v) {
assert(nb[v] > 0);
if(--nb[v] == 0) {
mesh.vert[v].SetV();
mesh.vert[v].ClearB();
}
}
/* compute angle from p to q, using axis for orientation */
float angle(Point3f p, Point3f q, Point3f &axis) {
p.Normalize();
q.Normalize();
Point3f vec = p^q;
float angle = acos(p*q);
if(vec*axis < 0) angle = -angle;
if(angle < 0) angle += 2*M_PI;
return angle;
}
/* add a new face. compute normals. */
void addFace(int a, int b, int c) {
CFace face;
face.V(0) = (CVertex *)a;
face.V(1) = (CVertex *)b;
face.V(2) = (CVertex *)c;
Point3f &p0 = mesh.vert[a].P();
Point3f &p1 = mesh.vert[b].P();
Point3f &p2 = mesh.vert[c].P();
face.N() = ((p1 - p0)^(p2 - p0)).Normalize();
mesh.face.push_back(face);
mesh.fn++;
}
/* intersects segment [v0, v1] with the sphere of radius radius. */
bool intersect(int v0, int v1, Point3f ¢er) {
Point3f m = mesh.vert[v1].P() - mesh.vert[v0].P();
float t = m*(center - mesh.vert[v0].P());
if(t < 0) return false;
if(t > m*m) return false;
return true;
}
float distance(int v0, int v1, Point3f ¢er) {
Point3f m = mesh.vert[v1].P() - mesh.vert[v0].P();
float t = m*(center - mesh.vert[v0].P())/(m*m);
Point3f p = mesh.vert[v0].P() + m*t;
return (p - center).Norm();
}
/* return all point in a given ball, notice as i want the index
of the vertices not the pointers... this may change in future */
unsigned int getInSphere(vcg::Point3f &p, float distance,
std::vector<int> &results,
std::vector<float> &dists) {
std::vector<CVertex *> ptr;
std::vector<Point3f> points;
int n = vcg::tri::GetInSphereVertex(mesh, grid, p, distance, ptr, dists, points);
for(int i = 0; i < ptr.size(); i++)
results.push_back(ptr[i] - &(mesh.vert[0]));
return n;
}
/* returns the sphere touching p0, p1, p2 of radius r such that
the normal of the face points toward the center of the sphere */
bool findSphere(Point3f &p0, Point3f &p1, Point3f &p2, Point3f ¢er) {
Point3f q1 = p1 - p0;
Point3f q2 = p2 - p0;
Point3f up = q1^q2;
float uplen = up.Norm();
//the three points are aligned
if(uplen < 0.001*q1.Norm()*q2.Norm()) return false;
up /= uplen;
float a11 = q1*q1;
float a12 = q1*q2;
float a22 = q2*q2;
float m = 4*(a11*a22 - a12*a12);
float l1 = 2*(a11*a22 - a22*a12)/m;
float l2 = 2*(a11*a22 - a12*a11)/m;
center = q1*l1 + q2*l2;
float circle_r = center.Norm();
if(circle_r > radius) return false; //need too big a sphere
float height = sqrt(radius*radius - circle_r*circle_r);
center += p0 + up*height;
return true;
}
std::list<Hinge>::iterator touches(int v, std::list<Hinge>::iterator e) {
//TODO what happens when it touches more than one front?
//might still work.
std::list<Hinge>::iterator touch = front.end();
if(mesh.vert[v].IsB()) {
//test nearby Hinges: it is faster
std::list<Hinge>::iterator p = e;
p = (*e).previous;
if(v == (*p).v0) return p;
e = (*e).next;
if(v == (*e).v0) return e;
p = (*p).previous;
if(v == (*p).v0) return p;
e = (*e).next;
if(v == (*e).v0) return e;
//test all. sigh.
for(std::list<Hinge>::iterator k = front.begin(); k != front.end(); k++) {
if(v == (*k).v0) {
touch = k;
break;
}
}
for(std::list<Hinge>::iterator k = deads.begin(); k != deads.end(); k++) {
if(v == (*k).v0) {
touch = k;
break;
}
}
assert(touch != front.end());
}
return touch;
}
public:
};
}//namespace
}//namespace
/* CODE FOR PIVOTING IN A TOUCH SITUATION not used now.
//if touch we want to check the ball could really pivot around that point
if(touch != front.end() && touch != (*Hinge.next).next && touch != Hinge.previous) {
Point3f &hinge = mesh.vert[min].P();
Point3f target = (*touch).center - hinge;
float d = (target * start_pivot)/(target.Norm()*start_pivot.Norm());
if(d < -0.8) {
return false;
}
if(d < 0.5) { //they are far enough so test .
Point3f naxis = (target ^ start_pivot).Normalize();
Point3f d1 = naxis^start_pivot;
Point3f d2 = target^naxis;
for(int i = 0; i < targets.size(); i++) {
int id = targets[i];
if(id == Hinge.v0 || id == Hinge.v1 || id == Hinge.v2 || id == min) continue;
if(mesh.vert[id].IsD()) {
continue;
}
Point3f intruder = mesh.vert[targets[i]].P() - hinge;
float h = intruder*naxis;
if(fabs(h) > radius) continue;
intruder -= naxis*h;
assert(fabs(intruder *naxis) < 0.01);
float off = radius - intruder.Norm(); //(distance from the center ring of the thorus
if(h*h + off*off > radius*radius) continue; //outside of thorus
if(d1*intruder < 0 || d2*intruder < 0) continue; //ouside of sector
cout << "could not pivot while touching;\n";
return false;
}
}
}*/
#endif
|