PrimitiveSetArmarX.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
24#include "PrimitiveSetArmarX.h"
25
28
31
32#include <AffordanceKit/primitives/Box.h>
33#include <AffordanceKit/primitives/Cylinder.h>
34#include <AffordanceKit/primitives/Plane.h>
35#include <AffordanceKit/primitives/Sphere.h>
38
39namespace AffordanceKitArmarX
40{
44
45 PrimitiveSetArmarX::PrimitiveSetArmarX(const AffordanceKit::PrimitiveSetPtr& primitiveSet)
46 {
47 for (auto& primitive : *primitiveSet)
48 {
49 push_back(primitive);
50 }
51 }
52
54 const memoryx::EnvironmentalPrimitiveSegmentBasePrx& segment,
55 long timestamp)
56 {
57 memoryx::PlanePrimitiveBaseList planes;
58 memoryx::CylinderPrimitiveBaseList cylinders;
59 memoryx::SpherePrimitiveBaseList spheres;
60 memoryx::BoxPrimitiveBaseList boxes;
61
62
63 if (timestamp == 0)
64 {
65 planes = segment->getMostRecentPlanes();
66 cylinders = segment->getMostRecentCylinders();
67 spheres = segment->getMostRecentSpheres();
68 boxes = segment->getMostRecentBoxes();
69 }
70 else
71 {
72 planes = segment->getPlanesByTimestamp(new armarx::TimestampVariant(timestamp));
73 cylinders = segment->getCylindersByTimestamp(new armarx::TimestampVariant(timestamp));
74 spheres = segment->getSpheresByTimestamp(new armarx::TimestampVariant(timestamp));
75 boxes = segment->getBoxesByTimestamp(new armarx::TimestampVariant(timestamp));
76 }
77
78 // Planar primitives
79 for (auto& primitive : planes)
80 {
81 memoryx::PointList originalPoints = primitive->getGraspPoints();
82
83 std::vector<Eigen::Vector3f> convexHull(originalPoints.size());
84 for (unsigned int i = 0; i < originalPoints.size(); i++)
85 {
86 convexHull[i] = armarx::Vector3Ptr::dynamicCast(originalPoints[i])->toEigen();
87 }
88
89 AffordanceKit::PrimitivePtr plane(new AffordanceKit::Plane(convexHull));
90 plane->setId(primitive->getId());
91 plane->setSampling(
92 armarx::MatrixFloatPtr::dynamicCast(primitive->getSampling())->toEigen());
93 plane->setTimestamp(primitive->getTime()->timestamp);
94 plane->setLabel(primitive->getLabel());
95 push_back(plane);
96 }
97
98 // Cylindrical primitives
99 for (auto& primitive : cylinders)
100 {
101 Eigen::Vector3f basePoint =
102 armarx::Vector3Ptr::dynamicCast(primitive->getCylinderPoint())->toEigen();
103 Eigen::Vector3f direction =
104 armarx::Vector3Ptr::dynamicCast(primitive->getCylinderAxisDirection())->toEigen();
105
106 AffordanceKit::PrimitivePtr cylinder(new AffordanceKit::Cylinder(
107 basePoint, direction, primitive->getLength(), primitive->getCylinderRadius()));
108 cylinder->setId(primitive->getId());
109 cylinder->setSampling(
110 armarx::MatrixFloatPtr::dynamicCast(primitive->getSampling())->toEigen());
111 cylinder->setTimestamp(primitive->getTime()->timestamp);
112 cylinder->setLabel(primitive->getLabel());
113 push_back(cylinder);
114 }
115
116 // Spherical primitives
117 for (auto& primitive : spheres)
118 {
119 Eigen::Vector3f center =
120 armarx::Vector3Ptr::dynamicCast(primitive->getSphereCenter())->toEigen();
121
122 AffordanceKit::PrimitivePtr sphere(
123 new AffordanceKit::Sphere(center, primitive->getSphereRadius()));
124 sphere->setId(primitive->getId());
125 sphere->setTimestamp(primitive->getTime()->timestamp);
126 sphere->setSampling(
127 armarx::MatrixFloatPtr::dynamicCast(primitive->getSampling())->toEigen());
128 push_back(sphere);
129 }
130
131 // Box primitives
132 for (auto& primitive : boxes)
133 {
134 Eigen::Matrix4f pose = armarx::PosePtr::dynamicCast(primitive->getPose())->toEigen();
135 Eigen::Vector3f dimensions =
136 armarx::Vector3Ptr::dynamicCast(primitive->getOBBDimensions())->toEigen();
137
138 AffordanceKit::PrimitivePtr box(new AffordanceKit::Box(pose, dimensions));
139 box->setId(primitive->getId());
140 box->setTimestamp(primitive->getTime()->timestamp);
141 box->setSampling(
142 armarx::MatrixFloatPtr::dynamicCast(primitive->getSampling())->toEigen());
143 push_back(box);
144 }
145 }
146
147 void
149 const memoryx::EnvironmentalPrimitiveSegmentBasePrx& segment) const
150 {
151 if (size() == 0)
152 {
153 // No primitives to export
154 return;
155 }
156
157 std::vector<memoryx::EntityBasePtr> newPrimitives;
158 for (auto& primitive : *this)
159 {
160 memoryx::EnvironmentalPrimitiveBasePtr p;
161
162 if (AffordanceKit::Plane* pl = dynamic_cast<AffordanceKit::Plane*>(primitive.get()))
163 {
164 const std::vector<Eigen::Vector3f>& ch = pl->getConvexHull();
165
166 memoryx::PointList convexHull;
167 convexHull.reserve(ch.size());
168 for (auto& p : ch)
169 {
170 convexHull.push_back(new armarx::Vector3(p));
171 }
172
173 p = new memoryx::PlanePrimitive();
174 p->setGraspPoints(convexHull);
175 }
176 else if (AffordanceKit::Cylinder* c =
177 dynamic_cast<AffordanceKit::Cylinder*>(primitive.get()))
178 {
180 p2->setCylinderRadius(c->getRadius());
181 p2->setCylinderPoint(new armarx::Vector3(c->getBasePoint()));
182 p2->setCylinderAxisDirection(new armarx::Vector3(c->getDirection()));
183 p2->setLength(c->getLength());
184
185 p = p2;
186 }
187 else if (AffordanceKit::Sphere* s =
188 dynamic_cast<AffordanceKit::Sphere*>(primitive.get()))
189 {
191 p2->setSphereCenter(new armarx::Vector3(s->getCenter()));
192 p2->setSphereRadius(s->getRadius());
193
194 p = p2;
195 }
196 else if (AffordanceKit::Box* b = dynamic_cast<AffordanceKit::Box*>(primitive.get()))
197 {
199 p2->setPose(new armarx::FramedPose(b->getPose(), "", ""));
200 p2->setOBBDimensions(new armarx::Vector3(b->getDimensions()));
201
202 p = p2;
203 }
204
205 p->setName("environmentalPrimitive_" + primitive->getId());
206 p->setId(primitive->getId());
207
208 ARMARX_INFO << "Writing primitive to memory: ID=" << p->getId()
209 << ", name=" << p->getName();
210 p->setCircularityProbability(primitive->isCircular());
211 p->setTime(new armarx::TimestampVariant(primitive->getTimestamp()));
212 p->setLabel(primitive->getLabel());
213
214 armarx::MatrixFloatBasePtr s(new armarx::MatrixFloat(primitive->getSampling()));
215 p->setSampling(s);
216
217
218 newPrimitives.push_back(p);
219 }
220
221 segment->upsertEntityList(newPrimitives);
222 }
223} // namespace AffordanceKitArmarX
std::string timestamp()
constexpr T c
void writeToMemory(const memoryx::EnvironmentalPrimitiveSegmentBasePrx &segment) const
The FramedPose class.
Definition FramedPose.h:281
The MatrixFloat class.
Implements a Variant type for timestamps.
The Vector3 class.
Definition Pose.h:113
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
IceInternal::Handle< SpherePrimitive > SpherePrimitivePtr
IceInternal::Handle< BoxPrimitive > BoxPrimitivePtr
IceInternal::Handle< CylinderPrimitive > CylinderPrimitivePtr