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