BlinkSequence
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_BLINKSQUENCE
#define OSGSIM_BLINKSQUENCE 1
#include <osgSim/Export>
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Object>
#include <osg/ref_ptr>
#include <vector>
namespace osgSim {
class OSGSIM_EXPORT SequenceGroup : public osg::Object
{
public:
SequenceGroup();
SequenceGroup(const SequenceGroup& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
SequenceGroup(double baseTime);
META_Object(osgSim,SequenceGroup);
inline void setBaseTime( double t ) { _baseTime = t; }
inline double getBaseTime() const { return _baseTime; }
double _baseTime;
};
class OSGSIM_EXPORT BlinkSequence : public osg::Object
{
public:
BlinkSequence();
BlinkSequence(const BlinkSequence& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Object(osgSim,BlinkSequence);
inline void addPulse(double length,const osg::Vec4& color);
inline int getNumPulses() const { return _pulseData.size(); }
inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
inline void setPulse(unsigned int i,double length,const osg::Vec4& color);
inline double getPulsePeriod() const { return _pulsePeriod; }
inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
inline void setPhaseShift(double ps) { _phaseShift = ps; }
inline double getPhaseShift() const { return _phaseShift; }
inline double localTime(double time) const;
inline osg::Vec4 color(double time,double length) const;
protected:
typedef std::pair<double,osg::Vec4> IntervalColor;
typedef std::vector<IntervalColor> PulseData;
double _pulsePeriod;
double _phaseShift;
PulseData _pulseData;
osg::ref_ptr<SequenceGroup> _sequenceGroup;
};
inline double BlinkSequence::localTime(double time) const
{
if (_sequenceGroup.valid()) time -= _sequenceGroup->_baseTime;
time -= _phaseShift;
return time - floor(time/_pulsePeriod)*_pulsePeriod;
}
inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
{
_pulseData.push_back(IntervalColor(length,color));
_pulsePeriod += length;
}
inline void BlinkSequence::getPulse(unsigned int i, double& length, osg::Vec4& color) const
{
const IntervalColor& ic = _pulseData[i];
length = ic.first;
color = ic.second;
}
inline void BlinkSequence::setPulse(unsigned int i,double length,const osg::Vec4& color)
{
if( i >= _pulseData.size() ) return;
IntervalColor& ic = _pulseData[i];
ic.first = length;
ic.second = color;
}
inline osg::Vec4 BlinkSequence::color(double time,double length) const
{
if (_pulseData.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
double lt = localTime(time);
PulseData::const_iterator itr = _pulseData.begin();
// find the first sample at this time point.
while (lt>itr->first)
{
lt -= itr->first;
++itr;
if (itr==_pulseData.end()) itr = _pulseData.begin();
}
// if time interval fits inside the current pulse
// then simply return this pulses color value.
if (lt+length<=itr->first)
{
return itr->second;
}
// time length exceeds the current pulse therefore
// we have to average out the pules to get the correct
// results...
// accumulate final part of the first active pulses.
osg::Vec4 color(itr->second*(itr->first-lt));
double len = length-(itr->first-lt);
++itr;
if (itr==_pulseData.end()) itr = _pulseData.begin();
// accumulate all the whole pluses pulses.
while (len>itr->first)
{
len -= itr->first;
color += itr->second*itr->first;
++itr;
if (itr==_pulseData.end()) itr = _pulseData.begin();
}
// add remaining part of the final pulse.
color += itr->second*len;
// normalise the time waited color.
color /= length;
return color;
}
}
#endif
Object
Quat
Vec3
Vec4
osgSim::BlinkSequence::getPulse
void getPulse(unsigned int i, double &length, osg::Vec4 &color) const
Definition BlinkSequence:119
osgSim::BlinkSequence::_pulseData
PulseData _pulseData
Definition BlinkSequence:101
osgSim::BlinkSequence::_pulsePeriod
double _pulsePeriod
Definition BlinkSequence:99
osgSim::BlinkSequence::setSequenceGroup
void setSequenceGroup(SequenceGroup *sg)
Definition BlinkSequence:72
osgSim::BlinkSequence::setPulse
void setPulse(unsigned int i, double length, const osg::Vec4 &color)
Definition BlinkSequence:126
osgSim::BlinkSequence::getSequenceGroup
SequenceGroup * getSequenceGroup()
Definition BlinkSequence:75
osgSim::BlinkSequence::getPhaseShift
double getPhaseShift() const
Definition BlinkSequence:84
osgSim::BlinkSequence::setPhaseShift
void setPhaseShift(double ps)
Definition BlinkSequence:81
osgSim::BlinkSequence::_phaseShift
double _phaseShift
Definition BlinkSequence:100
osgSim::BlinkSequence::PulseData
std::vector< IntervalColor > PulseData
Definition BlinkSequence:97
osgSim::BlinkSequence::getPulsePeriod
double getPulsePeriod() const
Definition BlinkSequence:69
osgSim::BlinkSequence::_sequenceGroup
osg::ref_ptr< SequenceGroup > _sequenceGroup
Definition BlinkSequence:102
osgSim::BlinkSequence::getSequenceGroup
const SequenceGroup * getSequenceGroup() const
Definition BlinkSequence:78
osgSim::BlinkSequence::BlinkSequence
BlinkSequence()
osgSim::BlinkSequence::META_Object
META_Object(osgSim, BlinkSequence)
osgSim::BlinkSequence::addPulse
void addPulse(double length, const osg::Vec4 &color)
Definition BlinkSequence:113
osgSim::BlinkSequence::IntervalColor
std::pair< double, osg::Vec4 > IntervalColor
Definition BlinkSequence:96
osgSim::BlinkSequence::localTime
double localTime(double time) const
Definition BlinkSequence:106
osgSim::BlinkSequence::color
osg::Vec4 color(double time, double length) const
Definition BlinkSequence:134
osgSim::BlinkSequence::BlinkSequence
BlinkSequence(const BlinkSequence &bs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgSim::BlinkSequence::getNumPulses
int getNumPulses() const
Definition BlinkSequence:60
osgSim::SequenceGroup
Definition BlinkSequence:31
osgSim::SequenceGroup::SequenceGroup
SequenceGroup()
osgSim::SequenceGroup::META_Object
META_Object(osgSim, SequenceGroup)
osgSim::SequenceGroup::SequenceGroup
SequenceGroup(const SequenceGroup &bs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgSim::SequenceGroup::_baseTime
double _baseTime
Definition BlinkSequence:43
osgSim::SequenceGroup::getBaseTime
double getBaseTime() const
Definition BlinkSequence:41
osgSim::SequenceGroup::SequenceGroup
SequenceGroup(double baseTime)
osgSim::SequenceGroup::setBaseTime
void setBaseTime(double t)
Definition BlinkSequence:40
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::Object
Definition Object:61
osg::ref_ptr
Definition ref_ptr:32
osgSim
Definition BlinkSequence:27
osg::Vec4
Vec4f Vec4
Definition Vec4:21
Export
OSGSIM_EXPORT
#define OSGSIM_EXPORT
Definition osgSim/Export:38
ref_ptr