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
33 const 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 
40 memoryx::GraphNode::GraphNode()
41 {
42 }
43 
44 memoryx::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);
53  putAttribute(GRAPH_NODE_ATTR_POSE,
54  ::armarx::FramedPoseBasePtr{new ::armarx::FramedPose(
55  rot, Eigen::Vector3f(x, y, 0), armarx::GlobalFrame, "")});
56  putAttribute(GRAPH_NODE_ATTR_SCENE, scene);
57  clearAdjacentNodes(); //init adjacent node attr
58 }
59 
60 memoryx::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 
76 {
77 }
78 
79 armarx::FramedPoseBasePtr
80 memoryx::GraphNode::getPose(const Ice::Current&) const
81 {
82  return getAttributeValue(GRAPH_NODE_ATTR_POSE)->get<armarx::FramedPoseBase>();
83 }
84 
85 void
86 memoryx::GraphNode::setPose(const armarx::FramedPoseBasePtr& pose, const Ice::Current&)
87 {
88  putAttribute(GRAPH_NODE_ATTR_POSE, pose);
89 }
90 
91 std::string
92 memoryx::GraphNode::getScene(const Ice::Current&) const
93 {
94  return getAttributeValue(GRAPH_NODE_ATTR_SCENE)->get<std::string>();
95 }
96 
97 void
98 memoryx::GraphNode::setScene(const std::string& scene, const Ice::Current&)
99 {
100  putAttribute(GRAPH_NODE_ATTR_SCENE, scene);
101 }
102 
103 int
104 memoryx::GraphNode::getOutdegree(const Ice::Current&) const
105 {
106  return getAttribute(GRAPH_NODE_ATTR_ADJ_NODES)->size();
107 }
108 
109 memoryx::EntityRefBasePtr
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 
126 memoryx::EntityRefBasePtr
127 memoryx::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 
141 memoryx::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 
156 void
157 memoryx::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 
170  getAttribute(GRAPH_NODE_ATTR_ADJ_NODES)
171  ->addValue(armarx::VariantBasePtr{new armarx::Variant{newAdjacentNode}});
172  ARMARX_VERBOSE_S << "adding new adjacent node: done! (new outdegree: " << getOutdegree() << ")";
173 }
174 
175 void
177 {
178  putAttribute(
179  memoryx::EntityAttributeBasePtr{new memoryx::EntityAttribute{GRAPH_NODE_ATTR_ADJ_NODES}});
180 }
181 
182 bool
183 memoryx::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 
198 float
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 
208 {
209  return this->clone();
210 }
211 
213 memoryx::GraphNode::clone(const Ice::Current&) const
214 {
215  GraphNodePtr ret = new GraphNode(*this);
216  // ret->deepCopy(*this);
217  return ret;
218 }
memoryx::GraphNode::addAdjacentNode
void addAdjacentNode(const ::memoryx::EntityRefBasePtr &newAdjacentNode, const ::Ice::Current &c=Ice::emptyCurrent) override
Definition: GraphNode.cpp:157
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
memoryx::GraphNode::clone
GraphNodePtr clone(const Ice::Current &=Ice::emptyCurrent) const
Definition: GraphNode.cpp:213
memoryx::GraphNode::getAdjacentNodeById
::memoryx::EntityRefBasePtr getAdjacentNodeById(const std::string &nodeId, const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraphNode.cpp:127
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:36
memoryx::GraphNode::getOutdegree
int getOutdegree(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: GraphNode.cpp:104
memoryx::GraphNode::getPose
::armarx::FramedPoseBasePtr getPose(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: GraphNode.cpp:80
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
memoryx::GraphNode::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: GraphNode.cpp:207
GRAPH_NODE_ATTR_ADJ_NODES
#define GRAPH_NODE_ATTR_ADJ_NODES
Definition: GraphNode.cpp:38
memoryx::GraphNode::~GraphNode
~GraphNode() override
Definition: GraphNode.cpp:75
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
memoryx::GraphNode::getAdjacentNode
::memoryx::EntityRefBasePtr getAdjacentNode(::Ice::Int i, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraphNode.cpp:110
IceInternal::Handle<::armarx::VariantBase >
memoryx::VariantType::GraphNode
const armarx::VariantTypeId GraphNode
Definition: GraphNode.h:40
memoryx::GraphNode::setScene
void setScene(const std::string &scene, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraphNode.cpp:98
FramedPose.h
GRAPH_NODE_ATTR_SCENE
#define GRAPH_NODE_ATTR_SCENE
Definition: GraphNode.cpp:36
GraphNode.h
GRAPH_NODE_ATTR_POSE
#define GRAPH_NODE_ATTR_POSE
Definition: GraphNode.cpp:37
memoryx::GraphNode::clearAdjacentNodes
void clearAdjacentNodes(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraphNode.cpp:176
memoryx::GraphNode::GRAPH_NODE_ATTR_SCENE
static const std::string GRAPH_NODE_ATTR_SCENE
Definition: GraphNode.h:60
memoryx::GraphNode::getAdjacentNodes
memoryx::GraphNodeBaseList getAdjacentNodes(const Ice::Current &c=Ice::emptyCurrent) override
Definition: GraphNode.cpp:142
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
memoryx::GraphNode::getYawAngle
float getYawAngle() const
Definition: GraphNode.cpp:199
memoryx::EntityAttribute
Attribute of MemoryX entities.
Definition: EntityAttribute.h:49
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:207
memoryx::GraphNode::setPose
void setPose(const ::armarx::FramedPoseBasePtr &pose, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: GraphNode.cpp:86
memoryx::GraphNode::removeAdjacentNode
bool removeAdjacentNode(const std::string &nodeId, const Ice::Current &) override
Definition: GraphNode.cpp:183
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
Logging.h
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
memoryx::GraphNode::getScene
::std::string getScene(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: GraphNode.cpp:92