OpenStructure
vec3.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef GEOM_VEC3_H
20 #define GEOM_VEC3_H
21 
22 #include <stdexcept>
23 #include <cstddef> // for size_t
24 #include <ostream>
25 #include <vector>
26 #include <boost/operators.hpp>
27 
28 
29 #include <ost/config.hh>
31 
32 namespace geom {
33 
34 // fw decl
35 class Vec2;
36 class Vec4;
37 
38 
41  private boost::equality_comparable<Vec3>,
42  private boost::additive<Vec3>,
43  private boost::additive<Vec3, Real>,
44  private boost::multiplicative<Vec3, Real>
45 {
46 public:
48  Vec3(): x(0), y(0), z(0) {}
49 
51  Vec3(Real px, Real py, Real pz): x(px), y(py), z(pz) {}
52 
54  Vec3(const Vec3& v): x(v.x), y(v.y), z(v.z) { }
55 
57  Vec3(const Vec2& v);
58 
60 
64  explicit Vec3(const Vec4& v);
65 
66  explicit Vec3(Real v): x(v), y(v), z(v) { }
67 
69  explicit Vec3(const double v[3]): x(v[0]), y(v[1]), z(v[2]) { }
70 
72  explicit Vec3(const float v[3]): x(v[0]), y(v[1]), z(v[2]) { }
73 
75  Vec3& operator=(const Vec3& v)
76  {
77  x=v.x;
78  y=v.y;
79  z=v.z;
80  return *this;
81  }
82 
84  bool operator==(const Vec3& rhs) const
85  {
86  return x==rhs.x && y==rhs.y && z==rhs.z;
87  }
88 
90  Real& operator[](std::size_t indx)
91  {
92  if (indx>2) {
93  throw std::out_of_range("Index must be in the range [0-2]");
94  }
95  return (&x)[indx];
96  }
97 
99  const Real& operator[](std::size_t indx) const
100  {
101  if (indx>2) {
102  throw std::out_of_range("Index must be in the range [0-2]");
103  }
104  return (&x)[indx];
105  }
107  Real GetX() const { return x; }
108  Real GetY() const { return y; }
109  Real GetZ() const { return z; }
110  void SetX(Real v) { x=v; }
111  void SetY(Real v) { y=v; }
112  void SetZ(Real v) { z=v; }
113 
115  Vec3& operator+=(const Vec3& rhs)
116  {
117  x+=rhs.x;
118  y+=rhs.y;
119  z+=rhs.z;
120  return *this;
121  }
122 
123  Vec3& operator+=(Real d)
124  {
125  x+=d;
126  y+=d;
127  z+=d;
128  return *this;
129  }
130 
132  Vec3& operator-=(const Vec3& rhs)
133  {
134  x-=rhs.x;
135  y-=rhs.y;
136  z-=rhs.z;
137  return *this;
138  }
139 
140  Vec3& operator-=(Real d)
141  {
142  x-=d;
143  y-=d;
144  z-=d;
145  return *this;
146  }
148  Vec3 operator-() const
149  {
150  return Vec3(-x, -y, -z);
151  }
152 
154  Vec3& operator*=(Real d)
155  {
156  x*=d;
157  y*=d;
158  z*=d;
159  return *this;
160  }
161 
163  Vec3& operator/=(Real d)
164  {
165  Real one_over_d=Real(1.0)/d;
166  x*=one_over_d;
167  y*=one_over_d;
168  z*=one_over_d;
169  return *this;
170  }
171 
172  Real* Data() {return &x;}
173  const Real* Data() const {return &x;}
174 
178 };
179 
180 inline Vec3 operator/(Real d, const Vec3& v)
181 {
182  Vec3 nrvo(d/v[0],d/v[1],d/v[2]);
183  return nrvo;
184 }
185 
186 inline std::ostream& operator<<(std::ostream& os, const Vec3& v)
187 {
188  os << "[" << v.x << ", " << v.y << ", " << v.z << "]";
189  return os;
190 }
191 }
192 
193 #include <ost/geom/vec2.hh>
194 #include <ost/geom/vec4.hh>
195 #include <ost/geom/mat3.hh>
196 
197 namespace geom {
198 
199 class DLLEXPORT_OST_GEOM Vec3List : public std::vector<Vec3> {
200 public:
201  typedef std::vector<Vec3> base_type;
203 
204  Vec3List(size_t size, const Vec3& value=Vec3()) : base_type(size, value) {}
205  Vec3List(base_type::iterator b, base_type::iterator e): base_type(b, e) { }
206 
207  Vec3List(const Vec3List& rhs) : base_type(rhs) { }
208 
209  Vec3List& operator=(const Vec3List& rhs)
210  {
211  base_type::operator=(rhs);
212  return *this;
213  }
214  Mat3 GetInertia() const;
215 
216  Vec3 GetCenter() const;
217 
218  Mat3 GetPrincipalAxes() const;
219 };
220 
221 
222 inline Vec3::Vec3(const Vec2& v): x(v.x), y(v.y), z(0.0) { }
223 inline Vec3::Vec3(const Vec4& v): x(v.x/v.w), y(v.y/v.w), z(v.z/v.w) { }
224 
225 } // namespace geom
226 
227 
228 # endif