summaryrefslogtreecommitdiff
path: root/src/lib/VSDXGeometryList.cpp
blob: c0ce8d8c7a4bbdf0a99c1905afa9567cb0934879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* libvisio
 * Version: MPL 1.1 / GPLv2+ / LGPLv2+
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License or as specified alternatively below. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Major Contributor(s):
 * Copyright (C) 2011 Fridrich Strba <fridrich.strba@bluewin.ch>
 * Copyright (C) 2011 Eilidh McAdam <tibbylickle@gmail.com>
 *
 *
 * All Rights Reserved.
 *
 * For minor contributions see the git repository.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
 * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
 * instead of those above.
 */

#include "VSDXCollector.h"
#include "VSDXGeometryList.h"
#include "libvisio_utils.h"

namespace libvisio {


class VSDXGeometry : public VSDXGeometryListElement
{
public:
  VSDXGeometry(unsigned id, unsigned level, unsigned geomFlags) :
    m_id(id), m_level(level), m_geomFlags(geomFlags) {}
  ~VSDXGeometry() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id;
  unsigned m_level;
  unsigned char m_geomFlags;
};

class VSDXMoveTo : public VSDXGeometryListElement
{
public:
  VSDXMoveTo(unsigned id, unsigned level, double x, double y) :
    m_id(id), m_level(level), m_x(x), m_y(y) {}
  ~VSDXMoveTo() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x, m_y;
};

class VSDXLineTo : public VSDXGeometryListElement
{
public:
  VSDXLineTo(unsigned id, unsigned level, double x, double y) :
    m_id(id), m_level(level), m_x(x), m_y(y) {}
  ~VSDXLineTo() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x, m_y;
};

class VSDXArcTo : public VSDXGeometryListElement
{
public:
  VSDXArcTo(unsigned id, unsigned level, double x2, double y2, double bow) :
    m_id(id), m_level(level), m_x2(x2), m_y2(y2), m_bow(bow) {}
  ~VSDXArcTo() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x2, m_y2, m_bow;
};

class VSDXEllipse : public VSDXGeometryListElement
{
public:
  VSDXEllipse(unsigned id, unsigned level, double cx, double cy, double xleft, double yleft, double xtop, double ytop) :
    m_id(id), m_level(level), m_cx(cx), m_cy(cy), m_xleft(xleft), m_yleft(yleft), m_xtop(xtop), m_ytop(ytop) {}
  ~VSDXEllipse() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_cx, m_cy, m_xleft, m_yleft, m_xtop, m_ytop;
};

class VSDXEllipticalArcTo : public VSDXGeometryListElement
{
public:
  VSDXEllipticalArcTo(unsigned id, unsigned level, double x3, double y3, double x2, double y2, double angle, double ecc) :
    m_id(id), m_level(level), m_x3(x3), m_y3(y3), m_x2(x2), m_y2(y2), m_angle(angle), m_ecc(ecc) {}
  ~VSDXEllipticalArcTo() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x3, m_y3, m_x2, m_y2, m_angle, m_ecc;
};

class VSDXNURBSTo1 : public VSDXGeometryListElement
{
public:
  VSDXNURBSTo1(unsigned id, unsigned level, double x2, double y2, unsigned char xType, unsigned char yType, unsigned degree, std::vector<std::pair<double, double> > controlPoints, std::vector<double> knotVector, std::vector<double> weights) :
    m_id(id), m_level(level), m_x2(x2), m_y2(y2), m_xType(xType), m_yType(yType), m_degree(degree), m_controlPoints(controlPoints), m_knotVector(knotVector), m_weights(weights) {}
  ~VSDXNURBSTo1() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x2, m_y2;
  unsigned m_xType, m_yType;
  unsigned m_degree;
  std::vector<std::pair<double, double> > m_controlPoints;
  std::vector<double> m_knotVector, m_weights;
};

class VSDXPolylineTo1 : public VSDXGeometryListElement
{
public:
  VSDXPolylineTo1(unsigned id , unsigned level, double x, double y, unsigned char xType, unsigned char yType, std::vector<std::pair<double, double> > points) :
    m_id(id), m_level(level), m_x(x), m_y(y), m_xType(xType), m_yType(yType), m_points(points) {}
  ~VSDXPolylineTo1() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x, m_y;
  unsigned m_xType, m_yType;
  std::vector<std::pair<double, double> > m_points;
};

class VSDXSplineStart : public VSDXGeometryListElement
{
public:
  VSDXSplineStart(unsigned id, unsigned level, double x, double y, double secondKnot, double firstKnot, double lastKnot, unsigned degree) :
    m_id(id), m_level(level), m_x(x), m_y(y), m_secondKnot(secondKnot), m_firstKnot(firstKnot), m_lastKnot(lastKnot), m_degree(degree) {}
  ~VSDXSplineStart() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x, m_y;
  double m_secondKnot, m_firstKnot, m_lastKnot;
  unsigned m_degree;
};

class VSDXSplineKnot : public VSDXGeometryListElement
{
public:
  VSDXSplineKnot(unsigned id, unsigned level, double x, double y, double knot) :
    m_id(id), m_level(level), m_x(x), m_y(y), m_knot(knot) {}
  ~VSDXSplineKnot() {}
  void handle(VSDXCollector *collector);
  VSDXGeometryListElement *clone();
private:
  unsigned m_id, m_level;
  double m_x, m_y;
  double m_knot;
};
} // namespace libvisio


