OpenStructure
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 
26 #include <boost/operators.hpp>
27 
29 #include <ost/geom/mat2.hh>
30 namespace geom {
31 
32 class Vec3;
33 
35  private boost::equality_comparable<Mat3>,
36  private boost::additive1<Mat3>,
37  private boost::multiplicative2<Mat3, Real>
38 {
39 public:
41  Mat3()
42  {
43  this->set(1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0);
44  }
46 
55  Mat3(Real i00, Real i01, Real i02,
56  Real i10, Real i11, Real i12,
57  Real i20, Real i21, Real i22)
58  {
59  this->set(i00,i01,i02,i10,i11,i12,i20,i21,i22);
60  }
61 
62  Mat3(const Mat3& m)
63  {
64  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));
65  }
66 
67  Mat3(const Mat2& m)
68  {
69  this->set(m(0, 0), m(0, 1), Real(0.0),
70  m(1, 0), m(1, 1), Real(0.0),
71  Real(0.0), Real(0.0), Real(1.0));
72  }
73 
74  explicit Mat3(const Real arr[9])
75  {
76  this->set(arr[0],arr[1],arr[2],arr[3],arr[4],arr[5],arr[6],arr[7],arr[8]);
77  }
78 
79  explicit Mat3(Real x, Real y, Real z)
80  {
81  this->set(x, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, z);
82  }
84  Real& operator()(std::size_t r, std::size_t c)
85  {
86  assert(r<=2 && c<=2);
87  return data_[r][c];
88  }
90  const Real& operator()(std::size_t r, std::size_t c) const
91  {
92  assert(r<=2 && c<=2);
93  return data_[r][c];
94  }
95 
96  Mat3& operator=(const Mat3& m)
97  {
98  if(&m!=this) {
99  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));
100  }
101  return *this;
102  }
103 
104  static Mat3 Identity()
105  {
106  static Mat3 i(1.0,0.0,0.0,
107  0.0,1.0,0.0,
108  0.0,0.0,1.0);
109  return i;
110  }
111 
112  bool operator==(const Mat3& rhs) const
113  {
114  return data_[0][0] == rhs.data_[0][0] &&
115  data_[1][0] == rhs.data_[1][0] &&
116  data_[2][0] == rhs.data_[2][0] &&
117  data_[0][1] == rhs.data_[0][1] &&
118  data_[1][1] == rhs.data_[1][1] &&
119  data_[2][1] == rhs.data_[2][1] &&
120  data_[0][2] == rhs.data_[0][2] &&
121  data_[1][2] == rhs.data_[1][2] &&
122  data_[2][2] == rhs.data_[2][2];
123  }
124 
125  Mat3& operator+=(const Mat3& rhs)
126  {
127  data_[0][0]+=rhs(0,0);
128  data_[0][1]+=rhs(0,1);
129  data_[0][2]+=rhs(0,2);
130  data_[1][0]+=rhs(1,0);
131  data_[1][1]+=rhs(1,1);
132  data_[1][2]+=rhs(1,2);
133  data_[2][0]+=rhs(2,0);
134  data_[2][1]+=rhs(2,1);
135  data_[2][2]+=rhs(2,2);
136  return *this;
137  }
138  Mat3& operator-=(const Mat3& rhs)
139  {
140  data_[0][0]-=rhs(0,0);
141  data_[0][1]-=rhs(0,1);
142  data_[0][2]-=rhs(0,2);
143  data_[1][0]-=rhs(1,0);
144  data_[1][1]-=rhs(1,1);
145  data_[1][2]-=rhs(1,2);
146  data_[2][0]-=rhs(2,0);
147  data_[2][1]-=rhs(2,1);
148  data_[2][2]-=rhs(2,2);
149  return *this;
150  }
151  Mat3& operator*=(const Real d)
152  {
153  data_[0][0]*=d;
154  data_[0][1]*=d;
155  data_[0][2]*=d;
156  data_[1][0]*=d;
157  data_[1][1]*=d;
158  data_[1][2]*=d;
159  data_[2][0]*=d;
160  data_[2][1]*=d;
161  data_[2][2]*=d;
162  return *this;
163  }
164  Mat3& operator/=(const Real d)
165  {
166  data_[0][0]/=d;
167  data_[0][1]/=d;
168  data_[0][2]/=d;
169  data_[1][0]/=d;
170  data_[1][1]/=d;
171  data_[1][2]/=d;
172  data_[2][0]/=d;
173  data_[2][1]/=d;
174  data_[2][2]/=d;
175  return *this;
176  }
177 
178  Mat3& operator*=(const Mat3& m)
179  {
180  (*this)=Mat3((*this)(0,0)*m(0,0)+(*this)(0,1)*m(1,0)+(*this)(0,2)*m(2,0),
181  (*this)(0,0)*m(0,1)+(*this)(0,1)*m(1,1)+(*this)(0,2)*m(2,1),
182  (*this)(0,0)*m(0,2)+(*this)(0,1)*m(1,2)+(*this)(0,2)*m(2,2),
183  (*this)(1,0)*m(0,0)+(*this)(1,1)*m(1,0)+(*this)(1,2)*m(2,0),
184  (*this)(1,0)*m(0,1)+(*this)(1,1)*m(1,1)+(*this)(1,2)*m(2,1),
185  (*this)(1,0)*m(0,2)+(*this)(1,1)*m(1,2)+(*this)(1,2)*m(2,2),
186  (*this)(2,0)*m(0,0)+(*this)(2,1)*m(1,0)+(*this)(2,2)*m(2,0),
187  (*this)(2,0)*m(0,1)+(*this)(2,1)*m(1,1)+(*this)(2,2)*m(2,1),
188  (*this)(2,0)*m(0,2)+(*this)(2,1)*m(1,2)+(*this)(2,2)*m(2,2));
189  return *this;
190  }
191 
192  Real* Data() {return &data_[0][0];}
193  const Real* Data() const {return &data_[0][0];}
194 
195  geom::Vec3 GetCol(int index) const;
196  geom::Vec3 GetRow(int index) const;
197 private:
198  Real data_[3][3];
199 
200  void set(Real i00, Real i01, Real i02,
201  Real i10, Real i11, Real i12,
202  Real i20, Real i21, Real i22)
203  {
204  data_[0][0]=i00; data_[0][1]=i01; data_[0][2]=i02;
205  data_[1][0]=i10; data_[1][1]=i11; data_[1][2]=i12;
206  data_[2][0]=i20; data_[2][1]=i21; data_[2][2]=i22;
207  }
208 };
209 
210 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& o, const Mat3& m);
211 
212 } // ns geom
213 
214 
215 #endif