BlinkSequence 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef OSGSIM_BLINKSQUENCE
  14. #define OSGSIM_BLINKSQUENCE 1
  15. #include <osgSim/Export>
  16. #include <osg/Quat>
  17. #include <osg/Vec3>
  18. #include <osg/Vec4>
  19. #include <osg/Object>
  20. #include <osg/ref_ptr>
  21. #include <vector>
  22. namespace osgSim {
  23. /** sequence group which can be used to synchronize related blink sequences.*/
  24. class OSGSIM_EXPORT SequenceGroup : public osg::Object
  25. {
  26. public:
  27. SequenceGroup();
  28. SequenceGroup(const SequenceGroup& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  29. SequenceGroup(double baseTime);
  30. META_Object(osgSim,SequenceGroup);
  31. inline void setBaseTime( double t ) { _baseTime = t; }
  32. inline double getBaseTime() const { return _baseTime; }
  33. double _baseTime;
  34. };
  35. class OSGSIM_EXPORT BlinkSequence : public osg::Object
  36. {
  37. public:
  38. BlinkSequence();
  39. BlinkSequence(const BlinkSequence& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  40. META_Object(osgSim,BlinkSequence);
  41. /** add a pulse of specified color and duration to the BlinkSequence.*/
  42. inline void addPulse(double length,const osg::Vec4& color);
  43. /** return the number of pulses. */
  44. inline int getNumPulses() const { return _pulseData.size(); }
  45. /** return the pulse data at position i. */
  46. inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
  47. /** set pulse of specified color and duration to the BlinkSequence.*/
  48. inline void setPulse(unsigned int i,double length,const osg::Vec4& color);
  49. /** get the total pulse period of the blink sequence, which is equal to the sum of all the pulse periods.*/
  50. inline double getPulsePeriod() const { return _pulsePeriod; }
  51. /** set the sequence group which can be used to synchronize related blink sequences.*/
  52. inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
  53. /** get the non const sequence group.*/
  54. inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
  55. /** get the const sequence group.*/
  56. inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
  57. /** set the phase shift of the blink sequence, this would be used to shift a sequence within a sequence group.*/
  58. inline void setPhaseShift(double ps) { _phaseShift = ps; }
  59. /** get the pahse shift.*/
  60. inline double getPhaseShift() const { return _phaseShift; }
  61. /** compute the local time clamped to this BlinkSequences period, and accounting for the phase shift and sequence group.*/
  62. inline double localTime(double time) const;
  63. /** compute the color for the time interval sepecifed. Averages the colors if the length is greater than the current pulse.*/
  64. inline osg::Vec4 color(double time,double length) const;
  65. protected:
  66. typedef std::pair<double,osg::Vec4> IntervalColor;
  67. typedef std::vector<IntervalColor> PulseData;
  68. double _pulsePeriod;
  69. double _phaseShift;
  70. PulseData _pulseData;
  71. osg::ref_ptr<SequenceGroup> _sequenceGroup;
  72. };
  73. inline double BlinkSequence::localTime(double time) const
  74. {
  75. if (_sequenceGroup.valid()) time -= _sequenceGroup->_baseTime;
  76. time -= _phaseShift;
  77. return time - floor(time/_pulsePeriod)*_pulsePeriod;
  78. }
  79. inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
  80. {
  81. _pulseData.push_back(IntervalColor(length,color));
  82. _pulsePeriod += length;
  83. }
  84. inline void BlinkSequence::getPulse(unsigned int i, double& length, osg::Vec4& color) const
  85. {
  86. const IntervalColor& ic = _pulseData[i];
  87. length = ic.first;
  88. color = ic.second;
  89. }
  90. inline void BlinkSequence::setPulse(unsigned int i,double length,const osg::Vec4& color)
  91. {
  92. if( i >= _pulseData.size() ) return;
  93. IntervalColor& ic = _pulseData[i];
  94. ic.first = length;
  95. ic.second = color;
  96. }
  97. inline osg::Vec4 BlinkSequence::color(double time,double length) const
  98. {
  99. if (_pulseData.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
  100. double lt = localTime(time);
  101. PulseData::const_iterator itr = _pulseData.begin();
  102. // find the first sample at this time point.
  103. while (lt>itr->first)
  104. {
  105. lt -= itr->first;
  106. ++itr;
  107. if (itr==_pulseData.end()) itr = _pulseData.begin();
  108. }
  109. // if time interval fits inside the current pulse
  110. // then simply return this pulses color value.
  111. if (lt+length<=itr->first)
  112. {
  113. return itr->second;
  114. }
  115. // time length exceeds the current pulse therefore
  116. // we have to average out the pules to get the correct
  117. // results...
  118. // accumulate final part of the first active pulses.
  119. osg::Vec4 color(itr->second*(itr->first-lt));
  120. double len = length-(itr->first-lt);
  121. ++itr;
  122. if (itr==_pulseData.end()) itr = _pulseData.begin();
  123. // accumulate all the whole pluses pulses.
  124. while (len>itr->first)
  125. {
  126. len -= itr->first;
  127. color += itr->second*itr->first;
  128. ++itr;
  129. if (itr==_pulseData.end()) itr = _pulseData.begin();
  130. }
  131. // add remaining part of the final pulse.
  132. color += itr->second*len;
  133. // normalise the time waited color.
  134. color /= length;
  135. return color;
  136. }
  137. }
  138. #endif