fa009d
fa009d
fa009d
#include <tools modifiers="" modifiersimplify.h=""></tools>
fa009d
#include <algorithm></algorithm>
fa009d
fa009d
fa009d
//*****************************************************************************************
fa009d
//    TModifierSimplify implementation
fa009d
//*****************************************************************************************
fa009d
fa009d
fa009d
TModifierSimplify::TModifierSimplify(double step):
fa009d
  step(step) { }
fa009d
fa009d
fa009d
void
fa009d
TModifierSimplify::modifyTrack(
fa009d
  const TTrack &track,
fa009d
  TTrackList &outTracks )
fa009d
{
fa009d
  if (!track.handler) {
fa009d
    Handler *handler = new Handler();
fa009d
    track.handler = handler;
fa009d
    handler->track = new TTrack(track);
fa009d
    new Interpolator(*handler->track);
fa009d
  }
fa009d
fa009d
  Handler *handler = dynamic_cast<handler*>(track.handler.getPointer());</handler*>
fa009d
  if (!handler)
fa009d
    return;
fa009d
  
fa009d
  outTracks.push_back(handler->track);
fa009d
  TTrack &subTrack = *handler->track;
fa009d
fa009d
  if (!track.changed())
fa009d
    return;
fa009d
  
fa009d
  // remove points
fa009d
  int start = track.size() - track.pointsAdded;
fa009d
  if (start < 0) start = 0;
fa009d
  int subStart = subTrack.floorIndex(subTrack.indexByOriginalIndex(start));
fa009d
  if (subStart < 0) subStart = 0;
fa009d
  start = track.floorIndex(subTrack[subStart].originalIndex);
fa009d
  if (start < 0) start = 0;
fa009d
  subTrack.truncate(subStart);
fa009d
fa009d
  // add points
fa009d
  double step2 = step*step;
fa009d
  TTrackPoint p0 = subTrack.back();
fa009d
  for(int i = start; i < track.size(); ++i) {
fa009d
    const TTrackPoint &p1 = subTrack.pointFromOriginal(i);
fa009d
    if (!subTrack.empty() && tdistance2(p1.position, p0.position) < step2) {
fa009d
      if (p0.pressure < p1.pressure) p0.pressure = p1.pressure;
fa009d
      if (i == track.size() - 1) p0.position = p1.position;
fa009d
      p0.tilt          = p1.tilt;
fa009d
      p0.time          = p1.time;
fa009d
      p0.final         = p1.final;
fa009d
      subTrack.pop_back();
fa009d
      subTrack.push_back(p0, false);
fa009d
    } else {
fa009d
      p0 = p1;
fa009d
      subTrack.push_back(p0, false);
fa009d
    }
fa009d
  }
fa009d
fa009d
  // fix points
fa009d
  if (track.fixedFinished())
fa009d
    subTrack.fix_all();
fa009d
  else
fa009d
  if (track.fixedSize())
fa009d
    subTrack.fix_to(
fa009d
      subTrack.floorIndex( subTrack.indexByOriginalIndex(track.fixedSize()-1) ));
fa009d
  
fa009d
  track.resetChanges();
fa009d
}