OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <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 Vec3;
37 class Vec4;
38 
39 /*
40  Two dimensional vector class, using Real precision.
41 */
42 class DLLEXPORT Vec2:
43  private boost::equality_comparable<Vec2>,
44  private boost::additive<Vec2>,
45  private boost::additive<Vec2, Real>,
46  private boost::multiplicative<Vec2, Real>
47 {
48 public:
50  Vec2(): x(0), y(0) { }
51 
53  Vec2(Real px, Real py): x(px), y(py) { }
54 
56  Vec2(const Vec2& v): x(v.x), y(v.y) { }
57 
59  explicit Vec2(const Vec3& v);
60 
62  explicit Vec2(const Vec4& v);
63 
65  explicit Vec2(const float v[2]): x(v[0]), y(v[1]) { }
66 
67 
69  explicit Vec2(const double v[2]): x(v[0]), y(v[1]) { }
70 
71 
72  Real GetX() const { return x; }
73  Real GetY() const { return y; }
74 
75  void SetX(Real d) { x=d; }
76  void SetY(Real d) { y=d; }
77 
79  bool operator==(const Vec2& rhs) const
80  {
81  return x==rhs.x && y==rhs.y;
82  }
83 
85  Real& operator[](std::size_t indx)
86  {
87  assert(indx<2);
88  return (&x)[indx];
89  }
90 
92  const Real& operator[](std::size_t indx) const
93  {
94  assert(indx<2);
95  return (&x)[indx];
96  }
97 
98  Real& At(size_t indx) {
99  if (indx>1) {
100  throw std::out_of_range("index must be smaller than 2");
101  }
102  return (&x)[indx];
103  }
104 
105  const Real& At(size_t indx) const {
106  if (indx>1) {
107  throw std::out_of_range("index must be smaller than 2");
108  }
109  return (&x)[indx];
110  }
111 
113  Vec2& operator+=(const Vec2& rhs)
114  {
115  x+=rhs.x;
116  y+=rhs.y;
117  return *this;
118  }
119 
120  Vec2& operator+=(Real d)
121  {
122  x+=d;
123  y+=d;
124  return *this;
125  }
126 
128  Vec2& operator-=(const Vec2& rhs)
129  {
130  x-=rhs.x;
131  y-=rhs.y;
132  return *this;
133  }
134 
135  Vec2& operator-=(Real d)
136  {
137  x-=d;
138  y-=d;
139  return *this;
140  }
142  Vec2 operator-() const
143  {
144  return Vec2(-x, -y);
145  }
146 
148  Vec2& operator*=(Real d)
149  {
150  x*=d;
151  y*=d;
152  return *this;
153  }
154 
156  Vec2& operator/=(Real d)
157  {
158  Real one_over_d=Real(1.0)/d;
159  x*=one_over_d;
160  y*=one_over_d;
161  return *this;
162  }
163 
164  Real* Data() {return &x;}
165  const Real* Data() const { return &x; }
166 
169 };
170 
171 inline Vec2 operator/(Real d, const Vec2& v)
172 {
173  return Vec2(d/v.x, d/v.y);
174 }
175 
176 inline std::ostream& operator<<(std::ostream& os, const Vec2& v)
177 {
178  os << "(" << v[0] << "," << v[1] << ")";
179  return os;
180 }
181 
182 }
183 
184 #include <ost/geom/vec3.hh>
185 #include <ost/geom/vec4.hh>
186 
187 namespace geom {
188 
189 inline Vec2::Vec2(const Vec3& v): x(v.x), y(v.y) { }
190 
191 
192 inline Vec2::Vec2(const Vec4& v): x(v.x), y(v.y)
193 {
194  if (std::fabs(v.w)<1e-10) {
195  throw DivideByZeroException();
196  }
197  x/=v.w;
198  y/=v.w;
199 }
200 
201 
202 typedef std::vector<Vec2> Vec2List;
203 
204 } // namespace geom
205 
206 # endif