Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef T_SEGMENTADJUSTER_H
Toshihiro Shimizu 890ddd
#define T_SEGMENTADJUSTER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TVECTORIMAGE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
e280ae
#ifdef _MSC_VER
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4251)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  The TSegmentAdjuster minimizes the distance between
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
     stroke0->getPoint(w0) and stroke1->getPoint(w1)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  changing the values of w0 and w1 (in a defined range).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  Usage:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p0(stroke0, w0, 0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p1(stroke1, w1, 0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster adjuster;
Toshihiro Shimizu 890ddd
  adjuster.compute(p0,p1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  // the adjusted segment is p0.getPoint(), p1.getPoint()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  if you want to adjust the segment only in the w-growing-direction:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p0(stroke0, w0, w0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p1(stroke1, w1, w1, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster adjuster;
Toshihiro Shimizu 890ddd
  adjuster.compute(p0,p1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TSegmentAdjuster {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  class End {
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    TStroke *m_stroke;
Shinya Kitaoka 120a6e
    double m_w;
Shinya Kitaoka 120a6e
    double m_wmin, m_wmax;
Shinya Kitaoka 120a6e
    End() : m_stroke(0), m_w(0), m_wmin(0), m_wmax(1) {}
Shinya Kitaoka 120a6e
    End(TStroke *stroke, double w)
Shinya Kitaoka 120a6e
        : m_stroke(stroke), m_w(w), m_wmin(0), m_wmax(1) {}
Shinya Kitaoka 120a6e
    End(TStroke *stroke, double w, double wmin, double wmax)
Shinya Kitaoka 120a6e
        : m_stroke(stroke), m_w(w), m_wmin(wmin), m_wmax(wmax) {}
Shinya Kitaoka 120a6e
    TPointD getPoint() const { return m_stroke->getPoint(m_w); }
Shinya Kitaoka 120a6e
    TPointD getSpeed() const { return m_stroke->getSpeed(m_w); }
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TSegmentAdjuster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // debug
Shinya Kitaoka 120a6e
  void enableTrace(bool enabled) { m_traceEnabled = enabled; }
Shinya Kitaoka 120a6e
  void draw();
Shinya Kitaoka 120a6e
  void clear() { m_links.clear(); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void compute(End &a, End &b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  End m_a, m_b, m_c, m_d;
Shinya Kitaoka 120a6e
  std::vector<std::pair<tpointd, tpointd="">> m_links;</std::pair<tpointd,>
Shinya Kitaoka 120a6e
  bool m_traceEnabled;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  inline double fun(double u, double v) const {
Shinya Kitaoka 120a6e
    return norm2(m_a.m_stroke->getPoint(u) - m_b.m_stroke->getPoint(v));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  inline void gradient(double &dfdu, double &dfdv, double u, double v) {
Shinya Kitaoka 120a6e
    const double h = 0.0001;
Shinya Kitaoka 120a6e
    dfdu           = (fun(u + h, v) - fun(u - h, v)) / (2 * h);
Shinya Kitaoka 120a6e
    dfdv           = (fun(u, v + h) - fun(u, v - h)) / (2 * h);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  inline static double dsda(TStroke *stroke, double w, double dwda) {
Shinya Kitaoka 120a6e
    const double h = 0.0001;
Shinya Kitaoka 120a6e
    return norm(stroke->getPoint(w + dwda * h) -
Shinya Kitaoka 120a6e
                stroke->getPoint(w - dwda * h)) /
Shinya Kitaoka 120a6e
           (2 * h);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // T_SEGMENTADJUSTER_H