Developer Documentation

PolyLineObjectSerializer.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 #include "PolyLineObjectSerializer.hh"
43
44 #include <ObjectTypes/PolyLine/PolyLineCircleData.hh>
45 #include <ObjectTypes/PolyLine/PolyLineBezierSplineData.hh>
46
47 PolyLineObjectSerializer::PolyLineObjectSerializer(PolyLineObject* _object)
48 : instance(_object)
49 {
50 }
51
52 void PolyLineObjectSerializer::serialize(std::ostream& _stream) {
53 std::ostream& fout = _stream;
54 PolyLine& _polyLine = *instance->line();
55 // is polyline closed?
56 fout << _polyLine.is_closed() << std::endl;
57
58 // number of points
59 fout << _polyLine.n_vertices() << std::endl;
60
61 //std::cerr << "write " << _filename << std::endl;
62 std::cerr << "#points: " << _polyLine.n_vertices() << std::endl;
63
64 fout.precision(14);
65
66 // write each point
67 for(unsigned int i=0; i< _polyLine.n_vertices(); ++i)
68 {
69
70 const PolyLine::Point& current_point = _polyLine.point(i);
71 fout << current_point[0] << " ";
72 fout << current_point[1] << " ";
73 fout << current_point[2] << std::endl;
74 }
75
76 fout << "VERTEXRADIUS" << std::endl << _polyLine.vertex_radius() << std::endl;
77 fout << "EDGERADIUS" << std::endl << _polyLine.edge_radius() << std::endl;
78
79
80 // ###########################
81 // Write properties(unsigned int)CircleType
82
83 if(_polyLine.vertex_vhandles_available())
84 {
85 fout << "VVHANDLES" << std::endl;
86 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
87 fout << _polyLine.vertex_vhandle(i) << std::endl;
88 }
89
90 if(_polyLine.vertex_ehandles_available())
91 {
92 fout << "VEHANDLES" << std::endl;
93 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
94 fout << _polyLine.vertex_ehandle(i) << std::endl;
95 }
96
97 if(_polyLine.vertex_fhandles_available())
98 {
99 fout << "VFHANDLES" << std::endl;
100 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
101 fout << _polyLine.vertex_fhandle(i) << std::endl;
102 }
103
104 if(_polyLine.vertex_scalars_available())
105 {
106 fout << "VSCALARS" << std::endl;
107 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
108 fout << _polyLine.vertex_scalar(i) << std::endl;
109 }
110
111 if(_polyLine.vertex_normals_available())
112 {
113 fout << "VNORMALS" << std::endl;
114 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
115 {
116 const PolyLine::Point& current_vnormal = _polyLine.vertex_normal(i);
117 fout << current_vnormal[0] << " ";
118 fout << current_vnormal[1] << " ";
119 fout << current_vnormal[2] << std::endl;
120 }
121 }
122
123 if(_polyLine.vertex_binormals_available())
124 {
125 fout << "VBINORMALS" << std::endl;
126 for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
127 {
128 const PolyLine::Point& current_bnormal = _polyLine.vertex_binormal(i);
129 fout << current_bnormal[0] << " ";
130 fout << current_bnormal[1] << " ";
131 fout << current_bnormal[2] << std::endl;
132 }
133 }
134
135 PolyLineCircleData* circleData = dynamic_cast<PolyLineCircleData*>(instance->objectData(CIRCLE_DATA));
136 if(circleData) {
137 fout << "CIRCLEDATA" << std::endl;
138 fout << circleData->circleCenter_ << std::endl;
139 fout << circleData->circleMainAxis_ << std::endl;
140 fout << circleData->circleMainRadius_ << std::endl;
141 fout << circleData->circleNormal_ << std::endl;
142 fout << circleData->circleSideAxis_ << std::endl;
143 fout << circleData->circleSideRadius_ << std::endl;
144 }
145
146 PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(instance->objectData(BEZSPLINE_DATA));
147 if(splineData) {
148 fout << "SPLINEDATA" << std::endl;
149 fout << (unsigned int)splineData->points_.size() << std::endl;
150 for(unsigned int i = 0; i < splineData->points_.size(); i++) {
151 fout << splineData->points_[i].normal << std::endl;
152 fout << splineData->points_[i].position << std::endl;
153 }
154 fout << (unsigned int)splineData->handles_.size() << std::endl;
155 for(unsigned int i = 0; i < splineData->handles_.size(); i++)
156 fout << splineData->handles_[i] << std::endl;
157 fout << std::endl;
158 }
159 }
160
161 void PolyLineObjectSerializer::deserialize(std::istream& _stream) {
162 std::istream& fin = _stream;
163 PolyLine& _polyLine = *instance->line();
164
165 // clear old polyline
166 _polyLine.clear();
167 // closed ?
168 bool closed = false;
169 fin >> closed;
170
171 _polyLine.set_closed(closed);
172
173 // number of points
174 int num_points;
175 fin >> num_points;
176
177 // read points
178 for(int i=0; i<num_points; ++i)
179 {
180 PolyLine::Point::value_type x,y,z;
181 fin >> x;
182 fin >> y;
183 fin >> z;
184 PolyLine::Point p(x,y,z);
185 _polyLine.add_point(p);
186 }
187
188 // ###########################
189 // READ properties
190
191 std::string token;
192 while(true)
193 {
194 token = "-";
195 fin >> token;
196 if(token == "VERTEXRADIUS")
197 {
198 double r;
199 fin >> r;
200 _polyLine.set_vertex_radius(r);
201 }
202 else if(token == "EDGERADIUS")
203 {
204 double r;
205 fin >> r;
206 _polyLine.set_edge_radius(r);
207 }
208 else if(token == "VVHANDLES")
209 {
210 if(!_polyLine.vertex_vhandles_available()) _polyLine.request_vertex_vhandles();
211 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
212 fin >> _polyLine.vertex_vhandle(i);
213 }
214 else if(token == "VEHANDLES")
215 {
216 if(!_polyLine.vertex_ehandles_available()) _polyLine.request_vertex_ehandles();
217 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
218 fin >> _polyLine.vertex_ehandle(i);
219 }
220 else if(token == "VFHANDLES")
221 {
222 if(!_polyLine.vertex_fhandles_available()) _polyLine.request_vertex_fhandles();
223 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
224 fin >> _polyLine.vertex_fhandle(i);
225 }
226 else if(token == "VSCALARS")
227 {
228 if(!_polyLine.vertex_scalars_available()) _polyLine.request_vertex_scalars();
229 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
230 fin >> _polyLine.vertex_scalar(i);
231 }
232 else if(token == "VNORMALS")
233 {
234 if(!_polyLine.vertex_normals_available()) _polyLine.request_vertex_normals();
235 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
236 {
237 fin >> _polyLine.vertex_normal(i)[0];
238 fin >> _polyLine.vertex_normal(i)[1];
239 fin >> _polyLine.vertex_normal(i)[2];
240 }
241 }
242 else if(token == "VBINORMALS")
243 {
244 if(!_polyLine.vertex_binormals_available()) _polyLine.request_vertex_binormals();
245 for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
246 {
247 fin >> _polyLine.vertex_binormal(i)[0];
248 fin >> _polyLine.vertex_binormal(i)[1];
249 fin >> _polyLine.vertex_binormal(i)[2];
250 }
251 }
252 else if(token == "CIRCLEDATA") {
253 ACG::Vec3d center, normal, main, side;
254 double rmain, rside;
255 fin >> center;
256 fin >> main;
257 fin >> rmain;
258 fin >> normal;
259 fin >> side;
260 fin >> rside;
261 PolyLineCircleData* circleData = new PolyLineCircleData(center,normal,main,side,rmain,rside,std::numeric_limits<unsigned int>::max());
262 instance->setObjectData(CIRCLE_DATA, circleData);
263 }
264 else if(token == "SPLINEDATA") {
265 PolyLineBezierSplineData* splineData = new PolyLineBezierSplineData(std::numeric_limits<unsigned int>::max());
266 unsigned int pointCount, handleCount;
267 fin >> pointCount;
268 PolyLineBezierSplineData::InterpolatePoint point;
269 for(unsigned int i = 0; i < pointCount; i++) {
270 fin >> point.normal;
271 fin >> point.position;
272 splineData->points_.push_back(point);
273 }
274 fin >> handleCount;
275 for(unsigned int i = 0; i < handleCount; i++) {
276 fin >> point.position;
277 splineData->handles_.push_back(point.position);
278 }
279 instance->setObjectData(BEZSPLINE_DATA, splineData);
280 }
281 else if(token != "") break; //eat up empty lines
282 }
283
284 }
285
286
287
288
289
ACG::PolyLineT::set_edge_radius
void set_edge_radius(const Scalar _r)
set cylinder-radius of edges
Definition: PolyLineT.hh:351
ACG::PolyLineT::add_point
void add_point(const Point &_p)
Append a point to the polyline.
Definition: PolyLineT_impl.hh:283
OpenMesh::VectorT
Definition: Vector11T.hh:83
PolyLineObject
Definition: PolyLineObject.hh:76
PolyLineObjectSerializer.hh
PolyLineCircleData
Definition: PolyLineCircleData.hh:52
ACG::PolyLineT::set_vertex_radius
void set_vertex_radius(const Scalar _r)
set ball-radius of vertices
Definition: PolyLineT.hh:345
ACG::PolyLineT::vertex_radius
Scalar vertex_radius() const
get ball-radius of vertices
Definition: PolyLineT.hh:342
ACG::PolyLineT::point
Point & point(unsigned int _i)
Get a point of the polyline.
Definition: PolyLineT.hh:142
PolyLineBezierSplineData::InterpolatePoint
Definition: PolyLineBezierSplineData.hh:58
ACG::PolyLineT::n_vertices
size_t n_vertices() const
Get number of vertices.
Definition: PolyLineT.hh:116
ACG::PolyLineT::set_closed
void set_closed(const bool _c)
Set if the polyline should be closed and therefore forms a loop.
Definition: PolyLineT.hh:113
ACG::PolyLineT::clear
void clear()
Clear the current polyline.
Definition: PolyLineT_impl.hh:182
ACG::PolyLineT::is_closed
bool is_closed() const
Check if the polyline is marked as closed.
Definition: PolyLineT.hh:107
ACG::PolyLineT::edge_radius
Scalar edge_radius() const
get cylinder-radius of edges
Definition: PolyLineT.hh:348
ACG::PolyLineT
Definition: PolyLineT.hh:84
PolyLineBezierSplineData
Definition: PolyLineBezierSplineData.hh:53