GraphProcessor.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: GraphProcessor.cpp
3 // Copyright: Kai Welke, Chair Prof. Dillmann (IAIM),
4 // Institute for Computer Science and Engineering (CSE),
5 // University of Karlsruhe. All rights reserved.
6 // Author: Kai Welke
7 // Date: 12.06.2007
8 // *****************************************************************
9 
10 
11 // *****************************************************************
12 // includes
13 // *****************************************************************
14 // system
15 #include <float.h>
16 #include <math.h>
17 
18 // local includes
19 #include "Base/Math/MathTools.h"
20 #include "Base/Tools/DebugMemory.h"
21 #include "GraphProcessor.h"
22 
23 void
25 {
26  TNodeList* pNodes = pGraph->getNodes();
27 
28  TSphereCoord position, position_tr;
29 
30  for (int n = 0; n < int(pNodes->size()); n++)
31  {
32  CSGNode* pNode = pNodes->at(n);
33  position = pNode->getPosition();
34  position_tr = MathTools::transform(position, transform);
35  pNode->setPosition(position_tr);
36  }
37 }
38 
39 void
41 {
42  TNodeList* pNodes = pGraph->getNodes();
43 
44  TSphereCoord position, position_tr;
45 
46  for (int n = 0; n < int(pNodes->size()); n++)
47  {
48  CSGNode* pNode = pNodes->at(n);
49  position = pNode->getPosition();
50  position_tr = MathTools::inverseTransform(position, transform);
51  pNode->setPosition(position_tr);
52  }
53 }
54 
55 void
57 {
58  TNodeList* pNodes = pGraph->getNodes();
59 
60  TSphereCoord position;
61 
62  for (int n = 0; n < int(pNodes->size()); n++)
63  {
64  CSGNode* pNode = pNodes->at(n);
65  position = pNode->getPosition();
66  position.fPhi = 360.0f - position.fPhi;
67  pNode->setPosition(position);
68  }
69 }
70 
71 void
73 {
74  TNodeList* pNodes = pGraph->getNodes();
75 
76  TSphereCoord position;
77 
78  for (int n = 0; n < int(pNodes->size()); n++)
79  {
80  CSGNode* pNode = pNodes->at(n);
81  position = pNode->getPosition();
82  position.fTheta = 180.0f - position.fTheta;
83  pNode->setPosition(position);
84  }
85 }
86 
87 void
89 {
90  pDstGraph->clear();
91 
92  TNodeList* pNodes = pSrcGraph->getNodes();
93  TEdgeList* pEdges = pSrcGraph->getEdges();
94 
95  for (int n = 0; n < int(pNodes->size()); n++)
96  {
97  CSGNode* pNode = pNodes->at(n);
98  CSGNode* pNewNode = pDstGraph->getNewNode();
99  pNewNode->setPosition(pNode->getPosition());
100  pDstGraph->addNode(pNewNode);
101  }
102 
103  for (int e = 0; e < int(pEdges->size()); e++)
104  {
105  CSGEdge* pEdge = pEdges->at(e);
106  pDstGraph->addEdge(pEdge->nIndex1, pEdge->nIndex2, pEdge->nLeftFace, pEdge->nRightFace);
107  }
108 }
109 
110 int
112 {
113  TNodeList* pNodes = pGraph->getNodes();
114 
115  float fMinDistance = FLT_MAX;
116  float fDistance = 0.0f;
117  int nIndex = -1;
118 
119  for (int n = 0; n < int(pNodes->size()); n++)
120  {
121  CSGNode* pNode = pNodes->at(n);
122  fDistance = MathTools::getDistanceOnArc(pNode->getPosition(), coord);
123 
124  if (fDistance < fMinDistance)
125  {
126  fMinDistance = fDistance;
127  nIndex = n;
128  }
129  }
130 
131  return nIndex;
132 }
133 
134 void
136 {
137  TNodeList* pNodes = pGraph->getNodes();
138 
139  float fMinDistance = FLT_MAX;
140  int nIndex1 = -1;
141  int nIndex2 = -1;
142  float fDistance;
143 
144  for (int i = 0; i < int(pNodes->size()); i++)
145  {
146  for (int j = 0; j < int(pNodes->size()); j++)
147  {
148  if (i != j)
149  {
150  fDistance = MathTools::getDistanceOnArc(pNodes->at(i)->getPosition(),
151  pNodes->at(j)->getPosition());
152 
153  if (fDistance < fMinDistance)
154  {
155  fMinDistance = fDistance;
156  nIndex1 = i;
157  nIndex2 = j;
158  }
159  }
160  }
161  }
162 
163  s = nIndex1;
164  t = nIndex2;
165 }
166 
167 int
168 GraphProcessor::findEdge(CSphericalGraph* pGraph, int nIndex1, int nIndex2)
169 {
170  TEdgeList* pEdges = pGraph->getEdges();
171 
172  for (int e = 0; e < int(pEdges->size()); e++)
173  {
174  if ((pEdges->at(e)->nIndex1 == nIndex1) && (pEdges->at(e)->nIndex2 == nIndex2))
175  {
176  return e;
177  }
178 
179  if ((pEdges->at(e)->nIndex1 == nIndex2) && (pEdges->at(e)->nIndex2 == nIndex1))
180  {
181  return e;
182  }
183  }
184 
185  // not found
186  return -1;
187 }
188 
189 bool
191  int nIndex1,
192  int nIndex2,
193  int nIndex3,
194  int nPointIndex,
195  bool& bInside)
196 {
197  TNodeList* pNodes = pGraph->getNodes();
198 
199  TSphereCoord center;
200  float fRadius;
201 
202  if (!getCircumcircle(pGraph, nIndex1, nIndex2, nIndex3, center, fRadius))
203  {
204  return false;
205  }
206 
207  TSphereCoord point = pNodes->at(nPointIndex)->getPosition();
208 
209  // check with give coordinates
210  float fDistance = sqrt((center.fPhi - point.fPhi) * (center.fPhi - point.fPhi) +
211  (center.fTheta - point.fTheta) * (center.fTheta - point.fTheta));
212 
213  bInside = (fDistance < (fRadius + 0.0001));
214 
215  return true;
216 }
217 
218 bool
220  int nIndex1,
221  int nIndex2,
222  int nIndex3,
223  TSphereCoord& center,
224  float& fRadius)
225 {
226  TNodeList* pNodes = pGraph->getNodes();
227  float cp;
228 
229  TSphereCoord s1, s2, s3;
230  s1 = pNodes->at(nIndex1)->getPosition();
231  s2 = pNodes->at(nIndex2)->getPosition();
232  s3 = pNodes->at(nIndex3)->getPosition();
233 
235 
236  if (cp != 0.0)
237  {
238  float p1Sq, p2Sq, p3Sq;
239  float num;
240  float cx, cy;
241 
242  p1Sq = s1.fPhi * s1.fPhi + s1.fTheta * s1.fTheta;
243  p2Sq = s2.fPhi * s2.fPhi + s2.fTheta * s2.fTheta;
244  p3Sq = s3.fPhi * s3.fPhi + s3.fTheta * s3.fTheta;
245 
246  num = p1Sq * (s2.fTheta - s3.fTheta) + p2Sq * (s3.fTheta - s1.fTheta) +
247  p3Sq * (s1.fTheta - s2.fTheta);
248  cx = num / (2.0f * cp);
249  num = p1Sq * (s3.fPhi - s2.fPhi) + p2Sq * (s1.fPhi - s3.fPhi) + p3Sq * (s2.fPhi - s1.fPhi);
250  cy = num / (2.0f * cp);
251 
252  center.fPhi = cx;
253  center.fTheta = cy;
254 
255  fRadius = sqrt((center.fPhi - s1.fPhi) * (center.fPhi - s1.fPhi) +
256  (center.fTheta - s1.fTheta) * (center.fTheta - s1.fTheta));
257  return true;
258  }
259  else
260  {
261  // points lie on a line
262  return false;
263  }
264 }
265 
266 int
268 {
269  TNodeList* pNodes = pGraph->getNodes();
270  float fMinRadius = FLT_MAX;
271  float fRadius;
272  int nIndex = -1;
273 
274  // go through all points
275  for (int v = 0; v < int(pNodes->size()); v++)
276  {
277  if ((v == pEdge->nIndex1) || (v == pEdge->nIndex2))
278  {
279  continue;
280  }
281 
282  // check if v is already connected to index1, index2
283  if (GraphProcessor::isEdgePresent(pGraph, pEdge->nIndex1, v))
284  {
285  continue;
286  }
287 
288  if (GraphProcessor::isEdgePresent(pGraph, pEdge->nIndex2, v))
289  {
290  continue;
291  }
292 
293  // check if v is an unconnected node
294  if (pGraph->getNodeAdjacency(v)->size() != 0)
295  {
296  continue;
297  }
298 
299  TSphereCoord center;
300 
301  getCircumcircle(pGraph, pEdge->nIndex1, pEdge->nIndex2, v, center, fRadius);
302 
303  if (fRadius < fMinRadius)
304  {
305  fMinRadius = fRadius;
306  nIndex = v;
307  }
308  }
309 
310  return nIndex;
311 }
312 
313 bool
314 GraphProcessor::isEdgePresent(CSphericalGraph* pGraph, int nIndex1, int nIndex2)
315 {
316 
317  bool bPresent = false;
318 
319  vector<int>::iterator iter = pGraph->getNodeAdjacency(nIndex1)->begin();
320 
321  while (iter != pGraph->getNodeAdjacency(nIndex1)->end())
322  {
323  if ((*iter) == nIndex2)
324  {
325  bPresent = true;
326  }
327 
328  iter++;
329  }
330 
331  return bPresent;
332 }
GraphProcessor::findEdge
int findEdge(CSphericalGraph *pGraph, int nIndex1, int nIndex2)
Definition: GraphProcessor.cpp:168
TNodeList
std::vector< CSGNode * > TNodeList
Definition: SphericalGraph.h:100
GraphProcessor::insideCircumcircle
bool insideCircumcircle(CSphericalGraph *pGraph, int nIndex1, int nIndex2, int nIndex3, int nPointIndex, bool &bInside)
Definition: GraphProcessor.cpp:190
TSphereCoord::fTheta
float fTheta
Definition: Structs.h:27
CSGEdge::nLeftFace
int nLeftFace
Definition: SphericalGraph.h:39
CSphericalGraph::getEdges
TEdgeList * getEdges()
Definition: SphericalGraph.cpp:164
GraphProcessor.h
CSphericalGraph::addNode
int addNode(CSGNode *pNode)
Definition: SphericalGraph.cpp:99
GraphProcessor::inverseTransform
void inverseTransform(CSphericalGraph *pGraph, TTransform transform)
Definition: GraphProcessor.cpp:40
GraphProcessor::copyGraphNodesAndEdges
void copyGraphNodesAndEdges(CSphericalGraph *pSrcGraph, CSphericalGraph *pDstGraph)
Definition: GraphProcessor.cpp:88
GraphProcessor::invertTheta
void invertTheta(CSphericalGraph *pGraph)
Definition: GraphProcessor.cpp:72
GraphProcessor::isEdgePresent
bool isEdgePresent(CSphericalGraph *pGraph, int nIndex1, int nIndex2)
Definition: GraphProcessor.cpp:314
GraphProcessor::findClosestNode
int findClosestNode(CSphericalGraph *pGraph, TSphereCoord coord)
Definition: GraphProcessor.cpp:111
CSGNode::getPosition
TSphereCoord getPosition()
Definition: SphericalGraph.h:60
CSphericalGraph::getNewNode
virtual CSGNode * getNewNode()
Definition: SphericalGraph.h:117
CSphericalGraph::addEdge
int addEdge(int nIndex1, int nIndex2)
Definition: SphericalGraph.cpp:109
CSGEdge::nIndex2
int nIndex2
Definition: SphericalGraph.h:36
CSGNode::setPosition
void setPosition(TSphereCoord position)
Definition: SphericalGraph.h:66
TTransform
Definition: Structs.h:43
GraphProcessor::findClosestNeighbours
void findClosestNeighbours(CSphericalGraph *pGraph, int &s, int &t)
Definition: GraphProcessor.cpp:135
CSGEdge
Definition: SphericalGraph.h:31
CSGNode
Definition: SphericalGraph.h:43
CSGEdge::nRightFace
int nRightFace
Definition: SphericalGraph.h:40
TSphereCoord::fPhi
float fPhi
Definition: Structs.h:26
MathTools::inverseTransform
static TSphereCoord inverseTransform(TSphereCoord sc, TTransform transform)
Definition: MathTools.cpp:148
GraphProcessor::getCircumcircle
bool getCircumcircle(CSphericalGraph *pGraph, int nIndex1, int nIndex2, int nIndex3, TSphereCoord &center, float &fRadius)
Definition: GraphProcessor.cpp:219
GraphProcessor::getDelaunayNeighbour
int getDelaunayNeighbour(CSphericalGraph *pGraph, CSGEdge *pEdge)
Definition: GraphProcessor.cpp:267
MathTools::CalculateCrossProductFromDifference
static float CalculateCrossProductFromDifference(TSphereCoord p1, TSphereCoord p2, TSphereCoord p3)
Definition: MathTools.cpp:281
MathTools::transform
static TSphereCoord transform(TSphereCoord sc, TTransform transform)
Definition: MathTools.cpp:127
CSphericalGraph::getNodeAdjacency
std::vector< int > * getNodeAdjacency(int nIndex)
Definition: SphericalGraph.cpp:188
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT >>
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:351
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
CSphericalGraph::clear
void clear()
Definition: SphericalGraph.cpp:69
CSphericalGraph::getNodes
TNodeList * getNodes()
Definition: SphericalGraph.cpp:170
CSGEdge::nIndex1
int nIndex1
Definition: SphericalGraph.h:35
TEdgeList
std::vector< CSGEdge * > TEdgeList
Definition: SphericalGraph.h:99
GraphProcessor::transform
void transform(CSphericalGraph *pGraph, TTransform transform)
Definition: GraphProcessor.cpp:24
MathTools::getDistanceOnArc
static float getDistanceOnArc(TSphereCoord p1, TSphereCoord p2)
Definition: MathTools.cpp:302
CSphericalGraph
Definition: SphericalGraph.h:105
TSphereCoord
Definition: Structs.h:23
GraphProcessor::invertPhi
void invertPhi(CSphericalGraph *pGraph)
Definition: GraphProcessor.cpp:56
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33