2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-06-05 20:44:55 +08:00
|
|
|
//osgParticle - Copyright (C) 2002 Marco Jez
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
#ifndef OSGPARTICLE_RANGE
|
|
|
|
#define OSGPARTICLE_RANGE 1
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2002-06-05 22:14:32 +08:00
|
|
|
// include Export simply to disable Visual Studio silly warnings.
|
|
|
|
#include <osgParticle/Export>
|
|
|
|
|
2002-06-07 22:54:33 +08:00
|
|
|
#ifndef __sgi
|
2002-06-05 20:44:55 +08:00
|
|
|
#include <cstdlib>
|
2002-06-07 22:54:33 +08:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
|
2002-06-05 22:14:32 +08:00
|
|
|
//using std::rand;
|
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
namespace osgParticle
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
A simple struct template useful to store ranges of values as min/max pairs.
|
2005-04-29 17:47:57 +08:00
|
|
|
This struct template helps storing min/max ranges for values of any kind; class <CODE>ValueType</CODE> is
|
|
|
|
the type of values to be stored, and it must support operations <CODE>ValueType + ValueType</CODE>, <CODE>ValueType - ValueType</CODE>,
|
|
|
|
and <CODE>ValueType * float</CODE>, otherwise the <CODE>geValueTyperandom()</CODE> method will not compile.
|
2002-06-05 20:44:55 +08:00
|
|
|
This struct could be extended to customize the random number generator (now it uses only
|
|
|
|
<CODE>std::rand()</CODE>).
|
|
|
|
*/
|
2005-04-29 17:47:57 +08:00
|
|
|
template<class ValueType> struct range
|
|
|
|
{
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Lower bound.
|
2005-04-29 17:47:57 +08:00
|
|
|
ValueType minimum;
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Higher bound.
|
2005-04-29 17:47:57 +08:00
|
|
|
ValueType maximum;
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Construct the object by calling default constructors for min and max.
|
2005-04-29 17:47:57 +08:00
|
|
|
range() : minimum(ValueType()), maximum(ValueType()) {}
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Construct and initialize min and max directly.
|
2005-04-29 17:47:57 +08:00
|
|
|
range(const ValueType& mn, const ValueType& mx) : minimum(mn), maximum(mx) {}
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Set min and max.
|
2005-04-29 17:47:57 +08:00
|
|
|
void set(const ValueType& mn, const ValueType& mx) { minimum = mn; maximum = mx; }
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Get a random value between min and max.
|
2005-04-29 17:47:57 +08:00
|
|
|
ValueType get_random() const
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2002-08-04 02:03:40 +08:00
|
|
|
return minimum + (maximum - minimum) * rand() / RAND_MAX;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2004-08-02 15:25:28 +08:00
|
|
|
/// Get a random square root value between min and max.
|
2005-04-29 17:47:57 +08:00
|
|
|
ValueType get_random_sqrtf() const
|
2004-08-02 15:25:28 +08:00
|
|
|
{
|
|
|
|
return minimum + (maximum - minimum) * sqrtf( static_cast<float>(rand()) / static_cast<float>(RAND_MAX) );
|
|
|
|
}
|
2005-08-25 22:12:08 +08:00
|
|
|
|
|
|
|
ValueType mid() const
|
|
|
|
{
|
|
|
|
return (minimum+maximum)*0.5f;
|
|
|
|
}
|
2004-08-02 15:25:28 +08:00
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Range of floats.
|
|
|
|
typedef range<float> rangef;
|
|
|
|
|
|
|
|
/// Range of osg::Vec2s.
|
|
|
|
typedef range<osg::Vec2> rangev2;
|
|
|
|
|
|
|
|
/// Range of osg::Vec3s.
|
|
|
|
typedef range<osg::Vec3> rangev3;
|
|
|
|
|
|
|
|
/// Range of osg::Vec4s.
|
|
|
|
typedef range<osg::Vec4> rangev4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|