mirror of https://github.com/CGAL/cgal
290 lines
11 KiB
C++
290 lines
11 KiB
C++
// Copyright (c) 2002,2011 Utrecht University (The Netherlands).
|
|
// All rights reserved.
|
|
//
|
|
// This file is part of CGAL (www.cgal.org).
|
|
// 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.
|
|
//
|
|
// Licensees holding a valid commercial license may use this file in
|
|
// accordance with the commercial license agreement provided with the software.
|
|
//
|
|
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
//
|
|
// $URL$
|
|
// $Id$
|
|
//
|
|
//
|
|
// Author(s) : Hans Tangelder (<hanst@cs.uu.nl>)
|
|
|
|
|
|
#ifndef CGAL_EUCLIDEAN_DISTANCE_H
|
|
#define CGAL_EUCLIDEAN_DISTANCE_H
|
|
|
|
#include <CGAL/Kd_tree_rectangle.h>
|
|
#include <CGAL/number_utils.h>
|
|
#include <CGAL/internal/Get_dimension_tag.h>
|
|
#include <vector>
|
|
|
|
namespace CGAL {
|
|
|
|
template <class SearchTraits>
|
|
class Euclidean_distance;
|
|
|
|
|
|
namespace internal{
|
|
template <class SearchTraits>
|
|
struct Spatial_searching_default_distance{
|
|
typedef ::CGAL::Euclidean_distance<SearchTraits> type;
|
|
};
|
|
} //namespace internal
|
|
|
|
template <class SearchTraits>
|
|
class Euclidean_distance {
|
|
|
|
SearchTraits traits;
|
|
|
|
public:
|
|
|
|
typedef typename SearchTraits::FT FT;
|
|
typedef typename SearchTraits::Point_d Point_d;
|
|
typedef Point_d Query_item;
|
|
|
|
typedef typename internal::Get_dimension_tag<SearchTraits>::Dimension D;
|
|
|
|
|
|
// default constructor
|
|
Euclidean_distance(const SearchTraits& traits_=SearchTraits()):traits(traits_) {}
|
|
|
|
// During the computation, if the partially-computed distance `pcd` gets greater or equal
|
|
// to `stop_if_geq_to_this`, the computation is stopped and `pcd` is returned
|
|
inline FT transformed_distance(const Query_item& q, const Point_d& p,
|
|
FT stop_if_geq_to_this = std::numeric_limits<FT>::max()) const
|
|
{
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it = traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d p_begin = construct_it(p), p_end = construct_it(p, 0);
|
|
return transformed_distance(q, p_begin, p_end, stop_if_geq_to_this);
|
|
}
|
|
|
|
// During the computation, if the partially-computed distance `pcd` gets greater or equal
|
|
// to `stop_if_geq_to_this`, the computation is stopped and `pcd` is returned
|
|
template <typename Coord_iterator>
|
|
inline FT transformed_distance(const Query_item& q,
|
|
Coord_iterator it_coord_begin, Coord_iterator it_coord_end,
|
|
FT stop_if_geq_to_this = std::numeric_limits<FT>::max()) const
|
|
{
|
|
return transformed_distance(q, it_coord_begin, it_coord_end, stop_if_geq_to_this, D());
|
|
}
|
|
|
|
// Dynamic version for runtime dimension, taking iterators on coordinates as parameters
|
|
// During the computation, if the partially-computed distance `pcd` gets greater or equal
|
|
// to `stop_if_geq_to_this`, the computation is stopped and `pcd` is returned
|
|
template <typename Coord_iterator>
|
|
inline FT transformed_distance(const Query_item& q,
|
|
Coord_iterator it_coord_begin, Coord_iterator it_coord_end,
|
|
FT stop_if_geq_to_this, Dynamic_dimension_tag) const
|
|
{
|
|
FT distance = FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it = traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q, 1);
|
|
if (qe - qit >= 4)
|
|
{
|
|
// Every 4 coordinates, the current partially-computed distance
|
|
// is compared to stop_if_geq_to_this
|
|
// Note: the concept SearchTraits specifies that Cartesian_const_iterator_d
|
|
// must be a random-access iterator
|
|
typename SearchTraits::Cartesian_const_iterator_d qe_minus_3 = qe - 3;
|
|
for (; qit < qe_minus_3; ++qit, ++it_coord_begin) {
|
|
FT diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
|
|
if (distance >= stop_if_geq_to_this)
|
|
return distance;
|
|
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
}
|
|
}
|
|
for (; qit != qe; ++qit, ++it_coord_begin)
|
|
{
|
|
FT diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
// Generic version for DIM > 3 taking iterators on coordinates as parameters
|
|
// During the computation, if the partially-computed distance `pcd` gets greater or equal
|
|
// to `stop_if_geq_to_this`, the computation is stopped and `pcd` is returned
|
|
template <int DIM, typename Coord_iterator>
|
|
inline FT transformed_distance(const Query_item& q,
|
|
Coord_iterator it_coord_begin, Coord_iterator it_coord_end,
|
|
FT stop_if_geq_to_this, Dimension_tag<DIM>) const
|
|
{
|
|
FT distance = FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it = traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q, 1);
|
|
if (qe - qit >= 4)
|
|
{
|
|
// Every 4 coordinates, the current partially-computed distance
|
|
// is compared to stop_if_geq_to_this
|
|
// Note: the concept SearchTraits specifies that Cartesian_const_iterator_d
|
|
// must be a random-access iterator
|
|
typename SearchTraits::Cartesian_const_iterator_d qe_minus_3 = qe - 3;
|
|
for (; qit < qe_minus_3; ++qit, ++it_coord_begin) {
|
|
FT diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
|
|
if (distance >= stop_if_geq_to_this)
|
|
return distance;
|
|
|
|
++qit; ++it_coord_begin;
|
|
diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
}
|
|
}
|
|
for (; qit != qe; ++qit, ++it_coord_begin)
|
|
{
|
|
FT diff = (*qit) - (*it_coord_begin);
|
|
distance += diff*diff;
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
//DIM = 2 loop unrolled
|
|
template <typename Coord_iterator>
|
|
inline FT transformed_distance(const Query_item& q,
|
|
Coord_iterator it_coord_begin, Coord_iterator /*unused*/,
|
|
FT /*unused*/, Dimension_tag<2>) const {
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it = traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q);
|
|
FT distance = square(*qit - *it_coord_begin);
|
|
qit++; it_coord_begin++;
|
|
distance += square(*qit - *it_coord_begin);
|
|
return distance;
|
|
}
|
|
|
|
//DIM = 3 loop unrolled
|
|
template <typename Coord_iterator>
|
|
inline FT transformed_distance(const Query_item& q,
|
|
Coord_iterator it_coord_begin, Coord_iterator /*unused*/,
|
|
FT /*unused*/, Dimension_tag<3>) const {
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it = traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q);
|
|
FT distance = square(*qit - *it_coord_begin);
|
|
qit++; it_coord_begin++;
|
|
distance += square(*qit - *it_coord_begin);
|
|
qit++; it_coord_begin++;
|
|
distance += square(*qit - *it_coord_begin);
|
|
return distance;
|
|
}
|
|
|
|
|
|
inline FT min_distance_to_rectangle(const Query_item& q,
|
|
const Kd_tree_rectangle<FT,D>& r) const {
|
|
FT distance = FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q,1);
|
|
for(unsigned int i = 0;qit != qe; i++, qit++){
|
|
if((*qit) < r.min_coord(i))
|
|
distance +=
|
|
(r.min_coord(i)-(*qit))*(r.min_coord(i)-(*qit));
|
|
else if ((*qit) > r.max_coord(i))
|
|
distance +=
|
|
((*qit)-r.max_coord(i))*((*qit)-r.max_coord(i));
|
|
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
inline FT min_distance_to_rectangle(const Query_item& q,
|
|
const Kd_tree_rectangle<FT,D>& r,std::vector<FT>& dists) const {
|
|
FT distance = FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q,1);
|
|
for(unsigned int i = 0;qit != qe; i++, qit++){
|
|
if((*qit) < r.min_coord(i)){
|
|
dists[i] = (r.min_coord(i)-(*qit));
|
|
distance += dists[i] * dists[i];
|
|
}
|
|
else if ((*qit) > r.max_coord(i)){
|
|
dists[i] = ((*qit)-r.max_coord(i));
|
|
distance += dists[i] * dists[i];
|
|
}
|
|
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
inline FT max_distance_to_rectangle(const Query_item& q,
|
|
const Kd_tree_rectangle<FT,D>& r) const {
|
|
FT distance=FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q,1);
|
|
for(unsigned int i = 0;qit != qe; i++, qit++){
|
|
if ((*qit) <= (r.min_coord(i)+r.max_coord(i))/FT(2.0))
|
|
distance += (r.max_coord(i)-(*qit))*(r.max_coord(i)-(*qit));
|
|
else
|
|
distance += ((*qit)-r.min_coord(i))*((*qit)-r.min_coord(i));
|
|
};
|
|
return distance;
|
|
}
|
|
|
|
inline FT max_distance_to_rectangle(const Query_item& q,
|
|
const Kd_tree_rectangle<FT,D>& r,std::vector<FT>& dists ) const {
|
|
FT distance=FT(0);
|
|
typename SearchTraits::Construct_cartesian_const_iterator_d construct_it=traits.construct_cartesian_const_iterator_d_object();
|
|
typename SearchTraits::Cartesian_const_iterator_d qit = construct_it(q),
|
|
qe = construct_it(q,1);
|
|
for(unsigned int i = 0;qit != qe; i++, qit++){
|
|
if ((*qit) <= (r.min_coord(i)+r.max_coord(i))/FT(2.0)){
|
|
dists[i] = (r.max_coord(i)-(*qit));
|
|
distance += dists[i] * dists[i];
|
|
}
|
|
else{
|
|
dists[i] = ((*qit)-r.min_coord(i));
|
|
distance += dists[i] * dists[i];
|
|
}
|
|
};
|
|
return distance;
|
|
}
|
|
|
|
inline FT new_distance(FT dist, FT old_off, FT new_off,
|
|
int /* cutting_dimension */) const {
|
|
|
|
FT new_dist = dist + (new_off*new_off - old_off*old_off);
|
|
return new_dist;
|
|
}
|
|
|
|
inline FT transformed_distance(FT d) const {
|
|
return d*d;
|
|
}
|
|
|
|
inline FT inverse_of_transformed_distance(FT d) const {
|
|
return CGAL::sqrt(d);
|
|
}
|
|
|
|
}; // class Euclidean_distance
|
|
|
|
} // namespace CGAL
|
|
#endif // EUCLIDEAN_DISTANCE_H
|