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
26#include <AffordanceKit/ColorMap.h>
27#include <AffordanceKit/primitives/Plane.h>
28#include <Inventor/nodes/SoDrawStyle.h>
29
30namespace AffordanceKitArmarX
31{
33 const AffordanceKit::UnimanualAffordancePtr& unimanualAffordance) :
34 visualizationNode(nullptr)
35 {
36 affordance = unimanualAffordance;
37 }
38
46
47 void
49 {
51 {
52 visualizationNode->removeAllChildren();
53 }
54 }
55
56 Eigen::Vector3f
57 UnimanualAffordanceArmarX::computeSamplingPosition(const Eigen::Matrix4f& pose, float offset)
58 {
59 // Add a slight offset in negative z direction for visualization purposes
60 return pose.block<3, 1>(0, 3) - offset * pose.block<3, 1>(0, 2);
61 }
62
63 float
65 const Eigen::Matrix4f& sampling2)
66 {
67 float pos = (sampling1.block<3, 1>(0, 3) - sampling2.block<3, 1>(0, 3)).norm();
68
69 float rot =
70 sampling1.block<3, 1>(0, 2).normalized().dot(sampling2.block<3, 1>(0, 2).normalized());
71 CLAMP_TO_ACOS_DOMAIN(rot);
72
73 return pos + 1000 * acos(rot);
74 }
75
76 void
78 const std::string& layerName,
79 const std::string& id,
80 float minExpectedProbability,
81 const AffordanceKit::PrimitivePtr& primitive)
82 {
83 const float pointCloudVisualizationOffset = 2;
84
85 if (!primitive)
86 {
87 return;
88 }
89
90 for (auto& entry : *affordance->getTheta())
91 {
92 if (primitive->getId() != entry.first.first->getId())
93 {
94 continue;
95 }
96
97 unsigned int size = primitive->getSamplingSize();
98 if (size == 0 || affordance->getThetaSize(entry.first) != size)
99 {
100 continue;
101 }
102
103#if 0
104 // DEBUG: Visualize plane bounding boxes
105 if (boost::dynamic_pointer_cast<AffordanceKit::Plane>(primitive))
106 {
107 AffordanceKit::PlanePtr p = boost::dynamic_pointer_cast<AffordanceKit::Plane>(primitive);
108
109 Eigen::Vector3f min = p->getBoundingBoxMin();
110 Eigen::Vector3f max = p->getBoundingBoxMax();
111 Eigen::Matrix4f pose = p->getPose();
112
113 armarx::PolygonPointList ppl;
114 ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(min(0), min(1), 0, 1)).head(3))));
115 ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(max(0), min(1), 0, 1)).head(3))));
116 ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(max(0), max(1), 0, 1)).head(3))));
117 ppl.push_back(new armarx::Vector3(Eigen::Vector3f((pose * Eigen::Vector4f(min(0), max(1), 0, 1)).head(3))));
118
119 debugDrawer->setPolygonVisu(layerName, id + "_boundingBox", ppl, armarx::DrawColor {0, 0, 0, 0}, armarx::DrawColor {0, 0, 0, 1}, 3);
120 debugDrawer->setPoseVisu(layerName, id + "_pose", new armarx::Pose(p->getPose()));
121 }
122#endif
123
124 armarx::DebugDrawer24BitColoredPointCloud originalSamplingPointCloud;
125 originalSamplingPointCloud.pointSize = 7;
126 originalSamplingPointCloud.transparency = 0;
127 originalSamplingPointCloud.points.reserve(size);
128
129 Eigen::Matrix4f maxP = Eigen::Matrix4f::Identity();
130 AffordanceKit::Belief maxBelief;
131 float maxExpectedProbability = -1;
132 int counter = 0;
133
134 for (unsigned int i = 0; i < size; i++)
135 {
136 bool addCurrentPoint = false;
137 Eigen::Matrix4f P;
138 if (i < size - 1)
139 {
140 P = primitive->getSampling(i);
141 addCurrentPoint = (i > 0 && computeSamplingDistance(maxP, P) > 1e-3);
142 }
143 if (i == size - 1)
144 {
145 addCurrentPoint = true;
146 }
147
148 if (addCurrentPoint)
149 {
150 if (maxExpectedProbability >= minExpectedProbability)
151 {
152 Eigen::Vector3f pos =
153 computeSamplingPosition(maxP, pointCloudVisualizationOffset);
154
155 armarx::DebugDrawer24BitColoredPointCloudElement e;
156 e.x = pos.x();
157 e.y = pos.y();
158 e.z = pos.z();
159
160 Eigen::Vector3i c =
161 AffordanceKit::ColorMap::GetVisualizationColor(maxBelief);
162 e.color = armarx::DrawColor24Bit{
163 (unsigned char)c(0), (unsigned char)c(1), (unsigned char)c(2)};
164
165 originalSamplingPointCloud.points.push_back(e);
166 }
167
168 maxExpectedProbability = -1;
169 counter = 0;
170 }
171
172 AffordanceKit::Belief b =
173 affordance->getTheta(AffordanceKit::PrimitivePair(primitive), i);
174
175 counter++;
176 float v = b.expectedProbability();
177 if (maxExpectedProbability < v)
178 {
179 maxP = P;
180 maxBelief = b;
181 maxExpectedProbability = v;
182 }
183 }
184
185 debugDrawer->set24BitColoredPointCloudVisu(
186 layerName, id + "_sampling", originalSamplingPointCloud);
187 }
188 }
189} // namespace AffordanceKitArmarX
constexpr T c
void visualize(const armarx::DebugDrawerInterfacePrx &debugDrawer, const std::string &layerName, const std::string &id, float minExpectedProbability, const AffordanceKit::PrimitivePtr &primitive)
UnimanualAffordanceArmarX(const AffordanceKit::UnimanualAffordancePtr &unimanualAffordance)
Eigen::Vector3f computeSamplingPosition(const Eigen::Matrix4f &pose, float offset)
AffordanceKit::UnimanualAffordancePtr affordance
float computeSamplingDistance(const Eigen::Matrix4f &sampling1, const Eigen::Matrix4f &sampling2)
The Pose class.
Definition Pose.h:243
The Vector3 class.
Definition Pose.h:113
T min(T t1, T t2)
Definition gdiam.h:44
T max(T t1, T t2)
Definition gdiam.h:51
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
double norm(const Point &a)
Definition point.hpp:102