1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGSIM_BLINKSQUENCE
15#define OSGSIM_BLINKSQUENCE 1
17#include <osgSim/Export>
29/** sequence group which can be used to synchronize related blink sequences.*/
30class OSGSIM_EXPORT SequenceGroup : public osg::Object
35 SequenceGroup(const SequenceGroup& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
36 SequenceGroup(double baseTime);
38 META_Object(osgSim,SequenceGroup);
40 inline void setBaseTime( double t ) { _baseTime = t; }
41 inline double getBaseTime() const { return _baseTime; }
46class OSGSIM_EXPORT BlinkSequence : public osg::Object
52 BlinkSequence(const BlinkSequence& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
54 META_Object(osgSim,BlinkSequence);
56 /** add a pulse of specified color and duration to the BlinkSequence.*/
57 inline void addPulse(double length,const osg::Vec4& color);
59 /** return the number of pulses. */
60 inline int getNumPulses() const { return _pulseData.size(); }
62 /** return the pulse data at position i. */
63 inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
65 /** set pulse of specified color and duration to the BlinkSequence.*/
66 inline void setPulse(unsigned int i,double length,const osg::Vec4& color);
68 /** get the total pulse period of the blink sequence, which is equal to the sum of all the pulse periods.*/
69 inline double getPulsePeriod() const { return _pulsePeriod; }
71 /** set the sequence group which can be used to synchronize related blink sequences.*/
72 inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
74 /** get the non const sequence group.*/
75 inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
77 /** get the const sequence group.*/
78 inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
80 /** set the phase shift of the blink sequence, this would be used to shift a sequence within a sequence group.*/
81 inline void setPhaseShift(double ps) { _phaseShift = ps; }
83 /** get the pahse shift.*/
84 inline double getPhaseShift() const { return _phaseShift; }
86 /** compute the local time clamped to this BlinkSequences period, and accounting for the phase shift and sequence group.*/
87 inline double localTime(double time) const;
89 /** compute the color for the time interval sepecifed. Averages the colors if the length is greater than the current pulse.*/
90 inline osg::Vec4 color(double time,double length) const;
96 typedef std::pair<double,osg::Vec4> IntervalColor;
97 typedef std::vector<IntervalColor> PulseData;
101 PulseData _pulseData;
102 osg::ref_ptr<SequenceGroup> _sequenceGroup;
106inline double BlinkSequence::localTime(double time) const
108 if (_sequenceGroup.valid()) time -= _sequenceGroup->_baseTime;
110 return time - floor(time/_pulsePeriod)*_pulsePeriod;
113inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
115 _pulseData.push_back(IntervalColor(length,color));
116 _pulsePeriod += length;
119inline void BlinkSequence::getPulse(unsigned int i, double& length, osg::Vec4& color) const
121 const IntervalColor& ic = _pulseData[i];
126inline void BlinkSequence::setPulse(unsigned int i,double length,const osg::Vec4& color)
128 if( i >= _pulseData.size() ) return;
129 IntervalColor& ic = _pulseData[i];
134inline osg::Vec4 BlinkSequence::color(double time,double length) const
136 if (_pulseData.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
137 double lt = localTime(time);
138 PulseData::const_iterator itr = _pulseData.begin();
140 // find the first sample at this time point.
141 while (lt>itr->first)
145 if (itr==_pulseData.end()) itr = _pulseData.begin();
148 // if time interval fits inside the current pulse
149 // then simply return this pulses color value.
150 if (lt+length<=itr->first)
155 // time length exceeds the current pulse therefore
156 // we have to average out the pules to get the correct
159 // accumulate final part of the first active pulses.
160 osg::Vec4 color(itr->second*(itr->first-lt));
161 double len = length-(itr->first-lt);
163 if (itr==_pulseData.end()) itr = _pulseData.begin();
165 // accumulate all the whole pluses pulses.
166 while (len>itr->first)
169 color += itr->second*itr->first;
171 if (itr==_pulseData.end()) itr = _pulseData.begin();
174 // add remaining part of the final pulse.
175 color += itr->second*len;
177 // normalise the time waited color.