OpenWalnut  1.5.0dev
WMeshReaderBrainVISA.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 "WMeshReaderBrainVISA.h"
30 
32  WObjectNDIP< WMeshReaderInterface >( "BrainVISA", "Load BrainVISA 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< WProgress > progress( new WProgress( "Read Mesh", 3 ) );
54  parentProgress->addSubProgress( progress );
55 
56  std::ifstream ifs;
57  ifs.open( fileName.c_str(), std::ifstream::in );
58  if( !ifs || ifs.bad() )
59  {
60  WLogger::getLogger()->addLogMessage( "Trying to load a broken file '" + fileName + "'", "Read Mesh", LL_ERROR );
61  throw std::runtime_error( "Problem during reading file. Probably file not found." );
62  }
63  std::string line;
64 
65  // ------ HEADER -------
66  char * buffer = new char[10];
67  ifs.read( buffer, 10 );
68  buffer[9] = 0;
69 
70  std::string fileType( buffer );
71 
72  if( fileType != "binarDCBA" )
73  {
74  WLogger::getLogger()->addLogMessage( "Unsupported file type", "Read Mesh", LL_ERROR );
75  progress->finish();
76  return std::shared_ptr< WTriangleMesh >();
77  }
78  size_t numVertices = 0;
79  size_t numNormals = 0;
80  size_t numTriangles = 0;
81 
82  ifs.seekg( 29 );
83  int* count = new int[1];
84  ifs.read( reinterpret_cast< char* >( count ), 4 );
85  numVertices = count[0];
86 
87  ifs.seekg( 33 + count[0] * 12 );
88  ifs.read( reinterpret_cast< char* >( count ), 4 );
89  numNormals = count[0];
90 
91  ifs.seekg( 41 + numVertices * 12 + numNormals * 12 );
92  ifs.read( reinterpret_cast< char* >( count ), 4 );
93  numTriangles = count[0];
94 
95  std::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( numVertices, numTriangles ) );
96 
97  ifs.seekg( 33 );
98  float *pointData = new float[ 3 * numVertices ];
99  ifs.read( reinterpret_cast< char* >( pointData ), 3 * sizeof( float ) * numVertices );
100 
101  // skipping normals, as they are calculated by the triangle mesh class
102 
103  ifs.seekg( 45 + numVertices * 12 + numNormals * 12 );
104  int *triData = new int[ 3 * numTriangles ];
105  ifs.read( reinterpret_cast< char* >( triData ), 3 * sizeof( int ) * numTriangles );
106 
107  for( size_t i = 0; i < numVertices; ++i )
108  {
109  triMesh->addVertex( pointData[i * 3], m_propDatasetSizeY->get( true )-pointData[i*3+1], m_propDatasetSizeZ->get( true )-pointData[i*3+2] );
110  }
111  for( size_t i = 0; i < numTriangles; ++i )
112  {
113  triMesh->addTriangle( triData[i * 3], triData[i * 3 + 2], triData[i * 3 + 1] );
114  }
115 
116 // skipping normals, as they are calculated by the triangle mesh class
117 // the following code would need an adjustment to the triangle mesh class, to avoid overriding
118 // the loaded normals
119 //
120 // if( numVertices == numNormals )
121 // {
122 // ifs.seekg( 37 + numVertices * 12 );
123 // float *normalData = new float[ 3 * numNormals ];
124 // ifs.read( reinterpret_cast< char* >( normalData ), 3 * sizeof( float ) * numVertices );
125 //
126 // for( size_t i = 0; i < numNormals; ++i )
127 // {
128 // triMesh->setVertexNormal( i, WPosition( normalData[i * 3], normalData[i * 3 + 1], normalData[i * 3 + 2] ) );
129 // }
130 // triMesh->setUseExternalVertexNormals( true );
131 // }
132 
133  ifs.close();
134  progress->finish();
135  parentProgress->removeSubProgress( progress );
136 
137  return triMesh;
138 }
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
WPropInt m_propDatasetSizeY
Size of the dataset (Y)
virtual ~WMeshReaderBrainVISA()
Destructor.
WPropInt m_propDatasetSizeX
Size of the dataset (X)
WPropInt m_propDatasetSizeZ
Size of the dataset (Z)
virtual WTriangleMesh::SPtr operator()(WProgressCombiner::SPtr parentProgress, boost::filesystem::path file)
Load the dataset.
WMeshReaderBrainVISA()
Constructor.
Define the interface which is injected into an WObjectNDIP.
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.
Class managing progress inside of modules.
Definition: WProgress.h:42
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