1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSG_BOUNDSCHECKING
15#define OSG_BOUNDSCHECKING 1
22/** If value is greater than or equal to minValue do nothing - legal value,
23 * Otherwise set value to minValue, and warn that valueName was clamped.
24 * Note this is effectively A=max(A,B). */
26inline void clampGEQUAL(T& value,const T minValue,const char* valueName)
30 notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
35/** If value is less than or equal to maxValue do nothing - legal value,
36 * Otherwise set value to maxValue, and warn that valueName was clamped.
37 * Note this is effectively A=min(A,B). */
39inline void clampLEQUAL(T& value,const T maxValue,const char* valueName)
43 notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
48/** If value is between or equal to minValue and maxValue do nothing - legal
49 * value, Otherwise clamp value to specified range and warn that valueName
50 * was clamped. Equivalent to calling
51 * clampGEQUAL( value, minValue, valueName );
52 * clampLEQUAL( value, maxValue, valueName ); */
54inline void clampBetweenRange(T& value,const T minValue,const T maxValue,const char* valueName)
58 notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
64 notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
70/** If value[i] is greater than or equal to minValue do nothing - legal value,
71 * Otherwise set value[i] to minValue, and warn that valueName[i] was clamped.
72 * Note this is effectively A[i]=max(A[i],B). */
73template <typename A, typename T>
74inline void clampArrayElementGEQUAL(A& value,unsigned int i,const T minValue,const char* valueName)
76 if (value[i]<minValue)
78 notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
83/** If value[i] is less than or equal to maxValue do nothing - legal value,
84 * Otherwise set value[i] to maxValue, and warn that valueName[i] was clamped.
85 * Note this is effectively A[i]=min(A[i],B). */
86template <typename A, typename T>
87inline void clampArrayElementLEQUAL(A& value,unsigned int i,const T maxValue,const char* valueName)
89 if (value[i]>maxValue)
91 notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
96/** If value[i] is between or equal to minValue and maxValue do nothing - legal
97 * value, Otherwise clamp value[i] to specified range and warn that
98 * valueName[i] was clamped. Equivalent to calling
99 * clampArrayElementGEQUAL( value, i, minValue, valueName );
100 * clampArrayElementLEQUAL( value, i, maxValue, valueName ); */
101template <typename A, typename T>
102inline void clampArrayElementBetweenRange(A& value,unsigned int i,const T minValue,const T maxValue,const char* valueName)
104 if (value[i]<minValue)
106 notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
110 if (value[i]>maxValue)
112 notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
118/** For each element of value[] in the range (first,last), if the element is
119 * greater than or equal to minValue do nothing - legal value, Otherwise
120 * clamp the element to minValue, and warn that valueName[i] was clamped. */
121template <typename A, typename T>
122inline void clampArrayElementsGEQUAL(A& value,unsigned int first,unsigned int last,const T minValue,const char* valueName)
124 for(unsigned int i=first;i<=last;++i)
125 clampArrayElementGEQUAL(value,i,minValue,valueName);
128/** For each element of value[] in the range (first,last), if the element is
129 * less than or equal to maxValue do nothing - legal value, Otherwise clamp
130 * the element to maxValue, and warn that valueName[i] was clamped. */
131template <typename A, typename T>
132inline void clampArrayElementsLEQUAL(A& value,unsigned int first,unsigned int last,const T maxValue,const char* valueName)
134 for(unsigned int i=first;i<=last;++i)
135 clampArrayElementLEQUAL(value,i,maxValue,valueName);
138/** For each element of value[] in the range (first,last), if the element is
139 * between or equal to minValue and maxValue do nothing - legal value,
140 * Otherwise clamp the element to the range and warn that valueName[i] was
141 * clamped. Equivalent to calling
142 * clampArrayElementsGEQUAL( value, first, last, minValue, valueName);
143 * clampArrayElementsLEQUAL( value, first, last, maxValue, valueName); */
144template <typename A, typename T>
145inline void clampArrayElementsBetweenRange(A& value,unsigned int first,unsigned int last,const T minValue,const T maxValue,const char* valueName)
147 for(unsigned int i=first;i<=last;++i)
148 clampArrayElementBetweenRange(value,i,minValue,maxValue,valueName);
152/** For each element of the three-element array value[], if the element is
153 * greater than or equal to minValue do nothing - legal value, Otherwise
154 * clamp the element to minValue, and warn that valueName[i] was clamped. */
155template <typename A, typename T>
156inline void clampArray3GEQUAL(A& value,const T minValue,const char* valueName)
158 clampArrayElementsGEQUAL(value,0u,2u,minValue,valueName);
161/** For each element of the three-element array value[], if the element is
162 * less than or equal to maxValue do nothing - legal value, Otherwise clamp
163 * the element to maxValue, and warn that valueName[i] was clamped. */
164template <typename A, typename T>
165inline void clampArray3LEQUAL(A& value,const T maxValue,const char* valueName)
167 clampArrayElementsLEQUAL(value,0u,2u,maxValue,valueName);
170/** For each element of the three-element array value[], if the element is
171 * between or equal to minValue and maxValue do nothing - legal value,
172 * Otherwise clamp the element to the range and warn that valueName[i] was
173 * clamped. Equivalent to calling
174 * clampArray3GEQUAL( value, minValue, valueName);
175 * clampArray3LEQUAL( value, maxValue, valueName); */
176template <typename A, typename T>
177inline void clampArray3BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
179 clampArrayElementsBetweenRange(value,0u,2u,minValue,maxValue,valueName);
184/** For each element of the four-element array value[], if the element is
185 * greater than or equal to minValue do nothing - legal value, Otherwise
186 * clamp the element to minValue, and warn that valueName[i] was clamped. */
187template <typename A, typename T>
188inline void clampArray4GEQUAL(A& value,const T minValue,const char* valueName)
190 clampArrayElementsGEQUAL(value,0u,3u,minValue,valueName);
193/** For each element of the four-element array value[], if the element is
194 * less than or equal to maxValue do nothing - legal value, Otherwise clamp
195 * the element to maxValue, and warn that valueName[i] was clamped. */
196template <typename A, typename T>
197inline void clampArray4LEQUAL(A& value,const T maxValue,const char* valueName)
199 clampArrayElementsLEQUAL(value,0u,3u,maxValue,valueName);
202/** For each element of the four-element array value[], if the element is
203 * between or equal to minValue and maxValue do nothing - legal value,
204 * Otherwise clamp the element to the range and warn that valueName[i] was
205 * clamped. Equivalent to calling
206 * clampArray4GEQUAL( value, minValue, valueName);
207 * clampArray4LEQUAL( value, maxValue, valueName); */
208template <typename A, typename T>
209inline void clampArray4BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
211 clampArrayElementsBetweenRange(value,0u,3u,minValue,maxValue,valueName);