forked from juj/MathGeoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle.cpp
298 lines (258 loc) · 6.77 KB
/
Circle.cpp
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
/* Copyright Jukka Jylänki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/** @file Circle.cpp
@author Jukka Jylänki
@brief Implementation for the Circle geometry object. */
#include "Circle.h"
#include "Plane.h"
#include "../Math/MathFunc.h"
#include "../Math/float2.h"
#include "../Math/float3x3.h"
#include "../Math/float3x4.h"
#include "../Math/float4x4.h"
#include "../Math/Quat.h"
#include "Ray.h"
#include "AABB.h"
#include "OBB.h"
#include "LineSegment.h"
#include "Line.h"
#ifdef MATH_ENABLE_STL_SUPPORT
#include <iostream>
#endif
MATH_BEGIN_NAMESPACE
Circle::Circle(const vec ¢er, const vec &n, float radius)
:pos(center),
normal(n),
r(radius)
{
}
vec Circle::BasisU() const
{
return normal.Perpendicular();
}
vec Circle::BasisV() const
{
return normal.AnotherPerpendicular();
}
vec Circle::GetPoint(float angleRadians) const
{
float sin, cos;
SinCos(angleRadians, sin, cos);
return pos + r * (sin * BasisU() + cos * BasisV());
}
vec Circle::GetPoint(float angleRadians, float d) const
{
float sin, cos;
SinCos(angleRadians, sin, cos);
return pos + r * d * (cos * BasisU() + sin * BasisV());
}
vec Circle::ExtremePoint(const vec &direction) const
{
vec d = direction - direction.ProjectToNorm(normal);
if (d.IsZero())
return pos;
else
return pos + d.ScaledToLength(r);
}
Plane Circle::ContainingPlane() const
{
return Plane(pos, normal);
}
void Circle::Translate(const vec &offset)
{
pos += offset;
}
void Circle::Transform(const float3x3 &transform)
{
assume(transform.HasUniformScale());
assume(transform.IsColOrthogonal());
pos = transform.Mul(pos);
normal = transform.Mul(normal).Normalized();
r *= transform.Col(0).Length(); // Scale the radius of the circle.
}
void Circle::Transform(const float3x4 &transform)
{
assume(transform.HasUniformScale());
assume(transform.IsColOrthogonal());
pos = transform.MulPos(pos);
normal = transform.MulDir(normal).Normalized();
r *= transform.Col(0).Length(); // Scale the radius of the circle.
}
void Circle::Transform(const float4x4 &transform)
{
assume(transform.HasUniformScale());
assume(transform.IsColOrthogonal3());
pos = transform.MulPos(pos);
normal = transform.MulDir(normal).Normalized();
r *= transform.Col3(0).Length(); // Scale the radius of the circle.
}
void Circle::Transform(const Quat &transform)
{
pos = transform.Mul(pos);
normal = transform.Mul(normal);
}
bool Circle::EdgeContains(const vec &point, float maxDistance) const
{
return DistanceToEdge(point) <= maxDistance;
}
/*
bool Circle::DiscContains(const vec &point, float maxDistance) const
{
return DistanceToDisc(point) <= maxDistance;
}
*/
float Circle::DistanceToEdge(const vec &point) const
{
return ClosestPointToEdge(point).Distance(point);
}
/*
float Circle::DistanceToEdge(const Ray &ray, float *d, vec *closestPoint) const
{
float t;
vec cp = ClosestPointToEdge(ray, &t);
if (closestPoint)
*closestPoint = cp;
if (d)
*d = t;
return cp.Distance(ray.GetPoint(t));
}
float Circle::DistanceToEdge(const LineSegment &lineSegment, float *d, vec *closestPoint) const
{
float t;
vec cp = ClosestPointToEdge(lineSegment, &t);
if (closestPoint)
*closestPoint = cp;
if (d)
*d = t;
return cp.Distance(lineSegment.GetPoint(t));
}
float Circle::DistanceToEdge(const Line &line, float *d, vec *closestPoint) const
{
float t;
vec cp = ClosestPointToEdge(line, &t);
if (closestPoint)
*closestPoint = cp;
if (d)
*d = t;
return cp.Distance(line.GetPoint(t));
}
*/
float Circle::DistanceToDisc(const vec &point) const
{
return ClosestPointToDisc(point).Distance(point);
}
vec Circle::ClosestPointToEdge(const vec &point) const
{
vec pointOnPlane = ContainingPlane().Project(point);
vec diff = pointOnPlane - pos;
if (diff.IsZero())
return GetPoint(0); // The point is in the center of the circle, all points are equally close.
return pos + diff.ScaledToLength(r);
}
vec Circle::ClosestPointToDisc(const vec &point) const
{
vec pointOnPlane = ContainingPlane().Project(point);
vec diff = pointOnPlane - pos;
float dist = diff.LengthSq();
if (dist > r*r)
diff = diff * (r / Sqrt(dist));
return pos + diff;
}
int Circle::Intersects(const Plane &plane, vec *pt1, vec *pt2) const
{
return plane.Intersects(*this, pt1, pt2);
}
int Circle::Intersects(const Plane &plane) const
{
return plane.Intersects(*this);
}
bool Circle::IntersectsDisc(const Line &line) const
{
float d;
bool intersectsPlane = line.Intersects(ContainingPlane(), &d);
if (intersectsPlane)
return false;
return line.GetPoint(d).DistanceSq(pos) <= r*r;
}
bool Circle::IntersectsDisc(const LineSegment &lineSegment) const
{
float d;
bool intersectsPlane = lineSegment.Intersects(ContainingPlane(), &d);
if (intersectsPlane)
return false;
return lineSegment.GetPoint(d).DistanceSq(pos) <= r*r;
}
bool Circle::IntersectsDisc(const Ray &ray) const
{
float d;
bool intersectsPlane = ray.Intersects(ContainingPlane(), &d);
if (intersectsPlane)
return false;
return ray.GetPoint(d).DistanceSq(pos) <= r*r;
}
#ifdef MATH_ENABLE_STL_SUPPORT
VecArray Circle::IntersectsFaces(const AABB &aabb) const
{
return IntersectsFaces(aabb.ToOBB());
}
VecArray Circle::IntersectsFaces(const OBB &obb) const
{
VecArray intersectionPoints;
for(int i = 0; i < 6; ++i)
{
Plane p = obb.FacePlane(i);
vec pt1, pt2;
int numIntersections = Intersects(p, &pt1, &pt2);
if (numIntersections >= 1 && obb.Contains(pt1))
intersectionPoints.push_back(pt1);
if (numIntersections >= 2 && obb.Contains(pt2))
intersectionPoints.push_back(pt2);
}
return intersectionPoints;
}
std::string Circle::ToString() const
{
char str[256];
sprintf(str, "Circle(pos:(%.2f, %.2f, %.2f) normal:(%.2f, %.2f, %.2f), r:%.2f)",
pos.x, pos.y, pos.z, normal.x, normal.y, normal.z, r);
return str;
}
std::ostream &operator <<(std::ostream &o, const Circle &circle)
{
o << circle.ToString();
return o;
}
#endif
Circle operator *(const float3x3 &transform, const Circle &circle)
{
Circle c(circle);
c.Transform(transform);
return c;
}
Circle operator *(const float3x4 &transform, const Circle &circle)
{
Circle c(circle);
c.Transform(transform);
return c;
}
Circle operator *(const float4x4 &transform, const Circle &circle)
{
Circle c(circle);
c.Transform(transform);
return c;
}
Circle operator *(const Quat &transform, const Circle &circle)
{
Circle c(circle);
c.Transform(transform);
return c;
}
MATH_END_NAMESPACE