Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tmachine.h"
Toshihiro Shimizu 890ddd
#include "tcurves.h"
Toshihiro Shimizu 890ddd
#include "tcurveutil.h"
Toshihiro Shimizu 890ddd
#include "tmathutil.h"
Toshihiro Shimizu 890ddd
#include "tbezier.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TSegment &segment) {
Shinya Kitaoka 120a6e
  return out << "S{" << segment.getP0() << ", " << segment.getP1() << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TCubic::split(double t, TCubic &first, TCubic &second) const {
Shinya Kitaoka 120a6e
  double s = 1.0 - t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD H = s * m_p1 + t * m_p2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p0 = m_p0;
Shinya Kitaoka 120a6e
  first.m_p1 = s * m_p0 + t * m_p1;
Shinya Kitaoka 120a6e
  first.m_p2 = s * first.m_p1 + t * H;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  second.m_p3 = m_p3;
Shinya Kitaoka 120a6e
  second.m_p2 = s * m_p2 + t * m_p3;
Shinya Kitaoka 120a6e
  second.m_p1 = s * H + t * second.m_p2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p3  = s * first.m_p2 + t * second.m_p1;
Shinya Kitaoka 120a6e
  second.m_p0 = first.m_p3;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TCubic::getLength(double t0, double t1) const { return -1; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPointD TQuadratic::getPoint(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return m_p0 * s * s + 2 * t * s * m_p1 + t * t * m_p2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TQuadratic::getX(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return m_p0.x * s * s + 2 * t * s * m_p1.x + t * t * m_p2.x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TQuadratic::getY(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return m_p0.y * s * s + 2 * t * s * m_p1.y + t * t * m_p2.y;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TQuadratic::getT(const TPointD &p) const {
Shinya Kitaoka 120a6e
  // risolvo l'equazione  min|| b(t) - p ||
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // esprimo b in forma di polinomio  ed ottengo
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  //    ||  2                ||
Shinya Kitaoka 120a6e
  // min || a t + b t + c - p ||
Shinya Kitaoka 120a6e
  //    ||                   ||
Shinya Kitaoka 120a6e
  //
Shinya Kitaoka 120a6e
  // il tutto si riconduce a cercare le radici
Shinya Kitaoka 120a6e
  //  di un'equazione del tipo
Shinya Kitaoka 120a6e
  //    2  3          2               2
Shinya Kitaoka 120a6e
  // 2·a ·t  + 3·a·b·t  + t·(2·a·v + b ) + b·v
Shinya Kitaoka 120a6e
  // dove v e' pari a c - p
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  vector<tpointd> bez(3), poly(3);</tpointd>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bez[0] = m_p0;
Shinya Kitaoka 120a6e
  bez[1] = m_p1;
Shinya Kitaoka 120a6e
  bez[2] = m_p2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bezier2poly(bez, poly);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD v = poly[0] - p;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  vector<double> toSolve(4);</double>
Shinya Kitaoka 120a6e
  vector<double> sol;</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  toSolve[3] = 2.0 * norm2(poly[2]);
Shinya Kitaoka 120a6e
  toSolve[2] = 3.0 * (poly[2].x * poly[1].x + poly[2].y * poly[1].y);
Shinya Kitaoka 120a6e
  toSolve[1] = 2.0 * (poly[2].x * v.x + poly[2].y * v.y) + norm2(poly[1]);
Shinya Kitaoka 120a6e
  toSolve[0] = (poly[1].x * v.x + poly[1].y * v.y);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int nSol = rootFinding(toSolve, sol);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (-1 == nSol)  // infinite soluzioni
Shinya Kitaoka 120a6e
    return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int minParameter = -1;
Shinya Kitaoka 120a6e
  double minDist   = (std::numeric_limits<double>::max)();</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < nSol; ++i) {
Shinya Kitaoka 120a6e
    if (sol[i] < 0.0)
Shinya Kitaoka 120a6e
      sol[i] = 0.0;
Shinya Kitaoka 120a6e
    else if (sol[i] > 1.0)
Shinya Kitaoka 120a6e
      sol[i] = 1.0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    double tmpDist = tdistance2(p, getPoint(sol[i]));
Shinya Kitaoka 120a6e
    if (tmpDist < minDist) {
Shinya Kitaoka 120a6e
      minDist      = tmpDist;
Shinya Kitaoka 120a6e
      minParameter = i;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (minParameter != -1) return sol[minParameter];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return tdistance2(m_p0, p) < tdistance2(m_p2, p) ? 0 : 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TQuadratic::split(double t, TQuadratic &left, TQuadratic &right) const {
Shinya Kitaoka 120a6e
  double dt;
Shinya Kitaoka 120a6e
  TPointD p;
Shinya Kitaoka 120a6e
  dt = 1.0 - t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_p0  = m_p0;
Shinya Kitaoka 120a6e
  right.m_p2 = m_p2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_p1  = dt * m_p0 + t * m_p1;
Shinya Kitaoka 120a6e
  right.m_p1 = dt * m_p1 + t * m_p2;
Shinya Kitaoka 120a6e
  p          = dt * left.m_p1 + t * right.m_p1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_p2 = right.m_p0 = p;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRectD TQuadratic::getBBox() const {
Shinya Kitaoka 120a6e
  TRectD bBox;
Shinya Kitaoka 120a6e
  if (m_p0.x < m_p2.x)
Shinya Kitaoka 120a6e
    bBox.x0 = m_p0.x, bBox.x1 = m_p2.x;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    bBox.x0 = m_p2.x, bBox.x1 = m_p0.x;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_p0.y < m_p2.y)
Shinya Kitaoka 120a6e
    bBox.y0 = m_p0.y, bBox.y1 = m_p2.y;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    bBox.y0 = m_p2.y, bBox.y1 = m_p0.y;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD denom = 2 * m_p1 - m_p0 - m_p2;
Shinya Kitaoka 120a6e
  if (denom.x != 0) {
Shinya Kitaoka 120a6e
    double tx = (m_p1.x - m_p0.x) / denom.x;
Shinya Kitaoka 120a6e
    if (tx >= 0 && tx <= 1) {
Shinya Kitaoka 120a6e
      double x = getPoint(tx).x;
Shinya Kitaoka 120a6e
      if (x < bBox.x0)
Shinya Kitaoka 120a6e
        bBox.x0 = x;
Shinya Kitaoka 120a6e
      else if (x > bBox.x1)
Shinya Kitaoka 120a6e
        bBox.x1 = x;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (denom.y != 0) {
Shinya Kitaoka 120a6e
    double ty = (m_p1.y - m_p0.y) / denom.y;
Shinya Kitaoka 120a6e
    if (ty >= 0 && ty <= 1) {
Shinya Kitaoka 120a6e
      double y = getPoint(ty).y;
Shinya Kitaoka 120a6e
      if (y < bBox.y0)
Shinya Kitaoka 120a6e
        bBox.y0 = y;
Shinya Kitaoka 120a6e
      else if (y > bBox.y1)
Shinya Kitaoka 120a6e
        bBox.y1 = y;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return bBox;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
Calcolo della curvatura per una Quadratica.
Toshihiro Shimizu 890ddd
Vedi Farin pag.176 per la spiegazione della formula
Toshihiro Shimizu 890ddd
usata.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
double TQuadratic::getCurvature(double t) const {
Shinya Kitaoka 120a6e
  assert(0 <= t && t <= 1.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TQuadratic q1, q2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  split(t, q1, q2);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double signum = 1.0;
Shinya Kitaoka 120a6e
  if (areAlmostEqual(t, 1.0)) {
Shinya Kitaoka 120a6e
    signum *= -1.0;
Shinya Kitaoka 120a6e
    std::swap(q1, q2);
Shinya Kitaoka 120a6e
    std::swap(q2.m_p0, q2.m_p2);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD v_1_0(q2.m_p1 - q2.m_p0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double a = norm2(v_1_0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (isAlmostZero(a)) return (std::numeric_limits<double>::max)();</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  a = 1.0 / sqrt(a);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double b = cross(v_1_0 * a, q2.m_p2 - q2.m_p0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return 0.5 * signum * b / a;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TQuadratic::getLength(double t0, double t1) const {
Shinya Kitaoka 120a6e
  TQuadraticLengthEvaluator lengthEval(*this);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t0 = min(max(0.0, t0), 1.0);  // backward compatibility
Shinya Kitaoka 120a6e
  t1 = min(max(0.0, t1), 1.0);  // backward compatibility
Shinya Kitaoka 120a6e
  if (t0 > t1) std::swap(t0, t1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (t0 > 0.0) return lengthEval.getLengthAt(t1) - lengthEval.getLengthAt(t0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return lengthEval.getLengthAt(t1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TQuadratic::getApproximateLength(double t0, double t1,
Shinya Kitaoka 120a6e
                                        double error) const {
Shinya Kitaoka 120a6e
  if (t0 == t1) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  t0 = min(max(0.0, t0), 1.0);
Shinya Kitaoka 120a6e
  t1 = min(max(0.0, t1), 1.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (t0 > t1) std::swap(t0, t1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TQuadratic q;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (t0 == 0.0 && t1 == 1.0)
Shinya Kitaoka 120a6e
    q = *this;
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TQuadratic q1;
Shinya Kitaoka 120a6e
    split(t0, q, q1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    assert(t0 != 1.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    double newPar = (t1 - t0) / (1.0 - t0);
Shinya Kitaoka 120a6e
    q1.split(newPar, q, q1);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double step = computeStep(q, error);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double length = 0.0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD p1 = q.getP0();
Shinya Kitaoka 120a6e
  TPointD p2;
Shinya Kitaoka 120a6e
  for (double t = step; t < 1.0; t += step) {
Shinya Kitaoka 120a6e
    p2 = q.getPoint(t);
Shinya Kitaoka 120a6e
    length += tdistance(p1, p2);
Shinya Kitaoka 120a6e
    p1 = p2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  length += tdistance(p1, q.getP2());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return length;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TQuadratic::getX(double y, double &x0, double &x1) const {
Shinya Kitaoka 120a6e
  int ret = 0;
Shinya Kitaoka 120a6e
  double t;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (y > getBBox().y1 || y < getBBox().y0) return 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double a      = getP0().y - 2 * getP1().y + getP2().y;
Shinya Kitaoka 120a6e
  double half_b = getP1().y - getP0().y;
Shinya Kitaoka 120a6e
  double c      = getP0().y - y;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (a == 0)  // segment
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    if (half_b == 0)  // horizontal segment, or point
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      if (c == 0) {
Shinya Kitaoka 120a6e
        x0 = getP0().x;
Shinya Kitaoka 120a6e
        x1 = getP2().x;
Shinya Kitaoka 120a6e
        return 2;
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        return 0;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      t = -c / (2 * half_b);
Shinya Kitaoka 120a6e
      if (t >= 0 && t <= 1) {
Shinya Kitaoka 120a6e
        x0 = getPoint(t).x;
Shinya Kitaoka 120a6e
        return 1;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double discr = half_b * half_b - a * c;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (discr < 0) return 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double coeff  = 1.0 / a;
Shinya Kitaoka 120a6e
  double coeff1 = -half_b * coeff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (discr == 0) {
Shinya Kitaoka 120a6e
    t = coeff1;
Shinya Kitaoka 120a6e
    if (t >= 0 && t <= 1) {
Shinya Kitaoka 120a6e
      ret = 2;
Shinya Kitaoka 120a6e
      x0 = x1 = getPoint(t).x;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    discr = sqrt(discr) * coeff;
Shinya Kitaoka 120a6e
    t     = coeff1 + discr;
Shinya Kitaoka 120a6e
    if (t >= 0 && t <= 1) {
Shinya Kitaoka 120a6e
      ret++;
Shinya Kitaoka 120a6e
      x0 = getPoint(t).x;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    t = coeff1 - discr;
Shinya Kitaoka 120a6e
    if (t >= 0 && t <= 1) {
Shinya Kitaoka 120a6e
      ret++;
Shinya Kitaoka 120a6e
      if (ret == 2)
Shinya Kitaoka 120a6e
        x1 = getPoint(t).x;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        x0 = getPoint(t).x;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return ret;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
int TQuadratic::getY(double y, double &y0, double &y1) const {
Shinya Kitaoka 120a6e
  TQuadratic temp(*this);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  swap(temp.m_p0.x, temp.m_p0.y);
Shinya Kitaoka 120a6e
  swap(temp.m_p1.x, temp.m_p1.y);
Shinya Kitaoka 120a6e
  swap(temp.m_p2.x, temp.m_p2.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return temp.getX(y, y0, y1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//=============================================================================
Shinya Kitaoka 120a6e
TPointD TCubic::getPoint(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return m_p0 * s * s * s + 3 * t * s * (s * m_p1 + t * m_p2) +
Shinya Kitaoka 120a6e
         t * t * t * m_p3;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
TPointD TCubic::getSpeed(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return 3.0 * ((m_p1 - m_p0) * s * s + 2 * (m_p2 - m_p0) * s * t +
Shinya Kitaoka 120a6e
                (m_p3 - m_p2) * t * t);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickQuadratic::TThickQuadratic()
Shinya Kitaoka 120a6e
    : TQuadratic(), m_thickP0(0), m_thickP1(0), m_thickP2(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickQuadratic::TThickQuadratic(const TQuadratic &q)
Shinya Kitaoka 120a6e
    : TQuadratic(q), m_thickP0(0.0), m_thickP1(0.0), m_thickP2(0.0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickQuadratic::TThickQuadratic(const TPointD &p0, double thickP0,
Shinya Kitaoka 120a6e
                                 const TPointD &p1, double thickP1,
Shinya Kitaoka 120a6e
                                 const TPointD &p2, double thickP2)
Shinya Kitaoka 120a6e
    : TQuadratic(p0, p1, p2)
Shinya Kitaoka 120a6e
    , m_thickP0(thickP0)
Shinya Kitaoka 120a6e
    , m_thickP1(thickP1)
Shinya Kitaoka 120a6e
    , m_thickP2(thickP2) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickQuadratic::TThickQuadratic(const TThickPoint &p0, const TThickPoint &p1,
Shinya Kitaoka 120a6e
                                 const TThickPoint &p2)
Shinya Kitaoka 120a6e
    : TQuadratic(TPointD(p0.x, p0.y), TPointD(p1.x, p1.y), TPointD(p2.x, p2.y))
Shinya Kitaoka 120a6e
    , m_thickP0(p0.thick)
Shinya Kitaoka 120a6e
    , m_thickP1(p1.thick)
Shinya Kitaoka 120a6e
    , m_thickP2(p2.thick) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickQuadratic::TThickQuadratic(const TThickQuadratic &thickQuadratic)
Shinya Kitaoka 120a6e
    : TQuadratic(thickQuadratic)
Shinya Kitaoka 120a6e
    , m_thickP0(thickQuadratic.m_thickP0)
Shinya Kitaoka 120a6e
    , m_thickP1(thickQuadratic.m_thickP1)
Shinya Kitaoka 120a6e
    , m_thickP2(thickQuadratic.m_thickP2) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThickQuadratic::setThickP0(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p0      = p;
Shinya Kitaoka 120a6e
  m_thickP0 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThickQuadratic::setThickP1(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p1      = p;
Shinya Kitaoka 120a6e
  m_thickP1 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThickQuadratic::setThickP2(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p2      = p;
Shinya Kitaoka 120a6e
  m_thickP2 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
TThickPoint TThickQuadratic::getThickPoint(double t) const {
Shinya Kitaoka 120a6e
  double s = 1 - t;
Shinya Kitaoka 120a6e
  return TThickPoint(
Shinya Kitaoka 120a6e
      m_p0 * s * s + 2 * t * s * m_p1 + t * t * m_p2,
Shinya Kitaoka 120a6e
      m_thickP0 * s * s + 2 * t * s * m_thickP1 + t * t * m_thickP2);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThickQuadratic::split(double t, TThickQuadratic &left,
Shinya Kitaoka 120a6e
                            TThickQuadratic &right) const {
Shinya Kitaoka 120a6e
  double dt;
Shinya Kitaoka 120a6e
  TPointD p;
Shinya Kitaoka 120a6e
  dt = 1.0 - t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // control points
Shinya Kitaoka 120a6e
  left.m_p0  = m_p0;
Shinya Kitaoka 120a6e
  right.m_p2 = m_p2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_p1  = dt * m_p0 + t * m_p1;
Shinya Kitaoka 120a6e
  right.m_p1 = dt * m_p1 + t * m_p2;
Shinya Kitaoka 120a6e
  p          = dt * left.m_p1 + t * right.m_p1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_p2 = right.m_p0 = p;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // thick points
Shinya Kitaoka 120a6e
  left.m_thickP0  = m_thickP0;
Shinya Kitaoka 120a6e
  right.m_thickP2 = m_thickP2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_thickP1  = dt * m_thickP0 + t * m_thickP1;
Shinya Kitaoka 120a6e
  right.m_thickP1 = dt * m_thickP1 + t * m_thickP2;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // store thickness of intermediary point
Shinya Kitaoka 120a6e
  p.x = dt * left.m_thickP1 + t * right.m_thickP1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  left.m_thickP2 = right.m_thickP0 = p.x;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRectD TThickQuadratic::getBBox() const {
Shinya Kitaoka 120a6e
  TRectD bBox = TQuadratic::getBBox();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double maxRadius = std::max({m_thickP0, m_thickP1, m_thickP2});
Shinya Kitaoka 120a6e
  if (maxRadius > 0) {
Shinya Kitaoka 120a6e
    //  bBox.enlarge(maxRadius) si comporta male nel caso bBox.isEmpty()
Shinya Kitaoka 120a6e
    bBox.x0 -= maxRadius;
Shinya Kitaoka 120a6e
    bBox.y0 -= maxRadius;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    bBox.x1 += maxRadius;
Shinya Kitaoka 120a6e
    bBox.y1 += maxRadius;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return bBox;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ============================================================================
Toshihiro Shimizu 890ddd
// Methods of the class TThickCubic
Toshihiro Shimizu 890ddd
// ============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickCubic::TThickCubic()
Shinya Kitaoka 120a6e
    : TCubic(), m_thickP0(0), m_thickP1(0), m_thickP2(0), m_thickP3(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickCubic::TThickCubic(const TPointD &p0, double thickP0, const TPointD &p1,
Shinya Kitaoka 120a6e
                         double thickP1, const TPointD &p2, double thickP2,
Shinya Kitaoka 120a6e
                         const TPointD &p3, double thickP3)
Shinya Kitaoka 120a6e
    : TCubic(p0, p1, p2, p3)
Shinya Kitaoka 120a6e
    , m_thickP0(thickP0)
Shinya Kitaoka 120a6e
    , m_thickP1(thickP1)
Shinya Kitaoka 120a6e
    , m_thickP2(thickP2)
Shinya Kitaoka 120a6e
    , m_thickP3(thickP3) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickCubic::TThickCubic(const TThickPoint &p0, const TThickPoint &p1,
Shinya Kitaoka 120a6e
                         const TThickPoint &p2, const TThickPoint &p3)
Shinya Kitaoka 120a6e
    : TCubic(TPointD(p0.x, p0.y), TPointD(p1.x, p1.y), TPointD(p2.x, p2.y),
Shinya Kitaoka 120a6e
             TPointD(p3.x, p3.y))
Shinya Kitaoka 120a6e
    , m_thickP0(p0.thick)
Shinya Kitaoka 120a6e
    , m_thickP1(p1.thick)
Shinya Kitaoka 120a6e
    , m_thickP2(p2.thick)
Shinya Kitaoka 120a6e
    , m_thickP3(p3.thick) {}
Toshihiro Shimizu 890ddd
//  tonino ***************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickCubic::TThickCubic(const T3DPointD &p0, const T3DPointD &p1,
Shinya Kitaoka 120a6e
                         const T3DPointD &p2, const T3DPointD &p3)
Shinya Kitaoka 120a6e
    : TCubic(TPointD(p0.x, p0.y), TPointD(p1.x, p1.y), TPointD(p2.x, p2.y),
Shinya Kitaoka 120a6e
             TPointD(p3.x, p3.y))
Shinya Kitaoka 120a6e
    , m_thickP0(p0.z)
Shinya Kitaoka 120a6e
    , m_thickP1(p1.z)
Shinya Kitaoka 120a6e
    , m_thickP2(p2.z)
Shinya Kitaoka 120a6e
    , m_thickP3(p3.z) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  tonino ***************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TThickCubic::TThickCubic(const TThickCubic &thickCubic)
Shinya Kitaoka 120a6e
    : TCubic(thickCubic)
Shinya Kitaoka 120a6e
    , m_thickP0(thickCubic.m_thickP0)
Shinya Kitaoka 120a6e
    , m_thickP1(thickCubic.m_thickP1)
Shinya Kitaoka 120a6e
    , m_thickP2(thickCubic.m_thickP2)
Shinya Kitaoka 120a6e
    , m_thickP3(thickCubic.m_thickP3) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThickCubic::setThickP0(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p0.x    = p.x;
Shinya Kitaoka 120a6e
  m_p0.y    = p.y;
Shinya Kitaoka 120a6e
  m_thickP0 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThickCubic::setThickP1(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p1.x    = p.x;
Shinya Kitaoka 120a6e
  m_p1.y    = p.y;
Shinya Kitaoka 120a6e
  m_thickP1 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThickCubic::setThickP2(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p2.x    = p.x;
Shinya Kitaoka 120a6e
  m_p2.y    = p.y;
Shinya Kitaoka 120a6e
  m_thickP2 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void TThickCubic::setThickP3(const TThickPoint &p) {
Shinya Kitaoka 120a6e
  m_p3.x    = p.x;
Shinya Kitaoka 120a6e
  m_p3.y    = p.y;
Shinya Kitaoka 120a6e
  m_thickP3 = p.thick;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TThickPoint TThickCubic::getThickPoint(double t) const {
Shinya Kitaoka 120a6e
  double thick_l1, thick_h, thick_r3;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double s = 1.0 - t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD l1(m_p0 * s + m_p1 * t);
Shinya Kitaoka 120a6e
  thick_l1 = m_thickP0 * s + m_thickP1 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD h(m_p1 * s + m_p2 * t);
Shinya Kitaoka 120a6e
  thick_h = m_thickP1 * s + m_thickP2 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD r3(m_p2 * s + m_p3 * t);
Shinya Kitaoka 120a6e
  thick_r3 = m_thickP2 * s + m_thickP3 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // adesso riutilizzo le variabili gia' utilizzate
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // l2
Shinya Kitaoka 120a6e
  l1       = l1 * s + h * t;
Shinya Kitaoka 120a6e
  thick_l1 = thick_l1 * s + thick_h * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // r1
Shinya Kitaoka 120a6e
  r3       = h * s + r3 * t;
Shinya Kitaoka 120a6e
  thick_r3 = thick_h * s + thick_r3 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // l3-r0
Shinya Kitaoka 120a6e
  h       = l1 * s + r3 * t;
Shinya Kitaoka 120a6e
  thick_h = thick_l1 * s + thick_r3 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TThickPoint(h, thick_h);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TThickCubic::split(double t, TThickCubic &first,
Shinya Kitaoka 120a6e
                        TThickCubic &second) const {
Shinya Kitaoka 120a6e
  double s = 1.0 - t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD H(m_p1 * s + m_p2 * t);
Shinya Kitaoka 120a6e
  double thick_h = m_thickP1 * s + m_thickP2 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p0      = m_p0;
Shinya Kitaoka 120a6e
  first.m_thickP0 = m_thickP0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p1      = m_p0 * s + m_p1 * t;
Shinya Kitaoka 120a6e
  first.m_thickP1 = m_thickP0 * s + m_thickP1 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p2      = first.m_p1 * s + H * t;
Shinya Kitaoka 120a6e
  first.m_thickP2 = first.m_thickP1 * s + thick_h * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  second.m_p3      = m_p3;
Shinya Kitaoka 120a6e
  second.m_thickP3 = m_thickP3;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  second.m_p2      = m_p2 * s + m_p3 * t;
Shinya Kitaoka 120a6e
  second.m_thickP2 = m_thickP2 * s + m_thickP3 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  second.m_p1      = H * s + second.m_p2 * t;
Shinya Kitaoka 120a6e
  second.m_thickP1 = thick_h * s + second.m_thickP2 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  first.m_p3      = first.m_p2 * s + second.m_p1 * t;
Shinya Kitaoka 120a6e
  first.m_thickP3 = first.m_thickP2 * s + second.m_thickP1 * t;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  second.m_p0      = first.m_p3;
Shinya Kitaoka 120a6e
  second.m_thickP0 = first.m_thickP3;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TQuadratic &curve) {
Shinya Kitaoka 120a6e
  return out << "Q{" << curve.getP0() << ", " << curve.getP1() << ", "
Shinya Kitaoka 120a6e
             << curve.getP2() << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TCubic &curve) {
Shinya Kitaoka 120a6e
  return out << "C{" << curve.getP0() << ", " << curve.getP1() << ", "
Shinya Kitaoka 120a6e
             << curve.getP2() << ", " << curve.getP3() << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TThickSegment &segment) {
Shinya Kitaoka 120a6e
  return out << "TS{" << segment.getThickP0() << ", " << segment.getThickP1()
Shinya Kitaoka 120a6e
             << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TThickQuadratic &tq) {
Shinya Kitaoka 120a6e
  return out << "TQ{" << tq.getThickP0() << ", " << tq.getThickP1() << ", "
Shinya Kitaoka 120a6e
             << tq.getThickP2() << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ostream &operator<<(ostream &out, const TThickCubic &tc) {
Shinya Kitaoka 120a6e
  return out << "TC{" << tc.getThickP0() << ", " << tc.getThickP1() << ", "
Shinya Kitaoka 120a6e
             << tc.getThickP2() << ", " << tc.getThickP3() << "}";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//  End Of File
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------