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