BoundsChecking 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifndef OSG_BOUNDSCHECKING
  14. #define OSG_BOUNDSCHECKING 1
  15. #include <osg/Notify>
  16. namespace osg {
  17. /** If value is greater than or equal to minValue do nothing - legal value,
  18. * Otherwise set value to minValue, and warn that valueName was clamped.
  19. * Note this is effectively A=max(A,B). */
  20. template <typename T>
  21. inline void clampGEQUAL(T& value,const T minValue,const char* valueName)
  22. {
  23. if (value<minValue)
  24. {
  25. notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
  26. value = minValue;
  27. }
  28. }
  29. /** If value is less than or equal to maxValue do nothing - legal value,
  30. * Otherwise set value to maxValue, and warn that valueName was clamped.
  31. * Note this is effectively A=min(A,B). */
  32. template <typename T>
  33. inline void clampLEQUAL(T& value,const T maxValue,const char* valueName)
  34. {
  35. if (value>maxValue)
  36. {
  37. notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
  38. value = maxValue;
  39. }
  40. }
  41. /** If value is between or equal to minValue and maxValue do nothing - legal
  42. * value, Otherwise clamp value to specified range and warn that valueName
  43. * was clamped. Equivalent to calling
  44. * clampGEQUAL( value, minValue, valueName );
  45. * clampLEQUAL( value, maxValue, valueName ); */
  46. template <typename T>
  47. inline void clampBetweenRange(T& value,const T minValue,const T maxValue,const char* valueName)
  48. {
  49. if (value<minValue)
  50. {
  51. notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
  52. value = minValue;
  53. }
  54. else
  55. if (value>maxValue)
  56. {
  57. notify(WARN) << "Warning: "<<valueName<<" of "<<value<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
  58. value = maxValue;
  59. }
  60. }
  61. /** If value[i] is greater than or equal to minValue do nothing - legal value,
  62. * Otherwise set value[i] to minValue, and warn that valueName[i] was clamped.
  63. * Note this is effectively A[i]=max(A[i],B). */
  64. template <typename A, typename T>
  65. inline void clampArrayElementGEQUAL(A& value,unsigned int i,const T minValue,const char* valueName)
  66. {
  67. if (value[i]<minValue)
  68. {
  69. notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
  70. value[i] = minValue;
  71. }
  72. }
  73. /** If value[i] is less than or equal to maxValue do nothing - legal value,
  74. * Otherwise set value[i] to maxValue, and warn that valueName[i] was clamped.
  75. * Note this is effectively A[i]=min(A[i],B). */
  76. template <typename A, typename T>
  77. inline void clampArrayElementLEQUAL(A& value,unsigned int i,const T maxValue,const char* valueName)
  78. {
  79. if (value[i]>maxValue)
  80. {
  81. notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
  82. value = maxValue;
  83. }
  84. }
  85. /** If value[i] is between or equal to minValue and maxValue do nothing - legal
  86. * value, Otherwise clamp value[i] to specified range and warn that
  87. * valueName[i] was clamped. Equivalent to calling
  88. * clampArrayElementGEQUAL( value, i, minValue, valueName );
  89. * clampArrayElementLEQUAL( value, i, maxValue, valueName ); */
  90. template <typename A, typename T>
  91. inline void clampArrayElementBetweenRange(A& value,unsigned int i,const T minValue,const T maxValue,const char* valueName)
  92. {
  93. if (value[i]<minValue)
  94. {
  95. notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is below permitted minimum, clamping to "<<minValue<<"."<< std::endl;
  96. value[i] = minValue;
  97. }
  98. else
  99. if (value[i]>maxValue)
  100. {
  101. notify(WARN) << "Warning: "<<valueName<<"["<<i<<"] of "<<value[i]<<" is above permitted maximum, clamping to "<<maxValue<<"."<< std::endl;
  102. value[i] = maxValue;
  103. }
  104. }
  105. /** For each element of value[] in the range (first,last), if the element is
  106. * greater than or equal to minValue do nothing - legal value, Otherwise
  107. * clamp the element to minValue, and warn that valueName[i] was clamped. */
  108. template <typename A, typename T>
  109. inline void clampArrayElementsGEQUAL(A& value,unsigned int first,unsigned int last,const T minValue,const char* valueName)
  110. {
  111. for(unsigned int i=first;i<=last;++i)
  112. clampArrayElementGEQUAL(value,i,minValue,valueName);
  113. }
  114. /** For each element of value[] in the range (first,last), if the element is
  115. * less than or equal to maxValue do nothing - legal value, Otherwise clamp
  116. * the element to maxValue, and warn that valueName[i] was clamped. */
  117. template <typename A, typename T>
  118. inline void clampArrayElementsLEQUAL(A& value,unsigned int first,unsigned int last,const T maxValue,const char* valueName)
  119. {
  120. for(unsigned int i=first;i<=last;++i)
  121. clampArrayElementLEQUAL(value,i,maxValue,valueName);
  122. }
  123. /** For each element of value[] in the range (first,last), if the element is
  124. * between or equal to minValue and maxValue do nothing - legal value,
  125. * Otherwise clamp the element to the range and warn that valueName[i] was
  126. * clamped. Equivalent to calling
  127. * clampArrayElementsGEQUAL( value, first, last, minValue, valueName);
  128. * clampArrayElementsLEQUAL( value, first, last, maxValue, valueName); */
  129. template <typename A, typename T>
  130. inline void clampArrayElementsBetweenRange(A& value,unsigned int first,unsigned int last,const T minValue,const T maxValue,const char* valueName)
  131. {
  132. for(unsigned int i=first;i<=last;++i)
  133. clampArrayElementBetweenRange(value,i,minValue,maxValue,valueName);
  134. }
  135. /** For each element of the three-element array value[], if the element is
  136. * greater than or equal to minValue do nothing - legal value, Otherwise
  137. * clamp the element to minValue, and warn that valueName[i] was clamped. */
  138. template <typename A, typename T>
  139. inline void clampArray3GEQUAL(A& value,const T minValue,const char* valueName)
  140. {
  141. clampArrayElementsGEQUAL(value,0u,2u,minValue,valueName);
  142. }
  143. /** For each element of the three-element array value[], if the element is
  144. * less than or equal to maxValue do nothing - legal value, Otherwise clamp
  145. * the element to maxValue, and warn that valueName[i] was clamped. */
  146. template <typename A, typename T>
  147. inline void clampArray3LEQUAL(A& value,const T maxValue,const char* valueName)
  148. {
  149. clampArrayElementsLEQUAL(value,0u,2u,maxValue,valueName);
  150. }
  151. /** For each element of the three-element array value[], if the element is
  152. * between or equal to minValue and maxValue do nothing - legal value,
  153. * Otherwise clamp the element to the range and warn that valueName[i] was
  154. * clamped. Equivalent to calling
  155. * clampArray3GEQUAL( value, minValue, valueName);
  156. * clampArray3LEQUAL( value, maxValue, valueName); */
  157. template <typename A, typename T>
  158. inline void clampArray3BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
  159. {
  160. clampArrayElementsBetweenRange(value,0u,2u,minValue,maxValue,valueName);
  161. }
  162. /** For each element of the four-element array value[], if the element is
  163. * greater than or equal to minValue do nothing - legal value, Otherwise
  164. * clamp the element to minValue, and warn that valueName[i] was clamped. */
  165. template <typename A, typename T>
  166. inline void clampArray4GEQUAL(A& value,const T minValue,const char* valueName)
  167. {
  168. clampArrayElementsGEQUAL(value,0u,3u,minValue,valueName);
  169. }
  170. /** For each element of the four-element array value[], if the element is
  171. * less than or equal to maxValue do nothing - legal value, Otherwise clamp
  172. * the element to maxValue, and warn that valueName[i] was clamped. */
  173. template <typename A, typename T>
  174. inline void clampArray4LEQUAL(A& value,const T maxValue,const char* valueName)
  175. {
  176. clampArrayElementsLEQUAL(value,0u,3u,maxValue,valueName);
  177. }
  178. /** For each element of the four-element array value[], if the element is
  179. * between or equal to minValue and maxValue do nothing - legal value,
  180. * Otherwise clamp the element to the range and warn that valueName[i] was
  181. * clamped. Equivalent to calling
  182. * clampArray4GEQUAL( value, minValue, valueName);
  183. * clampArray4LEQUAL( value, maxValue, valueName); */
  184. template <typename A, typename T>
  185. inline void clampArray4BetweenRange(A& value,const T minValue,const T maxValue,const char* valueName)
  186. {
  187. clampArrayElementsBetweenRange(value,0u,3u,minValue,maxValue,valueName);
  188. }
  189. }
  190. #endif