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