GraphNode.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 MemoryX::Core
17* @author Raphael Grimm <raphael dot grimm at kit dot edu>
18* @date 2014
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22#include "GraphNode.h"
23
24#include <Eigen/Geometry>
25
28#include <ArmarXCore/interface/core/UserException.h>
29
31
32//init static class members
33const std::string memoryx::GraphNode::GRAPH_NODE_ATTR_SCENE{"scene"};
34
35//constants
36#define GRAPH_NODE_ATTR_SCENE memoryx::GraphNode::GRAPH_NODE_ATTR_SCENE
37#define GRAPH_NODE_ATTR_POSE "pose"
38#define GRAPH_NODE_ATTR_ADJ_NODES "adjacentNodes"
39
40memoryx::GraphNode::GraphNode()
41{
42}
43
44memoryx::GraphNode::GraphNode(float x,
45 float y,
46 float alpha,
47 const std::string& nodeName,
48 const std::string& scene)
49{
50 Eigen::AngleAxisf aa(alpha, Eigen::Vector3f(0, 0, 1));
51 Eigen::Matrix3f rot = aa.toRotationMatrix();
52 setName(nodeName);
54 ::armarx::FramedPoseBasePtr{new ::armarx::FramedPose(
55 rot, Eigen::Vector3f(x, y, 0), armarx::GlobalFrame, "")});
57 clearAdjacentNodes(); //init adjacent node attr
58}
59
60memoryx::GraphNode::GraphNode(armarx::FramedPoseBasePtr pose,
61 const std::string& nodeName,
62 const std::string& scene)
63{
64 setName(nodeName);
65
66 if (pose)
67 {
68 putAttribute(GRAPH_NODE_ATTR_POSE, pose);
69 }
70
71 putAttribute(GRAPH_NODE_ATTR_SCENE, scene);
72 clearAdjacentNodes(); //init adjacent node attr
73}
74
78
79armarx::FramedPoseBasePtr
80memoryx::GraphNode::getPose(const Ice::Current&) const
81{
82 return getAttributeValue(GRAPH_NODE_ATTR_POSE)->get<armarx::FramedPoseBase>();
83}
84
85void
86memoryx::GraphNode::setPose(const armarx::FramedPoseBasePtr& pose, const Ice::Current&)
87{
89}
90
91std::string
92memoryx::GraphNode::getScene(const Ice::Current&) const
93{
94 return getAttributeValue(GRAPH_NODE_ATTR_SCENE)->get<std::string>();
95}
96
97void
98memoryx::GraphNode::setScene(const std::string& scene, const Ice::Current&)
99{
101}
102
103int
104memoryx::GraphNode::getOutdegree(const Ice::Current&) const
105{
107}
108
109memoryx::EntityRefBasePtr
110memoryx::GraphNode::getAdjacentNode(Ice::Int i, const Ice::Current&)
111{
112 auto deg = getOutdegree();
113
114 if (i < 0 || i >= deg)
115 {
116 std::stringstream s;
117 s << "Graph node: Access to adjacent node with index " << i << "! (outdegree is " << deg
118 << ")";
119 throw armarx::IndexOutOfBoundsException{s.str()};
120 }
121
122 return armarx::VariantPtr::dynamicCast(getAttribute(GRAPH_NODE_ATTR_ADJ_NODES)->getValueAt(i))
123 ->getClass<memoryx::EntityRefBase>();
124}
125
126memoryx::EntityRefBasePtr
127memoryx::GraphNode::getAdjacentNodeById(const std::string& nodeId, const Ice::Current& c)
128{
129 int nodes = getOutdegree();
130 for (int i = 0; i < nodes; ++i)
131 {
132 auto node = getAdjacentNode(i);
133 if (node->entityId == nodeId)
134 {
135 return node;
136 }
137 }
138 return nullptr;
139}
140
141memoryx::GraphNodeBaseList
143{
144 memoryx::GraphNodeBaseList result;
145 int nodes = getOutdegree();
146 for (int i = 0; i < nodes; ++i)
147 {
148 auto nodeEntity = getAdjacentNode(i);
149 auto graphNode = GraphNodeBasePtr::dynamicCast(nodeEntity->getEntity());
150 ARMARX_CHECK_EXPRESSION(graphNode);
151 result.push_back(graphNode);
152 }
153 return result;
154}
155
156void
157memoryx::GraphNode::addAdjacentNode(const memoryx::EntityRefBasePtr& newAdjacentNode,
158 const Ice::Current& c)
159{
160 ARMARX_VERBOSE_S << "node " << getName() << " (" << getId() << ") in scene " << getScene()
161 << ": adding new adjacent node...";
162
163 if (!memoryx::GraphNodeBasePtr::dynamicCast(newAdjacentNode->getEntity(c)))
164 {
165 std::stringstream s{};
166 s << "Expected " << memoryx::GraphNodeBase::ice_id(c);
167 throw armarx::InvalidTypeException{s.str()};
168 }
169
171 ->addValue(armarx::VariantBasePtr{new armarx::Variant{newAdjacentNode}});
172 ARMARX_VERBOSE_S << "adding new adjacent node: done! (new outdegree: " << getOutdegree() << ")";
173}
174
175void
177{
179 memoryx::EntityAttributeBasePtr{new memoryx::EntityAttribute{GRAPH_NODE_ATTR_ADJ_NODES}});
180}
181
182bool
183memoryx::GraphNode::removeAdjacentNode(const std::string& nodeId, const Ice::Current&)
184{
185 int nodes = getOutdegree();
186 for (int i = 0; i < nodes; ++i)
187 {
188 auto node = getAdjacentNode(i);
189 if (node->entityId == nodeId)
190 {
191 getAttribute(GRAPH_NODE_ATTR_ADJ_NODES)->removeValueAt(i);
192 return true;
193 }
194 }
195 return false;
196}
197
198float
200{
201 Eigen::Matrix3f mat = armarx::QuaternionPtr::dynamicCast(getPose()->orientation)->toEigen();
202 Eigen::Vector3f rpy = mat.eulerAngles(0, 1, 2);
203 return rpy[2];
204}
205
206Ice::ObjectPtr
208{
209 return this->clone();
210}
211
213memoryx::GraphNode::clone(const Ice::Current&) const
214{
215 GraphNodePtr ret = new GraphNode(*this);
216 // ret->deepCopy(*this);
217 return ret;
218}
#define GRAPH_NODE_ATTR_POSE
Definition GraphNode.cpp:37
#define GRAPH_NODE_ATTR_SCENE
Definition GraphNode.cpp:36
#define GRAPH_NODE_ATTR_ADJ_NODES
Definition GraphNode.cpp:38
constexpr T c
The Variant class is described here: Variants.
Definition Variant.h:224
Attribute of MemoryX entities.
EntityAttributeBasePtr getAttribute(const ::std::string &attrName, const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve attribute from entity.
Definition Entity.cpp:311
void setName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Set name of this entity.
Definition Entity.cpp:188
virtual armarx::VariantPtr getAttributeValue(const ::std::string &attrName) const
Retrieve value of an attribute from entity.
Definition Entity.cpp:327
::std::string getId(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve id of this entity which is an integer in string representation.
Definition Entity.cpp:167
void putAttribute(const ::memoryx::EntityAttributeBasePtr &attr, const ::Ice::Current &=Ice::emptyCurrent) override
Store attribute in entity.
Definition Entity.cpp:347
::std::string getName(const ::Ice::Current &=Ice::emptyCurrent) const override
Retrieve name of this entity.
Definition Entity.cpp:181
static const std::string GRAPH_NODE_ATTR_SCENE
Definition GraphNode.h:33
memoryx::GraphNodeBaseList getAdjacentNodes(const Ice::Current &c=Ice::emptyCurrent) override
void setScene(const std::string &scene, const ::Ice::Current &=Ice::emptyCurrent) override
Definition GraphNode.cpp:98
::memoryx::EntityRefBasePtr getAdjacentNodeById(const std::string &nodeId, const Ice::Current &c=Ice::emptyCurrent) override
void clearAdjacentNodes(const ::Ice::Current &=Ice::emptyCurrent) override
::armarx::FramedPoseBasePtr getPose(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition GraphNode.cpp:80
bool removeAdjacentNode(const std::string &nodeId, const Ice::Current &) override
GraphNodePtr clone(const Ice::Current &=Ice::emptyCurrent) const
Ice::ObjectPtr ice_clone() const override
int getOutdegree(const ::Ice::Current &=Ice::emptyCurrent) const override
float getYawAngle() const
~GraphNode() override
Definition GraphNode.cpp:75
void addAdjacentNode(const ::memoryx::EntityRefBasePtr &newAdjacentNode, const ::Ice::Current &c=Ice::emptyCurrent) override
::std::string getScene(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition GraphNode.cpp:92
void setPose(const ::armarx::FramedPoseBasePtr &pose, const ::Ice::Current &=Ice::emptyCurrent) override
Definition GraphNode.cpp:86
::memoryx::EntityRefBasePtr getAdjacentNode(::Ice::Int i, const ::Ice::Current &=Ice::emptyCurrent) override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_VERBOSE_S
Definition Logging.h:207
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
IceInternal::Handle< GraphNode > GraphNodePtr
Definition GraphNode.h:47