1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
* Modification History
*
* 2001-January-16 Jason Rohrer
* Created.
* Added a missing getMatrix() function.
*
* 2001-February-3 Jason Rohrer
* Updated serialization code to use new interfaces.
*/
#ifndef TRANSFORM_3D_INCLUDED
#define TRANSFORM_3D_INCLUDED
#include <stdio.h>
#include <math.h>
#include "minorGems/io/Serializable.h"
#include "Vector3D.h"
#include "Angle3D.h"
/**
* An affine transformation in 3D.
*
* @author Jason Rohrer
*/
class Transform3D : public Serializable {
public:
/**
* Creates a new identity transform.
*/
Transform3D();
/**
* Creates a new transform by copying another transform.
*
* @param inOther the transform to copy.
*/
Transform3D( Transform3D *inOther );
/**
* Adds a rotation to the end of this transform. Note that the
* rotations specified by inAngle are applied in the following order:
* rotX, rotY, rotZ
*
* @param inAngle angle rotation to add. Must be destructed by caller.
*/
void rotate( Angle3D *inAngle );
/**
* Adds a scaling operation to the end of this transform.
*
* @param inScale the uniform scale factor.
*/
void scale( double inScale );
/**
* Adds a scaling operation to the end of this transform.
*
* @param inScaleX the x direction scale factor.
* @param inScaleY the y direction scale factor.
* @param inScaleZ the z direction scale factor.
*/
void scale( double inScaleX, double inScaleY, double inScaleZ );
/**
* Adds a translation to the end of this transform.
*
* @param inTranslation angle rotation to add.
* Must be destructed by caller.
*/
void translate( Vector3D *inTranslation );
/**
* Adds a transformation to the end of this transformation.
*
* @param inTransform the transform to add. inTransform is no
* modified by this call, and must be destroyed by the caller.
*/
void transform( Transform3D *inTransform );
/**
* Transforms a vector using the built-up transformation.
*
* @param inTarget the vector to transform. The object passed
* in is modified directly, and must be destroyed by the caller.
*/
void apply( Vector3D *inTarget );
/**
* Transforms a vector using the built-up transformation, but skips
* the translation part of the transform. This is useful when
* applying the transform to normals, when translations will screw
* them up.
*
* @param inTarget the vector to transform. The object passed
* in is modified directly, and must be destroyed by the caller.
*/
void applyNoTranslation( Vector3D *inTarget );
/**
* Gets the transformation matrix underlying this transform.
*
* @return the transformation matrix.
*/
double *getMatrix();
/**
* Prints the transformation matrix to standard out.
*/
void print();
// implement the Serializable interface
virtual int serialize( OutputStream *inOutputStream );
virtual int deserialize( InputStream *inInputStream );
private:
double mMatrix[4][4];
/**
* Multiplies mMatrix by inMatrix, i.e., mMatrix = inMatrix * mMatrix.
*
* Note that this reversed multiplication order works for concatonating
* transformation matrices when we're using column vectors for our 3D
* points. Thus, if after the above operation, we compute:
* point = mMatrix * point,
* we will be transformping point first by the original
* mMatrix and then by inMatrix.
*
* @param inMatrix the matrix to multiply mMatrix by.
*/
void multiply( double inMatrix[][4] );
/**
* Multiplies inMatrixA by inMatrixB, i.e.,
* inMatrixA = inMatrixA * inMatrixB.
*
* @param inMatrixA the first matrix in the multiplication, and
* the destination of the result.
* @param inMatrixB the second matrix in the multiplication.
*/
//void multiply( double[4][4] inMatrixA, double[4][4] inMatrixB );
};
inline Transform3D::Transform3D() {
double tempM[4][4] = { { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } };
memcpy( mMatrix, tempM, 16 * sizeof( double ) );
}
inline Transform3D::Transform3D( Transform3D *inOther ) {
memcpy( mMatrix, inOther->getMatrix(), 16 * sizeof( double ) );
}
inline void Transform3D::rotate( Angle3D *inAngle ) {
double aX = inAngle->mX;
double rotX[4][4] = { { 1, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 } };
rotX[1][1] = cos( aX );
rotX[1][2] = -sin( aX );
rotX[2][1] = sin( aX );
rotX[2][2] = cos( aX );
double aY = inAngle->mY;
double rotY[4][4] = { { 0, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 } };
rotY[0][0] = cos( aY );
rotY[0][2] = sin( aY );
rotY[2][0] = -sin( aY );
rotY[2][2] = cos( aY );
double aZ = inAngle->mZ;
double rotZ[4][4] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } };
rotZ[0][0] = cos( aZ );
rotZ[0][1] = -sin( aZ );
rotZ[1][0] = sin( aZ );
rotZ[1][1] = cos( aZ );
multiply( rotX );
multiply( rotY );
multiply( rotZ );
}
inline void Transform3D::scale( double inScale ) {
scale( inScale, inScale, inScale );
}
inline void Transform3D::scale( double inScaleX,
double inScaleY, double inScaleZ ) {
double scaleM[4][4] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 } };
scaleM[0][0] = inScaleX;
scaleM[1][1] = inScaleY;
scaleM[2][2] = inScaleZ;
multiply( scaleM );
}
inline void Transform3D::translate( Vector3D *inTranslation ) {
double translateM[4][4] = { { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } };
translateM[0][3] = inTranslation->mX;
translateM[1][3] = inTranslation->mY;
translateM[2][3] = inTranslation->mZ;
multiply( translateM );
}
inline void Transform3D::transform( Transform3D *inTransform ) {
double tempM[4][4];
memcpy( tempM, inTransform->getMatrix(), 16 * sizeof( double ) );
multiply( tempM );
}
inline void Transform3D::apply( Vector3D *inTarget ) {
double x = inTarget->mX;
double y = inTarget->mY;
double z = inTarget->mZ;
inTarget->mX = mMatrix[0][0] * x + mMatrix[0][1] * y + mMatrix[0][2] * z
+ mMatrix[0][3];
inTarget->mY = mMatrix[1][0] * x + mMatrix[1][1] * y + mMatrix[1][2] * z
+ mMatrix[1][3];
inTarget->mZ = mMatrix[2][0] * x + mMatrix[2][1] * y + mMatrix[2][2] * z
+ mMatrix[2][3];
}
inline void Transform3D::applyNoTranslation( Vector3D *inTarget ) {
double x = inTarget->mX;
double y = inTarget->mY;
double z = inTarget->mZ;
inTarget->mX = mMatrix[0][0] * x + mMatrix[0][1] * y + mMatrix[0][2] * z;
inTarget->mY = mMatrix[1][0] * x + mMatrix[1][1] * y + mMatrix[1][2] * z;
inTarget->mZ = mMatrix[2][0] * x + mMatrix[2][1] * y + mMatrix[2][2] * z;
}
inline void Transform3D::multiply( double inMatrix[][4] ) {
double destM[4][4];
for( int dY=0; dY<4; dY++ ) {
for( int dX=0; dX<4; dX++ ) {
// take a row of inMatrix corresponding to dY
// take a column of mMatrix corresponding to dX
destM[dY][dX] = 0;
for( int i=0; i<4; i++ ) {
destM[dY][dX] += inMatrix[dY][i] * mMatrix[i][dX];
}
}
}
memcpy( mMatrix, destM, 16 * sizeof( double ) );
}
inline double *Transform3D::getMatrix() {
return &( mMatrix[0][0] );
}
inline void Transform3D::print() {
for( int y=0; y<4; y++ ) {
for( int x=0; x<4; x++ ) {
printf( "%f ", mMatrix[y][x] );
}
printf( "\n" );
}
}
inline int Transform3D::serialize( OutputStream *inOutputStream ) {
int numBytes = 0;
for( int y=0; y<4; y++ ) {
for( int x=0; x<4; x++ ) {
numBytes += inOutputStream->writeDouble( mMatrix[y][x] );
}
}
return numBytes;
}
inline int Transform3D::deserialize( InputStream *inInputStream ) {
int numBytes = 0;
for( int y=0; y<4; y++ ) {
for( int x=0; x<4; x++ ) {
numBytes += inInputStream->readDouble( &( mMatrix[y][x] ) );
}
}
return numBytes;
}
#endif
|