ManipulatorVisualization.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package <PACKAGE_NAME>::<CATEGORY>::ManipulatorVisualization
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 //Virtual Robot includes
27 #include <VirtualRobot/XML/RobotIO.h>
28 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
29 
30 /* Coin includes */
31 #include <Inventor/actions/SoGetMatrixAction.h>
32 #include <Inventor/actions/SoSearchAction.h>
33 #include <Inventor/SbViewportRegion.h>
34 #include <Inventor/nodes/SoCube.h>
35 
36 
37 #include <iostream>
38 
39 using namespace armarx;
40 
41 ManipulatorVisualization::ManipulatorVisualization() : isVisualizing(false), hasEndEffectorVisualizer(false), localTransformation(Eigen::Matrix4f::Identity())
42 {
43  this->ref();
44 }
45 
47 {
48  this->removeVisualization();
49  this->unref();
50 }
51 
52 void ManipulatorVisualization::setVisualization(VirtualRobot::EndEffectorPtr endEffector)
53 {
54  //Completely forget anything we displayed earlier
55  //This also works when this is the first time we display something
56  this->removeVisualization();
57 
58  this->hasEndEffectorVisualizer = endEffector ? true : false;
59 
60  //Display all indicators in the same color and with transparency
61  this->material = new SoMaterial();
62  this->material->transparency = 0.5;
63  this->material->setOverride(true);
64  this->addChild(material);
65 
66  //if no end effector is defined for the chain, just add a placeholder
67  if (this->hasEndEffectorVisualizer)
68  {
69  VirtualRobot::RobotPtr endEffectorRobot = endEffector->createEefRobot("", "");
70  VirtualRobot::CoinVisualizationPtr endEffectorVisualization = endEffectorRobot->getVisualization<VirtualRobot::CoinVisualization>();
71  this->addChild(endEffectorVisualization->getCoinVisualization());
72  }
73  else
74  {
75  SoCube* cube = new SoCube();
76  cube->width = 0.1;
77  cube->height = 0.1;
78  cube->depth = 0.1;
79 
80  this->addChild(cube);
81  }
82 
83  this->manip.reset(new SoTransformerManip());
84 
85  //We need this null separator to remove the scale knobs
86  //This won't lead to memory leak, because this separator
87  //will be deleted when the manipulator itself gets removed
88  //from the scene graph
89  SoSeparator* nullSep = new SoSeparator;
90 
91  //Make all scale knobs disappear
92  manip->getDragger()->setPart("scale1", nullSep);
93  manip->getDragger()->setPart("scale2", nullSep);
94  manip->getDragger()->setPart("scale3", nullSep);
95  manip->getDragger()->setPart("scale4", nullSep);
96  manip->getDragger()->setPart("scale5", nullSep);
97  manip->getDragger()->setPart("scale6", nullSep);
98  manip->getDragger()->setPart("scale7", nullSep);
99  manip->getDragger()->setPart("scale8", nullSep);
100 
101  // Make all translation knobs disappear
102  manip->getDragger()->setPart("translator1", nullSep);
103  manip->getDragger()->setPart("translator2", nullSep);
104  manip->getDragger()->setPart("translator3", nullSep);
105  manip->getDragger()->setPart("translator4", nullSep);
106  manip->getDragger()->setPart("translator5", nullSep);
107  manip->getDragger()->setPart("translator6", nullSep);
108 
109  // Make all rotation knobs disappear
110  manip->getDragger()->setPart("rotator1", nullSep);
111  manip->getDragger()->setPart("rotator2", nullSep);
112  manip->getDragger()->setPart("rotator3", nullSep);
113  manip->getDragger()->setPart("rotator4", nullSep);
114  manip->getDragger()->setPart("rotator5", nullSep);
115  manip->getDragger()->setPart("rotator6", nullSep);
116 
117  //Stores the global position and pose of tcp
118  Eigen::Matrix4f globalMat = endEffector->getTcp()->getGlobalPose();
119  if (this->hasEndEffectorVisualizer)
120  {
121  //Stores the local transformation from the endeffector base to the TCP node
122  this->localTransformation = endEffector->getBase()->getTransformationTo(endEffector->getTcp());
123 
124  globalMat = endEffector->getBase()->getGlobalPose();
125  }
126 
127  //Now go ahead and set new position
128  //Factor 1000 to match coin unit system
129  globalMat(0, 3) /= 1000;
130  globalMat(1, 3) /= 1000;
131  globalMat(2, 3) /= 1000;
132  manip->setMatrix(VirtualRobot::CoinVisualizationFactory::getSbMatrix(globalMat));
133  this->insertChild(manip.get(), 0);
134 
135  this->isVisualizing = true;
136 }
137 
139 {
140  //Remove all children and reset manip pointer
141  //This should bring ref counter of Inventor down to zero and free memory
142  this->removeAllChildren();
143  manip.reset();
144 
145  this->isVisualizing = false;
146  this->hasEndEffectorVisualizer = false;
147  this->localTransformation = Eigen::Matrix4f::Identity();
148 }
149 
150 void ManipulatorVisualization::setColor(float r, float g, float b)
151 {
152  if (this->getIsVisualizing())
153  {
154  this->material->ambientColor.setValue(r, g, b);
155  }
156 }
157 
159 {
160  if (this->getIsVisualizing())
161  {
162  SoGetMatrixAction* action = new SoGetMatrixAction(SbViewportRegion());
163  SoSearchAction sa;
164  sa.setNode(manip.get());
165  sa.setSearchingAll(TRUE); // Search all nodes
166  SoBaseKit::setSearchingChildren(TRUE); // Even inside nodekits
167  sa.apply(this);
168 
169  action->apply(sa.getPath());
170 
171  SbMatrix matrix = action->getMatrix();
172 
174  mat(0, 0) = matrix[0][0];
175  mat(0, 1) = matrix[1][0];
176  mat(0, 2) = matrix[2][0];
177  mat(0, 3) = matrix[3][0] * 1000;
178 
179  mat(1, 0) = matrix[0][1];
180  mat(1, 1) = matrix[1][1];
181  mat(1, 2) = matrix[2][1];
182  mat(1, 3) = matrix[3][1] * 1000;
183 
184  mat(2, 0) = matrix[0][2];
185  mat(2, 1) = matrix[1][2];
186  mat(2, 2) = matrix[2][2];
187  mat(2, 3) = matrix[3][2] * 1000;
188 
189  mat(3, 0) = matrix[0][3];
190  mat(3, 1) = matrix[1][3];
191  mat(3, 2) = matrix[2][3];
192  mat(3, 3) = matrix[3][3];
193 
194  //We need to take local transformation into account, because otherwise the visualizer
195  //shows a wrong position for tcp (offset equals local transformation of tcp in node set then)
196  //Just apply local transformation to fix this
197  if (this->hasEndEffectorVisualizer)
198  {
199  mat = mat * this->localTransformation;
200  }
201 
202  return mat;
203  }
204 
205  return Eigen::Matrix4f::Identity();
206 }
207 
209 {
210  Eigen::Matrix4f mat = this->getUserDesiredPose();
211 
212  //Convert to string
213  std::stringstream buffer;
214  buffer << mat;
215  return buffer.str();
216 }
217 
219 {
220  if (manip)
221  {
222  if (this->hasEndEffectorVisualizer)
223  {
224  globalPose = globalPose * this->localTransformation.inverse();
225  }
226  globalPose(0, 3) /= 1000;
227  globalPose(1, 3) /= 1000;
228  globalPose(2, 3) /= 1000;
229  manip->setMatrix(VirtualRobot::CoinVisualizationFactory::getSbMatrix(globalPose));
230  }
231 }
RemoteRobot.h
armarx::ManipulatorVisualization::setColor
void setColor(float r, float g, float b)
Definition: ManipulatorVisualization.cpp:150
Eigen
Definition: Elements.h:36
armarx::ManipulatorVisualization::getUserDesiredPoseString
std::string getUserDesiredPoseString()
Definition: ManipulatorVisualization.cpp:208
ManipulatorVisualization.h
armarx::ManipulatorVisualization::setUserDesiredPose
void setUserDesiredPose(Eigen::Matrix4f globalPose)
Definition: ManipulatorVisualization.cpp:218
armarx::ManipulatorVisualization::~ManipulatorVisualization
~ManipulatorVisualization() override
Definition: ManipulatorVisualization.cpp:46
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::ManipulatorVisualization::getIsVisualizing
bool getIsVisualizing() const
Definition: ManipulatorVisualization.h:66
CoinVisualizationPtr
boost::shared_ptr< VirtualRobot::CoinVisualization > CoinVisualizationPtr
Definition: ManipulatorVisualization.h:52
armarx::ManipulatorVisualization::ManipulatorVisualization
ManipulatorVisualization()
Definition: ManipulatorVisualization.cpp:41
armarx::ManipulatorVisualization::getUserDesiredPose
Eigen::Matrix4f getUserDesiredPose()
Definition: ManipulatorVisualization.cpp:158
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::ManipulatorVisualization::removeVisualization
void removeVisualization()
Definition: ManipulatorVisualization.cpp:138
armarx::ManipulatorVisualization::setVisualization
void setVisualization(VirtualRobot::EndEffectorPtr endEffector)
Definition: ManipulatorVisualization.cpp:52
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18