Files
PoissonRecon/Src/SurfaceTrimmer.cpp
T

426 lines
18 KiB
C++
Raw Normal View History

2015-07-14 19:33:02 -04:00
/*
Copyright (c) 2013, Michael Kazhdan
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/
2019-03-19 19:47:43 -04:00
#include "PreProcessor.h"
2018-04-14 23:00:29 -04:00
#define DIMENSION 3
2016-09-17 23:31:32 -04:00
2015-07-14 19:33:02 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <algorithm>
2018-04-14 23:00:29 -04:00
#include "FEMTree.h"
#include "MyMiscellany.h"
2015-07-14 19:33:02 -04:00
#include "CmdLineParser.h"
2018-04-14 23:00:29 -04:00
#include "MAT.h"
2015-07-14 19:33:02 -04:00
#include "Geometry.h"
#include "Ply.h"
2018-05-13 21:39:47 -04:00
#include "PointStreamData.h"
2015-07-14 19:33:02 -04:00
2018-04-14 23:00:29 -04:00
MessageWriter messageWriter;
cmdLineParameter< char* >
In( "in" ) ,
Out( "out" );
cmdLineParameter< int >
Smooth( "smooth" , 5 );
cmdLineParameter< float >
Trim( "trim" ) ,
IslandAreaRatio( "aRatio" , 0.001f );
cmdLineReadable
PolygonMesh( "polygonMesh" ) ,
2019-03-19 19:47:43 -04:00
Long( "long" ) ,
2018-04-14 23:00:29 -04:00
Verbose( "verbose" );
2015-07-14 19:33:02 -04:00
cmdLineReadable* params[] =
{
2019-03-19 19:47:43 -04:00
&In , &Out , &Trim , &PolygonMesh , &Smooth , &IslandAreaRatio , &Verbose , &Long ,
2018-04-14 23:00:29 -04:00
NULL
2015-07-14 19:33:02 -04:00
};
void ShowUsage( char* ex )
{
printf( "Usage: %s\n" , ex );
printf( "\t --%s <input polygon mesh>\n" , In.name );
2018-04-14 23:00:29 -04:00
printf( "\t --%s <trimming value>\n" , Trim.name );
2015-07-14 19:33:02 -04:00
printf( "\t[--%s <ouput polygon mesh>]\n" , Out.name );
printf( "\t[--%s <smoothing iterations>=%d]\n" , Smooth.name , Smooth.value );
printf( "\t[--%s <relative area of islands>=%f]\n" , IslandAreaRatio.name , IslandAreaRatio.value );
printf( "\t[--%s]\n" , PolygonMesh.name );
2019-03-19 19:47:43 -04:00
printf( "\t[--%s]\n" , Long.name );
2018-04-14 23:00:29 -04:00
printf( "\t[--%s]\n" , Verbose.name );
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Index >
struct EdgeKey
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
Index key1 , key2;
EdgeKey( Index k1=0 , Index k2=0 ) : key1(k1) , key2(k2) {}
bool operator == ( const EdgeKey &key ) const { return key1==key.key1 && key2==key.key2; }
#if 1
struct Hasher{ size_t operator()( const EdgeKey &key ) const { return (size_t)( key.key1 * key.key2 ); } };
#else
struct Hasher{ size_t operator()( const EdgeKey &key ) const { return key.key1 ^ key.key2; } };
#endif
};
2015-07-14 19:33:02 -04:00
2018-05-13 21:39:47 -04:00
template< typename Real , typename ... VertexData >
PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > > InterpolateVertices( const PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > >& v1 , const PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > >& v2 , Real value )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
if( v1.data.template data<0>()==v2.data.template data<0>() ) return (v1+v2)/Real(2.);
Real dx = ( v1.data.template data<0>()-value ) / ( v1.data.template data<0>()-v2.data.template data<0>() );
2018-05-13 21:39:47 -04:00
return v1*(1.f-dx) + v2*dx;
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Real , typename Index , typename ... VertexData >
void SmoothValues( std::vector< PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > > >& vertices , const std::vector< std::vector< Index > >& polygons )
2015-07-14 19:33:02 -04:00
{
std::vector< int > count( vertices.size() );
std::vector< Real > sums( vertices.size() , 0 );
for( size_t i=0 ; i<polygons.size() ; i++ )
{
int sz = int(polygons[i].size());
for( int j=0 ; j<sz ; j++ )
{
int j1 = j , j2 = (j+1)%sz;
2019-03-19 19:47:43 -04:00
Index v1 = polygons[i][j1] , v2 = polygons[i][j2];
2015-07-14 19:33:02 -04:00
count[v1]++ , count[v2]++;
2019-03-19 19:47:43 -04:00
sums[v1] += vertices[v2].data.template data<0>() , sums[v2] += vertices[v1].data.template data<0>();
2015-07-14 19:33:02 -04:00
}
}
2019-03-19 19:47:43 -04:00
for( size_t i=0 ; i<vertices.size() ; i++ ) vertices[i].data.template data<0>() = ( sums[i] + vertices[i].data.template data<0>() ) / ( count[i] + 1 );
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< class Real , typename Index , typename ... VertexData >
2015-07-14 19:33:02 -04:00
void SplitPolygon
2019-03-19 19:47:43 -04:00
(
const std::vector< Index >& polygon ,
2018-05-13 21:39:47 -04:00
std::vector< PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > > >& vertices ,
2019-03-19 19:47:43 -04:00
std::vector< std::vector< Index > >* ltPolygons , std::vector< std::vector< Index > >* gtPolygons ,
2015-07-14 19:33:02 -04:00
std::vector< bool >* ltFlags , std::vector< bool >* gtFlags ,
2019-03-19 19:47:43 -04:00
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >& vertexTable,
2015-07-14 19:33:02 -04:00
Real trimValue
2019-03-19 19:47:43 -04:00
)
2015-07-14 19:33:02 -04:00
{
int sz = int( polygon.size() );
std::vector< bool > gt( sz );
int gtCount = 0;
for( int j=0 ; j<sz ; j++ )
{
2019-03-19 19:47:43 -04:00
gt[j] = ( vertices[ polygon[j] ].data.template data<0>()>trimValue );
2015-07-14 19:33:02 -04:00
if( gt[j] ) gtCount++;
}
if ( gtCount==sz ){ if( gtPolygons ) gtPolygons->push_back( polygon ) ; if( gtFlags ) gtFlags->push_back( false ); }
else if( gtCount==0 ){ if( ltPolygons ) ltPolygons->push_back( polygon ) ; if( ltFlags ) ltFlags->push_back( false ); }
else
{
int start;
for( start=0 ; start<sz ; start++ ) if( gt[start] && !gt[(start+sz-1)%sz] ) break;
bool gtFlag = true;
2019-03-19 19:47:43 -04:00
std::vector< Index > poly;
2015-07-14 19:33:02 -04:00
// Add the initial vertex
{
int j1 = (start+int(sz)-1)%sz , j2 = start;
2019-03-19 19:47:43 -04:00
Index v1 = polygon[j1] , v2 = polygon[j2] , vIdx;
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = vertexTable.find( EdgeKey< Index >(v1,v2) );
2015-07-14 19:33:02 -04:00
if( iter==vertexTable.end() )
{
2019-03-19 19:47:43 -04:00
vertexTable[ EdgeKey< Index >(v1,v2) ] = vIdx = (Index)vertices.size();
2015-07-14 19:33:02 -04:00
vertices.push_back( InterpolateVertices( vertices[v1] , vertices[v2] , trimValue ) );
}
else vIdx = iter->second;
poly.push_back( vIdx );
}
for( int _j=0 ; _j<=sz ; _j++ )
{
int j1 = (_j+start+sz-1)%sz , j2 = (_j+start)%sz;
2019-03-19 19:47:43 -04:00
Index v1 = polygon[j1] , v2 = polygon[j2];
2015-07-14 19:33:02 -04:00
if( gt[j2]==gtFlag ) poly.push_back( v2 );
else
{
2019-03-19 19:47:43 -04:00
Index vIdx;
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = vertexTable.find( EdgeKey< Index >(v1,v2) );
2015-07-14 19:33:02 -04:00
if( iter==vertexTable.end() )
{
2019-03-19 19:47:43 -04:00
vertexTable[ EdgeKey< Index >(v1,v2) ] = vIdx = (Index)vertices.size();
2015-07-14 19:33:02 -04:00
vertices.push_back( InterpolateVertices( vertices[v1] , vertices[v2] , trimValue ) );
}
else vIdx = iter->second;
poly.push_back( vIdx );
if( gtFlag ){ if( gtPolygons ) gtPolygons->push_back( poly ) ; if( ltFlags ) ltFlags->push_back( true ); }
else { if( ltPolygons ) ltPolygons->push_back( poly ) ; if( gtFlags ) gtFlags->push_back( true ); }
poly.clear() , poly.push_back( vIdx ) , poly.push_back( v2 );
gtFlag = !gtFlag;
}
}
}
}
2019-03-19 19:47:43 -04:00
template< class Real , typename Index , class Vertex >
void Triangulate( const std::vector< Vertex >& vertices , const std::vector< std::vector< Index > >& polygons , std::vector< std::vector< Index > >& triangles )
2015-07-14 19:33:02 -04:00
{
triangles.clear();
for( size_t i=0 ; i<polygons.size() ; i++ )
if( polygons.size()>3 )
{
2018-04-14 23:00:29 -04:00
std::vector< Point< Real , DIMENSION > > _vertices( polygons[i].size() );
2015-07-14 19:33:02 -04:00
for( int j=0 ; j<int( polygons[i].size() ) ; j++ ) _vertices[j] = vertices[ polygons[i][j] ].point;
2019-03-19 19:47:43 -04:00
std::vector< TriangleIndex< Index > > _triangles = MinimalAreaTriangulation< Index , Real , DIMENSION >( ( ConstPointer( Point< Real , DIMENSION > ) )GetPointer( _vertices ) , _vertices.size() );
2015-07-14 19:33:02 -04:00
// Add the triangles to the mesh
size_t idx = triangles.size();
triangles.resize( idx+_triangles.size() );
for( int j=0 ; j<int(_triangles.size()) ; j++ )
{
triangles[idx+j].resize(3);
for( int k=0 ; k<3 ; k++ ) triangles[idx+j][k] = polygons[i][ _triangles[j].idx[k] ];
}
}
else if( polygons[i].size()==3 ) triangles.push_back( polygons[i] );
}
2019-03-19 19:47:43 -04:00
template< class Real , typename Index , class Vertex >
double PolygonArea( const std::vector< Vertex >& vertices , const std::vector< Index >& polygon )
2015-07-21 10:07:22 -04:00
{
if( polygon.size()<3 ) return 0.;
2018-04-14 23:00:29 -04:00
else if( polygon.size()==3 ) return Area( vertices[polygon[0]].point , vertices[polygon[1]].point , vertices[polygon[2]].point );
2015-07-21 10:07:22 -04:00
else
{
2018-04-14 23:00:29 -04:00
Point< Real , DIMENSION > center;
2015-07-21 10:07:22 -04:00
for( size_t i=0 ; i<polygon.size() ; i++ ) center += vertices[ polygon[i] ].point;
center /= Real( polygon.size() );
double area = 0;
2018-04-14 23:00:29 -04:00
for( size_t i=0 ; i<polygon.size() ; i++ ) area += Area( center , vertices[ polygon[i] ].point , vertices[ polygon[ (i+1)%polygon.size() ] ].point );
2015-07-21 10:07:22 -04:00
return area;
}
}
2019-03-19 19:47:43 -04:00
template< typename Index , class Vertex >
void RemoveHangingVertices( std::vector< Vertex >& vertices , std::vector< std::vector< Index > >& polygons )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
std::unordered_map< Index, Index > vMap;
2015-07-14 19:33:02 -04:00
std::vector< bool > vertexFlags( vertices.size() , false );
for( size_t i=0 ; i<polygons.size() ; i++ ) for( size_t j=0 ; j<polygons[i].size() ; j++ ) vertexFlags[ polygons[i][j] ] = true;
2019-03-19 19:47:43 -04:00
Index vCount = 0;
for( Index i=0 ; i<(Index)vertices.size() ; i++ ) if( vertexFlags[i] ) vMap[i] = vCount++;
2015-07-14 19:33:02 -04:00
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] ];
std::vector< Vertex > _vertices( vCount );
2019-03-19 19:47:43 -04:00
for( Index i=0 ; i<(Index)vertices.size() ; i++ ) if( vertexFlags[i] ) _vertices[ vMap[i] ] = vertices[i];
2015-07-14 19:33:02 -04:00
vertices = _vertices;
}
2019-03-19 19:47:43 -04:00
template< typename Index >
void SetConnectedComponents( const std::vector< std::vector< Index > >& polygons , std::vector< std::vector< Index > >& components )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
std::vector< Index > polygonRoots( polygons.size() );
for( size_t i=0 ; i<polygons.size() ; i++ ) polygonRoots[i] = (Index)i;
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher > edgeTable;
2015-07-14 19:33:02 -04:00
for( size_t i=0 ; i<polygons.size() ; i++ )
{
int sz = int( polygons[i].size() );
for( int j=0 ; j<sz ; j++ )
{
int j1 = j , j2 = (j+1)%sz;
2019-03-19 19:47:43 -04:00
Index v1 = polygons[i][j1] , v2 = polygons[i][j2];
EdgeKey< Index > eKey = EdgeKey< Index >(v1,v2);
typename std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher >::iterator iter = edgeTable.find(eKey);
if( iter==edgeTable.end() ) edgeTable[ eKey ] = (Index)i;
2015-07-14 19:33:02 -04:00
else
{
2019-03-19 19:47:43 -04:00
Index p = iter->second;
2015-07-14 19:33:02 -04:00
while( polygonRoots[p]!=p )
{
2019-03-19 19:47:43 -04:00
Index temp = polygonRoots[p];
polygonRoots[p] = (Index)i;
2015-07-14 19:33:02 -04:00
p = temp;
}
2019-03-19 19:47:43 -04:00
polygonRoots[p] = (Index)i;
2015-07-14 19:33:02 -04:00
}
}
}
for( size_t i=0 ; i<polygonRoots.size() ; i++ )
{
2019-03-19 19:47:43 -04:00
Index p = (Index)i;
2015-07-14 19:33:02 -04:00
while( polygonRoots[p]!=p ) p = polygonRoots[p];
2019-03-19 19:47:43 -04:00
Index root = p;
p = (Index)i;
2015-07-14 19:33:02 -04:00
while( polygonRoots[p]!=p )
{
2019-03-19 19:47:43 -04:00
Index temp = polygonRoots[p];
2015-07-14 19:33:02 -04:00
polygonRoots[p] = root;
p = temp;
}
}
int cCount = 0;
2019-03-19 19:47:43 -04:00
std::unordered_map< Index , Index > vMap;
for( Index i=0 ; i<(Index)polygonRoots.size() ; i++ ) if( polygonRoots[i]==i ) vMap[i] = cCount++;
2015-07-14 19:33:02 -04:00
components.resize( cCount );
2019-03-19 19:47:43 -04:00
for( Index i=0 ; i<(Index)polygonRoots.size() ; i++ ) components[ vMap[ polygonRoots[i] ] ].push_back(i);
2015-07-14 19:33:02 -04:00
}
2019-03-19 19:47:43 -04:00
template< typename Index , typename ... VertexData >
2015-07-21 10:07:22 -04:00
int Execute( void )
2015-07-14 19:33:02 -04:00
{
2018-05-13 21:39:47 -04:00
typedef PlyVertexWithData< float , DIMENSION , MultiPointStreamData< float , PointStreamValue< float > , VertexData ... > > Vertex;
2015-07-14 19:33:02 -04:00
float min , max;
2015-07-21 10:07:22 -04:00
std::vector< Vertex > vertices;
2019-03-19 19:47:43 -04:00
std::vector< std::vector< Index > > polygons;
2015-07-14 19:33:02 -04:00
2018-10-11 16:57:44 -04:00
int ft;
std::vector< std::string > comments;
PlyReadPolygons< Vertex >( In.value , vertices , polygons , Vertex::PlyReadProperties() , Vertex::PlyReadNum , ft , comments );
2019-03-19 19:47:43 -04:00
for( int i=0 ; i<Smooth.value ; i++ ) SmoothValues< float , Index >( vertices , polygons );
min = max = vertices[0].data.template data<0>();
for( size_t i=0 ; i<vertices.size() ; i++ ) min = std::min< float >( min , vertices[i].data.template data<0>() ) , max = std::max< float >( max , vertices[i].data.template data<0>() );
2015-07-14 19:33:02 -04:00
2019-03-19 19:47:43 -04:00
std::unordered_map< EdgeKey< Index > , Index , typename EdgeKey< Index >::Hasher > vertexTable;
std::vector< std::vector< Index > > ltPolygons , gtPolygons;
2015-07-21 10:07:22 -04:00
std::vector< bool > ltFlags , gtFlags;
2015-07-14 19:33:02 -04:00
2018-04-14 23:00:29 -04:00
messageWriter( comments , "*********************************************\n" );
messageWriter( comments , "*********************************************\n" );
messageWriter( comments , "** Running Surface Trimmer (Version %s) **\n" , VERSION );
messageWriter( comments , "*********************************************\n" );
messageWriter( comments , "*********************************************\n" );
char str[1024];
for( int i=0 ; params[i] ; i++ )
if( params[i]->set )
{
params[i]->writeValue( str );
if( strlen( str ) ) messageWriter( comments , "\t--%s %s\n" , params[i]->name , str );
else messageWriter( comments , "\t--%s\n" , params[i]->name );
}
2018-10-11 16:57:44 -04:00
if( Verbose.set ) printf( "Value Range: [%f,%f]\n" , min , max );
2015-07-14 19:33:02 -04:00
2015-07-21 10:07:22 -04:00
double t=Time();
for( size_t i=0 ; i<polygons.size() ; i++ ) SplitPolygon( polygons[i] , vertices , &ltPolygons , &gtPolygons , &ltFlags , &gtFlags , vertexTable , Trim.value );
if( IslandAreaRatio.value>0 )
{
2019-03-19 19:47:43 -04:00
std::vector< std::vector< Index > > _ltPolygons , _gtPolygons;
std::vector< std::vector< Index > > ltComponents , gtComponents;
2015-07-21 10:07:22 -04:00
SetConnectedComponents( ltPolygons , ltComponents );
SetConnectedComponents( gtPolygons , gtComponents );
std::vector< double > ltAreas( ltComponents.size() , 0. ) , gtAreas( gtComponents.size() , 0. );
std::vector< bool > ltComponentFlags( ltComponents.size() , false ) , gtComponentFlags( gtComponents.size() , false );
double area = 0.;
for( size_t i=0 ; i<ltComponents.size() ; i++ )
2015-07-14 19:33:02 -04:00
{
2015-07-21 10:07:22 -04:00
for( size_t j=0 ; j<ltComponents[i].size() ; j++ )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
ltAreas[i] += PolygonArea< float , Index , Vertex >( vertices , ltPolygons[ ltComponents[i][j] ] );
2015-07-21 10:07:22 -04:00
ltComponentFlags[i] = ( ltComponentFlags[i] || ltFlags[ ltComponents[i][j] ] );
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
area += ltAreas[i];
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
for( size_t i=0 ; i<gtComponents.size() ; i++ )
2015-07-14 19:33:02 -04:00
{
2015-07-21 10:07:22 -04:00
for( size_t j=0 ; j<gtComponents[i].size() ; j++ )
2015-07-14 19:33:02 -04:00
{
2019-03-19 19:47:43 -04:00
gtAreas[i] += PolygonArea< float , Index , Vertex >( vertices , gtPolygons[ gtComponents[i][j] ] );
2015-07-21 10:07:22 -04:00
gtComponentFlags[i] = ( gtComponentFlags[i] || gtFlags[ gtComponents[i][j] ] );
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
area += gtAreas[i];
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
for( size_t i=0 ; i<ltComponents.size() ; i++ )
{
if( ltAreas[i]<area*IslandAreaRatio.value && ltComponentFlags[i] ) for( size_t j=0 ; j<ltComponents[i].size() ; j++ ) _gtPolygons.push_back( ltPolygons[ ltComponents[i][j] ] );
else for( size_t j=0 ; j<ltComponents[i].size() ; j++ ) _ltPolygons.push_back( ltPolygons[ ltComponents[i][j] ] );
}
for( size_t i=0 ; i<gtComponents.size() ; i++ )
{
if( gtAreas[i]<area*IslandAreaRatio.value && gtComponentFlags[i] ) for( size_t j=0 ; j<gtComponents[i].size() ; j++ ) _ltPolygons.push_back( gtPolygons[ gtComponents[i][j] ] );
else for( size_t j=0 ; j<gtComponents[i].size() ; j++ ) _gtPolygons.push_back( gtPolygons[ gtComponents[i][j] ] );
}
ltPolygons = _ltPolygons , gtPolygons = _gtPolygons;
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
if( !PolygonMesh.set )
2015-07-14 19:33:02 -04:00
{
2015-07-21 10:07:22 -04:00
{
2019-03-19 19:47:43 -04:00
std::vector< std::vector< Index > > polys = ltPolygons;
Triangulate< float , Index , Vertex >( vertices , ltPolygons , polys ) , ltPolygons = polys;
2015-07-21 10:07:22 -04:00
}
{
2019-03-19 19:47:43 -04:00
std::vector< std::vector< Index > > polys = gtPolygons;
Triangulate< float , Index , Vertex >( vertices , gtPolygons , polys ) , gtPolygons = polys;
2015-07-21 10:07:22 -04:00
}
2015-07-14 19:33:02 -04:00
}
2015-07-21 10:07:22 -04:00
RemoveHangingVertices( vertices , gtPolygons );
2018-10-11 16:57:44 -04:00
char comment[1024];
sprintf( comment , "#Trimmed In: %9.1f (s)" , Time()-t );
comments.push_back( comment );
2018-10-30 11:09:34 -04:00
if( Out.set )
if( !PlyWritePolygons< Vertex >( Out.value , vertices , gtPolygons , Vertex::PlyWriteProperties() , Vertex::PlyWriteNum , ft , comments ) )
2019-03-19 19:47:43 -04:00
ERROR_OUT( "Could not write mesh to: " , Out.value );
2015-07-14 19:33:02 -04:00
return EXIT_SUCCESS;
}
2015-07-21 10:07:22 -04:00
int main( int argc , char* argv[] )
{
2018-04-14 23:00:29 -04:00
cmdLineParse( argc-1 , &argv[1] , params );
messageWriter.echoSTDOUT = Verbose.set;
2015-07-21 10:07:22 -04:00
if( !In.set || !Trim.set )
{
ShowUsage( argv[0] );
return EXIT_FAILURE;
}
2018-05-13 21:39:47 -04:00
typedef MultiPointStreamData< float , PointStreamValue< float > , PointStreamNormal< float , DIMENSION > , PointStreamColor< float > > VertexData;
typedef PlyVertexWithData< float , DIMENSION , VertexData > Vertex;
bool readFlags[ Vertex::PlyReadNum ];
2019-03-19 19:47:43 -04:00
if( !PlyReadHeader( In.value , Vertex::PlyReadProperties() , Vertex::PlyReadNum , readFlags ) ) ERROR_OUT( "Failed to read ply header: " , In.value );
2015-07-21 10:07:22 -04:00
2018-05-13 21:39:47 -04:00
bool hasValue = VertexData::ValidPlyReadProperties< 0 >( readFlags + DIMENSION );
bool hasNormal = VertexData::ValidPlyReadProperties< 1 >( readFlags + DIMENSION );
bool hasColor = VertexData::ValidPlyReadProperties< 2 >( readFlags + DIMENSION );
2015-07-14 19:33:02 -04:00
if( !hasValue ) ERROR_OUT( "Ply file does not contain values" );
2018-05-13 21:39:47 -04:00
2019-03-19 19:47:43 -04:00
if( Long.set )
if( hasColor )
if( hasNormal ) return Execute< long long , PointStreamNormal< float , DIMENSION > , PointStreamColor< float > >();
else return Execute< long long , PointStreamColor< float > >();
else
if( hasNormal ) return Execute< long long , PointStreamNormal< float , DIMENSION > >();
else return Execute< long long >();
2018-05-13 21:39:47 -04:00
else
2019-03-19 19:47:43 -04:00
if( hasColor )
if( hasNormal ) return Execute< int , PointStreamNormal< float , DIMENSION > , PointStreamColor< float > >();
else return Execute< int , PointStreamColor< float > >();
else
if( hasNormal ) return Execute< int , PointStreamNormal< float , DIMENSION > >();
else return Execute< int >();
2015-07-21 10:07:22 -04:00
}