| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <anki/math/CommonIncludes.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 Mat4& m4)
- {
- m_rotation = TMat3x4<T>(m4.getRotationPart());
- m_origin = m4.getTranslationPart().xyz0();
- m_scale = 1.0;
- checkW();
- }
- TTransform(
- const TVec4<T>& origin, const TMat3x4<T>& rotation, const T scale)
- : m_origin(origin)
- , m_rotation(rotation)
- , m_scale(scale)
- {
- checkW();
- }
- /// @}
- /// @name Accessors
- /// @{
- const TVec4<T>& getOrigin() const
- {
- return m_origin;
- }
- TVec4<T>& getOrigin()
- {
- return m_origin;
- }
- void setOrigin(const TVec4<T>& o)
- {
- m_origin = o;
- checkW();
- }
- const TMat3x4<T>& getRotation() const
- {
- return m_rotation;
- }
- TMat3x4<T>& getRotation()
- {
- return m_rotation;
- }
- void setRotation(const TMat3x4<T>& 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 const TTransform& getIdentity()
- {
- static const TTransform ident(
- TVec4<T>(0.0), TMat3x4<T>::getIdentity(), 1.0);
- return ident;
- }
- /// @copybrief combineTTransformations
- TTransform combineTransformations(const TTransform& b) const
- {
- checkW();
- const TTransform& a = *this;
- TTransform out;
- out.m_origin =
- TVec4<T>(a.m_rotation * (b.m_origin * a.m_scale), 0.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 = 1.0 / m_scale;
- o.m_origin = -((o.m_rotation * o.m_scale) * m_origin).xyz0();
- return o;
- }
- void invert()
- {
- m_rotation.transposeRotationPart();
- m_scale = 1.0 / m_scale;
- m_origin = -((m_rotation * m_scale) * m_origin);
- }
- /// Transform a TVec3
- TVec3<T> transform(const TVec3<T>& b) const
- {
- checkW();
- return (m_rotation.getRotationPart() * (b * m_scale)) + m_origin.xyz();
- }
- /// Transform a TVec4. SIMD optimized
- TVec4<T> transform(const TVec4<T>& b) const
- {
- checkW();
- TVec4<T> out = TVec4<T>(m_rotation * (b * m_scale), 0.0) + m_origin;
- return out;
- }
- template<typename TAlloc>
- String toString(TAlloc alloc) const
- {
- ANKI_ASSERT(0 && "TODO");
- return String();
- }
- /// @}
- private:
- /// @name Data
- /// @{
- TVec4<T> m_origin; ///< The rotation
- TMat3x4<T> m_rotation; ///< The translation
- T m_scale; ///< The uniform scaling
- /// @}
- void checkW() const
- {
- ANKI_ASSERT(m_origin.w() == 0.0);
- }
- };
- /// F32 transformation
- typedef TTransform<F32> Transform;
- /// @}
- } // end namespace anki
|