1 : /******************************************************************************
2 : * File: ogrdxf_polyline_smooth.h
3 : *
4 : * Project: Interpolation support for smooth POLYLINE and LWPOLYLINE entities.
5 : * Purpose: Definition of classes for OGR .dxf driver.
6 : * Author: TJ Snider, timsn@thtree.com
7 : * Ray Gardener, Daylon Graphics Ltd.
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2010 Daylon Graphics Ltd.
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 :
32 : #ifndef __OGRDXF_SMOOTH_POLYLINE_H__
33 : #define __OGRDXF_SMOOTH_POLYLINE_H__
34 :
35 : #include "ogrsf_frmts.h"
36 : #include "cpl_conv.h"
37 : #include <vector>
38 : #include "assert.h"
39 :
40 : #ifndef M_PI
41 : #define M_PI 3.14159265358979323846 /* pi */
42 : #endif
43 :
44 :
45 : class DXFSmoothPolylineVertex
46 0 : {
47 : public:
48 : double x, y, z, bulge;
49 :
50 :
51 32 : DXFSmoothPolylineVertex()
52 : {
53 32 : x = y = z = bulge = 0.0;
54 32 : }
55 :
56 :
57 50 : DXFSmoothPolylineVertex(double dfX, double dfY, double dfZ, double dfBulge)
58 : {
59 50 : this->set(dfX, dfY, dfZ, dfBulge);
60 50 : }
61 :
62 :
63 50 : void set(double dfX, double dfY, double dfZ, double dfBulge)
64 : {
65 50 : x = dfX;
66 50 : y = dfY;
67 50 : z = dfZ;
68 50 : bulge = dfBulge;
69 50 : }
70 :
71 :
72 : void scale(double s)
73 : {
74 : x *= s;
75 : y *= s;
76 : }
77 :
78 :
79 8 : double length() const
80 : {
81 8 : return (sqrt(x*x + y*y));
82 : }
83 :
84 :
85 8 : void normalize()
86 : {
87 8 : const double len = this->length();
88 8 : assert(len != 0.0);
89 :
90 8 : x /= len;
91 8 : y /= len;
92 8 : }
93 :
94 :
95 6 : bool shares_2D_pos(const DXFSmoothPolylineVertex& v) const
96 : {
97 6 : return (x == v.x && y == v.y);
98 : }
99 :
100 : };
101 :
102 :
103 : class DXFSmoothPolyline
104 15 : {
105 : // A DXF polyline that includes vertex bulge information.
106 : // Call Tesselate() to convert to an OGRGeometry.
107 : // We treat Z as constant over the entire string; this may
108 : // change in the future.
109 :
110 : private:
111 :
112 : std::vector<DXFSmoothPolylineVertex> m_vertices;
113 : mutable bool m_blinestringstarted;
114 : bool m_bClosed;
115 : int m_dim;
116 :
117 :
118 : public:
119 15 : DXFSmoothPolyline()
120 15 : {
121 15 : m_bClosed = false;
122 15 : m_dim = 2;
123 15 : }
124 :
125 : OGRGeometry* Tesselate() const;
126 :
127 40 : size_t size() { return m_vertices.size(); }
128 :
129 : void SetSize(int n) { m_vertices.reserve(n); }
130 :
131 50 : void AddPoint(double dfX, double dfY, double dfZ, double dfBulge)
132 : {
133 50 : m_vertices.push_back(DXFSmoothPolylineVertex(dfX, dfY, dfZ, dfBulge));
134 50 : }
135 :
136 : void Close();
137 :
138 11 : bool IsEmpty() const { return m_vertices.empty(); }
139 :
140 : bool HasConstantZ(double&) const;
141 :
142 17 : void setCoordinateDimension(int n) { m_dim = n; }
143 :
144 :
145 :
146 : private:
147 : void EmitArc(const DXFSmoothPolylineVertex&, const DXFSmoothPolylineVertex&,
148 : double radius, double len, double saggita,
149 : OGRLineString*, double dfZ = 0.0) const;
150 :
151 : void EmitLine(const DXFSmoothPolylineVertex&, const DXFSmoothPolylineVertex&,
152 : OGRLineString*, bool bConstantZ, double dfZ) const;
153 : };
154 :
155 : #endif /* __OGRDXF_SMOOTH_POLYLINE_H__ */
156 :
|