$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
algebra/h_mat.hh
1 // Copyright (C) 2007, 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_ALGEBRA_H_MAT_HH
28 # define MLN_ALGEBRA_H_MAT_HH
29 
45 # include <mln/algebra/mat.hh>
46 # include <mln/algebra/quat.hh>
47 
48 # include <mln/math/pi.hh>
49 # include <mln/util/couple.hh>
50 
51 namespace mln
52 {
53 
54  namespace algebra
55  {
56 
60  template <unsigned d, typename T>
61  struct h_mat : public mat<d+1, d+1, T>
62  {
64  enum { N = d,
65  M = d,
66  dim = d * d };
67 
69  h_mat();
71  h_mat(const mat<d+1, d+1, T>& x);
72 
73  };
74 
75 
77  template <typename C>
78  void from_to_(const algebra::h_mat<3,C>& from, algebra::quat& to);
79 
80 
81 # ifndef MLN_INCLUDE_ONLY
82 
83  template <unsigned d, typename T>
84  inline
86  : mat<d+1, d+1, T>(mat<d+1, d+1, T>::Id)
87  {
88  }
89 
90  template <unsigned d, typename T>
91  inline
92  h_mat<d,T>::h_mat(const mat<d+1, d+1, T>& x)
93  : mat<d+1, d+1, T>(x)
94  {
95  }
96 
97 
98  // Conversions
99 
100  template <typename C>
101  void from_to_(const algebra::h_mat<3,C>& from, algebra::quat& to)
102  {
103  C tr = from(0, 0) + from(1, 1) + from(2, 2) + 1;
104 
105  if (tr > 0.005f) // Actually, greater than 0
106  {
107  C s = 0.5 / sqrt(tr),
108  w = 0.25 / s,
109  x = (from(2, 1) - from(1, 2)) * s,
110  y = (from(0, 2) - from(2, 0)) * s,
111  z = (from(1, 0) - from(0, 1)) * s;
112 
113  to = algebra::quat(w, x, y, z);
114  return;
115  }
116 
117  // If the trace of the matrix is less than or equal to zero
118  // then identify which major diagonal element has the greatest
119  // value.
120 
121  C max = 0;
122  unsigned c = 0;
123  for (unsigned d = 0; d <= 3; ++d)
124  if (from(d, d) > max)
125  {
126  max = from(d, d);
127  c = d;
128  }
129 
130  // Depending on this value, calculate the following:
131  C s, w, x, y, z;
132  switch(c)
133  {
134  case 0:
135  s = sqrt(1.0 + from(0, 0) - from(1, 1) - from(2, 2)) * 2;
136  x = 0.5 / s;
137  y = (from(0, 1) + from(1, 0)) / s;
138  z = (from(0, 2) + from(2, 0)) / s;
139  w = (from(1, 2) + from(2, 1)) / s;
140  break;
141 
142  case 1:
143  s = sqrt(1.0 + from(1, 1) - from(0, 0) - from(2, 2)) * 2;
144  x = (from(0, 1) + from(1, 0)) / s;
145  y = 0.5 / s;
146  z = (from(1, 2) + from(2, 1)) / s;
147  w = (from(0, 2) + from(2, 0)) / s;
148  break;
149 
150  case 2:
151  s = sqrt(1.0 + from(2, 2) - from(0, 0) - from(1, 1)) * 2;
152  x = (from(0, 2) + from(2, 0)) / s;
153  y = (from(1, 2) + from(2, 1)) / s;
154  z = 0.5 / s;
155  w = (from(0, 1) + from(1, 0) ) / s;
156  break;
157 
158  // Error case
159  default:
160  x = 0;
161  y = 0;
162  z = 0;
163  w = 0;
164  }
165 
166  to = algebra::quat(w, x, y, z);
167  return;
168  }
169 
170 
171 # endif // ! MLN_INCLUDE_ONLY
172 
173  } // end of namespace mln::algebra
174 
175 } // end of namespace mln
176 
177 
178 
179 #endif // ! MLN_ALGEBRA_H_MAT_HH