OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vec4.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 //
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_VEC4_H
20 #define GEOM_VEC4_H
21 
22 #include <stdexcept>
23 #include <cassert>
24 #include <cstddef> // for size_t
25 #include <ostream>
26 
27 #include <boost/operators.hpp>
28 
30 #include <ost/config.hh>
31 
32 namespace geom {
33 
34 // fw decl
35 class Vec2;
36 class Vec3;
37 
38 /*
39  Four dimensional, homogeneous vector class, using Real precision.
40 */
41 class DLLEXPORT Vec4:
42  private boost::equality_comparable<Vec4>,
43  private boost::additive<Vec4>,
44  private boost::additive<Vec4, Real>,
45  private boost::multiplicative<Vec4, Real>
46 {
47 public:
49  Vec4(): x(0), y(0), z(0), w(1) { }
50 
52  Vec4(Real px, Real py, Real pz, Real pw): x(px), y(py), z(pz), w(pw) { }
53 
55  Vec4(const Vec4& v): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
56 
58  Vec4(const Vec2& v);
59 
61  Vec4(const Vec3& v);
62 
64  explicit Vec4(const float v[4]): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
65 
67  explicit Vec4(const double v[4]): x(v[0]), y(v[1]), z(v[2]), w(v[3]) { }
69  Vec4& operator=(const Vec4& v)
70  {
71  x=v.x;
72  y=v.y;
73  z=v.z;
74  w=v.w;
75  return *this;
76  }
77 
79  bool operator==(const Vec4& rhs) const
80  {
81  return x==rhs.x && y==rhs.y && z==rhs.z && w==rhs.w;
82  }
83 
85  Real& operator[](std::size_t indx)
86  {
87  assert(indx<4);
88  return (&x)[indx];
89  }
90 
92  const Real& operator[](std::size_t indx) const
93  {
94  assert(indx<4);
95  return (&x)[indx];
96  }
97 
98  Real& At(size_t indx) {
99  if (indx>3) {
100  throw std::out_of_range("index must be smaller than 4");
101  }
102  return (&x)[indx];
103  }
104 
105  const Real& At(size_t indx) const {
106  if (indx>3) {
107  throw std::out_of_range("index must be smaller than 4");
108  }
109  return (&x)[indx];
110  }
111 
113  Real GetX() const { return x; }
114  Real GetY() const { return y; }
115  Real GetZ() const { return z; }
116  Real GetW() const { return w; }
117 
118  void SetX(Real v) { x=v; }
119  void SetY(Real v) { y=v; }
120  void SetZ(Real v) { z=v; }
121  void SetW(Real v) { w=v; }
122 
124  Vec4& operator+=(const Vec4& rhs)
125  {
126  x+=rhs.x;
127  y+=rhs.y;
128  z+=rhs.z;
129  w+=rhs.w;
130  return *this;
131  }
132 
134  {
135  x+=d;
136  y+=d;
137  z+=d;
138  w+=d;
139  return *this;
140  }
141 
143  Vec4& operator-=(const Vec4& rhs)
144  {
145  x-=rhs.x;
146  y-=rhs.y;
147  z-=rhs.z;
148  w-=rhs.w;
149  return *this;
150  }
151 
153  {
154  x-=d;
155  y-=d;
156  z-=d;
157  w-=d;
158  return *this;
159  }
161  Vec4 operator-() const
162  {
163  return Vec4(-x, -y, -z, -w);
164  }
165 
168  {
169  x*=d;
170  y*=d;
171  z*=d;
172  w*=d;
173  return *this;
174  }
175 
178  {
179  Real one_over_d=Real(1.0)/d;
180  x*=one_over_d;
181  y*=one_over_d;
182  z*=one_over_d;
183  w*=one_over_d;
184  return *this;
185  }
186 
187  Real* Data() {return &x;}
188  const Real* Data() const {return &x;}
189 
194 };
195 
196 inline Vec4 operator/(Real d, const Vec4& v)
197 {
198  Vec4 nrvo(d/v[0],d/v[1],d/v[2],d/v[3]);
199  return nrvo;
200 }
201 
202 inline std::ostream& operator<<(std::ostream& os, const Vec4& v)
203 {
204  os << "(" << v[0] << "," << v[1] << "," << v[2] << "," << v[3] << ")";
205  return os;
206 }
207 }
208 
209 #include <ost/geom/vec2.hh>
210 #include <ost/geom/vec3.hh>
211 
212 namespace geom {
213 
215 inline Vec4::Vec4(const Vec2& v): x(v.x), y(v.y), z(0), w(1) { }
216 
218 inline Vec4::Vec4(const Vec3& v): x(v.x), y(v.y), z(v.z), w(1.0) { }
219 
220 } // namespace geom
221 
222 # endif
Vec4 & operator*=(Real d)
multipliable
Definition: vec4.hh:167
Vec4(const Vec4 &v)
copy ctor
Definition: vec4.hh:55
Real x
Definition: vec4.hh:190
Vec4 & operator/=(Real d)
dividable
Definition: vec4.hh:177
Vec4 & operator+=(Real d)
Definition: vec4.hh:133
Vec4()
Default initialization, all components are set to zero.
Definition: vec4.hh:49
Real * Data()
Definition: vec4.hh:187
Vec4(const double v[4])
explicit initialization with an array of doubles
Definition: vec4.hh:67
Real GetX() const
element access
Definition: vec4.hh:113
void SetZ(Real v)
Definition: vec4.hh:120
float Real
Definition: base.hh:44
Real & At(size_t indx)
Definition: vec4.hh:98
Real GetZ() const
Definition: vec4.hh:115
const Real & operator[](std::size_t indx) const
const element access
Definition: vec4.hh:92
Vec4 & operator=(const Vec4 &v)
assignement op
Definition: vec4.hh:69
Vec4 & operator+=(const Vec4 &rhs)
addable op
Definition: vec4.hh:124
Vec2 operator/(Real d, const Vec2 &v)
Definition: vec2.hh:171
Real w
Definition: vec4.hh:193
Vec4 & operator-=(Real d)
Definition: vec4.hh:152
bool operator==(const Vec4 &rhs) const
comparable
Definition: vec4.hh:79
const Real & At(size_t indx) const
Definition: vec4.hh:105
Vec4 operator-() const
negateable
Definition: vec4.hh:161
Real & operator[](std::size_t indx)
element access
Definition: vec4.hh:85
Three dimensional vector class, using Real precision.
Definition: vec3.hh:43
void SetY(Real v)
Definition: vec4.hh:119
const Real * Data() const
Definition: vec4.hh:188
Real z
Definition: vec4.hh:192
Vec4 & operator-=(const Vec4 &rhs)
subtractable op
Definition: vec4.hh:143
Vec4(Real px, Real py, Real pz, Real pw)
Initialization with x, y and z component.
Definition: vec4.hh:52
Real GetY() const
Definition: vec4.hh:114
Real GetW() const
Definition: vec4.hh:116
std::ostream & operator<<(std::ostream &os, const AlignedCuboid &c)
Real y
Definition: vec4.hh:191
void SetW(Real v)
Definition: vec4.hh:121
Vec4(const float v[4])
explicit initialization with an array of floats
Definition: vec4.hh:64
void SetX(Real v)
Definition: vec4.hh:118