Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgraphics.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "Tw/Gf.h"
Toshihiro Shimizu 890ddd
#include "tvis.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGraphics::TGraphics(_TWIDGET *_gf, int ras_x0, int ras_y0, int ras_x1,
Shinya Kitaoka 120a6e
                     int ras_y1, int gf_x0, int gf_y0, int gf_x1, int gf_y1,
Shinya Kitaoka 120a6e
                     int zoom_level)
Shinya Kitaoka 120a6e
    : gf(_gf)
Shinya Kitaoka 120a6e
    , currentPoint(0, 0)
Shinya Kitaoka 120a6e
    , gfRegion(gf_x0, gf_y0, gf_x1, gf_y1)
Shinya Kitaoka 120a6e
    , rasterRegion(ras_x0 - 1, ras_y0 - 1, ras_x1 + 1, ras_y1 + 1)
Shinya Kitaoka 120a6e
    //, rasterRegion(ras_x0,ras_y0,ras_x1,ras_y1)
Shinya Kitaoka 120a6e
    , zoomFactor(1)
Shinya Kitaoka 120a6e
    , pixelSize(1) {
Shinya Kitaoka 120a6e
  double dx, dy;
Shinya Kitaoka 120a6e
  int blx, bly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (zoom_level > 0)
Shinya Kitaoka 120a6e
    zoomFactor = 1 << zoom_level;
Shinya Kitaoka 120a6e
  else if (zoom_level < 0)
Shinya Kitaoka 120a6e
    zoomFactor = 1.0 / (1 << -zoom_level);
Shinya Kitaoka 120a6e
  pixelSize    = 1.0 / zoomFactor;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  blx = ras_x0 - gf_x0 / zoomFactor;
Shinya Kitaoka 120a6e
  bly = ras_y0 - gf_y0 / zoomFactor;
Shinya Kitaoka 120a6e
  dx  = 0.5 - blx;
Shinya Kitaoka 120a6e
  dy  = 0.5 - bly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  GfPushMatrix(gf);
Shinya Kitaoka 120a6e
  GfTranslate(gf, -0.5, -0.5);
Shinya Kitaoka 120a6e
  GfScale(gf, zoomFactor, zoomFactor);
Shinya Kitaoka 120a6e
  GfTranslate(gf, dx, dy);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGraphics::~TGraphics() { GfPopMatrix(gf); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::setColor(int r, int g, int b) {
Shinya Kitaoka 120a6e
  _r = r;
Shinya Kitaoka 120a6e
  _g = g;
Shinya Kitaoka 120a6e
  _b = b;
Shinya Kitaoka 120a6e
  GfCpack(gf, r, g, b);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawLine(const TPointI &a, const TPointI &b) {
Shinya Kitaoka 120a6e
  if (a.x < rasterRegion.x0 && b.x < rasterRegion.x0 ||
Shinya Kitaoka 120a6e
      a.x > rasterRegion.x1 && b.x > rasterRegion.x1 ||
Shinya Kitaoka 120a6e
      a.y < rasterRegion.y0 && b.y < rasterRegion.y0 ||
Shinya Kitaoka 120a6e
      a.y > rasterRegion.y1 && b.y > rasterRegion.y1)
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  int v[2];
Shinya Kitaoka 120a6e
  GfBgnLine(gf);
Shinya Kitaoka 120a6e
  v[0] = a.x;
Shinya Kitaoka 120a6e
  v[1] = a.y;
Shinya Kitaoka 120a6e
  GfV2i(gf, v);
Shinya Kitaoka 120a6e
  v[0] = b.x;
Shinya Kitaoka 120a6e
  v[1] = b.y;
Shinya Kitaoka 120a6e
  GfV2i(gf, v);
Shinya Kitaoka 120a6e
  GfEndLine(gf);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawLine(const TPointD &a, const TPointD &b) {
Shinya Kitaoka 120a6e
  if (a.x < rasterRegion.x0 && b.x < rasterRegion.x0 ||
Shinya Kitaoka 120a6e
      a.x > rasterRegion.x1 && b.x > rasterRegion.x1 ||
Shinya Kitaoka 120a6e
      a.y < rasterRegion.y0 && b.y < rasterRegion.y0 ||
Shinya Kitaoka 120a6e
      a.y > rasterRegion.y1 && b.y > rasterRegion.y1)
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  double v[2];
Shinya Kitaoka 120a6e
  GfBgnLine(gf);
Shinya Kitaoka 120a6e
  v[0] = a.x;
Shinya Kitaoka 120a6e
  v[1] = a.y;
Shinya Kitaoka 120a6e
  GfV2d(gf, v);
Shinya Kitaoka 120a6e
  v[0] = b.x;
Shinya Kitaoka 120a6e
  v[1] = b.y;
Shinya Kitaoka 120a6e
  GfV2d(gf, v);
Shinya Kitaoka 120a6e
  GfEndLine(gf);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::beginPolygon() { GfBgnPolygon(gf); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::endPolygon() { GfEndPolygon(gf); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::beginLine() { GfBgnLine(gf); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::endLine() { GfEndLine(gf); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::vertex(const TPointI &a) {
Shinya Kitaoka 120a6e
  int v[2];
Shinya Kitaoka 120a6e
  v[0] = a.x;
Shinya Kitaoka 120a6e
  v[1] = a.y;
Shinya Kitaoka 120a6e
  GfV2i(gf, v);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::vertex(const TPointD &a) {
Shinya Kitaoka 120a6e
  double v[2];
Shinya Kitaoka 120a6e
  v[0] = a.x;
Shinya Kitaoka 120a6e
  v[1] = a.y;
Shinya Kitaoka 120a6e
  GfV2d(gf, v);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawRect(const TPointI &a, const TPointI &b) {
Shinya Kitaoka 120a6e
  GfRecti(gf, a.x, a.y, b.x, b.y);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawRect(const TPointD &a, const TPointD &b) {
Shinya Kitaoka 120a6e
  GfRect(gf, a.x, a.y, b.x, b.y);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawRect(const TRectI &rect) {
Shinya Kitaoka 120a6e
  GfRecti(gf, rect.x0, rect.y0, rect.x1, rect.y1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawRect(const TRectD &rect) {
Shinya Kitaoka 120a6e
  GfRect(gf, rect.x0, rect.y0, rect.x1, rect.y1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawArc(const TBezierArc &arc) {
Shinya Kitaoka 120a6e
  int n = 50;
Shinya Kitaoka 120a6e
  beginLine();
Shinya Kitaoka 120a6e
  for (int i = 0; i <= n; i++) vertex(arc.getPoint((double)i / (double)n));
Shinya Kitaoka 120a6e
  endLine();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawArc(const TCubicCurve &arc) {
Shinya Kitaoka 120a6e
  int n = 80;
Shinya Kitaoka 120a6e
  beginLine();
Shinya Kitaoka 120a6e
  for (int i = 0; i <= n; i++) vertex(arc.getPoint((double)i / (double)n));
Shinya Kitaoka 120a6e
  endLine();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TGraphics::drawArcTo(const TPointD &d1, const TPointD &d2,
Shinya Kitaoka 120a6e
                          const TPointD &d3) {
Shinya Kitaoka 120a6e
  TPointD oldPoint = currentPoint;
Shinya Kitaoka 120a6e
  currentPoint += d1 + d2 + d3;
Shinya Kitaoka 120a6e
  drawArc(
Shinya Kitaoka 120a6e
      TBezierArc(oldPoint, oldPoint + d1, oldPoint + d1 + d2, currentPoint));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawDiamond(const TPointD &p, double r) {
Shinya Kitaoka 120a6e
  beginPolygon();
Shinya Kitaoka 120a6e
  vertex(p + TPointD(r, 0));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(0, r));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(-r, 0));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(0, -r));
Shinya Kitaoka 120a6e
  endPolygon();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawCross(const TPointD &p, double r) {
Shinya Kitaoka 120a6e
  drawLine(p - TPointD(r, r), p + TPointD(r, r));
Shinya Kitaoka 120a6e
  drawLine(p - TPointD(-r, r), p + TPointD(-r, r));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawSquare(const TPointD &p, double r) {
Shinya Kitaoka 120a6e
  beginLine();
Shinya Kitaoka 120a6e
  vertex(p + TPointD(-r, -r));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(-r, r));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(r, r));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(r, -r));
Shinya Kitaoka 120a6e
  vertex(p + TPointD(-r, -r));
Shinya Kitaoka 120a6e
  endLine();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawArc(const TPointD &p0, const TPointD &p1,
Shinya Kitaoka 120a6e
                        const TPointD &p2) {
Shinya Kitaoka 120a6e
  TRectD rect = convert(rasterRegion.enlarge(+10));
Shinya Kitaoka 120a6e
  // TRectD rect(rasterRegion.x0, rasterRegion.y0, rasterRegion.x1,
Shinya Kitaoka 120a6e
  // rasterRegion.y1);
Shinya Kitaoka 120a6e
  TRectD bBox = boundingBox(p0, p1, p2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!rect.overlaps(bBox)) {
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
unsigned char tmp_r = _r;
Shinya Kitaoka 120a6e
unsigned char tmp_g = _g;
Shinya Kitaoka 120a6e
unsigned char tmp_b = _b;
Shinya Kitaoka 120a6e
setColor(100,100,100);
Shinya Kitaoka 120a6e
drawRect(bBox);
Shinya Kitaoka 120a6e
drawLine(TLineD(bBox.x0, bBox.y0, bBox.x1, bBox.y1));
Shinya Kitaoka 120a6e
drawLine(TLineD(bBox.x0, bBox.y1, bBox.x1, bBox.y0));
Shinya Kitaoka 120a6e
setColor(tmp_r, tmp_g, tmp_b);
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double threshold = pixelSize * 0.125;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD v = p2 - p0;
Shinya Kitaoka 120a6e
  TPointD u = p1 - p0;
Shinya Kitaoka 120a6e
  TPointD r = rotate90(v);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double sqr_tsh = (threshold * threshold) * (v * v);
Shinya Kitaoka 120a6e
  double dist    = r * u;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if ((dist * dist) > sqr_tsh) {
Shinya Kitaoka 120a6e
    TPointD l1 = 0.5 * (p0 + p1);
Shinya Kitaoka 120a6e
    TPointD r1 = 0.5 * (p1 + p2);
Shinya Kitaoka 120a6e
    TPointD l2 = 0.5 * (l1 + r1);
Shinya Kitaoka 120a6e
    drawArc(p0, l1, l2);
Shinya Kitaoka 120a6e
    drawArc(l2, r1, p2);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    beginLine();
Shinya Kitaoka 120a6e
    vertex(p0);
Shinya Kitaoka 120a6e
    vertex(p2);
Shinya Kitaoka 120a6e
    endLine();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::drawCircle(TPointD p, double radius) {
Shinya Kitaoka 120a6e
  GfCirc(gf, p.x, p.y, radius);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::fillCircle(TPointD p, double radius) {
Shinya Kitaoka 120a6e
  GfCircf(gf, p.x, p.y, radius);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::rectWrap(int wrap_pixels) { GfRectWrap(gf, wrap_pixels); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGraphics::rectWrite(int x0, int y0, int x1, int y1, void *buffer) {
Shinya Kitaoka 120a6e
  GfRectWrite(gf, x0, y0, x1, y1, buffer);
Toshihiro Shimizu 890ddd
}