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