aimsdata  5.1.2
Neuroimaging data handling
plyw.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_PLYW_H
35 #define AIMS_IO_PLYW_H
36 
37 #include <aims/io/plyheader.h>
38 #include <aims/io/datatypecode.h>
39 #include <aims/data/pheader.h>
40 #include <aims/mesh/surface.h>
44 #include <aims/rply/rply.h>
45 
46 
47 namespace aims
48 {
49 
50  template<long D, typename T>
51  class PlyWriter
52  {
53  public:
54  PlyWriter( const std::string& name, bool ascii = false )
55  : _name( name ), _ascii( ascii )
56  {}
58 
59  void write( const AimsTimeSurface<D,T> & thing );
61  std::string removeExtension( const std::string& name ) const;
62 
63  private:
64  std::string _name;
65  bool _ascii;
66  };
67 
68 
69  template <long D, typename T>
70  inline PlyWriter<D,T> &
71  operator << ( PlyWriter<D,T> & writer, const AimsTimeSurface<D,T> & thing )
72  {
73  writer.write( thing );
74  return writer;
75  }
76 
77 
78  template <long D, typename T>
79  inline
81  {
82  PlyHeader hdr( _name );
83  const PythonHeader
84  *ph = dynamic_cast<const PythonHeader *>( &thing.header() );
85  if( ph )
86  hdr.copy( *ph );
87 
88  std::string dir = carto::FileUtil::dirname( _name );
89 
90  if( hdr.hasProperty( "nb_t_pos" ) )
91  hdr.removeProperty( "nb_t_pos" );
92  hdr.setProperty( "file_type", std::string( "PLY" ) );
93  hdr.setProperty( "object_type",
95  );
96  hdr.setProperty( "data_type", carto::DataTypeCode<T>::name() );
97  if( !dir.empty() )
99 
100  std::string fname = carto::FileUtil::removeExtension( _name )
101  + hdr.extension();
103  if( _ascii )
104  mode = PLY_ASCII;
105  p_ply ply = ply_create( fname.c_str(), mode, 0, 0, 0 );
106  if( !ply )
108 
109  if( thing.size() > 1 )
110  std::cout << "PlyWriter warning: only the first timestep of the "
111  << "surface will be writen" << std::endl;
112 
113  const std::vector<Point3df> & vert = thing.vertex();
114  const std::vector<Point3df> & norm = thing.normal();
115  const std::vector<AimsVector<uint, D> > & poly = thing.polygon();
116  unsigned i, j, n = vert.size(), np = poly.size();
117  bool normal = (n <= norm.size() );
118 
119  if( !ply_add_element( ply, "vertex", n ) )
120  throw carto::invalid_format_error( "PLY format internal error", fname );
121  if( !ply_add_scalar_property( ply, "x", PLY_FLOAT )
122  || !ply_add_scalar_property( ply, "y", PLY_FLOAT )
123  || !ply_add_scalar_property( ply, "z", PLY_FLOAT ) )
124  throw carto::invalid_format_error( "PLY format internal error", fname );
125  if( normal )
126  if( !ply_add_scalar_property( ply, "nx", PLY_FLOAT )
127  || !ply_add_scalar_property( ply, "ny", PLY_FLOAT )
128  || !ply_add_scalar_property( ply, "nz", PLY_FLOAT ) )
129  throw carto::invalid_format_error( "PLY format internal error",
130  fname );
131  if( !ply_add_element( ply, "face", np ) )
132  throw carto::invalid_format_error( "PLY format internal error", fname );
133  if( !ply_add_list_property( ply, "vertex_indices", PLY_UCHAR, PLY_INT ) )
134  throw carto::invalid_format_error( "PLY format internal error", fname );
135 
136  if( !ply_write_header( ply ) )
137  throw carto::invalid_format_error( "PLY format write error", fname );
138 
139  for( i=0; i<n; ++i )
140  {
141  if( !ply_write( ply, vert[i][0] )
142  || !ply_write( ply, vert[i][1] )
143  || !ply_write( ply, vert[i][2] )
144  )
146  if( normal )
147  if( !ply_write( ply, norm[i][0] )
148  || !ply_write( ply, norm[i][1] )
149  || !ply_write( ply, norm[i][2] )
150  )
152  }
153  for( i=0; i<np; ++i )
154  {
155  if( !ply_write( ply, rint( D ) ) )
157  for( j=0; j<D; ++j )
158  if( !ply_write( ply, rint( poly[i][j] ) ) )
160  }
161 
162  if( !ply_close(ply) )
164 
165  hdr.writeMinf( fname + ".minf" );
166  }
167 
168 }
169 
170 #endif
171 
The template class to manage a mesh with time if needed.
Definition: surface.h:317
const std::vector< AimsVector< uint, D > > & polygon() const
Get a const reference to the vector of polygons of the 0 surface.
Definition: surface.h:362
const aims::PythonHeader & header() const
Get the header.
Definition: surface.h:328
const std::vector< Point3df > & normal() const
Get a const reference to the vector of normals of the 0 surface.
Definition: surface.h:350
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of verteces of the surface of index 0.
Definition: surface.h:344
virtual std::string extension() const
standard file format extension of specialized headers
Definition: plyheader.h:52
std::string removeExtension(const std::string &name) const
Return a name without .ply extension.
PlyWriter(const std::string &name, bool ascii=false)
Definition: plyw.h:54
~PlyWriter()
Definition: plyw.h:57
void write(const AimsTimeSurface< D, T > &thing)
Definition: plyw.h:80
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:52
virtual void copy(const PythonHeader &, bool keepUuid=false)
virtual bool writeMinf(const std::string &filename)
write meta-info header, non-const version (may change some attributes)
static char separator()
static std::string dirname(const std::string &)
static std::string removeExtension(const std::string &)
virtual bool removeProperty(const std::string &)
virtual void setProperty(const std::string &, Object)
virtual bool hasProperty(const std::string &) const
static void launchErrnoExcept(const std::string &filename="")
The class for EcatSino data write operation.
Definition: borderfiller.h:13
MotionWriter & operator<<(MotionWriter &writer, const AffineTransformation3d &thing) __attribute__((__deprecated__("OBSOLETE")))
— OBSOLETE —
Definition: motionW.h:87
int ply_write_header(p_ply ply)
enum e_ply_storage_mode_ e_ply_storage_mode
int ply_add_list_property(p_ply ply, const char *name, e_ply_type length_type, e_ply_type value_type)
@ PLY_FLOAT
Definition: rply.h:42
@ PLY_INT
Definition: rply.h:42
@ PLY_UCHAR
Definition: rply.h:41
@ PLY_LITTLE_ENDIAN
Definition: rply.h:32
@ PLY_ASCII
Definition: rply.h:33
int ply_add_scalar_property(p_ply ply, const char *name, e_ply_type type)
struct t_ply_ * p_ply
Definition: rply.h:24
p_ply ply_create(const char *name, e_ply_storage_mode storage_mode, p_ply_error_cb error_cb, long idata, void *pdata)
int ply_write(p_ply ply, double value)
int ply_add_element(p_ply ply, const char *name, long ninstances)
int ply_close(p_ply ply)
AIMSDATA_API float norm(const Tensor &thing)
Definition: tensor.h:141