FCL  0.6.0
Flexible Collision Library
interval_matrix-inl.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 // This code is based on code developed by Stephane Redon at UNC and Inria for the CATCH library: http://graphics.ewha.ac.kr/CATCH/
38 #ifndef FCL_CCD_INTERVAL_MATRIX_INL_H
39 #define FCL_CCD_INTERVAL_MATRIX_INL_H
40 
41 #include "fcl/math/motion/taylor_model/interval_matrix.h"
42 
43 namespace fcl
44 {
45 
46 //==============================================================================
47 extern template
48 struct IMatrix3<double>;
49 
50 //==============================================================================
51 extern template
52 IMatrix3<double> rotationConstrain(const IMatrix3<double>& m);
53 
54 //==============================================================================
55 template <typename S>
56 IMatrix3<S>::IMatrix3() {}
57 
58 //==============================================================================
59 template <typename S>
60 IMatrix3<S>::IMatrix3(S v)
61 {
62  v_[0].setValue(v);
63  v_[1].setValue(v);
64  v_[2].setValue(v);
65 }
66 
67 //==============================================================================
68 template <typename S>
69 IMatrix3<S>::IMatrix3(const Matrix3<S>& m)
70 {
71  v_[0].setValue(m.row(0)[0], m.row(0)[1], m.row(0)[2]);
72  v_[1].setValue(m.row(1)[0], m.row(1)[1], m.row(1)[2]);
73  v_[2].setValue(m.row(2)[0], m.row(2)[1], m.row(2)[2]);
74 }
75 
76 //==============================================================================
77 template <typename S>
78 IMatrix3<S>::IMatrix3(S m[3][3][2])
79 {
80  v_[0].setValue(m[0]);
81  v_[1].setValue(m[1]);
82  v_[2].setValue(m[2]);
83 }
84 
85 //==============================================================================
86 template <typename S>
87 IMatrix3<S>::IMatrix3(S m[3][3])
88 {
89  v_[0].setValue(m[0]);
90  v_[1].setValue(m[1]);
91  v_[2].setValue(m[2]);
92 }
93 
94 //==============================================================================
95 template <typename S>
96 IMatrix3<S>::IMatrix3(Interval<S> m[3][3])
97 {
98  v_[0].setValue(m[0]);
99  v_[1].setValue(m[1]);
100  v_[2].setValue(m[2]);
101 }
102 
103 //==============================================================================
104 template <typename S>
105 IMatrix3<S>::IMatrix3(const IVector3<S>& v1, const IVector3<S>& v2, const IVector3<S>& v3)
106 {
107  v_[0] = v1;
108  v_[1] = v2;
109  v_[2] = v3;
110 }
111 
112 //==============================================================================
113 template <typename S>
114 void IMatrix3<S>::setIdentity()
115 {
116  v_[0].setValue(1, 0, 0);
117  v_[1].setValue(0, 1, 0);
118  v_[2].setValue(0, 0, 1);
119 }
120 
121 //==============================================================================
122 template <typename S>
123 IVector3<S> IMatrix3<S>::getColumn(size_t i) const
124 {
125  return IVector3<S>(v_[0][i], v_[1][i], v_[2][i]);
126 }
127 
128 //==============================================================================
129 template <typename S>
130 const IVector3<S>& IMatrix3<S>::getRow(size_t i) const
131 {
132  return v_[i];
133 }
134 
135 //==============================================================================
136 template <typename S>
137 Vector3<S> IMatrix3<S>::getColumnLow(size_t i) const
138 {
139  return Vector3<S>(v_[0][i][0], v_[1][i][0], v_[2][i][0]);
140 }
141 
142 //==============================================================================
143 template <typename S>
144 Vector3<S> IMatrix3<S>::getRowLow(size_t i) const
145 {
146  return Vector3<S>(v_[i][0][0], v_[i][1][0], v_[i][2][0]);
147 }
148 
149 //==============================================================================
150 template <typename S>
151 Vector3<S> IMatrix3<S>::getColumnHigh(size_t i) const
152 {
153  return Vector3<S>(v_[0][i][1], v_[1][i][1], v_[2][i][1]);
154 }
155 
156 //==============================================================================
157 template <typename S>
158 Vector3<S> IMatrix3<S>::getRowHigh(size_t i) const
159 {
160  return Vector3<S>(v_[i][0][1], v_[i][1][1], v_[i][2][1]);
161 }
162 
163 //==============================================================================
164 template <typename S>
165 Matrix3<S> IMatrix3<S>::getLow() const
166 {
167  Matrix3<S> m;
168  m << v_[0][0][1], v_[0][1][1], v_[0][2][1],
169  v_[1][0][1], v_[1][1][1], v_[1][2][1],
170  v_[2][0][1], v_[2][1][1], v_[2][2][1];
171  return m;
172 }
173 
174 //==============================================================================
175 template <typename S>
176 Matrix3<S> IMatrix3<S>::getHigh() const
177 {
178  Matrix3<S> m;
179  m << v_[0][0][1], v_[0][1][1], v_[0][2][1],
180  v_[1][0][1], v_[1][1][1], v_[1][2][1],
181  v_[2][0][1], v_[2][1][1], v_[2][2][1];
182  return m;
183 }
184 
185 //==============================================================================
186 template <typename S>
187 const Interval<S>& IMatrix3<S>::operator ()(size_t i, size_t j) const
188 {
189  return v_[i][j];
190 }
191 
192 //==============================================================================
193 template <typename S>
194 Interval<S>& IMatrix3<S>::operator ()(size_t i, size_t j)
195 {
196  return v_[i][j];
197 }
198 
199 //==============================================================================
200 template <typename S>
201 IMatrix3<S> IMatrix3<S>::operator * (const Matrix3<S>& m) const
202 {
203  return IMatrix3(IVector3<S>(v_[0].dot(m.col(0)), v_[0].dot(m.col(1)), v_[0].dot(m.col(2))),
204  IVector3<S>(v_[1].dot(m.col(0)), v_[1].dot(m.col(1)), v_[1].dot(m.col(2))),
205  IVector3<S>(v_[2].dot(m.col(0)), v_[2].dot(m.col(1)), v_[2].dot(m.col(2))));
206 }
207 
208 //==============================================================================
209 template <typename S>
210 IVector3<S> IMatrix3<S>::operator * (const Vector3<S>& v) const
211 {
212  return IVector3<S>(v_[0].dot(v), v_[1].dot(v), v_[2].dot(v));
213 }
214 
215 //==============================================================================
216 template <typename S>
217 IVector3<S> IMatrix3<S>::operator * (const IVector3<S>& v) const
218 {
219  return IVector3<S>(v_[0].dot(v), v_[1].dot(v), v_[2].dot(v));
220 }
221 
222 //==============================================================================
223 template <typename S>
224 IMatrix3<S> IMatrix3<S>::operator * (const IMatrix3<S>& m) const
225 {
226  const IVector3<S>& mc0 = m.getColumn(0);
227  const IVector3<S>& mc1 = m.getColumn(1);
228  const IVector3<S>& mc2 = m.getColumn(2);
229 
230  return IMatrix3(IVector3<S>(v_[0].dot(mc0), v_[0].dot(mc1), v_[0].dot(mc2)),
231  IVector3<S>(v_[1].dot(mc0), v_[1].dot(mc1), v_[1].dot(mc2)),
232  IVector3<S>(v_[2].dot(mc0), v_[2].dot(mc1), v_[2].dot(mc2)));
233 }
234 
235 //==============================================================================
236 template <typename S>
237 IMatrix3<S>& IMatrix3<S>::operator *= (const Matrix3<S>& m)
238 {
239  v_[0].setValue(v_[0].dot(m.col(0)), v_[0].dot(m.col(1)), v_[0].dot(m.col(2)));
240  v_[1].setValue(v_[1].dot(m.col(0)), v_[1].dot(m.col(1)), v_[1].dot(m.col(2)));
241  v_[2].setValue(v_[2].dot(m.col(0)), v_[2].dot(m.col(1)), v_[2].dot(m.col(2)));
242  return *this;
243 }
244 
245 //==============================================================================
246 template <typename S>
247 IMatrix3<S>& IMatrix3<S>::operator *= (const IMatrix3<S>& m)
248 {
249  const IVector3<S>& mc0 = m.getColumn(0);
250  const IVector3<S>& mc1 = m.getColumn(1);
251  const IVector3<S>& mc2 = m.getColumn(2);
252 
253  v_[0].setValue(v_[0].dot(mc0), v_[0].dot(mc1), v_[0].dot(mc2));
254  v_[1].setValue(v_[1].dot(mc0), v_[1].dot(mc1), v_[1].dot(mc2));
255  v_[2].setValue(v_[2].dot(mc0), v_[2].dot(mc1), v_[2].dot(mc2));
256  return *this;
257 }
258 
259 //==============================================================================
260 template <typename S>
261 IMatrix3<S> IMatrix3<S>::operator + (const IMatrix3<S>& m) const
262 {
263  return IMatrix3(v_[0] + m.v_[0], v_[1] + m.v_[1], v_[2] + m.v_[2]);
264 }
265 
266 //==============================================================================
267 template <typename S>
268 IMatrix3<S>& IMatrix3<S>::operator += (const IMatrix3<S>& m)
269 {
270  v_[0] += m.v_[0];
271  v_[1] += m.v_[1];
272  v_[2] += m.v_[2];
273  return *this;
274 }
275 
276 //==============================================================================
277 template <typename S>
278 IMatrix3<S> IMatrix3<S>::operator - (const IMatrix3<S>& m) const
279 {
280  return IMatrix3(v_[0] - m.v_[0], v_[1] - m.v_[1], v_[2] - m.v_[2]);
281 }
282 
283 //==============================================================================
284 template <typename S>
285 IMatrix3<S>& IMatrix3<S>::operator -= (const IMatrix3<S>& m)
286 {
287  v_[0] -= m.v_[0];
288  v_[1] -= m.v_[1];
289  v_[2] -= m.v_[2];
290  return *this;
291 }
292 
293 //==============================================================================
294 template <typename S>
295 IMatrix3<S>& IMatrix3<S>::rotationConstrain()
296 {
297  for(std::size_t i = 0; i < 3; ++i)
298  {
299  for(std::size_t j = 0; j < 3; ++j)
300  {
301  if(v_[i][j][0] < -1) v_[i][j][0] = -1;
302  else if(v_[i][j][0] > 1) v_[i][j][0] = 1;
303 
304  if(v_[i][j][1] < -1) v_[i][j][1] = -1;
305  else if(v_[i][j][1] > 1) v_[i][j][1] = 1;
306  }
307  }
308 
309  return *this;
310 }
311 
312 //==============================================================================
313 template <typename S>
314 void IMatrix3<S>::print() const
315 {
316  std::cout << "[" << v_[0][0][0] << "," << v_[0][0][1] << "]" << " [" << v_[0][1][0] << "," << v_[0][1][1] << "]" << " [" << v_[0][2][0] << "," << v_[0][2][1] << "]" << std::endl;
317  std::cout << "[" << v_[1][0][0] << "," << v_[1][0][1] << "]" << " [" << v_[1][1][0] << "," << v_[1][1][1] << "]" << " [" << v_[1][2][0] << "," << v_[1][2][1] << "]" << std::endl;
318  std::cout << "[" << v_[2][0][0] << "," << v_[2][0][1] << "]" << " [" << v_[2][1][0] << "," << v_[2][1][1] << "]" << " [" << v_[2][2][0] << "," << v_[2][2][1] << "]" << std::endl;
319 }
320 
321 //==============================================================================
322 template <typename S>
323 IMatrix3<S> rotationConstrain(const IMatrix3<S>& m)
324 {
325  IMatrix3<S> res;
326  for(std::size_t i = 0; i < 3; ++i)
327  {
328  for(std::size_t j = 0; j < 3; ++j)
329  {
330  if(m(i, j)[0] < -1) res(i, j)[0] = -1;
331  else if(m(i, j)[0] > 1) res(i, j)[0] = 1;
332 
333  if(m(i, j)[1] < -1) res(i, j)[1] = -1;
334  else if(m(i, j)[1] > 1) res(i, j)[1] = 1;
335  }
336  }
337 
338  return res;
339 }
340 
341 } // namespace fcl
342 
343 #endif
Main namespace.
Definition: broadphase_bruteforce-inl.h:45