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