vec3.h

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2000-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Tue Nov 28 18:27:19 2000
00010 // commit: $Id: vec3.h 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/geom/vec3.h $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_GEOM_VEC3_H_UTC20050626084023_DEFINED
00035 #define GROOVX_GEOM_VEC3_H_UTC20050626084023_DEFINED
00036 
00037 #include "geom/vec2.h"
00038 
00039 #include "rutz/debug.h"
00040 GVX_DBG_REGISTER
00041 
00042 namespace geom
00043 {
00044   template <class V> class vec3;
00045 
00047   template <class V>
00048   class vec3
00049   {
00050   private:
00051     V m_d[3];
00052 
00053   public:
00054     vec3() { m_d[0] = m_d[1] = m_d[2] = V(); }
00055 
00056     vec3(V x_, V y_, V z_) { set(x_, y_, z_); }
00057 
00058     explicit vec3(const V* arr) { set(arr[0], arr[1], arr[2]); }
00059 
00060     template <class U>
00061     explicit vec3(const vec3<U>& other)
00062     { set(V(other.x()), V(other.y()), V(other.z())); }
00063 
00064     static vec3 zeros() { return vec3(V(0), V(0), V(0)); }
00065     static vec3 ones()  { return vec3(V(1), V(1), V(1)); }
00066 
00067     static vec3<V> unit_x() { return vec3<V>(1.0, 0.0, 0.0); }
00068     static vec3<V> unit_y() { return vec3<V>(0.0, 1.0, 0.0); }
00069     static vec3<V> unit_z() { return vec3<V>(0.0, 0.0, 1.0); }
00070 
00071     V& x()       { return m_d[0]; }
00072     const V& x() const { return m_d[0]; }
00073 
00074     V& y()       { return m_d[1]; }
00075     const V& y() const { return m_d[1]; }
00076 
00077     V& z()       { return m_d[2]; }
00078     const V& z() const { return m_d[2]; }
00079 
00080     void get(V& x_, V& y_, V& z_) const { x_ = x(); y_ = y(); z_ = z(); }
00081     void set(V x_, V y_, V z_)          { x() = x_; y() = y_; z() = z_; }
00082 
00083     V* data()       { return &m_d[0]; }
00084     const V* data() const { return &m_d[0]; }
00085 
00086     vec2<V> as_vec2() const { return vec2<V>(x(), y()); }
00087 
00088     double length() const
00089     { return sqrt(x()*x() + y()*y() + z()*z()); }
00090 
00091     //
00092     // vec3-scalar math
00093     //
00094 
00095     template <class U>
00096     void scale_by(const U& factor)
00097     { x() *= factor; y() *= factor; z() *= factor; }
00098 
00099     template <class U>
00100     vec3& operator*=(const U& factor) { scale_by(factor); return *this; }
00101 
00102     template <class U>
00103     vec3& operator/=(const U& factor) { scale_by(1.0/factor); return *this; }
00104 
00105     vec3 operator*(const V& factor) const
00106     { vec3<V> copy(*this); copy *= factor; return copy; }
00107 
00108     vec3 operator/(const V& factor) const
00109     { vec3<V> copy(*this); copy /= factor; return copy; }
00110 
00111 
00112     //
00113     // vec3-vec2 math (fill in z==0 for addition)
00114     //
00115 
00116     vec3 operator+(const vec2<V>& rhs) const
00117     { return vec3<V>(x() + rhs.x(), y() + rhs.y(), z()); }
00118 
00119     vec3 operator-(const vec2<V>& rhs) const
00120     { return vec3<V>(x() - rhs.x(), y() - rhs.y(), z()); }
00121 
00122 
00123     //
00124     // vec3-vec3 math
00125     //
00126 
00127     vec3 operator+(const vec3<V>& rhs) const
00128     { return vec3<V>(x() + rhs.x(), y() + rhs.y(), z() + rhs.z()); }
00129 
00130     vec3 operator-(const vec3<V>& rhs) const
00131     { return vec3<V>(x() - rhs.x(), y() - rhs.y(), z() - rhs.z()); }
00132 
00133     vec3 operator*(const vec3<V>& rhs) const
00134     { return vec3<V>(x() * rhs.x(), y() * rhs.y(), z() * rhs.z()); }
00135 
00136     vec3 operator/(const vec3<V>& rhs) const
00137     { return vec3<V>(x() / rhs.x(), y() / rhs.y(), z() / rhs.z()); }
00138 
00139 
00140     //
00141     // debugging
00142     //
00143 
00144     void debug_dump() const throw()
00145     {
00146       dbg_eval(0, x());
00147       dbg_eval(0, y());
00148       dbg_eval_nl(0, z());
00149     }
00150   };
00151 
00152   typedef vec3<int> vec3i;
00153   typedef vec3<float> vec3f;
00154   typedef vec3<double> vec3d;
00155 
00156 } // end namespace geom
00157 
00158 static const char __attribute__((used)) vcid_groovx_geom_vec3_h_utc20050626084023[] = "$Id: vec3.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00159 #endif // !GROOVX_GEOM_VEC3_H_UTC20050626084023_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:38 2008 by Doxygen version 1.5.5.