SphericalGraph.cpp
Go to the documentation of this file.
1// *****************************************************************
2// Filename: AspectGraph.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#include "SphericalGraph.h"
15
16//#include "Base/Tools/DebugMemory.h"
17
18// *****************************************************************
19// construction / destruction
20// *****************************************************************
25
27{
28 *this = prototype;
29}
30
35
38{
39 clear();
40
41 // copy all nodes
42 const TNodeList* nodes = &rhs.m_Nodes;
43
44 int nNumberNodes = int(nodes->size());
45
46 for (int n = 0; n < nNumberNodes; n++)
47 {
48 addNode(nodes->at(n)->clone());
49 }
50
51 // copy all edges
52 const TEdgeList* edges = &rhs.m_Edges;
53
54 int nNumberEdges = int(edges->size());
55
56 for (int e = 0; e < nNumberEdges; e++)
57 {
58 CSGEdge* edge = edges->at(e);
59 addEdge(edge->nIndex1, edge->nIndex2, edge->nLeftFace, edge->nRightFace);
60 }
61
62 return *this;
63}
64
65// *****************************************************************
66// control
67// *****************************************************************
68void
70{
71 // clear edges
72 std::vector<CSGEdge*>::iterator iter = m_Edges.begin();
73
74 while (iter != m_Edges.end())
75 {
76 delete *iter;
77 iter++;
78 }
79
80 m_Edges.clear();
81
82 // clear nodes
83 std::vector<CSGNode*>::iterator iter_node = m_Nodes.begin();
84
85 while (iter_node != m_Nodes.end())
86 {
87 delete *iter_node;
88 iter_node++;
89 }
90
91 m_Nodes.clear();
92}
93
94// *****************************************************************
95// graph building
96// *****************************************************************
97// nodes
98int
100{
101 m_Nodes.push_back(pNode);
102 pNode->setIndex(int(m_Nodes.size() - 1));
103
104 return pNode->getIndex();
105}
106
107// edges
108int
109CSphericalGraph::addEdge(int nIndex1, int nIndex2)
110{
111 return addEdge(nIndex1, nIndex2, -1, -1);
112}
113
114int
115CSphericalGraph::addEdge(int nIndex1, int nIndex2, int nLeftFace, int nRightFace)
116{
117 CSGEdge* pEdge = new CSGEdge;
118 pEdge->nIndex1 = nIndex1;
119 pEdge->nIndex2 = nIndex2;
120 pEdge->nLeftFace = nLeftFace;
121 pEdge->nRightFace = nRightFace;
122
123 // printf("Adding edge %d --> %d\n",nIndex1,nIndex2);
124
125 // add to list of edges
126 m_Edges.push_back(pEdge);
127
128 // update node adjacency list
129 addNodeAdjacency(nIndex1, nIndex2);
130 addNodeAdjacency(nIndex2, nIndex1);
131
132 return int(m_Edges.size()) - 1;
133}
134
135void
136CSphericalGraph::addNodeAdjacency(int nNode, int nAdjacency)
137{
138 if (nNode == nAdjacency)
139 {
140 printf("Error: adding node as adjacenca\n");
141 }
142
143 // check if present
144 bool bPresent = false;
145
146 for (int n : m_NodeAdjacency[nNode])
147 {
148 if (n == nAdjacency)
149 {
150 bPresent = true;
151 }
152 }
153
154 if (!bPresent)
155 {
156 m_NodeAdjacency[nNode].push_back(nAdjacency);
157 }
158}
159
160// *****************************************************************
161// member access
162// *****************************************************************
165{
166 return &m_Edges;
167}
168
171{
172 return &m_Nodes;
173}
174
175CSGEdge*
177{
178 return m_Edges.at(nEdgeIndex);
179}
180
181CSGNode*
183{
184 return m_Nodes.at(nIndex);
185}
186
187std::vector<int>*
189{
190 return &m_NodeAdjacency[nIndex];
191}
192
193// *****************************************************************
194// file io
195// *****************************************************************
196bool
197CSphericalGraph::read(std::istream& infile)
198{
199 try
200 {
201 int nNumberNodes;
202
203 // read number of nodes
204 infile >> nNumberNodes;
205
206 // read all nodes
207 for (int n = 0; n < nNumberNodes; n++)
208 {
209 readNode(infile);
210 }
211
212 // read number of edges
213 int nNumberEdges;
214 infile >> nNumberEdges;
215
216 for (int e = 0; e < nNumberEdges; e++)
217 {
218 readEdge(infile);
219 }
220 }
221 catch (std::istream::failure&)
222 {
223 printf("ERROR: failed to write FeatureGraph to file\n");
224 return false;
225 }
226
227 return true;
228}
229
230bool
231CSphericalGraph::readNode(std::istream& infile)
232{
233 CSGNode* pNewNode = (CSGNode*)getNewNode();
234
235 int nIndex;
236 TSphereCoord pos;
237
238 infile >> nIndex;
239 infile >> pos.fPhi;
240 infile >> pos.fTheta;
241
242 pNewNode->setPosition(pos);
243
244 if (addNode(pNewNode) != nIndex)
245 {
246 printf("Error input file inconsistent\n");
247 return false;
248 }
249
250 return true;
251}
252
253bool
254CSphericalGraph::readEdge(std::istream& infile)
255{
256 int nIndex1, nIndex2, nLeftFace, nRightFace;
257
258 infile >> nIndex1;
259 infile >> nIndex2;
260
261 infile >> nLeftFace;
262 infile >> nRightFace;
263
264 addEdge(nIndex1, nIndex2, nLeftFace, nRightFace);
265 return true;
266}
267
268bool
269CSphericalGraph::write(std::ostream& outfile)
270{
271 try
272 {
273 int nNumberNodes = int(m_Nodes.size());
274 // write number of nodes
275 outfile << nNumberNodes << std::endl;
276
277 // write all nodes
278 for (int n = 0; n < nNumberNodes; n++)
279 {
280 writeNode(outfile, n);
281 }
282
283 // write number of edges
284 int nNumberEdges = int(m_Edges.size());
285 outfile << nNumberEdges << std::endl;
286
287 for (int e = 0; e < nNumberEdges; e++)
288 {
289 writeEdge(outfile, e);
290 }
291 }
292 catch (std::ostream::failure&)
293 {
294 printf("ERROR: failed to write FeatureGraph to file\n");
295 return false;
296 }
297
298 return true;
299}
300
301bool
302CSphericalGraph::writeNode(std::ostream& outfile, int n)
303{
304 CSGNode* pCurrentNode = m_Nodes.at(n);
305
306 outfile << pCurrentNode->getIndex() << std::endl;
307 outfile << pCurrentNode->getPosition().fPhi << std::endl;
308 outfile << pCurrentNode->getPosition().fTheta << std::endl;
309
310 outfile << std::endl;
311 return true;
312}
313
314bool
315CSphericalGraph::writeEdge(std::ostream& outfile, int e)
316{
317 CSGEdge* pCurrentEdge = m_Edges.at(e);
318 outfile << pCurrentEdge->nIndex1 << " ";
319 outfile << pCurrentEdge->nIndex2 << " ";
320
321 outfile << pCurrentEdge->nLeftFace << " ";
322 outfile << pCurrentEdge->nRightFace << " ";
323
324 outfile << std::endl;
325 return true;
326}
std::vector< CSGEdge * > TEdgeList
std::vector< CSGNode * > TNodeList
int getIndex()
TSphereCoord getPosition()
void setPosition(TSphereCoord position)
void setIndex(int nIndex)
CSGEdge * getEdge(int nEdgeIndex)
virtual bool writeNode(std::ostream &outfile, int n)
void addNodeAdjacency(int nNode, int nAdjacency)
TNodeList * getNodes()
virtual ~CSphericalGraph()
virtual bool write(std::ostream &outfile)
std::vector< int > * getNodeAdjacency(int nIndex)
std::vector< int > m_NodeAdjacency[MAX_NODES]
virtual bool readNode(std::istream &infile)
virtual bool readEdge(std::istream &infile)
int addEdge(int nIndex1, int nIndex2)
TEdgeList * getEdges()
int addNode(CSGNode *pNode)
virtual bool writeEdge(std::ostream &outfile, int e)
CSGNode * getNode(int nIndex)
virtual CSGNode * getNewNode()
CSphericalGraph & operator=(CSphericalGraph const &rhs)
virtual bool read(std::istream &infile)
float fTheta
Definition Structs.h:27
float fPhi
Definition Structs.h:26