| 1 |
/* |
| 2 |
Copyright (c) 2013, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "PreProcessor.h" |
| 30 |
|
| 31 |
#define DEFAULT_DIMENSION 3 |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <float.h> |
| 36 |
#include <algorithm> |
| 37 |
#include <unordered_set> |
| 38 |
#include <list> |
| 39 |
#include "FEMTree.h" |
| 40 |
#include "MyMiscellany.h" |
| 41 |
#include "CmdLineParser.h" |
| 42 |
#include "MAT.h" |
| 43 |
#include "Geometry.h" |
| 44 |
#include "Ply.h" |
| 45 |
#include "VertexFactory.h" |
| 46 |
|
| 47 |
using namespace PoissonRecon; |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
CmdLineParameter< char* > |
| 52 |
In( "in" ) , |
| 53 |
Out( "out" ); |
| 54 |
CmdLineParameter< float > |
| 55 |
Trim( "trim" ) , |
| 56 |
IslandAreaRatio( "aRatio" , 0.001f ); |
| 57 |
CmdLineReadable |
| 58 |
PolygonMesh( "polygonMesh" ) , |
| 59 |
Long( "long" ) , |
| 60 |
ASCII( "ascii" ) , |
| 61 |
RemoveIslands( "removeIslands" ) , |
| 62 |
Debug( "debug" ) , |
| 63 |
Verbose( "verbose" ); |
| 64 |
|
| 65 |
|
| 66 |
CmdLineReadable* params[] = |
| 67 |
{ |
| 68 |
&In , &Out , &Trim , &PolygonMesh , &IslandAreaRatio , &Verbose , &Long , &ASCII , &RemoveIslands , &Debug , |
| 69 |
NULL |
| 70 |
}; |
| 71 |
|
| 72 |
void ShowUsage( char* ex ) |
| 73 |
{ |
| 74 |
printf( "Usage: %s\n" , ex ); |
| 75 |
printf( "\t --%s <input polygon mesh>\n" , In.name ); |
| 76 |
printf( "\t --%s <trimming value>\n" , Trim.name ); |
| 77 |
printf( "\t[--%s <ouput polygon mesh>]\n" , Out.name ); |
| 78 |
printf( "\t[--%s <relative area of islands>=%f]\n" , IslandAreaRatio.name , IslandAreaRatio.value ); |
| 79 |
printf( "\t[--%s]\n" , RemoveIslands.name ); |
| 80 |
printf( "\t[--%s]\n" , Debug.name ); |
| 81 |
printf( "\t[--%s]\n" , PolygonMesh.name ); |
| 82 |
printf( "\t[--%s]\n" , Long.name ); |
| 83 |
printf( "\t[--%s]\n" , ASCII.name ); |
| 84 |
printf( "\t[--%s]\n" , Verbose.name ); |
| 85 |
} |
| 86 |
|
| 87 |
template< typename Index > |
| 88 |
struct ComponentGraph |
| 89 |
{ |
| 90 |
struct Node |
| 91 |
{ |
| 92 |
double area; |
| 93 |
std::vector< Node * > neighbors; |
| 94 |
std::list< Index > polygonIndices; |
| 95 |
|
| 96 |
Node( void ) : area(0) {} |
| 97 |
|
| 98 |
void merge( void ) |
| 99 |
{ |
| 100 |
auto PopBack =[&]( std::vector< Node * > &nodes , size_t idx ) |
| 101 |
{ |
| 102 |
nodes[idx] = nodes.back(); |
| 103 |
nodes.pop_back(); |
| 104 |
}; |
| 105 |
|
| 106 |
if( !neighbors.size() ) MK_THROW( "No neighbors" ); |
| 107 |
|
| 108 |
// Remove the node from the neighbors of the neighbors |
| 109 |
for( unsigned int i=0 ; i<neighbors.size() ; i++ ) for( int j=(int)neighbors[i]->neighbors.size()-1 ; j>=0 ; j-- ) if( neighbors[i]->neighbors[j]==this ) |
| 110 |
PopBack( neighbors[i]->neighbors , j ); |
| 111 |
|
| 112 |
// Merge the node into its first neighbor |
| 113 |
Node *first = neighbors[0]; |
| 114 |
first->area += area; |
| 115 |
first->polygonIndices.splice( first->polygonIndices.end() , polygonIndices ); |
| 116 |
|
| 117 |
// Merge the remaining neighbors into the first neighbor |
| 118 |
for( unsigned int i=1 ; i<neighbors.size() ; i++ ) |
| 119 |
{ |
| 120 |
first->area += neighbors[i]->area; |
| 121 |
first->polygonIndices.splice( first->polygonIndices.end() , neighbors[i]->polygonIndices ); |
| 122 |
for( unsigned int j=0 ; j<neighbors[i]->neighbors.size() ; j++ ) |
| 123 |
{ |
| 124 |
bool foundNeighbor = false; |
| 125 |
for( int k=(int)neighbors[i]->neighbors[j]->neighbors.size()-1 ; k>=0 ; k-- ) |
| 126 |
if( neighbors[i]->neighbors[j]->neighbors[k]==neighbors[i] ) PopBack( neighbors[i]->neighbors[j]->neighbors , k ); |
| 127 |
|
| 128 |
for( unsigned int k=0 ; k<first->neighbors.size() ; k++ ) foundNeighbor |= neighbors[i]->neighbors[j]==first->neighbors[k]; |
| 129 |
if( !foundNeighbor ) |
| 130 |
{ |
| 131 |
first->neighbors.push_back( neighbors[i]->neighbors[j] ); |
| 132 |
neighbors[i]->neighbors[j]->neighbors.push_back( first ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
neighbors[i]->area = 0; |
| 137 |
neighbors[i]->neighbors.clear(); |
| 138 |
} |
| 139 |
// Clean up the node |
| 140 |
polygonIndices.clear(); |
| 141 |
area = 0; |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
static void SanityCheck( size_t count , std::function< const Node * ( size_t ) > nodeFunction ) |
| 146 |
{ |
| 147 |
std::unordered_map< const Node * , int > flags; |
| 148 |
for( unsigned int i=0 ; i<count ; i++ ) flags[ nodeFunction(i) ] = 0; |
| 149 |
for( auto iter=flags.begin() ; iter!=flags.end() ; iter++ ) if( !iter->second ) _PropagateFlag( flags , iter->first , 1 ); |
| 150 |
|
| 151 |
for( auto iter=flags.begin() ; iter!=flags.end() ; iter++ ) for( unsigned int j=0 ; j<iter->first->neighbors.size() ; j++ ) |
| 152 |
{ |
| 153 |
if( iter->second==flags[ iter->first->neighbors[j] ] ) MK_THROW( "Not a bipartite graph" ); |
| 154 |
bool foundSelf = false; |
| 155 |
for( unsigned int k=0 ; k<iter->first->neighbors[j]->neighbors.size() ; k++ ) if( iter->first->neighbors[j]->neighbors[k]==iter->first ) foundSelf = true; |
| 156 |
if( !foundSelf ) MK_THROW( "Asymmetric graph" ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
protected: |
| 161 |
static void _PropagateFlag( std::unordered_map< const Node * , int > &flags , const Node *n , int flag ) |
| 162 |
{ |
| 163 |
if( flags[n] ) return; |
| 164 |
else |
| 165 |
{ |
| 166 |
flags[n] = flag; |
| 167 |
for( unsigned int i=0 ; i<n->neighbors.size() ; i++ ) _PropagateFlag( flags , n->neighbors[i] , -flag ); |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
}; |
| 173 |
|
| 174 |
template< typename Real , unsigned int Dim , typename ... AuxData > |
| 175 |
using ValuedPointData = DirectSum< Real , Point< Real , Dim > , Real , AuxData ... >; |
| 176 |
|
| 177 |
template< typename Index > |
| 178 |
size_t BoostHash( Index i1 , Index i2 ) |
| 179 |
{ |
| 180 |
size_t hash = (size_t)i1 + 0x9e3779b9; |
| 181 |
hash ^= (size_t)i2 + 0x9e3779b9 + (hash<<6) + (hash>>2); |
| 182 |
return hash; |
| 183 |
} |
| 184 |
|
| 185 |
template< typename Index > |
| 186 |
struct EdgeKey |
| 187 |
{ |
| 188 |
Index key1 , key2; |
| 189 |
EdgeKey( Index k1=0 , Index k2=0 ) |
| 190 |
{ |
| 191 |
if( k1<k2 ) key1 = k1 , key2 = k2; |
| 192 |
else key1 = k2 , key2 = k1; |
| 193 |
} |
| 194 |
bool operator == ( const EdgeKey &key ) const { return key1==key.key1 && key2==key.key2; } |
| 195 |
struct Hasher{ size_t operator()( const EdgeKey &key ) const { return BoostHash(key.key1,key.key2); } }; |
| 196 |
}; |
| 197 |
|
| 198 |
template< typename Index > |
| 199 |
struct HalfEdgeKey |
| 200 |
{ |
| 201 |
Index key1 , key2; |
| 202 |
HalfEdgeKey( Index k1=0 , Index k2=0 ) : key1(k1) , key2(k2) {} |
| 203 |
HalfEdgeKey opposite( void ) const { return HalfEdgeKey( key2 , key1 ); } |
| 204 |
bool operator == ( const HalfEdgeKey &key ) const { return key1==key.key1 && key2==key.key2; } |
| 205 |
struct Hasher{ size_t operator()( const HalfEdgeKey &key ) const { return BoostHash(key.key1,key.key2); } }; |
| 206 |
}; |
| 207 |
|
| 208 |
template< typename Real , unsigned int Dim , typename ... AuxData > |
| 209 |
ValuedPointData< Real , Dim , AuxData ... > InterpolateVertices( const ValuedPointData< Real , Dim , AuxData ... >& v1 , const ValuedPointData< Real , Dim , AuxData ... >& v2 , Real value ) |
| 210 |
{ |
| 211 |
if( v1.template get<1>()==v2.template get<1>() ) return (v1+v2)/Real(2.); |
| 212 |
Real dx = ( v1.template get<1>()-value ) / ( v1.template get<1>()-v2.template get<1>() ); |
| 213 |
return v1 * (Real)(1.-dx) + v2*dx; |
| 214 |
} |
| 215 |
|
| 216 |
template< typename Real , unsigned int Dim , typename Index , typename ... AuxData > |
| 217 |
void SplitPolygon |
| 218 |
( |
| 219 |
const std::vector< Index >& polygon , |
| 220 |
std::vector< ValuedPointData< Real , Dim , AuxData ... > >& vertices , |
| 221 |
std::vector< std::vector< Index > >* ltPolygons , std::vector< std::vector< Index > >* gtPolygons , |
| 222 |
std::vector< bool >* ltFlags , std::vector< bool >* gtFlags , |
| 223 |
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >& vertexTable, |
| 224 |
Real trimValue |
| 225 |
) |
| 226 |
{ |
| 227 |
int sz = int( polygon.size() ); |
| 228 |
std::vector< bool > gt( sz ); |
| 229 |
int gtCount = 0; |
| 230 |
for( int j=0 ; j<sz ; j++ ) |
| 231 |
{ |
| 232 |
gt[j] = ( vertices[ polygon[j] ].template get<1>()>trimValue ); |
| 233 |
if( gt[j] ) gtCount++; |
| 234 |
} |
| 235 |
if ( gtCount==sz ){ if( gtPolygons ) gtPolygons->push_back( polygon ) ; if( gtFlags ) gtFlags->push_back( false ); } |
| 236 |
else if( gtCount==0 ){ if( ltPolygons ) ltPolygons->push_back( polygon ) ; if( ltFlags ) ltFlags->push_back( false ); } |
| 237 |
else |
| 238 |
{ |
| 239 |
int start; |
| 240 |
for( start=0 ; start<sz ; start++ ) if( gt[start] && !gt[(start+sz-1)%sz] ) break; |
| 241 |
|
| 242 |
bool gtFlag = true; |
| 243 |
std::vector< Index > poly; |
| 244 |
|
| 245 |
// Add the initial vertex |
| 246 |
{ |
| 247 |
int j1 = (start+int(sz)-1)%sz , j2 = start; |
| 248 |
Index v1 = polygon[j1] , v2 = polygon[j2] , vIdx; |
| 249 |
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = vertexTable.find( EdgeKey< Index >(v1,v2) ); |
| 250 |
if( iter==vertexTable.end() ) |
| 251 |
{ |
| 252 |
vertexTable[ EdgeKey< Index >(v1,v2) ] = vIdx = (Index)vertices.size(); |
| 253 |
vertices.push_back( InterpolateVertices( vertices[v1] , vertices[v2] , trimValue ) ); |
| 254 |
} |
| 255 |
else vIdx = iter->second; |
| 256 |
poly.push_back( vIdx ); |
| 257 |
} |
| 258 |
|
| 259 |
for( int _j=0 ; _j<=sz ; _j++ ) |
| 260 |
{ |
| 261 |
int j1 = (_j+start+sz-1)%sz , j2 = (_j+start)%sz; |
| 262 |
Index v1 = polygon[j1] , v2 = polygon[j2]; |
| 263 |
if( gt[j2]==gtFlag ) poly.push_back( v2 ); |
| 264 |
else |
| 265 |
{ |
| 266 |
Index vIdx; |
| 267 |
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = vertexTable.find( EdgeKey< Index >(v1,v2) ); |
| 268 |
if( iter==vertexTable.end() ) |
| 269 |
{ |
| 270 |
vertexTable[ EdgeKey< Index >(v1,v2) ] = vIdx = (Index)vertices.size(); |
| 271 |
vertices.push_back( InterpolateVertices( vertices[v1] , vertices[v2] , trimValue ) ); |
| 272 |
} |
| 273 |
else vIdx = iter->second; |
| 274 |
poly.push_back( vIdx ); |
| 275 |
if( gtFlag ){ if( gtPolygons ) gtPolygons->push_back( poly ) ; if( ltFlags ) ltFlags->push_back( true ); } |
| 276 |
else { if( ltPolygons ) ltPolygons->push_back( poly ) ; if( gtFlags ) gtFlags->push_back( true ); } |
| 277 |
poly.clear() , poly.push_back( vIdx ) , poly.push_back( v2 ); |
| 278 |
gtFlag = !gtFlag; |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
template< class Real , unsigned int Dim , typename Index , class Vertex > |
| 285 |
void Triangulate( const std::vector< Vertex >& vertices , const std::vector< std::vector< Index > >& polygons , std::vector< std::vector< Index > >& triangles ) |
| 286 |
{ |
| 287 |
triangles.clear(); |
| 288 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 289 |
if( polygons[i].size()>3 ) |
| 290 |
{ |
| 291 |
std::vector< Point< Real , Dim > > _vertices( polygons[i].size() ); |
| 292 |
for( int j=0 ; j<int( polygons[i].size() ) ; j++ ) _vertices[j] = vertices[ polygons[i][j] ].template get<0>(); |
| 293 |
std::vector< TriangleIndex< Index > > _triangles = MinimalAreaTriangulation< Index , Real , Dim >( ( ConstPointer( Point< Real , Dim > ) )GetPointer( _vertices ) , _vertices.size() ); |
| 294 |
|
| 295 |
// Add the triangles to the mesh |
| 296 |
size_t idx = triangles.size(); |
| 297 |
triangles.resize( idx+_triangles.size() ); |
| 298 |
for( int j=0 ; j<int(_triangles.size()) ; j++ ) |
| 299 |
{ |
| 300 |
triangles[idx+j].resize(3); |
| 301 |
for( int k=0 ; k<3 ; k++ ) triangles[idx+j][k] = polygons[i][ _triangles[j].idx[k] ]; |
| 302 |
} |
| 303 |
} |
| 304 |
else if( polygons[i].size()==3 ) triangles.push_back( polygons[i] ); |
| 305 |
} |
| 306 |
|
| 307 |
template< class Real , unsigned int Dim , typename Index , class Vertex > |
| 308 |
double PolygonArea( const std::vector< Vertex >& vertices , const std::vector< Index >& polygon ) |
| 309 |
{ |
| 310 |
auto Area =[]( Point< Real , Dim > v1 , Point< Real , Dim > v2 , Point< Real , Dim > v3 ) |
| 311 |
{ |
| 312 |
Point< Real , Dim > v[] = { v2-v1 , v3-v1 }; |
| 313 |
XForm< Real , 2 > Mass; |
| 314 |
for( int i=0 ; i<2 ; i++ ) for( int j=0 ; j<2 ; j++ ) Mass(i,j) = Point< Real , Dim >::Dot( v[i] , v[j] ); |
| 315 |
double det = Mass.determinant(); |
| 316 |
if( det<0 ) return (Real)0; |
| 317 |
else return (Real)( sqrt( Mass.determinant() ) / 2. ); |
| 318 |
}; |
| 319 |
|
| 320 |
if( polygon.size()<3 ) return 0.; |
| 321 |
else if( polygon.size()==3 ) return Area( vertices[polygon[0]].template get<0>() , vertices[polygon[1]].template get<0>() , vertices[polygon[2]].template get<0>() ); |
| 322 |
else |
| 323 |
{ |
| 324 |
Point< Real , DEFAULT_DIMENSION > center; |
| 325 |
for( size_t i=0 ; i<polygon.size() ; i++ ) center += vertices[ polygon[i] ].template get<0>(); |
| 326 |
center /= Real( polygon.size() ); |
| 327 |
double area = 0; |
| 328 |
for( size_t i=0 ; i<polygon.size() ; i++ ) area += Area( center , vertices[ polygon[i] ].template get<0>() , vertices[ polygon[ (i+1)%polygon.size() ] ].template get<0>() ); |
| 329 |
return area; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
template< typename Index , class Vertex > |
| 334 |
void RemoveHangingVertices( std::vector< Vertex >& vertices , std::vector< std::vector< Index > >& polygons ) |
| 335 |
{ |
| 336 |
std::unordered_map< Index, Index > vMap; |
| 337 |
std::vector< bool > vertexFlags( vertices.size() , false ); |
| 338 |
for( size_t i=0 ; i<polygons.size() ; i++ ) for( size_t j=0 ; j<polygons[i].size() ; j++ ) vertexFlags[ polygons[i][j] ] = true; |
| 339 |
Index vCount = 0; |
| 340 |
for( Index i=0 ; i<(Index)vertices.size() ; i++ ) if( vertexFlags[i] ) vMap[i] = vCount++; |
| 341 |
for( size_t i=0 ; i<polygons.size() ; i++ ) for( size_t j=0 ; j<polygons[i].size() ; j++ ) polygons[i][j] = vMap[ polygons[i][j] ]; |
| 342 |
|
| 343 |
std::vector< Vertex > _vertices( vCount ); |
| 344 |
for( Index i=0 ; i<(Index)vertices.size() ; i++ ) if( vertexFlags[i] ) _vertices[ vMap[i] ] = vertices[i]; |
| 345 |
vertices = _vertices; |
| 346 |
} |
| 347 |
|
| 348 |
template< typename Index > |
| 349 |
void SetConnectedComponents( const std::vector< std::vector< Index > >& polygons , std::vector< std::vector< Index > >& components ) |
| 350 |
{ |
| 351 |
std::vector< Index > polygonRoots( polygons.size() ); |
| 352 |
for( size_t i=0 ; i<polygons.size() ; i++ ) polygonRoots[i] = (Index)i; |
| 353 |
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher > edgeTable; |
| 354 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 355 |
{ |
| 356 |
int sz = int( polygons[i].size() ); |
| 357 |
for( int j=0 ; j<sz ; j++ ) |
| 358 |
{ |
| 359 |
int j1 = j , j2 = (j+1)%sz; |
| 360 |
Index v1 = polygons[i][j1] , v2 = polygons[i][j2]; |
| 361 |
EdgeKey< Index > eKey = EdgeKey< Index >(v1,v2); |
| 362 |
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = edgeTable.find(eKey); |
| 363 |
if( iter==edgeTable.end() ) edgeTable[ eKey ] = (Index)i; |
| 364 |
else |
| 365 |
{ |
| 366 |
Index p = iter->second; |
| 367 |
while( polygonRoots[p]!=p ) |
| 368 |
{ |
| 369 |
Index temp = polygonRoots[p]; |
| 370 |
polygonRoots[p] = (Index)i; |
| 371 |
p = temp; |
| 372 |
} |
| 373 |
polygonRoots[p] = (Index)i; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
for( size_t i=0 ; i<polygonRoots.size() ; i++ ) |
| 378 |
{ |
| 379 |
Index p = (Index)i; |
| 380 |
while( polygonRoots[p]!=p ) p = polygonRoots[p]; |
| 381 |
Index root = p; |
| 382 |
p = (Index)i; |
| 383 |
while( polygonRoots[p]!=p ) |
| 384 |
{ |
| 385 |
Index temp = polygonRoots[p]; |
| 386 |
polygonRoots[p] = root; |
| 387 |
p = temp; |
| 388 |
} |
| 389 |
} |
| 390 |
int cCount = 0; |
| 391 |
std::unordered_map< Index , Index > vMap; |
| 392 |
for( Index i=0 ; i<(Index)polygonRoots.size() ; i++ ) if( polygonRoots[i]==i ) vMap[i] = cCount++; |
| 393 |
components.resize( cCount ); |
| 394 |
for( Index i=0 ; i<(Index)polygonRoots.size() ; i++ ) components[ vMap[ polygonRoots[i] ] ].push_back(i); |
| 395 |
} |
| 396 |
|
| 397 |
template< typename Real , unsigned int Dim , typename Index , typename ... AuxDataFactories > |
| 398 |
int Execute( AuxDataFactories ... auxDataFactories ) |
| 399 |
{ |
| 400 |
typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::ValueFactory< Real > , AuxDataFactories ... > Factory; |
| 401 |
typedef typename Factory::VertexType Vertex; |
| 402 |
typename VertexFactory::PositionFactory< Real , Dim > pFactory; |
| 403 |
typename VertexFactory::ValueFactory< Real > vFactory; |
| 404 |
Factory factory( pFactory , vFactory , auxDataFactories ... ); |
| 405 |
Real min , max; |
| 406 |
|
| 407 |
std::vector< Vertex > vertices; |
| 408 |
std::vector< std::vector< Index > > polygons; |
| 409 |
|
| 410 |
int ft; |
| 411 |
std::vector< std::string > comments; |
| 412 |
PLY::ReadPolygons< Factory , Index >( In.value , factory , vertices , polygons , ft , comments ); |
| 413 |
|
| 414 |
min = max = vertices[0].template get<1>(); |
| 415 |
for( size_t i=0 ; i<vertices.size() ; i++ ) min = std::min< Real >( min , vertices[i].template get<1>() ) , max = std::max< Real >( max , vertices[i].template get<1>() ); |
| 416 |
|
| 417 |
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher > vertexTable; |
| 418 |
std::vector< std::vector< Index > > ltPolygons , gtPolygons; |
| 419 |
std::vector< bool > ltFlags , gtFlags; |
| 420 |
|
| 421 |
if( Verbose.set ) |
| 422 |
{ |
| 423 |
std::cout << "*********************************************" << std::endl; |
| 424 |
std::cout << "*********************************************" << std::endl; |
| 425 |
std::cout << "** Running Surface Trimmer (Version " << ADAPTIVE_SOLVERS_VERSION << ") **" << std::endl; |
| 426 |
std::cout << "*********************************************" << std::endl; |
| 427 |
std::cout << "*********************************************" << std::endl; |
| 428 |
} |
| 429 |
char str[1024]; |
| 430 |
for( int i=0 ; params[i] ; i++ ) |
| 431 |
if( params[i]->set ) |
| 432 |
{ |
| 433 |
params[i]->writeValue( str ); |
| 434 |
if( Verbose.set ) |
| 435 |
{ |
| 436 |
if( strlen( str ) ) std::cout << "\t--" << params[i]->name << " " << str << std::endl; |
| 437 |
else std::cout << "\t--" << params[i]->name << std::endl; |
| 438 |
} |
| 439 |
} |
| 440 |
if( Verbose.set ) printf( "Value Range: [%f,%f]\n" , min , max ); |
| 441 |
|
| 442 |
double t=Time(); |
| 443 |
for( size_t i=0 ; i<polygons.size() ; i++ ) SplitPolygon( polygons[i] , vertices , <Polygons , >Polygons , <Flags , >Flags , vertexTable , Trim.value ); |
| 444 |
|
| 445 |
if( IslandAreaRatio.value>0 ) |
| 446 |
{ |
| 447 |
std::vector< std::vector< Index > > _polygons , _components; |
| 448 |
size_t gtComponentStart; |
| 449 |
{ |
| 450 |
std::vector< std::vector< Index > > ltComponents , gtComponents; |
| 451 |
SetConnectedComponents( ltPolygons , ltComponents ); |
| 452 |
SetConnectedComponents( gtPolygons , gtComponents ); |
| 453 |
gtComponentStart = ltComponents.size(); |
| 454 |
for( unsigned int i=0 ; i<gtComponents.size() ; i++ ) for( unsigned int j=0 ; j<gtComponents[i].size() ; j++ ) gtComponents[i][j] += (Index)ltPolygons.size(); |
| 455 |
|
| 456 |
_polygons.reserve( ltPolygons.size() + gtPolygons.size() ); |
| 457 |
_components.reserve( ltComponents.size() + gtComponents.size() ); |
| 458 |
_polygons.insert( _polygons.end() , ltPolygons.begin() , ltPolygons.end() ); |
| 459 |
_polygons.insert( _polygons.end() , gtPolygons.begin() , gtPolygons.end() ); |
| 460 |
_components.insert( _components.end() , ltComponents.begin() , ltComponents.end() ); |
| 461 |
_components.insert( _components.end() , gtComponents.begin() , gtComponents.end() ); |
| 462 |
} |
| 463 |
std::vector< typename ComponentGraph< Index >::Node > nodes( _components.size() ); |
| 464 |
|
| 465 |
// Set the polygons within each component and compute areas |
| 466 |
for( unsigned int i=0 ; i<_components.size() ; i++ ) |
| 467 |
{ |
| 468 |
nodes[i].polygonIndices.insert( nodes[i].polygonIndices.end() , _components[i].begin() , _components[i].end() ); |
| 469 |
for( auto iter=nodes[i].polygonIndices.begin() ; iter!=nodes[i].polygonIndices.end() ; iter++ ) nodes[i].area += PolygonArea< Real , Dim , Index , Vertex >( vertices , _polygons[ *iter ] ); |
| 470 |
} |
| 471 |
|
| 472 |
// Compute the connectivity |
| 473 |
|
| 474 |
// A map identifying half-edges along the boundaries of components and associating them with the component |
| 475 |
std::unordered_map< HalfEdgeKey< Index > , Index , typename HalfEdgeKey< Index >::Hasher > componentBoundaryHalfEdges; |
| 476 |
for( unsigned int i=0 ; i<_components.size() ; i++ ) |
| 477 |
{ |
| 478 |
// All the half-edges for a given component |
| 479 |
std::unordered_set< HalfEdgeKey< Index > , typename HalfEdgeKey< Index >::Hasher > componentHalfEdges; |
| 480 |
for( unsigned int j=0 ; j<_components[i].size() ; j++ ) |
| 481 |
{ |
| 482 |
const std::vector< Index > &poly = _polygons[ _components[i][j] ]; |
| 483 |
for( unsigned int k=0 ; k<poly.size() ; k++ ) |
| 484 |
{ |
| 485 |
Index v1 = poly[k] , v2 = poly[ (k+1)%poly.size() ]; |
| 486 |
HalfEdgeKey< Index > eKey = HalfEdgeKey< Index >(v1,v2); |
| 487 |
componentHalfEdges.insert( eKey ); |
| 488 |
} |
| 489 |
} |
| 490 |
for( auto iter=componentHalfEdges.begin() ; iter!=componentHalfEdges.end() ; iter++ ) |
| 491 |
{ |
| 492 |
HalfEdgeKey< Index > key = *iter; |
| 493 |
HalfEdgeKey< Index > _key = key.opposite(); |
| 494 |
if( componentHalfEdges.find( _key )==componentHalfEdges.end() ) componentBoundaryHalfEdges[ key ] = (Index)i; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
// A set identify the dual edges of the component graph |
| 499 |
std::unordered_set< EdgeKey< Index > , typename EdgeKey< Index >::Hasher > componentEdges; |
| 500 |
for( auto iter=componentBoundaryHalfEdges.begin() ; iter!=componentBoundaryHalfEdges.end() ; iter++ ) |
| 501 |
{ |
| 502 |
HalfEdgeKey< Index > key = iter->first; |
| 503 |
HalfEdgeKey< Index > _key = key.opposite(); |
| 504 |
auto _iter = componentBoundaryHalfEdges.find( _key ); |
| 505 |
if( _iter!=componentBoundaryHalfEdges.end() ) componentEdges.insert( EdgeKey< Index >( iter->second , _iter->second ) ); |
| 506 |
} |
| 507 |
for( auto iter=componentEdges.begin() ; iter!=componentEdges.end() ; iter++ ) |
| 508 |
{ |
| 509 |
nodes[ iter->key1 ].neighbors.push_back( &nodes[ iter->key2 ] ); |
| 510 |
nodes[ iter->key2 ].neighbors.push_back( &nodes[ iter->key1 ] ); |
| 511 |
} |
| 512 |
if( Debug.set ) ComponentGraph< Index >::SanityCheck( nodes.size() , [&]( size_t i ){ return &nodes[i]; } ); |
| 513 |
|
| 514 |
auto ComponentCount = [&]( void ) |
| 515 |
{ |
| 516 |
unsigned int count = 0; |
| 517 |
for( unsigned int i=0 ; i<nodes.size() ; i++ ) if( nodes[i].polygonIndices.size() ) count++; |
| 518 |
return count; |
| 519 |
}; |
| 520 |
|
| 521 |
double area = 0; |
| 522 |
for( unsigned int i=0 ; i<nodes.size() ; i++ ) area += nodes[i].area; |
| 523 |
|
| 524 |
bool done = false; |
| 525 |
while( !done ) |
| 526 |
{ |
| 527 |
done = true; |
| 528 |
unsigned int idx = -1; |
| 529 |
for( unsigned int i=0 ; i<nodes.size() ; i++ ) if( nodes[i].polygonIndices.size() && nodes[i].neighbors.size() ) if( idx==-1 || nodes[i].area<nodes[idx].area ) idx = i; |
| 530 |
if( idx!=-1 && nodes[idx].area<area*IslandAreaRatio.value ) |
| 531 |
{ |
| 532 |
nodes[idx].merge(); |
| 533 |
done = false; |
| 534 |
if( Debug.set ) ComponentGraph< Index >::SanityCheck( nodes.size() , [&]( size_t i ){ return &nodes[i]; } ); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
ltPolygons.clear() , gtPolygons.clear(); |
| 539 |
|
| 540 |
for( unsigned int i=0 ; i<gtComponentStart ; i++ ) |
| 541 |
if( !nodes[i].neighbors.size() && nodes[i].area<area*IslandAreaRatio.value && RemoveIslands.set ) ; // small island |
| 542 |
else for( auto iter=nodes[i].polygonIndices.begin() ; iter!=nodes[i].polygonIndices.end() ; iter++ ) ltPolygons.push_back( _polygons[ *iter ] ); |
| 543 |
for( unsigned int i=(unsigned int)gtComponentStart ; i<nodes.size() ; i++ ) |
| 544 |
if( !nodes[i].neighbors.size() && nodes[i].area<area*IslandAreaRatio.value && RemoveIslands.set ) ; // small island |
| 545 |
else for( auto iter=nodes[i].polygonIndices.begin() ; iter!=nodes[i].polygonIndices.end() ; iter++ ) gtPolygons.push_back( _polygons[ *iter ] ); |
| 546 |
} |
| 547 |
|
| 548 |
if( !PolygonMesh.set ) |
| 549 |
{ |
| 550 |
{ |
| 551 |
std::vector< std::vector< Index > > polys = ltPolygons; |
| 552 |
Triangulate< Real , Dim , Index , Vertex >( vertices , ltPolygons , polys ) , ltPolygons = polys; |
| 553 |
} |
| 554 |
{ |
| 555 |
std::vector< std::vector< Index > > polys = gtPolygons; |
| 556 |
Triangulate< Real , Dim , Index , Vertex >( vertices , gtPolygons , polys ) , gtPolygons = polys; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
RemoveHangingVertices( vertices , gtPolygons ); |
| 561 |
char comment[1024]; |
| 562 |
sprintf( comment , "#Trimmed In: %9.1f (s)" , Time()-t ); |
| 563 |
if( Out.set ) PLY::WritePolygons( Out.value , factory , vertices , gtPolygons , ASCII.set ? PLY_ASCII : ft , comments ); |
| 564 |
|
| 565 |
return EXIT_SUCCESS; |
| 566 |
} |
| 567 |
|
| 568 |
} // namespace |
| 569 |
|
| 570 |
int RunSurfaceTrimmer( int argc , char* argv[] ) |
| 571 |
{ |
| 572 |
CmdLineParse( argc-1 , &argv[1] , params ); |
| 573 |
|
| 574 |
if( !In.set || !Trim.set ) |
| 575 |
{ |
| 576 |
ShowUsage( argv[0] ); |
| 577 |
return EXIT_FAILURE; |
| 578 |
} |
| 579 |
typedef float Real; |
| 580 |
static constexpr unsigned int Dim = DEFAULT_DIMENSION; |
| 581 |
typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::ValueFactory< Real > > Factory; |
| 582 |
Factory factory; |
| 583 |
bool *readFlags = new bool[ factory.plyReadNum() ]; |
| 584 |
std::vector< PlyProperty > unprocessedProperties; |
| 585 |
size_t vNum; |
| 586 |
PLY::ReadVertexHeader( In.value , factory , readFlags , unprocessedProperties , vNum ); |
| 587 |
if( vNum>std::numeric_limits< int >::max() ) |
| 588 |
{ |
| 589 |
if( !Long.set ) MK_WARN( "Number of vertices not supported by 32-bit indexing. Switching to 64-bit indexing" ); |
| 590 |
Long.set = true; |
| 591 |
} |
| 592 |
if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" ); |
| 593 |
if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain values" ); |
| 594 |
delete[] readFlags; |
| 595 |
|
| 596 |
if( Long.set ) return Execute< Real , Dim , long long >( VertexFactory::DynamicFactory< Real >( unprocessedProperties ) ); |
| 597 |
else return Execute< Real , Dim , int >( VertexFactory::DynamicFactory< Real >( unprocessedProperties ) ); |
| 598 |
|
| 599 |
} |