Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
VectorBundle.h
1// trax track library
2// AD 2013
3//
4// "Die Stadt muss brennen"
5//
6// Casper
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#include "Position.h"
30#include "Vector.h"
31
32namespace spat{
33
34 template<typename,typename> struct VectorBundle2;
35 template<typename,typename> struct Frame;
36
40 template<typename Valtype,typename ValtypeT = Valtype>
42 {
45
46
59 constexpr VectorBundle() noexcept = default;
60 explicit constexpr VectorBundle( const Position<Valtype>& pos ) noexcept;
61 constexpr VectorBundle( const Position<Valtype>& pos,
62 const Vector<ValtypeT>& tan ) noexcept;
63 constexpr VectorBundle( Valtype x, Valtype y, Valtype z,
64 ValtypeT dx, ValtypeT dy, ValtypeT dz ) noexcept;
65 explicit constexpr VectorBundle( const VectorBundle2<Valtype,ValtypeT>& bundle ) noexcept;
66 explicit constexpr VectorBundle( const Frame<Valtype,ValtypeT>& frame ) noexcept;
67 template<typename Valtype2>
68 explicit VectorBundle( const Valtype2* const pVal ) noexcept;
70
71
75 VectorBundle& operator= ( const Frame<Valtype,ValtypeT>& frame ) noexcept;
76
77 template<typename Valtype2>
78 VectorBundle& operator=( const Valtype2* pVal ) noexcept;
80
81
83 Valtype* ptr() noexcept;
84
85
87 const Valtype* ptr() const noexcept;
88
89
91 VectorBundle& Init() noexcept;
92
93
96 VectorBundle& Init( const VectorBundle& vb ) noexcept;
97
98
101 VectorBundle& TransportTo( Valtype d ) noexcept;
102
103
106 VectorBundle& TransportBy( const Vector<Valtype>& v ) noexcept;
107
108
110 VectorBundle& Rotate( const Vector<Valtype>& nr, Valtype angle ) noexcept;
111
112
114 VectorBundle& Rotate( const Vector<Valtype>& r ) noexcept;
115
116
128 constexpr bool Equals( const VectorBundle& bundle,
129 Valtype epsilon_length = std::numeric_limits<Valtype>::epsilon(),
130 ValtypeT epsilon_lengthT = std::numeric_limits<ValtypeT>::epsilon() ) const noexcept;
131 };
132
134 template<typename Valtype,typename ValtypeT = Valtype>
135 using VectorBundle1 = VectorBundle<Valtype,ValtypeT>;
136
138 template<typename Valtype,typename ValtypeT=Valtype,typename Valtype2=Valtype,typename ValtypeT2=Valtype>
139 VectorBundle<Valtype,ValtypeT>& spatial_cast( VectorBundle<Valtype2,ValtypeT2>& from ) noexcept;
140
142 template<typename Valtype,typename ValtypeT=Valtype,typename Valtype2=Valtype,typename ValtypeT2=Valtype>
143 const VectorBundle<Valtype,ValtypeT>& spatial_cast( const VectorBundle<Valtype2,ValtypeT2>& from ) noexcept;
144
145
148
150 template<typename Valtype,typename ValtypeT> constexpr
151 bool operator ==( const VectorBundle<Valtype,ValtypeT>& b1, const VectorBundle<Valtype,ValtypeT>& b2 ) noexcept;
152 template<typename Valtype,typename ValtypeT> constexpr
153 bool operator !=( const VectorBundle<Valtype,ValtypeT>& b1, const VectorBundle<Valtype,ValtypeT>& b2 ) noexcept;
155
156
159 template<typename Valtype,typename ValtypeT = Valtype>
160 VectorBundle1<Valtype,Valtype> Distance( const VectorBundle1<Valtype,ValtypeT>& b1, const VectorBundle1<Valtype,ValtypeT>& b2 ) noexcept;
161
162
163template<typename Valtype,typename ValtypeT> constexpr
164inline VectorBundle<Valtype,ValtypeT>::VectorBundle( const Position<Valtype>& pos ) noexcept
165 : P{pos},
167{}
168
169template<typename Valtype,typename ValtypeT> constexpr
171 const Position<Valtype>& pos,
172 const Vector<ValtypeT>& tan ) noexcept
173 : P{pos},
174 T{tan}
175{}
176
177template<typename Valtype,typename ValtypeT> constexpr
179 Valtype x, Valtype y, Valtype z,
180 ValtypeT dx, ValtypeT dy, ValtypeT dz ) noexcept
181 : P{x,y,z},
182 T{dx,dy,dz}
183{}
184
185template<typename Valtype,typename ValtypeT>
187 : P{bundle.P},
188 T{bundle.T}
189{}
190
191template<typename Valtype,typename ValtypeT> constexpr
193 : P{frame.P},
194 T{frame.T}
195{}
196
197template<typename Valtype,typename ValtypeT>
198template<typename Valtype2>
199inline VectorBundle<Valtype,ValtypeT>::VectorBundle( const Valtype2* const pVal ) noexcept
200 : P{pVal},
201 T{pVal+3}
202{
203}
204
205template<typename Valtype,typename ValtypeT>
206template<typename Valtype2>
208 P = pVal;
209 T = pVal + 3;
210 return *this;
211}
212
213template<typename Valtype,typename ValtypeT>
214inline VectorBundle<Valtype,ValtypeT>& VectorBundle<Valtype,ValtypeT>::operator=( const Frame<Valtype,ValtypeT>& frame ) noexcept{
215 P = frame.P;
216 T = frame.T;
217 return *this;
218}
219
220template<typename Valtype,typename ValtypeT>
221inline Valtype* VectorBundle<Valtype,ValtypeT>::ptr() noexcept{
222 static_assert( sizeof(Valtype) == sizeof(ValtypeT), "Can not access a VectorBundle via pointer if the two types have different memory sizes." );
223 return P.ptr();
224}
225
226template<typename Valtype,typename ValtypeT>
227inline const Valtype* VectorBundle<Valtype,ValtypeT>::ptr() const noexcept{
228 static_assert( sizeof(Valtype) == sizeof(ValtypeT), "Can not access a VectorBundle via pointer if the two types have different memory sizes.");
229 return P.ptr();
230}
231
232template<typename Valtype,typename ValtypeT>
234 P.Init();
235 T.Init();
236 return *this;
237}
238
239template<typename Valtype,typename ValtypeT>
241 P = bundle.P;
242 T = bundle.T;
243 return *this;
244}
245
246template<typename Valtype,typename ValtypeT>
248 P += d * T;
249 return *this;
250}
251
252template<typename Valtype,typename ValtypeT>
254 P.Transport( v );
255 return *this;
256}
257
258template<typename Valtype,typename ValtypeT>
260 T.Rotate( nr, angle );
261 return *this;
262}
263
264template<typename Valtype,typename ValtypeT>
266 T.Rotate( r );
267 return *this;
268}
269
270template<typename Valtype,typename ValtypeT> constexpr
271bool VectorBundle<Valtype,ValtypeT>::Equals( const VectorBundle& bundle, Valtype epsilon_length_, ValtypeT epsilon_lengthT ) const noexcept{
272 return P.Equals( bundle.P, epsilon_length_ ) &&
273 T.Equals( bundle.T, epsilon_lengthT );
274}
275
276template<typename Valtype,typename ValtypeT,typename Valtype2,typename ValtypeT2>
278 static_assert( sizeof(Valtype) == sizeof(Valtype2), "Can not spatial_cast with different memory layout" );
279 static_assert( sizeof(ValtypeT) == sizeof(ValtypeT2), "Can not spatial_cast with different memory layout" );
280 return reinterpret_cast<VectorBundle<Valtype,ValtypeT>&>(from);
281}
282
283template<typename Valtype,typename ValtypeT,typename Valtype2,typename ValtypeT2>
285 static_assert( sizeof(Valtype) == sizeof(Valtype2), "Can not spatial_cast with different memory layout" );
286 static_assert( sizeof(ValtypeT) == sizeof(ValtypeT2), "Can not spatial_cast with different memory layout" );
287 return reinterpret_cast<const VectorBundle<Valtype,ValtypeT>&>(from);
288}
289
290template<typename Valtype,typename ValtypeT> constexpr
291bool operator ==( const VectorBundle<Valtype,ValtypeT>& b1, const VectorBundle<Valtype,ValtypeT>& b2 ) noexcept{
292 return !(b1 != b2);
293}
294
295template<typename Valtype,typename ValtypeT> constexpr
296bool operator !=( const VectorBundle<Valtype,ValtypeT>& b1, const VectorBundle<Valtype,ValtypeT>& b2 ) noexcept{
297 if( b1.P != b2.P ||
298 b1.T != b2.T )
299 return true;
300
301 return false;
302}
303
304template<typename Valtype, typename ValtypeT>
306// d : result
307//
308// d*n1 == 0
309// d*n2 == 0
310// d = D + s2n2 - s1n1
311//
312// -> s1 = D*(n1 - n2*n1n2) / (1 - n1n2*n1n2)
313// s2 = D*(n1*n1n2 - n2) / (1 - n1n2*n1n2)
314{
315 Vector<Valtype> D = b2.P - b1.P;
316 Vector<ValtypeT> n1 = Normalize(b1.T).second;
317 Vector<ValtypeT> n2 = Normalize(b2.T).second;
318 ValtypeT n1n2 = n1 * n2;
319
320 if( n1n2 == ValtypeT{1} )
321 return { b1.P, D - D*n1*n1 };
322 else{
323 Valtype s1 = D*(n1 - n2*n1n2) / (1 - n1n2*n1n2);
324 Valtype s2 = D*(n1*n1n2 - n2) / (1 - n1n2*n1n2);
325
326 return { b1.P + s1*n1, D + s2*n2 - s1*n1 };
327 }
328}
329
330//{
331// Vector<ValtypeT> n = b1.T % b2.T;
332// if( n == Null<ValtypeT> )
333// return ((b2.P - b1.P) % b2.T).Length() / b2.T.Length();
334//
335// return abs((b2.P - b1.P) * n) / n.Length();
336//}
337
338} // namespace spat
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
VectorBundle< Valtype, ValtypeT > VectorBundle1
Different name for VectorBundle.
Definition VectorBundle.h:135
auto Normalize(const Frame< Valtype, ValtypeT > &f) noexcept -> std::pair< Vector< ValtypeT >, Frame< Valtype, decltype(ValtypeT{}/ValtypeT{})> >
Outer normalizing.
Definition Frame.h:1144
Frame< Valtype, ValtypeT > & spatial_cast(Frame< Valtype2, ValtypeT2 > &from) noexcept
Cast to Frame with different underlying value types.
Definition Frame.h:1062
VectorBundle1< Valtype, Valtype > Distance(const VectorBundle1< Valtype, ValtypeT > &b1, const VectorBundle1< Valtype, ValtypeT > &b2) noexcept
Definition VectorBundle.h:305
STL namespace.
A Frame ("TNBFrame") describes a location in 3d space and an orientation using a right handed coordin...
Definition Frame.h:52
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Implements a tangential space bundle.
Definition VectorBundle2.h:43
Implements a Vector bundle.
Definition VectorBundle.h:42
VectorBundle & TransportBy(const Vector< Valtype > &v) noexcept
VectorBundle & Init() noexcept
constexpr bool Equals(const VectorBundle &bundle, Valtype epsilon_length=std::numeric_limits< Valtype >::epsilon(), ValtypeT epsilon_lengthT=std::numeric_limits< ValtypeT >::epsilon()) const noexcept
constexpr VectorBundle() noexcept=default
Does not initialize the members.
VectorBundle & Rotate(const Vector< Valtype > &nr, Valtype angle) noexcept
Position< Valtype > P
Definition VectorBundle.h:43
VectorBundle & TransportTo(Valtype d) noexcept
Vector< ValtypeT > T
Definition VectorBundle.h:44
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48
@ ex
Unit vector in x-direction.
Definition Vector.h:59