UnimanualAffordanceArmarX.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 Lesser General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * ArmarX is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * @package AffordanceKitArmarX
18  * @author Peter Kaiser ( peter dot kaiser at kit dot edu )
19  * @date 2016
20  * @copyright http://www.gnu.org/licenses/gpl.txt
21  * GNU General Public License
22  */
23 
25 #include <AffordanceKit/ColorMap.h>
26 #include <AffordanceKit/primitives/Plane.h>
27 
28 #include <Inventor/nodes/SoDrawStyle.h>
29 
30 namespace AffordanceKitArmarX
31 {
32  UnimanualAffordanceArmarX::UnimanualAffordanceArmarX(const AffordanceKit::UnimanualAffordancePtr& unimanualAffordance) :
33  visualizationNode(nullptr)
34  {
35  affordance = unimanualAffordance;
36  }
37 
39  {
41  {
42  visualizationNode->unref();
43  }
44  }
45 
47  {
49  {
50  visualizationNode->removeAllChildren();
51  }
52  }
53 
54  Eigen::Vector3f UnimanualAffordanceArmarX::computeSamplingPosition(const Eigen::Matrix4f& pose, float offset)
55  {
56  // Add a slight offset in negative z direction for visualization purposes
57  return pose.block<3, 1>(0, 3) - offset * pose.block<3, 1>(0, 2);
58  }
59 
61  {
62  float pos = (sampling1.block<3, 1>(0, 3) - sampling2.block<3, 1>(0, 3)).norm();
63 
64  float rot = sampling1.block<3, 1>(0, 2).normalized().dot(sampling2.block<3, 1>(0, 2).normalized());
65  CLAMP_TO_ACOS_DOMAIN(rot);
66 
67  return pos + 1000 * acos(rot);
68  }
69 
70  void UnimanualAffordanceArmarX::visualize(const armarx::DebugDrawerInterfacePrx& debugDrawer, const std::string& layerName, const std::string& id, float minExpectedProbability, const AffordanceKit::PrimitivePtr& primitive)
71  {
72  const float pointCloudVisualizationOffset = 2;
73 
74  if (!primitive)
75  {
76  return;
77  }
78 
79  for (auto& entry : *affordance->getTheta())
80  {
81  if (primitive->getId() != entry.first.first->getId())
82  {
83  continue;
84  }
85 
86  unsigned int size = primitive->getSamplingSize();
87  if (size == 0 || affordance->getThetaSize(entry.first) != size)
88  {
89  continue;
90  }
91 
92 #if 0
93  // DEBUG: Visualize plane bounding boxes
94  if (boost::dynamic_pointer_cast<AffordanceKit::Plane>(primitive))
95  {
96  AffordanceKit::PlanePtr p = boost::dynamic_pointer_cast<AffordanceKit::Plane>(primitive);
97 
98  Eigen::Vector3f min = p->getBoundingBoxMin();
99  Eigen::Vector3f max = p->getBoundingBoxMax();
100  Eigen::Matrix4f pose = p->getPose();
101 
102  armarx::PolygonPointList ppl;
103  ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(min(0), min(1), 0, 1)).head(3))));
104  ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(max(0), min(1), 0, 1)).head(3))));
105  ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(max(0), max(1), 0, 1)).head(3))));
106  ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(min(0), max(1), 0, 1)).head(3))));
107 
108  debugDrawer->setPolygonVisu(layerName, id + "_boundingBox", ppl, armarx::DrawColor {0, 0, 0, 0}, armarx::DrawColor {0, 0, 0, 1}, 3);
109  debugDrawer->setPoseVisu(layerName, id + "_pose", new armarx::Pose(p->getPose()));
110  }
111 #endif
112 
113  armarx::DebugDrawer24BitColoredPointCloud originalSamplingPointCloud;
114  originalSamplingPointCloud.pointSize = 7;
115  originalSamplingPointCloud.transparency = 0;
116  originalSamplingPointCloud.points.reserve(size);
117 
119  AffordanceKit::Belief maxBelief;
120  float maxExpectedProbability = -1;
121  int counter = 0;
122 
123  for (unsigned int i = 0; i < size; i++)
124  {
125  bool addCurrentPoint = false;
126  Eigen::Matrix4f P;
127  if (i < size - 1)
128  {
129  P = primitive->getSampling(i);
130  addCurrentPoint = (i > 0 && computeSamplingDistance(maxP, P) > 1e-3);
131  }
132  if (i == size - 1)
133  {
134  addCurrentPoint = true;
135  }
136 
137  if (addCurrentPoint)
138  {
139  if (maxExpectedProbability >= minExpectedProbability)
140  {
141  Eigen::Vector3f pos = computeSamplingPosition(maxP, pointCloudVisualizationOffset);
142 
143  armarx::DebugDrawer24BitColoredPointCloudElement e;
144  e.x = pos.x();
145  e.y = pos.y();
146  e.z = pos.z();
147 
148  Eigen::Vector3i c = AffordanceKit::ColorMap::GetVisualizationColor(maxBelief);
149  e.color = armarx::DrawColor24Bit {(unsigned char)c(0), (unsigned char)c(1), (unsigned char)c(2)};
150 
151  originalSamplingPointCloud.points.push_back(e);
152  }
153 
154  maxExpectedProbability = -1;
155  counter = 0;
156  }
157 
158  AffordanceKit::Belief b = affordance->getTheta(AffordanceKit::PrimitivePair(primitive), i);
159 
160  counter++;
161  float v = b.expectedProbability();
162  if (maxExpectedProbability < v)
163  {
164  maxP = P;
165  maxBelief = b;
166  maxExpectedProbability = v;
167  }
168  }
169 
170  debugDrawer->set24BitColoredPointCloudVisu(layerName, id + "_sampling", originalSamplingPointCloud);
171  }
172  }
173 }
AffordanceKitArmarX::UnimanualAffordanceArmarX::affordance
AffordanceKit::UnimanualAffordancePtr affordance
Definition: UnimanualAffordanceArmarX.h:52
AffordanceKitArmarX::UnimanualAffordanceArmarX::computeSamplingDistance
float computeSamplingDistance(const Eigen::Matrix4f &sampling1, const Eigen::Matrix4f &sampling2)
Definition: UnimanualAffordanceArmarX.cpp:60
AffordanceKitArmarX::UnimanualAffordanceArmarX::computeSamplingPosition
Eigen::Vector3f computeSamplingPosition(const Eigen::Matrix4f &pose, float offset)
Definition: UnimanualAffordanceArmarX.cpp:54
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
AffordanceKitArmarX::UnimanualAffordanceArmarX::~UnimanualAffordanceArmarX
~UnimanualAffordanceArmarX()
Definition: UnimanualAffordanceArmarX.cpp:38
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
AffordanceKitArmarX::UnimanualAffordanceArmarX::visualize
void visualize(const armarx::DebugDrawerInterfacePrx &debugDrawer, const std::string &layerName, const std::string &id, float minExpectedProbability, const AffordanceKit::PrimitivePtr &primitive)
Definition: UnimanualAffordanceArmarX.cpp:70
AffordanceKitArmarX::UnimanualAffordanceArmarX::visualizationNode
SoSeparator * visualizationNode
Definition: UnimanualAffordanceArmarX.h:57
armarx::Vector3
The Vector3 class.
Definition: Pose.h:112
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::Pose
The Pose class.
Definition: Pose.h:242
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
UnimanualAffordanceArmarX.h
AffordanceKitArmarX::UnimanualAffordanceArmarX::UnimanualAffordanceArmarX
UnimanualAffordanceArmarX(const AffordanceKit::UnimanualAffordancePtr &unimanualAffordance)
Definition: UnimanualAffordanceArmarX.cpp:32
min
T min(T t1, T t2)
Definition: gdiam.h:42
AffordanceKitArmarX
Definition: BimanualAffordanceArmarX.cpp:30
AffordanceKitArmarX::UnimanualAffordanceArmarX::reset
void reset()
Definition: UnimanualAffordanceArmarX.cpp:46
norm
double norm(const Point &a)
Definition: point.hpp:94