OpenStructure
vec2.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_VEC2_H
20 #define GEOM_VEC2_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 Vec3;
36 class Vec4;
37 
38 /*
39  Two dimensional vector class, using Real precision.
40 */
41 class DLLEXPORT Vec2:
42  private boost::equality_comparable<Vec2>,
43  private boost::additive<Vec2>,
44  private boost::additive<Vec2, Real>,
45  private boost::multiplicative<Vec2, Real>
46 {
47 public:
49  Vec2(): x(0), y(0) { }
50 
52  Vec2(Real px, Real py): x(px), y(py) { }
53 
55  Vec2(const Vec2& v): x(v.x), y(v.y) { }
56 
58  explicit Vec2(const Vec3& v);
59 
61  explicit Vec2(const Vec4& v);
62 
64  explicit Vec2(const float v[2]): x(v[0]), y(v[1]) { }
65 
66 
68  explicit Vec2(const double v[2]): x(v[0]), y(v[1]) { }
69 
70 
71  Real GetX() const { return x; }
72  Real GetY() const { return y; }
73 
74  void SetX(Real d) { x=d; }
75  void SetY(Real d) { y=d; }
76 
78  bool operator==(const Vec2& rhs) const
79  {
80  return x==rhs.x && y==rhs.y;
81  }
82 
84  Real& operator[](std::size_t indx)
85  {
86  if (indx>1) {
87  throw std::out_of_range("Index must be in the range [0-1]");
88  }
89  return (&x)[indx];
90  }
91 
93  const Real& operator[](std::size_t indx) const
94  {
95  if (indx>1) {
96  throw std::out_of_range("Index must be in the range [0-1]");
97  }
98  return (&x)[indx];
99  }
100 
102  Vec2& operator+=(const Vec2& rhs)
103  {
104  x+=rhs.x;
105  y+=rhs.y;
106  return *this;
107  }
108 
109  Vec2& operator+=(Real d)
110  {
111  x+=d;
112  y+=d;
113  return *this;
114  }
115 
117  Vec2& operator-=(const Vec2& rhs)
118  {
119  x-=rhs.x;
120  y-=rhs.y;
121  return *this;
122  }
123 
124  Vec2& operator-=(Real d)
125  {
126  x-=d;
127  y-=d;
128  return *this;
129  }
131  Vec2 operator-() const
132  {
133  return Vec2(-x, -y);
134  }
135 
137  Vec2& operator*=(Real d)
138  {
139  x*=d;
140  y*=d;
141  return *this;
142  }
143 
145  Vec2& operator/=(Real d)
146  {
147  Real one_over_d=Real(1.0)/d;
148  x*=one_over_d;
149  y*=one_over_d;
150  return *this;
151  }
152 
153  Real* Data() {return &x;}
154  const Real* Data() const { return &x; }
155 
158 };
159 
160 inline Vec2 operator/(Real d, const Vec2& v)
161 {
162  return Vec2(d/v.x, d/v.y);
163 }
164 
165 inline std::ostream& operator<<(std::ostream& os, const Vec2& v)
166 {
167  os << "(" << v[0] << "," << v[1] << ")";
168  return os;
169 }
170 
171 }
172 
173 #include <ost/geom/vec3.hh>
174 #include <ost/geom/vec4.hh>
175 
176 namespace geom {
177 
178 inline Vec2::Vec2(const Vec3& v): x(v.x), y(v.y) { }
179 
180 
181 inline Vec2::Vec2(const Vec4& v): x(v.x), y(v.y) { }
182 
183 
184 typedef std::vector<Vec2> Vec2List;
185 
186 } // namespace geom
187 
188 # endif