vmmlib  1.7.0
 All Classes Namespaces Functions Pages
validator.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 
33 /* @author Susanne Suter
34  *
35  * Tool to validate a vector, a matrix or a tensor
36  *
37  */
38 
39 #ifndef __VMML__VALIDATOR__HPP__
40 #define __VMML__VALIDATOR__HPP__
41 
42 #include <vmmlib/tensor3.hpp>
43 
44 
45 namespace vmml
46 {
47 
48  class validator
49  {
50  public:
51 
52 
53  template< size_t M, typename T >
54  static bool is_valid( const vector< M, T >& data_ );
55 
56  template< size_t M, size_t N, typename T >
57  static bool is_valid( const matrix< M, N, T >& data_ );
58 
59  template< size_t M, size_t N, size_t L, typename T >
60  static bool is_valid( const tensor3< M, N, L, T >& data_ );
61 
62 
63  }; //end validator class
64 
65 #define VMML_TEMPLATE_CLASSNAME validator
66 
67 
68 
69 template< size_t M, size_t N, size_t L, typename T >
70 bool
71 VMML_TEMPLATE_CLASSNAME::is_valid( const tensor3< M, N, L, T >& data_ )
72 {
73  typedef typename tensor3< M, N, L, T>::const_iterator const_t3_iterator;
74  bool valid = true;
75  for( const_t3_iterator it = data_.begin(); valid && it != data_.end(); ++it )
76  {
77  if ( std::isnan( *it ) )
78  valid = false;
79  if ( std::isinf( *it ) )
80  valid = false;
81  }
82 
83 #ifdef VMMLIB_THROW_EXCEPTIONS
84  if ( ! valid )
85  VMMLIB_ERROR( "vector contains nan or inf.", VMMLIB_HERE );
86 #endif
87 
88  return valid;
89 
90 
91 }
92 
93 
94 template< size_t M, size_t N, typename T >
95 bool
96 VMML_TEMPLATE_CLASSNAME::is_valid( const matrix< M, N, T >& data_ )
97 {
98  typedef typename matrix< M, N, T>::const_iterator const_m_iterator;
99 
100  bool valid = true;
101  for( const_m_iterator it = data_.begin(); valid && it != data_.end(); ++it )
102  {
103  if ( std::isnan( *it ) )
104  valid = false;
105  if ( std::isinf( *it ) )
106  valid = false;
107  }
108 
109 #ifdef VMMLIB_THROW_EXCEPTIONS
110  if ( ! valid )
111  VMMLIB_ERROR( "matrix contains nan or inf.", VMMLIB_HERE );
112 #endif
113 
114  return valid;
115 
116 }
117 
118 template< size_t M, typename T >
119 bool
120 VMML_TEMPLATE_CLASSNAME::is_valid( const vector< M, T >& data_ )
121 {
122  typedef typename vector< M, T>::const_iterator const_v_iterator;
123  bool valid = true;
124  for( const_v_iterator it = data_.begin(); valid && it != data_.end(); ++it )
125  {
126  if ( std::isnan( *it ) )
127  valid = false;
128  if ( std::isinf( *it ) )
129  valid = false;
130  }
131 
132 #ifdef VMMLIB_THROW_EXCEPTIONS
133  if ( ! valid )
134  VMMLIB_ERROR( "vector contains nan or inf.", VMMLIB_HERE );
135 #endif
136 
137  return valid;
138 
139 }
140 
141 #undef VMML_TEMPLATE_CLASSNAME
142 
143 
144 } // namespace vmml
145 
146 
147 
148 #endif
149 
150