Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
VectorBundle2.h
1// trax track library
2// AD 2013
3//
4// "we'll meet again some sunny day"
5//
6// Vera Lynn
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 VectorBundle;
35 template<typename,typename> struct Frame;
36 template<typename> class Rotation;
37
41 template<typename Valtype,typename ValtypeT = Valtype>
43 {
47
48
63 constexpr VectorBundle2() noexcept = default;
64 constexpr VectorBundle2( Valtype x, Valtype y, Valtype z,
65 ValtypeT tx, ValtypeT ty, ValtypeT tz,
66 ValtypeT nx, ValtypeT ny, ValtypeT nz ) noexcept;
67 constexpr VectorBundle2( const Position<Valtype>& pos,
68 const Vector<ValtypeT>& tan,
69 const Vector<ValtypeT>& nor ) noexcept;
70 constexpr VectorBundle2( const VectorBundle<Valtype,ValtypeT>& bundle,
71 const Vector<ValtypeT>& nor ) noexcept;
72 VectorBundle2( const Position<Valtype>& pos,
73 const Rotation<ValtypeT>& rot ) noexcept;
74 explicit constexpr VectorBundle2( const Frame<Valtype,ValtypeT>& frame ) noexcept;
75 explicit VectorBundle2( const Rotation<ValtypeT>& rot ) noexcept;
76 template<typename Valtype2>
77 explicit VectorBundle2( const Valtype2* pVal ) noexcept;
79
80
85 VectorBundle2& operator=( const Frame<Valtype,ValtypeT>& frame ) noexcept;
86
87 VectorBundle2& operator=( const Rotation<ValtypeT>& rot ) noexcept;
88
89 template<typename Valtype2>
90 VectorBundle2& operator=( const Valtype2* pVal ) noexcept;
92
93
96 operator VectorBundle<Valtype,ValtypeT>() const noexcept;
97
98
100 Valtype* ptr() noexcept;
101
102
104 const Valtype* ptr() const noexcept;
105
106
109 VectorBundle2& Init() noexcept;
110
111
116 VectorBundle2& Init( const VectorBundle<Valtype,ValtypeT>& bundle, const Vector<ValtypeT>& up ) noexcept;
117
118
121
122
126
127
131 bool IsOrthoNormal( ValtypeT epsilon_length = 2*std::numeric_limits<ValtypeT>::epsilon(), ValtypeT epsilon_angle = 2*std::numeric_limits<ValtypeT>::epsilon() ) const noexcept;
132
133
136 VectorBundle2& TransportTo( Valtype d ) noexcept;
137
138
141 VectorBundle2& TransportTo( const Position2D<Valtype>& p ) noexcept;
142
143
146 VectorBundle2& TransportTan( Valtype d ) noexcept;
147
148
151 VectorBundle2& TransportNor( Valtype d ) noexcept;
152
153
155 VectorBundle2& Rotate( const Vector<ValtypeT>& nr, ValtypeT angle ) noexcept;
156
157
159 VectorBundle2& Rotate( const Vector<ValtypeT>& r ) noexcept;
160
161
163 VectorBundle2& RotateTan( ValtypeT angle ) noexcept;
164
165
167 VectorBundle2& RotateNor( ValtypeT angle ) noexcept;
168
169
181 constexpr bool Equals( const VectorBundle2& bundle,
182 Valtype epsilon_length = std::numeric_limits<Valtype>::epsilon(),
183 ValtypeT epsilon_lengthT = std::numeric_limits<ValtypeT>::epsilon() ) const noexcept;
184 };
185
187 template<typename Valtype,typename ValtypeT=Valtype,typename Valtype2=Valtype,typename ValtypeT2=Valtype>
188 VectorBundle2<Valtype,ValtypeT>& spatial_cast( VectorBundle2<Valtype2,ValtypeT2>& from ) noexcept;
189
191 template<typename Valtype,typename ValtypeT=Valtype,typename Valtype2=Valtype,typename ValtypeT2=Valtype>
192 const VectorBundle2<Valtype,ValtypeT>& spatial_cast( const VectorBundle2<Valtype2,ValtypeT2>& from ) noexcept;
193
194
197
199 template<typename Valtype,typename ValtypeT> constexpr
200 bool operator==( const VectorBundle2<Valtype,ValtypeT>& b1, const VectorBundle2<Valtype,ValtypeT>& b2 ) noexcept;
201 template<typename Valtype,typename ValtypeT> constexpr
202 bool operator!=( const VectorBundle2<Valtype,ValtypeT>& b1, const VectorBundle2<Valtype,ValtypeT>& b2 ) noexcept;
204
206template<typename Valtype,typename ValtypeT> constexpr
207inline VectorBundle2<Valtype,ValtypeT>::VectorBundle2(
208 Valtype x, Valtype y, Valtype z,
209 ValtypeT tx, ValtypeT ty, ValtypeT tz,
210 ValtypeT nx, ValtypeT ny, ValtypeT nz ) noexcept
211 : P{x,y,z},
212 T{tx,ty,tz},
213 N{nx,ny,nz}
214{}
215
216template<typename Valtype,typename ValtypeT> constexpr
218 const Position<Valtype>& pos,
219 const Vector<ValtypeT>& tan,
220 const Vector<ValtypeT>& nor ) noexcept
221 : P{pos},
222 T{tan},
223 N{nor}
224{}
225
226template<typename Valtype,typename ValtypeT> constexpr
228 const VectorBundle<Valtype,ValtypeT>& bundle,
229 const Vector<ValtypeT>& nor ) noexcept
230 : P{bundle.P},
231 T{bundle.T},
232 N{nor}
233{}
234
235template<typename Valtype,typename ValtypeT>
237 const Position<Valtype>& pos,
238 const Rotation<ValtypeT>& rot ) noexcept
239 : P{pos}
240{
241 operator=( rot );
242}
243
244template<typename Valtype,typename ValtypeT> constexpr
246 const Frame<Valtype,ValtypeT>& frame ) noexcept
247 : P{frame.P},
248 T{frame.T},
249 N{frame.N}
250{}
251
252template<typename Valtype,typename ValtypeT>
254 const Rotation<ValtypeT>& rot ) noexcept
255{
256 operator=( rot );
257}
258
259template<typename Valtype,typename ValtypeT>
260inline VectorBundle2<Valtype,ValtypeT>& VectorBundle2<Valtype,ValtypeT>::operator=( const Frame<Valtype,ValtypeT>& frame ) noexcept{
261 P = frame.P;
262 T = frame.T;
263 N = frame.N;
264 return *this;
265}
266
267template<typename Valtype,typename ValtypeT>
268inline VectorBundle2<Valtype,ValtypeT>& VectorBundle2<Valtype,ValtypeT>::operator=( const Rotation<ValtypeT>& rot ) noexcept{
269 T.dx = rot(0,0);
270 T.dy = rot(0,1);
271 T.dz = rot(0,2);
272
273 N.dx = rot(1,0);
274 N.dy = rot(1,1);
275 N.dz = rot(1,2);
276
277 return *this;
278}
279
280template<typename Valtype,typename ValtypeT>
281template<typename Valtype2>
282inline VectorBundle2<Valtype,ValtypeT>::VectorBundle2( const Valtype2* pVal ) noexcept
283 : P{pVal},
284 T{pVal+3},
285 N{pVal+6}
286{
287}
288
289template<typename Valtype,typename ValtypeT>
290template<typename Valtype2>
292 P = pVal;
293 T = pVal + 3;
294 N = pVal + 6;
295 return *this;
296}
297
298template<typename Valtype,typename ValtypeT>
302
303template<typename Valtype,typename ValtypeT>
304inline Valtype* VectorBundle2<Valtype,ValtypeT>::ptr() noexcept{
305 static_assert( sizeof(Valtype) == sizeof(ValtypeT), "Can not access a VectorBundle via pointer if the two types have different memory sizes.");
306 return P.ptr();
307}
308
309template<typename Valtype,typename ValtypeT>
310inline const Valtype * VectorBundle2<Valtype,ValtypeT>::ptr() const noexcept{
311 static_assert( sizeof(Valtype) == sizeof(ValtypeT), "Can not access a VectorBundle via pointer if the two types have different memory sizes.");
312 return P.ptr();
313}
314
315template<typename Valtype,typename ValtypeT>
322
323template<typename Valtype,typename ValtypeT>
325 P = vb.P;
326 T = vb.T;
327
328 N = up % T;
329 if( N.Normalize() == 0 )
330 // up and tan are parallel
331 {
333 N -= (N*up)*up;
334 if( N.Normalize() == 0 )
335 // up and ey are parallel
336 {
338 N -= (N*up)*up;
339 N.Normalize();
340 assert(N.Length() != 0); // should not be possible...
341 }
342 }
343 return *this;
344}
345
346template<typename Valtype,typename ValtypeT>
348 T.Normalize();
349 N.Normalize();
350 return *this;
351}
352
353template<typename Valtype,typename ValtypeT>
356 {
357 T.dx = ValtypeT{1};
358 N = Ey<ValtypeT>;
359 }
360 T.Normalize();
361 N -= (N * T) * T;
362
364 {
365 if ( !(T % Ey<ValtypeT>).Is(Vector<ValtypeT>::specials::null) )
366 N = Ey<ValtypeT> - (Ey<ValtypeT> * T) * T;
367 else
368 N = Ex<ValtypeT> - (Ex<ValtypeT> * T) * T;
369 }
370
371 N.Normalize();
372 return *this;
373}
374
375template<typename Valtype,typename ValtypeT>
376inline bool VectorBundle2<Valtype,ValtypeT>::IsOrthoNormal( ValtypeT _epsilon_length, ValtypeT _epsilon_angle ) const noexcept{
377 return T.IsNormal( _epsilon_length ) &&
378 N.IsNormal( _epsilon_length ) &&
379 std::abs( T*N ) <= _epsilon_angle;
380}
381
382template<typename Valtype,typename ValtypeT>
384 P += d * T;
385 return *this;
386}
387
388template<typename Valtype,typename ValtypeT>
390 P += p.x * T + p.y * N;
391 return *this;
392}
393
394template<typename Valtype,typename ValtypeT>
396 P += dist * T;
397 return *this;
398}
399
400template<typename Valtype,typename ValtypeT>
402 P += dist * N;
403 return *this;
404}
405
406template<typename Valtype,typename ValtypeT>
408 T.Rotate( nr, angle );
409 N.Rotate( nr, angle );
410 return *this;
411}
412
413template<typename Valtype,typename ValtypeT>
415 Vector<ValtypeT> r(rot);
416 ValtypeT angle = r.Normalize();
417 T.Rotate( r, angle );
418 N.Rotate( r, angle );
419 return *this;
420}
421
422template<typename Valtype,typename ValtypeT>
424 N.Rotate( T, angle );
425 return *this;
426}
427
428template<typename Valtype,typename ValtypeT>
430 T.Rotate( N, angle );
431 return *this;
432}
433
434template<typename Valtype,typename ValtypeT> constexpr
435inline bool VectorBundle2<Valtype,ValtypeT>::Equals( const VectorBundle2& bundle, Valtype _epsilon_length, ValtypeT epsilon_lengthT ) const noexcept{
436 return P.Equals( bundle.P, _epsilon_length ) &&
437 T.Equals( bundle.T, epsilon_lengthT ) &&
438 N.Equals( bundle.N, epsilon_lengthT );
439}
440
441template<typename Valtype,typename ValtypeT,typename Valtype2,typename ValtypeT2>
443 static_assert( sizeof(Valtype) == sizeof(Valtype2), "Can not spatial_cast with different memory layout" );
444 static_assert( sizeof(ValtypeT) == sizeof(ValtypeT2), "Can not spatial_cast with different memory layout" );
445 return reinterpret_cast<VectorBundle2<Valtype,ValtypeT>&>(from);
446}
447
448
449template<typename Valtype,typename ValtypeT,typename Valtype2,typename ValtypeT2>
451 static_assert( sizeof(Valtype) == sizeof(Valtype2), "Can not spatial_cast with different memory layout" );
452 static_assert( sizeof(ValtypeT) == sizeof(ValtypeT2), "Can not spatial_cast with different memory layout" );
453 return reinterpret_cast<const VectorBundle2<Valtype,ValtypeT>&>(from);
454}
455
456template<typename Valtype,typename ValtypeT> constexpr
457inline bool operator ==( const VectorBundle2<Valtype,ValtypeT>& b1, const VectorBundle2<Valtype,ValtypeT>& b2 ) noexcept{
458 return !(b1 != b2);
459}
460
461template<typename Valtype,typename ValtypeT> constexpr
462inline bool operator !=( const VectorBundle2<Valtype,ValtypeT>& b1, const VectorBundle2<Valtype,ValtypeT>& b2 ) noexcept{
463 return b1.P != b2.P ||
464 b1.T != b2.T ||
465 b1.N != b2.N;
466}
467
468} // namespace spat
Rotation matrix.
Definition Matrix.h:607
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Frame< Valtype, ValtypeT > & spatial_cast(Frame< Valtype2, ValtypeT2 > &from) noexcept
Cast to Frame with different underlying value types.
Definition Frame.h:1062
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 2D - position in cartesian coordinates.
Definition Position2D.h:45
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Implements a tangential space bundle.
Definition VectorBundle2.h:43
VectorBundle2 & TransportTo(dim::Value< Dimension< 1, 0, 0 > > d) noexcept
Definition VectorBundle2.h:383
dim::Value< Dimension< 1, 0, 0 > > * ptr() noexcept
Definition VectorBundle2.h:304
Vector< ValtypeT > T
Tangent vector or x-axis.
Definition VectorBundle2.h:45
VectorBundle2 & Rotate(const Vector< dim::Value< Dimension< 0, 0, 0 > > > &nr, dim::Value< Dimension< 0, 0, 0 > > angle) noexcept
Definition VectorBundle2.h:407
VectorBundle2 & TransportNor(dim::Value< Dimension< 1, 0, 0 > > d) noexcept
Definition VectorBundle2.h:401
VectorBundle2 & TransportTan(dim::Value< Dimension< 1, 0, 0 > > d) noexcept
Definition VectorBundle2.h:395
bool IsOrthoNormal(dim::Value< Dimension< 0, 0, 0 > > epsilon_length=2 *std::numeric_limits< dim::Value< Dimension< 0, 0, 0 > > >::epsilon(), dim::Value< Dimension< 0, 0, 0 > > epsilon_angle=2 *std::numeric_limits< dim::Value< Dimension< 0, 0, 0 > > >::epsilon()) const noexcept
Definition VectorBundle2.h:376
constexpr bool Equals(const VectorBundle2 &bundle, dim::Value< Dimension< 1, 0, 0 > > epsilon_length=std::numeric_limits< dim::Value< Dimension< 1, 0, 0 > > >::epsilon(), dim::Value< Dimension< 0, 0, 0 > > epsilon_lengthT=std::numeric_limits< dim::Value< Dimension< 0, 0, 0 > > >::epsilon()) const noexcept
Definition VectorBundle2.h:435
constexpr VectorBundle2() noexcept=default
Does not initialize the members.
VectorBundle2 & RotateNor(dim::Value< Dimension< 0, 0, 0 > > angle) noexcept
Definition VectorBundle2.h:429
Vector< ValtypeT > N
Normal axis or y-axis.
Definition VectorBundle2.h:46
VectorBundle2 & RotateTan(dim::Value< Dimension< 0, 0, 0 > > angle) noexcept
Definition VectorBundle2.h:423
Position< Valtype > P
Base space postion.
Definition VectorBundle2.h:44
Implements a Vector bundle.
Definition VectorBundle.h:42
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48
@ null
Vector with all parameters zeroed.
Definition Vector.h:58
@ ex
Unit vector in x-direction.
Definition Vector.h:59
@ ey
Unit vector in y-direction.
Definition Vector.h:60
auto Normalize() noexcept -> decltype(Valtype{}/Valtype{})
Normalizes the vector to length 1.
Definition Vector.h:487