1 /*	Public domain	*/
2 
3 #ifndef _AGAR_MATH_M_PLOTTER_H_
4 #define _AGAR_MATH_M_PLOTTER_H_
5 
6 #include <agar/gui/widget.h>
7 #include <agar/gui/label.h>
8 #include <agar/gui/scrollbar.h>
9 
10 #include <agar/math/begin.h>
11 
12 #define M_PLOTTER_NDEFCOLORS	16
13 #define M_PLOTTER_LABEL_MAX	64
14 
15 struct m_plotter;
16 
17 enum m_plot_label_type {
18 	M_LABEL_X,				/* Refers to an x value */
19 	M_LABEL_Y,				/* Refers to an y value */
20 	M_LABEL_FREE,				/* Free placement in graph */
21 	M_LABEL_OVERLAY,			/* Overlay on widget */
22 };
23 
24 typedef struct m_plot_label {
25 	char text[M_PLOTTER_LABEL_MAX];	/* Label text */
26 	int  text_surface;			/* Text surface handle */
27 	enum m_plot_label_type type;		/* Packing alignment */
28 	Uint x;					/* X position (or -1) */
29 	Uint y;					/* Y position (or -1) */
30 	AG_TAILQ_ENTRY(m_plot_label) labels;
31 } M_PlotLabel;
32 
33 enum m_plot_type {
34 	M_PLOT_POINTS,		/* Individual points */
35 	M_PLOT_LINEAR,		/* Linear interpolation */
36 	M_PLOT_CUBIC_SPLINE,	/* Cubic spline interpolation */
37 	M_PLOT_VECTORS		/* Vector arrows/cones */
38 };
39 
40 enum m_plot_source {
41 	M_PLOT_MANUALLY,		/* Don't update automatically */
42 	M_PLOT_FROM_VARIABLE_VFS,	/* Object variable (VFS path) */
43 	M_PLOT_FROM_REAL,		/* M_Real variable */
44 	M_PLOT_FROM_INT,		/* Integer variable */
45 	M_PLOT_FROM_COMPONENT,		/* Single vector/matrix component */
46 	M_PLOT_DERIVATIVE		/* Derivative of other plot */
47 };
48 
49 typedef struct m_plot {
50 	enum m_plot_type type;
51 	enum m_plot_source src_type;
52 	union {
53 		struct {
54 			void *vfs;	 /* VFS root object */
55 			const char *key; /* Property path */
56 		} varVFS;
57 		M_Real *real;		/* Pointer to real value */
58 		int *integer;		/* Pointer to integer value */
59 		struct {
60 			M_Matrix *A;	/* Matrix/vector */
61 			Uint i, j;	/* Entry position */
62 		} com;
63 		struct m_plot *plot;	/* Other plot */
64 	} src;
65 	union {
66 		M_Real	*r;		/* Real points */
67 		M_Vector **v;		/* Real vectors */
68 	} data;
69 	struct m_plotter *plotter;	/* Back pointer to plotter */
70 	Uint n;				/* Number of points */
71 	Uint flags;
72 #define M_PLOT_SELECTED	0x01
73 #define M_PLOT_MOUSEOVER	0x02
74 #define M_PLOT_DRAGGING	0x04
75 #define M_PLOT_HIDDEN		0x08
76 
77 	char label_txt[32];		/* Label text */
78 	int label;			/* Label surface handle */
79 	AG_Color color;			/* Plot color */
80 	M_Real xScale, yScale;		/* Scaling factors */
81 	int xOffs, yOffs;		/* Offset in display */
82 	int xLabel, yLabel;		/* Item position */
83 	AG_TAILQ_HEAD_(m_plot_label) labels; /* User labels */
84 	AG_TAILQ_ENTRY(m_plot) plots;
85 } M_Plot;
86 
87 enum m_plotter_type {
88 	M_PLOT_2D,		/* 2D Cartesian */
89 	M_PLOT_POLAR,		/* 2D Polar */
90 	M_PLOT_SMITH,		/* Smith Chart */
91 	M_PLOT_3D,		/* 3D Cartesian */
92 	M_PLOT_SPHERICAL	/* 3D Spherical */
93 };
94 
95 typedef struct m_plotter {
96 	struct ag_widget wid;
97 	enum m_plotter_type type;
98 	Uint flags;
99 #define M_PLOTTER_HFILL	0x01
100 #define M_PLOTTER_VFILL	0x02
101 #define M_PLOTTER_EXPAND	(M_PLOTTER_HFILL|M_PLOTTER_VFILL)
102 #define M_PLOTTER_SCROLL	0x04
103 	int xMax;			/* Maximum X for single value plots */
104 	M_Real yMin, yMax;		/* Extrema for single value plots */
105 	M_Vector *vMin, *vMax;		/* Extrema for vector plots */
106 	int xOffs, yOffs;		/* Display offset */
107 	int wPre, hPre;			/* SizeHint dimensions */
108 	M_Real xScale, yScale;		/* Scaling factors */
109 	AG_Font *font;			/* Default font face (or NULL) */
110 	AG_Color colors[M_PLOTTER_NDEFCOLORS];	/* Default plot color */
111 	int curColor;				/* Current default color */
112 	AG_Scrollbar *hbar, *vbar;	/* Display scrollbars */
113 	AG_Rect r;			/* View area */
114 	AG_TAILQ_HEAD_(m_plot) plots;	/* Plots in this view */
115 } M_Plotter;
116 
117 __BEGIN_DECLS
118 extern AG_WidgetClass mPlotterClass;
119 
120 M_Plotter *M_PlotterNew(void *, Uint);
121 void       M_PlotterSizeHint(M_Plotter *, Uint, Uint);
122 void       M_PlotterUpdate(M_Plotter *);
123 void       M_PlotterSetDefaultFont(M_Plotter *, const char *, int);
124 void       M_PlotterSetDefaultColor(M_Plotter *, int, Uint8, Uint8, Uint8);
125 void       M_PlotterSetDefaultScale(M_Plotter *, M_Real, M_Real);
126 
127 M_Plot      *M_PlotNew(M_Plotter *, enum m_plot_type);
128 M_PlotLabel *M_PlotLabelNew(M_Plot *, enum m_plot_label_type, Uint, Uint,
129                             const char *, ...);
130 void         M_PlotLabelSetText(M_Plot *, M_PlotLabel *, const char *, ...);
131 M_PlotLabel *M_PlotLabelReplace(M_Plot *, enum m_plot_label_type, Uint, Uint,
132                                 const char *, ...);
133 
134 void	 M_PlotClear(M_Plot *);
135 M_Plot	*M_PlotFromReal(M_Plotter *, enum m_plot_type, const char *, M_Real *);
136 M_Plot	*M_PlotFromInt(M_Plotter *, enum m_plot_type, const char *, int *);
137 M_Plot	*M_PlotFromDerivative(M_Plotter *, enum m_plot_type, M_Plot *);
138 M_Plot	*M_PlotFromVariableVFS(M_Plotter *, enum m_plot_type, const char *,
139                                void *, const char *);
140 
141 struct ag_window *M_PlotSettings(M_Plot *);
142 
143 void	M_PlotSetColor(M_Plot *, Uint8, Uint8, Uint8);
144 void 	M_PlotSetLabel(M_Plot *, const char *, ...);
145 void 	M_PlotUpdateLabel(M_Plot *);
146 void	M_PlotSetScale(M_Plot *, M_Real, M_Real);
147 void	M_PlotSetXoffs(M_Plot *, int);
148 void	M_PlotSetYoffs(M_Plot *, int);
149 
150 void	M_PlotReal(M_Plot *, M_Real);
151 void	M_PlotRealv(M_Plot *, Uint, const M_Real *);
152 void	M_PlotVector(M_Plot *, const M_Vector *);
153 void	M_PlotVectorv(M_Plot *, Uint, const M_Vector **);
154 __END_DECLS
155 
156 #include <agar/math/close.h>
157 #endif /* _AGAR_MATH_M_PLOTTER_H_ */
158