Files
pvvmud/common/lib/utils/vector.C
2025-03-05 08:37:43 +01:00

170 lines
4.1 KiB
C

/*
* PVVMUD a 3D MUD
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "pvvmud.H"
#include <iostream.h>
#include "vector.H"
#include "vertex.H"
CVector::CVector(){
m_vector[0] = 0.0;
m_vector[1] = 0.0;
m_vector[2] = 0.0;
m_vector[3] = 1.0;
}
CVector::CVector(const CVector & vector){
m_vector[0] = vector.m_vector[0];
m_vector[1] = vector.m_vector[1];
m_vector[2] = vector.m_vector[2];
m_vector[3] = vector.m_vector[3];
}
CVector::CVector(double x, double y, double z){
m_vector[0] = x;
m_vector[1] = y;
m_vector[2] = z;
m_vector[3] = 1;
}
CVector::CVector(double x, double y, double z, double d){
m_vector[0] = x;
m_vector[1] = y;
m_vector[2] = z;
m_vector[3] = d;
}
CVector::CVector(const CVertex & vertex){
m_vector[0] = vertex.getX();
m_vector[1] = vertex.getY();
m_vector[2] = vertex.getZ();
m_vector[3] = 1;
}
double CVector::getX() const{
return m_vector[0];
}
double CVector::getY() const{
return m_vector[1];
}
double CVector::getZ() const{
return m_vector[2];
}
// Theory: The cross product is this determinant
//
// | i j k |
// det | m_vector[0] m_vector[1] m_vector[2] |
// | vector.m_vector[0] vector.m_vector[1] vector.m_vector[2] |
CVector CVector::cross( const CVector & vector ){
return CVector(
m_vector[1]*vector.m_vector[2] - m_vector[2]*vector.m_vector[1],
-m_vector[0]*vector.m_vector[2] + m_vector[2]*vector.m_vector[0],
m_vector[0]*vector.m_vector[1] - m_vector[1]*vector.m_vector[0]);
}
CVector& CVector::operator+=( const CVector & vector ){
int i;
for (i = 0; i < 3 ; i ++){
m_vector[i] += vector.m_vector[i];
}
return *this;
}
CVector CVector::operator+( const CVector & vector ) const{
CVector result;
int i;
for (i = 0; i < 3 ; i ++){
result.m_vector[i] = m_vector[i] + vector.m_vector[i];
}
result.m_vector[3] = 1;
return result;
}
CVector CVector::operator*( double scale ) const{
CVector result;
int i;
for (i = 0; i < 3 ; i ++){
result.m_vector[i] = m_vector[i]*scale;
}
result.m_vector[3] = 1;
return result;
}
CVector CVector::operator-( const CVector & vector ) const{
CVector result;
int i;
for (i = 0; i < 3 ; i ++){
result.m_vector[i] = m_vector[i] - vector.m_vector[i];
}
result.m_vector[3] = 1;
return result;
}
void CVector::transform(const CMatrix & transformation){
int ii,jj;
double sum;
//
// This create a compilation error with some compilers!
//
// double * m = ((CMatrix)transformation).getVector();
// CVector temp = *this;
// if (m == temp.m_vector)
// cdebug << "Error in compilation of CVector::transform !!!!\n";
//
CVector temp = *this;
CMatrix * ptrTransformation = (CMatrix*)&transformation;
double * m = ptrTransformation->getVector();
for (ii = 0; ii < 4; ii++){
sum = 0.0;
for (jj = 0; jj < 4; jj ++){
sum += m[ii+jj*4]*temp.m_vector[jj];
}
m_vector[ii] = sum;
}
}
CVector operator*(const CMatrix & m1, const CVector & v1){
CVector result;
double sum, *m = ((CMatrix)m1).getVector();
int ii,jj;
for (ii = 0; ii < 4; ii++){
sum = 0.0;
for (jj = 0; jj < 4; jj ++){
sum += m[ii+jj*4]*v1.m_vector[jj];
}
result.m_vector[ii] = sum;
}
return result;
}
ostream& operator<<(ostream&s,const CVector& v) {
return s << "[" << v.getX() << "," << v.getY() << "," << v.getZ() << "]";
}