author | fr4nko <francesco.bbt@gmail.com> | 2010年06月29日 11:48:05 +0200 |
---|---|---|
committer | fr4nko <francesco.bbt@gmail.com> | 2010年06月29日 11:48:05 +0200 |
commit | 1beceb467757384bd5695f5e7ed9acfba590bd4a (patch) | |
tree | 009f1a0df1484ebf721a93a6e3f2b1d135e4f4ba /agg-plot | |
parent | b5f50727239483643508795750262bc3ab6723ca (diff) | |
download | gsl-shell-1beceb467757384bd5695f5e7ed9acfba590bd4a.tar.gz |
-rw-r--r-- | agg-plot/Makefile | 12 | ||||
-rw-r--r-- | agg-plot/colors.cpp | 44 | ||||
-rw-r--r-- | agg-plot/colors.h | 17 | ||||
-rw-r--r-- | agg-plot/drawables.h | 30 | ||||
-rw-r--r-- | agg-plot/lua-cpp-utils.cpp | 2 | ||||
-rw-r--r-- | agg-plot/lua-cpp-utils.h | 20 | ||||
-rw-r--r-- | agg-plot/lua-draw.cpp | 415 | ||||
-rw-r--r-- | agg-plot/lua-draw.h | 48 | ||||
-rw-r--r-- | agg-plot/lua-plot-priv.h | 92 | ||||
-rw-r--r-- | agg-plot/lua-plot.cpp | 535 | ||||
-rw-r--r-- | agg-plot/lua-plot.h | 37 | ||||
-rw-r--r-- | agg-plot/plot.h | 6 | ||||
-rw-r--r-- | agg-plot/resource-manager.h | 3 | ||||
-rw-r--r-- | agg-plot/trans.h | 12 | ||||
-rw-r--r-- | agg-plot/vertex-source.h | 3 | ||||
-rw-r--r-- | agg-plot/xwin-show.cpp | 44 | ||||
-rw-r--r-- | agg-plot/xwin-show.h | 1 |
diff --git a/agg-plot/Makefile b/agg-plot/Makefile index eb7f3e16..7f5c9b0c 100644 --- a/agg-plot/Makefile +++ b/agg-plot/Makefile @@ -22,6 +22,7 @@ GSH_BASE_DIR = .. include $(GSH_BASE_DIR)/makeconfig
include $(GSH_BASE_DIR)/makeflags
+include $(GSH_BASE_DIR)/make-packages
LUADIR = $(GSH_BASE_DIR)/lua
AR= ar rcu
@@ -36,18 +37,14 @@ ifeq ($(strip $(PLATFORM)), mingw) # Option for Windows Platform
DEFS += -DWIN32
INCLUDES += -I/usr/include -I/usr/pthreads-w32/include
- AGG_HOME = /c/fra/src/agg-2.5
- AGG_CFLAGS = -I$(AGG_HOME)/include
- AGG_LIBS = -L$(AGG_HOME)/src -lagg -lgdi32 -lsupc++
PLATSUP_SRC_FILES = agg_platform_support_win32.cpp agg_win32_bmp.cpp
else
- AGG_CFLAGS = -I/usr/include/agg2
PLATSUP_SRC_FILES = agg_platform_support_x11.cpp
endif
-INCLUDES += -I$(GSH_BASE_DIR) -I$(LUADIR)/src -I$(LUADIR)/etc
+INCLUDES += $(AGG_INCLUDES) -I$(GSH_BASE_DIR) -I$(LUADIR)/src
-AGGPLOT_SRC_FILES = $(PLATSUP_SRC_FILES) utils.cpp units.cpp c-drawables.cpp colors.cpp xwin-show.cpp
+AGGPLOT_SRC_FILES = $(PLATSUP_SRC_FILES) utils.cpp units.cpp colors.cpp lua-draw.cpp lua-plot.cpp xwin-show.cpp
AGGPLOT_OBJ_FILES := $(AGGPLOT_SRC_FILES:%.cpp=%.o)
@@ -55,10 +52,9 @@ DEP_FILES := $(AGGPLOT_SRC_FILES:%.cpp=.deps/%.P) DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
-CXXCOMPILE = $(CXX) $(CXXFLAGS) $(DEFS) $(INCLUDES) $(AGG_CFLAGS)
+CXXCOMPILE = $(CXX) $(CXXFLAGS) $(DEFS) $(INCLUDES)
TARGETS = libaggplot.a
-#TARGETS = test.exe
all: $(TARGETS)
diff --git a/agg-plot/colors.cpp b/agg-plot/colors.cpp index 5a1c268e..c1ea0ea6 100644 --- a/agg-plot/colors.cpp +++ b/agg-plot/colors.cpp @@ -1,32 +1,26 @@ #include <string.h>
+#include "lua-cpp-utils.h"
#include "colors.h"
-#include "agg_color_rgba.h"
-void
-set_color_default (struct color *c)
+agg::rgba8 *
+rgba8_push_default (lua_State *L)
{
- c->r = 180;
- c->g = c->b = 0;
- c->a = 255;
+ return new(L, GS_RGBA_COLOR) agg::rgba8(180, 0, 0, 255);
}
-void
-color_lookup (struct color *c, const char *color_str)
+agg::rgba8 *
+rgba8_push_lookup (lua_State *L, const char *color_str)
{
const char *p = color_str;
- int val = 180;
-
- c->a = 255;
+ const int a = 255;
if (strcmp (p, "white") == 0)
- {
- c->r = c->g = c->b = 255;
- return;
- }
-
- c->r = c->g = c->b = 0;
+ return new(L, GS_RGBA_COLOR) agg::rgba8(255, 255, 255, a);
+
+ int val = 180;
+ int r = 0, g = 0, b = 0;
if (strncmp (p, "light", 5) == 0)
{
@@ -40,17 +34,19 @@ color_lookup (struct color *c, const char *color_str) }
if (strcmp (p, "red") == 0)
- c->r = val;
+ r = val;
else if (strcmp (p, "green") == 0)
- c->g = val;
+ g = val;
else if (strcmp (p, "blue") == 0)
- c->b = val;
+ b = val;
else if (strcmp (p, "cyan") == 0)
- c->g = c->b = val;
+ g = b = val;
else if (strcmp (p, "magenta") == 0)
- c->r = c->b = val;
+ r = b = val;
else if (strcmp (p, "yellow") == 0)
- c->r = c->g = val;
+ r = g = val;
else if (strcmp (p, "gray") == 0)
- c->r = c->g = c->b = val;
+ r = g = b = val;
+
+ return new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b, a);
}
diff --git a/agg-plot/colors.h b/agg-plot/colors.h index c2aa923b..560b6a5c 100644 --- a/agg-plot/colors.h +++ b/agg-plot/colors.h @@ -1,17 +1,14 @@ #ifndef AGGPLOT_COLORS_H
#define AGGPLOT_COLORS_H
-#include "defs.h"
-
-__BEGIN_DECLS
+extern "C" {
+#include "lua.h"
+}
-struct color {
- unsigned int r, g, b, a;
-};
-
-extern void color_lookup (struct color *c, const char *color_str);
-extern void set_color_default (struct color *c);
+#include "defs.h"
+#include "agg_color_rgba.h"
-__END_DECLS
+extern agg::rgba8 * rgba8_push_lookup(lua_State *L, const char *color_str);
+extern agg::rgba8 * rgba8_push_default(lua_State *L);
#endif
diff --git a/agg-plot/drawables.h b/agg-plot/drawables.h index aac888d3..471876ee 100644 --- a/agg-plot/drawables.h +++ b/agg-plot/drawables.h @@ -38,6 +38,7 @@ namespace my { agg::bounding_rect_single(*m_source, 0, x1, y1, x2, y2); }; + /* virtual void ref() { ref_count++; }; virtual unsigned unref() { @@ -45,6 +46,7 @@ namespace my { ref_count--; return ref_count; }; + */ }; class path : public vs_proxy<agg::path_storage> { @@ -55,40 +57,12 @@ namespace my { public: path(): vs_base(), m_path() { -#ifdef DEBUG_PLOT - fprintf(stderr, "creating path: %p\n", this); -#endif set_source(&m_path); }; -#ifdef DEBUG_PLOT - ~path() { fprintf(stderr, "freeing path: %p\n", this); }; -#endif - - agg::path_storage& get_path() { return m_path; }; }; - /* ellipse drawable */ - class ellipse : public vs_proxy<agg::ellipse> { - typedef vs_proxy<agg::ellipse> vs_base; - - agg::ellipse m_ellipse; - - public: - ellipse(double x, double y, double rx, double ry, - unsigned num_steps=0, bool cw=false): - vs_base(), m_ellipse(x, y, rx, ry, num_steps, cw) - { - set_source(&m_ellipse); - }; - - virtual void apply_transform(agg::trans_affine& m, double as) - { - m_ellipse.approximation_scale(as); - }; - }; - typedef vs_proxy<agg::conv_stroke<agg::conv_transform<agg::gsv_text> > > vs_text; /* text drawable */ diff --git a/agg-plot/lua-cpp-utils.cpp b/agg-plot/lua-cpp-utils.cpp new file mode 100644 index 00000000..2fce32bc --- /dev/null +++ b/agg-plot/lua-cpp-utils.cpp @@ -0,0 +1,2 @@ +
+#include "lua-cpp-utils.h"
diff --git a/agg-plot/lua-cpp-utils.h b/agg-plot/lua-cpp-utils.h new file mode 100644 index 00000000..df0c97bd --- /dev/null +++ b/agg-plot/lua-cpp-utils.h @@ -0,0 +1,20 @@ +#ifndef LUA_CPP_UTILS_H
+#define LUA_CPP_UTILS_H
+
+#include <new>
+
+#include "defs.h"
+__BEGIN_DECLS
+#include "lua.h"
+__END_DECLS
+
+#include "gs-types.h"
+
+inline void* operator new(size_t nbytes, lua_State *L, enum gs_type_e tp)
+{
+ void* p = lua_newuserdata(L, nbytes);
+ gs_set_metatable (L, tp);
+ return p;
+}
+
+#endif
diff --git a/agg-plot/lua-draw.cpp b/agg-plot/lua-draw.cpp new file mode 100644 index 00000000..94ff5ec5 --- /dev/null +++ b/agg-plot/lua-draw.cpp @@ -0,0 +1,415 @@ + +#include <pthread.h> + +extern "C" { +#include "lua.h" +#include "lauxlib.h" +} + +#include "lua-draw.h" +#include "gsl-shell.h" +#include "lua-cpp-utils.h" +#include "gs-types.h" +#include "trans.h" +#include "colors.h" + +pthread_mutex_t agg_mutex[1]; + +enum path_cmd_e { + CMD_ERROR = -1, + CMD_MOVE_TO = 0, + CMD_LINE_TO, + CMD_CLOSE, + CMD_ARC_TO, + CMD_CURVE3, + CMD_CURVE4, +}; + +struct cmd_call_stack { + double f[6]; + int b[2]; +}; + +struct path_cmd_reg { + enum path_cmd_e id; + const char *cmd; + const char *signature; +}; + +static int agg_obj_index (lua_State *L); +static int agg_obj_free (lua_State *L); + +static int agg_path_free (lua_State *L); +static int agg_path_index (lua_State *L); + +static int agg_text_free (lua_State *L); +static int agg_text_set_text (lua_State *L); +static int agg_text_set_point (lua_State *L); +static int agg_text_rotate (lua_State *L); + +static int agg_rgba_free (lua_State *L); + +static void path_cmd (my::path *p, int cmd, struct cmd_call_stack *stack); + +static struct path_cmd_reg cmd_table[] = { + {CMD_MOVE_TO, "move_to", "ff"}, + {CMD_LINE_TO, "line_to", "ff"}, + {CMD_CLOSE, "close", ""}, + {CMD_ARC_TO, "arc_to", "fffbbff"}, + {CMD_CURVE3, "curve3", "ffff"}, + {CMD_CURVE4, "curve4", "ffffff"}, + {CMD_ERROR, NULL, NULL} +}; + +static const struct luaL_Reg plot_functions[] = { + {"path", agg_path_new}, + {"text", agg_text_new}, + {"rgba", agg_rgba_new}, + {"rgb", agg_rgb_new}, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_obj_methods[] = { + {"__index", agg_obj_index}, + {"__gc", agg_obj_free}, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_path_methods[] = { + {"__index", agg_path_index}, + {"__gc", agg_path_free}, + {NULL, NULL} +}; + +static const struct luaL_Reg rgba_methods[] = { + {"__gc", agg_rgba_free}, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_text_methods[] = { + {"__gc", agg_text_free}, + {"set_point", agg_text_set_point}, + {"set_text", agg_text_set_text}, + {"rotate", agg_text_rotate}, + {NULL, NULL} +}; + +int +agg_path_new (lua_State *L) +{ + my::path *vs = new(L, GS_DRAW_PATH) my::path(); + + if (lua_gettop (L) >= 2) + { + double x = gs_check_number (L, 1, FP_CHECK_NORMAL); + double y = gs_check_number (L, 2, FP_CHECK_NORMAL); + struct cmd_call_stack s[1]; + + s->f[0] = x; + s->f[1] = y; + + path_cmd (vs, CMD_MOVE_TO, s); + } + + return 1; +} + +my::path * +check_agg_path (lua_State *L, int index) +{ + return (my::path *) gs_check_userdata (L, index, GS_DRAW_PATH); +} + +int +agg_path_free (lua_State *L) +{ + typedef my::path path_type; + path_type *path = check_agg_path (L, 1); + printf("freeing PATH %p\n", (void *) path); + path->~path_type(); + return 0; +} + +void +path_cmd (my::path *p, int _cmd, struct cmd_call_stack *s) +{ + agg::path_storage& ps = p->get_path(); + path_cmd_e cmd = (path_cmd_e) _cmd; + + switch (cmd) + { + case CMD_MOVE_TO: + ps.move_to (s->f[0], s->f[1]); + break; + case CMD_LINE_TO: + ps.line_to (s->f[0], s->f[1]); + break; + case CMD_CLOSE: + ps.close_polygon (); + break; + case CMD_ARC_TO: + ps.arc_to (s->f[0], s->f[1], s->f[2], s->b[0], s->b[1], s->f[3], s->f[4]); + break; + case CMD_CURVE3: + ps.curve3 (s->f[0], s->f[1], s->f[2], s->f[3]); + break; + case CMD_CURVE4: + ps.curve4 (s->f[0], s->f[1], s->f[2], s->f[3], s->f[4], s->f[5]); + break; + case CMD_ERROR: + ; + } +} + +static int +agg_path_cmd (lua_State *L) +{ + my::path *p = check_agg_path (L, 1); + int id = lua_tointeger (L, lua_upvalueindex(1)); + const char *signature = lua_tostring (L, lua_upvalueindex(2)); + int argc = 2, f_count = 0, b_count = 0; + struct cmd_call_stack s[1]; + const char *fc; + + for (fc = signature; fc[0]; fc++) + { + switch (fc[0]) + { + case 'f': + s->f[f_count++] = gs_check_number (L, argc++, FP_CHECK_NORMAL); + break; + case 'b': + if (lua_isboolean (L, argc)) + s->b[b_count++] = lua_toboolean (L, argc++); + else + return luaL_error (L, "expected boolean for argument #%i", argc); + } + } + + pthread_mutex_lock (agg_mutex); + path_cmd (p, id, s); + pthread_mutex_unlock (agg_mutex); + return 0; +} + +int +agg_path_index (lua_State *L) +{ + struct path_cmd_reg *r; + const char *key; + + if (! lua_isstring (L, 2)) + return 0; + + key = lua_tostring (L, 2); + for (r = cmd_table; r->cmd; r++) + { + if (strcmp (key, r->cmd) == 0) + break; + } + + if (r->cmd) + { + lua_pushinteger (L, (int) r->id); + lua_pushstring (L, r->signature); + lua_pushcclosure (L, agg_path_cmd, 2); + return 1; + } + + return 0; +} + +vertex_source * +check_agg_obj (lua_State *L, int index) +{ + int tplist[] = {GS_DRAW_OBJ, GS_DRAW_PATH, GS_DRAW_TEXT, GS_INVALID_TYPE}; + void *p = NULL; + int j; + + for (j = 0; tplist[j] != GS_INVALID_TYPE; j++) + { + p = gs_is_userdata (L, index, tplist[j]); + if (p) + break; + } + + if (p == NULL) + gs_type_error (L, index, "drawing object"); + + return (vertex_source *) p; +} + +int +agg_obj_free (lua_State *L) +{ + vertex_source *vs = check_agg_obj (L, 1); + printf("freeing OBJECT %p\n", (void *) vs); + vs->~vertex_source(); + return 0; +} + +static int +agg_obj_pcall (lua_State *L) +{ + int narg_out, narg_in = lua_gettop (L); + int status; + + pthread_mutex_lock (agg_mutex); + lua_pushvalue (L, lua_upvalueindex(1)); + lua_insert (L, 1); + status = lua_pcall (L, narg_in, LUA_MULTRET, 0); + pthread_mutex_unlock (agg_mutex); + if (status != 0) + { +#ifndef LUA_MODULE + error_report (L, status); +#else + luaL_error (L, "error in graphical object method"); +#endif + return 0; + } + narg_out = lua_gettop (L); + return narg_out; +} + +int +agg_obj_index (lua_State *L) +{ + vertex_source *vs = check_agg_obj (L, 1); + + lua_getmetatable (L, 1); + lua_insert (L, 2); + lua_gettable (L, 2); + + if (! lua_isnil (L, -1)) + { + lua_pushcclosure (L, agg_obj_pcall, 1); + return 1; + } + + return 0; +} + +my::text * +check_agg_text (lua_State *L, int index) +{ + return (my::text *) gs_check_userdata (L, index, GS_DRAW_TEXT); +} + +int +agg_text_new (lua_State *L) +{ + double size = luaL_optnumber (L, 1, 10.0); + double width = luaL_optnumber (L, 2, 1.0); + my::text *txt = new(L, GS_DRAW_TEXT) my::text(size, width); + return 1; +} + +int +agg_text_free (lua_State *L) +{ + typedef my::text text_type; + text_type *t = check_agg_text (L, 1); + t->~text_type(); + return 0; +} + +int +agg_text_set_text (lua_State *L) +{ + my::text *t = check_agg_text (L, 1); + const char *text = luaL_checkstring (L, 2); + t->set_text(text); + return 0; +} + +int +agg_text_set_point (lua_State *L) +{ + my::text *t = check_agg_text (L, 1); + double x = luaL_checknumber (L, 2); + double y = luaL_checknumber (L, 3); + t->start_point(x, y); + return 0; +} + +int +agg_text_rotate (lua_State *L) +{ + my::text *t = check_agg_text (L, 1); + double a = luaL_checknumber (L, 2); + t->rotate(a); + return 0; +}; + +static unsigned int double2uint8 (double x) +{ + int u = x * 255.0; + if (u > 255) + u = 255; + else if (u < 0) + u = 0; + return (unsigned int) u; +} + +agg::rgba8 * +check_agg_rgba8 (lua_State *L, int index) +{ + return (agg::rgba8 *) gs_check_userdata (L, index, GS_RGBA_COLOR); +} + +int +agg_rgba_new (lua_State *L) +{ + unsigned int r = double2uint8 (luaL_checknumber (L, 1)); + unsigned int g = double2uint8 (luaL_checknumber (L, 2)); + unsigned int b = double2uint8 (luaL_checknumber (L, 3)); + unsigned int a = double2uint8 (luaL_checknumber (L, 4)); + + new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b, a); + return 1; +} + +int +agg_rgb_new (lua_State *L) +{ + unsigned int r = double2uint8 (luaL_checknumber (L, 1)); + unsigned int g = double2uint8 (luaL_checknumber (L, 2)); + unsigned int b = double2uint8 (luaL_checknumber (L, 3)); + + new(L, GS_RGBA_COLOR) agg::rgba8(r, g, b, 255); + return 1; +} + +int +agg_rgba_free (lua_State *L) +{ + typedef agg::rgba8 rgba_t; + rgba_t *c = check_agg_rgba8 (L, 1); + c->~rgba_t(); + return 0; +} + +void +draw_register (lua_State *L) +{ + pthread_mutex_init (agg_mutex, NULL); + + luaL_newmetatable (L, GS_METATABLE(GS_DRAW_PATH)); + luaL_register (L, NULL, agg_path_methods); + lua_pop (L, 1); + + luaL_newmetatable (L, GS_METATABLE(GS_DRAW_TEXT)); + lua_pushvalue (L, -1); + lua_setfield (L, -2, "__index"); + luaL_register (L, NULL, agg_text_methods); + lua_pop (L, 1); + + luaL_newmetatable (L, GS_METATABLE(GS_DRAW_OBJ)); + luaL_register (L, NULL, agg_obj_methods); + lua_pop (L, 1); + + luaL_newmetatable (L, GS_METATABLE(GS_RGBA_COLOR)); + luaL_register (L, NULL, rgba_methods); + lua_pop (L, 1); +} diff --git a/agg-plot/lua-draw.h b/agg-plot/lua-draw.h new file mode 100644 index 00000000..d7f94550 --- /dev/null +++ b/agg-plot/lua-draw.h @@ -0,0 +1,48 @@ +#ifndef LUA_DRAW_H
+#define LUA_DRAW_H
+
+
+#include "defs.h"
+
+#include <pthread.h>
+
+__BEGIN_DECLS
+#include "lua.h"
+__END_DECLS
+
+#ifdef __cplusplus
+
+#include "vertex-source.h"
+#include "drawables.h"
+#include "trans.h"
+
+extern int agg_text_new (lua_State *L);
+extern int agg_path_new (lua_State *L);
+extern int agg_rgb_new (lua_State *L);
+extern int agg_rgba_new (lua_State *L);
+
+extern vertex_source* check_agg_obj (lua_State *L, int index);
+extern my::path* check_agg_path (lua_State *L, int index);
+extern my::text* check_agg_text (lua_State *L, int index);
+extern agg::rgba8* check_agg_rgba8 (lua_State *L, int index);
+
+#endif
+
+__BEGIN_DECLS
+
+extern pthread_mutex_t agg_mutex[1];
+
+#define AGG_LOCK() pthread_mutex_lock (agg_mutex);
+#define AGG_UNLOCK() pthread_mutex_unlock (agg_mutex);
+
+#define AGG_PROTECT(op) { \
+ pthread_mutex_lock (agg_mutex); \
+ op; \
+ pthread_mutex_unlock (agg_mutex); \
+ }
+
+extern void draw_register (lua_State *L);
+
+__END_DECLS
+
+#endif
diff --git a/agg-plot/lua-plot-priv.h b/agg-plot/lua-plot-priv.h deleted file mode 100644 index 37d55c1c..00000000 --- a/agg-plot/lua-plot-priv.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef LUA_CPLOT_PRIV_H -#define LUA_CPLOT_PRIV_H - -#include <pthread.h> - -#include "defs.h" -#include "c-drawables.h" - -__BEGIN_DECLS - -enum path_cmd_e { - CMD_ERROR = -1, - CMD_MOVE_TO = 0, - CMD_LINE_TO, - CMD_CLOSE, - CMD_ARC_TO, - CMD_CURVE3, - CMD_CURVE4, -}; - -struct cmd_call_stack { - double f[6]; - int b[2]; -}; - -struct agg_plot { - CPLOT *plot; - int lua_is_owner; - int is_shown; - void *window; -}; - -enum trans_type { - trans_end = -1, - trans_stroke = 0, - trans_curve, - trans_dash, - trans_marker, - trans_rotate, - trans_translate, -}; - -struct property_reg { - int id; - const char *name; -}; - -struct stroke_spec { - double width; - int line_cap; - int line_join; -}; - -struct dash_spec { - double len[2]; -}; - -struct marker_spec { - double size; -}; - -struct rotate_spec { - double angle; -}; - - -struct translate_spec { - double x, y; -}; - -struct trans_spec { - enum trans_type tag; - union { - struct stroke_spec stroke; - struct dash_spec dash; - struct marker_spec marker; - struct translate_spec translate; - struct rotate_spec rotate; - } content; -}; - -extern struct property_reg line_cap_properties[]; -extern struct property_reg line_join_properties[]; - -extern void agg_plot_destroy (struct agg_plot *cp); -extern int update_callback (void *_app); - -extern pthread_mutex_t agg_mutex[1]; - -__END_DECLS - -#endif diff --git a/agg-plot/lua-plot.cpp b/agg-plot/lua-plot.cpp new file mode 100644 index 00000000..d1600d30 --- /dev/null +++ b/agg-plot/lua-plot.cpp @@ -0,0 +1,535 @@ + +#include <pthread.h> + +extern "C" { +#include "lua.h" +#include "lauxlib.h" +} + +#include "agg_color_rgba.h" + +#include "lua-plot.h" +#include "lua-draw.h" +#include "lua-cpp-utils.h" +#include "lua-utils.h" +#include "gs-types.h" +#include "vertex-source.h" +#include "trans.h" +#include "colors.h" +#include "xwin-show.h" + +__BEGIN_DECLS + +static int agg_plot_new (lua_State *L); +static int agg_plot_show (lua_State *L); +static int agg_plot_add (lua_State *L); +static int agg_plot_update (lua_State *L); +static int agg_plot_add_line (lua_State *L); +static int agg_plot_index (lua_State *L); +static int agg_plot_newindex (lua_State *L); +static int agg_plot_free (lua_State *L); +static int agg_plot_title_set (lua_State *L); +static int agg_plot_title_get (lua_State *L); +static int agg_plot_units_set (lua_State *L); +static int agg_plot_units_get (lua_State *L); + +static int build_stroke (lua_State *L); +static int build_dash (lua_State *L); +static int build_curve (lua_State *L); +static int build_marker (lua_State *L); +static int build_rotate (lua_State *L); +static int build_translate (lua_State *L); + +/* DEBUG DEBUG DEBUG */ +static int window_debug_list (lua_State *L); +static int obj_getfenv (lua_State *L); + +static const struct luaL_Reg plot_functions[] = { + {"path", agg_path_new}, + {"text", agg_text_new}, + {"rgba", agg_rgba_new}, + {"rgb", agg_rgb_new}, + {"plot", agg_plot_new}, + {"windows", window_debug_list}, + {"objgetfenv", obj_getfenv}, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_plot_methods[] = { + {"show", agg_plot_show }, + {"add", agg_plot_add }, + {"addline", agg_plot_add_line }, + {"update", agg_plot_update }, + {"__index", agg_plot_index }, + {"__newindex", agg_plot_newindex }, + {"__gc", agg_plot_free }, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_plot_properties_get[] = { + {"title", agg_plot_title_get }, + {"units", agg_plot_units_get }, + {NULL, NULL} +}; + +static const struct luaL_Reg agg_plot_properties_set[] = { + {"title", agg_plot_title_set }, + {"units", agg_plot_units_set }, + {NULL, NULL} +}; + +struct property_reg { + int id; + const char *name; +}; + +__END_DECLS + + +struct property_reg line_cap_properties[] = { + {(int) agg::butt_cap, "butt" }, + {(int) agg::square_cap, "square"}, + {(int) agg::round_cap, "round" }, + {0, NULL} +}; + +struct property_reg line_join_properties[] = { + {(int) agg::miter_join, "miter" }, + {(int) agg::miter_join_revert, "miter.rev" }, + {(int) agg::round_join, "round" }, + {(int) agg::bevel_join, "bevel" }, + {(int) agg::miter_join_round, "miter.round"}, + {0, NULL} +}; + +const struct luaL_Reg trans_builder[] = { + {"stroke", build_stroke }, + {"dash", build_dash}, + {"curve", build_curve}, + {"marker", build_marker}, + {"translate", build_translate}, + {"rotate", build_rotate}, + {NULL, NULL} +}; + +static int +property_lookup (struct property_reg *prop, const char *key) +{ + int default_value = prop->id; + + if (key == NULL) + return default_value; + + for ( ; prop->name; prop++) + { + if (strcmp (prop->name, key) == 0) + return prop->id; + } + + return default_value; +} + +void agg_plot::wait_update() +{ + if (this->window) + update_callback (this->window); +}; + +agg_plot* agg_plot::arg_check(lua_State *L, int index) +{ + return (agg_plot *) gs_check_userdata (L, index, GS_DRAW_PLOT); +} + +int +agg_plot_new (lua_State *L) +{ + agg_plot *p = new(L, GS_DRAW_PLOT) agg_plot(); + + lua_newtable (L); + lua_setfenv (L, -2); + + if (lua_isstring (L, 1)) + { + const char *title = lua_tostring (L, 1); + if (title) + p->set_title(title); + } + + return 1; +} + +int +agg_plot_free (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + printf("freeing plot\n"); + p->~agg_plot(); + return 0; +} + +int +agg_plot_index (lua_State *L) +{ + return mlua_index_with_properties (L, agg_plot_properties_get, false); +} + +int +agg_plot_title_set (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + const char *title = lua_tostring (L, 2); + + if (title == NULL) + return gs_type_error (L, 2, "string"); + + AGG_LOCK(); + + p->set_title(title); + p->wait_update(); + + AGG_UNLOCK(); + + return 0; +} + +int +agg_plot_title_get (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + const char *title = p->get_title(); + lua_pushstring (L, title); + return 1; +} + +int +agg_plot_units_set (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + bool request = (bool) lua_toboolean (L, 2); + bool current = p->use_units(); + + if (current != request) + { + AGG_LOCK(); + p->set_units(request); + p->wait_update(); + AGG_UNLOCK(); + } + + return 0; +} + +int +agg_plot_units_get (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + lua_pushboolean (L, p->use_units()); + return 1; +} + +int +agg_plot_newindex (lua_State *L) +{ + return mlua_newindex_with_properties (L, agg_plot_properties_set); +} + +int +build_stroke (lua_State *L) +{ + const int specindex = 3, objindex = 2; + double width = mlua_named_optnumber (L, specindex, "width", 1.0); + const char *scap = mlua_named_optstring (L, specindex, "cap", NULL); + const char *sjoin = mlua_named_optstring (L, specindex, "join", NULL); + vertex_source *obj = check_agg_obj (L, objindex); + + trans::stroke *stroke = new(L, GS_DRAW_OBJ) trans::stroke(obj, width); + mlua_set_fenv_ref (L, objindex); + + if (scap) + { + int cap = property_lookup (line_cap_properties, scap); + stroke->line_cap((agg::line_cap_e) cap); + } + + if (sjoin) + { + int join = property_lookup (line_join_properties, sjoin); + stroke->line_join((agg::line_join_e) join); + } + + return 1; +} + +int +build_curve (lua_State *L) +{ + const int specindex = 3, objindex = 2; + vertex_source *obj = check_agg_obj (L, objindex); + + trans::curve *c = new(L, GS_DRAW_OBJ) trans::curve(obj); + mlua_set_fenv_ref (L, objindex); + + return 1; +} + +int +build_marker (lua_State *L) +{ + const int specindex = 3, objindex = 2; + double size = mlua_named_optnumber (L, specindex, "size", 3.0); + vertex_source *obj = check_agg_obj (L, objindex); + + new(L, GS_DRAW_OBJ) trans::marker(obj, size); + mlua_set_fenv_ref (L, objindex); + + return 1; +} + +int +build_dash (lua_State *L) +{ + const int specindex = 3, objindex = 2; + double a = mlua_named_optnumber (L, specindex, "a", 10.0); + double b = mlua_named_optnumber (L, specindex, "b", a); + vertex_source *obj = check_agg_obj (L, objindex); + + trans::dash *dash = new(L, GS_DRAW_OBJ) trans::dash(obj); + mlua_set_fenv_ref (L, objindex); + + dash->add_dash(a, b); + + return 1; +} + +int +build_translate (lua_State *L) +{ + const int specindex = 3, objindex = 2; + double x = mlua_named_number (L, specindex, "x"); + double y = mlua_named_number (L, specindex, "y"); + vertex_source *obj = check_agg_obj (L, objindex); + + trans::affine *t = new(L, GS_DRAW_OBJ) trans::affine(obj); + mlua_set_fenv_ref (L, objindex); + + t->translate(x, y); + + return 1; +} + +int +build_rotate (lua_State *L) +{ + const int specindex = 3, objindex = 2; + double a = mlua_named_number (L, specindex, "angle"); + vertex_source *obj = check_agg_obj (L, objindex); + + trans::affine *t = new(L, GS_DRAW_OBJ) trans::affine(obj); + mlua_set_fenv_ref (L, objindex); + + t->rotate(a); + + return 1; +} + +void +parse_spec (lua_State *L) +{ + const int specindex = 3, objindex = 2; + const char *tag; + const struct luaL_Reg *builder; + + lua_rawgeti (L, specindex, 1); + if (! lua_isstring (L, -1)) + { + luaL_error (L, "the tag of the transformation is invalid"); + return; + } + + tag = lua_tostring (L, -1); + lua_pop (L, 1); + + builder = mlua_find_method (trans_builder, tag); + + if (builder) + builder->func (L); + else + luaL_error (L, "error in definition of pre or post transforms"); + + lua_replace (L, objindex); + lua_pop (L, 1); +} + +int +lparse_spec_pipeline (lua_State *L) +{ + size_t k, nb; + + if (lua_type (L, 1) == LUA_TTABLE) + nb = lua_objlen (L, 1); + else + return luaL_error (L, "post transform argument should be a table"); + + for (k = nb; k > 0; k--) + { + lua_rawgeti (L, 1, k); + parse_spec (L); + } + + return 1; +} + +static agg::rgba8 * +color_arg_lookup (lua_State *L, int index) +{ + agg::rgba8 *c; + + if (lua_isnil (L, index)) + { + c = rgba8_push_default (L); + lua_replace (L, index); + } + else if (lua_isstring (L, index)) + { + const char *cstr = lua_tostring (L, index); + c = rgba8_push_lookup (L, cstr); + lua_replace (L, index); + } + else + { + c = check_agg_rgba8 (L, index); + } + + return c; +} + +static int +agg_plot_add_gener (lua_State *L, bool as_line) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + int narg = lua_gettop (L); + agg::rgba8 *color; + + if (narg <= 2) + color = rgba8_push_default (L); + else + color = color_arg_lookup (L, 3); + + if (narg > 4) + { + lua_pushcfunction (L, lparse_spec_pipeline); + lua_pushvalue (L, 5); + lua_pushvalue (L, 2); + lua_call (L, 2, 1); + lua_replace (L, 2); + } + + vertex_source *curr = check_agg_obj (L, 2); + if (curr->need_resize()) + { + new(L, GS_DRAW_OBJ) trans::resize(curr); + mlua_set_fenv_ref (L, 2); + lua_replace (L, 2); + } + + if (narg > 3) + { + lua_pushcfunction (L, lparse_spec_pipeline); + lua_pushvalue (L, 4); + lua_pushvalue (L, 2); + lua_call (L, 2, 1); + lua_replace (L, 2); + } + + curr = check_agg_obj (L, 2); + + lua_pushvalue (L, 1); + mlua_fenv_addref (L, 2); + lua_pop (L, 1); + + AGG_LOCK(); + p->add(curr, color, as_line); + p->wait_update(); + AGG_UNLOCK(); + + return 0; +} + +int +agg_plot_add (lua_State *L) +{ + return agg_plot_add_gener (L, false); +} + +int +agg_plot_add_line (lua_State *L) +{ + return agg_plot_add_gener (L, true); +} + +int +agg_plot_update (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + AGG_PROTECT(p->wait_update()); + return 0; +} + +int +agg_plot_show (lua_State *L) +{ + agg_plot *p = agg_plot::arg_check(L, 1); + pthread_t xwin_thread[1]; + pthread_attr_t attr[1]; + + AGG_LOCK(); + + if (! p->is_shown) + { + p->id = mlua_window_ref(L, 1); + + pthread_attr_init (attr); + pthread_attr_setdetachstate (attr, PTHREAD_CREATE_DETACHED); + + if (pthread_create(xwin_thread, attr, xwin_thread_function, (void*) p)) + { + pthread_attr_destroy (attr); + mlua_window_unref(L, p->id); + p->id = -1; + return luaL_error(L, "error creating thread."); + } + + p->is_shown = 1; + pthread_attr_destroy (attr); + } + + AGG_UNLOCK(); + + return 0; +} + +int +window_debug_list(lua_State *L) +{ + lua_getfield (L, LUA_REGISTRYINDEX, "GSL.windows"); + return 1; +} + +int +obj_getfenv(lua_State *L) +{ + lua_getfenv (L, 1); + return 1; +} + +void +plot_register (lua_State *L) +{ + /* plot declaration */ + luaL_newmetatable (L, GS_METATABLE(GS_DRAW_PLOT)); + luaL_register (L, NULL, agg_plot_methods); + lua_pop (L, 1); + + /* gsl module registration */ + luaL_register (L, NULL, plot_functions); +} diff --git a/agg-plot/lua-plot.h b/agg-plot/lua-plot.h new file mode 100644 index 00000000..ffc06393 --- /dev/null +++ b/agg-plot/lua-plot.h @@ -0,0 +1,37 @@ +#ifndef LUA_CPLOT_H
+#define LUA_CPLOT_H
+
+#include "defs.h"
+
+__BEGIN_DECLS
+#include "lua.h"
+__END_DECLS
+
+#ifdef __cplusplus
+
+#include "plot.h"
+#include "resource-manager.h"
+
+typedef plot<vertex_source, no_management> plot_type;
+
+struct agg_plot : public plot_type {
+ bool is_shown;
+ void *window;
+ int id;
+
+ agg_plot() : plot_type(), is_shown(false), window(NULL), id(0) {};
+
+ void wait_update();
+
+ static agg_plot* arg_check(lua_State *L, int index);
+};
+
+#endif
+
+__BEGIN_DECLS
+
+extern void plot_register (lua_State *L);
+
+__END_DECLS
+
+#endif
diff --git a/agg-plot/plot.h b/agg-plot/plot.h index e2b6467b..95430d20 100644 --- a/agg-plot/plot.h +++ b/agg-plot/plot.h @@ -34,8 +34,8 @@ class plot { container(): vs(NULL), color(), outline(false) {}; - container(VertexSource* vs, agg::rgba8 c, bool as_outline): - vs(vs), color(c), outline(as_outline) + container(VertexSource* vs, agg::rgba8 *c, bool as_outline): + vs(vs), color(*c), outline(as_outline) {}; ~container() {}; @@ -85,7 +85,7 @@ public: bool use_units() const { return m_use_units; }; void set_units(bool use_units); - void add(VertexSource* vs, agg::rgba8 color, bool outline = false) + void add(VertexSource* vs, agg::rgba8 *color, bool outline = false) { container d(vs, color, outline); m_elements.add(d); diff --git a/agg-plot/resource-manager.h b/agg-plot/resource-manager.h index c59782a3..c965a49c 100644 --- a/agg-plot/resource-manager.h +++ b/agg-plot/resource-manager.h @@ -11,7 +11,7 @@ class no_management { static void dispose(T* p) {};
};
-
+/*
class ref_manager {
public:
template <class T>
@@ -25,5 +25,6 @@ public: delete p;
};
};
+*/
#endif
diff --git a/agg-plot/trans.h b/agg-plot/trans.h index 6bcaae59..0cdacee2 100644 --- a/agg-plot/trans.h +++ b/agg-plot/trans.h @@ -8,6 +8,8 @@ #include "agg_conv_transform.h"
#include "agg_vcgen_markers_term.h"
#include "agg_arrowhead.h"
+#include "agg_bounding_rect.h"
+#include "agg_ellipse.h"
#include "my_conv_simple_marker.h"
@@ -23,15 +25,7 @@ protected: public:
vs_trans_proxy(vertex_source* src): m_output(*src), m_source(src)
{
-#ifdef DEBUG_PLOT
- fprintf(stderr, "creating trans: %p\n", this);
-#endif
};
-#ifdef DEBUG_PLOT
- ~vs_trans_proxy() { fprintf(stderr, "freeing trans: %p\n", this); };
-#endif
-
-
template <class init_type>
vs_trans_proxy(vertex_source* src, init_type& val):
@@ -41,6 +35,7 @@ public: virtual void rewind(unsigned path_id) { m_output.rewind(path_id); };
virtual unsigned vertex(double* x, double* y) { return m_output.vertex(x, y); };
+ /*
virtual void ref() { m_source->ref(); };
virtual unsigned unref()
{
@@ -49,6 +44,7 @@ public: delete m_source;
return 0;
};
+ */
virtual void apply_transform(agg::trans_affine& m, double as)
{
diff --git a/agg-plot/vertex-source.h b/agg-plot/vertex-source.h index 21e561d3..dad545dc 100644 --- a/agg-plot/vertex-source.h +++ b/agg-plot/vertex-source.h @@ -11,13 +11,16 @@ public: virtual ~vertex_source_base() {}; }; + class vertex_source : public vertex_source_base { public: virtual void apply_transform(agg::trans_affine& m, double as) = 0; virtual void bounding_box(double *x1, double *y1, double *x2, double *y2) = 0; + /* virtual void ref() = 0; virtual unsigned unref() = 0; + */ virtual bool need_resize() { return true; }; }; diff --git a/agg-plot/xwin-show.cpp b/agg-plot/xwin-show.cpp index 20a1b079..05692340 100644 --- a/agg-plot/xwin-show.cpp +++ b/agg-plot/xwin-show.cpp @@ -5,9 +5,11 @@ #include "platform/agg_platform_support.h" #include "xwin-show.h" +#include "gsl-shell.h" #include "canvas.h" -#include "plot.h" -#include "lua-plot-priv.h" +#include "lua-plot.h" +#include "lua-cpp-utils.h" +#include "lua-draw.h" extern void platform_support_prepare (); @@ -20,7 +22,7 @@ enum flip_y_e { flip_y = true }; class the_application : public agg::platform_support { public: - the_application(agg::pix_format_e format, bool flip_y, struct agg_plot *p) : + the_application(agg::pix_format_e format, bool flip_y, agg_plot *p) : agg::platform_support(format, flip_y), m_plot(p) { }; @@ -33,19 +35,16 @@ public: { canvas can(rbuf_window(), width(), height(), agg::rgba(1.0, 1.0, 1.0)); can.clear(); - plot_type* plot = (plot_type*) m_plot->plot; - plot->draw(can); + this->m_plot->draw(can); } virtual void on_draw() { - pthread_mutex_lock (agg_mutex); - on_draw_unprotected(); - pthread_mutex_unlock (agg_mutex); + AGG_PROTECT(on_draw_unprotected()); } private: - struct agg_plot *m_plot; + agg_plot *m_plot; }; int update_callback (void *_app) @@ -69,11 +68,11 @@ int update_callback (void *_app) void * xwin_thread_function (void *_plot) { - struct agg_plot *p = (struct agg_plot *) _plot; + agg_plot *p = (agg_plot *) _plot; platform_support_prepare(); - pthread_mutex_lock (agg_mutex); + AGG_LOCK(); the_application app(agg::pix_format_bgr24, flip_y, p); app.caption("GSL shell plot"); @@ -81,21 +80,20 @@ xwin_thread_function (void *_plot) if(app.init(780, 400, agg::window_resize)) { p->window = (void *) &app; - pthread_mutex_unlock (agg_mutex); + AGG_UNLOCK(); + + /* start main loop for the plot window */ app.run(); - pthread_mutex_lock (agg_mutex); - } - p->window = NULL; - if (p->lua_is_owner) - { + AGG_LOCK(); + p->window = NULL; p->is_shown = 0; - pthread_mutex_unlock (agg_mutex); - } - else - { - pthread_mutex_unlock (agg_mutex); - agg_plot_destroy (p); + AGG_UNLOCK(); + + GSL_SHELL_LOCK(); + printf("unref plot\n"); + gsl_shell_unref_plot (p->id); + GSL_SHELL_UNLOCK(); } return NULL; diff --git a/agg-plot/xwin-show.h b/agg-plot/xwin-show.h index 95dc7ed7..62b47b75 100644 --- a/agg-plot/xwin-show.h +++ b/agg-plot/xwin-show.h @@ -3,6 +3,7 @@ __BEGIN_DECLS +extern int update_callback (void *_app); extern void * xwin_thread_function (void *_cplot); __END_DECLS |