OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundsChecking.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_BOUNDSCHECKING
15 #define OSG_BOUNDSCHECKING 1
16 
17 #include <osg/Notify>
18 
19 namespace osg {
20 
21 
25 template <typename T>
26 inline void clampGEQUAL(T& value,const T minValue,const char* valueName)
27 {
28  if (value<minValue)
29  {
30  notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
31  value = minValue;
32  }
33 }
34 
38 template <typename T>
39 inline void clampLEQUAL(T& value,const T maxValue,const char* valueName)
40 {
41  if (value>maxValue)
42  {
43  notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
44  value = maxValue;
45  }
46 }
47 
53 template <typename T>
54 inline void clampBetweenRange(T& value,const T minValue,const T maxValue,const char* valueName)
55 {
56  if (value<minValue)
57  {
58  notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
59  value = minValue;
60  }
61  else
62  if (value>maxValue)
63  {
64  notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
65  value = maxValue;
66  }
67 
68 }
69 
73 template <typename A, typename T>
74 inline void clampArrayElementGEQUAL(A& value,unsigned int i,const T minValue,const char* valueName)
75 {
76  if (value[i]<minValue)
77  {
78  notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
79  value[i] = minValue;
80  }
81 }
82 
86 template <typename A, typename T>
87 inline void clampArrayElementLEQUAL(A& value,unsigned int i,const T maxValue,const char* valueName)
88 {
89  if (value[i]>maxValue)
90  {
91  notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
92  value = maxValue;
93  }
94 }
95 
101 template <typename A, typename T>
102 inline void clampArrayElementBetweenRange(A& value,unsigned int i,const T minValue,const T maxValue,const char* valueName)
103 {
104  if (value[i]<minValue)
105  {
106  notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
107  value[i] = minValue;
108  }
109  else
110  if (value[i]>maxValue)
111  {
112  notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
113  value[i] = maxValue;
114  }
115 
116 }
117 
121 template <typename A, typename T>
122 inline void clampArrayElementsGEQUAL(A& value,unsigned int first,unsigned int last,const T minValue,const char* valueName)
123 {
124  for(unsigned int i=first;i<=last;++i)
125  clampArrayElementGEQUAL(value,i,minValue,valueName);
126 }
127 
131 template <typename A, typename T>
132 inline void clampArrayElementsLEQUAL(A& value,unsigned int first,unsigned int last,const T maxValue,const char* valueName)
133 {
134  for(unsigned int i=first;i<=last;++i)
135  clampArrayElementLEQUAL(value,i,maxValue,valueName);
136 }
137 
144 template <typename A, typename T>
145 inline void clampArrayElementsBetweenRange(A& value,unsigned int first,unsigned int last,const T minValue,const T maxValue,const char* valueName)
146 {
147  for(unsigned int i=first;i<=last;++i)
148  clampArrayElementBetweenRange(value,i,minValue,maxValue,valueName);
149 }
150 
151 
155 template <typename A, typename T>
156 inline void clampArray3GEQUAL(A& value,const T minValue,const char* valueName)
157 {
158  clampArrayElementsGEQUAL(value,0u,2u,minValue,valueName);
159 }
160 
164 template <typename A, typename T>
165 inline void clampArray3LEQUAL(A& value,const T maxValue,const char* valueName)
166 {
167  clampArrayElementsLEQUAL(value,0u,2u,maxValue,valueName);
168 }
169 
176 template <typename A, typename T>
177 inline void clampArray3BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
178 {
179  clampArrayElementsBetweenRange(value,0u,2u,minValue,maxValue,valueName);
180 }
181 
182 
183 
187 template <typename A, typename T>
188 inline void clampArray4GEQUAL(A& value,const T minValue,const char* valueName)
189 {
190  clampArrayElementsGEQUAL(value,0u,3u,minValue,valueName);
191 }
192 
196 template <typename A, typename T>
197 inline void clampArray4LEQUAL(A& value,const T maxValue,const char* valueName)
198 {
199  clampArrayElementsLEQUAL(value,0u,3u,maxValue,valueName);
200 }
201 
208 template <typename A, typename T>
209 inline void clampArray4BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
210 {
211  clampArrayElementsBetweenRange(value,0u,3u,minValue,maxValue,valueName);
212 }
213 
214 }
215 
216 #endif
void clampArrayElementsLEQUAL(A &value, unsigned int first, unsigned int last, const T maxValue, const char *valueName)
void clampArray3GEQUAL(A &value, const T minValue, const char *valueName)
void clampArray3LEQUAL(A &value, const T maxValue, const char *valueName)
void clampArray4LEQUAL(A &value, const T maxValue, const char *valueName)
void clampArray3BetweenRange(A &value, const T minValue, const T maxValue, const char *valueName)
void clampBetweenRange(T &value, const T minValue, const T maxValue, const char *valueName)
void clampArray4GEQUAL(A &value, const T minValue, const char *valueName)
void clampArrayElementGEQUAL(A &value, unsigned int i, const T minValue, const char *valueName)
void clampArray4BetweenRange(A &value, const T minValue, const T maxValue, const char *valueName)
void clampLEQUAL(T &value, const T maxValue, const char *valueName)
void clampGEQUAL(T &value, const T minValue, const char *valueName)
void clampArrayElementLEQUAL(A &value, unsigned int i, const T maxValue, const char *valueName)
Definition: AlphaFunc.h:19
OSG_EXPORT std::ostream & notify(const NotifySeverity severity)
void clampArrayElementBetweenRange(A &value, unsigned int i, const T minValue, const T maxValue, const char *valueName)
void clampArrayElementsGEQUAL(A &value, unsigned int first, unsigned int last, const T minValue, const char *valueName)
void clampArrayElementsBetweenRange(A &value, unsigned int first, unsigned int last, const T minValue, const T maxValue, const char *valueName)