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