Horizon
Loading...
Searching...
No Matches
vector3.h
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
26template <class T>
28{
31 typedef T extended_type;
32};
33
34template <>
35struct VECTOR3_TRAITS<int>
36{
37 typedef int64_t extended_type;
38};
39
40
48template <class T = int>
50{
51public:
52 typedef typename VECTOR3_TRAITS<T>::extended_type extended_type;
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
116template <class T>
118{
119 x = y = z = 0.0;
120}
121
122
123template <class T>
124VECTOR3<T>::VECTOR3( T aX, T aY, T aZ )
125{
126 x = aX;
127 y = aY;
128 z = aZ;
129}
130
131
132template <class T>
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
142template <class T>
143typename 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
151template <class T>
153{
154 return sqrt( (extended_type) x * x + (extended_type) y * y + (extended_type) z * z );
155}
156
157
158template <class T>
160{
161 T norm = EuclideanNorm();
162 x /= norm;
163 y /= norm;
164 z /= norm;
165
166 return *this;
167}
168
169
170template <class T>
171bool VECTOR3<T>::operator==( VECTOR3<T> const& aVector ) const
172{
173 return ( aVector.x == x ) && ( aVector.y == y ) && ( aVector.z == z );
174}
175
176
177template <class T>
178bool 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 */
186typedef VECTOR3<int> VECTOR3I;
188
189#endif // VECTOR3_H_
Define a general 3D-vector.
Definition vector3.h:50
VECTOR3(const VECTOR3< T > &aVec)
Copy a vector.
Definition vector3.h:77
VECTOR3< T >::extended_type Dot(const VECTOR3< T > &aVector) const
Compute the dot product of self with aVector.
Definition vector3.h:143
VECTOR3()
Construct a 3D-vector with x, y = 0.
Definition vector3.h:117
VECTOR3< T > Normalize()
Compute the normalized vector.
Definition vector3.h:159
bool operator==(const VECTOR3< T > &aVector) const
Not equality operator.
Definition vector3.h:171
VECTOR3< T > Cross(const VECTOR3< T > &aVector) const
Compute cross product of self with aVector.
Definition vector3.h:133
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector3.h:152
VECTOR3(const VECTOR3< CastingType > &aVec)
Initializes a vector from another specialization.
Definition vector3.h:69
Traits class for VECTOR2.
Definition vector3.h:28
T extended_type
< extended range/precision types used by operations involving multiple multiplications to prevent ove...
Definition vector3.h:31
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition trigo.h:146