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