Debian Codegraph @

src/Base/Vector3D.h

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
26 #ifndef BASE_VECTOR3D_H
27 #define BASE_VECTOR3D_H
28
29 #include <limits>
30 #include <cmath>
31 #include <numbers>
32
33 namespace Base
34 {
35 template<class numT>
36 struct float_traits
37 {
38 };
39
40 // TODO: Remove these specializations and use the default implementation for all types.
41 template<>
42 struct float_traits<float>
43 {
44 using float_type = float;
45 [[nodiscard]] static consteval float_type pi()
46 {
47 return std::numbers::pi_v<float_type>;
48 }
49 [[nodiscard]] static consteval float_type epsilon()
50 {
51 return std::numeric_limits<float_type>::epsilon();
52 }
53 [[nodiscard]] static consteval float_type maximum()
54 {
55 return std::numeric_limits<float_type>::max();
56 }
57 };
58
59 template<>
60 struct float_traits<double>
61 {
62 using float_type = double;
63 [[nodiscard]] static consteval float_type pi()
64 {
65 return std::numbers::pi_v<float_type>;
66 }
67 [[nodiscard]] static consteval float_type epsilon()
68 {
69 return std::numeric_limits<float_type>::epsilon();
70 }
71 [[nodiscard]] static consteval float_type maximum()
72 {
73 return std::numeric_limits<float_type>::max();
74 }
75 };
76
77 /** The Vector Base class. */
78 template<class float_type>
79 class Vector3
80 {
81 public:
82 static const Vector3 UnitX;
83 static const Vector3 UnitY;
84 static const Vector3 UnitZ;
85
86 using num_type = float_type;
87 using traits_type = float_traits<num_type>;
88 [[nodiscard]] static constexpr num_type epsilon()
89 {
90 return traits_type::epsilon();
91 }
92
93 /** @name Public data members */
94 //@{
95 float_type x; /**< x-coordinate */
96 float_type y; /**< y-coordinate */
97 float_type z; /**< z-coordinate */
98 //@}
99
100 /// Construction
101 explicit Vector3(float_type fx = 0.0, float_type fy = 0.0, float_type fz = 0.0);
102 Vector3(const Vector3<float_type>& v) = default;
103 Vector3(Vector3<float_type>&& v) noexcept = default;
104 ~Vector3() = default;
105
106 /** @name Operator */
107 //@{
108 /// Returns a reference to a coordinate. \a usIndex must be in the range [0,2]
109 [[nodiscard]] float_type& operator[](unsigned short usIndex);
110 /// Returns a const reference to a coordinate. \a usIndex must be in the range [0,2]
111 [[nodiscard]] const float_type& operator[](unsigned short usIndex) const;
112 /// Vector addition
113 [[nodiscard]] Vector3 operator+(const Vector3<float_type>& rcVct) const;
114 [[nodiscard]] Vector3 operator&(const Vector3<float_type>& rcVct) const;
115 /// Vector subtraction
116 [[nodiscard]] Vector3 operator-(const Vector3<float_type>& rcVct) const;
117 /// Negative vector
118 [[nodiscard]] Vector3 operator-() const;
119 /// Vector summation
120 Vector3& operator+=(const Vector3<float_type>& rcVct);
121 /// Vector subtraction
122 Vector3& operator-=(const Vector3<float_type>& rcVct);
123 /// Vector scaling
124 [[nodiscard]] Vector3 operator*(float_type fScale) const;
125 [[nodiscard]] Vector3 operator/(float_type fDiv) const;
126 Vector3& operator*=(float_type fScale);
127 Vector3& operator/=(float_type fDiv);
128 /// Assignment
129 Vector3& operator=(const Vector3<float_type>& v) = default;
130 Vector3& operator=(Vector3<float_type>&& v) noexcept = default;
131 /// Scalar product
132 [[nodiscard]] float_type operator*(const Vector3<float_type>& rcVct) const;
133 /// Scalar product
134 [[nodiscard]] float_type Dot(const Vector3<float_type>& rcVct) const;
135 /// Cross product
136 [[nodiscard]] Vector3 operator%(const Vector3<float_type>& rcVct) const;
137 /// Cross product
138 [[nodiscard]] Vector3 Cross(const Vector3<float_type>& rcVct) const;
139
140 /// Comparing for inequality
141 [[nodiscard]] bool operator!=(const Vector3<float_type>& rcVct) const;
142 /// Comparing for equality
143 [[nodiscard]] bool operator==(const Vector3<float_type>& rcVct) const;
144 //@}
145
146 /// Check if Vector is on a line segment
147 [[nodiscard]] bool IsOnLineSegment(
148 const Vector3<float_type>& startVct,
149 const Vector3<float_type>& endVct
150 ) const;
151
152 /** @name Modification */
153 //@{
154 void ScaleX(float_type f);
155 void ScaleY(float_type f);
156 void ScaleZ(float_type f);
157 void Scale(float_type fX, float_type fY, float_type fZ);
158 void MoveX(float_type f);
159 void MoveY(float_type f);
160 void MoveZ(float_type f);
161 void Move(float_type fX, float_type fY, float_type fZ);
162 void RotateX(float_type f);
163 void RotateY(float_type f);
164 void RotateZ(float_type f);
165 //@}
166
167 void Set(float_type fX, float_type fY, float_type fZ);
168
169 /** @name Mathematics */
170 //@{
171 /// Length of the vector.
172 [[nodiscard]] float_type Length() const;
173 /// Squared length of the vector.
174 [[nodiscard]] float_type Sqr() const;
175 /// Set length to 1.
176 Vector3& Normalize();
177 Vector3 Normalized() const;
178 /// Checks whether this is the null vector
179 [[nodiscard]] bool IsNull() const;
180 /// Get angle between both vectors. The returned value lies in the interval [0,pi].
181 [[nodiscard]] float_type GetAngle(const Vector3& rcVect) const;
182 /// Get oriented angle between both vectors using a normal. The returned value lies in the
183 /// interval [0,2*pi].
184 [[nodiscard]] float_type GetAngleOriented(const Vector3& rcVect, const Vector3& norm) const;
185 /** Transforms this point to the coordinate system defined by origin \a rclBase,
186 * vector \a vector rclDirX and vector \a vector rclDirY.
187 * \note \a rclDirX must be perpendicular to \a rclDirY, i.e. \a rclDirX * \a rclDirY = 0..
188 */
189 void TransformToCoordinateSystem(
190 const Vector3& rclBase,
191 const Vector3& rclDirX,
192 const Vector3& rclDirY
193 );
194 /**
195 * @brief IsEqual
196 * @param rclPnt
197 * @param tol
198 * @return true or false
199 * If the distance to point \a rclPnt is within the tolerance \a tol both points are considered
200 * equal.
201 */
202 [[nodiscard]] bool IsEqual(const Vector3& rclPnt, float_type tol) const;
203 /// Returns true if two vectors are parallel within the tol
204 /// If one of the vectors is the null vector then false is returned.
205 [[nodiscard]] bool IsParallel(const Vector3& rclDir, float_type tol) const;
206 /// Returns true if two vectors are normal within the tol
207 /// If one of the vectors is the null vector then false is returned.
208 [[nodiscard]] bool IsNormal(const Vector3& rclDir, float_type tol) const;
209 /// Projects this point onto the plane given by the base \a rclBase and the normal \a rclNorm.
210 Vector3& ProjectToPlane(const Vector3& rclBase, const Vector3& rclNorm);
211 /**
212 * Projects this point onto the plane given by the base \a rclBase and the normal \a rclNorm
213 * and stores the result in rclProj.
214 */
215 void ProjectToPlane(const Vector3& rclBase, const Vector3& rclNorm, Vector3& rclProj) const;
216 /// Projects this point onto the line given by the base \a rclPoint and the direction \a
217 /// rclLine.
218 /**
219 * Projects a point \a rclPoint onto the line defined by the origin and the direction \a
220 * rclLine. The result is a vector from \a rclPoint to the point on the line. The length of this
221 * vector is the distance from \a rclPoint to the line. Note: The resulting vector does not
222 * depend on the current vector.
223 */
224 Vector3& ProjectToLine(const Vector3& rclPoint, const Vector3& rclLine);
225 /**
226 * Get the perpendicular of this point to the line defined by rclBase and rclDir.
227 * Note: Do not mix up this method with ProjectToLine.
228 */
229 [[nodiscard]] Vector3 Perpendicular(const Vector3& rclBase, const Vector3& rclDir) const;
230 /** Computes the distance to the given plane. Depending on the side this point is located
231 * the distance can also be negative. The distance is positive if the point is at the same
232 * side the plane normal points to, negative otherwise.
233 */
234 [[nodiscard]] float_type DistanceToPlane(const Vector3& rclBase, const Vector3& rclNorm) const;
235 /// Computes the distance from this point to the line given by \a rclBase and \a rclDirect.
236 [[nodiscard]] float_type DistanceToLine(const Vector3& rclBase, const Vector3& rclDirect) const;
237 /** Computes the vector from this point to the point on the line segment with the shortest
238 * distance. The line segment is defined by \a rclP1 and \a rclP2.
239 * Note: If the projection of this point is outside the segment then the shortest distance
240 * to \a rclP1 or \a rclP2 is computed.
241 */
242 [[nodiscard]] Vector3 DistanceToLineSegment(const Vector3& rclP1, const Vector3& rclP2) const;
243 //@}
244 };
245
246 template<class float_type>
247 Vector3<float_type> const Vector3<float_type>::UnitX(1.0, 0.0, 0.0);
248 template<class float_type>
249 Vector3<float_type> const Vector3<float_type>::UnitY(0.0, 1.0, 0.0);
250 template<class float_type>
251 Vector3<float_type> const Vector3<float_type>::UnitZ(0.0, 0.0, 1.0);
252
253 /// Returns the distance between two points
254 template<class float_type>
255 [[nodiscard]] inline float_type Distance(const Vector3<float_type>& v1, const Vector3<float_type>& v2)
256 {
257 float_type x = v1.x - v2.x;
258 float_type y = v1.y - v2.y;
259 float_type z = v1.z - v2.z;
260 return static_cast<float_type>(std::sqrt((x * x) + (y * y) + (z * z)));
261 }
262
263 /// Returns the squared distance between two points
264 template<class float_type>
265 [[nodiscard]] inline float_type DistanceP2(const Vector3<float_type>& v1, const Vector3<float_type>& v2)
266 {
267 float_type x = v1.x - v2.x;
268 float_type y = v1.y - v2.y;
269 float_type z = v1.z - v2.z;
270 return x * x + y * y + z * z;
271 }
272
273 /// Multiplication of scalar with vector.
274 template<class float_type>
275 [[nodiscard]] inline Vector3<float_type> operator*(float_type fFac, const Vector3<float_type>& rcVct)
276 {
277 return Vector3<float_type>(rcVct.x * fFac, rcVct.y * fFac, rcVct.z * fFac);
278 }
279
280 template<class Pr1, class Pr2>
281 [[nodiscard]] inline Vector3<Pr1> toVector(const Vector3<Pr2>& v)
282 {
283 return Vector3<Pr1>(static_cast<Pr1>(v.x), static_cast<Pr1>(v.y), static_cast<Pr1>(v.z));
284 }
285
286 using Vector3f = Vector3<float>;
287 using Vector3d = Vector3<double>;
288
289 } // namespace Base
290
291 #endif // BASE_VECTOR3D_H