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