wykobi_ext.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <wykobi.hpp>
25 
27 {
28  /**
29  * Compared to wykobi::closest_point_on_polygon_from_point, this function also projects points inside the polygon onto the boundary
30  */
31  template <typename T>
32  inline ::wykobi::point2d<T>
33  closest_point_on_polygon_from_point(const ::wykobi::polygon<T, 2>& polygon,
34  const ::wykobi::point2d<T>& point)
35  {
36  if (polygon.size() < 3)
37  {
38  return ::wykobi::degenerate_point2d<T>();
39  }
40 
41  std::size_t j = polygon.size() - 1;
42  T minDist = std::numeric_limits<T>::max();
43 
44  ::wykobi::point2d<T> closestPoint = ::wykobi::degenerate_point2d<T>();
45 
46  for (std::size_t i = 0; i < polygon.size(); ++i)
47  {
48  ::wykobi::point2d<T> currPoint = ::wykobi::closest_point_on_segment_from_point(
49  ::wykobi::make_segment(polygon[i], polygon[j]), point);
50 
51  const T currDist = distance(point, currPoint);
52 
53  if (currDist < minDist)
54  {
55  closestPoint = currPoint;
56  minDist = currDist;
57  }
58 
59  j = i;
60  }
61 
62  return closestPoint;
63  }
64 
65 } // namespace armarx::wykobi::ext
armarx::wykobi::ext
Definition: wykobi_ext.h:26
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::wykobi::ext::closest_point_on_polygon_from_point
inline ::wykobi::point2d< T > closest_point_on_polygon_from_point(const ::wykobi::polygon< T, 2 > &polygon, const ::wykobi::point2d< T > &point)
Compared to wykobi::closest_point_on_polygon_from_point, this function also projects points inside th...
Definition: wykobi_ext.h:33
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:95
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38