void libvisio::VSDXGeometry::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectGeometry(m_id, m_level, m_geomFlags);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXGeometry::clone()
{
  return new VSDXGeometry(m_id, m_level, m_geomFlags);
}


void libvisio::VSDXMoveTo::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectMoveTo(m_id, m_level, m_x, m_y);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXMoveTo::clone()
{
  return new VSDXMoveTo(m_id, m_level, m_x, m_y);
}


void libvisio::VSDXLineTo::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectLineTo(m_id, m_level, m_x, m_y);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXLineTo::clone()
{
  return new VSDXLineTo(m_id, m_level, m_x, m_y);
}


void libvisio::VSDXArcTo::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectArcTo(m_id, m_level, m_x2, m_y2, m_bow);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXArcTo::clone()
{
  return new VSDXArcTo(m_id, m_level, m_x2, m_y2, m_bow);
}


void libvisio::VSDXEllipse::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectEllipse(m_id, m_level, m_cx, m_cy, m_xleft, m_yleft, m_xtop, m_ytop);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXEllipse::clone()
{
  return new VSDXEllipse(m_id, m_level, m_cx, m_cy, m_xleft, m_yleft, m_xtop, m_ytop);
}


void libvisio::VSDXEllipticalArcTo::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectEllipticalArcTo(m_id, m_level, m_x3, m_y3, m_x2, m_y2, m_angle, m_ecc);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXEllipticalArcTo::clone()
{
  return new VSDXEllipticalArcTo(m_id, m_level, m_x3, m_y3, m_x2, m_y2, m_angle, m_ecc);
}


void libvisio::VSDXNURBSTo1::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectNURBSTo(m_id, m_level, m_x2, m_y2, m_xType, m_yType, m_degree, m_controlPoints, m_knotVector, m_weights);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXNURBSTo1::clone()
{
  return new VSDXNURBSTo1(m_id, m_level, m_x2, m_y2, m_xType, m_yType, m_degree, m_controlPoints, m_knotVector, m_weights);
}


void libvisio::VSDXNURBSTo2::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectNURBSTo(m_id, m_level, m_x2, m_y2, m_knot, m_knotPrev, m_weight, m_weightPrev, m_dataID);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXNURBSTo2::clone()
{
  return new VSDXNURBSTo2(m_id, m_level, m_x2, m_y2, m_knot, m_knotPrev, m_weight, m_weightPrev, m_dataID);
}


void libvisio::VSDXPolylineTo1::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectPolylineTo(m_id, m_level, m_x, m_y, m_xType, m_yType, m_points);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXPolylineTo1::clone()
{
  return new VSDXPolylineTo1(m_id, m_level, m_x, m_y, m_xType, m_yType, m_points);
}


