4df9cd
4df9cd
4df9cd
#include "guidelineline.h"
4df9cd
4df9cd
// TnzCore includes
4df9cd
#include "tgl.h"
4df9cd
4df9cd
4df9cd
//*****************************************************************************************
4df9cd
//    TGuidelineLineBase implementation
4df9cd
//*****************************************************************************************
4df9cd
4df9cd
TGuidelineLineBase::TGuidelineLineBase(const TPointD &p0, const TPointD &p1):
4df9cd
  p0(p0), p1(p1) { }
4df9cd
4df9cd
TPointD
4df9cd
TGuidelineLineBase::calcDirection(const TPointD &p0, const TPointD &p1) {
4df9cd
  TPointD d = p1 - p0;
4df9cd
  double k = norm2(d);
362052
  return k > TConsts::epsilon*TConsts::epsilon ? d*(1.0/sqrt(k)) : TPointD();
4df9cd
}
4df9cd
4df9cd
void
4df9cd
TGuidelineLineBase::truncateInfiniteLine(const TRectD &bounds, TPointD &p0, TPointD &p1) {
4df9cd
  TPointD d = p0 - p1;
4df9cd
  TDimensionD size = bounds.getSize();
4df9cd
  if (fabs(d.x)*bounds.y0 > bounds.x0*fabs(d.y)) {
4df9cd
    // horizontal
4df9cd
    if (fabs(d.x) < TConsts::epsilon) return;
4df9cd
    double k = d.y/d.x;
4df9cd
    p1 = TPointD(bounds.x1, p0.y + k*(bounds.x1 - p0.x));
4df9cd
    p0 = TPointD(bounds.x0, p0.y + k*(bounds.x0 - p0.x));
4df9cd
  } else {
4df9cd
    // vertical
4df9cd
    if (fabs(d.y) < TConsts::epsilon) return;
4df9cd
    double k = d.x/d.y;
4df9cd
    p1 = TPointD(p0.x + k*(bounds.y1 - p0.y), bounds.y1);
4df9cd
    p0 = TPointD(p0.x + k*(bounds.y0 - p0.y), bounds.y0);
4df9cd
  }
4df9cd
}
4df9cd
4df9cd
void
4df9cd
TGuidelineLineBase::drawInliniteLine(const TPointD &p0, const TPointD &p1, bool ray, bool active) const {
249386
  TAffine4 modelview, projection;
4df9cd
  glGetDoublev(GL_MODELVIEW_MATRIX, modelview.a);
249386
  glGetDoublev(GL_PROJECTION_MATRIX, projection.a);
4df9cd
249386
  TAffine matrix = (projection*modelview).get2d();
4df9cd
  TPointD pp0 = matrix*p0;
4df9cd
  TPointD pp1 = matrix*p1;
4df9cd
  truncateInfiniteLine(TRectD(-1.0, -1.0, 1.0, 1.0), pp0, pp1);
4df9cd
249386
  double pixelSize = sqrt(tglGetPixelSize2());
4df9cd
  TAffine matrixInv = matrix.inv();
4df9cd
  drawSegment((ray ? p0 : matrixInv*pp0), matrixInv*pp1, pixelSize, active);
4df9cd
}
4df9cd
4df9cd
4df9cd
//*****************************************************************************************
4df9cd
//    TGuidelineLine implementation
4df9cd
//*****************************************************************************************
4df9cd
4df9cd
TGuidelineLine::TGuidelineLine(const TPointD &p0, const TPointD &p1):
4df9cd
  TGuidelineLineBase(p0, p1),
4df9cd
  dir(calcDirection(p0, p1)),
4df9cd
  dist(norm(p1 - p0)) { }
4df9cd
4df9cd
TTrackPoint
4df9cd
TGuidelineLine::transformPoint(const TTrackPoint &point) const {
4df9cd
  TTrackPoint p(point);
4df9cd
  p.position = p0 + dir * std::max(0.0, std::min(dist, ((p.position - p0)*dir)));
4df9cd
  return p;
4df9cd
}
4df9cd
4df9cd
void
4df9cd
TGuidelineLine::draw(bool active) const
4df9cd
  { drawSegment(p0, p1, sqrt(tglGetPixelSize2()), active); }
4df9cd
4df9cd
4df9cd
//*****************************************************************************************
4df9cd
//    TGuidelineInfiniteLine implementation
4df9cd
//*****************************************************************************************
4df9cd
4df9cd
TGuidelineInfiniteLine::TGuidelineInfiniteLine(const TPointD &p0, const TPointD &p1):
4df9cd
  TGuidelineLineBase(p0, p1),
4df9cd
  dir(calcDirection(p0, p1)) { }
4df9cd
4df9cd
TTrackPoint
4df9cd
TGuidelineInfiniteLine::transformPoint(const TTrackPoint &point) const {
4df9cd
  TTrackPoint p(point);
4df9cd
  p.position = p0 + dir * ((p.position - p0)*dir);
4df9cd
  return p;
4df9cd
}
4df9cd
4df9cd
void
4df9cd
TGuidelineInfiniteLine::draw(bool active) const
4df9cd
  { drawInliniteLine(p0, p1, false, active); }
4df9cd
4df9cd
4df9cd
//*****************************************************************************************
4df9cd
//    TGuidelineRay implementation
4df9cd
//*****************************************************************************************
4df9cd
4df9cd
TGuidelineRay::TGuidelineRay(const TPointD &p0, const TPointD &p1):
4df9cd
  TGuidelineLineBase(p0, p1),
4df9cd
  dir(calcDirection(p0, p1)) { }
4df9cd
4df9cd
TTrackPoint
4df9cd
TGuidelineRay::transformPoint(const TTrackPoint &point) const {
4df9cd
  TTrackPoint p(point);
4df9cd
  p.position = p0 + dir * std::max(0.0, ((p.position - p0)*dir));
4df9cd
  return p;
4df9cd
}
4df9cd
4df9cd
void
4df9cd
TGuidelineRay::draw(bool active) const
4df9cd
  { drawInliniteLine(p0, p1, true, active); }
4df9cd