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// *****************************************************************
28
30
31 = default;
32
37
39
40void
42{
43 m_fIntensity = fIntensity;
44}
45
46float
48{
49 return m_fIntensity;
50}
51
52// *****************************************************************
53// implementation of CIntensityGraph
54// *****************************************************************
55// *****************************************************************
56// construction / destruction
57// *****************************************************************
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
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// *****************************************************************
110void
112{
113 set(0.0f);
114}
115
116void
117CIntensityGraph::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// *****************************************************************
130bool
131CIntensityGraph::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
164bool
165CIntensityGraph::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
192bool
193CIntensityGraph::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
207bool
208CIntensityGraph::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
240bool
241CIntensityGraph::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
254bool
255CIntensityGraph::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
268void
269CIntensityGraph::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}
std::vector< CSGNode * > TNodeList
bool readNode(std::istream &infile) override
bool read(std::istream &infile) override
~CIntensityGraph() override
bool write(std::ostream &outfile) override
void set(float fIntensity)
CIntensityGraph & operator=(CIntensityGraph const &rhs)
CIntensityNode * getNewNode() override
void graphToVec(std::vector< float > &vec)
bool writeNode(std::ostream &outfile, int n) override
bool readEdge(std::istream &infile) override
bool writeEdge(std::ostream &outfile, int e) override
void setIntensity(float fIntensity)
~CIntensityNode() override
int getIndex()
TSphereCoord getPosition()
void setPosition(TSphereCoord position)
TNodeList * getNodes()
int addEdge(int nIndex1, int nIndex2)
int addNode(CSGNode *pNode)
float fTheta
Definition Structs.h:27
float fPhi
Definition Structs.h:26