Changeset 31:2ab1b4fdb800 in rrlib_geometry


Ignore:
Timestamp:
06.05.2012 14:41:13 (8 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Children:
32:db115bb827ee, 40:ff9509bbfa3a
Phase:
public
Message:

Adapted to changes in rrlib_canvas and extendes interface using variadic templates

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • curves/tBezierCurve.h

    r29 r31  
    105105  tBezierCurve(TIterator begin, TIterator end); 
    106106 
     107  template <typename ... TPoints> 
     108  tBezierCurve(const typename tShape::tPoint &p1, const typename tShape::tPoint &p2, const TPoints &... rest); 
     109 
    107110  inline const typename tShape::tPoint *ControlPoints() const 
    108111  { 
  • curves/tBezierCurve.hpp

    r29 r31  
    6868template <typename TIterator> 
    6969tBezierCurve<Tdimension, TElement, Tdegree>::tBezierCurve(TIterator begin, TIterator end) 
    70   : tShape() 
    7170{ 
    7271  static_assert(Tdegree > 0, "The degree of Bezier curves must be greater than zero"); 
    73   assert(static_cast<size_t>(std::distance(begin, end)) == this->NumberOfControlPoints()); 
     72  assert(static_cast<size_t>(std::distance(begin, end)) == this->NumberOfControlPoints() && "A Bezier curve must have degree + 1 control points"); 
    7473  size_t index = 0; 
    7574  for (TIterator it = begin; it != end; ++it) 
     
    7776    this->control_points[index++] = *it; 
    7877  } 
     78} 
     79 
     80template <size_t Tdimension, typename TElement, unsigned int Tdegree> 
     81template <typename ... TPoints> 
     82tBezierCurve<Tdimension, TElement, Tdegree>::tBezierCurve(const typename tShape::tPoint &p1, const typename tShape::tPoint &p2, const TPoints &... rest) 
     83{ 
     84  static_assert(Tdegree > 0, "The degree of Bezier curves must be greater than zero"); 
     85  static_assert(sizeof...(rest) + 1 == Tdegree, "A Bezier curve must have degree + 1 control points"); 
     86  size_t index = 0; 
     87  util::ProcessVariadicValues<typename tShape::tPoint>([this, index](const typename tShape::tPoint & x) mutable { this->control_points[index++] = x; }, p1, p2, rest...); 
    7988} 
    8089 
     
    398407#ifdef _LIB_RRLIB_CANVAS_PRESENT_ 
    399408 
    400 template <typename TElement> 
    401 canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tBezierCurve<2, TElement, 2> &bezier_curve) 
    402 { 
    403   canvas.StartPath(bezier_curve.ControlPoints()[0]); 
    404   canvas.AppendQuadraticBezierCurve(bezier_curve.ControlPoints()[1], bezier_curve.ControlPoints()[2]); 
    405  
    406   return canvas; 
    407 } 
    408  
    409 template <typename TElement> 
    410 canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tBezierCurve<2, TElement, 3> &bezier_curve) 
    411 { 
    412   canvas.DrawCubicBezierCurve(bezier_curve.ControlPoints(), bezier_curve.ControlPoints() + 4); 
    413  
    414   return canvas; 
    415 } 
    416  
    417409template <typename TElement, unsigned int Tdegree> 
    418410canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tBezierCurve<2, TElement, Tdegree> &bezier_curve) 
    419411{ 
    420   if (bezier_curve.GetTwist() < 1E-6) 
    421   { 
    422     canvas.DrawLineSegment(bezier_curve.ControlPoints()[0], bezier_curve.ControlPoints()[Tdegree - 1]); 
    423     return canvas; 
    424   } 
    425  
    426   typename tBezierCurve<2, TElement, Tdegree>::tSubdivision subdivision(bezier_curve.GetSubdivision()); 
    427   canvas << subdivision.first << subdivision.second; 
     412  canvas.DrawBezierCurve(Tdegree, bezier_curve.ControlPoints(), bezier_curve.ControlPoints() + bezier_curve.NumberOfControlPoints()); 
    428413 
    429414  return canvas; 
  • curves/tSplineCurve.h

    r30 r31  
    9898  tSplineCurve(TIterator begin, TIterator end); 
    9999 
     100  template <typename ... TPoints> 
     101  tSplineCurve(const typename tShape::tPoint &p1, const typename tShape::tPoint &p2, const TPoints &... rest); 
     102 
    100103  inline const size_t NumberOfControlPoints() const 
    101104  { 
  • curves/tSplineCurve.hpp

    r30 r31  
    7070tSplineCurve<Tdimension, TElement, Tdegree>::tSplineCurve(TIterator begin, TIterator end) 
    7171{ 
     72  static_assert(Tdegree > 0, "The degree of spline curves must be greater than zero"); 
    7273  std::copy(begin, end, std::back_inserter(this->control_points)); 
    73   assert(control_points.size() > Tdegree); 
     74  assert(control_points.size() > Tdegree && "A spline curve needs at least degree + 1 control points"); 
     75  this->bezier_curve_cache.resize(this->NumberOfSegments()); 
     76} 
     77 
     78template <size_t Tdimension, typename TElement, unsigned int Tdegree> 
     79template <typename ... TPoints> 
     80tSplineCurve<Tdimension, TElement, Tdegree>::tSplineCurve(const typename tShape::tPoint &p1, const typename tShape::tPoint &p2, const TPoints &... rest) 
     81{ 
     82  static_assert(Tdegree > 0, "The degree of spline curves must be greater than zero"); 
     83  static_assert(sizeof...(rest) + 2 > Tdegree, "A spline curve needs at least degree + 1 control points"); 
     84  util::ProcessVariadicValues<typename tShape::tPoint>([this](const typename tShape::tPoint & x) mutable { this->control_points.push_back(x); }, p1, p2, rest...); 
    7485  this->bezier_curve_cache.resize(this->NumberOfSegments()); 
    7586} 
     
    350361#ifdef _LIB_RRLIB_CANVAS_PRESENT_ 
    351362 
    352 template <typename TElement> 
    353 inline canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tSplineCurve<2, TElement, 2> &spline_curve) 
    354 { 
    355   unsigned int number_of_segments = spline_curve.NumberOfSegments(); 
    356   auto bezier_curve = spline_curve.GetBezierCurveForSegment(0); 
    357   canvas.StartPath(bezier_curve->ControlPoints()[0]); 
    358   for (unsigned int i = 1; i < number_of_segments; ++i) 
    359   { 
    360     canvas.AppendQuadraticBezierCurve(bezier_curve->ControlPoints()[1], bezier_curve->ControlPoints()[2]); 
    361     bezier_curve = spline_curve.GetBezierCurveForSegment(i); 
    362   } 
    363   canvas.AppendQuadraticBezierCurve(bezier_curve->ControlPoints()[1], bezier_curve->ControlPoints()[2]); 
    364  
    365   return canvas; 
    366 } 
    367  
    368 template <typename TElement> 
    369 inline canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tSplineCurve<2, TElement, 3> &spline_curve) 
    370 { 
    371   unsigned int number_of_segments = spline_curve.NumberOfSegments(); 
    372   auto bezier_curve = spline_curve.GetBezierCurveForSegment(0); 
    373   canvas.StartPath(bezier_curve->ControlPoints()[0]); 
    374   for (unsigned int i = 1; i < number_of_segments; ++i) 
    375   { 
    376     canvas.AppendCubicBezierCurve(bezier_curve->ControlPoints()[1], bezier_curve->ControlPoints()[2], bezier_curve->ControlPoints()[3]); 
    377     bezier_curve = spline_curve.GetBezierCurveForSegment(i); 
    378   } 
    379   canvas.AppendCubicBezierCurve(bezier_curve->ControlPoints()[1], bezier_curve->ControlPoints()[2], bezier_curve->ControlPoints()[3]); 
    380  
    381   return canvas; 
    382 } 
    383  
    384363template <typename TElement, unsigned int Tdegree> 
    385364inline canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tSplineCurve<2, TElement, Tdegree> &spline_curve) 
  • test/test_geometries.cpp

    r29 r31  
    263263  std::cout << std::endl << "=== Bezier curve intersects curve ===" << std::endl; 
    264264 
    265   std::vector<tPoint> control_points2; 
    266   control_points2.push_back(tPoint(0.2, 0.1)); 
    267   control_points2.push_back(tPoint(0.5, 0.3)); 
    268   control_points2.push_back(tPoint(0.6, 0.7)); 
    269   control_points2.push_back(tPoint(0.4, 0.9)); 
    270  
    271   tBezierCurve bezier_curve2(control_points2.begin(), control_points2.end()); 
     265  tBezierCurve bezier_curve2(tPoint(0.2, 0.1), tPoint(0.5, 0.3), tPoint(0.6, 0.7), tPoint(0.4, 0.9)); 
    272266 
    273267  rrlib::math::tAngleDeg angle = -40; 
Note: See TracChangeset for help on using the changeset viewer.