KiCad PCB EDA Suite
vector3.h
Go to the documentation of this file.
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 
26 template <class T>
28 {
31  typedef T extended_type;
32 };
33 
34 template <>
35 struct VECTOR3_TRAITS<int>
36 {
37  typedef int64_t extended_type;
38 };
39 
40 
48 template <class T = int>
49 class VECTOR3
50 {
51 public:
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 
61  VECTOR3();
62 
64  VECTOR3( T x, T y, T z );
65 
68  template <typename CastingType>
70  {
71  x = (T) aVec.x;
72  y = (T) aVec.y;
73  z = (T) aVec.z;
74  }
75 
77  VECTOR3( const VECTOR3<T>& aVec )
78  {
79  x = aVec.x;
80  y = aVec.y;
81  z = aVec.z;
82  }
83 
87  VECTOR3<T> Cross( const VECTOR3<T>& aVector ) const;
88 
92  VECTOR3<T>::extended_type Dot( const VECTOR3<T>& aVector ) const;
93 
101  T EuclideanNorm() const;
102 
107 
109  bool operator==( const VECTOR3<T>& aVector ) const;
110 
112  bool operator!=( const VECTOR3<T>& aVector ) const;
113 };
114 
115 
116 template <class T>
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>
153 {
154  return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
155 }
156 
157 
158 template <class T>
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 */
188 
189 #endif // VECTOR3_H_
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:146
VECTOR3(const VECTOR3< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector3.h:69
Traits class for VECTOR2.
Definition: vector3.h:27
T z
Definition: vector3.h:58
VECTOR3< T > Normalize()
Compute the normalized vector.
Definition: vector3.h:159
VECTOR3(const VECTOR3< T > &aVec)
Copy a vector.
Definition: vector3.h:77
bool operator!=(const VECTOR3< T > &aVector) const
Definition: vector3.h:178
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector3.h:152
static constexpr extended_type ECOORD_MAX
Definition: vector3.h:55
int64_t extended_type
Definition: vector3.h:37
VECTOR3< T >::extended_type Dot(const VECTOR3< T > &aVector) const
Compute the dot product of self with aVector.
Definition: vector3.h:143
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector3.h:133
VECTOR3_TRAITS< T >::extended_type extended_type
Definition: vector3.h:52
static constexpr extended_type ECOORD_MIN
Definition: vector3.h:56
VECTOR3< unsigned int > VECTOR3U
Definition: vector3.h:187
T coord_type
Definition: vector3.h:53
T extended_type
< extended range/precision types used by operations involving multiple multiplications to prevent ove...
Definition: vector3.h:31
Define a general 3D-vector.
Definition: vector3.h:49
T x
Definition: vector3.h:58
VECTOR3< int > VECTOR3I
Definition: vector3.h:186
T y
Definition: vector3.h:58
VECTOR3()
Construct a 3D-vector with x, y = 0.
Definition: vector3.h:117
bool operator==(const VECTOR3< T > &aVector) const
Not equality operator.
Definition: vector3.h:171
VECTOR3< double > VECTOR3D
Definition: vector3.h:185