From Neil Salter, added osgSim::SphereSegment and osgSim::ScalarBar, and

osgspheresegment and osgscalarbar, and osgsimulation examples.
This commit is contained in:
Robert Osfield
2003-09-01 09:36:03 +00:00
parent 5400f8293b
commit 144ac14b07
25 changed files with 3214 additions and 5 deletions

57
include/osgSim/ColorRange Normal file
View File

@@ -0,0 +1,57 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* 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.
*/
#ifndef OSGSIM_COLORRANGE
#define OSGSIM_COLORRANGE 1
#include <osgSim/Export>
#include <osgSim/ScalarsToColors>
#include <vector>
namespace osgSim
{
/**
ColorRange is a ScalarsToColors object to define a color spectrum
for a scalar range. An optional vector of colors may be passed in at
construction time. The range of colors will be mapped to the scalar range,
and interpolation between the colors will be performed as necessary.
By default, the color range will run Red-Yellow-Green-Cyan-Blue.
*/
class OSGSIM_EXPORT ColorRange: public ScalarsToColors
{
public:
/** Constructor for a ColorRange
@param min minimum scalar value
@param max maximum scalar value
@param colors optional range of colors, defaulting to Red-Yellow-Green-Blue-Cyan
*/
ColorRange(float min, float max, const std::vector<osg::Vec4>& colors = std::vector<osg::Vec4>());
/** Set the range of colors. */
void setColors(const std::vector<osg::Vec4>& colors);
/** Get the color for a given scalar value. */
osg::Vec4 getColor(float scalar) const;
private:
// Default assignment and copy construction are OK.
std::vector<osg::Vec4> _colors;
};
}
#endif

219
include/osgSim/ScalarBar Normal file
View File

