-typedef struct device_st DEVICE;
-typedef struct charsp_st CHARSPEC;
-typedef struct state_st GRFSTATE;
-
-struct point {double x, y, z;};
-
-
-/* Constants */
-
-/* Flagcodes for motion directions */
-#define XPLUS 001 /* right */
-#define XMINUS 002 /* left */
-#define YPLUS 004 /* up */
-#define YMINUS 010 /* down */
-
-/* Codes for marker symbols */
-#define POINT 0 /* small dot */
-#define SQUARE 1 /* empty square */
-#define FSQUARE 2 /* filled square */
-#define DIAMOND 3 /* empty diamond */
-#define FDIAMOND 4 /* filled diamond */
-#define CROSS 5 /* cross */
-#define XCROSS 6 /* x */
-#define CERCLE 7 /* open circle */
-#define FCERCLE 8 /* filled circle */
-#define BSQUARE 9 /* big open square */
-#define BDIAMOND 10 /* big open diamond */
-
-#define NMARKERS 11 /* Number of available symbol types */
-
-/*-------------------------------------------------------------------------*/
-
-#define PSET 0 /* codes for raster merging */
-#define PRESET 1
-#define OR 2
-#define AND 3
-#define XOR 4
-
-/*-------------------------------------------------------------------------*/
-
-/* circle.cpp */
-void sgp2_draw_circle(const double r);
-void sgp2_draw_arc(double start, double stop, const double r);
-
-/* ctm.cpp */
-void ctm_xlat_pre_2(double x, double y);
-void ctm_xlat_post_2(double x, double y);
-void ctm_scale_pre_2(double sx, double sy);
-void ctm_scale_post_2(double sx, double sy);
-void ctm_rotate_pre_2(double theta);
-void ctm_rotate_post_2(double theta);
-void ctm_shear_pre_2(double shrx, double shry);
-void ctm_shear_post_2(double shrx, double shry);
-void xlat_gmtx_2(GRFMTX_2D m, double x, double y);
-void scale_gmtx_2(GRFMTX_2D m, double sx, double sy);
-void shear_gmtx_2(GRFMTX_2D m, double shrx, double shry);
-void rotate_gmtx_2(GRFMTX_2D m, double theta);
-void ident_gmtx_2(GRFMTX_2D m);
-void mult_gmtx_2(GRFMTX_2D a, GRFMTX_2D b, GRFMTX_2D c);
-void invert_gmtx_2(GRFMTX_2D a, GRFMTX_2D b);
-double determ_gmtx_2(GRFMTX_2D a);
-
-/* drawbox.cpp */
-void sgp2_draw_rect (double xmin, double ymin, double xmax, double ymax);
-
-/* sgp.cpp */
-SGP_ID sgp2_init (int xsize, int ysize, const char *title);
-void sgp2_close (SGP_ID gid);
-void sgp2_set_active_win (SGP_ID);
-SGP_ID sgp2_get_active_win (void);
-void sgp2_clear (void);
-void sgp2_window(double xmin, double ymin, double xmax, double ymax);
-void sgp2_viewport(double xmin, double ymin, double xmax, double ymax);
-void sgp2_frame_vpt(void);
-void calc_wc_to_ndc(void);
-void calc_ndc_to_mc(void);
-void wc_to_ndc(double xw, double yw, double *xn, double *yn);
-void ndc_to_wc(double xn, double yn, double *xw, double *yw);
-void sgp2_color(int icol);
-void sgp2_line_style(int style);
-void sgp2_line_abs(double x, double y);
-void sgp2_move_abs(double x, double y);
-void sgp2_line_rel(double x, double y);
-void sgp2_move_rel(double x, double y);
-void sgp2_draw_text(char *message);
-void sgp2_polyline_abs(double x[], double y[], int n);
-void sgp2_mark_abs(double x, double y);
-void sgp2_mark_rel(double x, double y);
-void sgp2_point_abs(double x, double y);
-void sgp2_point_rel(double x, double y);
-void charsize(double wid, double height);
-void textangle(double angle);
-void ctm_clr_2(void);
-void ctm_get_2(GRFMTX_2D m);
-void ctm_set_2(GRFMTX_2D m);
-void ctm_pre_mult_2(GRFMTX_2D m);
-void ctm_post_mult_2(GRFMTX_2D m);
-
-/* sgpdrive.cpp */
-int _sgp2_init_dev(SGP_ID gid);
-int initdevice(int dev, int mode, int xsize, int ysize);
-int opendevice(int dev);
-void closedevice(int dev);
-void termdevice(int dev);
-void _sgp2_stylus(SGP_ID gid, double x, double y, int beam);
-void pntndc (SGP_ID gid, double x, double y);
-void markndc (SGP_ID gid, double x, double y);
-GRFSTATE *inqstate(void);
-void gp_set_aspect(int dev, double asp);
-void setlinestyle(int style);
-void setlinewidth(int wid);
-DEVICE *inqdev(int dev);
-void _sgp2_set_text(SGP_ID gid, double width, double height, double textangle, int font);
-void settextclr(int fore, int back);
-void setcolor(int fore);
-void setbackg(int back);
-int initmarker(int marker, int color);
-int settextdir(int direction);
-void _sgp2_dev_text(SGP_ID gid, char *message);
-void termgrf2(void);
-void flushdevice(int dev);
-
-/* sgptext.cpp */
-void wrtsymbol(int sym, int x, int y, DEVICE *dev);
-void wrtchar(int ch, int x, int y, CHARSPEC *cspec, DEVICE *dev);
-void wrttext(char txtstr[], int x, int y, CHARSPEC *cspec, DEVICE *dev);
-void crtcolor(int mode, int *f, int *b);
+
+class SGP {
+private:
+ int m_iPhysicalXSize; // Physical Window size
+ int m_iPhysicalYSize;
+ const SGPDriver m_driver;
+
+ double xw_min; // Window extents
+ double yw_min;
+ double xw_max;
+ double yw_max;
+ double xv_min; // Viewport extents
+ double yv_min;
+ double xv_max;
+ double yv_max;
+ double viewNDC[4]; // Viewport array for clip_rect()
+
+ int m_iCurrentPhysicalX;
+ int m_iCurrentPhysicalY;
+ double m_dCurrentWorldX;
+ double m_dCurrentWorldY;
+ double m_dTextAngle;
+ bool m_bRecalcTransform;
+
+ // Master coordinates are coordinates before CTM transformation
+ // World coordinates are coordinates defined by setWindow()
+ // Normalized device coordinates range from 0. to 1. in both axes
+ TransformationMatrix2D wc_to_ndc; // World coord to NDC matrix
+ TransformationMatrix2D mc_to_ndc; // Master to NDC
+ TransformationMatrix2D ndc_to_mc; // NDC to Master
+ TransformationMatrix2D m_ctm; // Current transfromation matrix
+
+ void calc_transform ();
+
+public:
+ enum { // linestyles
+ LS_NOLINE = 0,
+ LS_SOLID = 0xffff,
+ LS_DASH1 = 0xff00,
+ LS_DASH2 = 0xf0f0,
+ LS_DASH3 = 0xcccc,
+ LS_DASH4 = 0xff3e,
+ LS_DOTTED = 0xaaaa,
+ };
+
+ enum { // Codes for marker symbols
+ MARK_POINT = 0, // small dot
+ MARK_SQUARE = 1, // empty square
+ MARK_FSQUARE = 2, // filled square
+ MARK_DIAMOND = 3, // empty diamond
+ MARK_FDIAMOND = 4, // filled diamond
+ MARK_CROSS = 5, // cross
+ MARK_XCROSS = 6, // x
+ MARK_CIRCLE = 7, // open circle
+ MARK_FCIRCLE = 8, // filled circle
+ MARK_BSQUARE = 9, // big open square
+ MARK_BDIAMOND = 10, // big open diamond
+ };
+ static const int MARK_COUNT = 11;
+ static const unsigned char MARKER_BITMAP[MARK_COUNT][5];
+
+ SGP (const SGPDriver& driver);
+
+ void drawCircle (const double r);
+ void drawArc (double start, double stop, const double r);
+ void drawRect (double xmin, double ymin, double xmax, double ymax);
+ void lineAbs(double x, double y);
+ void moveAbs(double x, double y);
+ void lineRel(double x, double y);
+ void moveRel(double x, double y);
+ void drawText(const char *szMessage);
+ void drawText(const string& rsMessage);
+ void polylineAbs(double x[], double y[], int n);
+ void markerAbs (double x, double y);
+ void markerRel(double x, double y);
+ void pointAbs(double x, double y);
+ void pointRel(double x, double y);
+
+ void eraseWindow ();
+ void setWindow (double xmin, double ymin, double xmax, double ymax);
+ void setViewport (double xmin, double ymin, double xmax, double ymax);
+ void frameViewport();
+
+ void setColor (int icol);
+ void setLineStyle (int style);
+ void setTextSize (double height);
+ void setTextAngle (double angle);
+ void setTextColor (int iFGcolor, int iBGcolor);
+ void setMarker (int idMarker, int color);
+
+ void ctmClear ();
+ void ctmSet (const TransformationMatrix2D& m);
+ void preTranslate (double x, double y);
+ void postTranslate (double x, double y);
+ void preScale (double sx, double sy);
+ void postScale (double sx, double sy);
+ void preRotate (double theta);
+ void postRotate (double theta);
+ void preShear (double shrx, double shry);
+ void postShear (double shrx, double shry);
+ void transformNDCtoMC (double* x, double* y);
+ void transformMCtoNDC (double* x, double* y);
+ void transformMCtoNDC (double xIn, double yIn, double* xOut, double* yOut);
+
+ void stylusNDC (double x, double y, bool beam);
+ void pointNDC (double x, double y);
+ void markerNDC (double x, double y);
+};