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 <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
103 bool operator==( const VECTOR3<T>& aVector ) const;
104
106 bool operator!=( const VECTOR3<T>& aVector ) const;
107
110};
111
112
113template <class T>
114VECTOR3<T>::VECTOR3( T aX, T aY, T aZ ) :
115 x( aX ), y( aY ), z( aZ )
116{
117}
118
119
120template <class T>
121template <typename CastingType>
123 x( aVec.x ), y( aVec.y ), z( aVec.z )
124{
125}
126
127
128template <class T>
130{
131 return VECTOR3<T>( ( y * aVector.z ) - ( z * aVector.y ),
132 ( z * aVector.x ) - ( x * aVector.z ),
133 ( x * aVector.y ) - ( y * aVector.x )
134 );
135}
136
137
138template <class T>
140{
141 return extended_type{x} * extended_type{aVector.x}
142 + extended_type{y} * extended_type{aVector.y}
143 + extended_type{z} * extended_type{aVector.z};
144}
145
146
147template <class T>
149{
150 return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
151}
152
153
154template <class T>
156{
157 T norm = EuclideanNorm();
158
159 wxCHECK_MSG( norm > T( 0 ), *this, wxT( "Invalid element length 0" ) );
160
161 x /= norm;
162 y /= norm;
163 z /= norm;
164
165 return *this;
166}
167
168
169template <class T>
170bool VECTOR3<T>::operator==( VECTOR3<T> const& aVector ) const
171{
172 return ( aVector.x == x ) && ( aVector.y == y ) && ( aVector.z == z );
173}
174
175
176template <class T>
177bool VECTOR3<T>::operator!=( VECTOR3<T> const& aVector ) const
178{
179 return ( aVector.x != x ) || ( aVector.y != y ) || ( aVector.z != z );
180}
181
182
183template <class T>
185{
186 x = x * aScalar;
187 y = y * aScalar;
188 z = z * aScalar;
189
190 return *this;
191}
192
193
194template <class T>
196{
197 x = x / aScalar;
198 y = y / aScalar;
199 z = z / aScalar;
200
201 return *this;
202}
203
204
205template <class T>
206std::ostream& operator<<( std::ostream& aStream, const VECTOR3<T>& aVector )
207{
208 aStream << "[ " << aVector.x << " | " << aVector.y << " | " << aVector.z << " ]";
209 return aStream;
210}
211
212
213/* Default specializations */
217
218#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:184
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:139
VECTOR3< T > Normalize()
Compute the normalized vector.
Definition: vector3.h:155
VECTOR3(T x, T y, T z)
Construct a vector with given components x, y, z.
Definition: vector3.h:114
VECTOR3_TRAITS< T >::extended_type extended_type
Definition: vector3.h:57
bool operator==(const VECTOR3< T > &aVector) const
Equality operator.
Definition: vector3.h:170
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
Definition: vector3.h:129
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:148
bool operator!=(const VECTOR3< T > &aVector) const
Not equality operator.
Definition: vector3.h:177
VECTOR3()=default
Construct a 3D-vector with x, y, z = 0.
VECTOR3< T > & operator/=(T val)
Definition: vector3.h:195
VECTOR3(const VECTOR3< CastingType > &aVec)
Initializes a vector from another specialization.
Definition: vector3.h:122
T x
Definition: vector3.h:63
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
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:215
VECTOR3< unsigned int > VECTOR3U
Definition: vector3.h:216
VECTOR3< double > VECTOR3D
Definition: vector3.h:214