OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mat4.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_MAT4_HH
20 #define GEOM_MAT4_HH
21 
22 #include <cassert>
23 #include <cstddef> // for size_t
24 #include <ostream>
25 #include <vector>
26 
27 #include <boost/operators.hpp>
28 
30 
31 namespace geom {
32 
33 // fw decl
34 class Mat2;
35 class Mat3;
36 class Vec3;
37 
39  private boost::equality_comparable<Mat4>,
40  private boost::additive<Mat4>,
41  private boost::multiplicative<Mat4, Real>
42 {
43 public:
44  static Mat4 Identity();
45 
47  Mat4();
48 
50 
61  Mat4(Real i00, Real i01, Real i02, Real i03, Real i10, Real i11, Real i12, Real i13,
62  Real i20, Real i21, Real i22, Real i23, Real i30, Real i31, Real i32, Real i33);
64  Mat4(const Mat4& m);
65 
67  explicit Mat4(const Mat2& m);
68 
70  explicit Mat4(const Mat3& m);
71 
73  explicit Mat4(const float[16]);
74 
75  explicit Mat4(const double[16]);
77  Mat4& operator=(const Mat4& m);
78 
80  bool operator==(const Mat4& rhs) const;
81 
82  const Real& At(std::size_t r, std::size_t c) const
83  {
84  if (r>3 || c>3) {
85  throw std::out_of_range("indices must be smaller than 4");
86  }
87  return data_[r][c];
88  }
89 
90  Real& At(std::size_t r, std::size_t c)
91  {
92  if (r>3 || c>3) {
93  throw std::out_of_range("indices must be smaller than 4");
94  }
95  return data_[r][c];
96  }
97 
98  Real& operator()(std::size_t r, std::size_t c)
99  {
100  assert(r<4 && c < 4);
101  return data_[r][c];
102  }
103 
104  const Real& operator()(std::size_t r, std::size_t c) const
105  {
106  assert(r<4 && c < 4);
107  return data_[r][c];
108  }
109 
111  Mat4& operator+=(const Mat4& rhs);
113  Mat4& operator-=(const Mat4& rhs);
114 
115  Mat4& operator*=(const Real d);
116  Mat4& operator/=(const Real d);
117 
118  Mat4& operator*=(const Mat4& m);
119 
120  Mat3 ExtractRotation() const;
121  void PasteRotation(const Mat3& m);
122  Vec3 ExtractTranslation() const;
123  void PasteTranslation(const Vec3& v);
124 
125  Real* Data() {return &data_[0][0];}
126  const Real* Data() const {return &data_[0][0];}
127 
128 private:
129  Real data_[4][4];
130 
131  void set(float i00, float i01, float i02, float i03, float i10, float i11, float i12,
132 float i13, float i20, float i21, float i22, float i23, float i30, float i31, float i32,
133 float i33);
134  void set(double i00, double i01, double i02, double i03, double i10, double i11, double
135 i12, double i13, double i20, double i21, double i22, double i23, double i30, double i31,
136 double i32, double i33);
137 
138 };
139 
140 typedef std::vector<Mat4> Mat4List;
141 
142 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& os, const Mat4& m);
143 
144 } // ns geom
145 
146 
147 #endif