SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
TransformPosition.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire Development Team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_TRANSFORM_POSITION_GUARD
6#define SAPPHIRE_MATHS_TRANSFORM_POSITION_GUARD
7
9
12
23namespace SA
24{
30 template <typename T>
32 {
35
36
37 //{ Constructors
38
40 TrPosition() = default;
41
47 TrPosition(const Vec3<T>& _pos) noexcept :
48 position{ _pos }
49 {
50 }
51
52 //}
53
54
55 //{ Cast
56
63 template <typename TOut>
64 operator TrPosition<TOut>() const noexcept
65 {
67 }
68
69 //}
70
71 protected:
72
73 //{ Equals
74
80 constexpr bool IsZero() const noexcept
81 {
82 return position.IsZero();
83 }
84
91 constexpr bool IsIdentity() const noexcept
92 {
93 // Position is Zero in identity transform.
94 return position.IsZero();
95 }
96
105 constexpr bool Equals(const TrPosition& _other,
106 T _epsilon = std::numeric_limits<T>::epsilon()) const noexcept
107 {
108 return position.Equals(_other.position, _epsilon);
109 }
110
111 //}
112
113
114 //{ Lerp
115
124 static TrPosition LerpUnclamped(const TrPosition& _start, const TrPosition& _end, float _alpha) noexcept
125 {
126 return TrPosition{ Vec3<T>::LerpUnclamped(_start.position, _end.position, _alpha) };
127 }
128
129 //}
130
131
132 //{ Operators
133
146 template <typename LhsT, typename RhsT>
147 static TrPosition Multiply(const LhsT& _lhs, const RhsT& _rhs) noexcept
148 {
149 if constexpr (TrTHasComponent(RhsT)<SA::TrPosition>())
150 {
151 // Position component found.
152
153 if constexpr (TrTHasComponent(LhsT)<TrRotation>())
154 {
155 // Self rotation component found.
156 // Apply translation with rotation:
157 // position + rotation.Rotate(_other.position)
158
159 return TrPosition{ _lhs.position + _lhs.rotation.Rotate(_rhs.position) };
160 }
161 else
162 {
163 // No rotation: apply translation.
164 return TrPosition{ _lhs.position + _rhs.position };
165 }
166 }
167 else
168 {
169 // Default: forward component (lhs always has this component).
170
171 return _lhs;
172 }
173 }
174
175
188 template <typename LhsT, typename RhsT>
189 static TrPosition Divide(const LhsT& _lhs, const RhsT& _rhs) noexcept
190 {
191 if constexpr (TrTHasComponent(RhsT)<SA::TrPosition>())
192 {
193 // Position component found.
194
195 if constexpr (TrTHasComponent(LhsT)<TrRotation>())
196 {
197 // Self rotation component found.
198 // Apply inverse translation with rotation:
199 // position - rotation.Rotate(_other.position)
200
201 return TrPosition{ _lhs.position - _lhs.rotation.Rotate(_rhs.position) };
202 }
203 else
204 {
205 // No rotation: apply inverse translation.
206 return TrPosition{ _lhs.position - _rhs.position };
207 }
208 }
209 else
210 {
211 // Default: forward component (lhs always has this component).
212
213 return _lhs;
214 }
215 }
216
217 //}
218
219
220 #if SA_LOGGER_IMPL
221
227 std::string ToString() const
228 {
229 return "Pos: " + SA::ToString(position);
230 }
231
232 #endif
233
234 };
235}
236
237
240#endif // GUARD
Transform Component base helper definition.
#define TrTHasComponent(_trT)
Helper macro for transform HasComponent check in templated functions.
Definition TransformComponent.hpp:21
Transform rotation component definition.
Vector 3 type implementation.
Transform position component.
Definition TransformPosition.hpp:32
constexpr bool IsIdentity() const noexcept
IsIdentity component implementation. position must be at Vec3::Zero for identity transform.
Definition TransformPosition.hpp:91
TrPosition()=default
Default constructor.
TrPosition(const Vec3< T > &_pos) noexcept
Constructor from Vec3.
Definition TransformPosition.hpp:47
constexpr bool IsZero() const noexcept
IsZero component implementation.
Definition TransformPosition.hpp:80
static TrPosition Multiply(const LhsT &_lhs, const RhsT &_rhs) noexcept
Transform multiplication implementation for position component.
Definition TransformPosition.hpp:147
static TrPosition LerpUnclamped(const TrPosition &_start, const TrPosition &_end, float _alpha) noexcept
LerpUnclamped component implementation.
Definition TransformPosition.hpp:124
static TrPosition Divide(const LhsT &_lhs, const RhsT &_rhs) noexcept
Transform division implementation for position component.
Definition TransformPosition.hpp:189
Vec3< T > position
Handled position.
Definition TransformPosition.hpp:34
constexpr bool Equals(const TrPosition &_other, T _epsilon=std::numeric_limits< T >::epsilon()) const noexcept
Equals component implementation.
Definition TransformPosition.hpp:105
Transform rotation component.
Definition TransformRotation.hpp:32
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43
static Vec3 LerpUnclamped(const Vec3 &_start, const Vec3 &_end, float _alpha) noexcept
Unclamped Lerp from _start to _end at _alpha.