Source: raytracer/vector.h


Annotated List
Files
Globals
Hierarchy
Index
/* *************************************************************************
                            vector.h  -  description
                             -------------------
    begin                : Wed Oct 16 2002  
    copyright            : (C) 2002 by Micha Riser
    email                : mriser@gmx.net
 ************************************************************************* */

/* *************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ************************************************************************* */

#ifndef VECTOR_H
#define VECTOR_H

#include<cmath>
#include "types.h"
#include "matrix.h"

class Matrix;

/**Class for describing points, normal vectors, direction vectors in 3D space.
  *@author Micha Riser
  */
class Vector3 {

public: // Static methods

    /**Returns vector describing (positive) infinit point.
      */
    static Vector3 positiveInfinit() {return Vector3( DBL_INFINITY,DBL_INFINITY,DBL_INFINITY );}
    
    /**Returns vector describing (negative) infinit point.
      */    
    static Vector3 negativeInfinit() {return Vector3( -DBL_INFINITY,-DBL_INFINITY,-DBL_INFINITY );}

public: // Public members
    DBL a[3];

public: // Constructors and destructor

    /**Create uninitialized vector
      */
    Vector3() {}

    /**Create vector with same coifficients.
      *@param w value for all coifficients.
      */
    Vector3(DBL w) {for(int i=0; i<3; i++) a[i]=w;}

    /**Create vector from given coifficients.
      */
    Vector3(DBL x, DBL y, DBL z) {
        a[0]=x; a[1]=y; a[2]=z;
    }

public: // Public methods
    /**Add vector.
      */
    void add(const Vector3 &v) {for(int i=0; i<3; i++) a[i]+=v.a[i];}
    /**Subtract vector.
      */
    void sub(const Vector3 &v) {for(int i=0; i<3; i++) a[i]-=v.a[i];}
    /**Scale vecotr by scalar.
      */
    void scale(DBL s) {for(int i=0; i<3; i++) a[i]*=s;}
    /**Normalize vector. Scale vector so that its length is 1.
      */
    void normalize() {scale(1/length());}
    /**Inverse vector componentwise. Calculates 1/a for all coifficients.
      */
    void inverse() {for(int i=0; i<3; i++) a[i]=1/a[i];}
    /**Calculate v -> M*v.
      *@param m matrix M
      */
    void applyFromLeft(const Matrix &m);
    /**Calculate v -> M^T * v.
      *@param m matrix M
      */
    void applyFromLeftTransposed(const Matrix &m);
    /**Minimize vector components.
      */
    void minimize(const Vector3 &v) {for(int i=0; i<3; i++) if (v.a[i]<a[i]) a[i]=v.a[i];}
    /**Maximize vector components.
      */
    void maximize(const Vector3 &v) {for(int i=0; i<3; i++) if (v.a[i]>a[i]) a[i]=v.a[i];}  
    /**
      *@returns vector scaled by scalar s
      */
    Vector3 scaled(DBL s) const {
        Vector3 ret;
        for(int i=0; i<3; i++) ret.a[i] = a[i]*s;
        return ret;
    }
    /**
      *@returns length in square
      */
    double lengthSq() const {return a[0]*a[0]+a[1]*a[1]+a[2]*a[2];}
    /**
      *@returns length of vector
      */
    double length() const {return sqrt(lengthSq());}
    /**Compare vector to other vector
      */
    bool equals(const Vector3& v) const {
        for(int i=0; i<3; i++)
            if(a[i]!=v.a[i]) return false;
        return true;
    }

    /**Introduce order between vectors.
      */
    bool operator<(const Vector3& v) const {
        for(int i=0; i<3; i++) {
            if (a[i]<v.a[i])
                return true;
            else
                if (a[i]>v.a[i]) return false;
        }
        return false;
    }

public: // Static methods:

    /**Calculate dot product of two vectors.
      */
    static DBL dotProd(const Vector3& v, const Vector3 &w) {
        DBL ret = 0;
        for(int i=0; i<3; i++) ret+=v.a[i]*w.a[i];
        return ret;
    }
    /**Add two vectors.
      */
    static Vector3 add(const Vector3& v, const Vector3 &w) {
        Vector3 ret;
        for(int i=0; i<3; i++) ret.a[i] = v.a[i]+w.a[i];
        return ret;
    }
    /**Subtract two vectors.
      */
    static Vector3 sub(const Vector3& v, const Vector3 &w) {
        Vector3 ret;
        for(int i=0; i<3; i++) ret.a[i] = v.a[i]-w.a[i];
        return ret;
    }

};



#endif

Generated by: micha@laptop on Thu Oct 24 20:25:24 2002, using kdoc 2.0a53.