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/RealFeatures.h"
00017
00018 CGeodesicMetric::CGeodesicMetric()
00019 : CSimpleDistance<DREAL>()
00020 {
00021 }
00022
00023 CGeodesicMetric::CGeodesicMetric(CRealFeatures* l, CRealFeatures* r)
00024 : CSimpleDistance<DREAL>()
00025 {
00026 init(l, r);
00027 }
00028
00029 CGeodesicMetric::~CGeodesicMetric()
00030 {
00031 cleanup();
00032 }
00033
00034 bool CGeodesicMetric::init(CFeatures* l, CFeatures* r)
00035 {
00036 bool result=CSimpleDistance<DREAL>::init(l,r);
00037
00038 return result;
00039 }
00040
00041 void CGeodesicMetric::cleanup()
00042 {
00043 }
00044
00045 bool CGeodesicMetric::load_init(FILE* src)
00046 {
00047 return false;
00048 }
00049
00050 bool CGeodesicMetric::save_init(FILE* dest)
00051 {
00052 return false;
00053 }
00054
00055 DREAL CGeodesicMetric::compute(INT idx_a, INT idx_b)
00056 {
00057 INT alen, blen;
00058 bool afree, bfree;
00059
00060 double* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00061 double* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00062
00063 ASSERT(alen==blen);
00064
00065 DREAL s=0;
00066 DREAL d=0;
00067 DREAL nx=0;
00068 DREAL ny=0;
00069 {
00070 for (INT i=0; i<alen; i++)
00071 {
00072 d+=avec[i]*bvec[i];
00073 nx+=avec[i]*avec[i];
00074 ny+=bvec[i]*bvec[i];
00075 s+=avec[i]+bvec[i];
00076 }
00077 }
00078
00079 ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00080 ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00081
00082
00083
00084 if(s==0 || nx==0 || ny==0)
00085 return 0;
00086
00087 d/=CMath::sqrt(nx*ny);
00088
00089
00090 if (CMath::abs(d)>1.0)
00091 d=CMath::sign(d);
00092
00093 return acos(d);
00094 }