-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircArc.h
230 lines (191 loc) · 8.13 KB
/
CircArc.h
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
// ==========================================================================
// Copyright (C) 2013 Lior Kogan ([email protected])
// ==========================================================================
// classes defined here:
// CircArc - circular arc
// CircArcTester - tester for CircArc class
// ==========================================================================
#pragma once
#include <cmath>
#include <assert.h>
#include "CircVal.h" // CircVal, CircValTypeDef
// ==========================================================================
// circular arc length
// Type should be defined using the CircValTypeDef macro
template <typename Type>
class CircArcLen
{
double l; // the arc length [0, Type::R]
public:
// ---------------------------------------------
operator double() const
{
return l;
}
// ---------------------------------------------
// construction based on a floating-point value
// floating-point is truncated into the range [0, Type::R]
CircArcLen(double r) : l(__min(__max(0., r), Type::R))
{
}
// construction based on a circular arc length of the same type
CircArcLen(const CircArcLen& c) : l(c.l)
{
}
// construction based on a circular arc length another type
// sample use: CircArcLen<SignedRadRange> c= c2; -or- CircArcLen<SignedRadRange> c(c2);
template<typename Type2>
CircArcLen(const CircArcLen<Type2>& c) : l((c == Type2::R) ? Type::R : Type::R/Type2::R * c) // ... to avoid rounding errors
{
}
// ---------------------------------------------
// assignment from a floating-point value
// floating-point is truncated into the range [0, Type::R]
CircArcLen& operator= (double r)
{
l = __min(__max(0., r), Type::R);
return *this;
}
// assignment from another type of circular arc length
template<typename Type2>
CircArcLen& operator= (const CircArcLen<Type2>& c)
{
l = (c == Type2::R) ? Type::R : Type::R/Type2::R * c; // ... to avoid rounding errors
return *this;
}
};
// ==========================================================================
// circular arc
// Type should be defined using the CircValTypeDef macro
template <typename Type>
class CircArc
{
// the arc [c1,c1+l] is defined by the shortest increasing walk from c1 to c1+l, unless l=Type::R where the arc is defined as the whole circle
CircVal <Type> c1; // the arc start-point [Type::L, Type::H)
CircVal <Type> c2; // the arc end -point [Type::L, Type::H). Note that c2=c1 in two cases: (a) l=0 (b) l=Type::R
CircArcLen<Type> l ; // the arc length [0 , Type::R]
// ---------------------------------------------
public:
CircVal <Type> GetC1() const { return c1; } // the arc start-point [Type::L, Type::H)
CircVal <Type> GetC2() const { return c2; } // the arc end -point [Type::L, Type::H). Note that c2=c1 in two cases: (a) l=0 (b) l=Type::R
CircArcLen<Type> GetL () const { return l ; } // the arc length [0 , Type::R]
// ---------------------------------------------
CircArc() : c1(Type::Z), c2(Type::Z), l(0)
{
}
// "reference" construction:
// construction based on CircVal (arc start point) and CircArcLen (arc length)
template<typename Type2, typename Type3>
CircArc(const CircVal<Type2>& _c1, const CircArcLen<Type3>& _l) : c1(_c1), c2((double)CircVal<Type>(c1)+(double)CircArcLen<Type>(_l)), l(_l)
{
}
// construction based on two floating-point values (arc start-point, arc length)
// c1 is wrapped into the range, r is truncated into the range
CircArc(double fc1, double fl) : c1(fc1), c2(fc1+CircArcLen<Type>(fl)), l(fl)
{
}
// construction based on two circular values of same/different type (arc start-point, arc end-point)
// note that if c1==c2, the arc length will be 0
template<typename Type2, typename Type3>
CircArc(const CircVal<Type2>& _c1, const CircVal<Type3>& _c2) : c1(_c1), c2(_c2), l(CircVal<Type>::Pdist(c1, c2))
{
}
// construction based on another circular arc of same/different circular-value type
// sample use: CircArc<SignedRadRange> a= a2; -or- CircArc<SignedRadRange> a(a2);
template<typename Type2>
CircArc(const CircArc<Type2>& a) : c1(a.GetC1()), c2(a.GetC2()), l(a.GetL())
{
}
// ---------------------------------------------
// assignment from a circular arc of same/different circular arc type
template<typename Type2>
CircArc& operator= (const CircArc<Type2>& a)
{
c1 = a.GetC1();
c2 = a.GetC2();
l = a.GetL ();
return *this;
}
// ---------------------------------------------
bool operator==(const CircArc& a) const
{
if ( (l == Type::R) && (a.l == Type::R) ) // both are full-circle; start-point doesn't matter
return true;
return (c1 == a.c1) && (l == a.l);
}
bool operator!=(const CircArc& a) const
{
if ( (l == Type::R) && (a.l == Type::R) ) // both are full-circle; start-point doesn't matter
return false;
return (c1 != a.c1) || (l != a.l);
}
// check if this arc contains a circular value (note that arc contains its endpoints)
bool Contains(const CircVal<Type>& c) const
{
return l - CircVal<Type>::Pdist(c1, c) > -1e-12;
}
// check if this arc contains another circular arc (note that arcs contain their endpoints)
bool Contains(const CircArc& a) const
{
if ( l == Type::R) return true ; // full-circle
if (a.l == Type::R) return false; // full-circle
// ensure order: c1 --- a.c1 --- a.c2 --- c2
const double l1= CircVal<Type>::Pdist(c1, a.c1);
const double l2= CircVal<Type>::Pdist(c1, a.c2);
return (l2 - l1 > -1e-12) && (l - l2 > -1e-12);
}
// check if two circular arcs intersect (note that arcs contain their endpoints)
bool Intersect(const CircArc& a) const
{
return Contains(a.c1) || a.Contains(c1); // one of them contains the start of the other
}
};
// ==========================================================================
// circular arcs
// Type should be defined using the CircValTypeDef macro
template <typename Type>
class CircArcs
{
};
// CircArcs Union (const CircArc& a);
// CircArcs Intersection(const CircArc& a);
// CircArcs Diff (const CircArc& a);
// ==========================================================================
// tester for CircVal class
template <typename Type>
class CircArcTester
{
public:
CircArcTester()
{
Test();
}
static void Test()
{
const unsigned nSteps = 36 ;
const double fStep = Type::R / nSteps;
unsigned m = 0, n = 0, p = 0, q[nSteps+1];
for (int i = 0; i<nSteps; ++i)
q[i] = 0;
for (int i = 0; i < nSteps; ++i)
for (int j = 0; j <= nSteps; ++j)
{
CircArc<Type> a1(Type::L+i*fStep, j*fStep); // start-point, length
for (int k = 0; k < nSteps; ++k)
for (int l = 0; l <= nSteps; ++l)
{
CircArc<Type> a2(Type::L+k*fStep, l*fStep); // start-point, length
bool b1 = a1.Contains(a2); if (b1) ++m;
bool b2 = a2.Contains(a1); if (b2) ++n;
if (a1 == a2) { assert( b1 && b2 ); ++p; }
else assert(!(b1 && b2));
if (a1.Intersect(a2)) ++q[j];
}
}
assert (p == 2*nSteps*nSteps ); // number of identical arcs
assert (m == nSteps*nSteps*(nSteps*nSteps+9*nSteps+8)/6); // number of sub-arcs
assert (m == n );
// assert (q == ...) // number of intersecting arcs
// --------------
}
};