Developer Documentation
Loading...
Searching...
No Matches
FileOFFT_impl.hh
1/*===========================================================================*\
2* *
3* OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
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#define FILEOFFPLUGIN_C
45
46#include "FileOFF.hh"
47
48#include <OpenMesh/Core/Utils/color_cast.hh>
49#include <OpenMesh/Core/Geometry/VectorT.hh>
50#include <sstream>
51
52
53template< class MeshT >
54bool FileOFFPlugin::writeMesh(std::ostream& _out, MeshT& _mesh, BaseObject &_baseObj){
55
56 /*****************
57 * HEADER
58 ******************/
59
60 // Write option ST
61 if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS) ) {
62 _out << "ST";
63 }
64
65 // Write option C
66 if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR) ) {
67 _out << "C";
68 }
69
70 // Write option N
71 if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL) ) {
72 _out << "N";
73 }
74
75 // Write
76 _out << "OFF";
77
78 // Write BINARY
79 if(userWriteOptions_ & OFFImporter::BINARY) {
80 _out << " BINARY";
81 }
82
83 _out << "\n";
84
85
86 /*
87 * Comment
88 */
90
91 if (_baseObj.hasComments()) {
92 _out << "# %% BEGIN OPENFLIPPER_COMMENT %%" << std::endl;
93 std::istringstream comment(_baseObj.getAllCommentsFlat().toStdString());
94 std::string commentLine;
95 while (std::getline(comment, commentLine)) {
96 _out << "# " << commentLine << std::endl;
97 }
98 _out << "# %% END OPENFLIPPER_COMMENT %%" << std::endl;
99 }
100
101 /*****************
102 * DATA
103 ******************/
104
105 // Call corresponding write routine
106 if(userWriteOptions_ & OFFImporter::BINARY) {
107 return writeBinaryData(_out, _mesh);
108 } else {
109 if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0)
110 _out.precision(savePrecision_->value());
111
112 return writeASCIIData(_out, _mesh);
113 }
114}
115
116//------------------------------------------------------------------------------------------------------
117
118template< class MeshT >
119bool FileOFFPlugin::writeASCIIData(std::ostream& _out, MeshT& _mesh ) {
120
121 typename MeshT::Point p;
122 typename MeshT::Normal n;
123 typename OpenMesh::Vec4f c;
124 typename MeshT::TexCoord2D t;
125
126 // #V #F #E
127 _out << _mesh.n_vertices() << " " << _mesh.n_faces() << " " << _mesh.n_edges();
128
129 // Write vertex data
130 for(auto vit : _mesh.vertices()) {
131
132 _out << "\n";
133
134 // Write vertex p[0] p[1] p[2]
135 p = _mesh.point(vit);
136 _out << p[0] << " " << p[1] << " " << p[2];
137
138 // Write vertex normals
139 if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL)) {
140 n = _mesh.normal(vit);
141 _out << " " << n[0] << " " << n[1] << " " << n[2];
142 }
143
144 // Write vertex colors
145 // Note: Vertex colors always have only three components.
146 // This has to be determined since it can not be read dynamically in binary files.
147 if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR)) {
148 c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(vit));
149 _out << " " << std::showpoint << c[0] << " " << std::showpoint << c[1] << " " << std::showpoint << c[2] << " " << std::showpoint << c[3];
150 }
151
152 // Write vertex texcoords
153 if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS)) {
154 t = _mesh.texcoord2D(vit);
155 _out << " " << t[0] << " " << t[1];
156 }
157 }
158
159 // Write face data
160 for(auto fit : _mesh.faces()) {
161
162 _out << "\n";
163
164 // Write face valence
165 _out << fit.valence();
166
167 // Write vertex indices
168 for(auto fvit : fit.vertices()) {
169 _out << " " << fvit.idx();
170 }
171
172 // Write face colors
173 if(_mesh.has_face_colors() && (userWriteOptions_ & OFFImporter::FACECOLOR ) ) {
174 c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(fit));
175 _out << " " << std::showpoint << c[0] << " " << std::showpoint << c[1] << " " << std::showpoint << c[2];
176
177 if(userWriteOptions_ & OFFImporter::COLORALPHA) _out << " " << std::showpoint << c[3];
178 }
179 }
180
181 return true;
182}
183
184//------------------------------------------------------------------------------------------------------
185
186template< class MeshT >
187bool FileOFFPlugin::writeBinaryData(std::ostream& _out, MeshT& _mesh ){
188
189 Vec3f v, n;
190 Vec2f t;
191 OpenMesh::Vec4f c(1.0,1.0,1.0,1.0);
193
194 // #vertices, #faces, #edges
195 writeValue(_out, (uint)_mesh.n_vertices() );
196 writeValue(_out, (uint)_mesh.n_faces() );
197 writeValue(_out, (uint)_mesh.n_edges() );
198
199 // Write vertex data
200 for(auto vit : _mesh.vertices()) {
201
202 // Write vertex p[0] p[1] p[2]
203 p = _mesh.point(vit);
204 writeValue(_out, p[0]);
205 writeValue(_out, p[1]);
206 writeValue(_out, p[2]);
207
208 // Write vertex normals
209 if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL)) {
210 n = _mesh.normal(vit);
211 writeValue(_out, n[0]);
212 writeValue(_out, n[1]);
213 writeValue(_out, n[2]);
214 }
215
216 // Write vertex colors
217 // Note: Vertex colors always have only three components.
218 // This has to be determined since it can not be read dynamically in binary files.
219 if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR)) {
220 c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(vit));
221 writeValue(_out, c[0]);
222 writeValue(_out, c[1]);
223 writeValue(_out, c[2]);
224 }
225
226 // Write vertex texcoords
227 if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS)) {
228 t = _mesh.texcoord2D(vit);
229 writeValue(_out, t[0]);
230 writeValue(_out, t[1]);
231 }
232 }
233
234 // Write face data
235 for(auto fit : _mesh.faces()) {
236
237 // Write face valence
238 writeValue(_out, fit.valence());
239
240 // Write vertex indices
241 for(auto fvit : fit.vertices()) {
242 writeValue(_out, fvit.idx());
243 }
244
245 // Write face colors
246 if(_mesh.has_face_colors() && (userWriteOptions_ & OFFImporter::FACECOLOR)) {
247
248 // Number of color components
249 if(userWriteOptions_ & OFFImporter::COLORALPHA) writeValue(_out, (uint)4);
250 else writeValue(_out, (uint)3);
251
252 // Color itself
253 c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(fit));
254 writeValue(_out, c[0]);
255 writeValue(_out, c[1]);
256 writeValue(_out, c[2]);
257
258 if(userWriteOptions_ & OFFImporter::COLORALPHA) writeValue(_out, c[3]);
259 }
260 }
261
262 return true;
263}
const QString getAllCommentsFlat() const
bool hasComments() const
bool writeASCIIData(std::ostream &_out, MeshT &_mesh)
Write ASCII mesh data to file.
bool writeBinaryData(std::ostream &_out, MeshT &_mesh)
Write binary mesh data to file.
bool writeMesh(std::ostream &_out, MeshT &_mesh, BaseObject &_baseObj)
Writer function.
PointT normal(HalfFaceHandle _hfh) const
size_t valence(VertexHandle _vh) const
Get valence of vertex (number of incident edges)
size_t n_vertices() const override
Get number of vertices in mesh.
size_t n_faces() const override
Get number of faces in mesh.
size_t n_edges() const override
Get number of edges in mesh.