ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

matrix4.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 /*
00024  * The following functions: inverseTranslate, inverseRotate, transpose
00025  * are adapted from Portalib3d, which is no longer available on the net.
00026  * But is available through archive.org, for reference:
00027  * http://web.archive.org/web/20050205215104/http://rsn.gamedev.net/pl3d/changelog.html
00028  *
00029  * This code was originally made available under the LGPL license (no version specified).
00030  * Copyright (C)2000, 2001, Brett Porter. All Rights Reserved.
00031  *
00032  * This code (and our modifications) is made available here under the GPLv2 (or later).
00033  *
00034  */
00035 
00036 #include "math/matrix4.h"
00037 #include "math/vector4d.h"
00038 #include "math/squarematrix.h"
00039 
00040 namespace Math {
00041 
00042 Matrix<4, 4>::Matrix() :
00043     MatrixType<4, 4>(), Rotation3D<Matrix4>() {
00044 
00045 }
00046 
00047 Matrix<4, 4>::Matrix(const MatrixBase<4, 4> &m) :
00048     MatrixType<4, 4>(m), Rotation3D<Matrix4>() {
00049 
00050 }
00051 
00052 void Matrix<4, 4>::transform(Vector3d *v, bool trans) const {
00053     Vector4d m;
00054     m(0, 0) = v->x();
00055     m(1, 0) = v->y();
00056     m(2, 0) = v->z();
00057     m(3, 0) = (trans ? 1.f : 0.f);
00058 
00059     m = *this * m;
00060 
00061     v->set(m(0, 0), m(1, 0), m(2, 0));
00062 }
00063 
00064 Vector3d Matrix<4, 4>::getPosition() const {
00065     return Vector3d(getValue(0, 3), getValue(1, 3), getValue(2, 3));
00066 }
00067 
00068 void Matrix<4, 4>::setPosition(const Vector3d &v) {
00069     setValue(0, 3, v.x());
00070     setValue(1, 3, v.y());
00071     setValue(2, 3, v.z());
00072 }
00073 
00074 Matrix3 Matrix<4, 4>::getRotation() const{
00075     Matrix3 m2;
00076     
00077     m2.setValue(0, 0, getValue(0, 0));
00078     m2.setValue(0, 1, getValue(0, 1));
00079     m2.setValue(0, 2, getValue(0, 2));
00080     m2.setValue(1, 0, getValue(1, 0));
00081     m2.setValue(1, 1, getValue(1, 1));
00082     m2.setValue(1, 2, getValue(1, 2));
00083     m2.setValue(2, 0, getValue(2, 0));
00084     m2.setValue(2, 1, getValue(2, 1));
00085     m2.setValue(2, 2, getValue(2, 2));
00086     
00087     return m2;
00088 }
00089 
00090 void Matrix<4, 4>::setRotation(const Matrix3 &m) {
00091     setValue(0, 0, m.getValue(0, 0));
00092     setValue(0, 1, m.getValue(0, 1));
00093     setValue(0, 2, m.getValue(0, 2));
00094     setValue(1, 0, m.getValue(1, 0));
00095     setValue(1, 1, m.getValue(1, 1));
00096     setValue(1, 2, m.getValue(1, 2));
00097     setValue(2, 0, m.getValue(2, 0));
00098     setValue(2, 1, m.getValue(2, 1));
00099     setValue(2, 2, m.getValue(2, 2));
00100 }
00101 
00102 void Matrix<4, 4>::translate(const Vector3d &vec) {
00103     Vector3d v(vec);
00104     transform(&v, false);
00105 
00106     operator()(0, 3) += v.x();
00107     operator()(1, 3) += v.y();
00108     operator()(2, 3) += v.z();
00109 }
00110 
00115 void Matrix<4, 4>::buildFromTargetDir(const Math::Vector3d &modelForward, const Math::Vector3d &targetDirection, 
00116                            const Math::Vector3d &modelUp, const Math::Vector3d &worldUp)
00117 {
00118     Matrix3 rotation;
00119     rotation.buildFromTargetDir(modelForward, targetDirection, modelUp, worldUp);
00120     this->setRotation(rotation);
00121 }
00122 
00123 void Matrix<4, 4>::invertAffineOrthonormal() {
00124     Matrix3 rotation(getRotation());
00125     rotation.transpose();
00126     
00127     Vector3d position(getPosition().getNegative());
00128     
00129     rotation.transformVector(&position);
00130     setRotation(rotation);
00131     setPosition(position);
00132 }
00133 
00134 void Matrix<4, 4>::inverseTranslate(Vector3d *v) const {
00135     v->x() = v->x() - getValue(0, 3);
00136     v->y() = v->y() - getValue(1, 3);
00137     v->z() = v->z() - getValue(2, 3);
00138 }
00139 
00140 void Matrix<4, 4>::inverseRotate(Vector3d *v) const {
00141     Vector3d temp;
00142     
00143     temp.x() = v->x() * getValue(0, 0) + v->y() * getValue(1, 0) + v->z() * getValue(2, 0);
00144     temp.y() = v->x() * getValue(0, 1) + v->y() * getValue(1, 1) + v->z() * getValue(2, 1);
00145     temp.z() = v->x() * getValue(0, 2) + v->y() * getValue(1, 2) + v->z() * getValue(2, 2);
00146     
00147     *v = temp;
00148 }
00149 
00150 void swap (float &a, float &b);
00151 
00152     
00153 void Matrix<4, 4>::transpose() {
00154     swap(operator ()(0,1), operator ()(1,0));
00155     swap(operator ()(0,2), operator ()(2,0));
00156     swap(operator ()(0,3), operator ()(3,0));
00157 
00158     swap(operator ()(1,2), operator ()(2,1));
00159     swap(operator ()(1,3), operator ()(3,1));
00160     swap(operator ()(2,3), operator ()(3,2));
00161 }
00162 
00163 } // end of namespace Math
00164 


Generated on Sat Apr 20 2019 05:03:12 for ResidualVM by doxygen 1.7.1
curved edge   curved edge