00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GEOM_VEC4_H
00020 #define GEOM_VEC4_H
00021
00022 #include <stdexcept>
00023 #include <cassert>
00024 #include <cstddef>
00025 #include <ostream>
00026
00027 #include <boost/operators.hpp>
00028
00029 #include <ost/geom/module_config.hh>
00030 #include <ost/config.hh>
00031
00032 namespace geom {
00033
00034
00035 class Vec2;
00036 class Vec3;
00037
00038
00039
00040
00041 class DLLEXPORT Vec4:
00042 private boost::equality_comparable<Vec4>,
00043 private boost::additive<Vec4>,
00044 private boost::additive<Vec4, Real>,
00045 private boost::multiplicative<Vec4, Real>
00046 {
00047 public:
00049 Vec4(): x(0), y(0), z(0), w(1) { }
00050
00052 Vec4(Real px, Real py, Real pz, Real pw): x(px), y(py), z(pz), w(pw) { }
00053
00055 Vec4(const Vec4& v): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
00056
00058 Vec4(const Vec2& v);
00059
00061 Vec4(const Vec3& v);
00062
00064 explicit Vec4(const float v[4]): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
00065
00067 explicit Vec4(const double v[4]): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
00069 Vec4& operator=(const Vec4& v)
00070 {
00071 x=v.x;
00072 y=v.y;
00073 z=v.z;
00074 w=v.w;
00075 return *this;
00076 }
00077
00079 bool operator==(const Vec4& rhs) const
00080 {
00081 return x==rhs.x && y==rhs.y && z==rhs.z && w==rhs.w;
00082 }
00083
00085 Real& operator[](std::size_t indx)
00086 {
00087 assert(indx<4);
00088 return (&x)[indx];
00089 }
00090
00092 const Real& operator[](std::size_t indx) const
00093 {
00094 assert(indx<4);
00095 return (&x)[indx];
00096 }
00097
00098 Real& At(size_t indx) {
00099 if (indx>3) {
00100 throw std::out_of_range("index must be smaller than 4");
00101 }
00102 return (&x)[indx];
00103 }
00104
00105 const Real& At(size_t indx) const {
00106 if (indx>3) {
00107 throw std::out_of_range("index must be smaller than 4");
00108 }
00109 return (&x)[indx];
00110 }
00111
00113 Real GetX() const { return x; }
00114 Real GetY() const { return y; }
00115 Real GetZ() const { return z; }
00116 Real GetW() const { return w; }
00117
00118 void SetX(Real v) { x=v; }
00119 void SetY(Real v) { y=v; }
00120 void SetZ(Real v) { z=v; }
00121 void SetW(Real v) { w=v; }
00122
00124 Vec4& operator+=(const Vec4& rhs)
00125 {
00126 x+=rhs.x;
00127 y+=rhs.y;
00128 z+=rhs.z;
00129 w+=rhs.w;
00130 return *this;
00131 }
00132
00133 Vec4& operator+=(Real d)
00134 {
00135 x+=d;
00136 y+=d;
00137 z+=d;
00138 w+=d;
00139 return *this;
00140 }
00141
00143 Vec4& operator-=(const Vec4& rhs)
00144 {
00145 x-=rhs.x;
00146 y-=rhs.y;
00147 z-=rhs.z;
00148 w-=rhs.w;
00149 return *this;
00150 }
00151
00152 Vec4& operator-=(Real d)
00153 {
00154 x-=d;
00155 y-=d;
00156 z-=d;
00157 w-=d;
00158 return *this;
00159 }
00161 Vec4 operator-() const
00162 {
00163 return Vec4(-x, -y, -z, -w);
00164 }
00165
00167 Vec4& operator*=(Real d)
00168 {
00169 x*=d;
00170 y*=d;
00171 z*=d;
00172 w*=d;
00173 return *this;
00174 }
00175
00177 Vec4& operator/=(Real d)
00178 {
00179 Real one_over_d=Real(1.0)/d;
00180 x*=one_over_d;
00181 y*=one_over_d;
00182 z*=one_over_d;
00183 w*=one_over_d;
00184 return *this;
00185 }
00186
00187 Real* Data() {return &x;}
00188 const Real* Data() const {return &x;}
00189
00190 Real x;
00191 Real y;
00192 Real z;
00193 Real w;
00194 };
00195
00196 inline Vec4 operator/(Real d, const Vec4& v)
00197 {
00198 Vec4 nrvo(d/v[0],d/v[1],d/v[2],d/v[3]);
00199 return nrvo;
00200 }
00201
00202 inline std::ostream& operator<<(std::ostream& os, const Vec4& v)
00203 {
00204 os << "(" << v[0] << "," << v[1] << "," << v[2] << "," << v[3] << ")";
00205 return os;
00206 }
00207 }
00208
00209 #include <ost/geom/vec2.hh>
00210 #include <ost/geom/vec3.hh>
00211
00212 namespace geom {
00213
00215 inline Vec4::Vec4(const Vec2& v): x(v.x), y(v.y), z(0), w(1) { }
00216
00218 inline Vec4::Vec4(const Vec3& v): x(v.x), y(v.y), z(v.z), w(1.0) { }
00219
00220 }
00221
00222 # endif