Debian Codegraph @

src/thirdparty/PoissonRecon/ScaleNormals.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 CmdLineParameters< std::string >
52 ConfidenceNames( "cNames" );
53
54 CmdLineParameters< float >
55 ConfidenceExponents( "cExps" );
56
57
58 CmdLineReadable
59 Verbose( "verbose" );
60
61 CmdLineReadable* params[] =
62 {
63 &In ,
64 &Out ,
65 &ConfidenceNames ,
66 &ConfidenceExponents ,
67 &Verbose ,
68 NULL
69 };
70
71
72 void ShowUsage( char* ex )
73 {
74 printf( "Usage: %s\n" , ex );
75 printf( "\t --%s <input points>\n" , In.name );
76 printf( "\t --%s <output points>\n" , Out.name );
77 printf( "\t --%s <confidence counts, confidence name1, confidence name2...>\n" , ConfidenceNames.name );
78 printf( "\t --%s <confidence counts, confidence exp1, confidence exp2...>\n" , ConfidenceExponents.name );
79 printf( "\t[--%s]\n" , Verbose.name );
80 }
81
82 template< typename Real , unsigned int Dim , typename Factory >
83 void Execute( Factory factory )
84 {
85 auto Scale = [&]( Point< Real > scales )
86 {
87 Real s = (Real)1.;
88 for( unsigned int d=0 ; d<scales.dim() ; d++ ) s *= (Real)pow( scales[d] , ConfidenceExponents.values[d] );
89 return s;
90 };
91
92 size_t count;
93 PLYInputDataStream< Factory > inPointStream( In.value.c_str() , factory , count );
94 PLYOutputDataStream< Factory > outPointStream( Out.value.c_str() , factory , count );
95
96 if( Verbose.set ) std::cout << "Points: " << count << std::endl;
97
98 typename Factory::VertexType v = factory();
99 while( inPointStream.read( v ) )
100 {
101 v.template get<1>() *= Scale( v.template get<2>() );
102 outPointStream.write( v );
103 }
104 };
105
106 template< typename Real , unsigned int Dim >
107 void Execute( void )
108 {
109 std::vector< std::pair< std::string , VertexFactory::TypeOnDisk > > namesAndTypesOnDisk( ConfidenceNames.count );
110 for( unsigned int i=0 ; i<ConfidenceNames.count ; i++ )
111 {
112 namesAndTypesOnDisk[i].first = ConfidenceNames.values[i];
113 namesAndTypesOnDisk[i].second = VertexFactory::template GetTypeOnDisk< Real >();
114 }
115
116 VertexFactory::PositionFactory< Real , Dim > positionFactory;
117 VertexFactory::NormalFactory< Real , Dim > normalFactory;
118 VertexFactory::DynamicFactory< Real > scaleFactory( namesAndTypesOnDisk );
119
120 typedef VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::NormalFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > > Factory;
121 Factory factory( positionFactory , normalFactory , scaleFactory );
122
123 bool *readFlags = new bool[ factory.plyReadNum() ];
124 std::vector< PlyProperty > unprocessedProperties;
125
126 PLY::ReadVertexHeader( In.value , factory , readFlags , unprocessedProperties );
127 if( !factory.template plyValidReadProperties<0>( readFlags ) ) MK_THROW( "Ply file does not contain positions" );
128 if( !factory.template plyValidReadProperties<1>( readFlags ) ) MK_THROW( "Ply file does not contain normals" );
129 if( !factory.template plyValidReadProperties<2>( readFlags ) ) MK_THROW( "Ply file does not contain scales" );
130 delete[] readFlags;
131
132 if( Verbose.set && unprocessedProperties.size() )
133 {
134 std::cout << "Unprocessed properties:" << std::endl;
135 for( unsigned int i=0 ; i<unprocessedProperties.size() ; i++ ) std::cout << "\t" << unprocessedProperties[i] << std::endl;
136 }
137
138 if( unprocessedProperties.size() )
139 {
140 using _Factory = VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::NormalFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > , VertexFactory::DynamicFactory< Real > >;
141 Execute< Real , Dim >( _Factory( positionFactory , normalFactory , scaleFactory , VertexFactory::DynamicFactory< Real >( unprocessedProperties ) ) );
142 }
143 else
144 {
145 using _Factory = VertexFactory::Factory< Real , typename VertexFactory::PositionFactory< Real , Dim > , typename VertexFactory::NormalFactory< Real , Dim > , VertexFactory::DynamicFactory< Real > >;
146 Execute< Real , Dim >( _Factory( positionFactory , normalFactory , scaleFactory ) );
147 }
148 }
149
150 int main( int argc , char* argv[] )
151 {
152 CmdLineParse( argc-1 , &argv[1] , params );
153 if( !In.set || !Out.set || !ConfidenceNames.set || !ConfidenceExponents.set )
154 {
155 ShowUsage( argv [0] );
156 return EXIT_FAILURE;
157 }
158
159 if( ConfidenceNames.count!=ConfidenceExponents.count )
160 MK_THROW( "Number of confidence names and exponents does not match: " , ConfidenceNames.count , " != " , ConfidenceExponents.count );
161
162 if( Verbose.set )
163 {
164 std::cout << "*******************************************" << std::endl;
165 std::cout << "*******************************************" << std::endl;
166 std::cout << "** Running Scale Normals (Version " << ADAPTIVE_SOLVERS_VERSION << ") **" << std::endl;
167 std::cout << "*******************************************" << std::endl;
168 std::cout << "*******************************************" << std::endl;
169 }
170
171 Execute< float , 3 >();
172
173
174 return EXIT_SUCCESS;
175 }