00001 /* 00002 * Copyright (c) 2013 University of Jaume-I. 00003 * All rights reserved. This program and the accompanying materials 00004 * are made available under the terms of the GNU Public License v3.0 00005 * which accompanies this distribution, and is available at 00006 * http://www.gnu.org/licenses/gpl.html 00007 * 00008 * Contributors: 00009 * Mario Prats 00010 * Javier Perez 00011 */ 00012 00013 #include <uwsim/ObjectPicker.h> 00014 #include <uwsim/UWSimUtils.h> 00015 00016 ObjectPicker::ObjectPicker() : 00017 VirtualRangeSensor() 00018 { 00019 } 00020 00021 ObjectPicker::ObjectPicker(std::string name, osg::Node *root, osg::Node *trackNode, double range, bool visible, 00022 boost::shared_ptr<URDFRobot> urdf) 00023 { 00024 init(name, root, trackNode, range, visible, urdf); 00025 } 00026 00027 void ObjectPicker::init(std::string name, osg::Node *root, osg::Node *trackNode, double range, bool visible, 00028 boost::shared_ptr<URDFRobot> urdf) 00029 { 00030 this->name = name; 00031 this->root = root; 00032 00033 this->trackNode = trackNode; 00034 //Add a switchable frame geometry on the sensor frame 00035 osg::ref_ptr < osg::Node > axis = UWSimGeometry::createSwitchableFrame(); 00036 this->trackNode->asGroup()->addChild(axis); 00037 00038 this->range = range; 00039 this->visible = visible; 00040 00041 //make this virtual ray track the node 00042 node_tracker = new ObjectPickerUpdateCallback(trackNode, range, visible, root, urdf); 00043 trackNode->setUpdateCallback((ObjectPickerUpdateCallback*)(node_tracker.get())); 00044 trackNode->asGroup()->addChild(node_tracker->geode); 00045 } 00046 00047 /* 00048 bool Hand::closeHand(){ 00049 00050 for(osg::NodePath::iterator i=node_tracker->impact.begin();i!=node_tracker->impact.end();++i){ 00051 osg::ref_ptr<NodeDataType> data = dynamic_cast<NodeDataType*> (i[0]->getUserData()); 00052 if(data!=NULL && data->catchable){ //Search for "catchable" objects in nodepath 00053 //Get coordinates to change them when changing position in graph 00054 osg::Matrixd *originalpos=getWorldCoords(i[0]); 00055 osg::Matrixd *hand = getWorldCoords(trackNode); 00056 hand->invert(*hand); 00057 00058 //Turn object to Kinematic state, add callback to move collision shape 00059 if(data->rb) 00060 data->rb->setCollisionFlags( data->rb->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT ); 00061 catched_callback=new BulletPhysics::MirrorTransformCallback(data->rb); 00062 i[0]->setUpdateCallback(catched_callback); 00063 00064 //ADD node in hand, remove object from original position. 00065 trackNode->asTransform()->addChild(i[0]); 00066 i[0]->getParent(0)->asGroup()->removeChild(i[0]); 00067 osg::Matrixd matrix=*originalpos * *hand; 00068 i[0]->asTransform()->asMatrixTransform()->setMatrix(matrix); 00069 freeHand=0; 00070 catched=i[0]; 00071 return 1; 00072 } 00073 00074 } 00075 return 0; 00076 } 00077 00078 00079 void Hand::openHand(){ 00080 osg::ref_ptr<NodeDataType> data = dynamic_cast<NodeDataType*> (catched->getUserData()); 00081 00082 osg::Matrixd *originalpos=getWorldCoords(catched); 00083 osg::Matrixd *world = getWorldCoords(root); 00084 world->invert(*world); 00085 00086 root->asGroup()->addChild(catched); 00087 trackNode->asGroup()->removeChild(catched); 00088 osg::Matrixd matrix=*originalpos * *world; 00089 catched->asTransform()->asMatrixTransform()->setMatrix(matrix); 00090 00091 //Get collision shape to current position, destroy callback and turn object to Dynamic state again. 00092 catched->removeUpdateCallback(catched_callback); 00093 catched_callback=NULL; 00094 data->rb->setWorldTransform( osgbCollision::asBtTransform(matrix) ); 00095 if(data->rb) 00096 data->rb->setCollisionFlags( data->rb->getCollisionFlags() & ~btCollisionObject::CF_KINEMATIC_OBJECT ); 00097 freeHand=1; 00098 catched=NULL; 00099 00100 } 00101 */ 00102