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-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_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& operator()(std::size_t r, std::size_t c)
86  {
87  if (r>2 || c >2) {
88  throw std::out_of_range("row and column must be in the range [0-2]");
89  }
90  return data_[r][c];
91  }
93  const Real& operator()(std::size_t r, std::size_t c) const
94  {
95  if (r>2 || c >2) {
96  throw std::out_of_range("row and column must be in the range [0-2]");
97  }
98  return data_[r][c];
99  }
100 
101  Mat3& operator=(const Mat3& m)
102  {
103  if(&m!=this) {
104  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));
105  }
106  return *this;
107  }
108 
109  static Mat3 Identity()
110  {
111  static Mat3 i(1.0,0.0,0.0,
112  0.0,1.0,0.0,
113  0.0,0.0,1.0);
114  return i;
115  }
116 
117  bool operator==(const Mat3& rhs) const
118  {
119  return data_[0][0] == rhs.data_[0][0] &&
120  data_[1][0] == rhs.data_[1][0] &&
121  data_[2][0] == rhs.data_[2][0] &&
122  data_[0][1] == rhs.data_[0][1] &&
123  data_[1][1] == rhs.data_[1][1] &&
124  data_[2][1] == rhs.data_[2][1] &&
125  data_[0][2] == rhs.data_[0][2] &&
126  data_[1][2] == rhs.data_[1][2] &&
127  data_[2][2] == rhs.data_[2][2];
128  }
129 
130  Mat3& operator+=(const Mat3& rhs)
131  {
132  data_[0][0]+=rhs(0,0);
133  data_[0][1]+=rhs(0,1);
134  data_[0][2]+=rhs(0,2);
135  data_[1][0]+=rhs(1,0);
136  data_[1][1]+=rhs(1,1);
137  data_[1][2]+=rhs(1,2);
138  data_[2][0]+=rhs(2,0);
139  data_[2][1]+=rhs(2,1);
140  data_[2][2]+=rhs(2,2);
141  return *this;
142  }
143  Mat3& operator-=(const Mat3& rhs)
144  {
145  data_[0][0]-=rhs(0,0);
146  data_[0][1]-=rhs(0,1);
147  data_[0][2]-=rhs(0,2);
148  data_[1][0]-=rhs(1,0);
149  data_[1][1]-=rhs(1,1);
150  data_[1][2]-=rhs(1,2);
151  data_[2][0]-=rhs(2,0);
152  data_[2][1]-=rhs(2,1);
153  data_[2][2]-=rhs(2,2);
154  return *this;
155  }
156  Mat3& operator*=(const Real d)
157  {
158  data_[0][0]*=d;
159  data_[0][1]*=d;
160  data_[0][2]*=d;
161  data_[1][0]*=d;
162  data_[1][1]*=d;
163  data_[1][2]*=d;
164  data_[2][0]*=d;
165  data_[2][1]*=d;
166  data_[2][2]*=d;
167  return *this;
168  }
169  Mat3& operator/=(const Real d)
170  {
171  data_[0][0]/=d;
172  data_[0][1]/=d;
173  data_[0][2]/=d;
174  data_[1][0]/=d;
175  data_[1][1]/=d;
176  data_[1][2]/=d;
177  data_[2][0]/=d;
178  data_[2][1]/=d;
179  data_[2][2]/=d;
180  return *this;
181  }
182 
183  Mat3& operator*=(const Mat3& m)
184  {
185  (*this)=Mat3((*this)(0,0)*m(0,0)+(*this)(0,1)*m(1,0)+(*this)(0,2)*m(2,0),
186  (*this)(0,0)*m(0,1)+(*this)(0,1)*m(1,1)+(*this)(0,2)*m(2,1),
187  (*this)(0,0)*m(0,2)+(*this)(0,1)*m(1,2)+(*this)(0,2)*m(2,2),
188  (*this)(1,0)*m(0,0)+(*this)(1,1)*m(1,0)+(*this)(1,2)*m(2,0),
189  (*this)(1,0)*m(0,1)+(*this)(1,1)*m(1,1)+(*this)(1,2)*m(2,1),
190  (*this)(1,0)*m(0,2)+(*this)(1,1)*m(1,2)+(*this)(1,2)*m(2,2),
191  (*this)(2,0)*m(0,0)+(*this)(2,1)*m(1,0)+(*this)(2,2)*m(2,0),
192  (*this)(2,0)*m(0,1)+(*this)(2,1)*m(1,1)+(*this)(2,2)*m(2,1),
193  (*this)(2,0)*m(0,2)+(*this)(2,1)*m(1,2)+(*this)(2,2)*m(2,2));
194  return *this;
195  }
196 
197  Real* Data() {return &data_[0][0];}
198  const Real* Data() const {return &data_[0][0];}
199 
200  geom::Vec3 GetCol(int index) const;
201  geom::Vec3 GetRow(int index) const;
202 private:
203  Real data_[3][3];
204 
205  void set(Real i00, Real i01, Real i02,
206  Real i10, Real i11, Real i12,
207  Real i20, Real i21, Real i22)
208  {
209  data_[0][0]=i00; data_[0][1]=i01; data_[0][2]=i02;
210  data_[1][0]=i10; data_[1][1]=i11; data_[1][2]=i12;
211  data_[2][0]=i20; data_[2][1]=i21; data_[2][2]=i22;
212  }
213 };
214 
215 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& o, const Mat3& m);
216 
217 } // ns geom
218 
219 
220 #endif