IntensityGraph.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: SensoryEgoSphere.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: 21.07.2008
8 // *****************************************************************
9 
10 // *****************************************************************
11 // includes
12 // *****************************************************************
13 #include "IntensityGraph.h"
14 //#include "Base/Math/DistributePoints.h"
15 //#include "MathTools.h"
16 //#include "Base/DataStructures/Graph/GraphProcessor.h"
17 
18 #include <cfloat>
19 #include <fstream>
20 
21 // *****************************************************************
22 // implementation of CIntensityNode
23 // *****************************************************************
25 {
26  setIntensity(0.0f);
27 }
28 
30 
31  = default;
32 
34  : CSGNode(coord)
35 {
36  setIntensity(0.0f);
37 }
38 
40  = default;
41 
42 void CIntensityNode::setIntensity(float fIntensity)
43 {
44  m_fIntensity = fIntensity;
45 }
46 
48 {
49  return m_fIntensity;
50 }
51 
52 // *****************************************************************
53 // implementation of CIntensityGraph
54 // *****************************************************************
55 // *****************************************************************
56 // construction / destruction
57 // *****************************************************************
59 {
60  reset();
61 }
62 
63 //CIntensityGraph::CIntensityGraph(int nNumberNodes)
64 //{
65 // // generate points on sphere
66 // points P = generatePoints(nNumberNodes,1000);
67 
68 // for(int i = 0 ; i < int(P.V.size()) ; i++)
69 // {
70 // // project point to polar coordinates
71 // Vec3d point;
72 // Math3d::SetVec(point,P.V.at(i).array()[0],P.V.at(i).array()[1],P.V.at(i).array()[2]);
73 // TSphereCoord point_on_sphere;
74 // MathTools::convert(point, point_on_sphere);
75 
76 // CIntensityNode* pNode = new CIntensityNode(point_on_sphere);
77 
78 // addNode(pNode);
79 // }
80 
81 // // initialize
82 // reset();
83 //}
84 
85 CIntensityGraph::CIntensityGraph(std::string sFilename)
86 {
87  printf("Reading intensity graph from: %s\n", sFilename.c_str());
88 
89  std::ifstream infile;
90  infile.open(sFilename.c_str());
91 
92  read(infile);
93 
94  //printf("Read number nodes: %d\n", getNodes()->size());
95  //printf("Read number edges: %d\n", getEdges()->size());
96 }
97 
99  = default;
100 
102 {
103  *((CSphericalGraph*) this) = (const CSphericalGraph) rhs;
104  return *this;
105 }
106 
107 // *****************************************************************
108 // setting
109 // *****************************************************************
111 {
112  set(0.0f);
113 }
114 
115 void CIntensityGraph::set(float fIntensity)
116 {
117  TNodeList* pNodes = getNodes();
118 
119  for (auto& pNode : *pNodes)
120  {
121  ((CIntensityNode*) pNode)->setIntensity(fIntensity);
122  }
123 }
124 
125 
126 // *****************************************************************
127 // file io
128 // *****************************************************************
129 bool CIntensityGraph::read(std::istream& infile)
130 {
131  try
132  {
133  int nNumberNodes;
134 
135  // read number of nodes
136  infile >> nNumberNodes;
137 
138  // read all nodes
139  for (int n = 0 ; n < nNumberNodes ; n++)
140  {
141  readNode(infile);
142  }
143 
144  // read number of edges
145  int nNumberEdges;
146  infile >> nNumberEdges;
147 
148  for (int e = 0 ; e < nNumberEdges ; e++)
149  {
150  readEdge(infile);
151  }
152 
153  }
154  catch (std::istream::failure&)
155  {
156  printf("ERROR: failed to write FeatureGraph to file\n");
157  return false;
158  }
159 
160  return true;
161 }
162 
163 bool CIntensityGraph::readNode(std::istream& infile)
164 {
165  CIntensityNode* pNewNode = getNewNode();
166 
167  int nIndex;
168  TSphereCoord pos;
169  float fIntensity;
170 
171  infile >> nIndex;
172  infile >> pos.fPhi;
173  infile >> pos.fTheta;
174  infile >> fIntensity;
175 
176  pNewNode->setPosition(pos);
177  pNewNode->setIntensity(fIntensity);
178 
179  int nTest = addNode(pNewNode);
180 
181  if (nTest != nIndex)
182  {
183  printf("Error input file inconsistent: %d %d\n", nTest, nIndex);
184  return false;
185  }
186 
187  return true;
188 }
189 
190 bool CIntensityGraph::readEdge(std::istream& infile)
191 {
192  int nIndex1, nIndex2, nLeftFace, nRightFace;
193 
194  infile >> nIndex1;
195  infile >> nIndex2;
196 
197  infile >> nLeftFace;
198  infile >> nRightFace;
199 
200  addEdge(nIndex1, nIndex2, nLeftFace, nRightFace);
201  return true;
202 }
203 
204 
205 bool CIntensityGraph::write(std::ostream& outfile)
206 {
207  try
208  {
209  int nNumberNodes = int(m_Nodes.size());
210  // write number of nodes
211  outfile << nNumberNodes << std::endl;
212 
213  // write all nodes
214  for (int n = 0 ; n < nNumberNodes ; n++)
215  {
216  writeNode(outfile, n);
217  }
218 
219  // write number of edges
220  int nNumberEdges = int(m_Edges.size());
221  outfile << nNumberEdges << std::endl;
222 
223  for (int e = 0 ; e < nNumberEdges ; e++)
224  {
225  writeEdge(outfile, e);
226  }
227 
228  }
229  catch (std::ostream::failure&)
230  {
231  printf("ERROR: failed to write FeatureGraph to file\n");
232  return false;
233  }
234 
235  return true;
236 }
237 
238 bool CIntensityGraph::writeNode(std::ostream& outfile, int n)
239 {
240  CIntensityNode* pCurrentNode = (CIntensityNode*) m_Nodes.at(n);
241 
242  outfile << pCurrentNode->getIndex() << std::endl;
243  outfile << pCurrentNode->getPosition().fPhi << std::endl;
244  outfile << pCurrentNode->getPosition().fTheta << std::endl;
245  outfile << pCurrentNode->getIntensity() << std::endl;
246 
247  outfile << std::endl;
248  return true;
249 }
250 
251 bool CIntensityGraph::writeEdge(std::ostream& outfile, int e)
252 {
253  CSGEdge* pCurrentEdge = m_Edges.at(e);
254  outfile << pCurrentEdge->nIndex1 << " ";
255  outfile << pCurrentEdge->nIndex2 << " ";
256 
257  outfile << pCurrentEdge->nLeftFace << " ";
258  outfile << pCurrentEdge->nRightFace << " ";
259 
260  outfile << std::endl;
261  return true;
262 }
263 
264 void CIntensityGraph::graphToVec(std::vector<float>& vec)
265 {
266  TNodeList* nodes = this->getNodes();
267 
268  for (auto& i : *nodes)
269  {
270  CIntensityNode* node = (CIntensityNode*) i;
271  vec.push_back(node->getIntensity());
272  }
273 }
TNodeList
std::vector< CSGNode * > TNodeList
Definition: SphericalGraph.h:88
CSGNode::getIndex
int getIndex()
Definition: SphericalGraph.h:62
CIntensityGraph::operator=
CIntensityGraph & operator=(CIntensityGraph const &rhs)
Definition: IntensityGraph.cpp:101
CIntensityGraph::reset
void reset()
Definition: IntensityGraph.cpp:110
CIntensityNode
Definition: IntensityGraph.h:25
TSphereCoord::fTheta
float fTheta
Definition: Structs.h:26
CSGEdge::nLeftFace
int nLeftFace
Definition: SphericalGraph.h:39
CIntensityGraph::set
void set(float fIntensity)
Definition: IntensityGraph.cpp:115
CSphericalGraph::addNode
int addNode(CSGNode *pNode)
Definition: SphericalGraph.cpp:96
CIntensityGraph::writeNode
bool writeNode(std::ostream &outfile, int n) override
Definition: IntensityGraph.cpp:238
CSGNode::getPosition
TSphereCoord getPosition()
Definition: SphericalGraph.h:53
CIntensityNode::CIntensityNode
CIntensityNode()
Definition: IntensityGraph.cpp:24
CSphericalGraph::addEdge
int addEdge(int nIndex1, int nIndex2)
Definition: SphericalGraph.cpp:105
CIntensityGraph::CIntensityGraph
CIntensityGraph()
Definition: IntensityGraph.cpp:58
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
IntensityGraph.h
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
CIntensityGraph
Definition: IntensityGraph.h:51
CIntensityGraph::writeEdge
bool writeEdge(std::ostream &outfile, int e) override
Definition: IntensityGraph.cpp:251
CIntensityGraph::read
bool read(std::istream &infile) override
Definition: IntensityGraph.cpp:129
CIntensityGraph::write
bool write(std::ostream &outfile) override
Definition: IntensityGraph.cpp:205
CIntensityNode::getIntensity
float getIntensity()
Definition: IntensityGraph.cpp:47
CIntensityGraph::readEdge
bool readEdge(std::istream &infile) override
Definition: IntensityGraph.cpp:190
CIntensityNode::~CIntensityNode
~CIntensityNode() override
CIntensityNode::setIntensity
void setIntensity(float fIntensity)
Definition: IntensityGraph.cpp:42
CIntensityGraph::~CIntensityGraph
~CIntensityGraph() override
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
CSphericalGraph
Definition: SphericalGraph.h:93
TSphereCoord
Definition: Structs.h:22
CIntensityGraph::readNode
bool readNode(std::istream &infile) override
Definition: IntensityGraph.cpp:163
CIntensityGraph::getNewNode
CIntensityNode * getNewNode() override
Definition: IntensityGraph.h:79
CIntensityGraph::graphToVec
void graphToVec(std::vector< float > &vec)
Definition: IntensityGraph.cpp:264