OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BlinkSequence.h
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 
27 namespace osgSim {
28 
31 {
32  public:
33 
34  SequenceGroup();
36  SequenceGroup(double baseTime);
37 
39 
40  inline void setBaseTime( double t ) { _baseTime = t; }
41  inline double getBaseTime() const { return _baseTime; }
42 
43  double _baseTime;
44 };
45 
47 {
48  public:
49 
50  BlinkSequence();
51 
53 
55 
57  inline void addPulse(double length,const osg::Vec4& color);
58 
60  inline int getNumPulses() const { return _pulseData.size(); }
61 
63  inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
64 
66  inline void setPulse(unsigned int i,double length,const osg::Vec4& color);
67 
69  inline double getPulsePeriod() const { return _pulsePeriod; }
70 
72  inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
73 
75  inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
76 
78  inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
79 
81  inline void setPhaseShift(double ps) { _phaseShift = ps; }
82 
84  inline double getPhaseShift() const { return _phaseShift; }
85 
87  inline double localTime(double time) const;
88 
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;
103 };
104 
105 
106 inline 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 
113 inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
114 {
115  _pulseData.push_back(IntervalColor(length,color));
116  _pulsePeriod += length;
117 }
118 
119 inline 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 
126 inline 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 
134 inline 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
#define OSGSIM_EXPORT
Definition: Export.h:38
double getBaseTime() const
Definition: BlinkSequence.h:41
#define META_Object(library, name)
Definition: Object.h:42
void setBaseTime(double t)
Definition: BlinkSequence.h:40