d8eddc
d8eddc
d8eddc
#include <tools modifiers="" modifiersegmentation.h=""></tools>
d8eddc
#include <algorithm></algorithm>
d8eddc
d8eddc
d8eddc
//*****************************************************************************************
d8eddc
//    TModifierSegmentation implementation
d8eddc
//*****************************************************************************************
d8eddc
d8eddc
493cac
TModifierSegmentation::TModifierSegmentation(const TPointD &step)
493cac
  { setStep(step); }
493cac
493cac
493cac
void
493cac
TModifierSegmentation::setStep(const TPointD &step) {
493cac
  m_step.x = std::max(TConsts::epsilon, fabs(step.x));
493cac
  m_step.y = std::max(TConsts::epsilon, fabs(step.y));
493cac
}
d8eddc
d8eddc
d8eddc
void
d8eddc
TModifierSegmentation::addSegments(
d8eddc
  TTrack &track,
d8eddc
  const TTrackPoint &p0,
d8eddc
  const TTrackPoint &p1,
d8eddc
  int level)
d8eddc
{
d8eddc
  static const int maxRecursion = 10;
d8eddc
  TPointD d = p1.position - p0.position;
d8eddc
493cac
  if (level >= maxRecursion || (fabs(d.x) <= m_step.x && fabs(d.y) <= m_step.y)) {
d0b5c0
    track.push_back(p1, false);
d8eddc
    return;
d8eddc
  }
d8eddc
d8eddc
  TTrackPoint p = track.modifier->calcPoint(0.5*(p0.originalIndex + p1.originalIndex));
d8eddc
  addSegments(track, p0, p, level + 1);
d8eddc
  addSegments(track, p, p1, level + 1);
d8eddc
}
d8eddc
d8eddc
d8eddc
void
d8eddc
TModifierSegmentation::modifyTrack(
c3c215
  const TTrack &track,
d8eddc
  TTrackList &outTracks )
d8eddc
{
c3c215
  if (!track.handler) {
c3c215
    track.handler = new TTrackHandler(track);
c3c215
    track.handler->tracks.push_back(
d8eddc
      new TTrack(
c3c215
        new TTrackModifier(*track.handler) ));
d8eddc
  }
d8eddc
9f0c16
  if (track.handler->tracks.empty())
d8eddc
    return;
d8eddc
c3c215
  TTrack &subTrack = *track.handler->tracks.front();
c3c215
  outTracks.push_back(track.handler->tracks.front());
d8eddc
9f0c16
  if (!track.changed())
9f0c16
    return;
9f0c16
d8eddc
  // remove points
c3c215
  int start = track.size() - track.pointsAdded;
d8eddc
  if (start < 0) start = 0;
9f0c16
  int subStart = subTrack.floorIndex(subTrack.indexByOriginalIndex(start-1)) + 1;
00337d
  subTrack.truncate(subStart);
d8eddc
d8eddc
  // add points
d8eddc
  TTrackPoint p0 = subTrack.modifier->calcPoint(start - 1);
c3c215
  for(int i = start; i < track.size(); ++i) {
d8eddc
    TTrackPoint p1 = subTrack.modifier->calcPoint(i);
d8eddc
    addSegments(subTrack, p0, p1);
d8eddc
    p0 = p1;
d8eddc
  }
d8eddc
d0b5c0
  // fix points
d0b5c0
  if (track.fixedSize())
d0b5c0
    subTrack.fix_to(
d0b5c0
      subTrack.floorIndex(subTrack.indexByOriginalIndex(track.fixedSize() - 1)) + 1 );
d0b5c0
  
00337d
  track.resetChanges();
d8eddc
}