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
23void
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
39void
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
55void
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
71void
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
87void
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
110int
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
134void
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
167int
168GraphProcessor::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
189bool
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
218bool
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
266int
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
313bool
314GraphProcessor::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}
std::vector< CSGEdge * > TEdgeList
std::vector< CSGNode * > TNodeList
TSphereCoord getPosition()
void setPosition(TSphereCoord position)
TNodeList * getNodes()
std::vector< int > * getNodeAdjacency(int nIndex)
int addEdge(int nIndex1, int nIndex2)
TEdgeList * getEdges()
int addNode(CSGNode *pNode)
virtual CSGNode * getNewNode()
static TSphereCoord inverseTransform(TSphereCoord sc, TTransform transform)
static float getDistanceOnArc(TSphereCoord p1, TSphereCoord p2)
static float CalculateCrossProductFromDifference(TSphereCoord p1, TSphereCoord p2, TSphereCoord p3)
static TSphereCoord transform(TSphereCoord sc, TTransform transform)
int findClosestNode(CSphericalGraph *pGraph, TSphereCoord coord)
void findClosestNeighbours(CSphericalGraph *pGraph, int &s, int &t)
void inverseTransform(CSphericalGraph *pGraph, TTransform transform)
void copyGraphNodesAndEdges(CSphericalGraph *pSrcGraph, CSphericalGraph *pDstGraph)
bool insideCircumcircle(CSphericalGraph *pGraph, int nIndex1, int nIndex2, int nIndex3, int nPointIndex, bool &bInside)
void invertTheta(CSphericalGraph *pGraph)
int getDelaunayNeighbour(CSphericalGraph *pGraph, CSGEdge *pEdge)
bool getCircumcircle(CSphericalGraph *pGraph, int nIndex1, int nIndex2, int nIndex3, TSphereCoord &center, float &fRadius)
void transform(CSphericalGraph *pGraph, TTransform transform)
void invertPhi(CSphericalGraph *pGraph)
bool isEdgePresent(CSphericalGraph *pGraph, int nIndex1, int nIndex2)
int findEdge(CSphericalGraph *pGraph, int nIndex1, int nIndex2)
float fTheta
Definition Structs.h:27
float fPhi
Definition Structs.h:26