vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SectorColorGen.h
Go to the documentation of this file.
1 #pragma once
2 #include "ColorGen.h"
3 #include <osg\BoundsChecking>
4 #include <osg\BufferObject>
5 
6 
7 
8 struct Sector : public osg::Referenced
9 {
10  enum Direction
11  {
14  };
15 
16  const unsigned int ID;
17  double start;
18  double end;
19  osg::Vec4 color;
21 
23  Sector() : ID(counter++) { set(0.0, 0.0, osg::Vec4(1,0,0,1)); }
24  Sector(double start, double end, const osg::Vec4& color, Direction direction = SD_FORWARD) : ID(counter++)
25  { set(start, end, color, direction); }
26 
28  typedef std::pair<double, double> DoublePair;
29  DoublePair getSectorRange(unsigned int arraySize) { return DoublePair(start * arraySize, end * arraySize); }
30 
31  void set(double start, double end, const osg::Vec4& color, Direction direction = SD_FORWARD) {
32  this->start = osg::clampBetween(start, 0.0, 1.0);
33  this->end = osg::clampBetween(end, 0.0, 1.0);
34  this->color = color;
35  this->direction = direction;
36  }
37 
38 protected:
39  static unsigned int counter;
40  virtual ~Sector() {};
41 };
42 
43 typedef osg::ref_ptr<Sector> SectorPtr;
44 typedef std::vector<SectorPtr> SectorArray;
45 
46 class SectorColorGen : public ColorGen
47 {
48 public:
49  SectorColorGen(const osg::Vec4& defaultColor)
50  : _defaultColor(defaultColor) { _dirty = true; };
51 
52  META_DAObject(SectorColorGen, "Sector Color");
53 
55  virtual Vec4ArrayPtr getColors( unsigned int leadSplineSize, unsigned int crossSectionSize )
56  {
57  _dirty = false;
58 
59  Vec4ArrayPtr result = new osg::Vec4Array;
60  unsigned int arraySize = leadSplineSize * crossSectionSize;
61 
62  result->reserve(arraySize);
63  result->assign(arraySize, _defaultColor);
64 
65  for (auto it = _sectors.begin(); it != _sectors.end(); ++it)
66  {
67  unsigned int sectorStart = int((*it)->start * leadSplineSize) * crossSectionSize;
68  unsigned int sectorEnd = int((*it)->end * leadSplineSize) * crossSectionSize;
69 
70  if(sectorStart == sectorEnd) continue;
71 
72  if(sectorEnd < sectorStart) sectorEnd += arraySize;
73 
74  double step = 1.0 / (sectorEnd - sectorStart);
75  double counter = (*it)->direction == Sector::SD_FORWARD ? 0.0 : 1.0;
76 
77  for (unsigned int i = sectorStart; i < sectorEnd; ++i)
78  {
79  (*result)[i % arraySize] = osg::Vec4(counter, 1.0 - counter, 0, 1.0);
80  counter += (*it)->direction == Sector::SD_FORWARD ? step : -step;
81  }
82  }
83 
84  return result;
85  }
86 
87  const osg::Vec4& getDefaultColor() const { return _defaultColor; }
88  void setDefaultColor(osg::Vec4 val) { _dirty = true; _defaultColor = val; }
89 
91  void addSector(SectorPtr sector) { _dirty = true; _sectors.push_back(sector); }
92  void setSector(unsigned int i, SectorPtr val) { _dirty = true; _sectors[i] = val; }
93  const SectorPtr getSector(unsigned int i) { return _sectors[i]; }
94  int getNumSectors() { return _sectors.size(); }
95  void removeSectors(unsigned int i, int numSectorsToRemove = 1) {
96  _dirty = true;
97  osg::clampLEQUAL(i, _sectors.size(), "i");
98  _sectors.erase(_sectors.begin() + i, _sectors.begin() + i + numSectorsToRemove);
99  }
100 
101 protected:
103  osg::Vec4 _defaultColor;
104 
105 };
106 
107 typedef osg::ref_ptr<SectorColorGen> SectorColorGenPtr;