@@ -98,6 +98,44 @@ void copyArrayToContainer( const uint8_t* buffer,
9898 }
9999}
100100
101+ template <typename S>
102+ void r ( S* data, size_t count ) {
103+ using V = Eigen::Matrix<S, 3 , 1 >;
104+ V min = { data[0 ], data[1 ], data[2 ] };
105+ V max = { data[0 ], data[1 ], data[2 ] };
106+ for ( size_t i = 0 ; i < count; ++i ) {
107+ V v = { data[i * 3 + 0 ], data[i * 3 + 1 ], data[i * 3 + 2 ] };
108+ min = min.cwiseMin ( v );
109+ max = max.cwiseMax ( v );
110+ }
111+ V mid = (S).5 * ( min + max );
112+ for ( size_t i = 0 ; i < count; ++i ) {
113+ data[i * 3 + 0 ] -= mid[0 ];
114+ data[i * 3 + 1 ] -= mid[1 ];
115+ data[i * 3 + 2 ] -= mid[2 ];
116+ }
117+ }
118+
119+ void recenterVertices ( std::shared_ptr<tinyply::PlyData>& buffer ) {
120+
121+ if ( buffer && buffer->count != 0 ) {
122+ switch ( buffer->t ) {
123+ case tinyply::Type::FLOAT32 : {
124+ auto data = reinterpret_cast <float *>( buffer->buffer .get () );
125+ auto count = buffer->count ;
126+ r ( data, count );
127+ } break ;
128+ case tinyply::Type::FLOAT64 : {
129+ auto data = reinterpret_cast <double *>( buffer->buffer .get () );
130+ auto count = buffer->count ;
131+ r ( data, count );
132+ } break ;
133+ default :
134+ break ;
135+ }
136+ }
137+ }
138+
101139template <typename ContainerType>
102140void copyBufferToContainer ( const std::shared_ptr<tinyply::PlyData>& buffer,
103141 ContainerType& container ) {
@@ -204,7 +242,8 @@ FileData* TinyPlyFileLoader::loadFile( const std::string& filename ) {
204242 return nullptr ;
205243 }
206244 // / request for standard vertex attributes
207- // / \todo merge with non standard attributes when all will be stored as Attribs in GeometryData
245+ // / \todo merge with non standard attributes when all will be stored as Attribs in
246+ // / GeometryData
208247 auto normalBuffer { initBuffer ( " vertex" , { " nx" , " ny" , " nz" } ) };
209248 auto alphaBuffer { initBuffer ( " vertex" , { " alpha" } ) };
210249 auto colorBuffer { initBuffer ( " vertex" , { " red" , " green" , " blue" } ) };
@@ -229,6 +268,7 @@ FileData* TinyPlyFileLoader::loadFile( const std::string& filename ) {
229268 file.read ( *file_stream );
230269 {
231270 auto unlocker = geomData->getGeometry ().vertexAttribs ().getScopedLockState ();
271+ recenterVertices ( vertBuffer );
232272 copyBufferToContainer ( vertBuffer, geomData->getGeometry ().verticesWithLock () );
233273 copyBufferToContainer ( normalBuffer, geomData->getGeometry ().normalsWithLock () );
234274 }
0 commit comments