00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GEOM_VEC3_H
00020 #define GEOM_VEC3_H
00021
00022 #include <stdexcept>
00023 #include <cassert>
00024 #include <cstddef>
00025 #include <ostream>
00026 #include <vector>
00027 #include <boost/operators.hpp>
00028
00029
00030 #include <ost/config.hh>
00031 #include <ost/geom/module_config.hh>
00032 #include <ost/geom/exc.hh>
00033 namespace geom {
00034
00035
00036 class Vec2;
00037 class Vec4;
00038 class Line3;
00039 class Plane;
00040
00042 class DLLEXPORT_OST_GEOM Vec3:
00043 private boost::equality_comparable<Vec3>,
00044 private boost::additive<Vec3>,
00045 private boost::additive<Vec3, Real>,
00046 private boost::multiplicative<Vec3, Real>
00047 {
00048 public:
00050 Vec3(): x(0), y(0), z(0) {}
00051
00053 Vec3(Real px, Real py, Real pz): x(px), y(py), z(pz) {}
00054
00056 Vec3(const Vec3& v): x(v.x), y(v.y), z(v.z) { }
00057
00059 Vec3(const Vec2& v);
00060
00062
00066 explicit Vec3(const Vec4& v);
00067
00068 explicit Vec3(Real v): x(v), y(v), z(v) { }
00069
00071 explicit Vec3(const double v[3]): x(v[0]), y(v[1]), z(v[2]) { }
00072
00074 explicit Vec3(const float v[3]): x(v[0]), y(v[1]), z(v[2]) { }
00075
00077 Vec3& operator=(const Vec3& v)
00078 {
00079 x=v.x;
00080 y=v.y;
00081 z=v.z;
00082 return *this;
00083 }
00084
00086 bool operator==(const Vec3& rhs) const
00087 {
00088 return x==rhs.x && y==rhs.y && z==rhs.z;
00089 }
00090
00092 Real& operator[](std::size_t indx)
00093 {
00094 assert(indx<3);
00095 return (&x)[indx];
00096 }
00097
00099 const Real& operator[](std::size_t indx) const
00100 {
00101 assert(indx<3);
00102 return (&x)[indx];
00103 }
00104
00105 Real& At(size_t indx) {
00106 if (indx>2) {
00107 throw std::out_of_range("index must be smaller than 3");
00108 }
00109 return (&x)[indx];
00110 }
00111
00112 const Real& At(size_t indx) const {
00113 if (indx>2) {
00114 throw std::out_of_range("index must be smaller than 3");
00115 }
00116 return (&x)[indx];
00117 }
00119 Real GetX() const { return x; }
00120 Real GetY() const { return y; }
00121 Real GetZ() const { return z; }
00122 void SetX(Real v) { x=v; }
00123 void SetY(Real v) { y=v; }
00124 void SetZ(Real v) { z=v; }
00125
00127 Vec3& operator+=(const Vec3& rhs)
00128 {
00129 x+=rhs.x;
00130 y+=rhs.y;
00131 z+=rhs.z;
00132 return *this;
00133 }
00134
00135 Vec3& operator+=(Real d)
00136 {
00137 x+=d;
00138 y+=d;
00139 z+=d;
00140 return *this;
00141 }
00142
00144 Vec3& operator-=(const Vec3& rhs)
00145 {
00146 x-=rhs.x;
00147 y-=rhs.y;
00148 z-=rhs.z;
00149 return *this;
00150 }
00151
00152 Vec3& operator-=(Real d)
00153 {
00154 x-=d;
00155 y-=d;
00156 z-=d;
00157 return *this;
00158 }
00160 Vec3 operator-() const
00161 {
00162 return Vec3(-x, -y, -z);
00163 }
00164
00166 Vec3& operator*=(Real d)
00167 {
00168 x*=d;
00169 y*=d;
00170 z*=d;
00171 return *this;
00172 }
00173
00175 Vec3& operator/=(Real d)
00176 {
00177 Real one_over_d=Real(1.0)/d;
00178 x*=one_over_d;
00179 y*=one_over_d;
00180 z*=one_over_d;
00181 return *this;
00182 }
00183
00184 Real* Data() {return &x;}
00185 const Real* Data() const {return &x;}
00186
00187 Real x;
00188 Real y;
00189 Real z;
00190 };
00191
00192 inline Vec3 operator/(Real d, const Vec3& v)
00193 {
00194 Vec3 nrvo(d/v[0],d/v[1],d/v[2]);
00195 return nrvo;
00196 }
00197
00198
00199
00200
00201
00202 inline std::ostream& operator<<(std::ostream& os, const Vec3& v)
00203 {
00204 os << "[" << v.x << ", " << v.y << ", " << v.z << "]";
00205 return os;
00206 }
00207 }
00208
00209 namespace geom {
00210
00211
00212 class Mat3;
00213
00214 class DLLEXPORT_OST_GEOM Vec3List :
00215 public std::vector<Vec3>,
00216 private boost::equality_comparable<Vec3List>,
00217 private boost::additive<Vec3List>,
00218 private boost::additive<Vec3List, Real>,
00219 private boost::multiplicative<Vec3List, Real>
00220 {
00221 public:
00222 typedef std::vector<Vec3> base_type;
00223 Vec3List() : base_type() {}
00224
00225 Vec3List(size_t size, const Vec3& value=Vec3()) : base_type(size, value) {}
00226 Vec3List(base_type::iterator b, base_type::iterator e): base_type(b, e) { }
00227
00228 Vec3List(const Vec3List& rhs) : base_type(rhs) { }
00229 Vec3List(const base_type& rhs) : base_type(rhs) { }
00230 Vec3List& operator=(const Vec3List& rhs)
00231 {
00232 base_type::operator=(rhs);
00233 return *this;
00234 }
00236 bool operator==(const Vec3List& rhs) const
00237 {
00238 if (this->size()!=rhs.size()){
00239 throw std::length_error("Vec3List must have the same size");
00240 }
00241 for (unsigned int i=0;i!=this->size();++i) {
00242 if (((*this)[i])!=((rhs)[i])){
00243 return false;
00244 }
00245 }
00246 return true;
00247 }
00249 Vec3List& operator+=(const Vec3List& rhs)
00250 {
00251 if (this->size()!=rhs.size()){
00252 throw std::length_error("Vec3List must have the same size");
00253 }
00254 for (unsigned int i=0;i!=this->size();++i) {
00255 (*this)[i]+=(rhs)[i];
00256 }
00257 return *this;
00258 }
00259 Vec3List& operator+=(Real d)
00260 {
00261 for (unsigned int i=0;i!=this->size();++i) {
00262 (*this)[i]+=d;
00263 }
00264 return *this;
00265 }
00266
00268 Vec3List& operator-=(const Vec3List& rhs)
00269 {
00270 if (this->size()!=rhs.size()){
00271 throw std::length_error("Vec3List must have the same size");
00272 }
00273 for (unsigned int i=0;i!=this->size();++i) {
00274 (*this)[i]-=(rhs)[i];
00275 }
00276 return *this;
00277 }
00278
00279 Vec3List& operator-=(Real d)
00280 {
00281 for (unsigned int i=0;i!=this->size();++i) {
00282 (*this)[i]-=d;
00283 }
00284 return *this;
00285 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00298 Vec3List& operator*=(Real d)
00299 {
00300 for (unsigned int i=0;i!=this->size();++i) {
00301 (*this)[i]*=d;
00302 }
00303 return *this;
00304 }
00305
00307 Vec3List& operator/=(Real d)
00308 {
00309 for (unsigned int i=0;i!=this->size();++i) {
00310 (*this)[i]/=d;
00311 }
00312 return *this;
00313 }
00314
00315
00316 Mat3 GetInertia() const;
00317 Vec3 GetCenter() const;
00318 Mat3 GetPrincipalAxes() const;
00319 Line3 GetODRLine() const;
00320 Plane GetODRPlane() const;
00321
00322
00323
00324
00325
00326
00327
00328
00329 std::pair<Line3, Real> FitCylinder(const Vec3& initial_direction) const;
00330 };
00331 }
00332
00333
00334 #include <ost/geom/vec2.hh>
00335 #include <ost/geom/vec4.hh>
00336 #include <ost/geom/mat3.hh>
00337 #include <ost/geom/composite3.hh>
00338
00339 namespace geom {
00340 inline Vec3::Vec3(const Vec2& v): x(v.x), y(v.y), z(0.0) { }
00341
00342 inline Vec3::Vec3(const Vec4& v): x(v.x), y(v.y), z(v.z)
00343 {
00344 if (std::fabs(v.w)<1e-10) {
00345
00346
00347
00348
00349 } else {
00350 x/=v.w;
00351 y/=v.w;
00352 z/=v.w;
00353 }
00354 }
00355 }
00356
00357
00358 # endif