/* vector.h -*-C++-*- * by Will Wagner * last updated 14 Sep 1997, 22:25:45 wwagner * * This file contains the pov_vector template class, which is a * derivative of the STL vector class. * * Changes * 04 Aug 1997 * - Added this comment block. * * 14 Sep 1997 * - Added the operator== and operator< routines, since they are * not actually members of the base class (we need them in * Parser::expr_parse). * * Things to do * */ #ifndef __INC_POV_VECTOR_H__ #define __INC_POV_VECTOR_H__ #include #include #include template class pov_vector : public vector { public: pov_vector(void) : vector() { }; pov_vector(int i, T v) : vector(i, v) { }; /* The vector-scalar routines */ inline pov_vector operator*(T num) const { pov_vector nv; transform(this->begin(), this->end(), nv.begin(), bind2nd(times(), num)); return nv; }; inline pov_vector operator/(T num) const { pov_vector nv; transform(this->begin(), this->end(), nv.begin(), bind2nd(divides(), (num ? num : 1))); return nv; }; inline const pov_vector& operator*=(T num) { transform(this->begin(), this->end(), this->begin(), bind2nd(times(), num)); return *this; }; inline const pov_vector& operator/=(T num) { transform(this->begin(), this->end(), this->begin(), bind2nd(divides(), (num ? num : 1))); return *this; }; /* The vector-vector routines */ inline pov_vector operator+(const pov_vector& second) const { pov_vector nv = *this; transform(this->begin(), this->end(), second.begin(), nv.begin(), plus()); return nv; }; inline pov_vector operator-(const pov_vector& second) const { pov_vector nv = *this; transform(this->begin(), this->end(), second.begin(), nv.begin(), minus()); return nv; }; inline pov_vector operator*(const pov_vector& second) const { pov_vector nv = *this; transform(this->begin(), this->end(), second.begin(), nv.begin(), times()); return nv; }; inline pov_vector operator/(const pov_vector& second) const { pov_vector nv = *this; transform(this->begin(), this->end(), second.begin(), nv.begin(), divides()); return nv; }; inline const pov_vector& operator+=(const pov_vector& second) { transform(this->begin(), this->end(), second.begin(), this->begin(), plus()); return *this; }; inline const pov_vector& operator-=(const pov_vector& second) { transform(this->begin(), this->end(), second.begin(), this->begin(), minus()); return *this; }; inline const pov_vector& operator*=(const pov_vector& second) { transform(this->begin(), this->end(), second.begin(), this->begin(), times()); return *this; }; inline const pov_vector& operator/=(const pov_vector& second) { transform(this->begin(), this->end(), second.begin(), this->begin(), divides()); return *this; }; inline pov_vector Cross(pov_vector& second) { pov_vector tmp = *this; tmp[0] = (*this)[1] * second[2] - (*this)[2] * second[1]; tmp[1] = (*this)[2] * second[0] - (*this)[0] * second[2]; tmp[2] = (*this)[0] * second[1] - (*this)[1] * second[0]; return tmp; }; inline T Dot(const pov_vector& second) const { pov_vector tmp = *this * second; return accumulate(tmp.begin(), tmp.end(), 0.0); }; inline T Length(void) { return sqrt(this->SumSquares()); }; inline T SumSquares(void) { return this->Dot(*this); }; inline const pov_vector& Normalize(void) { *this /= this->Length(); return *this; }; inline pov_vector Normalized(void) { pov_vector nv = *this; nv.Normalize(); return nv; }; inline const pov_vector& Half(const pov_vector& second) { *this += second; *this *= 0.5; return *this; }; inline pov_vector Halved(const pov_vector& second) { pov_vector nv = *this; nv += second; nv *= 0.5; return nv; }; }; template inline pov_vector operator*(T num, const pov_vector& v) { return v * num; }; /* The comparison routines */ template inline bool operator==(const pov_vector& i, const pov_vector& j) { return operator==((const vector&)i, (const vector&)j); }; template inline bool operator<(const pov_vector& i, const pov_vector& j) { return operator<((const vector&)i, (const vector&)j); }; #endif /* __INC_POV_VECTOR_H__ */