vmmlib  1.7.0
 All Classes Namespaces Functions Pages
matrix_functors.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef __VMML__MATRIX_FUNCTORS__HPP__
33 #define __VMML__MATRIX_FUNCTORS__HPP__
34 
35 #include <vmmlib/enable_if.hpp>
36 
37 #include <cstddef>
38 #include <functional>
39 
40 namespace vmml
41 {
42 
43 template< typename T >
45 {
46  inline void operator()( T& matrix_ ) const
47  {
48  matrix_ = static_cast< typename T::value_type >( 0.0 );
49  }
50 
51 }; // struct set_to_zero
52 
53 
54 template< typename T >
56 {
57  inline
58  typename enable_if< T::ROWS == T::COLS >::type*
59  operator()( T& matrix_ )
60  {
61  set_to_zero_functor< T >()( matrix_ );
62  for( size_t index = 0; index < T::ROWS; ++index )
63  {
64  matrix_( index, index ) = static_cast< typename T::value_type >( 1.0 );
65  }
66  return 0; // for sfinae
67  }
68 }; // struct set_to_identity
69 
70 
71 // this functor compares to matrices, and also returns true/equal if
72 // the matrices have the same values but some rows/columns are inverted
73 template< typename T >
74 struct matrix_equals_allow_inverted_rows : std::binary_function< const T&, const T&, bool >
75 {
76 
77  bool operator()( const T& matrix0, const T& matrix1 )
78  {
79  const size_t r = matrix0.get_number_of_rows();
80  bool ok = true;
81  for( size_t index = 0; ok && index < r; ++index )
82  {
83  if ( matrix0.get_row( index ) != matrix1.get_row( index )
84  && matrix0.get_row( index ) != - matrix1.get_row( index ) )
85  {
86  ok = false;
87  }
88 
89  }
90  return ok;
91  }
92 
93  bool operator()( const T& matrix0, const T& matrix1, typename T::value_type tolerance )
94  {
95  const size_t r = matrix0.get_number_of_rows();
96  bool ok = true;
97  for( size_t index = 0; ok && index < r; ++index )
98  {
99  if (
100  ! matrix0.get_row( index ).equals( matrix1.get_row( index ), tolerance )
101  && ! matrix0.get_row( index ).equals( - matrix1.get_row( index ), tolerance )
102  )
103  {
104  ok = false;
105  }
106 
107  }
108  return ok;
109  }
110 
111 
112 }; // struct matrix_equals_allow_inverted_rows
113 
114 template< typename T >
115 struct matrix_equals_allow_inverted_columns : std::binary_function< const T&, const T&, bool >
116 {
117 
118  bool operator()( const T& matrix0, const T& matrix1 )
119  {
120  const size_t r = matrix0.get_number_of_columns();
121  bool ok = true;
122  for( size_t index = 0; ok && index < r; ++index )
123  {
124  if ( matrix0.get_column( index ) != matrix1.get_column( index )
125  && matrix0.get_column( index ) != - matrix1.get_column( index ) )
126  {
127  ok = false;
128  }
129 
130  }
131  return ok;
132  }
133 
134  bool operator()( const T& matrix0, const T& matrix1, typename T::value_type tolerance )
135  {
136  const size_t r = matrix0.get_number_of_columns();
137  bool ok = true;
138  for( size_t index = 0; ok && index < r; ++index )
139  {
140  if (
141  ! matrix0.get_column( index ).equals( matrix1.get_column( index ), tolerance )
142  && ! matrix0.get_column( index ).equals( - matrix1.get_column( index ), tolerance )
143  )
144  {
145  ok = false;
146  }
147 
148  }
149  return ok;
150  }
151 
152 
153 }; // struct matrix_equals_allow_inverted_columns
154 
155 
156 } // namespace vmml
157 
158 #endif