aimsdata  4.7.0
Neuroimaging data handling
meshW.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 #ifndef AIMS_IO_MESHW_H
35 #define AIMS_IO_MESHW_H
36 
38 #include <aims/io/defaultItemW.h>
39 #include <aims/io/meshheader.h>
41 #include <aims/mesh/surface.h>
42 #include <aims/io/datatypecode.h>
43 #include <memory>
44 
45 
46 namespace aims
47 {
48 
52  template<int D, class T=Void>
54  {
55  public:
56  MeshWriter( const std::string & name, bool ascii = false,
57  ItemWriter<T> *ir = 0 )
58  : _name( name ), _ascii( ascii ), _itemw( ir ) { }
59  ~MeshWriter() { delete _itemw; }
60 
61  inline void write( const AimsTimeSurface<D,T> & thing );
62 
64  inline std::string removeExtension(const std::string& name) const;
66  { delete _itemw; _itemw = ir; }
67 
68  private:
69  std::string _name;
70  bool _ascii;
71  ItemWriter<T> *_itemw;
72  };
73 
74 
75  template <int D, class T>
76  inline
78  operator << ( MeshWriter<D,T> & writer, const AimsTimeSurface<D,T> & thing )
79  {
80  writer.write( thing );
81  return writer;
82  }
83 
84 
85  template <int D, class T> inline
86  std::string MeshWriter<D,T>::removeExtension( const std::string& name ) const
87  {
88  std::string res = name;
89  std::string ext="";
90  if( res.length() > 5 )
91  ext = res.substr( int(res.length() - 5), 5 );
92  if( ext == ".mesh" )
93  res = res.substr( 0, res.length() - 5 );
94  return res;
95  }
96 
97 
98  template <int D, class T> inline
100  {
101  std::string fname = removeExtension( _name ) + ".mesh";
102  std::string mode = ( _ascii ? "ascii" : "binar" );
103  std::ios::openmode omd = std::ios::out;
104  if( !_ascii )
105  omd |= std::ios::binary;
106  std::ofstream os( fname.c_str(), omd );
107  if( !os )
108  throw carto::file_error( fname );
109 
110  if( !_itemw )
111  _itemw = new DefaultItemWriter<T>;
112  std::unique_ptr< ItemWriter<T> > ir( _itemw->writer( mode, false ) );
114  std::unique_ptr< ItemWriter<uint32_t> > sr( sr1.writer( mode, false ) );
116  std::unique_ptr< ItemWriter<Point3df> > pr( pr1.writer( mode, false ) );
118  std::unique_ptr< ItemWriter<AimsVector<uint,D> > > plr ( plr1.writer( mode, false ) );
119 
120  // write header
121 
122  os << ( _ascii ? "ascii\n" : "binar" );
124  std::string code = dt.dataType();
125  std::string sep = ( _ascii ? "\n" : "" );
126 
127  if ( !_ascii )
128  {
129  uint32_t magicNumber = AIMS_MAGIC_NUMBER;
130  os.write( (const char *) &magicNumber, sizeof(uint32_t) );
131 
132  size_t tmp = code.size();
133  uint32_t typeSize = (uint32_t)tmp;
134  sr->write( os, typeSize );
135  }
136 
137  os << code << sep;
138 
139  uint32_t dim = (uint32_t)D;
140  sr->write( os, dim );
141  os << sep;
142 
143  uint32_t size = (uint32_t)thing.size();
144  sr->write( os, size );
145  os << sep;
146 
147  MeshHeader hdr( _name );
148  hdr.copy( thing.header() );
149  if( hdr.hasProperty( "nb_t_pos" ) )
150  hdr.removeProperty( "nb_t_pos" );
151  hdr.writeMinf( hdr.filename() + ".minf" );
152 
153  // write main data
154 
155  uint32_t nvertex, nnormal, time, ntexture, npolygon;
156  typename AimsTimeSurface<D,T>::const_iterator is, es = thing.end();
157 
158  for ( is=thing.begin(); is!=es; ++is )
159  {
160  time = (*is).first;
161  const std::vector<Point3df> & vert = (*is).second.vertex();
162  const std::vector<Point3df> & norm = (*is).second.normal();
163  const std::vector<T> & tex = (*is).second.texture();
164  const std::vector<AimsVector<uint,D> > & poly = (*is).second.polygon();
165 
166  nvertex = (uint32_t)vert.size();
167  nnormal = (uint32_t)norm.size();
168  ntexture = (uint32_t)tex.size();
169  npolygon = (uint32_t)poly.size();
170 
171  // time
172  sr->write( os, time );
173  os << sep;
174 
175  // vertices
176  sr->write( os, nvertex );
177  os << sep;
178 
179  pr->write( os, &vert[0], nvertex );
180  os << sep;
181 
182  // normals
183  sr->write( os, nnormal );
184  os << sep;
185  pr->write( os, &norm[0], nnormal );
186  os << sep;
187 
188  // textures
189  sr->write( os, ntexture );
190  os << sep;
191  ir->write( os, &tex[0], ntexture );
192  os << sep;
193 
194  // polygons
195  sr->write( os, npolygon );
196  os << sep;
197  plr->write( os, &poly[0], npolygon );
198  }
199  }
200 
201 }
202 
203 
204 #endif
Default low-levels writers.
Definition: defaultItemW.h:58
#define AIMSDATA_API
virtual void copy(const PythonHeader &, bool keepUuid=false)
The class for EcatSino data write operation.
Definition: border.h:42
virtual bool writeMinf(const std::string &filename)
write meta-info header, non-const version (may change some attributes)
virtual bool removeProperty(const std::string &)
The template class to manage a mesh with time if needed.
Definition: surface.h:290
Low-level "small item" writer, used by higher-level file readers.
Definition: itemW.h:50
#define AIMS_MAGIC_NUMBER
Definition: general.h:51
MeshWriter(const std::string &name, bool ascii=false, ItemWriter< T > *ir=0)
Definition: meshW.h:56
void setItemWriter(ItemWriter< T > *ir)
Definition: meshW.h:65
std::string removeExtension(const std::string &name) const
Return a name without .mesh extension.
Definition: meshW.h:86
Mesh format writer for mesh objects.
Definition: meshW.h:53
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of verteces of the surface of index 0.
Definition: surface.h:335
AIMSDATA_API float norm(const Tensor &thing)
Definition: tensor.h:141
void write(const AimsTimeSurface< D, T > &thing)
Definition: meshW.h:99
std::map< int, AimsSurface< D, T > >::const_iterator const_iterator
Definition: surface.h:310
virtual bool hasProperty(const std::string &) const
std::string filename() const
const aims::PythonHeader & header() const
Get the header.
Definition: surface.h:317