@@ -0,0 +1,219 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* 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.
*/
#ifndef OSGSIM_SCALARBAR
#define OSGSIM_SCALARBAR 1
#include <osgSim/Export>
#include <osgSim/ColorRange> // The default ScalarsToColors is a ColorRange
#include <osg/Geode>
#include <string>
namespace osgSim
{
/**
A ScalarBar is an osg::Geode to render a colored bar representing a range
of scalars. The scalar/color ranges are specified by an instance of
ScalarsToColors. There are a number of configurable properties on the
ScalarBar, such as the orientation, the number of labels to be displayed
across the range, the number of distinct colors to use when rendering the
bar, text details etc.
In summary, the main configurables on the ScalarBar are:
-# The range of scalars represented by the bar, and the colors
corresponding to this range - these are specified by the
ScalarsToColors object.
-# The number of colors used when rendering the bar geometry -
this may be thought of as the bar 'density'.
-# The number of text labels to be used when displaying the bar.
The other configurables should be self-explanatory.
*/
class OSGSIM_EXPORT ScalarBar: public osg::Geode
{
public:
/** ScalarBar orientation specification. */
enum Orientation{
HORIZONTAL, ///< a horizontally ascending scalar bar (x-axis)
VERTICAL ///< a vertically ascending scalar bar (y-axis)
};
/**
Users may provide their own ScalarPrinter by deriving from this base class and
overriding the printScalar() method. Users may map the scalar float passed in
to any string they wish.
*/
struct ScalarPrinter: public osg::Referenced
{
virtual std::string printScalar(float scalar);
};
/**
TextProperties allows users to specify a number of properties for the
text used to display the labels & title on the ScalarBar. Specifiying a character
size of 0 will cause the ScalarBar to estimate an appropriate size. Note that
the attributes are public, and may be set directly.
*/
struct TextProperties
{
std::string _fontFile;
std::pair<int,int> _fontResolution;
int _characterSize;
osg::Vec4 _color;
TextProperties():
_fontFile("fonts/arial.ttf"),
_fontResolution(40,40),
_characterSize(0),
_color(1.0f,1.0f,1.0f,1.0f)
{
}
};
/** Default constructor. */
ScalarBar(): osg::Geode(),
_numColors(256),
_numLabels(11),
_stc(new ColorRange(0.0f,1.0f)),
_title("Scalar Bar"),
_orientation(HORIZONTAL),
_aspectRatio(0.03),
_sp(new ScalarPrinter)
{
createDrawables();
}
/**
Construct a ScalarBar with the supplied parameters.
@param numColors Specify the number of colors in the scalar bar. Color
interpolation occurs where necessary.
@param stc The ScalarsToColors defining the range of scalars
and the colors they map to.
@param title The title to be used when displaying the ScalarBar.
Specify "" for no title.
@param orientation The orientation of the ScalarBar. @see Orientation.
@param apectRatio The aspect ration (y/x) for the displayed bar. Bear in mind you
may want to change this if you change the orientation.
@param sp A ScalarPrinter object for the ScalarBar. For every displayed
ScalarBar label, the scalar value will be passed to the
ScalarPrinter object to turn it into a string. Users may
override the default ScalarPrinter object to map scalars to
whatever strings they wish. @see ScalarPrinter
*/
ScalarBar(int numColors, int numLabels, ScalarsToColors* stc,
const std::string& title,
Orientation orientation = HORIZONTAL,
float aspectRatio=0.25,
ScalarPrinter* sp=new ScalarPrinter):
osg::Geode(),
_numColors(numColors),
_numLabels(numLabels),
_stc(stc),
_title(title),
_orientation(orientation),
_aspectRatio(aspectRatio),
_sp(sp)
{
createDrawables();
}
/** Copy constructor */
ScalarBar(const ScalarBar& rhs, const osg::CopyOp& co): osg::Geode(rhs,co),
_numColors(rhs._numColors),
_numLabels(rhs._numLabels),
_stc(rhs._stc), // Consider clone for deep copy?
_title(rhs._title),
_orientation(rhs._orientation),
_aspectRatio(rhs._aspectRatio),
_sp(rhs._sp), // Consider clone for deep copy?
_textProperties(rhs._textProperties)
{
}
/** Set the number of distinct colours on the ScalarBar. */
void setNumColors(int numColors);
/** Get the number of distinct colours on the ScalarBar. */
int getNumColors() const;
/** Set the number of labels to display along the ScalarBar. There
will be one label at each end point, and evenly distributed labels
in between. */
void setNumLabels(int numLabels);
/** Get the number of labels displayed along the ScalarBar. */
int getNumLabels() const;
/** Set the ScalarsToColors mapping object for the ScalarBar. */
void setScalarsToColors(ScalarsToColors* stc);
/** Get the ScalarsToColors mapping object from the ScalarBar. */
const ScalarsToColors* getScalarsToColors() const;
/** Set the title for the ScalarBar, set "" for no title. */
void setTitle(const std::string& title);
/** Get the title for the ScalarBar. */
std::string getTitle() const;
/** Set the orientation of the ScalarBar. @see Orientation */
void setOrientation(ScalarBar::Orientation orientation);
/** Get the orientation of the ScalarBar. @see Orientation */
ScalarBar::Orientation getOrientation() const;
/** Set the aspect ration (y/x) for the displayed bar. Bear in mind you
may want to change this if you change the orientation. */
void setAspectRatio(float aspectRatio);
/** Get the aspect ration (y/x) for the displayed bar. */
float getAspectRatio() const;
/** Set a ScalarPrinter object for the ScalarBar. For every displayed
ScalarBar label, the scalar value will be passed to the ScalarPrinter
object to turn it into a string. Users may override the default ScalarPrinter
object to map scalars to whatever strings they wish. @see ScalarPrinter */
void setScalarPrinter(ScalarPrinter* sp);
/** Get the ScalarPrinter object */
const ScalarPrinter* getScalarPrinter() const;
/** Set the TextProperties for the labels & title. @see TextProperties */
void setTextProperties(const TextProperties& tp);
/** Get the TextProperties for the labels & title. @see TextProperties */
const TextProperties& getTextProperties() const;
META_Node(osgSim, ScalarBar);
private:
int _numColors;
int _numLabels;
osg::ref_ptr<ScalarsToColors> _stc;
std::string _title;
Orientation _orientation;
float _aspectRatio;
osg::ref_ptr<ScalarPrinter> _sp;
TextProperties _textProperties;
void createDrawables();
};
}
#endif

View File

@@ -0,0 +1,53 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* 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.
*/
#ifndef OSGSIM_SCALARSTCOLORS
#define OSGSIM_SCALARSTCOLORS 1
#include <osgSim/Export>
#include <osg/Vec4>
#include <osg/Referenced>
namespace osgSim
{
/**
ScalarsToColors defines the interface to map a scalar value to a color,
and provides a default implementation of the mapping functionaltity,
with colors ranging from black to white across the min - max scalar
range.
*/
class OSGSIM_EXPORT ScalarsToColors: public osg::Referenced
{
public:
ScalarsToColors(float scalarMin, float scalarMax);
virtual ~ScalarsToColors() {}
/** Get the color for a given scalar value. */
virtual osg::Vec4 getColor(float scalar) const;
/** Get the minimum scalar value. */
float getMin() const;
/** Get the maximum scalar value. */
float getMax() const;
private:
float _min, _max;
};
}
#endif

