Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
OMFormat.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 
50 //=============================================================================
51 //
52 // Helper Functions for binary reading / writing
53 //
54 //=============================================================================
55 
56 //== INCLUDES =================================================================
57 
58 #include <OpenMesh/Core/IO/OMFormat.hh>
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace OpenMesh {
63 namespace IO {
64 namespace OMFormat {
65 
66 //== IMPLEMENTATION ===========================================================
67 
68  Chunk::Integer_Size needed_bits( size_t s )
69  {
70  if (s <= 0x000100) return Chunk::Integer_8;
71  if (s <= 0x010000) return Chunk::Integer_16;
72 
73 #if 0
74  // !Not tested yet! This most probably won't work!
75  // NEED a 64bit system!
76  if ( (sizeof( size_t ) == 8) && (s >= 0x100000000) )
77  return Chunk::Integer_64;
78 #endif
79 
80  return Chunk::Integer_32;
81  }
82 
83 //-----------------------------------------------------------------------------
84 
85  uint16&
86  operator << (uint16& val, const Chunk::Header& hdr)
87  {
88  val = 0;
89  val |= hdr.name_ << OMFormat::Chunk::OFF_NAME;
90  val |= hdr.entity_ << OMFormat::Chunk::OFF_ENTITY;
91  val |= hdr.type_ << OMFormat::Chunk::OFF_TYPE;
92  val |= hdr.signed_ << OMFormat::Chunk::OFF_SIGNED;
93  val |= hdr.float_ << OMFormat::Chunk::OFF_FLOAT;
94  val |= hdr.dim_ << OMFormat::Chunk::OFF_DIM;
95  val |= hdr.bits_ << OMFormat::Chunk::OFF_BITS;
96  return val;
97  }
98 
99 
100 //-----------------------------------------------------------------------------
101 
102  Chunk::Header&
103  operator << (Chunk::Header& hdr, const uint16 val)
104  {
105  hdr.reserved_ = 0;
106  hdr.name_ = val >> OMFormat::Chunk::OFF_NAME;
107  hdr.entity_ = val >> OMFormat::Chunk::OFF_ENTITY;
108  hdr.type_ = val >> OMFormat::Chunk::OFF_TYPE;
109  hdr.signed_ = val >> OMFormat::Chunk::OFF_SIGNED;
110  hdr.float_ = val >> OMFormat::Chunk::OFF_FLOAT;
111  hdr.dim_ = val >> OMFormat::Chunk::OFF_DIM;
112  hdr.bits_ = val >> OMFormat::Chunk::OFF_BITS;
113  return hdr;
114  }
115 
116 //-----------------------------------------------------------------------------
117 
118  const char *as_string(Chunk::Entity e)
119  {
120  switch(e)
121  {
122  case Chunk::Entity_Vertex: return "Vertex";
123  case Chunk::Entity_Mesh: return "Mesh";
124  case Chunk::Entity_Edge: return "Edge";
125  case Chunk::Entity_Halfedge: return "Halfedge";
126  case Chunk::Entity_Face: return "Face";
127  default:
128  std::clog << "as_string(Chunk::Entity): Invalid value!";
129  }
130  return NULL;
131  }
132 
133 
134 //-----------------------------------------------------------------------------
135 
136  const char *as_string(Chunk::Type t)
137  {
138  switch(t)
139  {
140  case Chunk::Type_Pos: return "Pos";
141  case Chunk::Type_Normal: return "Normal";
142  case Chunk::Type_Texcoord: return "Texcoord";
143  case Chunk::Type_Status: return "Status";
144  case Chunk::Type_Color: return "Color";
145  case Chunk::Type_Custom: return "Custom";
146  case Chunk::Type_Topology: return "Topology";
147  }
148  return NULL;
149  }
150 
151 
152 //-----------------------------------------------------------------------------
153 
154  const char *as_string(Chunk::Dim d)
155  {
156  switch(d)
157  {
158  case Chunk::Dim_1D: return "1D";
159  case Chunk::Dim_2D: return "2D";
160  case Chunk::Dim_3D: return "3D";
161  case Chunk::Dim_4D: return "4D";
162  case Chunk::Dim_5D: return "5D";
163  case Chunk::Dim_6D: return "6D";
164  case Chunk::Dim_7D: return "7D";
165  case Chunk::Dim_8D: return "8D";
166  }
167  return NULL;
168  }
169 
170 
171 //-----------------------------------------------------------------------------
172 
173  const char *as_string(Chunk::Integer_Size d)
174  {
175  switch(d)
176  {
177  case Chunk::Integer_8 : return "8";
178  case Chunk::Integer_16 : return "16";
179  case Chunk::Integer_32 : return "32";
180  case Chunk::Integer_64 : return "64";
181  }
182  return NULL;
183  }
184 
185  const char *as_string(Chunk::Float_Size d)
186  {
187  switch(d)
188  {
189  case Chunk::Float_32 : return "32";
190  case Chunk::Float_64 : return "64";
191  case Chunk::Float_128: return "128";
192  }
193  return NULL;
194  }
195 
196 
197 //-----------------------------------------------------------------------------
198 
199  std::ostream& operator << ( std::ostream& _os, const Chunk::Header& _c )
200  {
201  _os << "Chunk Header : 0x" << std::setw(4)
202  << std::hex << (*(uint16*)(&_c)) << std::dec << '\n';
203  _os << "entity = "
204  << as_string(Chunk::Entity(_c.entity_)) << '\n';
205  _os << "type = "
206  << as_string(Chunk::Type(_c.type_));
207  if ( Chunk::Type(_c.type_)!=Chunk::Type_Custom)
208  {
209  _os << '\n'
210  << "signed = "
211  << _c.signed_ << '\n';
212  _os << "float = "
213  << _c.float_ << '\n';
214  _os << "dim = "
215  << as_string(Chunk::Dim(_c.dim_)) << '\n';
216  _os << "bits = "
217  << (_c.float_
218  ? as_string(Chunk::Float_Size(_c.bits_))
219  : as_string(Chunk::Integer_Size(_c.bits_)));
220  }
221  return _os;
222  }
223 
224 
225 //-----------------------------------------------------------------------------
226 
227  std::ostream& operator << ( std::ostream& _os, const Header& _h )
228  {
229  _os << "magic = '" << _h.magic_[0] << _h.magic_[1] << "'\n"
230  << "mesh = '" << _h.mesh_ << "'\n"
231  << "version = 0x" << std::hex << (uint16)_h.version_ << std::dec
232  << " (" << major_version(_h.version_)
233  << "." << minor_version(_h.version_) << ")\n"
234  << "#V = " << _h.n_vertices_ << '\n'
235  << "#F = " << _h.n_faces_ << '\n'
236  << "#E = " << _h.n_edges_;
237  return _os;
238  }
239 
240 
241 } // namespace OMFormat
242  // --------------------------------------------------------------------------
243 
244 //=============================================================================
245 } // namespace IO
246 } // namespace OpenMesh
247 //=============================================================================
auto operator<<(std::ostream &os, const VectorT< Scalar, DIM > &_vec) -> typename std::enable_if< sizeof(decltype(os<< _vec[0])) >=0
output a vector by printing its space-separated compontens
Definition: VectorT.hh:652