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
|
/****************************************************************************
* 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 VCG_TRI_CONVEX_HULL_H
#define VCG_TRI_CONVEX_HULL_H
#include <queue>
#include <unordered_map>
#include <vcg/complex/complex.h>
#include <vcg/complex/algorithms/clean.h>
namespace vcg
{
namespace tri
{
template <class InputMesh, class CHMesh>
class ConvexHull
{
public:
typedef typename InputMesh::ScalarType ScalarType;
typedef typename InputMesh::CoordType CoordType;
typedef typename InputMesh::VertexPointer InputVertexPointer;
typedef typename InputMesh::VertexIterator InputVertexIterator;
typedef typename CHMesh::VertexIterator CHVertexIterator;
typedef typename CHMesh::VertexPointer CHVertexPointer;
typedef typename CHMesh::FaceIterator CHFaceIterator;
typedef typename CHMesh::FacePointer CHFacePointer;
private:
typedef std::pair<InputVertexPointer, ScalarType> Pair;
// Initialize the convex hull with the biggest tetraedron created using the vertices of the input mesh
static void InitConvexHull(InputMesh& mesh, CHMesh& convexHull)
{
typename CHMesh:: template PerVertexAttributeHandle<size_t> indexInputVertex = Allocator<CHMesh>::template GetPerVertexAttribute<size_t>(convexHull, std::string("indexInput"));
InputVertexPointer v[3];
//Find the 6 points with min/max coordinate values
InputVertexIterator vi = mesh.vert.begin();
std::vector<InputVertexPointer> minMax(6, &(*vi));
for (; vi != mesh.vert.end(); vi++)
{
if ((*vi).P().X() < (*minMax[0]).P().X())
minMax[0] = &(*vi);
if ((*vi).P().Y() < (*minMax[1]).P().Y())
minMax[1] = &(*vi);
if ((*vi).P().Z() < (*minMax[2]).P().Z())
minMax[2] = &(*vi);
if ((*vi).P().X() > (*minMax[3]).P().X())
minMax[3] = &(*vi);
if ((*vi).P().Y() > (*minMax[4]).P().Y())
minMax[4] = &(*vi);
if ((*vi).P().Z() > (*minMax[5]).P().Z())
minMax[5] = &(*vi);
}
//Find the farthest two points
ScalarType maxDist = 0;
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 6; j++)
{
float dist = (minMax[i]->P() - minMax[j]->P()).SquaredNorm();
if (dist > maxDist)
{
maxDist = dist;
v[0] = minMax[i];
v[1] = minMax[j];
}
}
}
//Find the third point to create the base of the tetrahedron
vcg::Line3<ScalarType> line(v[0]->P(), (v[0]->P() - v[1]->P()));
maxDist = 0;
for (vi = mesh.vert.begin(); vi != mesh.vert.end(); vi++)
{
ScalarType dist = vcg::Distance(line, (*vi).P());
if (dist > maxDist)
{
maxDist = dist;
v[2] = &(*vi);
}
}
//Create face in the convex hull
CHVertexIterator chVi = vcg::tri::Allocator<CHMesh>::AddVertices(convexHull, 3);
for (int i = 0; i < 3; i++)
{
(*chVi).P().Import(v[i]->P());
v[i]->SetV();
indexInputVertex[chVi] = vcg::tri::Index(mesh, v[i]);
chVi++;
}
CHFaceIterator fi = vcg::tri::Allocator<CHMesh>::AddFace(convexHull, 0, 1, 2);
(*fi).N() = vcg::NormalizedTriangleNormal(*fi);
//Find the fourth point to create the tetrahedron
InputVertexPointer v4;
float distance = 0;
float absDist = -1;
for (vi = mesh.vert.begin(); vi != mesh.vert.end(); vi++)
{
float tempDist = ((*vi).P() - (*fi).P(0)).dot((*fi).N());
if (fabs(tempDist) > absDist)
{
distance = tempDist;
v4 = &(*vi);
absDist = fabs(distance);
}
}
//Flip the previous face if the fourth point is above the face
if (distance > 0)
{
(*fi).N() = -(*fi).N();
CHVertexPointer tempV = (*fi).V(1);
(*fi).V(1) = (*fi).V(2);
(*fi).V(2) = tempV;
}
//Create the other 3 faces of the tetrahedron
chVi = vcg::tri::Allocator<CHMesh>::AddVertices(convexHull, 1);
(*chVi).P().Import(v4->P());
indexInputVertex[chVi] = vcg::tri::Index(mesh, v4);
v4->SetV();
fi = vcg::tri::Allocator<CHMesh>::AddFace(convexHull, &convexHull.vert[3], convexHull.face[0].V0(1), convexHull.face[0].V0(0));
(*fi).N() = vcg::NormalizedTriangleNormal(*fi);
fi = vcg::tri::Allocator<CHMesh>::AddFace(convexHull, &convexHull.vert[3], convexHull.face[0].V1(1), convexHull.face[0].V1(0));
(*fi).N() = vcg::NormalizedTriangleNormal(*fi);
fi = vcg::tri::Allocator<CHMesh>::AddFace(convexHull, &convexHull.vert[3], convexHull.face[0].V2(1), convexHull.face[0].V2(0));
(*fi).N() = vcg::NormalizedTriangleNormal(*fi);
vcg::tri::UpdateTopology<CHMesh>::FaceFace(convexHull);
}
public:
/**
Return the convex hull of the input mesh using the Quickhull algorithm.
For each vertex of the convex hull the algorithm stores the vertex index
of the original mesh in attribute "indexInput".
"The quickhull algorithm for convex hulls" by C. Bradford Barber et al.
ACM Transactions on Mathematical Software, Volume 22 Issue 4, Dec. 1996
*/
static bool ComputeConvexHull(InputMesh& mesh, CHMesh& convexHull, ScalarType distTolerance = 0)
{
assert(distTolerance >= 0);
vcg::tri::RequireFFAdjacency(convexHull);
vcg::tri::RequirePerFaceNormal(convexHull);
vcg::tri::Allocator<InputMesh>::CompactVertexVector(mesh);
typename CHMesh:: template PerVertexAttributeHandle<size_t> indexInputVertex = Allocator<CHMesh>::template GetPerVertexAttribute<size_t>(convexHull, std::string("indexInput"));
if (mesh.vert.size() < 4)
return false;
vcg::tri::UpdateFlags<InputMesh>::VertexClearV(mesh);
InitConvexHull(mesh, convexHull);
//Build list of visible vertices for each convex hull face and find the furthest vertex for each face
std::vector<std::vector<InputVertexPointer>> listVertexPerFace(convexHull.face.size());
std::vector<Pair> furthestVexterPerFace(convexHull.face.size(), std::make_pair((InputVertexPointer)NULL, 0.0f));
for (size_t i = 0; i < mesh.vert.size(); i++)
{
if (!mesh.vert[i].IsV())
{
for (size_t j = 0; j < convexHull.face.size(); j++)
{
ScalarType dist = (mesh.vert[i].P() - convexHull.face[j].P(0)).dot(convexHull.face[j].N());
if (dist > distTolerance)
{
listVertexPerFace[j].push_back(&mesh.vert[i]);
if (dist > furthestVexterPerFace[j].second)
{
furthestVexterPerFace[j].second = dist;
furthestVexterPerFace[j].first = &mesh.vert[i];
}
}
}
}
}
for (size_t i = 0; i < listVertexPerFace.size(); i++)
{
if (listVertexPerFace[i].size() > 0)
{
//Find faces to remove and face on the border where to connect the new fan faces
InputVertexPointer vertex = furthestVexterPerFace[i].first;
std::queue<int> queue;
std::vector<int> visFace;
std::vector<int> borderFace;
visFace.push_back(i);
queue.push(i);
while (queue.size() > 0)
{
CHFacePointer fp = &convexHull.face[queue.front()];
queue.pop();
fp->SetV();
for (int ii = 0; ii < 3; ii++)
{
CHFacePointer nextF = fp->FFp(ii);
if (!nextF->IsV())
{
int indexF = vcg::tri::Index(convexHull, nextF);
ScalarType dist = (vertex->P() - nextF->P(0)).dot(nextF->N());
if (dist < distTolerance)
{
borderFace.push_back(indexF);
fp->SetB(ii);
nextF->SetB(fp->FFi(ii));
}
else
{
visFace.push_back(indexF);
queue.push(indexF);
}
}
}
}
if (borderFace.size() > 0)
{
CHVertexIterator vi = vcg::tri::Allocator<CHMesh>::AddVertices(convexHull, 1);
(*vi).P().Import((*vertex).P());
vertex->SetV();
indexInputVertex[vi] = vcg::tri::Index(mesh, vertex);
}
//Add a new face for each border
std::unordered_map< CHVertexPointer, std::pair<int, char> > fanMap;
for (size_t jj = 0; jj < borderFace.size(); jj++)
{
int indexFace = borderFace[jj];
CHFacePointer f = &convexHull.face[indexFace];
for (int j = 0; j < 3; j++)
{
if (f->IsB(j))
{
f->ClearB(j);
//Add new face
CHFaceIterator fi = vcg::tri::Allocator<CHMesh>::AddFace(convexHull, &convexHull.vert.back(), f->V1(j), f->V0(j));
(*fi).N() = vcg::NormalizedTriangleNormal(*fi);
f = &convexHull.face[indexFace];
int newFace = vcg::tri::Index(convexHull, *fi);
//Update convex hull FF topology
CHVertexPointer vp[] = { f->V1(j), f->V0(j) };
for (int ii = 0; ii < 2; ii++)
{
int indexE = ii * 2;
typename std::unordered_map< CHVertexPointer, std::pair<int, char> >::iterator vIter = fanMap.find(vp[ii]);
if (vIter != fanMap.end())
{
CHFacePointer f2 = &convexHull.face[(*vIter).second.first];
char edgeIndex = (*vIter).second.second;
f2->FFp(edgeIndex) = &convexHull.face.back();
f2->FFi(edgeIndex) = indexE;
fi->FFp(indexE) = f2;
fi->FFi(indexE) = edgeIndex;
}
else
{
fanMap[vp[ii]] = std::make_pair(newFace, indexE);
}
}
//Build the visibility list for the new face
std::vector<InputVertexPointer> tempVect;
int indices[2] = { indexFace, int(vcg::tri::Index(convexHull, f->FFp(j)) )};
std::vector<InputVertexPointer> vertexToTest(listVertexPerFace[indices[0]].size() + listVertexPerFace[indices[1]].size());
typename std::vector<InputVertexPointer>::iterator tempIt = std::set_union(listVertexPerFace[indices[0]].begin(), listVertexPerFace[indices[0]].end(), listVertexPerFace[indices[1]].begin(), listVertexPerFace[indices[1]].end(), vertexToTest.begin());
vertexToTest.resize(tempIt - vertexToTest.begin());
Pair newInfo = std::make_pair((InputVertexPointer)NULL , 0.0f);
for (size_t ii = 0; ii < vertexToTest.size(); ii++)
{
if (!(*vertexToTest[ii]).IsV())
{
float dist = ((*vertexToTest[ii]).P() - (*fi).P(0)).dot((*fi).N());
if (dist > distTolerance)
{
tempVect.push_back(vertexToTest[ii]);
if (dist > newInfo.second)
{
newInfo.second = dist;
newInfo.first = vertexToTest[ii];
}
}
}
}
listVertexPerFace.push_back(tempVect);
furthestVexterPerFace.push_back(newInfo);
//Update topology of the new face
CHFacePointer ffp = f->FFp(j);
int ffi = f->FFi(j);
ffp->FFp(ffi) = ffp;
ffp->FFi(ffi) = ffi;
f->FFp(j) = &convexHull.face.back();
f->FFi(j) = 1;
fi->FFp(1) = f;
fi->FFi(1) = j;
}
}
}
//Delete the faces inside the updated convex hull
for (size_t j = 0; j < visFace.size(); j++)
{
if (!convexHull.face[visFace[j]].IsD())
{
std::vector<InputVertexPointer> emptyVec;
vcg::tri::Allocator<CHMesh>::DeleteFace(convexHull, convexHull.face[visFace[j]]);
listVertexPerFace[visFace[j]].swap(emptyVec);
}
}
}
}
tri::UpdateTopology<CHMesh>::ClearFaceFace(convexHull);
vcg::tri::Allocator<CHMesh>::CompactFaceVector(convexHull);
vcg::tri::Clean<CHMesh>::RemoveUnreferencedVertex(convexHull);
return true;
}
/**
* @brief ComputePointVisibility
* Select the <b>visible points</b> in a point cloud, as viewed from a given viewpoint.
* It uses the Qhull implementation of che convex hull in the vcglibrary
* The algorithm used (Katz, Tal and Basri 2007) determines visibility without
* reconstructing a surface or estimating normals.
* A point is considered visible if its transformed point lies on the convex hull
* of a transformed points cloud from the original mesh points.
*
* @param m The point cloud
* @param visible The mesh that will contain the visible hull
* @param viewpoint
* @param logR Bounds the radius of the sphere used to select visible points.
* It is used to adjust the radius of the sphere (calculated as distance between
* the center and the farthest point from it) according to the following equation:
* radius = radius * pow(10,threshold);
* As the radius increases more points are marked as visible.
* Use a big threshold for dense point clouds, a small one for sparse clouds.
*/
static void ComputePointVisibility(InputMesh& m, CHMesh& visible, CoordType viewpoint, ScalarType logR=2)
{
visible.Clear();
tri::RequireCompactness(m);
InputMesh flipM;
printf("Input mesh m %i %i\n",m.vn,m.fn);
tri::Allocator<InputMesh>::AddVertices(flipM,m.vn);
ScalarType maxDist=0;
InputVertexIterator ci=flipM.vert.begin();
for(InputVertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
{
ci->P()=vi->P()-viewpoint;
maxDist = std::max(maxDist,Norm(ci->P()));
++ci;
}
ScalarType R = maxDist*pow(10,logR);
printf("Using R = %f logR = %f maxdist=%f \n",R,logR,maxDist);
for(InputVertexIterator vi=flipM.vert.begin();vi!=flipM.vert.end();++vi)
{
ScalarType d = Norm(vi->P());
vi->P() = vi->P() + vi->P()*ScalarType(2.0*(R - d)/d);
}
tri::Allocator<InputMesh>::AddVertex(flipM,CoordType(0,0,0));
assert(m.vn+1 == flipM.vn);
ComputeConvexHull(flipM,visible);
assert(flipM.vert[m.vn].P()==Point3f(0,0,0));
int vpInd=-1; // Index of the viewpoint in the ConvexHull mesh
int selCnt=0;
typename CHMesh:: template PerVertexAttributeHandle<size_t> indexInputVertex = Allocator<InputMesh>::template GetPerVertexAttribute<size_t>(visible, std::string("indexInput"));
for(int i=0;i<visible.vn;++i)
{
size_t ind = indexInputVertex[i];
if(ind==m.vn) vpInd = i;
else
{
visible.vert[i].P() = m.vert[ind].P();
m.vert[ind].SetS();
//m.vert[ind].C() = Color4b::LightBlue;
selCnt++;
}
}
printf("Selected %i visible points\n",selCnt);
assert(vpInd != -1);
// Final pass delete all the faces of the convex hull incident in the viewpoint
for(int i=0;i<visible.fn;++i)
{
if( (Index(visible,visible.face[i].V(0)) == vpInd) ||
(Index(visible,visible.face[i].V(1)) == vpInd) ||
(Index(visible,visible.face[i].V(2)) == vpInd) )
tri::Allocator<CHMesh>::DeleteFace(visible,visible.face[i]);
}
tri::Allocator<CHMesh>::CompactEveryVector(visible);
tri::Clean<CHMesh>::FlipMesh(visible);
tri::UpdateNormal<CHMesh>::PerFaceNormalized(visible);
tri::UpdateNormal<CHMesh>::PerVertexNormalized(visible);
}
};
} // end namespace tri
} // end namespace vcg
#endif //VCG_TRI_CONVEX_HULL_H
|