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