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
23CGraphLookupTable::CGraphLookupTable(int nMaxZenithBins, int nMaxAzimuthBins)
24{
25 m_nMaxZenithBins = nMaxZenithBins;
26 m_nMaxAzimuthBins = nMaxAzimuthBins;
27
28 buildMemory();
29}
30
32{
33 cleanMemory();
34}
35
36void
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
53int
55{
56 bool bExact;
57 return getClosestNode(position, bExact);
58}
59
60int
61CGraphLookupTable::getClosestNode(Eigen::Vector3d position)
62{
63 bool bExact;
64 return getClosestNode(position, bExact);
65}
66
67int
68CGraphLookupTable::getClosestNode(Eigen::Vector3d position, bool& bExact)
69{
70 TSphereCoord coords;
71 MathTools::convert(position, coords);
72
73 return getClosestNode(coords, bExact);
74}
75
76int
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
132void
133CGraphLookupTable::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// *****************************************************************
151void
152CGraphLookupTable::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
161void
162CGraphLookupTable::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
175void
176CGraphLookupTable::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
186int
187CGraphLookupTable::getNumberZenithBins()
188{
189 return m_nMaxZenithBins;
190}
191
192int
193CGraphLookupTable::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
216int
217CGraphLookupTable::getAzimuthBinIndex(float fAzimuth, float fZenith)
218{
219 float fBinIndex = fAzimuth / 360.0f * (getNumberAzimuthBins(fZenith) - 1) + 0.5f;
220 return int(fBinIndex);
221}
222
223int
224CGraphLookupTable::getZenithBinIndex(float fZenith)
225{
226 float fBinIndex = fZenith / 180.0f * (m_nMaxZenithBins - 1) + 0.5f;
227 return int(fBinIndex);
228}
229
230void
231CGraphLookupTable::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}
#define M_PI
Definition MathTools.h:17
std::vector< CSGNode * > TNodeList
CGraphLookupTable(int nMaxZenithBins, int nMaxAzimuthBins)
int getClosestNode(Eigen::Vector3d position)
void buildLookupTable(CSphericalGraph *pGraph)
TSphereCoord getPosition()
TNodeList * getNodes()
static float getDistanceOnArc(TSphereCoord p1, TSphereCoord p2)
static void convert(TSphereCoord in, Eigen::Vector3d &out)
float fTheta
Definition Structs.h:27
float fPhi
Definition Structs.h:26