OpenStructure
mat2.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_MAT2_HH
20 #define GEOM_MAT2_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 
31 namespace geom {
32 
34  private boost::equality_comparable<Mat2>,
35  private boost::additive<Mat2>,
36  private boost::multiplicative2<Mat2, Real>
37 {
38 public:
39  static Mat2 Identity();
40 
42  Mat2();
43 
45 
53  Mat2(Real i00, Real i01, Real i10, Real i11);
54 
56  Mat2(const Mat2& m);
57 
59  explicit Mat2(const Real[4]);
60 
62  Mat2& operator=(const Mat2& m);
63 
65  bool operator==(const Mat2& rhs) const;
66 
67 
69  const Real& At(std::size_t r, std::size_t c) const
70  {
71  if (r>1 || c>1) {
72  throw std::out_of_range("indices must be smaller than 2");
73  }
74  return data_[r][c];
75  }
76 
78  Real& At(std::size_t r, std::size_t c)
79  {
80  if (r>1 || c>1) {
81  throw std::out_of_range("indices must be smaller than 2");
82  }
83  return data_[r][c];
84  }
85 
87  Real& operator()(std::size_t r, std::size_t c)
88  {
89  assert(r<2 && c<2);
90  return data_[r][c];
91  }
93  const Real& operator()(std::size_t r, std::size_t c) const
94  {
95  assert(r<2 && c<2);
96  return data_[r][c];
97  }
98 
100  Mat2& operator+=(const Mat2& rhs);
102  Mat2& operator-=(const Mat2& rhs);
103 
104  Mat2& operator*=(const Real d);
105  Mat2& operator/=(const Real d);
106 
107  Mat2& operator*=(const Mat2& m);
108 
109  Real* Data() {return &data_[0][0];}
110  const Real* Data() const {return &data_[0][0];}
111 
112 private:
113  Real data_[2][2];
114 
115  void set(Real i00, Real i01, Real i10, Real i11);
116 };
117 
118 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& os, const Mat2& m);
119 
120 } // ns geom
121 
122 
123 #endif
Mat2 & operator+=(const Mat2 &rhs)
addable op
Mat2()
Default initialization, identity matrix.
Mat2(Real i00, Real i01, Real i10, Real i11)
In with 4 values in row-major order.
Mat2 & operator/=(const Real d)
bool operator==(const Mat2 &rhs) const
comparable
Mat2(const Real[4])
initialization from array
const Real & operator()(std::size_t r, std::size_t c) const
const element access
Definition: mat2.hh:93
const Real * Data() const
Definition: mat2.hh:110
const Real & At(std::size_t r, std::size_t c) const
const element access
Definition: mat2.hh:69
static Mat2 Identity()
Mat2(const Mat2 &m)
Copy ctor.
Mat2 & operator*=(const Mat2 &m)
Real * Data()
Definition: mat2.hh:109
Mat2 & operator*=(const Real d)
Real & At(std::size_t r, std::size_t c)
element access
Definition: mat2.hh:78
Real & operator()(std::size_t r, std::size_t c)
element access
Definition: mat2.hh:87
Mat2 & operator=(const Mat2 &m)
assignement op
Mat2 & operator-=(const Mat2 &rhs)
subtractable op
#define DLLEXPORT_OST_GEOM
float Real
Definition: base.hh:44
std::ostream & operator<<(std::ostream &os, const AlignedCuboid &c)