OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mat3.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_MAT3_HH
20 #define GEOM_MAT3_HH
21 
22 #include <cstddef> // for size_t
23 #include <ostream>
24 #include <cassert>
25 #include <stdexcept>
26 
27 #include <boost/operators.hpp>
28 
30 #include <ost/geom/mat2.hh>
31 namespace geom {
32 
33 class Vec3;
34 
36  private boost::equality_comparable<Mat3>,
37  private boost::additive1<Mat3>,
38  private boost::multiplicative2<Mat3, Real>
39 {
40 public:
42  Mat3()
43  {
44  this->set(1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0);
45  }
47 
56  Mat3(Real i00, Real i01, Real i02,
57  Real i10, Real i11, Real i12,
58  Real i20, Real i21, Real i22)
59  {
60  this->set(i00,i01,i02,i10,i11,i12,i20,i21,i22);
61  }
62 
63  Mat3(const Mat3& m)
64  {
65  this->set(m(0,0),m(0,1),m(0,2),m(1,0),m(1,1),m(1,2),m(2,0),m(2,1),m(2,2));
66  }
67 
68  Mat3(const Mat2& m)
69  {
70  this->set(m(0, 0), m(0, 1), Real(0.0),
71  m(1, 0), m(1, 1), Real(0.0),
72  Real(0.0), Real(0.0), Real(1.0));
73  }
74 
75  explicit Mat3(const Real arr[9])
76  {
77  this->set(arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8]);
78  }
79 
80  explicit Mat3(Real x, Real y, Real z)
81  {
82  this->set(x, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, z);
83  }
85  Real& At(std::size_t r, std::size_t c)
86  {
87  if (r>2 || c>2) {
88  throw std::out_of_range("indices must be smaller than 3");
89  }
90  return data_[r][c];
91  }
93  const Real& At(std::size_t r, std::size_t c) const
94  {
95  if (r>2 || c>2) {
96  throw std::out_of_range("indices must be smaller than 3");
97  }
98  return data_[r][c];
99  }
101  Real& operator()(std::size_t r, std::size_t c)
102  {
103  assert(r<3 && c<3);
104  return data_[r][c];
105  }
107  const Real& operator()(std::size_t r, std::size_t c) const
108  {
109  assert(r<3 && c<3);
110  return data_[r][c];
111  }
112 
113  Mat3& operator=(const Mat3& m)
114  {
115  if(&m!=this) {
116  this->set(m(0,0),m(0,1),m(0,2),m(1,0),m(1,1),m(1,2),m(2,0),m(2,1),m(2,2));
117  }
118  return *this;
119  }
120 
121  static Mat3 Identity()
122  {
123  static Mat3 i(1.0,0.0,0.0,
124  0.0,1.0,0.0,
125  0.0,0.0,1.0);
126  return i;
127  }
128 
129  bool operator==(const Mat3& rhs) const
130  {
131  return data_[0][0] == rhs.data_[0][0] &&
132  data_[1][0] == rhs.data_[1][0] &&
133  data_[2][0] == rhs.data_[2][0] &&
134  data_[0][1] == rhs.data_[0][1] &&
135  data_[1][1] == rhs.data_[1][1] &&
136  data_[2][1] == rhs.data_[2][1] &&
137  data_[0][2] == rhs.data_[0][2] &&
138  data_[1][2] == rhs.data_[1][2] &&
139  data_[2][2] == rhs.data_[2][2];
140  }
141 
142  Mat3& operator+=(const Mat3& rhs)
143  {
144  data_[0][0]+=rhs(0,0);
145  data_[0][1]+=rhs(0,1);
146  data_[0][2]+=rhs(0,2);
147  data_[1][0]+=rhs(1,0);
148  data_[1][1]+=rhs(1,1);
149  data_[1][2]+=rhs(1,2);
150  data_[2][0]+=rhs(2,0);
151  data_[2][1]+=rhs(2,1);
152  data_[2][2]+=rhs(2,2);
153  return *this;
154  }
155  Mat3& operator-=(const Mat3& rhs)
156  {
157  data_[0][0]-=rhs(0,0);
158  data_[0][1]-=rhs(0,1);
159  data_[0][2]-=rhs(0,2);
160  data_[1][0]-=rhs(1,0);
161  data_[1][1]-=rhs(1,1);
162  data_[1][2]-=rhs(1,2);
163  data_[2][0]-=rhs(2,0);
164  data_[2][1]-=rhs(2,1);
165  data_[2][2]-=rhs(2,2);
166  return *this;
167  }
168  Mat3& operator*=(const Real d)
169  {
170  data_[0][0]*=d;
171  data_[0][1]*=d;
172  data_[0][2]*=d;
173  data_[1][0]*=d;
174  data_[1][1]*=d;
175  data_[1][2]*=d;
176  data_[2][0]*=d;
177  data_[2][1]*=d;
178  data_[2][2]*=d;
179  return *this;
180  }
181  Mat3& operator/=(const Real d)
182  {
183  data_[0][0]/=d;
184  data_[0][1]/=d;
185  data_[0][2]/=d;
186  data_[1][0]/=d;
187  data_[1][1]/=d;
188  data_[1][2]/=d;
189  data_[2][0]/=d;
190  data_[2][1]/=d;
191  data_[2][2]/=d;
192  return *this;
193  }
194 
195  Mat3& operator*=(const Mat3& m)
196  {
197  (*this)=Mat3((*this)(0,0)*m(0,0)+(*this)(0,1)*m(1,0)+(*this)(0,2)*m(2,0),
198  (*this)(0,0)*m(0,1)+(*this)(0,1)*m(1,1)+(*this)(0,2)*m(2,1),
199  (*this)(0,0)*m(0,2)+(*this)(0,1)*m(1,2)+(*this)(0,2)*m(2,2),
200  (*this)(1,0)*m(0,0)+(*this)(1,1)*m(1,0)+(*this)(1,2)*m(2,0),
201  (*this)(1,0)*m(0,1)+(*this)(1,1)*m(1,1)+(*this)(1,2)*m(2,1),
202  (*this)(1,0)*m(0,2)+(*this)(1,1)*m(1,2)+(*this)(1,2)*m(2,2),
203  (*this)(2,0)*m(0,0)+(*this)(2,1)*m(1,0)+(*this)(2,2)*m(2,0),
204  (*this)(2,0)*m(0,1)+(*this)(2,1)*m(1,1)+(*this)(2,2)*m(2,1),
205  (*this)(2,0)*m(0,2)+(*this)(2,1)*m(1,2)+(*this)(2,2)*m(2,2));
206  return *this;
207  }
208 
209  Real* Data() {return &data_[0][0];}
210  const Real* Data() const {return &data_[0][0];}
211 
212  geom::Vec3 GetCol(int index) const;
213  geom::Vec3 GetRow(int index) const;
214 private:
215  Real data_[3][3];
216 
217  void set(Real i00, Real i01, Real i02,
218  Real i10, Real i11, Real i12,
219  Real i20, Real i21, Real i22)
220  {
221  data_[0][0]=i00; data_[0][1]=i01; data_[0][2]=i02;
222  data_[1][0]=i10; data_[1][1]=i11; data_[1][2]=i12;
223  data_[2][0]=i20; data_[2][1]=i21; data_[2][2]=i22;
224  }
225 };
226 
227 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& o, const Mat3& m);
228 
229 } // ns geom
230 
231 
232 #endif
float Real
Definition: base.hh:44
bool operator==(const Mat3 &rhs) const
Definition: mat3.hh:129
Mat3 & operator/=(const Real d)
Definition: mat3.hh:181
static Mat3 Identity()
Definition: mat3.hh:121
Real & At(std::size_t r, std::size_t c)
element access
Definition: mat3.hh:85
const Real * Data() const
Definition: mat3.hh:210
Mat3(Real i00, Real i01, Real i02, Real i10, Real i11, Real i12, Real i20, Real i21, Real i22)
In with 9 values in row-major order.
Definition: mat3.hh:56
const Real & operator()(std::size_t r, std::size_t c) const
const element access
Definition: mat3.hh:107
Mat3 & operator+=(const Mat3 &rhs)
Definition: mat3.hh:142
Mat3(const Mat3 &m)
Definition: mat3.hh:63
Mat3 & operator*=(const Real d)
Definition: mat3.hh:168
Mat3(const Real arr[9])
Definition: mat3.hh:75
Three dimensional vector class, using Real precision.
Definition: vec3.hh:43
Real * Data()
Definition: mat3.hh:209
Mat3 & operator*=(const Mat3 &m)
Definition: mat3.hh:195
Mat3(Real x, Real y, Real z)
Definition: mat3.hh:80
const Real & At(std::size_t r, std::size_t c) const
element access
Definition: mat3.hh:93
Mat3 & operator=(const Mat3 &m)
Definition: mat3.hh:113
std::ostream & operator<<(std::ostream &os, const AlignedCuboid &c)
#define DLLEXPORT_OST_GEOM
Mat3 & operator-=(const Mat3 &rhs)
Definition: mat3.hh:155
Mat3(const Mat2 &m)
Definition: mat3.hh:68
Mat3()
Default initialization, identity matrix.
Definition: mat3.hh:42
Real & operator()(std::size_t r, std::size_t c)
element access
Definition: mat3.hh:101