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 {
35  setIntensity(0.0f);
36 }
37 
39 
40 void
42 {
43  m_fIntensity = fIntensity;
44 }
45 
46 float
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 
102 {
103  *((CSphericalGraph*)this) = (const CSphericalGraph)rhs;
104  return *this;
105 }
106 
107 // *****************************************************************
108 // setting
109 // *****************************************************************
110 void
112 {
113  set(0.0f);
114 }
115 
116 void
117 CIntensityGraph::set(float fIntensity)
118 {
119  TNodeList* pNodes = getNodes();
120 
121  for (auto& pNode : *pNodes)
122  {
123  ((CIntensityNode*)pNode)->setIntensity(fIntensity);
124  }
125 }
126 
127 // *****************************************************************
128 // file io
129 // *****************************************************************
130 bool
131 CIntensityGraph::read(std::istream& infile)
132 {
133  try
134  {
135  int nNumberNodes;
136 
137  // read number of nodes
138  infile >> nNumberNodes;
139 
140  // read all nodes
141  for (int n = 0; n < nNumberNodes; n++)
142  {
143  readNode(infile);
144  }
145 
146  // read number of edges
147  int nNumberEdges;
148  infile >> nNumberEdges;
149 
150  for (int e = 0; e < nNumberEdges; e++)
151  {
152  readEdge(infile);
153  }
154  }
155  catch (std::istream::failure&)
156  {
157  printf("ERROR: failed to write FeatureGraph to file\n");
158  return false;
159  }
160 
161  return true;
162 }
163 
164 bool
165 CIntensityGraph::readNode(std::istream& infile)
166 {
167  CIntensityNode* pNewNode = getNewNode();
168 
169  int nIndex;
170  TSphereCoord pos;
171  float fIntensity;
172 
173  infile >> nIndex;
174  infile >> pos.fPhi;
175  infile >> pos.fTheta;
176  infile >> fIntensity;
177 
178  pNewNode->setPosition(pos);
179  pNewNode->setIntensity(fIntensity);
180 
181  int nTest = addNode(pNewNode);
182 
183  if (nTest != nIndex)
184  {
185  printf("Error input file inconsistent: %d %d\n", nTest, nIndex);
186  return false;
187  }
188 
189  return true;
190 }
191 
192 bool
193 CIntensityGraph::readEdge(std::istream& infile)
194 {
195  int nIndex1, nIndex2, nLeftFace, nRightFace;
196 
197  infile >> nIndex1;
198  infile >> nIndex2;
199 
200  infile >> nLeftFace;
201  infile >> nRightFace;
202 
203  addEdge(nIndex1, nIndex2, nLeftFace, nRightFace);
204  return true;
205 }
206 
207 bool
208 CIntensityGraph::write(std::ostream& outfile)
209 {
210  try
211  {
212  int nNumberNodes = int(m_Nodes.size());
213  // write number of nodes
214  outfile << nNumberNodes << std::endl;
215 
216  // write all nodes
217  for (int n = 0; n < nNumberNodes; n++)
218  {
219  writeNode(outfile, n);
220  }
221 
222  // write number of edges
223  int nNumberEdges = int(m_Edges.size());
224  outfile << nNumberEdges << std::endl;
225 
226  for (int e = 0; e < nNumberEdges; e++)
227  {
228  writeEdge(outfile, e);
229  }
230  }
231  catch (std::ostream::failure&)
232  {
233  printf("ERROR: failed to write FeatureGraph to file\n");
234  return false;
235  }
236 
237  return true;
238 }
239 
240 bool
241 CIntensityGraph::writeNode(std::ostream& outfile, int n)
242 {
243  CIntensityNode* pCurrentNode = (CIntensityNode*)m_Nodes.at(n);
244 
245  outfile << pCurrentNode->getIndex() << std::endl;
246  outfile << pCurrentNode->getPosition().fPhi << std::endl;
247  outfile << pCurrentNode->getPosition().fTheta << std::endl;
248  outfile << pCurrentNode->getIntensity() << std::endl;
249 
250  outfile << std::endl;
251  return true;
252 }
253 
254 bool
255 CIntensityGraph::writeEdge(std::ostream& outfile, int e)
256 {
257  CSGEdge* pCurrentEdge = m_Edges.at(e);
258  outfile << pCurrentEdge->nIndex1 << " ";
259  outfile << pCurrentEdge->nIndex2 << " ";
260 
261  outfile << pCurrentEdge->nLeftFace << " ";
262  outfile << pCurrentEdge->nRightFace << " ";
263 
264  outfile << std::endl;
265  return true;
266 }
267 
268 void
269 CIntensityGraph::graphToVec(std::vector<float>& vec)
270 {
271  TNodeList* nodes = this->getNodes();
272 
273  for (auto& i : *nodes)
274  {
275  CIntensityNode* node = (CIntensityNode*)i;
276  vec.push_back(node->getIntensity());
277  }
278 }
TNodeList
std::vector< CSGNode * > TNodeList
Definition: SphericalGraph.h:100
CSGNode::getIndex
int getIndex()
Definition: SphericalGraph.h:72
CIntensityGraph::operator=
CIntensityGraph & operator=(CIntensityGraph const &rhs)
Definition: IntensityGraph.cpp:101
CIntensityGraph::reset
void reset()
Definition: IntensityGraph.cpp:111
CIntensityNode
Definition: IntensityGraph.h:25
TSphereCoord::fTheta
float fTheta
Definition: Structs.h:27
CSGEdge::nLeftFace
int nLeftFace
Definition: SphericalGraph.h:39
CIntensityGraph::set
void set(float fIntensity)
Definition: IntensityGraph.cpp:117
CSphericalGraph::addNode
int addNode(CSGNode *pNode)
Definition: SphericalGraph.cpp:99
CIntensityGraph::writeNode
bool writeNode(std::ostream &outfile, int n) override
Definition: IntensityGraph.cpp:241
CSGNode::getPosition
TSphereCoord getPosition()
Definition: SphericalGraph.h:60
CIntensityNode::CIntensityNode
CIntensityNode()
Definition: IntensityGraph.cpp:24
CSphericalGraph::addEdge
int addEdge(int nIndex1, int nIndex2)
Definition: SphericalGraph.cpp:109
CIntensityGraph::CIntensityGraph
CIntensityGraph()
Definition: IntensityGraph.cpp:58
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
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:26
CSphericalGraph::m_Nodes
TNodeList m_Nodes
Definition: SphericalGraph.h:167
CIntensityGraph
Definition: IntensityGraph.h:51
CIntensityGraph::writeEdge
bool writeEdge(std::ostream &outfile, int e) override
Definition: IntensityGraph.cpp:255
CIntensityGraph::read
bool read(std::istream &infile) override
Definition: IntensityGraph.cpp:131
CIntensityGraph::write
bool write(std::ostream &outfile) override
Definition: IntensityGraph.cpp:208
CIntensityNode::getIntensity
float getIntensity()
Definition: IntensityGraph.cpp:47
CIntensityGraph::readEdge
bool readEdge(std::istream &infile) override
Definition: IntensityGraph.cpp:193
CIntensityNode::~CIntensityNode
~CIntensityNode() override
CIntensityNode::setIntensity
void setIntensity(float fIntensity)
Definition: IntensityGraph.cpp:41
CIntensityGraph::~CIntensityGraph
~CIntensityGraph() override
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
CSphericalGraph
Definition: SphericalGraph.h:105
TSphereCoord
Definition: Structs.h:23
CIntensityGraph::readNode
bool readNode(std::istream &infile) override
Definition: IntensityGraph.cpp:165
CIntensityGraph::getNewNode
CIntensityNode * getNewNode() override
Definition: IntensityGraph.h:79
CIntensityGraph::graphToVec
void graphToVec(std::vector< float > &vec)
Definition: IntensityGraph.cpp:269