| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Math/Common.h>
- namespace anki {
- /// @addtogroup math
- /// @{
- /// Transformation
- template<typename T>
- class TTransform
- {
- public:
- /// @name Constructors
- /// @{
- TTransform()
- {
- }
- TTransform(const TTransform& b)
- : m_origin(b.m_origin)
- , m_rotation(b.m_rotation)
- , m_scale(b.m_scale)
- {
- checkW();
- }
- explicit TTransform(const TMat<T, 4, 4>& m4)
- {
- const TVec<T, 3> s0 = m4.getColumn(0).xyz();
- const TVec<T, 3> s1 = m4.getColumn(1).xyz();
- const TVec<T, 3> s2 = m4.getColumn(2).xyz();
- const TVec<T, 3> scales(s0.getLength(), s1.getLength(), s2.getLength());
- [[maybe_unused]] const T E = T(0.001);
- ANKI_ASSERT(isZero(scales.x() - scales.y(), E) && isZero(scales.y() - scales.z(), E)
- && "Expecting uniform scale");
- m_rotation.setColumns(s0 / scales.x(), s1 / scales.x(), s2 / scales.x(), TVec<T, 3>(T(0)));
- m_origin = m4.getTranslationPart().xyz0();
- m_scale = scales.x();
- checkW();
- }
- TTransform(const TVec<T, 4>& origin, const TMat<T, 3, 4>& rotation, const T scale)
- : m_origin(origin)
- , m_rotation(rotation)
- , m_scale(scale)
- {
- checkW();
- }
- explicit TTransform(const TVec<T, 4>& origin)
- : m_origin(origin)
- , m_rotation(TMat<T, 3, 4>::getIdentity())
- , m_scale(T(1))
- {
- checkW();
- }
- explicit TTransform(const TMat<T, 3, 4>& rotation)
- : m_origin(TVec<T, 4>(T(0)))
- , m_rotation(rotation)
- , m_scale(T(1))
- {
- checkW();
- }
- TTransform(const T scale)
- : m_origin(TVec<T, 4>(T(0)))
- , m_rotation(TMat<T, 3, 4>::getIdentity())
- , m_scale(scale)
- {
- checkW();
- }
- /// @}
- /// @name Accessors
- /// @{
- const TVec<T, 4>& getOrigin() const
- {
- return m_origin;
- }
- TVec<T, 4>& getOrigin()
- {
- return m_origin;
- }
- void setOrigin(const TVec<T, 4>& o)
- {
- m_origin = o;
- checkW();
- }
- const TMat<T, 3, 4>& getRotation() const
- {
- return m_rotation;
- }
- TMat<T, 3, 4>& getRotation()
- {
- return m_rotation;
- }
- void setRotation(const TMat<T, 3, 4>& r)
- {
- m_rotation = r;
- }
- T getScale() const
- {
- return m_scale;
- }
- T& getScale()
- {
- return m_scale;
- }
- void setScale(const T s)
- {
- m_scale = s;
- }
- /// @}
- /// @name Operators with same type
- /// @{
- TTransform& operator=(const TTransform& b)
- {
- m_origin = b.m_origin;
- m_rotation = b.m_rotation;
- m_scale = b.m_scale;
- checkW();
- return *this;
- }
- Bool operator==(const TTransform& b) const
- {
- return m_origin == b.m_origin && m_rotation == b.m_rotation && m_scale == b.m_scale;
- }
- Bool operator!=(const TTransform& b) const
- {
- return !operator==(b);
- }
- /// @}
- /// @name Other
- /// @{
- void setIdentity()
- {
- (*this) = getIdentity();
- }
- static TTransform getIdentity()
- {
- return TTransform(TVec<T, 4>(T(0)), TMat<T, 3, 4>::getIdentity(), T(1));
- }
- /// @copybrief combineTTransformations
- TTransform combineTransformations(const TTransform& b) const
- {
- checkW();
- const TTransform& a = *this;
- TTransform out;
- out.m_origin = TVec<T, 4>(a.m_rotation * (b.m_origin * a.m_scale), T(0)) + a.m_origin;
- out.m_rotation = a.m_rotation.combineTransformations(b.m_rotation);
- out.m_scale = a.m_scale * b.m_scale;
- return out;
- }
- /// Get the inverse transformation. Its faster that inverting a Mat4
- TTransform getInverse() const
- {
- checkW();
- TTransform o;
- o.m_rotation = m_rotation;
- o.m_rotation.transposeRotationPart();
- o.m_scale = T(1) / m_scale;
- o.m_origin = -(o.m_rotation * (o.m_scale * m_origin)).xyz0();
- return o;
- }
- void invert()
- {
- m_rotation.transposeRotationPart();
- m_scale = T(1) / m_scale;
- m_origin = -(m_rotation * (m_scale * m_origin));
- }
- /// Transform a TVec3
- TVec<T, 3> transform(const TVec<T, 3>& b) const
- {
- checkW();
- return (m_rotation.getRotationPart() * (b * m_scale)) + m_origin.xyz();
- }
- /// Transform a TVec4. SIMD optimized
- TVec<T, 4> transform(const TVec<T, 4>& b) const
- {
- checkW();
- TVec<T, 4> out = TVec<T, 4>(m_rotation * (b * m_scale), T(0)) + m_origin;
- return out;
- }
- template<U VEC_DIMS>
- TTransform& lookAt(const TVec<T, VEC_DIMS>& refPoint, const TVec<T, VEC_DIMS>& up)
- {
- const TVec<T, 4> j = up.xyz0();
- const TVec<T, 4> vdir = (refPoint.xyz0() - m_origin).getNormalized();
- const TVec<T, 4> vup = (j - vdir * j.dot(vdir)).getNormalized();
- const TVec<T, 4> vside = vdir.cross(vup);
- m_rotation.setColumns(vside.xyz(), vup.xyz(), (-vdir).xyz());
- return *this;
- }
- ANKI_ENABLE_METHOD(std::is_floating_point<T>::value) void toString(StringAuto& str) const
- {
- StringAuto b(str.getAllocator());
- m_origin.toString(b);
- str.append(b);
- str.append("\n");
- b.destroy();
- m_rotation.toString(b);
- str.append(b);
- str.append("\n");
- b.destroy();
- b.sprintf("%f", m_scale);
- str.append(b);
- }
- /// @}
- private:
- /// @name Data
- /// @{
- TVec<T, 4> m_origin; ///< The rotation
- TMat<T, 3, 4> m_rotation; ///< The translation
- T m_scale; ///< The uniform scaling
- /// @}
- void checkW() const
- {
- ANKI_ASSERT(m_origin.w() == T(0));
- }
- };
- /// F32 transformation
- using Transform = TTransform<F32>;
- /// F64 transformation
- using DTransform = TTransform<F64>;
- /// @}
- } // end namespace anki
|