View File

@@ -0,0 +1,259 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* 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.
*/
#ifndef OSGSIM_SPHERESEGMENT
#define OSGSIM_SPHERESEGMENT 1
#include <osgSim/Export>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Geode>
#include <osg/BlendFunc>
namespace osgSim{
/**
A SphereSegment is a Geode to represent an portion of a sphere (potentially
the whole sphere). The sphere is aligned such that the line through the
sphere's poles is parallel to the z axis. The sphere segment
may be rendered various components switched on or off:
- The specified area of the sphere surface.
- An edge line around the boundary of the specified area
of the sphere surface.
- Four <i>spokes</i>, where a spoke is the line from
the sphere's centre to a corner of the rendered area.
- Four planar areas, where the planar areas are formed
between the spokes.
Caveats:
- It's worth noting that the line through the sphere's poles is
parallel to the z axis. This has implications when specifying the
area to be rendered, and specifying areas where the centre of
the rendered area <i>is</i> the Z axis may lead to unexpected
geometry.
- It's possible to render the whole sphere by specifying elevation
and azimuth ranges round the full 360 degrees. When doing
so you may consider switching the planes, spokes, and edge lines
off, to avoid rendering artefacts, e.g. the upper and lower
planes will be coincident.
*/
class OSGSIM_EXPORT SphereSegment: public osg::Geode
{
public:
/**
DrawMask represents a bit field, the values of which may be OR'ed together
to specify which parts of the sphere segment should be drawn. E.g.
\code
sphereSegment->setDrawMask(SphereSegment::DrawMask(SphereSegment::SURFACE|SphereSegment::SPOKES));
\endcode
*/
enum DrawMask{
SURFACE = 0x00000001, ///< Draw the specified area on the sphere's surface
SPOKES = 0x00000002, ///< Draw the spokes from the sphere's centre to the surface's corners
EDGELINE = 0x00000008, ///< Draw the line round the edge of the area on the sphere's surface
PLANES = 0x00000010, ///< Draw the planes from the sphere's centre to the edge of the sphere's surface
ALL = 0xffffffff ///< Draw every part of the sphere segment
};
/** Default constructor. */
SphereSegment():Geode(),
_centre(0.0f,0.0f,0.0f), _radius(1.0f),
_azMin(0.0f), _azMax(osg::PI/2.0f),
_elevMin(0.0f), _elevMax(osg::PI/2.0f),
_density(10),
_drawMask(DrawMask(ALL))
{}
/**
Construct by angle ranges. Note that the azimuth 'zero' is the Y axis; specifying
an azimuth range from azMin -osg::PI/2.0f to azMax osg::PI/2.0f will cover the
'top half' of the circle in the XY plane. The elev angles are 'out' of the 'zero'
XY plane with +ve angles above the plane, and -ve angles below.
@param centre sphere centre
@param radius radius of sphere
@param azMin azimuth minimum
@param azMin azimuth maximum
@param elevMin elevation minimum
@param elevMax elevation maximum
@param density number of units to divide the azimuth and elevation ranges into
*/
SphereSegment(const osg::Vec3& centre, float radius, float azMin, float azMax,
float elevMin, float elevMax, int density):
Geode(),
_centre(centre), _radius(radius),
_azMin(azMin), _azMax(azMax),
_elevMin(elevMin), _elevMax(elevMax),
_density(density),
_drawMask(DrawMask(ALL))
{
init();
}
/**
Construct by vector.
@param centre sphere centre
@param radius radius of sphere
@param vec vector pointing from sphere centre to centre point
of rendered area on sphere surface
@param azRange azimuth range in radians (with centre along vec)
@param elevRange elevation range in radians (with centre along vec)
@param density number of units to divide the azimuth and elevation ranges into
*/
SphereSegment(const osg::Vec3& centre, float radius, const osg::Vec3& vec, float azRange,
float elevRange, int density);
/** Copy constructor */
SphereSegment(const SphereSegment& rhs, const osg::CopyOp& co):
Geode(rhs,co),
_centre(rhs._centre), _radius(rhs._radius),
_azMin(rhs._azMin), _azMax(rhs._azMax),
_elevMin(rhs._elevMin), _elevMax(rhs._elevMax),
_density(rhs._density),
_drawMask(rhs._drawMask)
{
init();
}
/** Set the centre point of the SphereSegment */
void setCentre(const osg::Vec3& c);
/** Get the centre point of the SphereSegment */
const osg::Vec3& getCentre() const;
/** Set the radius of the SphereSegment */
void setRadius(float r);
/** Get the radius of the SphereSegment */
float getRadius() const;
/** Set the area of the sphere segment
@param vec vector pointing from sphere centre to centre point
of rendered area on sphere surface
@param azRange azimuth range in radians (with centre along vec)
@param elevRange elevation range in radians (with centre along vec)
*/
void setArea(const osg::Vec3& v, float azRange, float elevRange);
/** Get the area of the sphere segment
@param vec vector pointing from sphere centre to centre point
of rendered area on sphere surface (normalized)
@param azRange azimuth range in radians (with centre along vec)
@param elevRange elevation range in radians (with centre along vec)
*/
void getArea(osg::Vec3& v, float& azRange, float& elevRange) const;
/** Set the density of the sphere segment */
void setDensity(int d);
/** Get the density of the sphere segment */
int getDensity() const;
/**
Specify the DrawMask.
@param dm Bitmask specifying which parts of the sphere segment should be drawn.
@see DrawMask
*/
void setDrawMask(DrawMask dm);
/** Set the color of the surface. */
void setSurfaceColor(const osg::Vec4& c);
/** Set the color of the spokes. */
void setSpokeColor(const osg::Vec4& c);
/** Set the color of the edge line. */
void setEdgeLineColor(const osg::Vec4& c);
/** Set the color of the planes. */
void setPlaneColor(const osg::Vec4& c);
/** Set color of all components. */
void setAllColors(const osg::Vec4& c);
META_Node(osgSim, SphereSegment)
private:
void init(); // Shared constructor code, generates the drawables
void dirtyAllDrawableDisplayLists(); // Force re-calling of gl functions
void dirtyAllDrawableBounds(); // Force recalculation of bound geometry
// SphereSegment is actually made up of a number of Drawable classes,
// all of which are nested private classes, as declared below. These
// classes are defined in the .cpp for minimum visibility and physical
// coupling. (Reduces time spent compiling! :-)
//
// Each of the nested classes holds a pointer to the SphereSegment
// 'parent', which stores the geometry details, and performs any
// work required. The nested classes are lightweight objects which
// just pass the work on.
//
// Why are things done with these sub-Drawables? Alpha-blended
// Drawables need to be drawn last, depth sorted, and the various
// components of a SphereSegment also need to be depth sorted
// against one another (they may all be drawn with alpha blending).
// Making these Drawables allows us to get the OSG to depth sort
// for us.
class Surface;
friend class Surface;
bool Surface_computeBound(osg::BoundingBox&) const;
void Surface_drawImplementation(osg::State&) const;
class EdgeLine;
friend class EdgeLine;
bool EdgeLine_computeBound(osg::BoundingBox&) const;
void EdgeLine_drawImplementation(osg::State&) const;
enum BoundaryAngle{MIN,MAX}; // Why here and not in Plane class? Because we can't forward
enum PlaneOrientation{AZIM,ELEV}; // declare enums, Plane is in the .cpp, and this is tidier...
class Plane;
friend class Plane;
bool Plane_computeBound(osg::BoundingBox&, PlaneOrientation, BoundaryAngle) const;
void Plane_drawImplementation(osg::State&, PlaneOrientation, BoundaryAngle) const;
class Spoke;
friend class Spoke;
bool Spoke_computeBound(osg::BoundingBox&, BoundaryAngle, BoundaryAngle) const;
void Spoke_drawImplementation(osg::State&, BoundaryAngle, BoundaryAngle) const;
// Sphere segment geometry details
osg::Vec3 _centre;
float _radius;
float _azMin, _azMax, _elevMin, _elevMax;
int _density;
// Draw details
DrawMask _drawMask;
osg::Vec4 _surfaceColor;
osg::Vec4 _spokeColor;
osg::Vec4 _edgeLineColor;
osg::Vec4 _planeColor;
};
}
#endif