Developer Documentation
Loading...
Searching...
No Matches
OMFormat.cc
1/* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2025, 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
45//=============================================================================
46//
47// Helper Functions for binary reading / writing
48//
49//=============================================================================
50
51//== INCLUDES =================================================================
52
53#include <OpenMesh/Core/IO/OMFormat.hh>
54
55//== NAMESPACES ===============================================================
56
57namespace OpenMesh {
58namespace IO {
59namespace OMFormat {
60
61//== IMPLEMENTATION ===========================================================
62
63 Chunk::Integer_Size needed_bits( size_t s )
64 {
65 if (s <= 0x000100) return Chunk::Integer_8;
66 if (s <= 0x010000) return Chunk::Integer_16;
67
68#if 0
69 // !Not tested yet! This most probably won't work!
70 // NEED a 64bit system!
71 if ( (sizeof( size_t ) == 8) && (s >= 0x100000000) )
72 return Chunk::Integer_64;
73#endif
74
75 return Chunk::Integer_32;
76 }
77
78//-----------------------------------------------------------------------------
79
80 uint16&
81 operator << (uint16& val, const Chunk::Header& hdr)
82 {
83 val = 0;
84 val |= hdr.name_ << OMFormat::Chunk::OFF_NAME;
85 val |= hdr.entity_ << OMFormat::Chunk::OFF_ENTITY;
86 val |= hdr.type_ << OMFormat::Chunk::OFF_TYPE;
87 val |= hdr.signed_ << OMFormat::Chunk::OFF_SIGNED;
88 val |= hdr.float_ << OMFormat::Chunk::OFF_FLOAT;
89 val |= hdr.dim_ << OMFormat::Chunk::OFF_DIM;
90 val |= hdr.bits_ << OMFormat::Chunk::OFF_BITS;
91 return val;
92 }
93
94
95//-----------------------------------------------------------------------------
96
97 Chunk::Header&
98 operator << (Chunk::Header& hdr, const uint16 val)
99 {
100 hdr.reserved_ = 0;
101 hdr.name_ = val >> OMFormat::Chunk::OFF_NAME;
102 hdr.entity_ = val >> OMFormat::Chunk::OFF_ENTITY;
103 hdr.type_ = val >> OMFormat::Chunk::OFF_TYPE;
104 hdr.signed_ = val >> OMFormat::Chunk::OFF_SIGNED;
105 hdr.float_ = val >> OMFormat::Chunk::OFF_FLOAT;
106 hdr.dim_ = val >> OMFormat::Chunk::OFF_DIM;
107 hdr.bits_ = val >> OMFormat::Chunk::OFF_BITS;
108 return hdr;
109 }
110
111//-----------------------------------------------------------------------------
112
113
114 std::string as_string(uint8 version)
115 {
116 std::stringstream ss;
117 ss << major_version(version);
118 ss << ".";
119 ss << minor_version(version);
120 return ss.str();
121 }
122
123
124//-----------------------------------------------------------------------------
125
126 const char *as_string(Chunk::Entity e)
127 {
128 switch(e)
129 {
130 case Chunk::Entity_Vertex: return "Vertex";
131 case Chunk::Entity_Mesh: return "Mesh";
132 case Chunk::Entity_Edge: return "Edge";
133 case Chunk::Entity_Halfedge: return "Halfedge";
134 case Chunk::Entity_Face: return "Face";
135 default:
136 std::clog << "as_string(Chunk::Entity): Invalid value!";
137 }
138 return nullptr;
139 }
140
141
142//-----------------------------------------------------------------------------
143
144 const char *as_string(Chunk::Type t)
145 {
146 switch(t)
147 {
148 case Chunk::Type_Pos: return "Pos";
149 case Chunk::Type_Normal: return "Normal";
150 case Chunk::Type_Texcoord: return "Texcoord";
151 case Chunk::Type_Status: return "Status";
152 case Chunk::Type_Color: return "Color";
153 case Chunk::Type_Custom: return "Custom";
154 case Chunk::Type_Topology: return "Topology";
155 }
156 return nullptr;
157 }
158
159
160//-----------------------------------------------------------------------------
161
162 const char *as_string(Chunk::Dim d)
163 {
164 switch(d)
165 {
166 case Chunk::Dim_1D: return "1D";
167 case Chunk::Dim_2D: return "2D";
168 case Chunk::Dim_3D: return "3D";
169 case Chunk::Dim_4D: return "4D";
170 case Chunk::Dim_5D: return "5D";
171 case Chunk::Dim_6D: return "6D";
172 case Chunk::Dim_7D: return "7D";
173 case Chunk::Dim_8D: return "8D";
174 }
175 return nullptr;
176 }
177
178
179//-----------------------------------------------------------------------------
180
181 const char *as_string(Chunk::Integer_Size d)
182 {
183 switch(d)
184 {
185 case Chunk::Integer_8 : return "8";
186 case Chunk::Integer_16 : return "16";
187 case Chunk::Integer_32 : return "32";
188 case Chunk::Integer_64 : return "64";
189 }
190 return nullptr;
191 }
192
193 const char *as_string(Chunk::Float_Size d)
194 {
195 switch(d)
196 {
197 case Chunk::Float_32 : return "32";
198 case Chunk::Float_64 : return "64";
199 case Chunk::Float_128: return "128";
200 }
201 return nullptr;
202 }
203
204
205//-----------------------------------------------------------------------------
206
207 std::ostream& operator << ( std::ostream& _os, const Chunk::Header& _c )
208 {
209 _os << "Chunk Header : 0x" << std::setw(4)
210 << std::hex << (*(uint16*)(&_c)) << std::dec << '\n';
211 _os << "entity = "
212 << as_string(Chunk::Entity(_c.entity_)) << '\n';
213 _os << "type = "
214 << as_string(Chunk::Type(_c.type_));
215 if ( Chunk::Type(_c.type_)!=Chunk::Type_Custom)
216 {
217 _os << '\n'
218 << "signed = "
219 << _c.signed_ << '\n';
220 _os << "float = "
221 << _c.float_ << '\n';
222 _os << "dim = "
223 << as_string(Chunk::Dim(_c.dim_)) << '\n';
224 _os << "bits = "
225 << (_c.float_
226 ? as_string(Chunk::Float_Size(_c.bits_))
227 : as_string(Chunk::Integer_Size(_c.bits_)));
228 }
229 return _os;
230 }
231
232
233//-----------------------------------------------------------------------------
234
235 std::ostream& operator << ( std::ostream& _os, const Header& _h )
236 {
237 _os << "magic = '" << _h.magic_[0] << _h.magic_[1] << "'\n"
238 << "mesh = '" << _h.mesh_ << "'\n"
239 << "version = 0x" << std::hex << (uint16)_h.version_ << std::dec
240 << " (" << major_version(_h.version_)
241 << "." << minor_version(_h.version_) << ")\n"
242 << "#V = " << _h.n_vertices_ << '\n'
243 << "#F = " << _h.n_faces_ << '\n'
244 << "#E = " << _h.n_edges_;
245 return _os;
246 }
247
248
249 //-----------------------------------------------------------------------------
250
251
252} // namespace OMFormat
253 // --------------------------------------------------------------------------
254
255//=============================================================================
256} // namespace IO
257} // namespace OpenMesh
258//=============================================================================