Changeset 16:a9e6bf94cfe4 in rrlib_geometry


Ignore:
Timestamp:
15.07.2011 10:45:01 (8 years ago)
Author:
Tobias F?hst <foehst@…>
Branch:
default
Phase:
public
Rebase:
38386433346230373436303936363430356136643330376363656464313732333165393366333332
Message:

Added GetClosestPoint to Bezier curves and splines

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • curves/tBezierCurve.h

    r8 r16  
    119119                              const tLine<Tdimension, TElement> &line) const; 
    120120 
     121  const typename tShape::tPoint GetClosestPoint(const typename tShape::tPoint &reference_point) const; 
     122 
    121123  virtual tBezierCurve &Translate(const math::tVector<Tdimension, TElement> &translation); 
    122124  virtual tBezierCurve &Rotate(const math::tMatrix<Tdimension, Tdimension, TElement> &rotation); 
  • curves/tBezierCurve.hpp

    r6 r16  
    294294 
    295295//---------------------------------------------------------------------- 
     296// tBezierCurve GetClosestPoint 
     297//---------------------------------------------------------------------- 
     298template <size_t Tdimension, typename TElement, unsigned int Tdegree> 
     299const typename tShape<Tdimension, TElement>::tPoint tBezierCurve<Tdimension, TElement, Tdegree>::GetClosestPoint(const typename tShape::tPoint &reference_point) const 
     300{ 
     301  tSubdivision subdivision(this->GetSubdivision()); 
     302 
     303  tLineSegment<Tdimension, TElement> first_segment_base_line(subdivision.first.GetControlPoint(0), subdivision.first.GetControlPoint(Tdegree)); 
     304  tLineSegment<Tdimension, TElement> second_segment_base_line(subdivision.second.GetControlPoint(0), subdivision.second.GetControlPoint(Tdegree)); 
     305 
     306  typename tShape::tPoint first_candidate = first_segment_base_line.GetClosestPoint(reference_point); 
     307  typename tShape::tPoint second_candidate = second_segment_base_line.GetClosestPoint(reference_point); 
     308 
     309  double squared_distance_to_first_candidate = (reference_point - first_candidate).SquaredLength(); 
     310  double squared_distance_to_second_candidate = (reference_point - second_candidate).SquaredLength(); 
     311 
     312  if (squared_distance_to_first_candidate < squared_distance_to_second_candidate) 
     313  { 
     314    if (subdivision.first.GetTwist() < 1E-6) 
     315    { 
     316      return first_candidate; 
     317    } 
     318    return subdivision.first.GetClosestPoint(reference_point); 
     319  } 
     320 
     321  if (subdivision.second.GetTwist() < 1E-6) 
     322  { 
     323    return second_candidate; 
     324  } 
     325  return subdivision.second.GetClosestPoint(reference_point); 
     326} 
     327 
     328//---------------------------------------------------------------------- 
    296329// tBezierCurve Translate 
    297330//---------------------------------------------------------------------- 
  • curves/tSplineCurve.h

    r8 r16  
    134134                        const tLine<Tdimension, TElement> &line) const; 
    135135 
     136  const typename tShape::tPoint GetClosestPoint(const typename tShape::tPoint &reference_point) const; 
     137 
    136138  virtual tSplineCurve &Translate(const math::tVector<Tdimension, TElement> &translation); 
    137139  virtual tSplineCurve &Rotate(const math::tMatrix<Tdimension, Tdimension, TElement> &rotation); 
  • curves/tSplineCurve.hpp

    r6 r16  
    224224 
    225225//---------------------------------------------------------------------- 
     226// tSplineCurve GetClosestPoint 
     227//---------------------------------------------------------------------- 
     228template <size_t Tdimension, typename TElement, unsigned int Tdegree> 
     229const typename tShape<Tdimension, TElement>::tPoint tSplineCurve<Tdimension, TElement, Tdegree>::GetClosestPoint(const typename tShape::tPoint &reference_point) const 
     230{ 
     231  typename tShape::tPoint closest_point = this->GetBezierCurveForSegment(0).GetClosestPoint(reference_point); 
     232  double min_distance = (closest_point - reference_point).Length(); 
     233 
     234  for (size_t i = 1; i < this->GetNumberOfSegments(); ++i) 
     235  { 
     236    typename tShape::tPoint candidate = this->GetBezierCurveForSegment(i).GetClosestPoint(reference_point); 
     237    double distance = (candidate - reference_point).Length(); 
     238 
     239    if (distance < min_distance) 
     240    { 
     241      min_distance = distance; 
     242      closest_point = candidate; 
     243    } 
     244  } 
     245 
     246  return closest_point; 
     247} 
     248 
     249//---------------------------------------------------------------------- 
    226250// tSplineCurve Translate 
    227251//---------------------------------------------------------------------- 
  • test/test_geometries.cpp

    r8 r16  
    284284  control_points.push_back(tPoint(0.9, 0.4)); 
    285285 
    286  
    287286  tConcreteSplineCurve spline(control_points); 
    288287 
     
    380379  window.Render(); 
    381380 
    382  
     381  std::cout << std::endl << "=== Closest point to Bezier curve ===" << std::endl; 
     382 
     383  window.Clear(); 
     384  window.SetColor(0); 
     385  DrawBezierCurve(window, bezier_curve); 
     386  window.Render(); 
     387 
     388  for (int i = 0; i < 10; ++i) 
     389  { 
     390    tPoint reference_point(drand48(), drand48()); 
     391    tPoint closest_point(bezier_curve.GetClosestPoint(reference_point)); 
     392 
     393    window.SetColor(3); 
     394    DrawLineSegment(window, tLineSegment(reference_point, closest_point)); 
     395    window.SetColor(1); 
     396    DrawPoint(window, reference_point); 
     397    window.SetColor(2); 
     398    DrawPoint(window, closest_point); 
     399    window.Render(); 
     400  } 
     401 
     402  std::cout << std::endl << "=== Closest point to spline curve ===" << std::endl; 
     403 
     404  window.Clear(); 
     405  window.SetColor(0); 
     406  DrawSplineCurve(window, spline); 
     407  window.Render(); 
     408 
     409  for (int i = 0; i < 10; ++i) 
     410  { 
     411    tPoint reference_point(drand48(), drand48()); 
     412    tPoint closest_point(spline.GetClosestPoint(reference_point)); 
     413 
     414    window.SetColor(3); 
     415    DrawLineSegment(window, tLineSegment(reference_point, closest_point)); 
     416    window.SetColor(1); 
     417    DrawPoint(window, reference_point); 
     418    window.SetColor(2); 
     419    DrawPoint(window, closest_point); 
     420    window.Render(); 
     421  } 
    383422 
    384423  window.Render(); 
Note: See TracChangeset for help on using the changeset viewer.