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 // *****************************************************************
22 {
23  clear();
24 }
25 
27 {
28  *this = prototype;
29 }
30 
32 {
33  clear();
34 }
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 // *****************************************************************
68 void
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
98 int
100 {
101  m_Nodes.push_back(pNode);
102  pNode->setIndex(int(m_Nodes.size() - 1));
103 
104  return pNode->getIndex();
105 }
106 
107 // edges
108 int
109 CSphericalGraph::addEdge(int nIndex1, int nIndex2)
110 {
111  return addEdge(nIndex1, nIndex2, -1, -1);
112 }
113 
114 int
115 CSphericalGraph::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 
135 void
136 CSphericalGraph::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 // *****************************************************************
163 TEdgeList*
165 {
166  return &m_Edges;
167 }
168 
169 TNodeList*
171 {
172  return &m_Nodes;
173 }
174 
175 CSGEdge*
177 {
178  return m_Edges.at(nEdgeIndex);
179 }
180 
181 CSGNode*
183 {
184  return m_Nodes.at(nIndex);
185 }
186 
187 std::vector<int>*
189 {
190  return &m_NodeAdjacency[nIndex];
191 }
192 
193 // *****************************************************************
194 // file io
195 // *****************************************************************
196 bool
197 CSphericalGraph::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 
230 bool
231 CSphericalGraph::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 
253 bool
254 CSphericalGraph::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 
268 bool
269 CSphericalGraph::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 
301 bool
302 CSphericalGraph::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 
314 bool
315 CSphericalGraph::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 }
TNodeList
std::vector< CSGNode * > TNodeList
Definition: SphericalGraph.h:100
CSGNode::getIndex
int getIndex()
Definition: SphericalGraph.h:72
CSphericalGraph::operator=
CSphericalGraph & operator=(CSphericalGraph const &rhs)
Definition: SphericalGraph.cpp:37
CSGNode::setIndex
void setIndex(int nIndex)
Definition: SphericalGraph.h:78
CSphericalGraph::read
virtual bool read(std::istream &infile)
Definition: SphericalGraph.cpp:197
TSphereCoord::fTheta
float fTheta
Definition: Structs.h:27
CSGEdge::nLeftFace
int nLeftFace
Definition: SphericalGraph.h:39
CSphericalGraph::getEdges
TEdgeList * getEdges()
Definition: SphericalGraph.cpp:164
CSphericalGraph::readNode
virtual bool readNode(std::istream &infile)
Definition: SphericalGraph.cpp:231
CSphericalGraph::addNode
int addNode(CSGNode *pNode)
Definition: SphericalGraph.cpp:99
SphericalGraph.h
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
CSphericalGraph::CSphericalGraph
CSphericalGraph()
Definition: SphericalGraph.cpp:21
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
CSphericalGraph::m_Nodes
TNodeList m_Nodes
Definition: SphericalGraph.h:167
CSphericalGraph::readEdge
virtual bool readEdge(std::istream &infile)
Definition: SphericalGraph.cpp:254
CSphericalGraph::getNodeAdjacency
std::vector< int > * getNodeAdjacency(int nIndex)
Definition: SphericalGraph.cpp:188
CSphericalGraph::clear
void clear()
Definition: SphericalGraph.cpp:69
CSphericalGraph::writeNode
virtual bool writeNode(std::ostream &outfile, int n)
Definition: SphericalGraph.cpp:302
CSphericalGraph::m_Edges
TEdgeList m_Edges
Definition: SphericalGraph.h:166
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
CSphericalGraph::getEdge
CSGEdge * getEdge(int nEdgeIndex)
Definition: SphericalGraph.cpp:176
CSphericalGraph::addNodeAdjacency
void addNodeAdjacency(int nNode, int nAdjacency)
Definition: SphericalGraph.cpp:136
CSphericalGraph::writeEdge
virtual bool writeEdge(std::ostream &outfile, int e)
Definition: SphericalGraph.cpp:315
CSphericalGraph::m_NodeAdjacency
std::vector< int > m_NodeAdjacency[MAX_NODES]
Definition: SphericalGraph.h:168
CSphericalGraph
Definition: SphericalGraph.h:105
TSphereCoord
Definition: Structs.h:23
CSphericalGraph::write
virtual bool write(std::ostream &outfile)
Definition: SphericalGraph.cpp:269
CSphericalGraph::getNode
CSGNode * getNode(int nIndex)
Definition: SphericalGraph.cpp:182
CSphericalGraph::~CSphericalGraph
virtual ~CSphericalGraph()
Definition: SphericalGraph.cpp:31