openscenegraph
BlinkSequence
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
14#ifndef OSGSIM_BLINKSQUENCE
15#define OSGSIM_BLINKSQUENCE 1
16
17#include <osgSim/Export>
18
19#include <osg/Quat>
20#include <osg/Vec3>
21#include <osg/Vec4>
22#include <osg/Object>
23#include <osg/ref_ptr>
24
25#include <vector>
26
27namespace osgSim {
28
29/** sequence group which can be used to synchronize related blink sequences.*/
30class OSGSIM_EXPORT SequenceGroup : public osg::Object
31{
32 public:
33
34 SequenceGroup();
35 SequenceGroup(const SequenceGroup& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
36 SequenceGroup(double baseTime);
37
38 META_Object(osgSim,SequenceGroup);
39
40 inline void setBaseTime( double t ) { _baseTime = t; }
41 inline double getBaseTime() const { return _baseTime; }
42
43 double _baseTime;
44};
45
46class OSGSIM_EXPORT BlinkSequence : public osg::Object
47{
48 public:
49
50 BlinkSequence();
51
52 BlinkSequence(const BlinkSequence& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
53
54 META_Object(osgSim,BlinkSequence);
55
56 /** add a pulse of specified color and duration to the BlinkSequence.*/
57 inline void addPulse(double length,const osg::Vec4& color);
58
59 /** return the number of pulses. */
60 inline int getNumPulses() const { return _pulseData.size(); }
61
62 /** return the pulse data at position i. */
63 inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
64
65 /** set pulse of specified color and duration to the BlinkSequence.*/
66 inline void setPulse(unsigned int i,double length,const osg::Vec4& color);
67
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; }
70
71 /** set the sequence group which can be used to synchronize related blink sequences.*/
72 inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
73
74 /** get the non const sequence group.*/
75 inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
76
77 /** get the const sequence group.*/
78 inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
79
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; }
82
83 /** get the pahse shift.*/
84 inline double getPhaseShift() const { return _phaseShift; }
85
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;
88
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;
91
92
93 protected:
94
95
96 typedef std::pair<double,osg::Vec4> IntervalColor;
97 typedef std::vector<IntervalColor> PulseData;
98
99 double _pulsePeriod;
100 double _phaseShift;
101 PulseData _pulseData;
102 osg::ref_ptr<SequenceGroup> _sequenceGroup;
103};
104
105
106inline double BlinkSequence::localTime(double time) const
107{
108 if (_sequenceGroup.valid()) time -= _sequenceGroup->_baseTime;
109 time -= _phaseShift;
110 return time - floor(time/_pulsePeriod)*_pulsePeriod;
111}
112
113inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
114{
115 _pulseData.push_back(IntervalColor(length,color));
116 _pulsePeriod += length;
117}
118
119inline void BlinkSequence::getPulse(unsigned int i, double& length, osg::Vec4& color) const
120{
121 const IntervalColor& ic = _pulseData[i];
122 length = ic.first;
123 color = ic.second;
124}
125
126inline void BlinkSequence::setPulse(unsigned int i,double length,const osg::Vec4& color)
127{
128 if( i >= _pulseData.size() ) return;
129 IntervalColor& ic = _pulseData[i];
130 ic.first = length;
131 ic.second = color;
132}
133
134inline osg::Vec4 BlinkSequence::color(double time,double length) const
135{
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();
139
140 // find the first sample at this time point.
141 while (lt>itr->first)
142 {
143 lt -= itr->first;
144 ++itr;
145 if (itr==_pulseData.end()) itr = _pulseData.begin();
146 }
147
148 // if time interval fits inside the current pulse
149 // then simply return this pulses color value.
150 if (lt+length<=itr->first)
151 {
152 return itr->second;
153 }
154
155 // time length exceeds the current pulse therefore
156 // we have to average out the pules to get the correct
157 // results...
158
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);
162 ++itr;
163 if (itr==_pulseData.end()) itr = _pulseData.begin();
164
165 // accumulate all the whole pluses pulses.
166 while (len>itr->first)
167 {
168 len -= itr->first;
169 color += itr->second*itr->first;
170 ++itr;
171 if (itr==_pulseData.end()) itr = _pulseData.begin();
172 }
173
174 // add remaining part of the final pulse.
175 color += itr->second*len;
176
177 // normalise the time waited color.
178 color /= length;
179
180 return color;
181}
182
183}
184
185#endif