distance.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2010 Savarese Software Research Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * https://www.savarese.com/software/ApacheLicense-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 #ifndef __SSRC_SPATIAL_DISTANCE_H 00023 #define __SSRC_SPATIAL_DISTANCE_H 00024 00025 #include <ssrc/libssrckdtree-packages.h> 00026 00027 #include <tuple> 00028 00029 __BEGIN_NS_SSRC_SPATIAL 00030 00037 template<typename Point, 00038 typename Distance = double, 00039 const unsigned int i = NS_TR1::tuple_size<Point>::value - 1> 00040 struct euclidean_distance { 00041 typedef Distance distance_type; 00042 00050 static distance_type d2(const Point & from, const Point & to) { 00051 const distance_type d = 00052 static_cast<distance_type>(to[i]) - static_cast<distance_type>(from[i]); 00053 00054 return (d*d) + euclidean_distance<Point, distance_type, i-1>::d2(from, to); 00055 } 00056 }; 00057 00058 template<typename Point, typename Distance> 00059 struct euclidean_distance<Point, Distance, 0> { 00060 typedef Distance distance_type; 00061 00062 static distance_type d2(const Point & from, const Point & to) { 00063 const distance_type d0 = 00064 static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]); 00065 00066 return (d0*d0); 00067 } 00068 }; 00069 00070 template<typename Point, typename Distance> 00071 struct euclidean_distance<Point, Distance, 1> { 00072 typedef Distance distance_type; 00073 00074 static distance_type d2(const Point & from, const Point & to) { 00075 const distance_type d0 = 00076 static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]); 00077 const distance_type d1 = 00078 static_cast<distance_type>(to[1]) - static_cast<distance_type>(from[1]); 00079 00080 return (d0*d0) + (d1*d1); 00081 } 00082 }; 00083 00084 template<typename Point, typename Distance> 00085 struct euclidean_distance<Point, Distance, 2> { 00086 typedef Distance distance_type; 00087 00088 static distance_type d2(const Point & from, const Point & to) { 00089 const distance_type d0 = 00090 static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]); 00091 const distance_type d1 = 00092 static_cast<distance_type>(to[1]) - static_cast<distance_type>(from[1]); 00093 const distance_type d2 = 00094 static_cast<distance_type>(to[2]) - static_cast<distance_type>(from[2]); 00095 00096 return (d0*d0) + (d1*d1) + (d2*d2); 00097 } 00098 }; 00099 00100 __END_NS_SSRC_SPATIAL 00101 00102 #endif