/* * Math3d - The 3D Computer Graphics Math Library * Copyright (C) 1996-2000 by J.E. Hoffmann * All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: m3d.cpp,v 1.1 2002/11/18 08:23:01 rst Exp $ */ #define _MATH3D_EXPORT #include "m2d.h" #include "m3d.h" #include "m4d.h" #include #include /*! * Construction from cartesian coordinates * ([X, Y] -> [X, Y, 1]). * * \param A 2-dimensional vector. */ Math3d::M3d::M3d(const M2d& A) { d_v[0]=A.d_v[0]; d_v[1]=A.d_v[1]; d_v[2]=1.0; } /*! * */ Math3d::M3d::M3d(const M3d& A) { d_v[0]=A.d_v[0]; d_v[1]=A.d_v[1]; d_v[2]=A.d_v[2]; } /*! * Construction from homogenous coordinates * ([W*X, W*Y, W*Z, W] with scale factor W !=0 -> [X, Y, Z]). * * \param A 4-dimensional vector. */ Math3d::M3d::M3d(const M4d& A) { if (fabs(A.d_v[3])=0 && i<3); return(d_v[i]); } /*! * */ Math3d::M3d Math3d::M3d::operator+(const M3d& A) { return(M3d( d_v[0]+A.d_v[0], d_v[1]+A.d_v[1], d_v[2]+A.d_v[2] )); } /*! * */ Math3d::M3d Math3d::M3d::operator+() { return(*this); } /*! * */ const Math3d::M3d& Math3d::M3d::operator+=(const M3d& A) { d_v[0]+=A.d_v[0]; d_v[1]+=A.d_v[1]; d_v[2]+=A.d_v[2]; return(*this); } /*! * */ Math3d::M3d Math3d::M3d::operator-(const M3d& A) { return(M3d( d_v[0]-A.d_v[0], d_v[1]-A.d_v[1], d_v[2]-A.d_v[2] )); } /*! * */ Math3d::M3d Math3d::M3d::operator-() { return(M3d(-d_v[0],-d_v[1],-d_v[2])); } /*! * */ const Math3d::M3d& Math3d::M3d::operator-=(const M3d& A) { d_v[0]-=A.d_v[0]; d_v[1]-=A.d_v[1]; d_v[2]-=A.d_v[2]; return(*this); } /*! * */ Math3d::M3d Math3d::M3d::operator*(double k) { return(M3d( d_v[0]*k, d_v[1]*k, d_v[2]*k )); } /*! * */ const Math3d::M3d& Math3d::M3d::operator*=(double k) { d_v[0]*=k; d_v[1]*=k; d_v[2]*=k; return(*this); } /*! * */ void Math3d::M3d::neg() { d_v[0]=-d_v[0]; d_v[1]=-d_v[1]; d_v[2]=-d_v[2]; } /*! * */ void Math3d::M3d::abs() { d_v[0]=fabs(d_v[0]); d_v[1]=fabs(d_v[1]); d_v[2]=fabs(d_v[2]); } /*! * */ void Math3d::M3d::add(const M3d& A, const M3d& B) { d_v[0]=A.d_v[0] + B.d_v[0]; d_v[1]=A.d_v[1] + B.d_v[1]; d_v[2]=A.d_v[2] + B.d_v[2]; } /*! * */ void Math3d::M3d::sub(const M3d& A, const M3d& B) { d_v[0]=A.d_v[0] - B.d_v[0]; d_v[1]=A.d_v[1] - B.d_v[1]; d_v[2]=A.d_v[2] - B.d_v[2]; } /*! * */ void Math3d::M3d::scalar(double k) { d_v[0]*=k; d_v[1]*=k; d_v[2]*=k; } /*! * */ void Math3d::M3d::normalize() { double l,c; l=length(); if (fabs(l)d_v[0]) d_v[0]=m.d_v[0]; if (m.d_v[1]>d_v[1]) d_v[1]=m.d_v[1]; if (m.d_v[2]>d_v[2]) d_v[2]=m.d_v[2]; } /*! * */ void Math3d::M3d::cubic(const M3d& A, const M3d& TA, const M3d& TB, const M3d& B, double t) { double a,b,c,d; a=2*t*t*t - 3*t*t + 1; b=-2*t*t*t + 3*t*t; c=t*t*t - 2*t*t + t; d=t*t*t - t*t; d_v[0]=a*A.d_v[0] + b*B.d_v[0] + c*TA.d_v[0] + d*TB.d_v[0]; d_v[1]=a*A.d_v[1] + b*B.d_v[1] + c*TA.d_v[1] + d*TB.d_v[1]; d_v[2]=a*A.d_v[2] + b*B.d_v[2] + c*TA.d_v[2] + d*TB.d_v[2]; } /*! * */ double Math3d::M3d::get(int i) const { ASSERT(i>=0 && i<3); return(d_v[i]); } /*! * */ double Math3d::M3d::operator*(const M3d& A) const { return(d_v[0]*A.d_v[0] + d_v[1]*A.d_v[1] + d_v[2]*A.d_v[2]); } /*! * */ bool Math3d::M3d::operator==(const M3d& A) const { return(cmp(A)); } /*! * */ bool Math3d::M3d::operator!=(const M3d& A) const { return(!cmp(A)); } /*! * */ double Math3d::M3d::dot(const M3d& A) const { return(d_v[0]*A.d_v[0] + d_v[1]*A.d_v[1] + d_v[2]*A.d_v[2]); } /*! * */ bool Math3d::M3d::cmp(const M3d& A, double epsilon) const { return( (fabs(d_v[0]-A.d_v[0])