/* Test of pswr.h and pswr_iso.h */ /* Last edited on 2003-10-12 20:27:27 by stolfi */ #include #include #include #include #include #include #include int main (int argc, char **argv); void DoEPSTests(void); void DoPSTests(void); void DoPaintings(PSStream *ps); void PlotFunc2D ( PSStream *ps, double func(double x, double y), double xc, double yc, bool bands, bool lines, double vStart, /* Synchronize levels with this value. */ double vStep, /* Spacing between levels. */ int kMin, /* Minimum isoline index. */ int kMax, /* Maximum isoline index. */ double *R, double *G, double *B ); int main (int argc, char **argv) { DoEPSTests(); DoPSTests(); return 0; } void DoEPSTests(void) { PSStream *ps = pswr_new_stream(TRUE, "testiso", NULL, NULL, 368.0, 548.0); pswr_new_page(ps, NULL); pswr_set_window ( ps, -12.0, +12.0, -18.0, +18.0, 4.00, 364.00, 4.00, 544.00, 12,36 ); DoPaintings(ps); pswr_close_stream(ps); } void DoPSTests(void) { PSStream *ps = pswr_new_stream(FALSE, "testiso", NULL, "letter", 0, 0); pswr_new_page(ps, "one"); pswr_set_window ( ps, -12.0, +12.0, -18.0, +18.0, 144.00, 504.00, 72.00, 612.00, 12,36 ); DoPaintings(ps); pswr_close_stream(ps); } double FA(double x, double y); double FB(double x, double y); void DoPaintings(PSStream *ps) { double vMin = -1.0, vMax = +1.0, vStep = 0.2, vStart = vStep/2; /* Indices of first and last isoline: */ int kMin = pswr_sup_isoline(vStart, vStep, vMin); int kMax = pswr_inf_isoline(vStart, vStep, vMax); affirm(pswr_level(vStart, vStep, kMin) >= vMin, "duh"); affirm(pswr_level(vStart, vStep, kMin-1) < vMin, "duh"); affirm(pswr_level(vStart, vStep, kMax) <= vMax, "duh"); affirm(pswr_level(vStart, vStep, kMax+1) > vMax, "duh"); /* Create standard color table (blue-white-red): */ int N; /* Number of color bands: */ double *Ra, *Ga, *Ba; pswr_make_color_table ( vStart, vStep, kMin, kMax, 0.000, 0.667, 1.000, 1.000, 1.000, 1.000, 1.000, 0.333, 0.000, &N, &Ra, &Ga, &Ba ); affirm(N == kMax - kMin + 2, "bad N"); /* Create a modified color table with some invisible colors: */ double Rb[N], Gb[N], Bb[N]; int i; /* Index of color band. */ for (i = 0; i < N; i++) { if ((i == 0) || (i == N-1)) { Rb[i] = -1.000; Gb[i] = -1.000; Bb[i] = -1.000; } else { double Y = 0.299*Ra[i] + 0.587*Ga[i] + 0.114*Ba[i]; double Rc = Ra[i]-Y, Gc = Ga[i]-Y, Bc = Ba[i]-Y; double cost = (Rc - Gc)/sqrt(2); double sint = (2*Bc - Rc - Gc)/sqrt(6); double r = ((double)i)/((double)N-1); double rot = 2*3.1415926*r; double cosw = cos(rot); double sinw = sin(rot); double sinu = sint*cosw + sinw*cost; double cosu = cost*cosw - sint*sinw; double Rd = + cosu/sqrt(2) - sinu/sqrt(6); double Gd = - cosu/sqrt(2) - sinu/sqrt(6); double Bd = 2*sinu/sqrt(6); Rb[i] = Rd+0.8*Y; Gb[i] = Gd+0.8*Y; Bb[i] = Bd+0.8*Y; } } pswr_set_pen(ps, 0.800, 0.700, 0.600, 0.10, 0.0, 0.0); pswr_grid_lines(ps); pswr_comment(ps, "Isolines only:"); PlotFunc2D(ps, FB, -11.0, -17.0, FALSE, TRUE, vStart,vStep, kMin,kMax, Ra,Ga,Ba); pswr_comment(ps, "Color bands only:"); PlotFunc2D(ps, FB, -11.0, -5.0, TRUE, FALSE, vStart,vStep, kMin,kMax, Ra,Ga,Ba); pswr_comment(ps, "Color bands and isolines:"); PlotFunc2D(ps, FB, -11.0, +7.0, TRUE, TRUE, vStart,vStep, kMin,kMax, Rb,Gb,Bb); pswr_comment(ps, "Isolines only:"); PlotFunc2D(ps, FA, +1.0, -17.0, FALSE, TRUE, vStart,vStep, kMin,kMax, Ra,Ga,Ba); pswr_comment(ps, "Color bands with invisible colors:"); PlotFunc2D(ps, FA, +1.0, -5.0, TRUE, FALSE, vStart,vStep, kMin,kMax, Ra,Ga,Ba); pswr_comment(ps, "Color bands with invisible colors and isolines:"); PlotFunc2D(ps, FA, +1.0, +7.0, TRUE, TRUE, vStart,vStep, kMin,kMax, Rb,Gb,Bb); } void PlotFunc2D ( PSStream *ps, double func(double x, double y), double xc, double yc, bool bands, bool lines, double vStart, /* Synchronize levels with this value. */ double vStep, /* Spacing between levels. */ int kMin, /* Minimum isoline index. */ int kMax, /* Maximum isoline index. */ double *R, double *G, double *B ) { double wx = 10.0; double wy = 10.0; /* Usable area {[0 _ wx]×[0 _ wy]} */ pswr_set_pen(ps, 0.000, 0.000, 0.000, 0.20, 0.0, 0.0); pswr_rectangle(ps, -0.25+xc, +0.25+wx+xc, -0.25+yc, +0.25+wy+yc, FALSE, TRUE); int nx = 20; int ny = 20; double dx = 1.0/((double)nx); double dy = 1.0/((double)ny); pswr_set_pen(ps, 0.000, 0.000, 0.333, 0.10, 0.0, 0.0); int ix, iy; for (ix = 0; ix < nx; ix++) { for (iy = 0; iy < ny; iy++) { double x0 = (ix + 0.0001)*dx, x1 = x0 + 0.9999*dx; double y0 = (iy + 0.0001)*dy, y1 = y0 + 0.9999*dy; double f00 = func(x0,y0); double f01 = func(x0,y1); double f10 = func(x1,y0); double f11 = func(x1,y1); if (bands) { pswr_bands_in_quadrilateral ( ps, wx*x0+xc, wy*y0+yc, f00, wx*x0+xc, wy*y1+yc, f01, wx*x1+xc, wy*y0+yc, f10, wx*x1+xc, wy*y1+yc, f11, vStart, vStep, kMin, kMax, R, G, B ); } if (lines) { pswr_isolines_in_quadrilateral ( ps, wx*x0+xc, wy*y0+yc, f00, wx*x0+xc, wy*y1+yc, f01, wx*x1+xc, wy*y0+yc, f10, wx*x1+xc, wy*y1+yc, f11, vStart, vStep, kMin, kMax ); } } } } double FA(double x, double y) { x -= 0.5; y -= 0.5; double r2 = sqrt(x*x + y*y + 0.001); return sin(r2*4*3.1415926) + 4.0*x*y; } double FB(double x, double y) { double den = (0.1 - 0.5)*(0.1 - sqrt(0.85)); return (x - sqrt(0.85))*(y - 0.5)/den; }