OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <cassert>
24 #include <cstddef> // for size_t
25 #include <ostream>
26 #include <vector>
27 #include <boost/operators.hpp>
28 
29 
30 #include <ost/config.hh>
32 #include <ost/geom/exc.hh>
33 namespace geom {
34 
35 // fw decl
36 class Vec2;
37 class Vec4;
38 class Line3;
39 class Plane;
40 
43  private boost::equality_comparable<Vec3>,
44  private boost::additive<Vec3>,
45  private boost::additive<Vec3, Real>,
46  private boost::multiplicative<Vec3, Real>
47 {
48 public:
50  Vec3(): x(0), y(0), z(0) {}
51 
53  Vec3(Real px, Real py, Real pz): x(px), y(py), z(pz) {}
54 
56  Vec3(const Vec3& v): x(v.x), y(v.y), z(v.z) { }
57 
59  Vec3(const Vec2& v);
60 
62 
66  explicit Vec3(const Vec4& v);
67 
68  explicit Vec3(Real v): x(v), y(v), z(v) { }
69 
71  explicit Vec3(const double v[3]): x(v[0]), y(v[1]), z(v[2]) { }
72 
74  explicit Vec3(const float v[3]): x(v[0]), y(v[1]), z(v[2]) { }
75 
77  Vec3& operator=(const Vec3& v)
78  {
79  x=v.x;
80  y=v.y;
81  z=v.z;
82  return *this;
83  }
84 
86  bool operator==(const Vec3& rhs) const
87  {
88  return x==rhs.x && y==rhs.y && z==rhs.z;
89  }
90 
92  Real& operator[](std::size_t indx)
93  {
94  assert(indx<3);
95  return (&x)[indx];
96  }
97 
99  const Real& operator[](std::size_t indx) const
100  {
101  assert(indx<3);
102  return (&x)[indx];
103  }
104 
105  Real& At(size_t indx) {
106  if (indx>2) {
107  throw std::out_of_range("index must be smaller than 3");
108  }
109  return (&x)[indx];
110  }
111 
112  const Real& At(size_t indx) const {
113  if (indx>2) {
114  throw std::out_of_range("index must be smaller than 3");
115  }
116  return (&x)[indx];
117  }
119  Real GetX() const { return x; }
120  Real GetY() const { return y; }
121  Real GetZ() const { return z; }
122  void SetX(Real v) { x=v; }
123  void SetY(Real v) { y=v; }
124  void SetZ(Real v) { z=v; }
125 
127  Vec3& operator+=(const Vec3& rhs)
128  {
129  x+=rhs.x;
130  y+=rhs.y;
131  z+=rhs.z;
132  return *this;
133  }
134 
136  {
137  x+=d;
138  y+=d;
139  z+=d;
140  return *this;
141  }
142 
144  Vec3& operator-=(const Vec3& rhs)
145  {
146  x-=rhs.x;
147  y-=rhs.y;
148  z-=rhs.z;
149  return *this;
150  }
151 
153  {
154  x-=d;
155  y-=d;
156  z-=d;
157  return *this;
158  }
160  Vec3 operator-() const
161  {
162  return Vec3(-x, -y, -z);
163  }
164 
167  {
168  x*=d;
169  y*=d;
170  z*=d;
171  return *this;
172  }
173 
176  {
177  Real one_over_d=Real(1.0)/d;
178  x*=one_over_d;
179  y*=one_over_d;
180  z*=one_over_d;
181  return *this;
182  }
183 
184  Real* Data() {return &x;}
185  const Real* Data() const {return &x;}
186 
190 };
191 
192 inline Vec3 operator/(Real d, const Vec3& v)
193 {
194  Vec3 nrvo(d/v[0],d/v[1],d/v[2]);
195  return nrvo;
196 }
197 
198 // The following operator is among other things used to write vector
199 // data into info files. If its format is changed, the string to
200 // vector type cast in item_type_cast.hh has to be changed
201 // accordingly.
202 inline std::ostream& operator<<(std::ostream& os, const Vec3& v)
203 {
204  os << "[" << v.x << ", " << v.y << ", " << v.z << "]";
205  return os;
206 }
207 } // ns geom
208 
209 namespace geom {
210 
211  // TODO: move to separate file
212  class Mat3;
213 
215  public std::vector<Vec3>,
216  private boost::equality_comparable<Vec3List>,
217  private boost::additive<Vec3List>,
218  private boost::additive<Vec3List, Real>,
219  private boost::multiplicative<Vec3List, Real>
220  {
221 public:
222  typedef std::vector<Vec3> base_type;
224 
225  Vec3List(size_t size, const Vec3& value=Vec3()) : base_type(size, value) {}
226  Vec3List(base_type::iterator b, base_type::iterator e): base_type(b, e) { }
227 
228  Vec3List(const Vec3List& rhs) : base_type(rhs) { }
229  Vec3List(const base_type& rhs) : base_type(rhs) { }
231  {
232  base_type::operator=(rhs);
233  return *this;
234  }
236  bool operator==(const Vec3List& rhs) const
237  {
238  if (this->size()!=rhs.size()){
239  throw std::length_error("Vec3List must have the same size");
240  }
241  for (unsigned int i=0;i!=this->size();++i) {
242  if (((*this)[i])!=((rhs)[i])){
243  return false;
244  }
245  }
246  return true;
247  }
250  {
251  if (this->size()!=rhs.size()){
252  throw std::length_error("Vec3List must have the same size");
253  }
254  for (unsigned int i=0;i!=this->size();++i) {
255  (*this)[i]+=(rhs)[i];
256  }
257  return *this;
258  }
260  {
261  for (unsigned int i=0;i!=this->size();++i) {
262  (*this)[i]+=d;
263  }
264  return *this;
265  }
266 
269  {
270  if (this->size()!=rhs.size()){
271  throw std::length_error("Vec3List must have the same size");
272  }
273  for (unsigned int i=0;i!=this->size();++i) {
274  (*this)[i]-=(rhs)[i];
275  }
276  return *this;
277  }
278 
280  {
281  for (unsigned int i=0;i!=this->size();++i) {
282  (*this)[i]-=d;
283  }
284  return *this;
285  }
287  //Vec3List3 operator-() const
288  //{
289  // geom::Vec3List vl;
290  // for (unsigned int i=0;i!=this->size();++i) {
291  // geom::Vec3 v=(*this)[i];
292  // vl.push_back(-v);
293  // }
294  // return vl;
295  //}
296 
299  {
300  for (unsigned int i=0;i!=this->size();++i) {
301  (*this)[i]*=d;
302  }
303  return *this;
304  }
305 
308  {
309  for (unsigned int i=0;i!=this->size();++i) {
310  (*this)[i]/=d;
311  }
312  return *this;
313  }
314 
315  // TODO: move some or all of these to stand-alone functions
316  Mat3 GetInertia() const;
317  Vec3 GetCenter() const;
318  Mat3 GetPrincipalAxes() const;
319  Line3 GetODRLine() const;
320  Plane GetODRPlane() const;
321 
322  //This function fits a cylinder to the positions in Vec3List
323  //It takes as argument an initial guess for the direction.
324  //The center is set to the geometric centero of the atoms
325  //and is not changed during optimisation as the best fitting cylinder
326  //can be shown to have its axis pass through the geometric center
327  //It returns a pair containing a line3, giving the direction of the Cylinder
328  //and a Real containing the radius.
329  std::pair<Line3, Real> FitCylinder(const Vec3& initial_direction) const;
330 };
331 } // ns geom
332 
333 
334 #include <ost/geom/vec2.hh>
335 #include <ost/geom/vec4.hh>
336 #include <ost/geom/mat3.hh>
337 #include <ost/geom/composite3.hh>
338 
339 namespace geom {
340  inline Vec3::Vec3(const Vec2& v): x(v.x), y(v.y), z(0.0) { }
341 
342  inline Vec3::Vec3(const Vec4& v): x(v.x), y(v.y), z(v.z)
343  {
344  if (std::fabs(v.w)<1e-10) {
345  // it is better to ignore very small w and to simply assume
346  // that this is not a homogeneous coordinate rather than
347  // throwing an exception
348  //throw DivideByZeroException();
349  } else {
350  x/=v.w;
351  y/=v.w;
352  z/=v.w;
353  }
354  }
355 } // namespace geom
356 
357 
358 # endif
Vec3 & operator=(const Vec3 &v)
assignement op
Definition: vec3.hh:77
Vec3List & operator*=(Real d)
negateable
Definition: vec3.hh:298
Real y
Definition: vec3.hh:188
const Real & At(size_t indx) const
Definition: vec3.hh:112
float Real
Definition: base.hh:44
Real GetY() const
Definition: vec3.hh:120
Vec3List & operator-=(Real d)
Definition: vec3.hh:279
bool operator==(const Vec3 &rhs) const
comparable
Definition: vec3.hh:86
const Real * Data() const
Definition: vec3.hh:185
Vec3List(const Vec3List &rhs)
Definition: vec3.hh:228
Vec3(Real v)
Definition: vec3.hh:68
Vec3List(base_type::iterator b, base_type::iterator e)
Definition: vec3.hh:226
Vec2 operator/(Real d, const Vec2 &v)
Definition: vec2.hh:171
void SetZ(Real v)
Definition: vec3.hh:124
Real w
Definition: vec4.hh:193
Real & operator[](std::size_t indx)
element access
Definition: vec3.hh:92
Vec3List(size_t size, const Vec3 &value=Vec3())
Definition: vec3.hh:225
Vec3List & operator-=(const Vec3List &rhs)
subtractable op
Definition: vec3.hh:268
Line3.
Definition: composite3.hh:39
void SetY(Real v)
Definition: vec3.hh:123
Vec3(const float v[3])
explicit initialization with an array of floats
Definition: vec3.hh:74
Real x
Definition: vec3.hh:187
Vec3List & operator=(const Vec3List &rhs)
Definition: vec3.hh:230
Vec3()
Default initialization, all components are set to zero.
Definition: vec3.hh:50
Vec3(Real px, Real py, Real pz)
Initialization with x, y and z component.
Definition: vec3.hh:53
std::vector< Vec3 > base_type
Definition: vec3.hh:222
Real z
Definition: vec3.hh:189
Vec3 & operator+=(const Vec3 &rhs)
addable op
Definition: vec3.hh:127
Real GetZ() const
Definition: vec3.hh:121
Vec3List(const base_type &rhs)
Definition: vec3.hh:229
Vec3 & operator-=(const Vec3 &rhs)
subtractable op
Definition: vec3.hh:144
Three dimensional vector class, using Real precision.
Definition: vec3.hh:42
Vec3 & operator*=(Real d)
multipliable
Definition: vec3.hh:166
Vec3List & operator/=(Real d)
dividable
Definition: vec3.hh:307
Vec3 operator-() const
negateable
Definition: vec3.hh:160
Vec3List & operator+=(const Vec3List &rhs)
addable op
Definition: vec3.hh:249
Real GetX() const
element access
Definition: vec3.hh:119
const Real & operator[](std::size_t indx) const
const element access
Definition: vec3.hh:99
void SetX(Real v)
Definition: vec3.hh:122
Real * Data()
Definition: vec3.hh:184
Vec3(const Vec3 &v)
copy ctor
Definition: vec3.hh:56
Vec3List & operator+=(Real d)
Definition: vec3.hh:259
std::ostream & operator<<(std::ostream &os, const AlignedCuboid &c)
#define DLLEXPORT_OST_GEOM
Vec3 & operator+=(Real d)
Definition: vec3.hh:135
Vec3 & operator/=(Real d)
dividable
Definition: vec3.hh:175
Vec3(const double v[3])
explicit initialization with an array of doubles
Definition: vec3.hh:71
Real & At(size_t indx)
Definition: vec3.hh:105
bool operator==(const Vec3List &rhs) const
comparable
Definition: vec3.hh:236
Vec3 & operator-=(Real d)
Definition: vec3.hh:152