libssrckdtree 1.0.8 C++ Unit Test Coverage
Current view: top level - ssrc/spatial - distance.h (source / functions) Hit Total Coverage
Test: libssrckdtree 1.0.8 C++ Unit Tests Lines: 15 15 100.0 %
Date: 2011-06-03 Functions: 15 15 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2010 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines distance functions.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_SPATIAL_DISTANCE_H
      23                 :            : #define __SSRC_SPATIAL_DISTANCE_H
      24                 :            : 
      25                 :            : #include <ssrc/libssrckdtree-packages.h>
      26                 :            : 
      27                 :            : #include <tuple>
      28                 :            : 
      29                 :            : __BEGIN_NS_SSRC_SPATIAL
      30                 :            : 
      31                 :            : /**
      32                 :            :  * euclidean_distance is a template class that houses functions for
      33                 :            :  * determining distances and distance-related values in a Euclidean
      34                 :            :  * space.  These functions are static and rely on the enclosing class
      35                 :            :  * to enable implementation of partial specializations.
      36                 :            :  */
      37                 :            : template<typename Point,
      38                 :            :          typename Distance = double,
      39                 :            :          const unsigned int i = NS_TR1::tuple_size<Point>::value - 1>
      40                 :            : struct euclidean_distance {
      41                 :            :   typedef Distance distance_type;
      42                 :            : 
      43                 :            :   /**
      44                 :            :    * Returns the square of the distance between two points.
      45                 :            :    *
      46                 :            :    * @param from The first end point.
      47                 :            :    * @param to The second end point.
      48                 :            :    * @return The square of the distance between from and to.
      49                 :            :    */
      50                 :          6 :   static distance_type d2(const Point & from, const Point & to) {
      51                 :            :     const distance_type d =
      52                 :          6 :       static_cast<distance_type>(to[i]) - static_cast<distance_type>(from[i]);
      53                 :            : 
      54                 :          6 :     return (d*d) + euclidean_distance<Point, distance_type, i-1>::d2(from, to);
      55                 :            :   }
      56                 :            : };
      57                 :            : 
      58                 :            : template<typename Point, typename Distance>
      59                 :            : struct euclidean_distance<Point, Distance, 0> {
      60                 :            :   typedef Distance distance_type;
      61                 :            : 
      62                 :         12 :   static distance_type d2(const Point & from, const Point & to) {
      63                 :            :     const distance_type d0 =
      64                 :         12 :       static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]);
      65                 :            : 
      66                 :         12 :     return (d0*d0);
      67                 :            :   }
      68                 :            : };
      69                 :            : 
      70                 :            : template<typename Point, typename Distance>
      71                 :            : struct euclidean_distance<Point, Distance, 1> {
      72                 :            :   typedef Distance distance_type;
      73                 :            : 
      74                 :    1781198 :   static distance_type d2(const Point & from, const Point & to) {
      75                 :            :     const distance_type d0 =
      76                 :    1781198 :       static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]);
      77                 :            :     const distance_type d1 =
      78                 :    1781198 :       static_cast<distance_type>(to[1]) - static_cast<distance_type>(from[1]);
      79                 :            : 
      80                 :    1781198 :     return (d0*d0) + (d1*d1);
      81                 :            :   }
      82                 :            : };
      83                 :            : 
      84                 :            : template<typename Point, typename Distance>
      85                 :            : struct euclidean_distance<Point, Distance, 2> {
      86                 :            :   typedef Distance distance_type;
      87                 :            : 
      88                 :         15 :   static distance_type d2(const Point & from, const Point & to) {
      89                 :            :     const distance_type d0 =
      90                 :         15 :       static_cast<distance_type>(to[0]) - static_cast<distance_type>(from[0]);
      91                 :            :     const distance_type d1 =
      92                 :         15 :       static_cast<distance_type>(to[1]) - static_cast<distance_type>(from[1]);
      93                 :            :     const distance_type d2 =
      94                 :         15 :       static_cast<distance_type>(to[2]) - static_cast<distance_type>(from[2]);
      95                 :            : 
      96                 :         15 :     return (d0*d0) + (d1*d1) + (d2*d2);
      97                 :            :   }
      98                 :            : };
      99                 :            : 
     100                 :            : __END_NS_SSRC_SPATIAL
     101                 :            : 
     102                 :            : #endif