vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <osg/io_utils>
4 #include "DATool.h"
5 #include "Spline.h"
6 
7 namespace APDYNAMICART
8 {
11 class Utils;
12 
14 typedef osg::ref_ptr<osg::UIntArray> UIntArrayPtr;
15 typedef osg::ref_ptr<osg::Vec3Array> Vec3ArrayPtr;
16 typedef osg::ref_ptr<osg::Vec4Array> Vec4ArrayPtr;
17 typedef osg::ref_ptr<osg::Geode> GeodePtr;
18 typedef osg::ref_ptr<osg::Geometry> GeometryPtr;
19 
20 typedef osg::ref_ptr<vreckoUtils::Menu> MenuPtr;
21 typedef osg::ref_ptr<vreckoUtils::MenuRoot> MenuRootPtr;
22 typedef osg::ref_ptr<vreckoUtils::IMenuComponent> IMenuComponentPtr;
23 
25 #define err(msg) \
26  std::cerr << __FUNCTION__<< " - " << msg << std::endl;
27 
29 #if 0
30 #define dbgAtt(msg) \
31  std::cout << msg << std::endl;
32 #else
33 #define dbgAtt(msg)
34 #endif
35 
37 #if 1
38 #define dbgPS(msg) \
39  std::cout << msg << std::endl;
40 #else
41 #define dbgPS(msg)
42 #endif
43 
45 class Stroke
46 {
47 public:
48  virtual Vec3ArrayPtr getPositionSamples() const = 0;
49 
50  //TODO ask Martin - what about this
51  virtual Vec4ArrayPtr getOrientationSamples() const = 0;
52 
53  virtual osg::ref_ptr<osg::DoubleArray> getTimeStamps() const = 0;
54  virtual SplinePtr getLeadingSpline() const = 0;
55 };
56 
58 template <class T>
59 class PoAAdapter : public osg::Referenced
60 {
61 public:
63  virtual void createMenu(osg::ref_ptr<vreckoUtils::Menu> menu) {}
64 
66  virtual osg::ref_ptr<T> getObject() = 0;
67 
68  virtual bool itemClicked(const char* itemID) { return false; }
69 
71  virtual int load( XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* ParametersNode ) { return 0; }
72 
74  virtual void pointed( bool pointed ) {}
75 
77  virtual void save(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* Document, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* poaNode) {}
78 
79 
80  virtual bool sliderChanged(const char* sliderID, float sliderPos) { return false; }
81  virtual void update() {}
82 
83  virtual std::string classDescription() const = 0;
84 
85  virtual std::string className() const = 0;
86 protected:
87  virtual ~PoAAdapter() {};
88 };
89 
91 template <class T>
93 {
94 public:
96  const std::vector<osg::ref_ptr<T> >& getSupportedAdapters() const { return _adapters; }
97 
99  osg::ref_ptr<T> getAdapter(const std::string& className)
100  {
101  for (u_int i = 0; i < this->_adapters.size(); ++i)
102  {
103  if (this->_adapters[i]->className() == className)
104  return this->_adapters[i];
105  }
106  return NULL;
107  }
108 
109 protected:
110  std::vector<osg::ref_ptr<T> > _adapters;
111 };
112 
114 class Utils
115 {
116 public:
117  static void interpolateVec3(osg::Vec3Array* controlPoints, osg::Vec3Array* interpolatedPoints, int p_steps);
118  static osg::Vec3 interpolatePoint(double t, osg::Vec3 point1, osg::Vec3 point2, osg::Vec3 point0, osg::Vec3 point3);
119  static bool isValueInsideRange( double value, double rangeStart, double rangeEnd );
120 
121  inline static DATool* createDATool( const std::string& toolName, vrecko::BaseClass* owner) {
122  return static_cast<DATool*>(world->createAbility("DynamicArt", toolName, owner));
123  }
124 
125  inline static void disableShaders(osg::StateSet* stateSet) {
126  stateSet->setAttributeAndModes(new osg::Program());
127  }
128  static void setLayout(const std::string& name);
129 
130 
131  // functions for splitting a string by defined delimiter
132  static std::vector<std::string> &splitString(const std::string &s, char delim, std::vector<std::string> &elems) {
133  std::stringstream ss(s);
134  std::string item;
135  while(std::getline(ss, item, delim)) {
136  elems.push_back(item);
137  }
138  return elems;
139  }
140 
141  static std::vector<std::string> splitString(const std::string &s, char delim) {
142  std::vector<std::string> elems;
143  splitString(s, delim, elems);
144  return elems;
145  }
146 
147  static string convertInt(int number)
148  {
149  stringstream ss;//create a stringstream
150  ss << number;//add number to the stream
151  return ss.str();//return a string with the contents of the stream
152  }
153 
154  static int convertString(string number)
155  {
156  istringstream is(number);
157  int num;
158  is >> num;
159  return num;
160  }
161 
162 
163 private:
164  Utils() {};
165 };
166 
168 struct Limits
169 {
170  double minX, minY, minZ;
171  double maxX, maxY, maxZ;
172 
173  inline void reset() {
174  minX = minY = minZ = INT_MAX;
175  maxX = maxY = maxZ = INT_MIN;
176  }
177 
178  inline void adjustLimits( const osg::Vec3& point ) {
179  double x = point._v[0]; double y = point._v[1]; double z = point._v[2];
180 
181  if(x < minX) minX = x; if(x > maxX) maxX = x; //X-axis
182  if(y < minY) minY = y; if(y > maxY) maxY = y; //Y-axis
183  if(z < minZ) minZ = z; if(z > maxZ) maxZ = z; //Z-axis
184  }
185 
186  inline double getVolume() {
187  return (maxX - minX) * (maxY - minY) * (maxZ - minZ);
188  }
189 };
190 
191 typedef osg::ref_ptr<std::vector<osg::Vec4>> ColorList;
192 
200 {
201 public:
202  osg::Vec4 getColor(double val);
203  void addColor(osg::Vec4 color) { _colors.push_back(color); }
204 
205 private:
206 
207  //TODO value range can be defined here somehow
208  std::vector<osg::Vec4> _colors;
209 
210 };
211 
213 class PoAState
214 {
215 public:
216  void saveState(ref_ptr_PoA poa) {
217  _stateMap[poa] = PosRot(poa->getPosition(), poa->getRotation().getRotate());
218  }
220  auto it = _stateMap.find(poa);
221  if(it == _stateMap.end()) { err("State of given PoA wasn't found!"); return; }
222  _stateMap.erase(it);
223  }
224 
226  for(auto it = _stateMap.begin(); it != _stateMap.end(); ++it)
227  {
228  if(it->first.valid()) {
229  it->first->setPosition(it->second.first);
230  it->first->setRotation(it->second.second);
231  }
232  }
233  }
234 
235 protected:
236  typedef std::pair<osg::Vec3, osg::Quat> PosRot;
237  typedef std::map<ref_ptr_PoA, PosRot> StateMap;
238 
240 };
241 
242 
243 }