Debian Codegraph @

src/Base/Vector3D.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2005 Imetric 3D GmbH *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <cmath>
26 #include <limits>
27
28 #include <boost/math/special_functions/fpclassify.hpp>
29
30 #include "Vector3D.h"
31 #include "Tools.h"
32
33
34 using namespace Base;
35
36 template<class float_type>
37 Vector3<float_type>::Vector3(float_type fx, float_type fy, float_type fz)
38 : x(fx)
39 , y(fy)
40 , z(fz)
41 {}
42
43 template<class float_type>
44 float_type& Vector3<float_type>::operator[](unsigned short usIndex)
45 {
46 switch (usIndex) {
47 case 0:
48 return x;
49 case 1:
50 return y;
51 case 2:
52 return z;
53 }
54 return x;
55 }
56
57 template<class float_type>
58 const float_type& Vector3<float_type>::operator[](unsigned short usIndex) const
59 {
60 switch (usIndex) {
61 case 0:
62 return x;
63 case 1:
64 return y;
65 case 2:
66 return z;
67 }
68 return x;
69 }
70
71 template<class float_type>
72 Vector3<float_type> Vector3<float_type>::operator+(const Vector3<float_type>& rcVct) const
73 {
74 Vector3<float_type> cVctRes;
75 cVctRes.x = x + rcVct.x;
76 cVctRes.y = y + rcVct.y;
77 cVctRes.z = z + rcVct.z;
78 return cVctRes;
79 }
80
81 template<class float_type>
82 Vector3<float_type> Vector3<float_type>::operator&(const Vector3<float_type>& rcVct) const
83 {
84 Vector3<float_type> cVctRes;
85 cVctRes.x = x * (float_type)fabs(rcVct.x);
86 cVctRes.y = y * (float_type)fabs(rcVct.y);
87 cVctRes.z = z * (float_type)fabs(rcVct.z);
88 return cVctRes;
89 }
90
91 template<class float_type>
92 Vector3<float_type> Vector3<float_type>::operator-(const Vector3<float_type>& rcVct) const
93 {
94 Vector3<float_type> cVctRes;
95 cVctRes.x = x - rcVct.x;
96 cVctRes.y = y - rcVct.y;
97 cVctRes.z = z - rcVct.z;
98 return cVctRes;
99 }
100
101 template<class float_type>
102 Vector3<float_type> Vector3<float_type>::operator-() const
103 {
104 return Vector3(-x, -y, -z);
105 }
106
107 template<class float_type>
108 Vector3<float_type>& Vector3<float_type>::operator+=(const Vector3<float_type>& rcVct)
109 {
110 x += rcVct.x;
111 y += rcVct.y;
112 z += rcVct.z;
113 return *this;
114 }
115
116 template<class float_type>
117 Vector3<float_type>& Vector3<float_type>::operator-=(const Vector3<float_type>& rcVct)
118 {
119 x -= rcVct.x;
120 y -= rcVct.y;
121 z -= rcVct.z;
122 return *this;
123 }
124
125 template<class float_type>
126 Vector3<float_type>& Vector3<float_type>::operator*=(float_type fScale)
127 {
128 x *= fScale;
129 y *= fScale;
130 z *= fScale;
131 return *this;
132 }
133
134 template<class float_type>
135 Vector3<float_type>& Vector3<float_type>::operator/=(float_type fDiv)
136 {
137 x /= fDiv;
138 y /= fDiv;
139 z /= fDiv;
140 return *this;
141 }
142
143 template<class float_type>
144 Vector3<float_type> Vector3<float_type>::operator*(float_type fScale) const
145 {
146 return Vector3<float_type>(this->x * fScale, this->y * fScale, this->z * fScale);
147 }
148
149 template<class float_type>
150 Vector3<float_type> Vector3<float_type>::operator/(float_type fDiv) const
151 {
152 return Vector3<float_type>(this->x / fDiv, this->y / fDiv, this->z / fDiv);
153 }
154
155 template<class float_type>
156 float_type Vector3<float_type>::operator*(const Vector3<float_type>& rcVct) const
157 {
158 return (x * rcVct.x) + (y * rcVct.y) + (z * rcVct.z);
159 }
160
161 template<class float_type>
162 float_type Vector3<float_type>::Dot(const Vector3<float_type>& rcVct) const
163 {
164 return (x * rcVct.x) + (y * rcVct.y) + (z * rcVct.z);
165 }
166
167 template<class float_type>
168 Vector3<float_type> Vector3<float_type>::operator%(const Vector3<float_type>& rcVct) const
169 {
170 Vector3<float_type> cVctRes;
171 cVctRes.x = (y * rcVct.z) - (z * rcVct.y);
172 cVctRes.y = (z * rcVct.x) - (x * rcVct.z);
173 cVctRes.z = (x * rcVct.y) - (y * rcVct.x);
174 return cVctRes;
175 }
176
177 template<class float_type>
178 Vector3<float_type> Vector3<float_type>::Cross(const Vector3<float_type>& rcVct) const
179 {
180 Vector3<float_type> cVctRes;
181 cVctRes.x = (y * rcVct.z) - (z * rcVct.y);
182 cVctRes.y = (z * rcVct.x) - (x * rcVct.z);
183 cVctRes.z = (x * rcVct.y) - (y * rcVct.x);
184 return cVctRes;
185 }
186
187 template<class float_type>
188 bool Vector3<float_type>::IsOnLineSegment(
189 const Vector3<float_type>& startVct,
190 const Vector3<float_type>& endVct
191 ) const
192 {
193 Vector3<float_type> vectorAB = endVct - startVct;
194 Vector3<float_type> vectorAC = *this - startVct;
195 Vector3<float_type> crossproduct = vectorAB.Cross(vectorAC);
196 float_type dotproduct = vectorAB.Dot(vectorAC);
197
198 if (crossproduct.Length() > traits_type::epsilon()) {
199 return false;
200 }
201
202 if (dotproduct < 0) {
203 return false;
204 }
205
206 if (dotproduct > vectorAB.Sqr()) {
207 return false;
208 }
209
210 return true;
211 }
212
213 template<class float_type>
214 bool Vector3<float_type>::operator!=(const Vector3<float_type>& rcVct) const
215 {
216 return !((*this) == rcVct);
217 }
218
219 template<class float_type>
220 bool Vector3<float_type>::operator==(const Vector3<float_type>& rcVct) const
221 {
222 return (std::fabs(x - rcVct.x) <= traits_type::epsilon())
223 && (std::fabs(y - rcVct.y) <= traits_type::epsilon())
224 && (std::fabs(z - rcVct.z) <= traits_type::epsilon());
225 }
226
227 template<class float_type>
228 bool Vector3<float_type>::IsEqual(const Vector3<float_type>& rclPnt, float_type tol) const
229 {
230 return Distance(*this, rclPnt) <= tol;
231 }
232
233 template<class float_type>
234 bool Vector3<float_type>::IsParallel(const Vector3<float_type>& rclDir, float_type tol) const
235 {
236 float_type angle = GetAngle(rclDir);
237 if (boost::math::isnan(angle)) {
238 return false;
239 }
240
241 return angle <= tol || traits_type::pi() - angle <= tol;
242 }
243
244 template<class float_type>
245 bool Vector3<float_type>::IsNormal(const Vector3<float_type>& rclDir, float_type tol) const
246 {
247 float_type angle = GetAngle(rclDir);
248 if (boost::math::isnan(angle)) {
249 return false;
250 }
251
252 float_type diff = std::abs(traits_type::pi() / 2.0 - angle); // NOLINT
253 return diff <= tol;
254 }
255
256 template<class float_type>
257 Vector3<float_type>& Vector3<float_type>::ProjectToPlane(
258 const Vector3<float_type>& rclBase,
259 const Vector3<float_type>& rclNorm
260 )
261 {
262 Vector3<float_type> clTemp(rclNorm);
263 *this = *this - (clTemp *= ((*this - rclBase) * clTemp) / clTemp.Sqr());
264 return *this;
265 }
266
267 template<class float_type>
268 void Vector3<float_type>::ProjectToPlane(
269 const Vector3& rclBase,
270 const Vector3& rclNorm,
271 Vector3& rclProj
272 ) const
273 {
274 Vector3<float_type> clTemp(rclNorm);
275 rclProj = *this - (clTemp *= ((*this - rclBase) * clTemp) / clTemp.Sqr());
276 }
277
278 template<class float_type>
279 float_type Vector3<float_type>::DistanceToPlane(
280 const Vector3<float_type>& rclBase,
281 const Vector3<float_type>& rclNorm
282 ) const
283 {
284 return ((*this - rclBase) * rclNorm) / rclNorm.Length();
285 }
286
287 template<class float_type>
288 float_type Vector3<float_type>::Length() const
289 {
290 return static_cast<float_type>(std::sqrt((x * x) + (y * y) + (z * z)));
291 }
292
293 template<class float_type>
294 float_type Vector3<float_type>::DistanceToLine(
295 const Vector3<float_type>& base,
296 const Vector3<float_type>& dir
297 ) const
298 {
299 // clang-format off
300 return static_cast<float_type>(std::fabs((dir % Vector3(*this - base)).Length() / dir.Length()));
301 // clang-format on
302 }
303
304 template<class float_type>
305 Vector3<float_type> Vector3<float_type>::DistanceToLineSegment(
306 const Vector3& rclP1,
307 const Vector3& rclP2
308 ) const
309 {
310 float_type len2 = Base::DistanceP2(rclP1, rclP2);
311 if (len2 == 0) {
312 return rclP1;
313 }
314
315 Vector3<float_type> p2p1 = rclP2 - rclP1;
316 Vector3<float_type> pXp1 = *this - rclP1;
317 float_type dot = pXp1 * p2p1;
318 float_type t = clamp<float_type>(dot / len2, 0, 1);
319 Vector3<float_type> dist = t * p2p1 - pXp1;
320 return dist;
321 }
322
323 template<class float_type>
324 Vector3<float_type>& Vector3<float_type>::ProjectToLine(
325 const Vector3<float_type>& rclPoint,
326 const Vector3<float_type>& rclLine
327 )
328 {
329 return (*this = ((((rclPoint * rclLine) / rclLine.Sqr()) * rclLine) - rclPoint));
330 }
331
332 template<class float_type>
333 Vector3<float_type> Vector3<float_type>::Perpendicular(
334 const Vector3<float_type>& rclBase,
335 const Vector3<float_type>& rclDir
336 ) const
337 {
338 float_type t = ((*this - rclBase) * rclDir) / (rclDir * rclDir);
339 return rclBase + t * rclDir;
340 }
341
342 template<class float_type>
343 float_type Vector3<float_type>::Sqr() const
344 {
345 return (float_type)((x * x) + (y * y) + (z * z));
346 }
347
348 template<class float_type>
349 void Vector3<float_type>::Set(float_type fX, float_type fY, float_type fZ)
350 {
351 x = fX;
352 y = fY;
353 z = fZ;
354 }
355
356 template<class float_type>
357 void Vector3<float_type>::ScaleX(float_type f)
358 {
359 x *= f;
360 }
361
362 template<class float_type>
363 void Vector3<float_type>::ScaleY(float_type f)
364 {
365 y *= f;
366 }
367
368 template<class float_type>
369 void Vector3<float_type>::ScaleZ(float_type f)
370 {
371 z *= f;
372 }
373
374 template<class float_type>
375 void Vector3<float_type>::Scale(float_type fX, float_type fY, float_type fZ)
376 {
377 x *= fX;
378 y *= fY;
379 z *= fZ;
380 }
381
382 template<class float_type>
383 void Vector3<float_type>::MoveX(float_type f)
384 {
385 x += f;
386 }
387
388 template<class float_type>
389 void Vector3<float_type>::MoveY(float_type f)
390 {
391 y += f;
392 }
393
394 template<class float_type>
395 void Vector3<float_type>::MoveZ(float_type f)
396 {
397 z += f;
398 }
399
400 template<class float_type>
401 void Vector3<float_type>::Move(float_type fX, float_type fY, float_type fZ)
402 {
403 x += fX;
404 y += fY;
405 z += fZ;
406 }
407
408 template<class float_type>
409 void Vector3<float_type>::RotateX(float_type f)
410 {
411 Vector3 cPt(*this);
412
413 float_type fsin = static_cast<float_type>(sin(f));
414 float_type fcos = static_cast<float_type>(cos(f));
415 y = (cPt.y * fcos) - (cPt.z * fsin);
416 z = (cPt.y * fsin) + (cPt.z * fcos);
417 }
418
419 template<class float_type>
420 void Vector3<float_type>::RotateY(float_type f)
421 {
422 Vector3 cPt(*this);
423
424 float_type fsin = static_cast<float_type>(sin(f));
425 float_type fcos = static_cast<float_type>(cos(f));
426 x = (cPt.z * fsin) + (cPt.x * fcos);
427 z = (cPt.z * fcos) - (cPt.x * fsin);
428 }
429
430 template<class float_type>
431 void Vector3<float_type>::RotateZ(float_type f)
432 {
433 Vector3 cPt(*this);
434
435 float_type fsin = static_cast<float_type>(sin(f));
436 float_type fcos = static_cast<float_type>(cos(f));
437 x = (cPt.x * fcos) - (cPt.y * fsin);
438 y = (cPt.x * fsin) + (cPt.y * fcos);
439 }
440
441 template<class float_type>
442 Vector3<float_type>& Vector3<float_type>::Normalize()
443 {
444 float_type fLen = Length();
445 if (fLen != static_cast<float_type>(0.0) && fLen != static_cast<float_type>(1.0)) {
446 x /= fLen;
447 y /= fLen;
448 z /= fLen;
449 }
450 return *this;
451 }
452
453 template<class float_type>
454 Vector3<float_type> Vector3<float_type>::Normalized() const
455 {
456 Vector3<float_type> copy = *this;
457 copy.Normalize();
458
459 return copy;
460 }
461
462 template<class float_type>
463 bool Vector3<float_type>::IsNull() const
464 {
465 float_type n {0.0};
466 return (x == n) && (y == n) && (z == n);
467 }
468
469 template<class float_type>
470 float_type Vector3<float_type>::GetAngle(const Vector3& rcVect) const
471 {
472 float_type len1 = Length();
473 float_type len2 = rcVect.Length();
474 if (len1 <= traits_type::epsilon() || len2 <= traits_type::epsilon()) {
475 return std::numeric_limits<float_type>::quiet_NaN(); // division by zero
476 }
477
478 float_type dot = Dot(rcVect);
479 dot /= len1;
480 dot /= len2;
481
482 if (dot <= -1.0) {
483 return traits_type::pi();
484 }
485 if (dot >= 1.0) {
486 return 0.0;
487 }
488
489 return float_type(acos(dot));
490 }
491
492 template<class float_type>
493 float_type Vector3<float_type>::GetAngleOriented(const Vector3& rcVect, const Vector3& norm) const
494 {
495 float_type angle = GetAngle(rcVect);
496
497 Vector3<float_type> crossProduct = Cross(rcVect);
498
499 // Use dot product to determine the sign
500 float_type dot = crossProduct.Dot(norm);
501 if (dot < 0) {
502 angle = 2 * traits_type::pi() - angle;
503 }
504
505 return angle;
506 }
507
508 template<class float_type>
509 void Vector3<float_type>::TransformToCoordinateSystem(
510 const Vector3& rclBase,
511 const Vector3& rclDirX,
512 const Vector3& rclDirY
513 )
514 {
515 Vector3 clVectX;
516 Vector3 clVectY;
517 Vector3 clVectZ;
518 Vector3 clVectOld;
519
520 clVectX = rclDirX;
521 clVectY = rclDirY;
522 clVectZ = rclDirX % rclDirY;
523 clVectX.Normalize();
524 clVectY.Normalize();
525 clVectZ.Normalize();
526
527 clVectOld = *this - rclBase;
528
529 x = clVectX * clVectOld;
530 y = clVectY * clVectOld;
531 z = clVectZ * clVectOld;
532 }
533
534 // explicit template instantiation
535 namespace Base
536 {
537 template class BaseExport Vector3<float>;
538 template class BaseExport Vector3<double>;
539 } // namespace Base