Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
PositionH.h
1// trax track library
2// AD 2013
3//
4// "the resolution of all the fruitless searches"
5//
6// Peter Gabriel
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29
30namespace spat{
31
32 template<typename> struct Position;
33 template<typename> struct Vector;
34
39 template<typename Valtype>
40 struct PositionH
41 {
42 typedef Valtype value_type;
43
44 Valtype x;
45 Valtype y;
46 Valtype z;
47 Valtype w;
48
49
58 PositionH() = default;
59 PositionH( Valtype x, Valtype y, Valtype z, Valtype w ) noexcept;
60 template<typename Valtype2>
61 explicit constexpr PositionH( const Position<Valtype2>& p ) noexcept;
62 template<typename Valtype2>
63 explicit PositionH( const Vector<Valtype2>& v ) noexcept;
64 template<typename Valtype2>
65 explicit PositionH( const Valtype2* pVal ) noexcept;
67
68
72 const Valtype& operator[]( size_t index ) const;
73
74 Valtype& operator[]( size_t index );
76
77
79 Valtype* ptr() noexcept;
80
81
83 const Valtype* ptr() const noexcept;
84
86 void Init() noexcept;
87
88
90 bool Homogenize() noexcept;
91
92
98 bool Equals( const PositionH& p, Valtype epsilon = Valtype{0} ) const noexcept;
99 };
100
103 template<typename Valtype> constexpr
104 bool operator==( const PositionH<Valtype>& p1, const PositionH<Valtype>& p2 ) noexcept;
105 template<typename Valtype> constexpr
106 bool operator!=( const PositionH<Valtype>& p1, const PositionH<Valtype>& p2 ) noexcept;
108
109
110 template<typename Valtype>
111 constexpr size_t size( const PositionH<Valtype> ) noexcept {
112 return 4;
113 }
114// inlines:
115template<typename Valtype>
116inline PositionH<Valtype>::PositionH( Valtype x, Valtype y, Valtype z, Valtype w ) noexcept
117 : x{x}, y{y}, z{z}, w{w}
118{}
119
120template<typename Valtype>
121template<typename Valtype2>
122inline constexpr PositionH<Valtype>::PositionH( const Position<Valtype2>& p ) noexcept
123 : x{ p.x/Valtype2{1} },
124 y{ p.y/Valtype2{1} },
125 z{ p.z/Valtype2{1} },
126 w{ Valtype{1} }
127{}
128
129template<typename Valtype>
130template<typename Valtype2>
132 : x{ v.dx/Valtype2{1} },
133 y{ v.dy/Valtype2{1} },
134 z{ v.dz/Valtype2{1} },
135 w{ Valtype{0} }
136{}
137
138template<typename Valtype>
139template<typename Valtype2>
140inline PositionH<Valtype>::PositionH( const Valtype2* pVal ) noexcept
141 : x{ Valtype{ pVal[0] } },
142 y{ Valtype{ pVal[1] } },
143 z{ Valtype{ pVal[2] } },
144 w{ Valtype{ pVal[3] } }
145{}
146
147template<typename Valtype>
148inline const Valtype& PositionH<Valtype>::operator[]( size_t index ) const{
149 switch( index ){
150 case 0:
151 return x;
152 case 1:
153 return y;
154 case 2:
155 return z;
156 case 3:
157 return w;
158 default:
159 assert(0);
160 throw std::out_of_range( "invalid PositionH subscript" );
161 }
162}
163
164template<typename Valtype>
165inline Valtype& PositionH<Valtype>::operator[]( size_t index ){
166 switch( index ){
167 case 0:
168 return x;
169 case 1:
170 return y;
171 case 2:
172 return z;
173 case 3:
174 return w;
175 default:
176 assert(0);
177 throw std::out_of_range( "invalid PositionH subscript" );
178 }
179}
180
181template<typename Valtype>
182inline Valtype* PositionH<Valtype>::ptr() noexcept{
183 return &x;
184}
185
186template<typename Valtype>
187inline const Valtype* PositionH<Valtype>::ptr() const noexcept{
188 return &x;
189}
190
191template<typename Valtype>
192inline void PositionH<Valtype>::Init() noexcept{
193 x = Valtype{0}; y = Valtype{0}; z = Valtype{0}; w = Valtype{1};
194}
195
196template<typename Valtype>
197inline bool PositionH<Valtype>::Homogenize() noexcept{
198 if( w != Valtype{0} ){
199 x /= w;
200 y /= w;
201 z /= w;
202 w = Valtype{1};
203 return true;
204 }
205 return false;
206}
207
208template<typename Valtype>
209inline bool PositionH<Valtype>::Equals( const PositionH<Valtype>& p, Valtype epsilon_ ) const noexcept{
210 return std::abs(x/w - p.x/p.w) <= epsilon_ &&
211 std::abs(y/w - p.y/p.w) <= epsilon_ &&
212 std::abs(z/w - p.z/p.w) <= epsilon_;
213}
214
215template<typename Valtype>
216inline constexpr bool operator==( const PositionH<Valtype>& p1, const PositionH<Valtype>& p2 ) noexcept{
217 return p1.x/p1.w == p2.x/p2.w &&
218 p1.y/p1.w == p2.y/p2.w &&
219 p1.z/p1.w == p2.z/p2.w;
220}
221
222template<typename Valtype>
223inline constexpr bool operator!=( const PositionH<Valtype>& p1, const PositionH<Valtype>& p2 ) noexcept{
224 return !(p1 == p2);
225}
226
227} // namespace spat
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Implements a 4D - position in homogenous coordinates.
Definition PositionH.h:41
Valtype y
y coordinate
Definition PositionH.h:45
void Init() noexcept
Sets the values to (0,0,0,1).
Definition PositionH.h:192
bool Homogenize() noexcept
Transforms the position so that w == 1.
Definition PositionH.h:197
Valtype z
z coordinate
Definition PositionH.h:46
bool Equals(const PositionH &p, Valtype epsilon=Valtype{0}) const noexcept
Comparison within some range. The area within two positions regarded as equal is a sphere with radius...
Definition PositionH.h:209
PositionH()=default
Does not initialize the members.
Valtype w
w coordinate, homogenous coordinate
Definition PositionH.h:47
Valtype * ptr() noexcept
Pointer accessor for direct copying.
Definition PositionH.h:182
Valtype x
x coordinate
Definition PositionH.h:44
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48