00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_SIMPLETRANSFORM2_HPP_)
00012 #define _XRB_SIMPLETRANSFORM2_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include <stdio.h>
00017
00018 #include "xrb_vector.hpp"
00019
00020 namespace Xrb
00021 {
00022
00023 template <typename T>
00024 class SimpleTransform2
00025 {
00026 public:
00027
00028 static SimpleTransform2<T> const ms_identity;
00029
00030 enum ScaleFactorComponent
00031 {
00032 R = Dim::X,
00033 S = Dim::Y
00034 };
00035
00036 enum TranslationComponent
00037 {
00038 X = Dim::X,
00039 Y = Dim::Y
00040 };
00041
00042
00043
00044
00045 Vector<T, 2> m_scale_factors;
00046 Vector<T, 2> m_translation;
00047
00048
00049
00050
00051
00052 inline SimpleTransform2 () { }
00053 inline SimpleTransform2 (T r, T s, T x, T y)
00054 {
00055 m_scale_factors.SetComponents(r, s);
00056 m_translation.SetComponents(x, y);
00057 }
00058 inline SimpleTransform2 (SimpleTransform2<T> const &source)
00059 {
00060 m_scale_factors = source.m_scale_factors;
00061 m_translation = source.m_translation;
00062 }
00063 inline ~SimpleTransform2 () { }
00064
00065
00066
00067
00068
00069 inline T const &operator [] (ScaleFactorComponent scale_factor_component) const
00070 {
00071 return m_scale_factors.m[static_cast<Dim::Component>(scale_factor_component)];
00072 }
00073 inline T &operator [] (ScaleFactorComponent scale_factor_component)
00074 {
00075 return m_scale_factors.m[static_cast<Dim::Component>(scale_factor_component)];
00076 }
00077 inline T const &operator [] (TranslationComponent translation_component) const
00078 {
00079 return m_translation.m[static_cast<Dim::Component>(translation_component)];
00080 }
00081 inline T &operator [] (TranslationComponent translation_component)
00082 {
00083 return m_translation.m[static_cast<Dim::Component>(translation_component)];
00084 }
00085 inline void operator *= (SimpleTransform2<T> const &operand)
00086 {
00087 m_scale_factors *= operand.m_scale_factors;
00088 m_translation *= operand.m_scale_factors;
00089 m_translation += operand.m_translation;
00090 }
00091 inline void operator = (SimpleTransform2<T> const &operand)
00092 {
00093 m_scale_factors = operand.m_scale_factors;
00094 m_translation = operand.m_translation;
00095 }
00096
00097
00098
00099
00100
00101 Vector<T, 2> const &ScaleFactors () const
00102 {
00103 return m_scale_factors;
00104 }
00105 Vector<T, 2> const &Translation () const
00106 {
00107 return m_translation;
00108 }
00109
00110 SimpleTransform2<T> const &Transformation () const
00111 {
00112 return *this;
00113 }
00114
00115 SimpleTransform2<T> Inverse () const
00116 {
00117 SimpleTransform2<T> retval;
00118 T determinant = m_scale_factors[Dim::X] * m_scale_factors[Dim::Y];
00119 T negative_determinant = -determinant;
00120 if (determinant != negative_determinant)
00121 {
00122 retval.m_translation = -m_translation / m_scale_factors;
00123 retval.m_scale_factors = 1.0f / m_scale_factors;
00124 }
00125 return retval;
00126 }
00127
00128
00129
00130
00131
00132 inline void Invert ()
00133 {
00134 T determinant = m_scale_factors[Dim::X] * m_scale_factors[Dim::Y];
00135 T negative_determinant = -determinant;
00136 if (determinant != negative_determinant)
00137 {
00138 m_translation /= -m_scale_factors;
00139 m_scale_factors = 1.0f / m_scale_factors;
00140 }
00141 }
00142
00143 inline void Scale (Vector<T, 2> const &scale_factors)
00144 {
00145 m_scale_factors *= scale_factors;
00146 }
00147 inline void Scale (T r, T s)
00148 {
00149 m_scale_factors[Dim::X] *= r;
00150 m_scale_factors[Dim::Y] *= s;
00151 }
00152 inline void Scale (T scale_factor)
00153 {
00154 m_scale_factors *= scale_factor;
00155 }
00156 inline void Translate (Vector<T, 2> const &translation)
00157 {
00158 m_translation += translation;
00159 }
00160 inline void Translate (T x, T y)
00161 {
00162 m_translation[Dim::X] += x;
00163 m_translation[Dim::Y] += y;
00164 }
00165
00166 inline void SetScaleFactors (Vector<T, 2> const &scale_factors)
00167 {
00168 m_scale_factors = scale_factors;
00169 }
00170 inline void SetScaleFactors (T r, T s)
00171 {
00172 m_scale_factors[Dim::X] = r;
00173 m_scale_factors[Dim::Y] = s;
00174 }
00175 inline void SetScaleFactor (ScaleFactorComponent component, T scale_factor)
00176 {
00177 ASSERT1(component == R || component == S);
00178 m_scale_factors[component] = scale_factor;
00179 }
00180 inline void SetScaleFactor (T scale_factor)
00181 {
00182 m_scale_factors[Dim::X] == scale_factor;
00183 m_scale_factors[Dim::Y] == scale_factor;
00184 }
00185 inline void SetTranslation (Vector<T, 2> const &translation)
00186 {
00187 m_translation = translation;
00188 }
00189 inline void SetTranslation (T x, T y)
00190 {
00191 m_translation[Dim::X] = x;
00192 m_translation[Dim::Y] = y;
00193 }
00194 inline void SetTranslation (TranslationComponent component, T translation)
00195 {
00196 ASSERT1(component == X || component == Y);
00197 m_translation[component] = translation;
00198 }
00199
00200 inline void SetComponents (T r, T s, T x, T y)
00201 {
00202 m_scale_factors.SetComponents(r, s);
00203 m_translation.SetComponents(x, y);
00204 }
00205
00206
00207
00208
00209
00210 void Fprint (FILE *fptr, char const *component_printf_format) const
00211 {
00212 ASSERT1(fptr != NULL);
00213 ASSERT1(component_printf_format != NULL);
00214
00215
00216 T zero = m_scale_factors[Dim::X] - m_scale_factors[Dim::X];
00217
00218 fprintf(fptr, "SimpleTransform2:\n\t[");
00219 fprintf(fptr, component_printf_format, m_scale_factors[Dim::X]);
00220 fprintf(fptr, ", ");
00221 fprintf(fptr, component_printf_format, zero);
00222 fprintf(fptr, ", ");
00223 fprintf(fptr, component_printf_format, m_translation[Dim::X]);
00224 fprintf(fptr, "]\n\t[");
00225 fprintf(fptr, component_printf_format, m_scale_factors[Dim::Y]);
00226 fprintf(fptr, ", ");
00227 fprintf(fptr, component_printf_format, zero);
00228 fprintf(fptr, ", ");
00229 fprintf(fptr, component_printf_format, m_translation[Dim::Y]);
00230 fprintf(fptr, "]\n");
00231 }
00232 };
00233
00239 template <typename T>
00240 SimpleTransform2<T> const SimpleTransform2<T>::ms_identity(
00241 static_cast<T>(1), static_cast<T>(1),
00242 static_cast<T>(0), static_cast<T>(0));
00243
00244
00245
00246
00247
00248
00249 template <typename T>
00250 inline SimpleTransform2<T> operator * (SimpleTransform2<T> const left_operand,
00251 SimpleTransform2<T> const right_operand)
00252 {
00253 return SimpleTransform2<T>(
00254 left_operand[SimpleTransform2<T>::R] * right_operand[SimpleTransform2<T>::R],
00255 left_operand[SimpleTransform2<T>::S] * right_operand[SimpleTransform2<T>::S],
00256 left_operand[SimpleTransform2<T>::R] * right_operand[SimpleTransform2<T>::X] +
00257 left_operand[SimpleTransform2<T>::X],
00258 left_operand[SimpleTransform2<T>::S] * right_operand[SimpleTransform2<T>::Y] +
00259 left_operand[SimpleTransform2<T>::Y]);
00260 }
00261
00262
00263 template <typename T>
00264 inline void operator *= (Vector<T, 2> &assignee,
00265 SimpleTransform2<T> const &operand)
00266 {
00267 assignee *= operand.m_scale_factors;
00268 assignee += operand.m_translation;
00269 }
00270
00271
00272 template <typename T>
00273 inline Vector<T, 2> operator * (SimpleTransform2<T> const &left_operand,
00274 Vector<T, 2> const &right_operand)
00275 {
00276 return Vector<T, 2>(
00277 left_operand[SimpleTransform2<T>::R] * right_operand[Dim::X] +
00278 left_operand[SimpleTransform2<T>::X],
00279 left_operand[SimpleTransform2<T>::S] * right_operand[Dim::Y] +
00280 left_operand[SimpleTransform2<T>::Y]);
00281 }
00282
00283
00284
00285
00286
00287
00288
00292 typedef SimpleTransform2<Float> FloatSimpleTransform2;
00296 typedef SimpleTransform2<Sint32> Sint32SimpleTransform2;
00297
00304 void Fprint (FILE *fptr, FloatSimpleTransform2 const &simple_transform);
00305
00306 }
00307
00308 #endif // !defined(_XRB_SIMPLETRANSFORM2_HPP_)
00309