GraphMap.cpp
Go to the documentation of this file.
1// *****************************************************************
2// Filename: GraphMap.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: 9.08.2007
8// *****************************************************************
9
10// *****************************************************************
11// includes
12// *****************************************************************
13#include "GraphMap.h"
14
15#include <list>
16
17#include "Base/DataStructures/Graph/GraphProcessor.h"
18#include "Base/Math/MathTools.h"
19#include "Helpers/helpers.h"
20
21using namespace std;
22
23// *****************************************************************
24// implementation of CGraphMap
25// *****************************************************************
26// construction / destruction
27CGraphMap::CGraphMap(CIntensityGraph* pGraph, bool bUseLookup)
28{
29 m_pGraph = pGraph;
30 m_nNumberNodes = pGraph->getNodes()->size();
31
32 m_nWidth = (int)sqrt(float(m_nNumberNodes));
33 m_nHeight = m_nNumberNodes / m_nWidth + 1;
34
35 m_bUseLookup = bUseLookup;
36
37 // init lookup table
38 if (m_bUseLookup)
39 {
40 m_pLookupTable = new CGraphPyramidLookupTable(100, 100);
41 m_pLookupTable->buildLookupTable(pGraph);
42 }
43
44 // create float matrix
45 m_pMap = new CFloatMatrix(m_nWidth, m_nHeight);
46 m_bOwnMemory = true;
47 toMap();
48}
49
51{
52 if (m_bOwnMemory)
53 {
54 delete m_pMap;
55 }
56
57 if (m_bUseLookup)
58 {
59 delete m_pLookupTable;
60 }
61}
62
63void
64CGraphMap::setMap(CFloatMatrix* pMap, bool bUpdateGraph)
65{
66 if (m_bOwnMemory)
67 {
68 delete m_pMap;
69 }
70
71 m_bOwnMemory = false;
72
73 m_pMap = pMap;
74 m_nWidth = pMap->columns;
75 m_nHeight = pMap->rows;
76
77 if (bUpdateGraph)
78 {
79 toGraph();
80 }
81}
82
83CFloatMatrix*
85{
86 return m_pMap;
87}
88
89int
91{
92 return m_nWidth;
93}
94
95int
97{
98 return m_nHeight;
99}
100
101// manipulation
102void
103CGraphMap::applyGaussian(EOperation nOperation, Vec3d position, float fAmplitude, float fVariance)
104{
105 TSphereCoord coord;
106 MathTools::convert(position, coord);
107
108 applyGaussian(nOperation, coord, fAmplitude, fVariance);
109}
110
111void
113 TSphereCoord coord,
114 float fAmplitude,
115 float fVariance)
116{
117 int nNodeID = 0;
118
119 if (m_bUseLookup)
120 {
121 nNodeID = m_pLookupTable->getClosestNode(coord);
122 }
123 else
124 {
125 nNodeID = GraphProcessor::findClosestNode(m_pGraph, coord);
126 }
127
128 applyGaussian(nOperation, nNodeID, fAmplitude, fVariance);
129}
130
131void
132CGraphMap::applyGaussian(EOperation nOperation, int nNodeID, float fAmplitude, float fVariance)
133{
134 list<int> nodes;
135 vector<int> accomplished;
136
137 // store initial node
138 int nFirstNode = nNodeID;
139 TSphereCoord coord = m_pGraph->getNodes()->at(nFirstNode)->getPosition();
140 nodes.push_back(nFirstNode);
141
142 // eat nodelist
143 TSphereCoord cur_sc;
144 Vec3d ref, cur;
145 MathTools::convert(coord, ref);
146 float fIntensity;
147
148 while (nodes.size() > 0)
149 {
150 // retrieve first node
151 int nCurrentNode = nodes.front();
152 nodes.pop_front();
153 accomplished.push_back(nCurrentNode);
154
155 // calculate angle
156 cur_sc = m_pGraph->getNodes()->at(nCurrentNode)->getPosition();
157 MathTools::convert(cur_sc, cur);
158 float fAngle = Math3d::Angle(ref, cur);
159
160 // check if maximum angle
161 if (fAngle > 2.0 * fVariance)
162 {
163 continue;
164 }
165
166 // evaluate gaussian at angle
167 fIntensity = evaluateGaussian(fAngle, fAmplitude, fVariance);
168
169 // update graph and map
170 switch (nOperation)
171 {
172 case eSet:
173 m_pMap->data[nCurrentNode] = fIntensity;
174 break;
175
176 case eAdd:
177 m_pMap->data[nCurrentNode] += fIntensity;
178 break;
179
180 case eMultiply:
181 m_pMap->data[nCurrentNode] *= fIntensity;
182 break;
183
184 case eMax:
185 if (m_pMap->data[nCurrentNode] < fIntensity)
186 {
187 m_pMap->data[nCurrentNode] = fIntensity;
188 }
189
190 break;
191
192 default:
193 printf("Illegal operation\n");
194 break;
195 }
196
197 // add neighbours to nodelist
198 vector<int>* pAdjacency = m_pGraph->getNodeAdjacency(nCurrentNode);
199
200 // check for double nodes
201 int nAdjNode;
202
203 for (int a = 0; a < int(pAdjacency->size()); a++)
204 {
205 nAdjNode = pAdjacency->at(a);
206
207 // check if we already processed this node
208 bool bNewNode = true;
209
210 for (int c = 0; c < int(accomplished.size()); c++)
211 if (accomplished.at(c) == nAdjNode)
212 {
213 bNewNode = false;
214 }
215
216 list<int>::iterator iter = nodes.begin();
217
218 while (iter != nodes.end())
219 {
220 if (*iter == nAdjNode)
221 {
222 bNewNode = false;
223 }
224
225 iter++;
226 }
227
228
229 if (bNewNode)
230 {
231 nodes.push_back(nAdjNode);
232 }
233 }
234 }
235}
236
237void
239{
240 toGraph();
241}
242
243void
245{
246 toMap();
247}
248
249float
250CGraphMap::evaluateGaussian(float fDist, float fAmplitude, float fVariance)
251{
252 return exp(-(fDist / fVariance * 2.0f)) * fAmplitude;
253}
254
255void
256CGraphMap::toGraph()
257{
258 float fIntensity;
259
260 for (int n = 0; n < m_nNumberNodes; n++)
261 {
262 fIntensity = m_pMap->data[n];
263 ((CIntensityNode*)m_pGraph->getNodes()->at(n))->setIntensity(fIntensity);
264 }
265}
266
267void
268CGraphMap::toMap()
269{
270 float fIntensity;
271
272 for (int n = 0; n < m_nNumberNodes; n++)
273 {
274 fIntensity = ((CIntensityNode*)m_pGraph->getNodes()->at(n))->getIntensity();
275 m_pMap->data[n] = fIntensity;
276 }
277}
EOperation
Definition GraphMap.h:23
@ eMultiply
Definition GraphMap.h:26
@ eSet
Definition GraphMap.h:24
@ eAdd
Definition GraphMap.h:25
@ eMax
Definition GraphMap.h:27
constexpr T c
void applyGaussian(EOperation nOperation, Vec3d position, float fAmplitude, float fVariance)
Definition GraphMap.cpp:103
CFloatMatrix * getMap()
Definition GraphMap.cpp:84
int getHeight()
Definition GraphMap.cpp:96
void setMap(CFloatMatrix *pMap, bool bUpdateGraph=true)
Definition GraphMap.cpp:64
CGraphMap(CIntensityGraph *pGraph, bool bUseLookup=false)
Definition GraphMap.cpp:27
int getWidth()
Definition GraphMap.cpp:90
void updateGraph()
Definition GraphMap.cpp:238
void updateMap()
Definition GraphMap.cpp:244
TNodeList * getNodes()
static void convert(TSphereCoord in, Eigen::Vector3d &out)
int findClosestNode(CSphericalGraph *pGraph, TSphereCoord coord)
constexpr auto n() noexcept