void libvisio::VSDXPolylineTo2::handle(VSDXCollector *collector)
{
  collector->collectSplineEnd();
  collector->collectPolylineTo(m_id, m_level, m_x, m_y, m_dataID);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXPolylineTo2::clone()
{
  return new VSDXPolylineTo2(m_id, m_level, m_x, m_y, m_dataID);
}


void libvisio::VSDXSplineStart::handle(VSDXCollector *collector)
{
  collector->collectSplineStart(m_id, m_level, m_x, m_y, m_secondKnot, m_firstKnot, m_lastKnot, m_degree);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXSplineStart::clone()
{
  return new VSDXSplineStart(m_id, m_level, m_x, m_y, m_secondKnot, m_firstKnot, m_lastKnot, m_degree);
}


void libvisio::VSDXSplineKnot::handle(VSDXCollector *collector)
{
  collector->collectSplineKnot(m_id, m_level, m_x, m_y, m_knot);
}

libvisio::VSDXGeometryListElement *libvisio::VSDXSplineKnot::clone()
{
  return new VSDXSplineKnot(m_id, m_level, m_x, m_y, m_knot);
}


libvisio::VSDXGeometryList::VSDXGeometryList()
{
}

libvisio::VSDXGeometryList::VSDXGeometryList(const VSDXGeometryList &geomList)
{
  std::map<unsigned, VSDXGeometryListElement *>::const_iterator iter = geomList.m_elements.begin();
  for (; iter != geomList.m_elements.end(); iter++)
      m_elements[iter->first] = iter->second->clone();
  m_elementsOrder = geomList.getElementsOrder();
}

libvisio::VSDXGeometryList &libvisio::VSDXGeometryList::operator=(const VSDXGeometryList &geomList)
{
  clear();
  std::map<unsigned, VSDXGeometryListElement *>::const_iterator iter = geomList.m_elements.begin();
  for (; iter != geomList.m_elements.end(); iter++)
      m_elements[iter->first] = iter->second->clone();
  m_elementsOrder = geomList.getElementsOrder();
  return *this;
}

libvisio::VSDXGeometryList::~VSDXGeometryList()
{
  clear();
}

void libvisio::VSDXGeometryList::addGeometry(unsigned id, unsigned level, unsigned char geomFlags)
{
  m_elements[id] = new VSDXGeometry(id, level, geomFlags);
}

void libvisio::VSDXGeometryList::addMoveTo(unsigned id, unsigned level, double x, double y)
{
  m_elements[id] = new VSDXMoveTo(id, level, x, y);
}

void libvisio::VSDXGeometryList::addLineTo(unsigned id, unsigned level, double x, double y)
{
  m_elements[id] = new VSDXLineTo(id, level, x, y);
}

void libvisio::VSDXGeometryList::addArcTo(unsigned id, unsigned level, double x2, double y2, double bow)
{
  m_elements[id] = new VSDXArcTo(id, level, x2, y2, bow);
}

void libvisio::VSDXGeometryList::addNURBSTo(unsigned id, unsigned level, double x2, double y2, unsigned char xType, unsigned char yType, unsigned degree, std::vector<std::pair<double, double> > controlPoints, std::vector<double> knotVector, std::vector<double> weights)
{
  m_elements[id] = new VSDXNURBSTo1(id, level, x2, y2, xType, yType, degree, controlPoints, knotVector, weights);
}

void libvisio::VSDXGeometryList::addNURBSTo(unsigned id, unsigned level, double x2, double y2, double knot, double knotPrev, double weight, double weightPrev, unsigned dataID)
{
  m_elements[id] = new VSDXNURBSTo2(id, level, x2, y2, knot, knotPrev, weight, weightPrev, dataID);
}

void libvisio::VSDXGeometryList::addPolylineTo(unsigned id , unsigned level, double x, double y, unsigned char xType, unsigned char yType, std::vector<std::pair<double, double> > points)
{
  m_elements[id] = new VSDXPolylineTo1(id, level, x, y, xType, yType, points);
}

void libvisio::VSDXGeometryList::addPolylineTo(unsigned id , unsigned level, double x, double y, unsigned dataID)
{
  m_elements[id] = new VSDXPolylineTo2(id, level, x, y, dataID);
}

void libvisio::VSDXGeometryList::addEllipse(unsigned id, unsigned level, double cx, double cy, double xleft, double yleft, double xtop, double ytop)
{
  m_elements[id] = new VSDXEllipse(id, level, cx, cy, xleft, yleft, xtop, ytop);
}

void libvisio::VSDXGeometryList::addEllipticalArcTo(unsigned id, unsigned level, double x3, double y3, double x2, double y2, double angle, double ecc)
{
  m_elements[id] = new VSDXEllipticalArcTo(id, level, x3, y3, x2, y2, angle, ecc);
}

void libvisio::VSDXGeometryList::addSplineStart(unsigned id, unsigned level, double x, double y, double secondKnot, double firstKnot, double lastKnot, unsigned degree)
{
  m_elements[id] = new VSDXSplineStart(id, level, x, y, secondKnot, firstKnot, lastKnot, degree);
}

void libvisio::VSDXGeometryList::addSplineKnot(unsigned id, unsigned level, double x, double y, double knot)
{
  m_elements[id] = new VSDXSplineKnot(id, level, x, y, knot);
}

void libvisio::VSDXGeometryList::setElementsOrder(const std::vector<unsigned> &elementsOrder)
{
  m_elementsOrder.clear();
  for (unsigned i = 0; i<elementsOrder.size(); i++)
    m_elementsOrder.push_back(elementsOrder[i]);
}

void libvisio::VSDXGeometryList::handle(VSDXCollector *collector) const
{
  if (empty())
    return;
  std::map<unsigned, VSDXGeometryListElement *>::const_iterator iter;
  if (m_elementsOrder.size())
  {
    for (unsigned i = 0; i < m_elementsOrder.size(); i++)
    {
      iter = m_elements.find(m_elementsOrder[i]);
      if (iter != m_elements.end())
        iter->second->handle(collector);
    }
  }
  else
  {
    for (iter = m_elements.begin(); iter != m_elements.end(); iter++)
      iter->second->handle(collector);
  }
  collector->collectSplineEnd();
}

void libvisio::VSDXGeometryList::clear()
{
  for (std::map<unsigned, VSDXGeometryListElement *>::iterator iter = m_elements.begin(); iter != m_elements.end(); iter++)
    delete iter->second;
  m_elements.clear();
  m_elementsOrder.clear();
}

libvisio::VSDXGeometryListElement * libvisio::VSDXGeometryList::getElement(unsigned index) const
{
  if (m_elementsOrder.size() > index)
    index = m_elementsOrder[index];    
    
  std::map<unsigned, VSDXGeometryListElement *>::const_iterator iter = m_elements.find(index);
  if (iter != m_elements.end())
    return iter->second;
  else
    return 0;
}