OpenWalnut  1.5.0dev
WMeshReaderFreesurfer.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "WMeshReaderFreesurfer.h"
30 
32  WObjectNDIP< WMeshReaderInterface >( "Freesurfer", "Load Freesurfer Meshes." )
33 {
34  // add properties
35  m_propDatasetSizeX = m_properties->addProperty( "Dataset size X", "Size of the dataset in x direction", 160 );
36  m_propDatasetSizeY = m_properties->addProperty( "Dataset size Y", "Size of the dataset in y direction", 200 );
37  m_propDatasetSizeZ = m_properties->addProperty( "Dataset size Z", "Size of the dataset in z direction", 160 );
38 }
39 
41 {
42  // cleanup
43 }
44 
46  boost::filesystem::path file )
47 {
48  namespace su = string_utils;
49 
50  std::string fileName = file.string();
51  WAssert( !fileName.empty(), "No filename specified." );
52 
53  std::shared_ptr< std::ifstream > ifs( new std::ifstream() );
54  ifs->open( fileName.c_str(), std::ifstream::in | std::ifstream::binary );
55  if( !ifs || ifs->bad() )
56  {
57  throw WDHIOFailure( std::string( "internal error while opening" ) );
58  }
59  getLine( ifs, "" );
60  getLine( ifs, "" );
61 
62  int *pointData = new int[ 2 ];
63  ifs->read( reinterpret_cast< char* >( pointData ), 8 );
64  switchByteOrderOfArray( pointData, 2 );
65  size_t numVertices = pointData[0];
66  size_t numTriangles = pointData[1];
67  //std::cout << numVertices << " " << numTriangles << std::endl;
68 
69  float* verts = new float[ numVertices * 3 ];
70  ifs->read( reinterpret_cast< char* >( verts ), 3 * sizeof( float ) * numVertices );
71  switchByteOrderOfArray( verts, numVertices * 3 );
72 
73  int* tris = new int[ numTriangles * 3 ];
74  ifs->read( reinterpret_cast< char* >( tris ), 3 * sizeof( int ) * numTriangles );
75  switchByteOrderOfArray( tris, numTriangles * 3 );
76 
77  std::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( numVertices, numTriangles ) );
78 
79  for( size_t i = 0; i < numVertices; ++i )
80  {
81  triMesh->addVertex( verts[i*3]+ ( ( m_propDatasetSizeX->get( true )+1 )/2.0 ),
82  verts[i*3+1]+( ( m_propDatasetSizeY->get( true )+1 )/2.0 ),
83  verts[i*3+2]+( ( m_propDatasetSizeZ->get( true )+1 )/2.0 ) );
84  }
85  for( size_t i = 0; i < numTriangles; ++i )
86  {
87  triMesh->addTriangle( tris[i * 3], tris[i * 3 + 2], tris[i * 3 + 1] );
88  }
89  return triMesh;
90 }
Use this for IO error handling.
Definition: WDHIOFailure.h:38
WPropInt m_propDatasetSizeZ
Size of the dataset (Z)
virtual ~WMeshReaderFreesurfer()
Destructor.
WPropInt m_propDatasetSizeY
Size of the dataset (Y)
WPropInt m_propDatasetSizeX
Size of the dataset (X)
virtual WTriangleMesh::SPtr operator()(WProgressCombiner::SPtr parentProgress, boost::filesystem::path file)
Load the dataset.
Define the interface which is injected into an WObjectNDIP.
std::string getLine(std::shared_ptr< std::ifstream > ifs, const std::string &desc) const
Read a line from the given input stream and throw an exception on error.
This is a base class for everything which has a Name,Description,Icon and Properties (=NDIP).
Definition: WObjectNDIP.h:42
WProperties::SPtr m_properties
the properties of the object.
Definition: WObjectNDIP.h:99
std::shared_ptr< WProgressCombiner > SPtr
Abbreviate shared_ptr for this class.
Triangle mesh data structure allowing for convenient access of the elements.
Definition: WTriangleMesh.h:46
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
Definition: WTriangleMesh.h:55
Some utilities for string manipulation and output operations.
Definition: WStringUtils.h:59