GeodesicMetric.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/config.h"
00012 #include "lib/common.h"
00013 #include "lib/io.h"
00014 #include "distance/GeodesicMetric.h"
00015 #include "features/Features.h"
00016 #include "features/SimpleFeatures.h"
00017
00018 using namespace shogun;
00019
00020 CGeodesicMetric::CGeodesicMetric()
00021 : CSimpleDistance<float64_t>()
00022 {
00023 }
00024
00025 CGeodesicMetric::CGeodesicMetric(CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r)
00026 : CSimpleDistance<float64_t>()
00027 {
00028 init(l, r);
00029 }
00030
00031 CGeodesicMetric::~CGeodesicMetric()
00032 {
00033 cleanup();
00034 }
00035
00036 bool CGeodesicMetric::init(CFeatures* l, CFeatures* r)
00037 {
00038 bool result=CSimpleDistance<float64_t>::init(l,r);
00039
00040 return result;
00041 }
00042
00043 void CGeodesicMetric::cleanup()
00044 {
00045 }
00046
00047 float64_t CGeodesicMetric::compute(int32_t idx_a, int32_t idx_b)
00048 {
00049 int32_t alen, blen;
00050 bool afree, bfree;
00051
00052 float64_t* avec=
00053 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00054 float64_t* bvec=
00055 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00056
00057 ASSERT(alen==blen);
00058
00059 float64_t s=0;
00060 float64_t d=0;
00061 float64_t nx=0;
00062 float64_t ny=0;
00063 {
00064 for (int32_t i=0; i<alen; i++)
00065 {
00066 d+=avec[i]*bvec[i];
00067 nx+=avec[i]*avec[i];
00068 ny+=bvec[i]*bvec[i];
00069 s+=avec[i]+bvec[i];
00070 }
00071 }
00072
00073 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00074 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00075
00076
00077
00078 if(s==0 || nx==0 || ny==0)
00079 return 0;
00080
00081 d/=CMath::sqrt(nx*ny);
00082
00083
00084 if (CMath::abs(d)>1.0)
00085 d=CMath::sign(d);
00086
00087 return acos(d);
00088 }