| 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 | |
|---|
| 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). */ |
|---|
| 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, clampping to "<<minValue<<"."<< std::endl; |
|---|
| 31 | value = minValue; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 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). */ |
|---|
| 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, clampping to "<<maxValue<<"."<< std::endl; |
|---|
| 44 | value = maxValue; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 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. Equivilant to calling |
|---|
| 51 | * clampGEQUAL( value, minValue, valueName ); |
|---|
| 52 | * clampLEQUAL( value, maxValue, valueName ); */ |
|---|
| 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, clampping 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, clampping to "<<maxValue<<"."<< std::endl; |
|---|
| 65 | value = maxValue; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 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). */ |
|---|
| 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, clampping to "<<minValue<<"."<< std::endl; |
|---|
| 79 | value[i] = minValue; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 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). */ |
|---|
| 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, clampping to "<<maxValue<<"."<< std::endl; |
|---|
| 92 | value = maxValue; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 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. Equivilant to calling |
|---|
| 99 | * clampArrayElementGEQUAL( value, i, minValue, valueName ); |
|---|
| 100 | * clampArrayElementLEQUAL( value, i, maxValue, valueName ); */ |
|---|
| 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, clampping 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, clampping to "<<maxValue<<"."<< std::endl; |
|---|
| 113 | value[i] = maxValue; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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); */ |
|---|
| 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 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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); */ |
|---|
| 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 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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. */ |
|---|
| 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 | |
|---|
| 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); */ |
|---|
| 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 |
|---|