range 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_RANGE
  15. #define OSGPARTICLE_RANGE 1
  16. // include Export simply to disable Visual Studio silly warnings.
  17. #include <osgParticle/Export>
  18. #include <stdlib.h>
  19. #include <osg/Vec2>
  20. #include <osg/Vec3>
  21. #include <osg/Vec4>
  22. namespace osgParticle
  23. {
  24. /**
  25. A simple struct template useful to store ranges of values as min/max pairs.
  26. This struct template helps storing min/max ranges for values of any kind; class <CODE>ValueType</CODE> is
  27. the type of values to be stored, and it must support operations <CODE>ValueType + ValueType</CODE>, <CODE>ValueType - ValueType</CODE>,
  28. and <CODE>ValueType * float</CODE>, otherwise the <CODE>geValueTyperandom()</CODE> method will not compile.
  29. This struct could be extended to customize the random number generator (now it uses only
  30. <CODE>std::rand()</CODE>).
  31. */
  32. template<class ValueType> struct range
  33. {
  34. /// Lower bound.
  35. ValueType minimum;
  36. /// Higher bound.
  37. ValueType maximum;
  38. /// Construct the object by calling default constructors for min and max.
  39. range() : minimum(ValueType()), maximum(ValueType()) {}
  40. /// Construct and initialize min and max directly.
  41. range(const ValueType& mn, const ValueType& mx) : minimum(mn), maximum(mx) {}
  42. /// Set min and max.
  43. void set(const ValueType& mn, const ValueType& mx) { minimum = mn; maximum = mx; }
  44. /// Get a random value between min and max.
  45. ValueType get_random() const
  46. {
  47. return minimum + (maximum - minimum) * rand() / RAND_MAX;
  48. }
  49. /// Get a random square root value between min and max.
  50. ValueType get_random_sqrtf() const
  51. {
  52. return minimum + (maximum - minimum) * sqrtf( static_cast<float>(rand()) / static_cast<float>(RAND_MAX) );
  53. }
  54. ValueType mid() const
  55. {
  56. return (minimum+maximum)*0.5f;
  57. }
  58. };
  59. /// Range of floats.
  60. typedef range<float> rangef;
  61. /// Range of osg::Vec2s.
  62. typedef range<osg::Vec2> rangev2;
  63. /// Range of osg::Vec3s.
  64. typedef range<osg::Vec3> rangev3;
  65. /// Range of osg::Vec4s.
  66. typedef range<osg::Vec4> rangev4;
  67. }
  68. #endif