Privacy
An open-source, flexible 3D physical simulation framework
gui_helper_functions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "gui_helper_functions.h"
22 #include <iostream>
23 #include <osg/TriangleFunctor>
24 #include <osgDB/ReadFile>
25 
26 //#include <osgDB/WriteFile>
27 #include <osg/MatrixTransform>
28 #include <osg/ComputeBoundsVisitor>
29 #include <osgUtil/Optimizer>
30 
31 #ifdef HAVE_OSG_VERSION_H
32  #include <osg/Version>
33 #else
34  #include <osg/Export>
35 #endif
36 
37 #ifdef WIN32
38  #include <cv.h>
39  #include <highgui.h>
40 #else
41  #include <opencv/cv.h>
42  #include <opencv/highgui.h>
43 #endif
44 
45 #include <mars/utils/mathUtils.h>
46 
47 namespace mars {
48  namespace graphics {
49 
50  using namespace std;
51  using mars::utils::Color;
52  using mars::utils::Vector;
55 
56  vector<nodeFileStruct> GuiHelper::nodeFiles;
57  vector<textureFileStruct> GuiHelper::textureFiles;
58  vector<imageFileStruct> GuiHelper::imageFiles;
59 
61 
62  osg::Vec4 toOSGVec4(const Color &col)
63  {
64  return osg::Vec4(col.r, col.g, col.b, col.a);
65  }
66  osg::Vec4 toOSGVec4(const Vector &v, float w)
67  {
68  return osg::Vec4(v.x(), v.y(), v.z(), w);
69  }
70 
72 
73  GeodeVisitor::GeodeVisitor(const std::string name):osg::NodeVisitor(TRAVERSE_ALL_CHILDREN){
74  resultNode=NULL;
75  this->name=name;
76  }
77 
78  void GeodeVisitor::apply(osg::Node &searchNode){
79  //go through node and return geode
80  osg::Geode* dynamicTry=dynamic_cast<osg::Geode*>(&searchNode);
81  if(dynamicTry) {
82  resultNode=dynamicTry;
83  }
84  else resultNode = 0;
85  traverse(searchNode);
86  }
87 
88  osg::Geode* GeodeVisitor::getNode(){
89  return resultNode;
90  }
91 
93 
96  public:
97  // This will store all triangle vertices.
98  std::vector<osg::Vec3> vertices;
99 
100  // When triangle vertices are added to 'vertices', they'll be transformed
101  // by this matrix. This is useful because 'osg::TriangleFunctor' operates
102  // on the model coordinate system, and we want do draw our normals in the
103  // world coordinate system.
104  osg::Matrix transformMatrix;
105 
106  // This will be called once for each triangle in the geometry. As
107  // parameters, it takes the three triangle vertices and a boolean
108  // parameter indicating if the vertices are coming from a "real" vertex
109  // array or from a temporary vertex array created from some other geometry
110  // representation.
111  // The implementation is quite simple: we just store the vertices
112  // (transformed by 'transformMatrix') in a 'std::vector'.
113 #if (OPENSCENEGRAPH_MAJOR_VERSION < 3 || ( OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION < 5) || ( OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION == 5 && OPENSCENEGRAPH_PATCH_VERSION < 9))
114  void operator() (const osg::Vec3& v1, const osg::Vec3& v2,
115  const osg::Vec3& v3, bool treatVertexDataAsTemporary) {
116  (void)treatVertexDataAsTemporary;
117 #elif (OPENSCENEGRAPH_MAJOR_VERSION > 3 || (OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION > 5) || (OPENSCENEGRAPH_MAJOR_VERSION == 3 && OPENSCENEGRAPH_MINOR_VERSION == 5 && OPENSCENEGRAPH_PATCH_VERSION >= 9))
118  void operator() (const osg::Vec3& v1, const osg::Vec3& v2,
119  const osg::Vec3& v3) {
120 #else
121 #error Unknown OSG Version
122 #endif
123  vertices.push_back (v1 * transformMatrix);
124  vertices.push_back (v2 * transformMatrix);
125  vertices.push_back (v3 * transformMatrix);
126  }
127 
128  vector<osg::Vec3>& getVertices() {
129  return vertices;
130  }
131  };
132 
134 
136  this->gi = gi;
137  }
138 
139  /*
140  void GuiHelper::setGraphicsWidget(GraphicsWidget *widget){
141  this->gw = widget;
142  }
143 
144  bool GuiHelper::validateGraphicsWidget(void){
145  return ( this->gw != NULL && this->gw != 0 );
146  }
147  */
148 
149  void GuiHelper::clearStates(osg::ref_ptr<osg::Node> node) {
150  osg::ref_ptr<osg::Group> group;
151  if(node != 0) {
152  node->getOrCreateStateSet()->clear();
153  group = node->asGroup();
154  if (group != 0)
155  for (unsigned int i=0; i<group->getNumChildren(); i++)
156  clearStates(group->getChild(i));
157  }
158  }
159 
163  snmesh GuiHelper::convertOsgNodeToSnMesh(osg::Node* node, double scaleX,
164  double scaleY, double scaleZ,
165  double pivotX, double pivotY,
166  double pivotZ) {
167  vector<int> indices;
168  vector<osg::Vec3> OSGvertices;
169  snmesh mesh;
170  //visitor for getting drawables inside node
171  GeodeVisitor* visitor=new GeodeVisitor("PLACEHOLDER");
172  osg::Geode* geode;
173  osg::Node* tmpNode;
174  osg::ref_ptr<osg::Group> osgGroupFromRead = NULL;
175  if ((osgGroupFromRead = node->asGroup()) == 0)
176  fprintf(stderr, "error\n");
177 
178  // todo: parse the tree recursive and search for drawables
179  //get geometries of node
180  int indexcounter = 0;
181  for (size_t m = 0; m < osgGroupFromRead->getNumChildren(); m++) {
182  tmpNode = osgGroupFromRead->getChild(m);
183  tmpNode->accept(*visitor);
184  geode = visitor->getNode();
185  for (size_t j = 0; j<geode->getNumDrawables(); j++) {
186  //initialize functors for getting the drawables
187  osg::TriangleFunctor<GetVerticesFunctor> triangleFunctor_;
188  geode->getDrawable(j)->accept(triangleFunctor_);
189 
190  //Here we get the triangles
191  vector<osg::Vec3>& OSGverticestemp = triangleFunctor_.getVertices();
192  for (unsigned int i = 0; i < OSGverticestemp.size(); i++) {
193  OSGvertices.push_back(OSGverticestemp[i]);
194  indices.push_back(indexcounter);
195  indexcounter++;
196  }
197  }
198  }
199 
200  //store vertices in a mydVector3 structure
201  mars::interfaces::mydVector3 *vertices = 0;
202  if(OSGvertices.size() > 0){
203  vertices = new mars::interfaces::mydVector3[OSGvertices.size()];
204  }
205  // dVector3 *normals = new dVector3[normals_x.size()];
206  int *indexarray = 0;
207  if(indices.size() > 0){
208  indexarray = new int[indices.size()];
209  }
210 
211  //convert osg vertice vector to standard array
212  for (size_t i = 0; i < OSGvertices.size(); i++) {
213  vertices[i][0] = (OSGvertices[i][0] - pivotX) * scaleX;
214  vertices[i][1] = (OSGvertices[i][1] - pivotY) * scaleY;
215  vertices[i][2] = (OSGvertices[i][2] - pivotZ) * scaleZ;
216  }
217 
218  //construct an appropriate index array
219  for (int i = 0; i < indexcounter; i++) {
220  indexarray[i] = indices[i];
221  }
222 
223  mesh.vertices = vertices;
224  mesh.vertexcount = OSGvertices.size();
225  // mesh.normals = normals;
226  mesh.indices = indexarray;
227  mesh.indexcount = indexcounter;
228  delete visitor;
229  return mesh;
230  }
231 
232  Vector GuiHelper::getExtend(osg::Node *oGroup){
233  osg::ComputeBoundsVisitor cbbv;
234  oGroup->accept(cbbv);
235  osg::BoundingBox bb = cbbv.getBoundingBox();
236  Vector ex;
237  // compute bounding box has to be done in this way
238  (fabs(bb.xMax()) > fabs(bb.xMin())) ? ex.x() = fabs(bb.xMax() - bb.xMin())
239  : ex.x() = fabs(bb.xMin() - bb.xMax());
240  (fabs(bb.yMax()) > fabs(bb.yMin())) ? ex.y() = fabs(bb.yMax() - bb.yMin())
241  : ex.y() = fabs(bb.yMin() - bb.yMax());
242  (fabs(bb.zMax()) > fabs(bb.zMin())) ? ex.z() = fabs(bb.zMax() - bb.zMin())
243  : ex.z() = fabs(bb.zMin() - bb.zMax());
244 
245  return ex;
246  }
247 
248  std::vector<double> GuiHelper::getMeshSize(const std::string &filename) {
249  std::vector<double> r;
250  Vector size(0, 0, 0);
251  if(filename.substr(filename.size()-5, 5) == ".bobj") {
252  size = getExtend(GuiHelper::readBobjFromFile(filename));
253  }
254  else {
255  size = getExtend(GuiHelper::readNodeFromFile(filename));
256  }
257  r.push_back(size.x());
258  r.push_back(size.y());
259  r.push_back(size.z());
260  return r;
261  }
262 
264  if(node->filename.substr(node->filename.size()-5, 5) == ".bobj") {
265  getPhysicsFromNode(node, GuiHelper::readBobjFromFile(node->filename));
266  }
267  else {
268  getPhysicsFromNode(node, GuiHelper::readNodeFromFile(node->filename));
269  }
270  }
271 
273  osg::ref_ptr<osg::Node> completeNode) {
274  osg::ref_ptr<osg::Group> myCreatedGroup;
275  osg::ref_ptr<osg::Group> myGroupFromRead;
276  osg::ref_ptr<osg::Geode> myGeodeFromRead;
277  nodemanager tempnode;
278  bool found = false;
279  // check whether it is a osg::Group (.obj file)
280  if(!completeNode.valid()){
281  throw std::runtime_error("cannot read node from file");
282  }
283 
284  if((myGroupFromRead = completeNode->asGroup()) != 0){
285  //go through the read node group and combine the parts of the actually
286  //handled node
287  myCreatedGroup = new osg::Group();
288  osg::ref_ptr<osg::StateSet> stateset = myCreatedGroup->getOrCreateStateSet();
289  for (unsigned int i = 0; i < myGroupFromRead->getNumChildren(); i ++) {
290  osg::ref_ptr<osg::Node> myTestingNode = myGroupFromRead->getChild(i);
291  if (myTestingNode == 0) {
292  return;
293  }
294  if (myTestingNode->getName() == node->origName) {
295  myTestingNode->setStateSet(stateset.get());
296  myCreatedGroup->addChild(myTestingNode.get());
297  found = true;
298  } else {
299  if (found) {
300  break;
301  found = false;
302  }
303  }
304  }
305  }
306  // or if it is a osg::Geode (.stl file)
307  else if((myGeodeFromRead = completeNode->asGeode()) != 0) {
308  //if the node was read from a .stl-file it read as geode not as group
309  myCreatedGroup = new osg::Group();
310  osg::ref_ptr<osg::StateSet> stateset = myCreatedGroup->getOrCreateStateSet();
311  completeNode->setStateSet(stateset.get());
312  myCreatedGroup->addChild(completeNode.get());
313  }
314 
315  osg::ComputeBoundsVisitor cbbv;
316  myCreatedGroup.get()->accept(cbbv);
317  osg::BoundingBox bb = cbbv.getBoundingBox();
318  Vector ex;
319  // compute bounding box has to be done in this way
320  (fabs(bb.xMax()) > fabs(bb.xMin())) ? ex.x() = fabs(bb.xMax() - bb.xMin())
321  : ex.x() = fabs(bb.xMin() - bb.xMax());
322  (fabs(bb.yMax()) > fabs(bb.yMin())) ? ex.y() = fabs(bb.yMax() - bb.yMin())
323  : ex.y() = fabs(bb.yMin() - bb.yMax());
324  (fabs(bb.zMax()) > fabs(bb.zMin())) ? ex.z() = fabs(bb.zMax() - bb.zMin())
325  : ex.z() = fabs(bb.zMin() - bb.zMax());
326 
327  if (node->map.find("loadSizeFromMesh") != node->map.end()) {
328  if (node->map["loadSizeFromMesh"]) {
329  Vector physicalScale;
330  utils::vectorFromConfigItem(&(node->map["physicalScale"][0]), &physicalScale);
331  node->ext=Vector(ex.x()*physicalScale.x(), ex.y()*physicalScale.y(), ex.z()*physicalScale.z());
332  }
333  }
334 
335  //compute scale factor
336  double scaleX = 1, scaleY = 1, scaleZ = 1;
337  if (ex.x() != 0) scaleX = node->ext.x() / ex.x();
338  if (ex.y() != 0) scaleY = node->ext.y() / ex.y();
339  if (ex.z() != 0) scaleZ = node->ext.z() / ex.z();
340 
341  // create transform and group Node for the actual node
342  osg::ref_ptr<osg::PositionAttitudeTransform> transform;
343  osg::ref_ptr<osg::MatrixTransform> tx;
344 
345  transform = new osg::PositionAttitudeTransform();
346  tx = new osg::MatrixTransform;
347  tx->setMatrix(osg::Matrix::scale(scaleX, scaleY, scaleZ));
348  tx->setDataVariance(osg::Node::STATIC);
349  tx->addChild(myCreatedGroup.get());
350 
351  //add the node to a transformation to make him movable
352  transform->addChild(tx.get());
353  transform->setPivotPoint(osg::Vec3(node->pivot.x()*scaleX,
354  node->pivot.y()*scaleY,
355  node->pivot.z()*scaleZ));
356  transform->setPosition(osg::Vec3(node->pos.x() + node->visual_offset_pos.x(),
357  node->pos.y() + node->visual_offset_pos.y(),
358  node->pos.z() + node->visual_offset_pos.z()));
359  //set rotation
360  osg::Quat oquat;
361  Quaternion qrot = node->rot * node->visual_offset_rot;
362  oquat.set(qrot.x(), qrot.y(), qrot.z(), qrot.w());
363  transform->setAttitude(oquat);
364 
365  tempnode.transform = transform.get();
366  tempnode.node = myCreatedGroup.get();
367  tempnode.matrix = tx.get();
368  tempnode.offset = node->visual_offset_pos;
369  tempnode.r_off = node->visual_offset_rot;
370 
371  node->mesh = GuiHelper::convertOsgNodeToSnMesh(tempnode.node.get(),
372  scaleX, scaleY, scaleZ,
373  node->pivot.x(),
374  node->pivot.y(),
375  node->pivot.z());
376  }
377 
378  osg::ref_ptr<osg::Node> GuiHelper::readNodeFromFile(string fileName) {
379  std::vector<nodeFileStruct>::iterator iter;
380 
381  for(iter = GuiHelper::nodeFiles.begin();
382  iter != GuiHelper::nodeFiles.end(); iter++) {
383  if((*iter).fileName == fileName) return (*iter).node;
384  }
385  nodeFileStruct newNodeFile;
386  newNodeFile.fileName = fileName;
387  newNodeFile.node = osgDB::readNodeFile(fileName);
388  GuiHelper::nodeFiles.push_back(newNodeFile);
389  return newNodeFile.node;
390  }
391 
392 
393  osg::ref_ptr<osg::Node> GuiHelper::readBobjFromFile(const std::string &filename) {
394 
395  std::vector<nodeFileStruct>::iterator iter;
396 
397  for(iter = GuiHelper::nodeFiles.begin();
398  iter != GuiHelper::nodeFiles.end(); iter++) {
399  if((*iter).fileName == filename) return (*iter).node;
400  }
401  nodeFileStruct newNodeFile;
402  newNodeFile.fileName = filename;
403 
404  FILE* input = fopen(filename.c_str(), "rb");
405  if(!input) return 0;
406 
407  char buffer[312];
408 
409  int da, i, r, o, foo=0;
410  int iData[3];
411  float fData[4];
412 
413  osg::Geode *geode = new osg::Geode();
414  std::vector<osg::Vec3> vertices;
415  std::vector<osg::Vec3> normals;
416  std::vector<osg::Vec2> texcoords;
417 
418  std::vector<osg::Vec3> vertices2;
419  std::vector<osg::Vec3> normals2;
420  std::vector<osg::Vec2> texcoords2;
421 
422  osg::ref_ptr<osg::Vec3Array> osgVertices = new osg::Vec3Array();
423  osg::ref_ptr<osg::Vec2Array> osgTexcoords = new osg::Vec2Array();
424  osg::ref_ptr<osg::Vec3Array> osgNormals = new osg::Vec3Array();
425  osg::ref_ptr<osg::DrawElementsUInt> osgIndices = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
426  bool useIndices = true;
427  while((r = fread(buffer+foo, 1, 256, input)) > 0 ) {
428  o = 0;
429  while(o < r+foo-50 || (r<256 && o < r+foo)) {
430  da = *(int*)(buffer+o);
431  o += 4;
432  if(da == 1) {
433  for(i=0; i<3; i++) {
434  fData[i] = *(float*)(buffer+o);
435  o+=4;
436  }
437  vertices.push_back(osg::Vec3(fData[0], fData[1], fData[2]));
438  }
439  else if(da == 2) {
440  for(i=0; i<2; i++) {
441  fData[i] = *(float*)(buffer+o);
442  o+=4;
443  }
444  texcoords.push_back(osg::Vec2(fData[0], fData[1]));
445  }
446  else if(da == 3) {
447  for(i=0; i<3; i++) {
448  fData[i] = *(float*)(buffer+o);
449  o+=4;
450  }
451  normals.push_back(osg::Vec3(fData[0], fData[1], fData[2]));
452  }
453  else if(da == 4) {
454  // 1. vertice
455  for(i=0; i<3; i++) {
456  iData[i] = *(int*)(buffer+o);
457  o+=4;
458  }
459  if(iData[0] != iData[2]) {
460  useIndices = false;
461  }
462  // add osg vertices etc.
463  osgIndices->push_back(iData[0]-1);
464  vertices2.push_back(vertices[iData[0]-1]);
465  if(iData[1] > 0) {
466  texcoords2.push_back(texcoords[iData[1]-1]);
467  }
468  normals2.push_back(normals[iData[2]-1]);
469 
470  // 2. vertice
471  for(i=0; i<3; i++) {
472  iData[i] = *(int*)(buffer+o);
473  o+=4;
474  }
475  if(iData[0] != iData[2]) {
476  useIndices = false;
477  }
478  osgIndices->push_back(iData[0]-1);
479  // add osg vertices etc.
480  vertices2.push_back(vertices[iData[0]-1]);
481  if(iData[1] > 0) {
482  texcoords2.push_back(texcoords[iData[1]-1]);
483  }
484  normals2.push_back(normals[iData[2]-1]);
485 
486  // 3. vertice
487  for(i=0; i<3; i++) {
488  iData[i] = *(int*)(buffer+o);
489  o+=4;
490  }
491  if(iData[0] != iData[2]) {
492  useIndices = false;
493  }
494  osgIndices->push_back(iData[0]-1);
495  // add osg vertices etc.
496  vertices2.push_back(vertices[iData[0]-1]);
497  if(iData[1] > 0) {
498  texcoords2.push_back(texcoords[iData[1]-1]);
499  }
500  normals2.push_back(normals[iData[2]-1]);
501  }
502  }
503  foo = r+foo-o;
504  if(r==256) memcpy(buffer, buffer+o, foo);
505  }
506 
507  if(useIndices) {
508  for(size_t i=0; i<vertices.size(); ++i) {
509  osgVertices->push_back(vertices[i]);
510  osgNormals->push_back(normals[i]);
511  if(texcoords.size() > i) {
512  osgTexcoords->push_back(texcoords[i]);
513  }
514  }
515  }
516  else {
517  for(size_t i=0; i<vertices2.size(); ++i) {
518  osgVertices->push_back(vertices2[i]);
519  }
520  for(size_t i=0; i<normals2.size(); ++i) {
521  osgNormals->push_back(normals2[i]);
522  }
523  for(size_t i=0; i<texcoords2.size(); ++i) {
524  osgTexcoords->push_back(texcoords2[i]);
525  }
526  }
527 
528  osg::Geometry* geometry = new osg::Geometry;
529  geometry->setVertexArray(osgVertices.get());
530  geometry->setNormalArray(osgNormals.get());
531  geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
532  if(osgTexcoords->size() > 0) {
533  geometry->setTexCoordArray(0, osgTexcoords.get());
534  }
535  if(useIndices) {
536  geometry->addPrimitiveSet(osgIndices);
537  }
538  else {
539  osg::DrawArrays* drawArrays = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,osgVertices->size());
540  geometry->addPrimitiveSet(drawArrays);
541  }
542  geode->addDrawable(geometry);
543  geode->setName("bobj");
544 
545  fclose(input);
546  osgUtil::Optimizer optimizer;
547  optimizer.optimize( geode );
548 
549  newNodeFile.node = geode;
550  GuiHelper::nodeFiles.push_back(newNodeFile);
551  return newNodeFile.node;
552  }
553 
554  // TODO: should not be in graphics!
555  // physics without graphics would need this too.
556  // maybe move to NodeFactory ??
558 
559 #if !defined (WIN32) && !defined (__linux__)
560  IplImage* img=0;
561 
562  img=cvLoadImage(terrain->srcname.data(), -1);
563  if(img) {
564  terrain->width = img->width;
565  terrain->height = img->height;
566  fprintf(stderr, "w h = %d %d\n", img->width, img->height);
567  terrain->pixelData = (double*)calloc((terrain->width*
568  terrain->height),
569  sizeof(double));
570 
571 
572  CvScalar s;
573  int count = 0;
574  double imageMaxValue = pow(2., img->depth);
575  //for(int y=0; y<terrain->height; y++) {
576  //for(int x=terrain->width-1; x>=0; x--) {
577  for(int y=terrain->height-1; y>=0; y--) {
578  for(int x=0; x<terrain->width; x++) {
579 
580  s=cvGet2D(img,y,x);
581  terrain->pixelData[count++] = ((double)s.val[0])/(imageMaxValue-1);
582  // if(y==0 || y == terrain->height-1 ||
583  // x==0 || x == terrain->width-1)
584  // terrain->pixelData[count-1] -= 0.002;
585  // if(terrain->pixelData[count-1] <= 0.0)
586  // terrain->pixelData[count-1] = 0.001;
587 
588  }
589  }
590  cvReleaseImage(&img);
591  }
592 
593 #else
594  //QImage image(QString::fromStdString(snode->filename));
595 #ifdef __linux__
596 
597  std::string absolutePathIndicator = terrain->srcname.substr(0,1);
598  if( absolutePathIndicator != "/") {
599  fprintf(stderr, "Terrain file needs to be provided by using an absolute path: currently given \"%s\"\n", terrain->srcname.c_str());
600  // exit(0);
601  }
602 #endif
603 
604  osg::Image* image = osgDB::readImageFile(terrain->srcname);
605 
606  if(image) {
607  terrain->width = image->s();
608  terrain->height = image->t();
609  //if(terrain->pixelData) free(terrain->pixelData);
610 
611  terrain->pixelData = (double*)calloc((terrain->width*
612  terrain->height),
613  sizeof(double));
614  //image->setPixelFormat(GL_RGB);
615  //image->setDataType(GL_UNSIGNED_SHORT);
616  //osgDB::writeImageFile(*image, std::string("test.jpg"));
617  float r = 0;
618  //float g = 0, b = 0;
619  int count = 0;
620  osg::Vec4 pixel;
621  for (int y = terrain->height; y >= 1; --y){
622  for (int x = terrain->width; x >= 1; --x){
623  pixel = image->getColor(osg::Vec2(x, y));
624  r = pixel[0];
625  //g = pixel[1];
626  //b = pixel[2];
627  //QColor converter(image.pixel(x, y));
628  //converter.getRgb(&r, &g, &b);
629  //convert to greyscale by common used scale
630  terrain->pixelData[count++] = r;//((r*0.3+g*0.59+b*0.11));
631  }
632  }
633  }
634 #endif
635  }
636 
637  osg::ref_ptr<osg::Texture2D> GuiHelper::loadTexture(string filename) {
638  std::vector<textureFileStruct>::iterator iter;
639 
640  for (iter = textureFiles.begin();
641  iter != textureFiles.end(); iter++) {
642  if ((*iter).fileName == filename) {
643  return (*iter).texture;
644  }
645  }
646  textureFileStruct newTextureFile;
647  newTextureFile.fileName = filename;
648  newTextureFile.texture = new osg::Texture2D;
649  newTextureFile.texture->setDataVariance(osg::Object::DYNAMIC);
650  newTextureFile.texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
651  newTextureFile.texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
652  newTextureFile.texture->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT);
653 
654  osg::Image* textureImage = loadImage(filename);
655  newTextureFile.texture->setImage(textureImage);
656  textureFiles.push_back(newTextureFile);
657 
658  return newTextureFile.texture;
659  }
660 
661  osg::ref_ptr<osg::Image> GuiHelper::loadImage(string filename) {
662  std::vector<imageFileStruct>::iterator iter;
663 
664  for (iter = imageFiles.begin();
665  iter != imageFiles.end(); iter++) {
666  if ((*iter).fileName == filename) {
667  return (*iter).image;
668  }
669  }
670  imageFileStruct newImageFile;
671  newImageFile.fileName = filename;
672  osg::Image* image = osgDB::readImageFile(filename);
673  newImageFile.image = image;
674  imageFiles.push_back(newImageFile);
675 
676  return newImageFile.image;
677  }
678 
679  } // end of namespace graphics
680 } // end of namespace mars
utils::Vector pos
The position of the node.
Definition: NodeData.h:192
std::string origName
The original object name; needed for importing objects from files (like wavefront object files)...
Definition: NodeData.h:151
configmaps::ConfigMap map
If the data is created from a ConfigMap map the original map is stored here.
Definition: NodeData.h:384
used to get a geode from a nodegroup
utils::Quaternion rot
This quaternion describes the orientation of the node.
Definition: NodeData.h:205
osg::ref_ptr< osg::Node > node
static std::vector< nodeFileStruct > nodeFiles
static mars::interfaces::snmesh convertOsgNodeToSnMesh(osg::Node *node, double scaleX, double scaleY, double scaleZ, double pivotX, double pivotY, double pivotZ)
converts the mesh of an osgNode to the snmesh struct
utils::Vector visual_offset_pos
The visual representation of a node can have a different position as the physical representation...
Definition: NodeData.h:305
osg::ref_ptr< osg::MatrixTransform > matrix
osg::ref_ptr< osg::Group > node
terrainStruct is a struct to exchange height maps between the GUI and the simulation ...
Definition: terrainStruct.h:34
NodeData is a struct to exchange node information between the GUI and the simulation.
Definition: NodeData.h:52
bool vectorFromConfigItem(ConfigItem *item, Vector *v)
Definition: mathUtils.cpp:267
u_int8_t r
Definition: CameraSensor.h:41
std::string filename
The filename of the node to load the visual representation from.
Definition: NodeData.h:159
virtual void apply(osg::Node &searchNode)
static osg::ref_ptr< osg::Node > readBobjFromFile(const std::string &filename)
utils::Quaternion visual_offset_rot
In the same way as the visual_offset_pos, this quaternion can be used to differentiate the orientatio...
Definition: NodeData.h:313
static osg::ref_ptr< osg::Image > loadImage(std::string filename)
virtual std::vector< double > getMeshSize(const std::string &filename)
osg::ref_ptr< osg::Image > image
This visitor class is used to get triangles from a mesh.
mars::utils::Quaternion r_off
static std::vector< imageFileStruct > imageFiles
GeodeVisitor(const std::string name)
virtual void getPhysicsFromMesh(mars::interfaces::NodeData *node)
osg::Vec4 toOSGVec4(const Color &col)
utils::Vector pivot
The pivot point is set automatically by importing objects.
Definition: NodeData.h:198
osg::ref_ptr< osg::PositionAttitudeTransform > transform
Copyright 2012, DFKI GmbH Robotics Innovation Center.
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector
Definition: Vector.h:43
Eigen::Quaternion< double, Eigen::DontAlign > Quaternion
Definition: Quaternion.h:56
mars::utils::Vector getExtend(osg::Node *oGroup)
virtual void readPixelData(mars::interfaces::terrainStruct *terrain)
void getPhysicsFromNode(mars::interfaces::NodeData *node, osg::ref_ptr< osg::Node > completeNode)
GuiHelper(interfaces::GraphicsManagerInterface *gi)
static void clearStates(osg::ref_ptr< osg::Node > node)
snmesh mesh
The mesh struct stores the poly information for a physical representation of a mesh.
Definition: NodeData.h:255
static osg::ref_ptr< osg::Texture2D > loadTexture(std::string filename)
osg::ref_ptr< osg::Texture2D > texture
utils::Vector ext
The size of the node.
Definition: NodeData.h:247
static std::vector< textureFileStruct > textureFiles
sReal mydVector3[4]
Definition: MARSDefs.h:50
static osg::ref_ptr< osg::Node > readNodeFromFile(std::string fileName)
internal struct for GraphicManager.h connecting the node indices with the osg nodes ...
mydVector3 * vertices
Definition: snmesh.h:47