KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <iostream>
24#include <stdint.h>
25#include <limits>
26#include <wx/debug.h>
27
31template <class T>
33{
36 typedef T extended_type;
37};
38
39template <>
40struct VECTOR3_TRAITS<int>
41{
42 typedef int64_t extended_type;
43};
44
45
53template <class T = int>
55{
56public:
58 typedef T coord_type;
59
60 static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
61 static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
62
63 T x{};
64 T y{};
65 T z{};
66
68 VECTOR3() = default;
69
71 VECTOR3( T x, T y, T z );
72
75 template <typename CastingType>
77
81 VECTOR3<T> Cross( const VECTOR3<T>& aVector ) const;
82
86 VECTOR3<T>::extended_type Dot( const VECTOR3<T>& aVector ) const;
87
95 T EuclideanNorm() const;
96
101
106
108 bool operator==( const VECTOR3<T>& aVector ) const;
109
111 bool operator!=( const VECTOR3<T>& aVector ) const;
112
115};
116
117
118template <class T>
119VECTOR3<T>::VECTOR3( T aX, T aY, T aZ ) :
120 x( aX ), y( aY ), z( aZ )
121{
122}
123
124
125template <class T>
126template <typename CastingType>
128 x( aVec.x ), y( aVec.y ), z( aVec.z )
129{
130}
131
132
133template <class T>
135{
136 return VECTOR3<T>( ( y * aVector.z ) - ( z * aVector.y ),
137 ( z * aVector.x ) - ( x * aVector.z ),
138 ( x * aVector.y ) - ( y * aVector.x )
139 );
140}
141
142
143template <class T>
145{
146 return extended_type{x} * extended_type{aVector.x}
147 + extended_type{y} * extended_type{aVector.y}
148 + extended_type{z} * extended_type{aVector.z};
149}
150
151
152template <class T>
154{
155 return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
156}
157
158
159template <class T>
161{
162 T norm = EuclideanNorm();
163
164 wxCHECK_MSG( norm > T( 0 ), *this, wxT( "Invalid element length 0" ) );
165
166 x /= norm;
167 y /= norm;
168 z /= norm;
169
170 return *this;
171}
172
173
174template <class T>
176{
177 x = val;
178 y = val;
179 z = val;
180
181 return *this;
182}
183
184
185template <class T>
186bool VECTOR3<T>::operator==( VECTOR3<T> const& aVector ) const
187{
188 return ( aVector.x == x ) && ( aVector.y == y ) && ( aVector.z == z );
189}
190
191
192template <class T>
193bool VECTOR3<T>::operator!=( VECTOR3<T> const& aVector ) const
194{
195 return ( aVector.x != x ) || ( aVector.y != y ) || ( aVector.z != z );
196}
197
198
199template <class T>
201{
202 x = x * aScalar;
203 y = y * aScalar;
204 z = z * aScalar;
205
206 return *this;
207}
208
209
210template <class T>
212{
213 x = x / aScalar;
214 y = y / aScalar;
215 z = z / aScalar;
216
217 return *this;
218}
219
220
221template <class T>
222std::ostream& operator<<( std::ostream& aStream, const VECTOR3<T>& aVector )
223{
224 aStream << "[ " << aVector.x << " | " << aVector.y << " | " << aVector.z << " ]";
225 return aStream;
226}
227
228
229/* Default specializations */
233
234#endif // VECTOR3_H_
Define a general 3D-vector.
Definition: vector3.h:55
T y
Definition: vector3.h:64
VECTOR3< T > & operator*=(T val)
Definition: vector3.h:200
T z
Definition: vector3.h:65
static constexpr extended_type ECOORD_MAX
Definition: vector3.h:60
VECTOR3< T >::extended_type Dot(const VECTOR3< T > &aVector) const
Compute the dot product of self with aVector.
Definition: vector3.h:144
VECTOR3< T > SetAll(T val)
Set all elements to val.
Definition: vector3.h:175
VECTOR3< T > Normalize()
Compute the normalized vector.
Definition: vector3.h:160
VECTOR3(T x, T y, T z)
Construct a vector with given components x, y, z.
Definition: vector3.h:119
VECTOR3_TRAITS< T >::extended_type extended_type
Definition: vector3.h:57
bool operator==(const VECTOR3< T > &aVector) const
Equality operator.
Definition: vector3.h:186
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector3.h:134
static constexpr extended_type ECOORD_MIN
Definition: vector3.h:61
T coord_type
Definition: vector3.h:58
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector3.h:153
bool operator!=(const VECTOR3< T > &aVector) const
Not equality operator.
Definition: vector3.h:193
VECTOR3()=default
Construct a 3D-vector with x, y, z = 0.
VECTOR3< T > & operator/=(T val)
Definition: vector3.h:211
VECTOR3(const VECTOR3< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector3.h:127
T x
Definition: vector3.h:63
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1311
int64_t extended_type
Definition: vector3.h:42
Traits class for VECTOR2.
Definition: vector3.h:33
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
Definition: vector3.h:36
VECTOR3< int > VECTOR3I
Definition: vector3.h:231
VECTOR3< unsigned int > VECTOR3U
Definition: vector3.h:232
VECTOR3< double > VECTOR3D
Definition: vector3.h:230