Debian Codegraph @

src/thirdparty/PoissonRecon/PointsToDisks.cpp

raw
1 /*
2 Copyright (c) 2024, 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 #include <stdio.h>
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <algorithm>
35 #include <iostream>
36 #define _USE_MATH_DEFINES
37 #include <cmath>
38 #include "Ply.h"
39 #include "CmdLineParser.h"
40 #include "Geometry.h"
41 #include "VertexFactory.h"
42 #include "MyMiscellany.h"
43 #include "DataStream.imp.h"
44
45 using namespace PoissonRecon;
46
47 CmdLineParameter< std::string >
48 In( "in" ) ,
49 Out( "out" );
50
51 CmdLineParameter< unsigned int >
52 Res( "res" , 12 ) ,
53 PointsToKeep( "keep" );
54
55 CmdLineParameter< float >
56 Scale( "scale" , 0.005f ) ,
57 Fraction( "fraction" , 1.f );
58
59 CmdLineReadable
60 Verbose( "verbose" );
61
62 CmdLineParameter< float >
63 LengthToRadiusExponent( "lExp" , 0.66f );
64
65 CmdLineReadable* params[] =
66 {
67 &In ,
68 &Out ,
69 &Scale ,
70 &Res ,
71 &PointsToKeep ,
72 &Fraction ,
73 &LengthToRadiusExponent ,
74 &Verbose ,
75 NULL
76 };
77
78
79 void ShowUsage( char* ex )
80 {
81 printf( "Usage: %s\n" , ex );
82 printf( "\t --%s <input mesh>\n" , In.name );
83 printf( "\t --%s <ouBtput mesh>\n" , Out.name );
84 printf( "\t[--%s <disk size>=%f]\n" , Scale.name , Scale.value );
85 printf( "\t[--%s <disk resolution>=%d]\n" , Res.name , Res.value );
86 printf( "\t[--%s <points to keep>]\n" , PointsToKeep.name );
87 printf( "\t[--%s <fraction to keep>=%f]\n" , Fraction.name , Fraction.value );
88 printf( "\t[--%s <length to radius exponent>=%f]\n" , LengthToRadiusExponent.name , LengthToRadiusExponent.value );
89 printf( "\t[--%s]\n" , Verbose.name );
90 }
91
92 int main( int argc , char* argv[] )
93 {
94 auto SampleRadius = []( float length , float radius ){ return radius * (float)pow(length,0.66); };
95
96 auto NormalColor = []( Point< float , 3 > n )
97 {
98 Point< float , 3 > color =
99 (
100 ( ( n[0]<0 ) ? Point< float , 3 >( 0 , -n[0] , -n[0] ) : Point< float , 3 >( n[0] , 0 , 0 ) ) +
101 ( ( n[1]<0 ) ? Point< float , 3 >( -n[1] , 0 , -n[1] ) : Point< float , 3 >( 0 , n[1] , 0 ) ) +
102 ( ( n[2]<0 ) ? Point< float , 3 >( -n[2] , -n[2] , 0 ) : Point< float , 3 >( 0 , 0 , n[2] ) )
103 ) * 255;
104 color[0] = std::min< float >( 255.f , color[0] );
105 color[1] = std::min< float >( 255.f , color[1] );
106 color[2] = std::min< float >( 255.f , color[2] );
107 return color;
108 };
109
110 CmdLineParse( argc-1 , &argv[1] , params );
111 if( !In.set || !Out.set )
112 {
113 ShowUsage( argv [0] );
114 return EXIT_FAILURE;
115 }
116
117 if( Verbose.set )
118 {
119 std::cout << "***********************************************************" << std::endl;
120 std::cout << "***********************************************************" << std::endl;
121 std::cout << "** Running Points to Disks (Version " << ADAPTIVE_SOLVERS_VERSION << ") **" << std::endl;
122 std::cout << "***********************************************************" << std::endl;
123 std::cout << "***********************************************************" << std::endl;
124 }
125
126 if( PointsToKeep.set && Fraction.set )
127 {
128 MK_WARN( "One of --" , PointsToKeep.name , " and --" , Fraction.name , " should be set. Using --" , PointsToKeep.name );
129 Fraction.set = false;
130 }
131
132 using InFactory = VertexFactory::Factory< float , VertexFactory::PositionFactory< float , 3 > , VertexFactory::NormalFactory< float , 3 > , VertexFactory::RGBColorFactory< float > >;
133 using InVertex = typename InFactory::VertexType;
134 std::vector< InVertex > vertices;
135
136 bool hasNormals = true;
137 bool hasColors = false;
138 int fileType = PLY_BINARY_NATIVE;
139 {
140 std::vector< std::vector< int > > polygons;
141 char *ext = GetFileExtension( In.value.c_str() );
142
143 if( !strcasecmp( ext , "bnpts" ) )
144 {
145 using Factory = VertexFactory::Factory< float , VertexFactory::PositionFactory< float , 3 > , VertexFactory::NormalFactory< float , 3 > >;
146 using Vertex = typename Factory::VertexType;
147 Factory factory;
148 BinaryInputDataStream< Factory > pointStream( In.value.c_str() , factory );
149 Vertex v;
150 while( pointStream.read( v ) )
151 {
152 InVertex _v;
153 _v.template get<0>() = v.template get<0>();
154 _v.template get<1>() = v.template get<1>();
155 vertices.push_back( _v );
156 }
157 }
158 else if( !strcasecmp( ext , "ply" ) )
159 {
160 InFactory inFactory;
161 bool *readFlags = new bool[ inFactory.plyReadNum() ];
162 int file_type;
163 std::vector< std::string > comments;
164 PLY::ReadPolygons( In.value , inFactory , vertices , polygons , file_type , comments , readFlags );
165 hasNormals = readFlags[3] && readFlags[4] && readFlags[5];
166 hasColors = ( readFlags[6] && readFlags[7] && readFlags[8] ) || ( readFlags[9] && readFlags[10] && readFlags[11] );
167 delete[] readFlags;
168 }
169 else
170 {
171 using Factory = VertexFactory::Factory< float , VertexFactory::PositionFactory< float , 3 > , VertexFactory::NormalFactory< float , 3 > >;
172 using Vertex = typename Factory::VertexType;
173 Factory factory;
174 ASCIIInputDataStream< Factory > pointStream( In.value.c_str() , factory );
175 Vertex v;
176 while( pointStream.read( v ) )
177 {
178 InVertex _v;
179 _v.template get<0>() = v.template get<0>();
180 _v.template get<1>() = v.template get<1>();
181 vertices.push_back( _v );
182 }
183 }
184 delete[] ext;
185 }
186 if( Verbose.set ) std::cout << "Input points: " << vertices.size() << std::endl;
187 if( PointsToKeep.set && PointsToKeep.value>vertices.size() )
188 {
189 MK_WARN( "--" , PointsToKeep.name , " value exceeds number of points: " , PointsToKeep.value , " > " , vertices.size() );
190 PointsToKeep.value = (unsigned int)vertices.size();
191 }
192
193 if( !hasNormals ) MK_THROW( "Input is not oriented" );
194
195 if( PointsToKeep.set )
196 {
197 std::vector< InVertex > _vertices( PointsToKeep.value );
198 for( unsigned int i=0 ; i<PointsToKeep.value ; i++ )
199 {
200 int idx = (int)( ( i * vertices.size() ) / PointsToKeep.value );
201 _vertices[i] = vertices[idx];
202 }
203 vertices = _vertices;
204 }
205 else if( Fraction.set )
206 {
207 std::vector< InVertex > _vertices;
208 for( unsigned int i=0 ; i<vertices.size() ; i++ ) if( Random< float >()<=Fraction.value ) _vertices.push_back( vertices[i] );
209 vertices = _vertices;
210 }
211
212 Point< float , 3 > min , max;
213 min = max = vertices[0].template get<0>();
214 for( int i=0 ; i<vertices.size() ; i++ ) for( int j=0 ; j<3 ; j++ )
215 {
216 if( vertices[i].template get<0>()[j]<min[j] ) min[j] = vertices[i].template get<0>()[j];
217 if( vertices[i].template get<0>()[j]>max[j] ) max[j] = vertices[i].template get<0>()[j];
218 }
219 float radius = (float)sqrt( Point< float , 3 >::SquareNorm( max-min ) ) * Scale.value;
220 if( Verbose.set ) std::cout << "Scale -> Radius: " << Scale.value << " -> " << radius << std::endl;
221
222 {
223 std::vector< InVertex > verts;
224 std::vector< std::vector< int > > polygons;
225
226 verts.reserve( vertices.size() * Res.value );
227 polygons.reserve( vertices.size() );
228
229 auto ProcessPoint = [&]( Point< float , 3 > p , Point< float , 3 > n , Point< float , 3 > c )
230 {
231 unsigned int vIdx = (unsigned int)verts.size();
232 Point< float , 3 > v1 , v2;
233 double l = sqrt( Point< float , 3 >::SquareNorm( n ) );
234 if( !l ) return;
235 n /= (float)l;
236
237 float radiusScale = (float)pow( l , LengthToRadiusExponent.value );
238 v1 = Point< float , 3 >( -n[2] , 0 , n[0] );
239 if( Point< float , 3 >::SquareNorm( v1 )<0.0000001 ) v1 = Point< float , 3 >( 1 , 0 , 0 );
240 v2 = Point< float , 3 >::CrossProduct( n , v1 );
241 v1 /= sqrt( Point< float , 3 >::SquareNorm( v1 ) );
242 v2 /= sqrt( Point< float , 3 >::SquareNorm( v2 ) );
243
244 InVertex v;
245 v.template get<2>() = hasColors ? c : NormalColor( n );
246 v.template get<1>() = n;
247
248 for( unsigned int j=0 ; j<Res.value ; j++ )
249 {
250 double theta = 2.0 * M_PI * double(j) / Res.value;
251 v.template get<0>() = p + ( v1 * (float)cos(theta) + v2 * (float)sin(theta) ) * radius * radiusScale;
252 verts.push_back( v );
253 }
254 std::vector< int > polygon( Res.value );
255 for( unsigned int j=0 ; j<Res.value ; j++ ) polygon[j] = vIdx + j;
256 polygons.push_back( polygon );
257 };
258
259 for( unsigned int i=0 ; i<vertices.size() ; i++ ) ProcessPoint( vertices[i].template get<0>() , vertices[i].template get<1>() , vertices[i].template get<2>() );
260
261 InFactory inFactory;
262 std::vector< std::string > comments;
263 PLY::WritePolygons( Out.value , inFactory , verts , polygons, fileType , comments );
264 }
265 return EXIT_SUCCESS;
266 }