aimsdata  5.1.2
Neuroimaging data handling
meshR.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * Surface reader class
36  */
37 #ifndef AIMS_IO_MESHR_H
38 #define AIMS_IO_MESHR_H
39 
41 #include <aims/io/defaultItemR.h>
42 #include <aims/io/meshheader.h>
44 
45 
46 namespace aims
47 {
48 
57  template<int D, class T=Void>
58  class MeshReader
59  {
60  public:
61  MeshReader( const std::string & name, ItemReader<T> *ir = 0 )
62  : _name( name ), _itemr( ir ) { }
63  ~MeshReader() { delete _itemr; }
64 
65  inline void read( AimsTimeSurface<D,T>& thing, int frame = -1 );
66 
68  inline std::string removeExtension(const std::string& name);
70  { delete _itemr; _itemr = ir; }
71 
72  private:
73  std::string _name;
74  ItemReader<T> *_itemr;
75  };
76 
77 
78  template <int D, class T>
79  inline
80  AIMSDATA_API MeshReader<D,T> &
82  {
83  reader.read( thing );
84  return reader;
85  }
86 
87 
88  template <int D, class T> inline
89  std::string MeshReader<D,T>::removeExtension( const std::string& name )
90  {
91  std::string res = name;
92  std::string ext="";
93  if ( res.length() > 5 )
94  ext = res.substr( int(res.length() - 5), 5 );
95  if ( ext == ".mesh" )
96  res = res.substr( 0, res.length() - 5 );
97  return res;
98  }
99 
100 
101  template <int D, class T> inline
102  void
104  {
105  if ( frame >= 0 )
106  std::cerr << "Warning : .mesh single frame reading not implemented yet "
107  << "-\nreading whole mesh\n";
108 
109  MeshHeader hdr( _name );
110  uint32_t buf;
111  if ( !hdr.read( &buf ) )
113  int polydim = D;
114  hdr.getProperty( "polygon_dimension", polydim );
115  if( polydim != D )
116  {
117  std::stringstream msg;
118  msg << "Wrong polygon dimension, " << polydim << ", should be " << D
119  << ".";
120  throw carto::datatype_format_error( msg.str(), hdr.filename() );
121  }
122  std::ifstream::off_type offset = buf; // ## does not support large files!
123 
124  std::ios::openmode omd = std::ios::in;
125  if ( hdr.openMode() == "binar" )
126  omd |= std::ios::binary;
127  std::ifstream is( hdr.filename().c_str(), omd );
128  is.unsetf( std::ios::skipws );
129  if ( !is )
131  is.seekg( offset );
132  if ( !is )
134 
135  if ( hdr.openMode() == "ascii" )
136  is.setf( std::ios::skipws );
137  thing.erase();
138  if ( !_itemr )
139  _itemr = new DefaultItemReader<T>;
140  ItemReader<T> *ir
141  = _itemr->reader( hdr.openMode(), hdr.byteSwapping() );
143  ItemReader<uint32_t> *sr = sr1.reader( hdr.openMode(),
144  hdr.byteSwapping() );
146  ItemReader<Point3df> *pr = pr1.reader( hdr.openMode(),
147  hdr.byteSwapping() );
150  = plr1.reader( hdr.openMode(), hdr.byteSwapping() );
151 
152  int size = 0;
153  hdr.getProperty( "nb_t_pos", size );
154  bool broken = false;
155  typename std::vector<AimsVector<uint,D> >::iterator ip;
156 
157  thing.setHeader( hdr );
158 
159  uint32_t time, nvertex, nnormal, ntexture, npolygon;
160  for ( int t=0; t<size; ++t )
161  {
162  // time
163  sr->read( is, time );
164 
165  // vertices
166  sr->read( is, nvertex );
167 
168  AimsSurface<D,T> & surf = thing[ time ];
169 
170  std::vector<Point3df> & vert = surf.vertex();
171  vert.insert( vert.end(), nvertex, Point3df() );
172  pr->read( is, &vert[0], nvertex );
173 
174  // normals
175  sr->read( is, nnormal );
176  std::vector<Point3df> & norm = surf.normal();
177  norm.insert( norm.end(), nnormal, Point3df() );
178  pr->read( is, &norm[0], nnormal );
179 
180  // textures
181  sr->read( is, ntexture );
182  std::vector<T> & tex = surf.texture();
183  tex.insert( tex.end(), ntexture, T() );
184  ir->read( is, &tex[0], ntexture );
185 
186  // polygons
187  sr->read( is, npolygon );
188  std::vector<AimsVector<uint,D> > & poly = surf.polygon();
189  poly.insert( poly.end(), npolygon, AimsVector<uint,D>() );
190  plr->read( is, &poly[0], npolygon );
191 
192  // verify polygons are all in the vertices range
193  for ( ip=poly.begin(); ip!=poly.end(); ++ip )
194  {
195  AimsVector<uint,D> & pol = *ip;
196  for ( int j=0; j<D; ++j )
197  if ( pol[j] >= nvertex )
198  {
199  if ( !broken )
200  {
201  broken = true;
202  std::cerr << "Broken mesh: polygon pointing to a "
203  << "vertex out of range" << std::endl;
204  }
205  poly.erase( ip );
206  --ip;
207  break;
208  }
209  }
210 
211  if ( norm.size() != vert.size() )
212  surf.updateNormals();
213  }
214 
215  delete plr;
216  delete pr;
217  delete sr;
218  delete ir;
219  }
220 
221 }
222 
223 
224 #endif
#define AIMSDATA_API
The template class to manage a mesh.
Definition: surface.h:69
const std::vector< Point3df > & normal() const
Get a const reference to the vector of normals.
Definition: surface.h:98
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of vertices.
Definition: surface.h:93
void updateNormals()
Update/Compute the normals.
Definition: surface.h:188
const std::vector< T > & texture() const
Get a const reference to the vector of textures.
Definition: surface.h:103
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons.
Definition: surface.h:108
The template class to manage a mesh with time if needed.
Definition: surface.h:317
void erase()
Clear all the meshes.
Definition: surface.h:505
void setHeader(const aims::PythonHeader &hdr)
Set the header.
Definition: surface.h:332
Default low-levels readers.
Definition: defaultItemR.h:56
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const
Definition: defaultItemR.h:154
Low-level "small item" reader, used by higher-level file readers.
Definition: itemR.h:99
virtual void read(std::istream &is, T &item) const
Definition: itemR.h:103
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const =0
std::string filename() const
virtual bool read(uint32_t *offset=0)
Reads the header, and if offset is not null, sets the file offset to the data field.
virtual std::string openMode() const
virtual bool byteSwapping() const
Mesh format readers for mesh objects.
Definition: meshR.h:59
void setItemReader(ItemReader< T > *ir)
Definition: meshR.h:69
void read(AimsTimeSurface< D, T > &thing, int frame=-1)
Definition: meshR.h:103
MeshReader(const std::string &name, ItemReader< T > *ir=0)
Definition: meshR.h:61
std::string removeExtension(const std::string &name)
Return a name without .mesh extension.
Definition: meshR.h:89
virtual bool getProperty(const std::string &, Object &) const
static void launchErrnoExcept(const std::string &filename="")
The class for EcatSino data write operation.
Definition: borderfiller.h:13
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
Definition: genesisR.h:70
AIMSDATA_API float norm(const Tensor &thing)
Definition: tensor.h:141