OpenStructure
point.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-2020 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  integer triplet based point
23 
24  Authors: Ansgar Philippsen, Andreas Schenk
25 */
26 
27 #ifndef IMG_POINT_H
28 #define IMG_POINT_H
29 
30 #include <vector>
31 #include <iosfwd>
32 #include <boost/operators.hpp>
33 
34 #include <ost/img/module_config.hh>
35 #include "vecmat.hh"
36 
37 namespace ost { namespace img {
38 
39 //fw decl
40 class Size;
41 
43 class DLLEXPORT_OST_IMG_BASE Point: boost::additive<Point,
44  boost::additive2<Point, Size,
45  boost::multipliable2<Point,int,
46  boost::less_than_comparable<Point,
47  boost::equality_comparable<Point> > > > >{
48 public:
49  Point():
50  x(),
51  y(),
52  z()
53  {
54  }
55 
56  Point(const Point &p):
57  x(p.x),
58  y(p.y),
59  z(p.z)
60  {
61  }
62 
63 
65  Point(int a):
66  x(a),
67  y(),
68  z()
69  {
70  }
71 
73  Point(int a, int b):
74  x(a),
75  y(b),
76  z()
77  {
78  }
79 
81  Point(int a, int b, int c):
82  x(a),
83  y(b),
84  z(c)
85  {
86  }
87 
89  explicit Point(const Vec2& v):
90  x(static_cast<int>(round(v[0]))),
91  y(static_cast<int>(round(v[1]))),
92  z()
93  {
94  }
95 
96 
98  explicit Point(const Vec3& v):
99  x(static_cast<int>(round(v[0]))),
100  y(static_cast<int>(round(v[1]))),
101  z(static_cast<int>(round(v[2])))
102  {
103  }
104 
105 
107  explicit Point(const Vec4& v):
108  x(static_cast<int>(round(v[0]))),
109  y(static_cast<int>(round(v[1]))),
110  z(static_cast<int>(round(v[2])))
111  {
112  if(std::abs(v[3])<1e-100) {
113  throw geom::OutOfRangeException("4th element of Vec4 is too close to zero for normalization");
114  } else {
115  Real sf = 1.0/v[3];
116  x = static_cast<int>(x*sf);
117  y = static_cast<int>(y*sf);
118  z = static_cast<int>(z*sf);
119  }
120  }
121 
123  Point(const Size& size);
124 
126  Point Mirror(int planes);
127 
128  // operators
129  int& operator[](unsigned int index)
130  {
131  assert(index<=2);
132  return (&x)[index];
133  }
134 
135  int operator[](unsigned int index) const
136  {
137  assert(index<=2);
138  return (&x)[index];
139  }
140  // operators
141 
142  int& At(unsigned int index)
143  {
144  if(index>2) throw std::range_error("Point index out of range");
145  return (&x)[index];
146  }
147 
148  int At(unsigned int index) const
149  {
150  if(index>2) throw geom::OutOfRangeException("Point index out of range");
151  return (&x)[index];
152  }
153 
154 
155  Point& operator=(const Point& p);
156 
157  Point& operator+=(const Point& p);
158 
159  Point& operator-=(const Point& p);
160 
161  Point operator-() const;
162 
163  Point& operator+=(const Size& p);
164 
165  Point& operator-=(const Size& p);
166 
167  bool operator==(const Point &p) const;
168 
169  bool operator<(const Point &p) const;
170 
171  Point& operator*=(int s) {x*=s;y*=s;z*=s;return *this;}
172 
173  // conversion to vectors
174  Vec2 ToVec2() const;
175  Vec3 ToVec3() const;
176  Vec4 ToVec4() const;
177 
178  int x;
179  int y;
180  int z;
181 };
182 
183 
184 DLLEXPORT_OST_IMG_BASE std::ostream& operator<<(std::ostream& os, const Point &p);
185 
186 
187 }} // namespace img
188 
189 
190 #endif
191 
192 
193 
class encapsulating 1D to 3D point
Definition: point.hh:47
int & operator[](unsigned int index)
Definition: point.hh:129
Point(int a)
1D int constructor
Definition: point.hh:65
Point(const Vec3 &v)
conversion from Vec3
Definition: point.hh:98
Point & operator*=(int s)
Definition: point.hh:171
Point(int a, int b)
2D int constructor
Definition: point.hh:73
Point operator-() const
int At(unsigned int index) const
Definition: point.hh:148
Point(const Point &p)
Definition: point.hh:56
Point & operator=(const Point &p)
Vec2 ToVec2() const
Point(int a, int b, int c)
3D int constructor
Definition: point.hh:81
Point & operator+=(const Size &p)
Point & operator+=(const Point &p)
Point Mirror(int planes)
return mirror point according to planes
bool operator<(const Point &p) const
int operator[](unsigned int index) const
Definition: point.hh:135
Vec3 ToVec3() const
Point(const Size &size)
(implicit) conversion of size to point
Point & operator-=(const Point &p)
Point(const Vec4 &v)
conversion from Vec4 (normalization)
Definition: point.hh:107
int & At(unsigned int index)
Definition: point.hh:142
bool operator==(const Point &p) const
Vec4 ToVec4() const
Point(const Vec2 &v)
conversion from Vec2
Definition: point.hh:89
Point & operator-=(const Size &p)
class encapsulating 1D to 3D size
Definition: size.hh:39
#define DLLEXPORT_OST_IMG_BASE
float Real
Definition: base.hh:44
DLLEXPORT_OST_IMG_BASE std::ostream & operator<<(std::ostream &os, const img::Extent &b)
Definition: base.dox:1