69 m_nKdTreeMaxLeaves = -1;
99 m_nDimension = nDimension;
100 m_nDataSets = nDataSets;
108 m_pData =
new float[nDimension * nDataSets];
110 memcpy(m_pData, pData, nDimension * nDataSets *
sizeof(
float));
114 const int nOverallDimension = nDimension + 2;
117 float **ppValues =
new float*[nDataSets];
120 for (i = 0; i < nDataSets; i++)
122 ppValues[i] =
new float[nOverallDimension];
125 memcpy(ppValues[i], pData + i * nDimension, nDimension *
sizeof(
float));
128 memcpy(&ppValues[i][nDimension], &i,
sizeof(
int));
135 m_pKdTree->
Build(ppValues, 0, nDataSets - 1, 3, nDimension, 2);
137 for (i = 0; i < nDataSets; i++)
138 delete [] ppValues[i];
158 printf(
"error: classifier not trained in CNearestNeighbor::Classify\n");
162 if (m_nDimension != nDimension)
164 printf(
"error: query dimension and trained dimension do not match in CNearestNeighbor::Classify\n");
170 const float *pData = m_pData;
175 for (
int i = 0; i < m_nDataSets; i++)
179 for (
int j = 0; j < m_nDimension; j++)
181 register float v = pQuery[j] - pData[j];
191 pData += m_nDimension;
204 memcpy(&nResultIndex, pData + m_nDimension,
sizeof(
int));
206 fResultError = error;
225 printf(
"error: classifier not trained in CNearestNeighbor::Classify\n");
229 if (m_nDimension != nDimension)
231 printf(
"error: query dimension and trained dimension do not match in CNearestNeighbor::Classify\n");
237 const float *pQuery = pQueries;
239 for (
int k = 0; k < nQueries; k++)
241 const float *pData = m_pData;
246 for (
int i = 0; i < m_nDataSets; i++)
250 for (
int j = 0; j < m_nDimension; j++)
252 register float v = pQuery[j] - pData[j];
262 pData += m_nDimension;
265 pResults[k] = best_i;
266 pResultErrors[k] = min;
268 pQuery += m_nDimension;
275 const float *pQuery = pQueries;
277 for (
int k = 0; k < nQueries; k++)
282 memcpy(pResults + k, pData + m_nDimension,
sizeof(
int));
283 pResultErrors[k] = error;
285 pQuery += m_nDimension;
#define OPTIMIZED_FUNCTION_HEADER_5(name, p1, p2, p3, p4, p5)
#define OPTIMIZED_FUNCTION_HEADER_3(name, p1, p2, p3)
void NearestNeighborBBF(const float *pfQuery, float &fError, float *&pfNN, int nMaximumLeavesToVisit=-1)
#define OPTIMIZED_FUNCTION_HEADER_4(name, p1, p2, p3, p4)
void Build(float **ppfValues, int nLow, int nHigh, int nBucketSize, int nDimensions, int nUserDataSize)
bool Train(const float *pData, int nDimension, int nDataSets)
int Classify(const float *pQuery, int nDimension, float &fResultError)
#define OPTIMIZED_FUNCTION_FOOTER
CNearestNeighbor(ComputationMethod method)
#define OPTIMIZED_FUNCTION_HEADER_0(name)