Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
FileOFFT.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #define FILEOFFPLUGIN_C
51 
52 #include "FileOFF.hh"
53 
54 #include <OpenMesh/Core/Utils/color_cast.hh>
55 #include <OpenMesh/Core/Geometry/VectorT.hh>
56 #include <sstream>
57 
58 
59 template< class MeshT >
60 bool FileOFFPlugin::writeMesh(std::ostream& _out, MeshT& _mesh, BaseObject &_baseObj){
61 
62  /*****************
63  * HEADER
64  ******************/
65 
66  // Write option ST
67  if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS) ) {
68  _out << "ST";
69  }
70 
71  // Write option C
72  if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR) ) {
73  _out << "C";
74  }
75 
76  // Write option N
77  if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL) ) {
78  _out << "N";
79  }
80 
81  // Write
82  _out << "OFF";
83 
84  // Write BINARY
85  if(userWriteOptions_ & OFFImporter::BINARY) {
86  _out << " BINARY";
87  }
88 
89  _out << "\n";
90 
91 
92  /*
93  * Comment
94  */
96 
97  if (_baseObj.hasComments()) {
98  _out << "# %% BEGIN OPENFLIPPER_COMMENT %%" << std::endl;
99  std::istringstream comment(_baseObj.getAllCommentsFlat().toStdString());
100  std::string commentLine;
101  while (std::getline(comment, commentLine)) {
102  _out << "# " << commentLine << std::endl;
103  }
104  _out << "# %% END OPENFLIPPER_COMMENT %%" << std::endl;
105  }
106 
107  /*****************
108  * DATA
109  ******************/
110 
111  // Call corresponding write routine
112  if(userWriteOptions_ & OFFImporter::BINARY) {
113  return writeBinaryData(_out, _mesh);
114  } else {
115  if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0)
116  _out.precision(savePrecision_->value());
117 
118  return writeASCIIData(_out, _mesh);
119  }
120 }
121 
122 //------------------------------------------------------------------------------------------------------
123 
124 template< class MeshT >
125 bool FileOFFPlugin::writeASCIIData(std::ostream& _out, MeshT& _mesh ) {
126 
127  typename MeshT::Point p;
128  typename MeshT::Normal n;
129  typename OpenMesh::Vec4f c;
130  typename MeshT::TexCoord2D t;
131 
132  typename MeshT::VertexIter vit = _mesh.vertices_begin();
133  typename MeshT::VertexIter end_vit = _mesh.vertices_end();
134 
135  // #V #F #E
136  _out << _mesh.n_vertices() << " " << _mesh.n_faces() << " " << _mesh.n_edges();
137 
138  // Write vertex data
139  for(; vit != end_vit; ++vit) {
140 
141  _out << "\n";
142 
143  // Write vertex p[0] p[1] p[2]
144  p = _mesh.point(*vit);
145  _out << p[0] << " " << p[1] << " " << p[2];
146 
147  // Write vertex normals
148  if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL)) {
149  n = _mesh.normal(*vit);
150  _out << " " << n[0] << " " << n[1] << " " << n[2];
151  }
152 
153  // Write vertex colors
154  // Note: Vertex colors always have only three components.
155  // This has to be determined since it can not be read dynamically in binary files.
156  if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR)) {
157  c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(*vit));
158  _out << " " << std::showpoint << c[0] << " " << std::showpoint << c[1] << " " << std::showpoint << c[2] << " " << std::showpoint << c[3];
159  }
160 
161  // Write vertex texcoords
162  if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS)) {
163  t = _mesh.texcoord2D(*vit);
164  _out << " " << t[0] << " " << t[1];
165  }
166  }
167 
168  typename MeshT::FaceIter fit = _mesh.faces_begin();
169  typename MeshT::FaceIter end_fit = _mesh.faces_end();
170  typename MeshT::FaceVertexIter fvit;
171 
172  // Write face data
173  for(; fit != end_fit; ++fit) {
174 
175  _out << "\n";
176 
177  // Write face valence
178  _out << _mesh.valence(*fit);
179 
180  // Get face-vertex iterator
181  fvit = _mesh.fv_iter(*fit);
182 
183  // Write vertex indices
184  for(;fvit.is_valid(); ++fvit) {
185  _out << " " << fvit->idx();
186  }
187 
188  // Write face colors
189  if(_mesh.has_face_colors() && (userWriteOptions_ & OFFImporter::FACECOLOR ) ) {
190  c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(*fit));
191  _out << " " << std::showpoint << c[0] << " " << std::showpoint << c[1] << " " << std::showpoint << c[2];
192 
193  if(userWriteOptions_ & OFFImporter::COLORALPHA) _out << " " << std::showpoint << c[3];
194  }
195  }
196 
197  return true;
198 }
199 
200 //------------------------------------------------------------------------------------------------------
201 
202 template< class MeshT >
203 bool FileOFFPlugin::writeBinaryData(std::ostream& _out, MeshT& _mesh ){
204 
205  Vec3f v, n;
206  Vec2f t;
207  OpenMesh::Vec4f c(1.0,1.0,1.0,1.0);
208  OpenMesh::Vec3f p;
209 
210  typename MeshT::VertexIter vit = _mesh.vertices_begin();
211  typename MeshT::VertexIter end_vit = _mesh.vertices_end();
212 
213  // #vertices, #faces, #edges
214  writeValue(_out, (uint)_mesh.n_vertices() );
215  writeValue(_out, (uint)_mesh.n_faces() );
216  writeValue(_out, (uint)_mesh.n_edges() );
217 
218  // Write vertex data
219  for(; vit != end_vit; ++vit) {
220 
221  // Write vertex p[0] p[1] p[2]
222  p = _mesh.point(*vit);
223  writeValue(_out, p[0]);
224  writeValue(_out, p[1]);
225  writeValue(_out, p[2]);
226 
227  // Write vertex normals
228  if(_mesh.has_vertex_normals() && (userWriteOptions_ & OFFImporter::VERTEXNORMAL)) {
229  n = _mesh.normal(*vit);
230  writeValue(_out, n[0]);
231  writeValue(_out, n[1]);
232  writeValue(_out, n[2]);
233  }
234 
235  // Write vertex colors
236  // Note: Vertex colors always have only three components.
237  // This has to be determined since it can not be read dynamically in binary files.
238  if(_mesh.has_vertex_colors() && (userWriteOptions_ & OFFImporter::VERTEXCOLOR)) {
239  c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(*vit));
240  writeValue(_out, c[0]);
241  writeValue(_out, c[1]);
242  writeValue(_out, c[2]);
243  }
244 
245  // Write vertex texcoords
246  if(_mesh.has_vertex_texcoords2D() && (userWriteOptions_ & OFFImporter::VERTEXTEXCOORDS)) {
247  t = _mesh.texcoord2D(*vit);
248  writeValue(_out, t[0]);
249  writeValue(_out, t[1]);
250  }
251  }
252 
253  typename MeshT::FaceIter fit = _mesh.faces_begin();
254  typename MeshT::FaceIter end_fit = _mesh.faces_end();
255  typename MeshT::FaceVertexIter fvit;
256 
257  // Write face data
258  for(; fit != end_fit; ++fit) {
259 
260  // Write face valence
261  writeValue(_out, _mesh.valence(*fit));
262 
263  // Get face-vertex iterator
264  fvit = _mesh.fv_iter(*fit);
265 
266  // Write vertex indices
267  for(;fvit.is_valid(); ++fvit) {
268  writeValue(_out, fvit->idx());
269  }
270 
271  // Write face colors
272  if(_mesh.has_face_colors() && (userWriteOptions_ & OFFImporter::FACECOLOR)) {
273 
274  // Number of color components
275  if(userWriteOptions_ & OFFImporter::COLORALPHA) writeValue(_out, (uint)4);
276  else writeValue(_out, (uint)3);
277 
278  // Color itself
279  c = OpenMesh::color_cast<OpenMesh::Vec4f> (_mesh.color(*fit));
280  writeValue(_out, c[0]);
281  writeValue(_out, c[1]);
282  writeValue(_out, c[2]);
283 
284  if(userWriteOptions_ & OFFImporter::COLORALPHA) writeValue(_out, c[3]);
285  }
286  }
287 
288  return true;
289 }
const QString getAllCommentsFlat() const
Definition: BaseObject.hh:593
bool writeMesh(std::ostream &_out, MeshT &_mesh, BaseObject &_baseObj)
Writer function.
Definition: FileOFFT.cc:60
Add 2D texture coordinates (vertices, halfedges)
Definition: Attributes.hh:92
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
bool writeBinaryData(std::ostream &_out, MeshT &_mesh)
Write binary mesh data to file.
Definition: FileOFFT.cc:203
bool writeASCIIData(std::ostream &_out, MeshT &_mesh)
Write ASCII mesh data to file.
Definition: FileOFFT.cc:125
bool hasComments() const
Definition: BaseObject.hh:575