Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsegmentadjuster.h"
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qdebug></qdebug>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSegmentAdjuster::TSegmentAdjuster() : m_traceEnabled(false) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void TSegmentAdjuster::compute(End &pa, End &pb) {
Shinya Kitaoka 120a6e
  m_a = m_c = pa;
Shinya Kitaoka 120a6e
  m_b = m_d = pb;
Shinya Kitaoka 120a6e
  m_links.clear();
Shinya Kitaoka 120a6e
  if (m_traceEnabled)
Shinya Kitaoka 120a6e
    m_links.push_back(std::make_pair(m_a.getPoint(), m_b.getPoint()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double u = m_a.m_w, v = m_b.m_w;
Shinya Kitaoka 120a6e
  // double ua=w0, va=w1;
Shinya Kitaoka 120a6e
  // TPointD c = (stroke0->getPoint(ua)+stroke1->getPoint(va))*0.5;
Shinya Kitaoka 120a6e
  int count = 0;
Shinya Kitaoka 120a6e
  for (;;) {
Shinya Kitaoka 120a6e
    if (++count > 1000) break;
Shinya Kitaoka 120a6e
    double du, dv;
Shinya Kitaoka 120a6e
    gradient(du, dv, u, v);
Shinya Kitaoka 120a6e
    double dd = sqrt(du * du + dv * dv);
Shinya Kitaoka 120a6e
    if (dd < 0.01) break;
Shinya Kitaoka 120a6e
    du *= 1.0 / dd;
Shinya Kitaoka 120a6e
    dv *= 1.0 / dd;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double a_dsda = dsda(m_a.m_stroke, u, du);
Shinya Kitaoka 120a6e
    double b_dsda = dsda(m_b.m_stroke, v, dv);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    const double ds  = 0.5;
Shinya Kitaoka 120a6e
    const double eps = 0.0001;
Shinya Kitaoka 120a6e
    double a         = 0.01;
Shinya Kitaoka 120a6e
    double aa;
Shinya Kitaoka 120a6e
    if (a_dsda > eps && (aa = ds / a_dsda, aa < a)) a = aa;
Shinya Kitaoka 120a6e
    if (b_dsda > eps && (aa = ds / b_dsda, aa < a)) a = aa;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    u -= a * du;
Shinya Kitaoka 120a6e
    v -= a * dv;
Shinya Kitaoka 120a6e
    u = tcrop(u, m_a.m_wmin, m_a.m_wmax);
Shinya Kitaoka 120a6e
    v = tcrop(v, m_b.m_wmin, m_b.m_wmax);
Shinya Kitaoka 120a6e
    if (m_traceEnabled)
Shinya Kitaoka 120a6e
      m_links.push_back(
Shinya Kitaoka 120a6e
          std::make_pair(m_a.m_stroke->getPoint(u), m_b.m_stroke->getPoint(v)));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_c.m_w = u;
Shinya Kitaoka 120a6e
  m_d.m_w = v;
Shinya Kitaoka 120a6e
  pa.m_w  = u;
Shinya Kitaoka 120a6e
  pb.m_w  = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSegmentAdjuster::draw() {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)m_links.size(); i++) {
Shinya Kitaoka 120a6e
    glColor3d(0.9, 0.8, 0.7);
Shinya Kitaoka 120a6e
    tglDrawSegment(m_links[i].first, m_links[i].second);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}