| 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 NEW_CHUNKS |
| 32 |
#define DISABLE_PARALLELIZATION |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <algorithm> |
| 37 |
#include <limits> |
| 38 |
#include <sstream> |
| 39 |
#include <unordered_map> |
| 40 |
#include "MyMiscellany.h" |
| 41 |
#include "CmdLineParser.h" |
| 42 |
#include "Geometry.h" |
| 43 |
#include "Ply.h" |
| 44 |
#include "DataStream.h" |
| 45 |
#include "VertexFactory.h" |
| 46 |
#include "DataStream.imp.h" |
| 47 |
|
| 48 |
using namespace PoissonRecon; |
| 49 |
|
| 50 |
CmdLineParameters< char* > In( "in" ); |
| 51 |
CmdLineParameter< char* > Out( "out" ); |
| 52 |
CmdLineParameter< float > Width( "width" , -1.f ) , PadRadius( "radius" , 0.f ); |
| 53 |
CmdLineParameterArray< float , 6 > BoundingBox( "bBox" ); |
| 54 |
CmdLineParameters< Point< float , 4 > > HalfSpaces( "halfSpaces" ); |
| 55 |
CmdLineReadable ASCII( "ascii" ) , Verbose( "verbose" ) , NoNormals( "noNormals" ) , Colors( "colors" ) , Values( "values" ); |
| 56 |
|
| 57 |
CmdLineReadable* params[] = { &In , &Out , &Width , &PadRadius , &ASCII , &Verbose , &BoundingBox , &NoNormals , &Colors , &Values , &HalfSpaces , NULL }; |
| 58 |
|
| 59 |
void ShowUsage( char* ex ) |
| 60 |
{ |
| 61 |
printf( "Usage: %s\n" , ex ); |
| 62 |
printf( "\t --%s <input geometry file count, geometry file #1, geometry file #2, ... >\n" , In.name ); |
| 63 |
printf( "\t[--%s <output ply file name/header>]\n" , Out.name ); |
| 64 |
printf( "\t[--%s <chunk width>=%f]\n" , Width.name , Width.value ); |
| 65 |
printf( "\t[--%s <padding radius (as a fraction of the width)>=%f]\n" , PadRadius.name , PadRadius.value ); |
| 66 |
printf( "\t[--%s <minx miny minz maxx maxy maxz>]\n" , BoundingBox.name ); |
| 67 |
printf( "\t[--%s <half-space num, {x1,y1,z1,o1}, ..., {xn,yn,zn,on}>]\n" , HalfSpaces.name ); |
| 68 |
printf( "\t[--%s]\n" , NoNormals.name ); |
| 69 |
printf( "\t[--%s]\n" , Colors.name ); |
| 70 |
printf( "\t[--%s]\n" , Values.name ); |
| 71 |
printf( "\t[--%s]\n" , ASCII.name ); |
| 72 |
printf( "\t[--%s]\n" , Verbose.name ); |
| 73 |
} |
| 74 |
|
| 75 |
void PrintBoundingBox( Point< float , 3 > min , Point< float , 3 > max ) |
| 76 |
{ |
| 77 |
printf( "[" ); |
| 78 |
for( unsigned int d=0 ; d<3 ; d++ ) printf( " %f" , min[d] ); |
| 79 |
printf( " ] [" ); |
| 80 |
for( unsigned int d=0 ; d<3 ; d++ ) printf( " %f" , max[d] ); |
| 81 |
printf( " ] ->" ); |
| 82 |
for( unsigned int d=0 ; d<3 ; d++ ) printf( " %.2e" , max[d]-min[d] ); |
| 83 |
printf( " ]" ); |
| 84 |
} |
| 85 |
|
| 86 |
template< typename Real , unsigned int Dim , typename VertexDataFactory > |
| 87 |
using FullVertexFactory = VertexFactory::Factory< Real , VertexFactory::PositionFactory< Real , Dim > , VertexDataFactory >; |
| 88 |
template< typename Real , unsigned int Dim , typename VertexDataFactory > |
| 89 |
using VertexType = typename FullVertexFactory< Real , Dim , VertexDataFactory >::VertexType; |
| 90 |
|
| 91 |
|
| 92 |
template< typename VertexDataFactory > |
| 93 |
void Read( char *const *fileNames , unsigned int fileNum , VertexDataFactory vertexDataFactory , std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , std::vector< std::vector< long long > > &polygons , int &ft , std::vector< std::string > &comments ) |
| 94 |
{ |
| 95 |
typedef float Real; |
| 96 |
static constexpr unsigned int Dim = 3; |
| 97 |
FullVertexFactory< Real , Dim , VertexDataFactory > vertexFactory( VertexFactory::PositionFactory< Real , Dim >() , vertexDataFactory ); |
| 98 |
|
| 99 |
for( unsigned int i=0 ; i<fileNum ; i++ ) |
| 100 |
{ |
| 101 |
std::vector< VertexType< Real , Dim , VertexDataFactory > > _vertices; |
| 102 |
std::vector< std::vector< long long > > _polygons; |
| 103 |
char *ext = GetFileExtension( fileNames[i] ); |
| 104 |
|
| 105 |
if( !strcasecmp( ext , "ply" ) ) PLY::ReadPolygons( fileNames[i] , vertexFactory , _vertices , _polygons , ft , comments ); |
| 106 |
else |
| 107 |
{ |
| 108 |
InputDataStream< VertexType< Real , Dim , VertexDataFactory > > *pointStream; |
| 109 |
if( !strcasecmp( ext , "bnpts" ) ) pointStream = new BinaryInputDataStream< FullVertexFactory< Real , Dim , VertexDataFactory > >( fileNames[i] , vertexFactory ); |
| 110 |
else pointStream = new ASCIIInputDataStream< FullVertexFactory< Real , Dim , VertexDataFactory > >( fileNames[i] , vertexFactory ); |
| 111 |
size_t count = 0; |
| 112 |
VertexType< Real , Dim , VertexDataFactory > v = vertexFactory(); |
| 113 |
while( pointStream->read( v ) ) count++; |
| 114 |
pointStream->reset(); |
| 115 |
_vertices.resize( count , v ); |
| 116 |
comments.resize( 0 ); |
| 117 |
ft = PLY_BINARY_NATIVE; |
| 118 |
|
| 119 |
count = 0; |
| 120 |
while( pointStream->read( _vertices[count++] ) ); |
| 121 |
delete pointStream; |
| 122 |
} |
| 123 |
delete[] ext; |
| 124 |
|
| 125 |
long long vOffset = (long long)vertices.size(); |
| 126 |
|
| 127 |
vertices.reserve( vertices.size() + _vertices.size() ); |
| 128 |
for( int i=0 ; i<_vertices.size() ; i++ ) vertices.push_back( _vertices[i] ); |
| 129 |
|
| 130 |
polygons.reserve( polygons.size() + _polygons.size() ); |
| 131 |
for( int i=0 ; i<_polygons.size() ; i++ ) |
| 132 |
{ |
| 133 |
for( int j=0 ; j<_polygons[i].size() ; j++ ) _polygons[i][j] += vOffset; |
| 134 |
polygons.push_back( _polygons[i] ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
template< typename VertexDataFactory > |
| 140 |
void WritePoints( const char *fileName , int ft , VertexDataFactory vertexDataFactory , const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , const std::vector< std::string > &comments ) |
| 141 |
{ |
| 142 |
typedef float Real; |
| 143 |
static constexpr unsigned int Dim = 3; |
| 144 |
FullVertexFactory< Real , Dim , VertexDataFactory > vertexFactory( VertexFactory::PositionFactory< Real , Dim >() , vertexDataFactory ); |
| 145 |
char *ext = GetFileExtension( fileName ); |
| 146 |
|
| 147 |
OutputDataStream< VertexType< Real , Dim , VertexDataFactory > > *pointStream; |
| 148 |
|
| 149 |
if ( !strcasecmp( ext , "ply" ) ) pointStream = new PLYOutputDataStream< FullVertexFactory< Real , Dim , VertexDataFactory > >( fileName , vertexFactory , vertices.size() , ft ); |
| 150 |
else if( !strcasecmp( ext , "bnpts" ) ) pointStream = new BinaryOutputDataStream< FullVertexFactory< Real , Dim , VertexDataFactory > >( fileName , vertexFactory ); |
| 151 |
else pointStream = new ASCIIOutputDataStream< FullVertexFactory< Real , Dim , VertexDataFactory > >( fileName , vertexFactory ); |
| 152 |
for( size_t i=0 ; i<vertices.size() ; i++ ) pointStream->write( vertices[i] ); |
| 153 |
delete pointStream; |
| 154 |
|
| 155 |
delete[] ext; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
template< typename VertexDataFactory > |
| 160 |
void WriteMesh( const char *fileName , int ft , VertexDataFactory vertexDataFactory , const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , const std::vector< std::vector< long long > > &polygons , const std::vector< std::string > &comments ) |
| 161 |
{ |
| 162 |
typedef float Real; |
| 163 |
static constexpr unsigned int Dim = 3; |
| 164 |
FullVertexFactory< Real , Dim , VertexDataFactory > vertexFactory( VertexFactory::PositionFactory< Real , Dim >() , vertexDataFactory ); |
| 165 |
|
| 166 |
char *ext = GetFileExtension( fileName ); |
| 167 |
if( strcasecmp( ext , "ply" ) ) MK_THROW( "Can only output mesh to .ply file" ); |
| 168 |
delete[] ext; |
| 169 |
|
| 170 |
if( vertices.size()>std::numeric_limits< int >::max() ) |
| 171 |
{ |
| 172 |
if( vertices.size()>std::numeric_limits< unsigned int >::max() ) MK_THROW( "more vertices than can be indexed by an unsigned int: %llu" , (unsigned long long)vertices.size() ); |
| 173 |
MK_WARN( "more vertices than can be indexed by an int, using unsigned int instead: %llu" , (unsigned long long)vertices.size() ); |
| 174 |
std::vector< std::vector< unsigned int > > outPolygons; |
| 175 |
outPolygons.resize( polygons.size() ); |
| 176 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 177 |
{ |
| 178 |
outPolygons[i].resize( polygons[i].size() ); |
| 179 |
for( int j=0 ; j<polygons[i].size() ; j++ ) outPolygons[i][j] = (unsigned int)polygons[i][j]; |
| 180 |
} |
| 181 |
PLY::WritePolygons( fileName , vertexFactory , vertices , outPolygons , ft , comments ); |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
std::vector< std::vector< int > > outPolygons; |
| 186 |
outPolygons.resize( polygons.size() ); |
| 187 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 188 |
{ |
| 189 |
outPolygons[i].resize( polygons[i].size() ); |
| 190 |
for( int j=0 ; j<polygons[i].size() ; j++ ) outPolygons[i][j] = (int)polygons[i][j]; |
| 191 |
} |
| 192 |
PLY::WritePolygons( fileName , vertexFactory , vertices , outPolygons , ft , comments ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
template< typename VertexDataFactory > |
| 197 |
void GetBoundingBox( const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , Point< float , 3 > &min , Point< float , 3 > &max ) |
| 198 |
{ |
| 199 |
min = max = vertices[0].template get<0>(); |
| 200 |
for( size_t i=0 ; i<vertices.size() ; i++ ) for( unsigned int d=0 ; d<3 ; d++ ) |
| 201 |
{ |
| 202 |
min[d] = std::min< float >( min[d] , vertices[i].template get<0>()[d] ); |
| 203 |
max[d] = std::max< float >( max[d] , vertices[i].template get<0>()[d] ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
template< typename VertexDataFactory > |
| 208 |
void GetSubPoints( const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , Point< float , 3 > min , Point< float , 3 > max , std::vector< VertexType< float , 3 , VertexDataFactory > > &subVertices ) |
| 209 |
{ |
| 210 |
subVertices.resize( 0 ); |
| 211 |
|
| 212 |
for( size_t i=0 ; i<vertices.size() ; i++ ) |
| 213 |
{ |
| 214 |
bool inside = true; |
| 215 |
for( unsigned int d=0 ; d<3 ; d++ ) if( vertices[i].template get<0>()[d]<min[d] || vertices[i].template get<0>()[d]>=max[d] ) inside = false; |
| 216 |
if( inside ) subVertices.push_back( vertices[i] ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
template< typename VertexDataFactory > |
| 221 |
void GetSubVertices( const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , std::vector< std::vector< long long > > &polygons , std::vector< VertexType< float , 3 , VertexDataFactory > > &subVertices ) |
| 222 |
{ |
| 223 |
subVertices.resize( 0 ); |
| 224 |
long long count = 0; |
| 225 |
std::unordered_map< long long , long long > vMap; |
| 226 |
for( size_t i=0 ; i<polygons.size() ; i++ ) for( size_t j=0 ; j<polygons[i].size() ; j++ ) |
| 227 |
{ |
| 228 |
auto iter = vMap.find( polygons[i][j] ); |
| 229 |
if( iter==vMap.end() ) vMap[ polygons[i][j] ] = count++; |
| 230 |
} |
| 231 |
|
| 232 |
subVertices.resize( vMap.size() ); |
| 233 |
|
| 234 |
for( size_t i=0 ; i<polygons.size() ; i++ ) for( size_t j=0 ; j<polygons[i].size() ; j++ ) |
| 235 |
{ |
| 236 |
long long oldIdx = polygons[i][j]; |
| 237 |
long long newIdx = vMap[ oldIdx ]; |
| 238 |
subVertices[ newIdx ] = vertices[ oldIdx ]; |
| 239 |
polygons[i][j] = newIdx; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
template< typename VertexDataFactory > |
| 244 |
void GetSubMesh( const std::vector< VertexType< float , 3 , VertexDataFactory > > &vertices , const std::vector< std::vector< long long > > &polygons , Point< float , 3 > min , Point< float , 3 > max , std::vector< VertexType< float , 3 , VertexDataFactory > > &subVertices , std::vector< std::vector< long long > > &subPolygons ) |
| 245 |
{ |
| 246 |
subPolygons.resize( 0 ); |
| 247 |
|
| 248 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 249 |
{ |
| 250 |
Point< float , 3 > center; |
| 251 |
for( size_t j=0 ; j<polygons[i].size() ; j++ ) center += vertices[ polygons[i][j] ].template get<0>(); |
| 252 |
center /= (float)polygons[i].size(); |
| 253 |
bool inside = true; |
| 254 |
for( unsigned int d=0 ; d<3 ; d++ ) if( center[d]<min[d] || center[d]>=max[d] ) inside = false; |
| 255 |
if( inside ) subPolygons.push_back( polygons[i] ); |
| 256 |
} |
| 257 |
|
| 258 |
GetSubVertices( vertices , subPolygons , subVertices ); |
| 259 |
} |
| 260 |
|
| 261 |
template< typename VertexDataFactory > |
| 262 |
void Execute( VertexDataFactory vertexDataFactory ) |
| 263 |
{ |
| 264 |
typedef float Real; |
| 265 |
static constexpr unsigned int Dim = 3; |
| 266 |
Timer timer; |
| 267 |
double t = Time(); |
| 268 |
std::vector< VertexType< Real , Dim , VertexDataFactory > > vertices; |
| 269 |
std::vector< std::vector< long long > > polygons; |
| 270 |
|
| 271 |
int ft; |
| 272 |
std::vector< std::string > comments; |
| 273 |
Read( In.values , In.count , vertexDataFactory , vertices , polygons , ft , comments ); |
| 274 |
printf( "Vertices / Polygons: %llu / %llu\n" , (unsigned long long)vertices.size() , (unsigned long long)polygons.size() ); |
| 275 |
printf( "Time (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 276 |
printf( "Peak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 277 |
|
| 278 |
Point< float , 3 > min , max; |
| 279 |
GetBoundingBox< VertexDataFactory >( vertices , min , max ); |
| 280 |
PrintBoundingBox( min , max ) ; printf( "\n" ); |
| 281 |
|
| 282 |
float width = Width.value; |
| 283 |
|
| 284 |
|
| 285 |
if( BoundingBox.set || HalfSpaces.set ) |
| 286 |
{ |
| 287 |
Point< float , 3 > min , max; |
| 288 |
|
| 289 |
if( BoundingBox.set ) |
| 290 |
{ |
| 291 |
min = Point< float , 3 >( BoundingBox.values[0] , BoundingBox.values[1] , BoundingBox.values[2] ); |
| 292 |
max = Point< float , 3 >( BoundingBox.values[3] , BoundingBox.values[4] , BoundingBox.values[5] ); |
| 293 |
} |
| 294 |
|
| 295 |
auto Inside = [&]( Point< float , 3 > p ) |
| 296 |
{ |
| 297 |
bool inside = true; |
| 298 |
if( BoundingBox.set ) inside &= p[0]>=min[0] && p[0]<max[0] && p[1]>=min[1] && p[1]<max[1] && p[2]>=min[2] && p[2]<max[2]; |
| 299 |
if( inside && HalfSpaces.set ) |
| 300 |
{ |
| 301 |
Point< float , 4 > _p( p[0] , p[1] , p[2] , 1.f ); |
| 302 |
for( unsigned int i=0 ; i<(unsigned int)HalfSpaces.count ; i++ ) inside &= Point< float , 4 >::Dot( _p , HalfSpaces.values[i] )<=0; |
| 303 |
} |
| 304 |
return inside; |
| 305 |
}; |
| 306 |
|
| 307 |
if( polygons.size() ) |
| 308 |
{ |
| 309 |
std::vector< std::vector< long long > > _polygons; |
| 310 |
|
| 311 |
Timer timer; |
| 312 |
#ifdef NEW_CHUNKS |
| 313 |
{ |
| 314 |
size_t polygonCount = 0; |
| 315 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 316 |
{ |
| 317 |
Point< float , 3 > center; |
| 318 |
for( int j=0 ; j<polygons[i].size() ; j++ ) center += vertices[ polygons[i][j] ].template get<0>(); |
| 319 |
center /= polygons[i].size(); |
| 320 |
if( Inside( center ) ) polygonCount++; |
| 321 |
} |
| 322 |
_polygons.reserve( polygonCount ); |
| 323 |
} |
| 324 |
#endif // NEW_CHUNKS |
| 325 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 326 |
{ |
| 327 |
Point< float , 3 > center; |
| 328 |
for( int j=0 ; j<polygons[i].size() ; j++ ) center += vertices[ polygons[i][j] ].template get<0>(); |
| 329 |
center /= polygons[i].size(); |
| 330 |
if( Inside( center ) ) _polygons.push_back( polygons[i] ); |
| 331 |
} |
| 332 |
printf( "\tChunked polygons:\n" ); |
| 333 |
printf( "\t\tTime (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 334 |
printf( "\t\tPeak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 335 |
|
| 336 |
if( Out.set ) |
| 337 |
if( _polygons.size() ) |
| 338 |
{ |
| 339 |
std::vector< VertexType< Real , Dim , VertexDataFactory > > _vertices; |
| 340 |
GetSubVertices< VertexDataFactory >( vertices , _polygons , _vertices ); |
| 341 |
|
| 342 |
if( Verbose.set ) |
| 343 |
{ |
| 344 |
printf( "\t\t%s\n" , Out.value ); |
| 345 |
printf( "\t\t\tVertices / Polygons: %llu / %llu\n" , (unsigned long long)_vertices.size() , (unsigned long long)_polygons.size() ); |
| 346 |
} |
| 347 |
|
| 348 |
WriteMesh( Out.value , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , _vertices , _polygons , comments ); |
| 349 |
} |
| 350 |
else MK_WARN( "no polygons in bounding box" ); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
std::vector< VertexType< Real , Dim , VertexDataFactory > > _vertices; |
| 355 |
|
| 356 |
Timer timer; |
| 357 |
#ifdef NEW_CHUNKS |
| 358 |
{ |
| 359 |
size_t vertexCount = 0; |
| 360 |
for( size_t i=0 ; i<vertices.size() ; i++ ) if( Inside( vertices[i].template get<0>() ) ) vertexCount++; |
| 361 |
_vertices.reserve( vertexCount ); |
| 362 |
} |
| 363 |
#endif // NEW_CHUNKS |
| 364 |
|
| 365 |
for( size_t i=0 ; i<vertices.size() ; i++ ) if( Inside( vertices[i].template get<0>() ) ) _vertices.push_back( vertices[i] ); |
| 366 |
printf( "\tChunked vertices:\n" ); |
| 367 |
printf( "\t\tTime (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 368 |
printf( "\t\tPeak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 369 |
|
| 370 |
if( Out.set ) |
| 371 |
if( _vertices.size() ) |
| 372 |
{ |
| 373 |
if( Verbose.set ) |
| 374 |
{ |
| 375 |
printf( "\t\t%s\n" , Out.value ); |
| 376 |
printf( "\t\t\tPoints: %llu\n" , (unsigned long long)_vertices.size() ); |
| 377 |
} |
| 378 |
|
| 379 |
WritePoints( Out.value , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , _vertices , comments ); |
| 380 |
} |
| 381 |
else MK_WARN( "no vertices in bounding box" ); |
| 382 |
} |
| 383 |
} |
| 384 |
else if( width>0 ) |
| 385 |
{ |
| 386 |
float radius = PadRadius.value * width; |
| 387 |
size_t vCount=0 , pCount=0; |
| 388 |
for( unsigned int d=0 ; d<3 ; d++ ) min[d] -= radius , max[d] += radius; |
| 389 |
for( unsigned int d=0 ; d<3 ; d++ ) min[d] -= width/10000.f , max[d] += width/10000.f; |
| 390 |
int begin[] = { (int)floor( min[0]/width ) , (int)floor( min[1]/width ) , (int)floor( min[2]/width ) }; |
| 391 |
int end [] = { (int)ceil ( max[0]/width ) , (int)ceil ( max[1]/width ) , (int)ceil ( max[2]/width ) }; |
| 392 |
int size [] = { end[0]-begin[0]+1 , end[1]-begin[1]+1 , end[2]-begin[2]+1 }; |
| 393 |
struct Range{ int begin[3] , end[3]; }; |
| 394 |
auto SetRange = [&]( Point< float , 3 > p , Range &range ) |
| 395 |
{ |
| 396 |
for( int d=0 ; d<3 ; d++ ) |
| 397 |
{ |
| 398 |
range.begin[d] = (int)floor( (p[d]-radius)/width ) , range.end[d] = (int)ceil( (p[d]+radius)/width ); |
| 399 |
if( range.begin[d]==range.end[d] ) range.end[d]++; |
| 400 |
} |
| 401 |
}; |
| 402 |
auto Index1D = [&]( int x , int y , int z ) |
| 403 |
{ |
| 404 |
x -= begin[0] , y -= begin[1] , z -= begin[2]; |
| 405 |
return x + y*size[0] + z*size[0]*size[1]; |
| 406 |
}; |
| 407 |
auto Index3D = [&]( int idx , int &x , int &y , int &z ) |
| 408 |
{ |
| 409 |
x = idx % size[0]; |
| 410 |
idx /= size[0]; |
| 411 |
y = idx % size[1]; |
| 412 |
idx /= size[1]; |
| 413 |
z = idx % size[2]; |
| 414 |
x += begin[0] , y += begin[1] , z += begin[2]; |
| 415 |
}; |
| 416 |
|
| 417 |
if( polygons.size() ) |
| 418 |
{ |
| 419 |
std::vector< std::vector< std::vector< long long > > > _polygons( size[0]*size[1]*size[2] ); |
| 420 |
Range range; |
| 421 |
|
| 422 |
Timer timer; |
| 423 |
#ifdef NEW_CHUNKS |
| 424 |
{ |
| 425 |
std::vector< size_t > polygonCounts( size[0]*size[1]*size[2] , 0 ); |
| 426 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 427 |
{ |
| 428 |
Point< float , 3 > center; |
| 429 |
for( int j=0 ; j<polygons[i].size() ; j++ ) center += vertices[ polygons[i][j] ].template get<0>(); |
| 430 |
center /= polygons[i].size(); |
| 431 |
SetRange( center , range ); |
| 432 |
for( int x=range.begin[0] ; x<range.end[0] ; x++ ) for( int y=range.begin[1] ; y<range.end[1] ; y++ ) for( int z=range.begin[2] ; z<range.end[2] ; z++ ) |
| 433 |
polygonCounts[ Index1D(x,y,z) ]++; |
| 434 |
} |
| 435 |
for( size_t i=0 ; i<polygonCounts.size() ; i++ ) _polygons[i].reserve( polygonCounts[i] ); |
| 436 |
} |
| 437 |
#endif // NEW_CHUNKS |
| 438 |
for( size_t i=0 ; i<polygons.size() ; i++ ) |
| 439 |
{ |
| 440 |
Point< float , 3 > center; |
| 441 |
for( int j=0 ; j<polygons[i].size() ; j++ ) center += vertices[ polygons[i][j] ].template get<0>(); |
| 442 |
center /= polygons[i].size(); |
| 443 |
SetRange( center , range ); |
| 444 |
for( int x=range.begin[0] ; x<range.end[0] ; x++ ) for( int y=range.begin[1] ; y<range.end[1] ; y++ ) for( int z=range.begin[2] ; z<range.end[2] ; z++ ) |
| 445 |
_polygons[ Index1D(x,y,z) ].push_back( polygons[i] ); |
| 446 |
} |
| 447 |
printf( "\tChunked polygons:\n" ); |
| 448 |
printf( "\t\tTime (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 449 |
printf( "\t\tPeak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 450 |
|
| 451 |
if( Out.set ) |
| 452 |
{ |
| 453 |
#ifdef DISABLE_PARALLELIZATION |
| 454 |
#else // !DISABLE_PARALLELIZATION |
| 455 |
#pragma omp parallel for |
| 456 |
#endif // DISABLE_PARALLELIZATION |
| 457 |
for( int i=0 ; i<_polygons.size() ; i++ ) if( _polygons[i].size() ) |
| 458 |
{ |
| 459 |
std::vector< VertexType< Real , Dim , VertexDataFactory > > _vertices; |
| 460 |
GetSubVertices< VertexDataFactory >( vertices , _polygons[i] , _vertices ); |
| 461 |
std::stringstream stream; |
| 462 |
int x , y , z; |
| 463 |
Index3D( i , x , y , z ); |
| 464 |
|
| 465 |
stream << Out.value << "." << x << "." << y << "." << z << ".ply"; |
| 466 |
|
| 467 |
|
| 468 |
Point< float , 3 > min , max; |
| 469 |
min = Point< float , 3 >( x+0 , y+0 , z+0 ) * width; |
| 470 |
max = Point< float , 3 >( x+1 , y+1 , z+1 ) * width; |
| 471 |
if( Verbose.set ) |
| 472 |
{ |
| 473 |
static std::mutex mutex; |
| 474 |
std::lock_guard< std::mutex > lock( mutex ); |
| 475 |
printf( "\t\t%s\n" , stream.str().c_str() ); |
| 476 |
printf( "\t\t\tVertices / Polygons: %llu / %llu\n" , (unsigned long long)_vertices.size() , (unsigned long long)_polygons[i].size() ); |
| 477 |
printf( "\t\t\t" ) ; PrintBoundingBox( min , max ) ; printf( "\n" ); |
| 478 |
} |
| 479 |
|
| 480 |
WriteMesh( stream.str().c_str() , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , _vertices , _polygons[i] , comments ); |
| 481 |
vCount += _vertices.size() , pCount += _polygons[i].size(); |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
else |
| 486 |
{ |
| 487 |
std::vector< std::vector< VertexType< Real , Dim , VertexDataFactory > > > _vertices( size[0]*size[1]*size[2] ); |
| 488 |
Range range; |
| 489 |
|
| 490 |
Timer timer; |
| 491 |
#ifdef NEW_CHUNKS |
| 492 |
{ |
| 493 |
std::vector< size_t > vertexCounts( size[0]*size[1]*size[2] , 0 ); |
| 494 |
for( size_t i=0 ; i<vertices.size() ; i++ ) |
| 495 |
{ |
| 496 |
SetRange( vertices[i].template get<0>() , range ); |
| 497 |
for( int x=range.begin[0] ; x<range.end[0] ; x++ ) for( int y=range.begin[1] ; y<range.end[1] ; y++ ) for( int z=range.begin[2] ; z<range.end[2] ; z++ ) |
| 498 |
vertexCounts[ Index1D(x,y,z) ]++; |
| 499 |
} |
| 500 |
for( size_t i=0 ; i<vertexCounts.size() ; i++ ) _vertices[i].reserve( vertexCounts[i] ); |
| 501 |
} |
| 502 |
#endif // NEW_CHUNKS |
| 503 |
|
| 504 |
for( size_t i=0 ; i<vertices.size() ; i++ ) |
| 505 |
{ |
| 506 |
SetRange( vertices[i].template get<0>() , range ); |
| 507 |
for( int x=range.begin[0] ; x<range.end[0] ; x++ ) for( int y=range.begin[1] ; y<range.end[1] ; y++ ) for( int z=range.begin[2] ; z<range.end[2] ; z++ ) |
| 508 |
_vertices[ Index1D(x,y,z) ].push_back( vertices[i] ); |
| 509 |
} |
| 510 |
printf( "\tChunked vertices:\n" ); |
| 511 |
printf( "\t\tTime (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 512 |
printf( "\t\tPeak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 513 |
|
| 514 |
if( Out.set ) |
| 515 |
{ |
| 516 |
#ifdef DISABLE_PARALLELIZATION |
| 517 |
#else // !DISABLE_PARALLELIZATION |
| 518 |
#pragma omp parallel for |
| 519 |
#endif // DISABLE_PARALLELIZATION |
| 520 |
for( int i=0 ; i<_vertices.size() ; i++ ) if( _vertices[i].size() ) |
| 521 |
{ |
| 522 |
std::stringstream stream; |
| 523 |
int x , y , z; |
| 524 |
Index3D( i , x , y , z ); |
| 525 |
stream << Out.value << "." << x << "." << y << "." << z << ".ply"; |
| 526 |
Point< float , 3 > min , max; |
| 527 |
min = Point< float , 3 >( x+0 , y+0 , z+0 ) * width; |
| 528 |
max = Point< float , 3 >( x+1 , y+1 , z+1 ) * width; |
| 529 |
|
| 530 |
if( Verbose.set ) |
| 531 |
{ |
| 532 |
static std::mutex mutex; |
| 533 |
std::lock_guard< std::mutex > lock( mutex ); |
| 534 |
printf( "\t\t%s\n" , stream.str().c_str() ); |
| 535 |
printf( "\t\t\tPoints: %llu\n" , (unsigned long long)_vertices[i].size() ); |
| 536 |
printf( "\t\t\t" ) ; PrintBoundingBox( min , max ) ; printf( "\n" ); |
| 537 |
} |
| 538 |
|
| 539 |
WritePoints( stream.str().c_str() , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , _vertices[i] , comments ); |
| 540 |
vCount += _vertices[i].size(); |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
if( !radius ) |
| 545 |
{ |
| 546 |
if( polygons.size() ) |
| 547 |
{ |
| 548 |
if( pCount!=polygons.size() ) MK_WARN( "polygon counts don't match: " , polygons.size() , " != " , pCount ); |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
if( vCount!=vertices.size() ) MK_WARN( "vertex counts don't match:" , vertices.size() , " != " , vCount ); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
else |
| 557 |
{ |
| 558 |
if( Out.set ) |
| 559 |
{ |
| 560 |
if( polygons.size() ) WriteMesh( Out.value , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , vertices , polygons , comments ); |
| 561 |
else WritePoints( Out.value , ASCII.set ? PLY_ASCII : ft , vertexDataFactory , vertices , comments ); |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
int main( int argc , char* argv[] ) |
| 566 |
{ |
| 567 |
typedef float Real; |
| 568 |
static constexpr unsigned int Dim = 3; |
| 569 |
|
| 570 |
CmdLineParse( argc-1 , &argv[1] , params ); |
| 571 |
#ifdef ARRAY_DEBUG |
| 572 |
MK_WARN( "Array debugging enabled" ); |
| 573 |
#endif // ARRAY_DEBUG |
| 574 |
|
| 575 |
if( !In.set ) |
| 576 |
{ |
| 577 |
ShowUsage( argv[0] ); |
| 578 |
return EXIT_FAILURE; |
| 579 |
} |
| 580 |
Timer timer; |
| 581 |
|
| 582 |
bool isPly; |
| 583 |
for( int i=0 ; i<In.count ; i++ ) |
| 584 |
{ |
| 585 |
char *ext = GetFileExtension( In.values[i] ); |
| 586 |
bool _isPly = strcasecmp( ext , "ply" )==0; |
| 587 |
if( !i ) isPly = _isPly; |
| 588 |
else if( isPly!=_isPly ) MK_THROW( "All files must be of the same type" ); |
| 589 |
delete[] ext; |
| 590 |
} |
| 591 |
if( isPly ) |
| 592 |
{ |
| 593 |
VertexFactory::PositionFactory< Real , Dim > factory; |
| 594 |
bool *readFlags = new bool[ factory.plyReadNum() ]; |
| 595 |
VertexFactory::DynamicFactory< Real > *remainingProperties = NULL; |
| 596 |
for( int i=0 ; i<In.count ; i++ ) |
| 597 |
{ |
| 598 |
std::vector< PlyProperty > unprocessedProperties; |
| 599 |
PLY::ReadVertexHeader( In.values[i] , factory , readFlags , unprocessedProperties ); |
| 600 |
if( !factory.plyValidReadProperties( readFlags ) ) MK_THROW( "Ply file does not contain positions" ); |
| 601 |
VertexFactory::DynamicFactory< Real > _remainingProperties( unprocessedProperties ); |
| 602 |
if( !i ) remainingProperties = new VertexFactory::DynamicFactory< Real >( _remainingProperties ); |
| 603 |
else if( (*remainingProperties)!=(_remainingProperties) ) MK_THROW( "Remaining properties differ" ); |
| 604 |
} |
| 605 |
delete[] readFlags; |
| 606 |
if( !remainingProperties || !remainingProperties->size() ) Execute( VertexFactory::EmptyFactory< Real >() ); |
| 607 |
else Execute( *remainingProperties ); |
| 608 |
delete remainingProperties; |
| 609 |
} |
| 610 |
else |
| 611 |
{ |
| 612 |
if( Values.set ) |
| 613 |
if( Colors.set ) |
| 614 |
if( !NoNormals.set ) Execute( VertexFactory::Factory< Real , VertexFactory::ValueFactory< Real > , VertexFactory::NormalFactory< Real , Dim > , VertexFactory::RGBColorFactory< Real > >() ); |
| 615 |
else Execute( VertexFactory::Factory< Real , VertexFactory::ValueFactory< Real > , VertexFactory::RGBColorFactory< Real > >() ); |
| 616 |
else |
| 617 |
if( !NoNormals.set ) Execute( VertexFactory::Factory< Real , VertexFactory::ValueFactory< Real > , VertexFactory::NormalFactory< Real , Dim > >() ); |
| 618 |
else Execute( VertexFactory::ValueFactory< Real > () ); |
| 619 |
else |
| 620 |
if( Colors.set ) |
| 621 |
if( !NoNormals.set ) Execute( VertexFactory::Factory< Real , VertexFactory::NormalFactory< Real , Dim > , VertexFactory::RGBColorFactory< Real > >() ); |
| 622 |
else Execute( VertexFactory::RGBColorFactory< Real > () ); |
| 623 |
else |
| 624 |
if( !NoNormals.set ) Execute( VertexFactory::NormalFactory< Real , Dim > () ); |
| 625 |
else Execute( VertexFactory::EmptyFactory< Real >() ); |
| 626 |
} |
| 627 |
|
| 628 |
printf( "Time (Wall/CPU): %.2f / %.2f\n" , timer.wallTime() , timer.cpuTime() ); |
| 629 |
printf( "Peak Memory (MB): %d\n" , MemoryInfo::PeakMemoryUsageMB() ); |
| 630 |
|
| 631 |
return EXIT_SUCCESS; |
| 632 |
} |