Debian Codegraph @

3rd_party/router/kimath/include/math/vector3.h

raw
1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef VECTOR3_H_
21 #define VECTOR3_H_
22
23 /**
24 * Traits class for VECTOR2.
25 */
26 template <class T>
27 struct VECTOR3_TRAITS
28 {
29 ///< extended range/precision types used by operations involving multiple
30 ///< multiplications to prevent overflow.
31 typedef T extended_type;
32 };
33
34 template <>
35 struct VECTOR3_TRAITS<int>
36 {
37 typedef int64_t extended_type;
38 };
39
40
41 /**
42 * Define a general 3D-vector.
43 *
44 * This class uses templates to be universal. Several operators are provided to help
45 * easy implementing of linear algebra equations.
46 *
47 */
48 template <class T = int>
49 class VECTOR3
50 {
51 public:
52 typedef typename VECTOR3_TRAITS<T>::extended_type extended_type;
53 typedef T coord_type;
54
55 static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
56 static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
57
58 T x, y, z;
59
60 /// Construct a 3D-vector with x, y = 0
61 VECTOR3();
62
63 /// Construct a vector with given components x, y
64 VECTOR3( T x, T y, T z );
65
66 /// Initializes a vector from another specialization. Beware of rounding
67 /// issues.
68 template <typename CastingType>
69 VECTOR3( const VECTOR3<CastingType>& aVec )
70 {
71 x = (T) aVec.x;
72 y = (T) aVec.y;
73 z = (T) aVec.z;
74 }
75
76 /// Copy a vector
77 VECTOR3( const VECTOR3<T>& aVec )
78 {
79 x = aVec.x;
80 y = aVec.y;
81 z = aVec.z;
82 }
83
84 /**
85 * Compute cross product of self with \a aVector
86 */
87 VECTOR3<T> Cross( const VECTOR3<T>& aVector ) const;
88
89 /**
90 * Compute the dot product of self with \a aVector
91 */
92 VECTOR3<T>::extended_type Dot( const VECTOR3<T>& aVector ) const;
93
94 /**
95 * Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
96 *
97 * It is used to calculate the length of the vector.
98 *
99 * @return Scalar, the euclidean norm
100 */
101 T EuclideanNorm() const;
102
103 /**
104 * Compute the normalized vector.
105 */
106 VECTOR3<T> Normalize();
107
108 ///< Equality operator
109 bool operator==( const VECTOR3<T>& aVector ) const;
110
111 ///< Not equality operator
112 bool operator!=( const VECTOR3<T>& aVector ) const;
113 };
114
115
116 template <class T>
117 VECTOR3<T>::VECTOR3()
118 {
119 x = y = z = 0.0;
120 }
121
122
123 template <class T>
124 VECTOR3<T>::VECTOR3( T aX, T aY, T aZ )
125 {
126 x = aX;
127 y = aY;
128 z = aZ;
129 }
130
131
132 template <class T>
133 VECTOR3<T> VECTOR3<T>::Cross( const VECTOR3<T>& aVector ) const
134 {
135 return VECTOR3<T>( ( y * aVector.z ) - ( z * aVector.y ),
136 ( z * aVector.x ) - ( x * aVector.z ),
137 ( x * aVector.y ) - ( y * aVector.x )
138 );
139 }
140
141
142 template <class T>
143 typename VECTOR3<T>::extended_type VECTOR3<T>::Dot( const VECTOR3<T>& aVector ) const
144 {
145 return extended_type{x} * extended_type{aVector.x}
146 + extended_type{y} * extended_type{aVector.y}
147 + extended_type{z} * extended_type{aVector.z};
148 }
149
150
151 template <class T>
152 T VECTOR3<T>::EuclideanNorm() const
153 {
154 return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
155 }
156
157
158 template <class T>
159 VECTOR3<T> VECTOR3<T>::Normalize()
160 {
161 T norm = EuclideanNorm();
162 x /= norm;
163 y /= norm;
164 z /= norm;
165
166 return *this;
167 }
168
169
170 template <class T>
171 bool VECTOR3<T>::operator==( VECTOR3<T> const& aVector ) const
172 {
173 return ( aVector.x == x ) && ( aVector.y == y ) && ( aVector.z == z );
174 }
175
176
177 template <class T>
178 bool VECTOR3<T>::operator!=( VECTOR3<T> const& aVector ) const
179 {
180 return ( aVector.x != x ) || ( aVector.y != y ) || ( aVector.z != z );
181 }
182
183
184 /* Default specializations */
185 typedef VECTOR3<double> VECTOR3D;
186 typedef VECTOR3<int> VECTOR3I;
187 typedef VECTOR3<unsigned int> VECTOR3U;
188
189 #endif // VECTOR3_H_