1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2006-2008 Christian Gehl
* Copyright (C) 2006-2008 Fraunhofer Institute FIRST
*/
#include "lib/config.h"
#include "lib/common.h"
#include "lib/io.h"
#include "distance/GeodesicMetric.h"
#include "features/Features.h"
#include "features/RealFeatures.h"
CGeodesicMetric::CGeodesicMetric()
: CSimpleDistance<DREAL>()
{
}
CGeodesicMetric::CGeodesicMetric(CRealFeatures* l, CRealFeatures* r)
: CSimpleDistance<DREAL>()
{
init(l, r);
}
CGeodesicMetric::~CGeodesicMetric()
{
cleanup();
}
bool CGeodesicMetric::init(CFeatures* l, CFeatures* r)
{
bool result=CSimpleDistance<DREAL>::init(l,r);
return result;
}
void CGeodesicMetric::cleanup()
{
}
bool CGeodesicMetric::load_init(FILE* src)
{
return false;
}
bool CGeodesicMetric::save_init(FILE* dest)
{
return false;
}
DREAL CGeodesicMetric::compute(INT idx_a, INT idx_b)
{
INT alen, blen;
bool afree, bfree;
double* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
double* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
ASSERT(alen==blen);
DREAL s=0;
DREAL d=0;
DREAL nx=0;
DREAL ny=0;
{
for (INT i=0; i<alen; i++)
{
d+=avec[i]*bvec[i];
nx+=avec[i]*avec[i];
ny+=bvec[i]*bvec[i];
s+=avec[i]+bvec[i];
}
}
((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
// trap division by zero
if(s==0 || nx==0 || ny==0)
return 0;
d/=CMath::sqrt(nx*ny);
// can only happen due to numerical problems
if (CMath::abs(d)>1.0)
d=CMath::sign(d);
return acos(d);
}
|