transformlib.h
Go to the documentation of this file.00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #if !defined (INCLUDED_TRANSFORMLIB_H)
00027 #define INCLUDED_TRANSFORMLIB_H
00028
00029 #include "generic/constant.h"
00030 #include "math/matrix.h"
00031 #include "math/quaternion.h"
00032
00034 class TransformNode
00035 {
00036 public:
00037 STRING_CONSTANT(Name, "TransformNode");
00038 virtual ~TransformNode ()
00039 {
00040 }
00042 virtual const Matrix4& localToParent () const = 0;
00043 };
00044
00046 class IdentityTransform: public TransformNode
00047 {
00048 public:
00050 const Matrix4& localToParent () const
00051 {
00052 return g_matrix4_identity;
00053 }
00054 };
00055
00057 class MatrixTransform: public TransformNode
00058 {
00059 Matrix4 m_localToParent;
00060 public:
00061 MatrixTransform () :
00062 m_localToParent(g_matrix4_identity)
00063 {
00064 }
00065
00066 Matrix4& localToParent ()
00067 {
00068 return m_localToParent;
00069 }
00071 const Matrix4& localToParent () const
00072 {
00073 return m_localToParent;
00074 }
00075 };
00076
00077 #include "generic/callback.h"
00078
00079 typedef Vector3 Translation;
00080 typedef Quaternion Rotation;
00081 typedef Vector3 Scale;
00082
00083 inline Matrix4 matrix4_transform_for_components (const Translation& translation, const Rotation& rotation,
00084 const Scale& scale)
00085 {
00086 Matrix4 result(matrix4_rotation_for_quaternion_quantised(rotation));
00087 result.x().getVector3() *= scale.x();
00088 result.y().getVector3() *= scale.y();
00089 result.z().getVector3() *= scale.z();
00090 result.tx() = translation.x();
00091 result.ty() = translation.y();
00092 result.tz() = translation.z();
00093 return result;
00094 }
00095
00096 typedef bool TransformModifierType;
00097 const TransformModifierType TRANSFORM_PRIMITIVE = false;
00098 const TransformModifierType TRANSFORM_COMPONENT = true;
00099
00109 class Transformable
00110 {
00111 public:
00112 STRING_CONSTANT(Name, "Transformable");
00113
00114 virtual ~Transformable ()
00115 {
00116 }
00117
00118 virtual void setType (TransformModifierType type) = 0;
00119 virtual void setTranslation (const Translation& value) = 0;
00120 virtual void setRotation (const Rotation& value) = 0;
00121 virtual void setScale (const Scale& value) = 0;
00122 virtual void freezeTransform () = 0;
00123 };
00124
00125 const Translation c_translation_identity = Translation(0, 0, 0);
00126 const Rotation c_rotation_identity = c_quaternion_identity;
00127 const Scale c_scale_identity = Scale(1, 1, 1);
00128
00129 class TransformModifier: public Transformable
00130 {
00131 Translation m_translation;
00132 Rotation m_rotation;
00133 Scale m_scale;
00134 Callback m_changed;
00135 Callback m_apply;
00136 TransformModifierType m_type;
00137 public:
00138
00139 TransformModifier (const Callback& changed, const Callback& apply) :
00140 m_translation(c_translation_identity), m_rotation(c_quaternion_identity), m_scale(c_scale_identity),
00141 m_changed(changed), m_apply(apply), m_type(TRANSFORM_PRIMITIVE)
00142 {
00143 }
00144 void setType (TransformModifierType type)
00145 {
00146 m_type = type;
00147 }
00148 TransformModifierType getType () const
00149 {
00150 return m_type;
00151 }
00152 void setTranslation (const Translation& value)
00153 {
00154 m_translation = value;
00155 m_changed();
00156 }
00157 void setRotation (const Rotation& value)
00158 {
00159 m_rotation = value;
00160 m_changed();
00161 }
00162 void setScale (const Scale& value)
00163 {
00164 m_scale = value;
00165 m_changed();
00166 }
00167 void freezeTransform ()
00168 {
00169 if (m_translation != c_translation_identity || m_rotation != c_rotation_identity || m_scale
00170 != c_scale_identity) {
00171 m_apply();
00172 m_translation = c_translation_identity;
00173 m_rotation = c_rotation_identity;
00174 m_scale = c_scale_identity;
00175 m_changed();
00176 }
00177 }
00178 const Translation& getTranslation () const
00179 {
00180 return m_translation;
00181 }
00182 const Rotation& getRotation () const
00183 {
00184 return m_rotation;
00185 }
00186 const Scale& getScale () const
00187 {
00188 return m_scale;
00189 }
00190 Matrix4 calculateTransform () const
00191 {
00192 return matrix4_transform_for_components(getTranslation(), getRotation(), getScale());
00193 }
00194 };
00195
00196 #endif