GraphLookupTable.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: GraphLookupTable.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: 10.10.2008
8 // *****************************************************************
9 
10 // *****************************************************************
11 // includes
12 // *****************************************************************
13 #include "GraphLookupTable.h"
14 
15 #include <cfloat>
16 
17 #include "MathTools.h"
18 
19 // *****************************************************************
20 // implementation of CGraphLookupTable
21 // *****************************************************************
22 // construction / destruction
23 CGraphLookupTable::CGraphLookupTable(int nMaxZenithBins, int nMaxAzimuthBins)
24 {
25  m_nMaxZenithBins = nMaxZenithBins;
26  m_nMaxAzimuthBins = nMaxAzimuthBins;
27 
28  buildMemory();
29 }
30 
32 {
33  cleanMemory();
34 }
35 
36 void
38 {
39  reset();
40 
41  TNodeList* pNodes = pGraph->getNodes();
42  int nSize = pNodes->size();
43 
44  for (int n = 0; n < nSize; n++)
45  {
46  CSGNode* pNode = pNodes->at(n);
47  addEntry(pNode->getPosition(), n);
48  }
49 
50  m_pGraph = pGraph;
51 }
52 
53 int
55 {
56  bool bExact;
57  return getClosestNode(position, bExact);
58 }
59 
60 int
61 CGraphLookupTable::getClosestNode(Eigen::Vector3d position)
62 {
63  bool bExact;
64  return getClosestNode(position, bExact);
65 }
66 
67 int
68 CGraphLookupTable::getClosestNode(Eigen::Vector3d position, bool& bExact)
69 {
70  TSphereCoord coords;
71  MathTools::convert(position, coords);
72 
73  return getClosestNode(coords, bExact);
74 }
75 
76 int
78 {
79  int nIndex1, nIndex2;
80  nIndex1 = getZenithBinIndex(position.fPhi);
81  nIndex2 = getAzimuthBinIndex(position.fTheta, position.fPhi);
82 
83  std::list<int>::iterator iter = m_ppTable[nIndex1][nIndex2].begin();
84 
85  float fMinDistance = FLT_MAX;
86  int nBestIndex = -1;
87  float fDistance;
88 
89  while (iter != m_ppTable[nIndex1][nIndex2].end())
90  {
91  fDistance = MathTools::getDistanceOnArc(position, m_pGraph->getNode(*iter)->getPosition());
92 
93  if (fDistance < fMinDistance)
94  {
95  fMinDistance = fDistance;
96  nBestIndex = *iter;
97  }
98 
99  iter++;
100  }
101 
102  // retrieve distance to bounding
103  float fMinPhi, fMaxPhi, fMinTheta, fMaxTheta;
104 
105  float fTestValues[4];
106  // printf("Current position: phi %f, theta %f\n", position.fPhi, position.fTheta);
107 
108  getBinMinMaxValues(nIndex1, nIndex2, fMinPhi, fMaxPhi, fMinTheta, fMaxTheta);
109  // printf("B Phi: %f - %f\n", fMinPhi, fMaxPhi);
110  // printf("B Theta: %f - %f\n", fMinTheta, fMaxTheta);
111 
112  fTestValues[0] = std::fabs(fMinPhi - position.fPhi) * M_PI / 180.0f;
113  fTestValues[1] = std::fabs(fMaxPhi - position.fPhi) * M_PI / 180.0f;
114  fTestValues[2] = std::fabs(fMinTheta - position.fTheta) * M_PI / 180.0f;
115  fTestValues[3] = std::fabs(fMaxTheta - position.fTheta) * M_PI / 180.0f;
116  bExact = true;
117 
118  // printf("AzimuthDistance (min,max,current): (%f,%f,%f)\n", fTestValues[2], fTestValues[3], fMinDistance);
119  // printf("ZenithDistance (min,max,current): (%f,%f,%f)\n", fTestValues[0], fTestValues[1], fMinDistance);
120 
121  for (float fTestValue : fTestValues)
122  {
123  if (fTestValue < fMinDistance)
124  {
125  bExact = false;
126  }
127  }
128 
129  return nBestIndex;
130 }
131 
132 void
133 CGraphLookupTable::reset()
134 {
135  float fZenith;
136 
137  for (int z = 0; z < getNumberZenithBins(); z++)
138  {
139  fZenith = 180.0f / m_nMaxZenithBins * z;
140 
141  for (int a = 0; a < getNumberAzimuthBins(fZenith); a++)
142  {
143  m_ppTable[z][a].clear();
144  }
145  }
146 }
147 
148 // *****************************************************************
149 // private methods
150 // *****************************************************************
151 void
152 CGraphLookupTable::addEntry(TSphereCoord position, int nIndex)
153 {
154  int nIndex1, nIndex2;
155  nIndex1 = getZenithBinIndex(position.fPhi);
156  nIndex2 = getAzimuthBinIndex(position.fTheta, position.fPhi);
157 
158  m_ppTable[nIndex1][nIndex2].push_back(nIndex);
159 }
160 
161 void
162 CGraphLookupTable::buildMemory()
163 {
164  float fZenith;
165 
166  m_ppTable = new std::list<int>*[m_nMaxZenithBins];
167 
168  for (int z = 0; z < m_nMaxZenithBins; z++)
169  {
170  fZenith = 180.0f / m_nMaxZenithBins * z;
171  m_ppTable[z] = new std::list<int>[getNumberAzimuthBins(fZenith)];
172  }
173 }
174 
175 void
176 CGraphLookupTable::cleanMemory()
177 {
178  for (int z = 0; z < m_nMaxZenithBins; z++)
179  {
180  delete[] m_ppTable[z];
181  }
182 
183  delete[] m_ppTable;
184 }
185 
186 int
187 CGraphLookupTable::getNumberZenithBins()
188 {
189  return m_nMaxZenithBins;
190 }
191 
192 int
193 CGraphLookupTable::getNumberAzimuthBins(float fZenith)
194 {
195  int nZenithBin = getZenithBinIndex(fZenith);
196 
197  // 0 degree means 1 min bins
198  // 90 degree means max bins
199  int nMinBins = 1;
200  float fNumberBins;
201 
202  if (getNumberZenithBins() > 1)
203  {
204  fNumberBins = sin(float(nZenithBin) / (getNumberZenithBins() - 1) * M_PI) *
205  (m_nMaxAzimuthBins - nMinBins) +
206  nMinBins;
207  }
208  else
209  {
210  fNumberBins = 0.5f;
211  }
212 
213  return int(fNumberBins + 0.5f);
214 }
215 
216 int
217 CGraphLookupTable::getAzimuthBinIndex(float fAzimuth, float fZenith)
218 {
219  float fBinIndex = fAzimuth / 360.0f * (getNumberAzimuthBins(fZenith) - 1) + 0.5f;
220  return int(fBinIndex);
221 }
222 
223 int
224 CGraphLookupTable::getZenithBinIndex(float fZenith)
225 {
226  float fBinIndex = fZenith / 180.0f * (m_nMaxZenithBins - 1) + 0.5f;
227  return int(fBinIndex);
228 }
229 
230 void
231 CGraphLookupTable::getBinMinMaxValues(int nZenithBinIndex,
232  int nAzimuthBinIndex,
233  float& fMinZenith,
234  float& fMaxZenith,
235  float& fMinAzimuth,
236  float& fMaxAzimuth)
237 {
238  if ((m_nMaxZenithBins - 1) == 0)
239  {
240  fMinZenith = -FLT_MAX;
241  fMaxZenith = FLT_MAX;
242  }
243  else
244  {
245  fMinZenith = (nZenithBinIndex - 0.5f) * 180.0f / (m_nMaxZenithBins - 1);
246  fMaxZenith = (nZenithBinIndex + 0.5f) * 180.0f / (m_nMaxZenithBins - 1);
247  }
248 
249  if ((getNumberAzimuthBins((fMinZenith + fMaxZenith) / 2.0f) - 1) == 0)
250  {
251  fMinAzimuth = -FLT_MAX;
252  fMaxAzimuth = FLT_MAX;
253  }
254  else
255  {
256  fMinAzimuth = (nAzimuthBinIndex - 0.5f) * 360.0f /
257  (getNumberAzimuthBins((fMinZenith + fMaxZenith) / 2.0f) - 1);
258  fMaxAzimuth = (nAzimuthBinIndex + 0.5f) * 360.0f /
259  (getNumberAzimuthBins((fMinZenith + fMaxZenith) / 2.0f) - 1);
260  }
261 }
TNodeList
std::vector< CSGNode * > TNodeList
Definition: SphericalGraph.h:100
TSphereCoord::fTheta
float fTheta
Definition: Structs.h:27
CGraphLookupTable::~CGraphLookupTable
~CGraphLookupTable()
Definition: GraphLookupTable.cpp:31
CSGNode::getPosition
TSphereCoord getPosition()
Definition: SphericalGraph.h:60
GraphLookupTable.h
CSGNode
Definition: SphericalGraph.h:43
TSphereCoord::fPhi
float fPhi
Definition: Structs.h:26
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
M_PI
#define M_PI
Definition: MathTools.h:17
MathTools.h
CSphericalGraph::getNodes
TNodeList * getNodes()
Definition: SphericalGraph.cpp:170
CGraphLookupTable::CGraphLookupTable
CGraphLookupTable(int nMaxZenithBins, int nMaxAzimuthBins)
Definition: GraphLookupTable.cpp:23
MathTools::getDistanceOnArc
static float getDistanceOnArc(TSphereCoord p1, TSphereCoord p2)
Definition: MathTools.cpp:302
CSphericalGraph
Definition: SphericalGraph.h:105
TSphereCoord
Definition: Structs.h:23
MathTools::convert
static void convert(TSphereCoord in, Eigen::Vector3d &out)
Definition: MathTools.cpp:440
CSphericalGraph::getNode
CSGNode * getNode(int nIndex)
Definition: SphericalGraph.cpp:182
CGraphLookupTable::getClosestNode
int getClosestNode(Eigen::Vector3d position)
Definition: GraphLookupTable.cpp:61
CGraphLookupTable::buildLookupTable
void buildLookupTable(CSphericalGraph *pGraph)
Definition: GraphLookupTable.cpp:37