23 #include <osg/TriangleFunctor> 24 #include <osgDB/ReadFile> 27 #include <osg/MatrixTransform> 28 #include <osg/ComputeBoundsVisitor> 29 #include <osgUtil/Optimizer> 31 #ifdef HAVE_OSG_VERSION_H 32 #include <osg/Version> 41 #include <opencv/cv.h> 42 #include <opencv/highgui.h> 45 #include <mars/utils/mathUtils.h> 64 return osg::Vec4(col.
r, col.
g, col.
b, col.
a);
68 return osg::Vec4(v.x(), v.y(), v.z(), w);
80 osg::Geode* dynamicTry=
dynamic_cast<osg::Geode*
>(&searchNode);
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) {
121 #error Unknown OSG Version 123 vertices.push_back (v1 * transformMatrix);
124 vertices.push_back (v2 * transformMatrix);
125 vertices.push_back (v3 * transformMatrix);
150 osg::ref_ptr<osg::Group> group;
152 node->getOrCreateStateSet()->clear();
153 group = node->asGroup();
155 for (
unsigned int i=0; i<group->getNumChildren(); i++)
156 clearStates(group->getChild(i));
164 double scaleY,
double scaleZ,
165 double pivotX,
double pivotY,
168 vector<osg::Vec3> OSGvertices;
174 osg::ref_ptr<osg::Group> osgGroupFromRead = NULL;
175 if ((osgGroupFromRead = node->asGroup()) == 0)
176 fprintf(stderr,
"error\n");
180 int indexcounter = 0;
181 for (
size_t m = 0; m < osgGroupFromRead->getNumChildren(); m++) {
182 tmpNode = osgGroupFromRead->getChild(m);
183 tmpNode->accept(*visitor);
185 for (
size_t j = 0; j<geode->getNumDrawables(); j++) {
187 osg::TriangleFunctor<GetVerticesFunctor> triangleFunctor_;
188 geode->getDrawable(j)->accept(triangleFunctor_);
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);
202 if(OSGvertices.size() > 0){
207 if(indices.size() > 0){
208 indexarray =
new int[indices.size()];
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;
219 for (
int i = 0; i < indexcounter; i++) {
220 indexarray[i] = indices[i];
233 osg::ComputeBoundsVisitor cbbv;
234 oGroup->accept(cbbv);
235 osg::BoundingBox bb = cbbv.getBoundingBox();
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());
249 std::vector<double>
r;
251 if(filename.substr(filename.size()-5, 5) ==
".bobj") {
257 r.push_back(size.x());
258 r.push_back(size.y());
259 r.push_back(size.z());
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;
280 if(!completeNode.valid()){
281 throw std::runtime_error(
"cannot read node from file");
284 if((myGroupFromRead = completeNode->asGroup()) != 0){
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) {
294 if (myTestingNode->getName() == node->
origName) {
295 myTestingNode->setStateSet(stateset.get());
296 myCreatedGroup->addChild(myTestingNode.get());
307 else if((myGeodeFromRead = completeNode->asGeode()) != 0) {
309 myCreatedGroup =
new osg::Group();
310 osg::ref_ptr<osg::StateSet> stateset = myCreatedGroup->getOrCreateStateSet();
311 completeNode->setStateSet(stateset.get());
312 myCreatedGroup->addChild(completeNode.get());
315 osg::ComputeBoundsVisitor cbbv;
316 myCreatedGroup.get()->accept(cbbv);
317 osg::BoundingBox bb = cbbv.getBoundingBox();
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());
327 if (node->
map.find(
"loadSizeFromMesh") != node->
map.end()) {
328 if (node->
map[
"loadSizeFromMesh"]) {
331 node->
ext=
Vector(ex.x()*physicalScale.x(), ex.y()*physicalScale.y(), ex.z()*physicalScale.z());
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();
342 osg::ref_ptr<osg::PositionAttitudeTransform> transform;
343 osg::ref_ptr<osg::MatrixTransform> tx;
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());
352 transform->addChild(tx.get());
353 transform->setPivotPoint(osg::Vec3(node->
pivot.x()*scaleX,
354 node->
pivot.y()*scaleY,
355 node->
pivot.z()*scaleZ));
362 oquat.set(qrot.x(), qrot.y(), qrot.z(), qrot.w());
363 transform->setAttitude(oquat);
366 tempnode.
node = myCreatedGroup.get();
367 tempnode.
matrix = tx.get();
372 scaleX, scaleY, scaleZ,
379 std::vector<nodeFileStruct>::iterator iter;
381 for(iter = GuiHelper::nodeFiles.begin();
382 iter != GuiHelper::nodeFiles.end(); iter++) {
383 if((*iter).fileName == fileName)
return (*iter).node;
387 newNodeFile.
node = osgDB::readNodeFile(fileName);
388 GuiHelper::nodeFiles.push_back(newNodeFile);
389 return newNodeFile.
node;
395 std::vector<nodeFileStruct>::iterator iter;
397 for(iter = GuiHelper::nodeFiles.begin();
398 iter != GuiHelper::nodeFiles.end(); iter++) {
399 if((*iter).fileName == filename)
return (*iter).node;
404 FILE* input = fopen(filename.c_str(),
"rb");
409 int da, i,
r, o, foo=0;
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;
418 std::vector<osg::Vec3> vertices2;
419 std::vector<osg::Vec3> normals2;
420 std::vector<osg::Vec2> texcoords2;
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 ) {
429 while(o < r+foo-50 || (r<256 && o < r+foo)) {
430 da = *(
int*)(buffer+o);
434 fData[i] = *(
float*)(buffer+o);
437 vertices.push_back(osg::Vec3(fData[0], fData[1], fData[2]));
441 fData[i] = *(
float*)(buffer+o);
444 texcoords.push_back(osg::Vec2(fData[0], fData[1]));
448 fData[i] = *(
float*)(buffer+o);
451 normals.push_back(osg::Vec3(fData[0], fData[1], fData[2]));
456 iData[i] = *(
int*)(buffer+o);
459 if(iData[0] != iData[2]) {
463 osgIndices->push_back(iData[0]-1);
464 vertices2.push_back(vertices[iData[0]-1]);
466 texcoords2.push_back(texcoords[iData[1]-1]);
468 normals2.push_back(normals[iData[2]-1]);
472 iData[i] = *(
int*)(buffer+o);
475 if(iData[0] != iData[2]) {
478 osgIndices->push_back(iData[0]-1);
480 vertices2.push_back(vertices[iData[0]-1]);
482 texcoords2.push_back(texcoords[iData[1]-1]);
484 normals2.push_back(normals[iData[2]-1]);
488 iData[i] = *(
int*)(buffer+o);
491 if(iData[0] != iData[2]) {
494 osgIndices->push_back(iData[0]-1);
496 vertices2.push_back(vertices[iData[0]-1]);
498 texcoords2.push_back(texcoords[iData[1]-1]);
500 normals2.push_back(normals[iData[2]-1]);
504 if(r==256) memcpy(buffer, buffer+o, foo);
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]);
517 for(
size_t i=0; i<vertices2.size(); ++i) {
518 osgVertices->push_back(vertices2[i]);
520 for(
size_t i=0; i<normals2.size(); ++i) {
521 osgNormals->push_back(normals2[i]);
523 for(
size_t i=0; i<texcoords2.size(); ++i) {
524 osgTexcoords->push_back(texcoords2[i]);
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());
536 geometry->addPrimitiveSet(osgIndices);
539 osg::DrawArrays* drawArrays =
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,osgVertices->size());
540 geometry->addPrimitiveSet(drawArrays);
542 geode->addDrawable(geometry);
543 geode->setName(
"bobj");
546 osgUtil::Optimizer optimizer;
547 optimizer.optimize( geode );
549 newNodeFile.
node = geode;
550 GuiHelper::nodeFiles.push_back(newNodeFile);
551 return newNodeFile.
node;
559 #if !defined (WIN32) && !defined (__linux__) 562 img=cvLoadImage(terrain->
srcname.data(), -1);
564 terrain->
width = img->width;
565 terrain->
height = img->height;
566 fprintf(stderr,
"w h = %d %d\n", img->width, img->height);
574 double imageMaxValue = pow(2., img->depth);
577 for(
int y=terrain->
height-1; y>=0; y--) {
578 for(
int x=0; x<terrain->
width; x++) {
581 terrain->
pixelData[count++] = ((double)s.val[0])/(imageMaxValue-1);
590 cvReleaseImage(&img);
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());
604 osg::Image* image = osgDB::readImageFile(terrain->
srcname);
607 terrain->
width = image->s();
608 terrain->
height = image->t();
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));
638 std::vector<textureFileStruct>::iterator iter;
640 for (iter = textureFiles.begin();
641 iter != textureFiles.end(); iter++) {
642 if ((*iter).fileName == filename) {
643 return (*iter).texture;
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);
654 osg::Image* textureImage = loadImage(filename);
655 newTextureFile.
texture->setImage(textureImage);
656 textureFiles.push_back(newTextureFile);
662 std::vector<imageFileStruct>::iterator iter;
664 for (iter = imageFiles.begin();
665 iter != imageFiles.end(); iter++) {
666 if ((*iter).fileName == filename) {
667 return (*iter).image;
672 osg::Image* image = osgDB::readImageFile(filename);
673 newImageFile.
image = image;
674 imageFiles.push_back(newImageFile);
676 return newImageFile.
image;
utils::Vector pos
The position of the node.
std::string origName
The original object name; needed for importing objects from files (like wavefront object files)...
configmaps::ConfigMap map
If the data is created from a ConfigMap map the original map is stored here.
used to get a geode from a nodegroup
utils::Quaternion rot
This quaternion describes the orientation of the node.
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...
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 ...
NodeData is a struct to exchange node information between the GUI and the simulation.
bool vectorFromConfigItem(ConfigItem *item, Vector *v)
std::string filename
The filename of the node to load the visual representation from.
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...
static osg::ref_ptr< osg::Image > loadImage(std::string filename)
virtual std::vector< double > getMeshSize(const std::string &filename)
std::vector< osg::Vec3 > vertices
osg::ref_ptr< osg::Image > image
This visitor class is used to get triangles from a mesh.
osg::Matrix transformMatrix
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.
osg::ref_ptr< osg::PositionAttitudeTransform > transform
Copyright 2012, DFKI GmbH Robotics Innovation Center.
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector
Eigen::Quaternion< double, Eigen::DontAlign > Quaternion
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.
static osg::ref_ptr< osg::Texture2D > loadTexture(std::string filename)
osg::ref_ptr< osg::Texture2D > texture
utils::Vector ext
The size of the node.
static std::vector< textureFileStruct > textureFiles
mars::utils::Vector offset
static osg::ref_ptr< osg::Node > readNodeFromFile(std::string fileName)
internal struct for GraphicManager.h connecting the node indices with the osg nodes ...
vector< osg::Vec3 > & getVertices()