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
23#include <limits>
24#include <wx/debug.h>
25
29template <class T>
31{
34 typedef T extended_type;
35};
36
37template <>
38struct VECTOR3_TRAITS<int>
39{
40 typedef int64_t extended_type;
41};
42
43
51template <class T = int>
53{
54public:
56 typedef T coord_type;
57
58 static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
59 static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
60
61 T x{};
62 T y{};
63 T z{};
64
66 VECTOR3() = default;
67
69 VECTOR3( T x, T y, T z );
70
73 template <typename CastingType>
75
79 VECTOR3<T> Cross( const VECTOR3<T>& aVector ) const;
80
84 VECTOR3<T>::extended_type Dot( const VECTOR3<T>& aVector ) const;
85
93 T EuclideanNorm() const;
94
99
101 bool operator==( const VECTOR3<T>& aVector ) const;
102
104 bool operator!=( const VECTOR3<T>& aVector ) const;
105
108};
109
110
111template <class T>
112VECTOR3<T>::VECTOR3( T aX, T aY, T aZ ) :
113 x( aX ), y( aY ), z( aZ )
114{
115}
116
117
118template <class T>
119template <typename CastingType>
121 x( aVec.x ), y( aVec.y ), z( aVec.z )
122{
123}
124
125
126template <class T>
128{
129 return VECTOR3<T>( ( y * aVector.z ) - ( z * aVector.y ),
130 ( z * aVector.x ) - ( x * aVector.z ),
131 ( x * aVector.y ) - ( y * aVector.x )
132 );
133}
134
135
136template <class T>
138{
139 return extended_type{x} * extended_type{aVector.x}
140 + extended_type{y} * extended_type{aVector.y}
141 + extended_type{z} * extended_type{aVector.z};
142}
143
144
145template <class T>
147{
148 return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
149}
150
151
152template <class T>
154{
155 T norm = EuclideanNorm();
156
157 wxCHECK_MSG( norm > T( 0 ), *this, wxT( "Invalid element length 0" ) );
158
159 x /= norm;
160 y /= norm;
161 z /= norm;
162
163 return *this;
164}
165
166
167template <class T>
168bool VECTOR3<T>::operator==( VECTOR3<T> const& aVector ) const
169{
170 return ( aVector.x == x ) && ( aVector.y == y ) && ( aVector.z == z );
171}
172
173
174template <class T>
175bool VECTOR3<T>::operator!=( VECTOR3<T> const& aVector ) const
176{
177 return ( aVector.x != x ) || ( aVector.y != y ) || ( aVector.z != z );
178}
179
180
181template <class T>
183{
184 x = x * aScalar;
185 y = y * aScalar;
186 z = z * aScalar;
187
188 return *this;
189}
190
191
192template <class T>
194{
195 x = x / aScalar;
196 y = y / aScalar;
197 z = z / aScalar;
198
199 return *this;
200}
201
202
203/* Default specializations */
207
208#endif // VECTOR3_H_
Define a general 3D-vector.
Definition: vector3.h:53
T y
Definition: vector3.h:62
VECTOR3< T > & operator*=(T val)
Definition: vector3.h:182
T z
Definition: vector3.h:63
static constexpr extended_type ECOORD_MAX
Definition: vector3.h:58
VECTOR3< T >::extended_type Dot(const VECTOR3< T > &aVector) const
Compute the dot product of self with aVector.
Definition: vector3.h:137
VECTOR3< T > Normalize()
Compute the normalized vector.
Definition: vector3.h:153
VECTOR3(T x, T y, T z)
Construct a vector with given components x, y, z.
Definition: vector3.h:112
VECTOR3_TRAITS< T >::extended_type extended_type
Definition: vector3.h:55
bool operator==(const VECTOR3< T > &aVector) const
Not equality operator.
Definition: vector3.h:168
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector3.h:127
static constexpr extended_type ECOORD_MIN
Definition: vector3.h:59
T coord_type
Definition: vector3.h:56
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector3.h:146
bool operator!=(const VECTOR3< T > &aVector) const
Definition: vector3.h:175
VECTOR3()=default
Construct a 3D-vector with x, y, z = 0.
VECTOR3< T > & operator/=(T val)
Definition: vector3.h:193
VECTOR3(const VECTOR3< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector3.h:120
T x
Definition: vector3.h:61
int64_t extended_type
Definition: vector3.h:40
Traits class for VECTOR2.
Definition: vector3.h:31
T extended_type
< extended range/precision types used by operations involving multiple multiplications to prevent ove...
Definition: vector3.h:34
double EuclideanNorm(const VECTOR2I &vector)
Definition: trigo.h:129
VECTOR3< int > VECTOR3I
Definition: vector3.h:205
VECTOR3< unsigned int > VECTOR3U
Definition: vector3.h:206
VECTOR3< double > VECTOR3D
Definition: vector3.h:204