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