Changeset 52:26851bc6daf6 in rrlib_geometry


Ignore:
Timestamp:
25.06.2013 19:28:25 (6 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Made tBSplineCurve::NumberOfSegments cache its result and fixed the lookup functions for local segments and parameters

Location:
curves
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • curves/tBSplineCurve.h

    r51 r52  
    125125  /*! Bezier control points used to evaluate b-spline curve which is converted to a set of bezier curves  */ 
    126126  mutable std::vector<typename tShape::tPoint> bezier_control_point_cache; 
     127  mutable unsigned int number_of_segments; 
    127128 
    128129  /*! 
  • curves/tBSplineCurve.hpp

    r51 r52  
    6464template <size_t Tdimension, typename TElement, unsigned int Tdegree> 
    6565tBSplineCurve<Tdimension, TElement, Tdegree>::tBSplineCurve() : 
    66   tSplineCurve() 
     66  tSplineCurve(), 
     67  number_of_segments(-1) 
    6768{ 
    6869  this->CalculateKnotVector(); 
     
    7273template<typename TIterator, typename TKnotIterator> 
    7374tBSplineCurve<Tdimension, TElement, Tdegree>::tBSplineCurve(TIterator control_points_begin, TIterator control_points_end, TKnotIterator knots_begin, TKnotIterator knots_end) : 
    74   tSplineCurve(control_points_begin, control_points_end) 
     75  tSplineCurve(control_points_begin, control_points_end), 
     76  number_of_segments(-1) 
    7577{ 
    7678  // copy knots 
     
    8284template<typename TIterator> 
    8385tBSplineCurve<Tdimension, TElement, Tdegree>::tBSplineCurve(TIterator begin, TIterator end) : 
    84   tSplineCurve(begin, end) 
     86  tSplineCurve(begin, end), 
     87  number_of_segments(-1) 
    8588{ 
    8689  this->CalculateKnotVector(); 
     
    9396const unsigned int tBSplineCurve<Tdimension, TElement, Tdegree>::NumberOfSegments() const 
    9497{ 
    95   unsigned int number_of_segments = 0; 
    96   typename tSplineCurve::tParameter current_knot = this->knots[0]; 
    97   for (size_t i = 1; i < this->knots.size(); ++i) 
    98   { 
    99     if (current_knot != this->knots[i]) 
    100     { 
    101       number_of_segments++; 
    102       current_knot = this->knots[i]; 
    103     } 
    104   } 
    105   return number_of_segments; 
     98  if (this->number_of_segments == static_cast<unsigned int>(-1)) 
     99  { 
     100    this->number_of_segments = 0; 
     101    for (size_t i = 0; i < this->knots.size() - 1; ++i) 
     102    { 
     103      if (this->knots[i] != this->knots[i + 1]) 
     104      { 
     105        this->number_of_segments++; 
     106      } 
     107    } 
     108  } 
     109  return this->number_of_segments; 
    106110}; 
    107111 
     
    115119  this->CalculateKnotVector(); 
    116120  this->bezier_control_point_cache.clear(); 
     121  this->number_of_segments = -1; 
    117122} 
    118123 
     
    211216{ 
    212217  assert((this->knots.front() <= t) && (t <= this->knots.back())); 
    213   auto it = std::lower_bound(this->knots.begin(), this->knots.end(), t); 
    214  
    215   return static_cast<unsigned int>(t == this->knots.front() ? 0 : std::distance(this->knots.begin(), it) - 1); 
     218  assert(this->NumberOfSegments() > 0); 
     219  unsigned int segment = 0; 
     220  for (size_t i = 0; i < this->knots.size() - 1; ++i) 
     221  { 
     222    if (this->knots[i] != this->knots[i + 1]) 
     223    { 
     224      if (this->knots[i + 1] > t) 
     225      { 
     226        return segment; 
     227      } 
     228      segment++; 
     229    } 
     230  } 
     231  assert(t == this->knots.back()); 
     232  return this->NumberOfSegments() - 1; 
    216233} 
    217234 
     
    222239typename tSplineCurve<Tdimension, TElement, Tdegree>::tParameter tBSplineCurve<Tdimension, TElement, Tdegree>::GetLocalParameter(typename tSplineCurve::tParameter t) const 
    223240{ 
    224   unsigned int start = this->GetSegmentForParameter(t); 
    225   unsigned int stop = start + 1; 
    226   while (this->knots[start] == this->knots[stop]) 
    227   { 
    228     stop++; 
    229     assert(stop < this->knots.size()); 
    230   } 
    231   return (t - this->knots[start]) / (this->knots[stop] - this->knots[start]); 
     241  assert((this->knots.front() <= t) && (t <= this->knots.back())); 
     242  assert(this->NumberOfSegments() > 0); 
     243  unsigned int segment = 0; 
     244  for (size_t i = 0; i < this->knots.size() - 1; ++i) 
     245  { 
     246    if (this->knots[i] != this->knots[i + 1]) 
     247    { 
     248      if (this->knots[i + 1] > t) 
     249      { 
     250        return (t - this->knots[i]) / (this->knots[i + 1] - this->knots[i]); 
     251      } 
     252      segment++; 
     253    } 
     254  } 
     255  assert(t == this->knots.back()); 
     256  return 1; 
    232257} 
    233258 
Note: See TracChangeset for help on using the changeset viewer.