From: <and...@us...> - 2011-03-23 21:34:36
|
Revision: 11677 http://plplot.svn.sourceforge.net/plplot/?rev=11677&view=rev Author: andrewross Date: 2011-03-23 21:34:29 +0000 (Wed, 23 Mar 2011) Log Message: ----------- Add lua support for plstransform. Update example 19 to use this. Results are now consistent with the C version. Modified Paths: -------------- trunk/bindings/lua/plplotluac.i trunk/bindings/swig-support/plplotcapi.i trunk/examples/lua/x19.lua Modified: trunk/bindings/lua/plplotluac.i =================================================================== --- trunk/bindings/lua/plplotluac.i 2011-03-22 22:01:34 UTC (rev 11676) +++ trunk/bindings/lua/plplotluac.i 2011-03-23 21:34:29 UTC (rev 11677) @@ -634,6 +634,7 @@ typedef PLINT (*defined_func)(PLFLT, PLFLT); typedef void (*fill_func)(PLINT, const PLFLT*, const PLFLT*); typedef void (*pltr_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); +typedef void (*ct_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); typedef void (*mapform_func)(PLINT, PLFLT*, PLFLT*); typedef PLFLT (*f2eval_func)(PLINT, PLINT, PLPointer); typedef void (*label_func)(PLINT, PLFLT, char*, PLINT, PLPointer); @@ -642,6 +643,7 @@ typedef PLINT (*defined_func)(PLFLT, PLFLT); typedef void (*fill_func)(PLINT, const PLFLT*, const PLFLT*); typedef void (*pltr_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); +typedef void (*ct_func)(PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer); typedef void (*mapform_func)(PLINT, PLFLT *, PLFLT*); typedef PLFLT (*f2eval_func)(PLINT, PLINT, PLPointer); typedef void (*label_func)(PLINT, PLFLT, char*, PLINT, PLPointer); @@ -687,6 +689,47 @@ return; } +static char myct_funcstr[255]; + +/* This is the callback that gets handed to the C code. + It, in turn, calls the Lua callback */ +void myct(PLFLT x, PLFLT y, PLFLT *tx, PLFLT *ty, void *pltr_data) +{ + *tx=0; + *ty=0; + + /* check Lua state */ + if(myL==NULL) { + fprintf(stderr, "Lua state is not set!"); + return; + } + + /* push functions and arguments */ + lua_getglobal(myL, myct_funcstr); /* function to be called */ + lua_pushnumber(myL, x); /* push 1st argument */ + lua_pushnumber(myL, y); /* push 2nd argument */ + + /* do the call (2 arguments, 2 result) */ + if(lua_pcall(myL, 2, 2, 0) != 0) + fprintf(stderr, "error running function `%s': %s", + myct_funcstr, lua_tostring(myL, -1)); + + /* retrieve results */ + if(!lua_isnumber(myL, -2)) { + fprintf(stderr, "function `%s' must return a number as 1st result", myct_funcstr); + return; + } + if(!lua_isnumber(myL, -1)) { + fprintf(stderr, "function `%s' must return a number as 2nd result", myct_funcstr); + return; + } + *tx = lua_tonumber(myL, -2); + *ty = lua_tonumber(myL, -1); + lua_pop(myL, 2); /* pop returned values */ + + return; +} + static char label_funcstr[255]; void mylabel(PLINT axis, PLFLT value, char* label, PLINT length, PLPointer data) @@ -798,6 +841,23 @@ } %apply pltr_func pltr { pltr_func pltr_img }; +%typemap(in) ct_func ctf { + $1 = NULL; + myct_funcstr[0]='\0'; + + if(lua_isstring(L, $input)) { + const char* funcstr = lua_tostring(L, $input); + $1 = myct; + strncpy(myct_funcstr, funcstr, 255); + myL = L; + } else + SWIG_fail_arg("$symname", $argnum, "$1_type") +} +/* you can omit the ct func */ +%typemap(default) ct_func ctf { + $1 = NULL; + myct_funcstr[0]='\0'; +} %typemap(arginit) PLPointer OBJECT_DATA { cgrid1$argnum.xg = cgrid1$argnum.yg = cgrid1$argnum.zg = NULL; @@ -1180,6 +1240,7 @@ %rename(ssym) plssym; %rename(star) plstar; %rename(start) plstart; +%rename(stransform) plstransform; %rename(string) plstring; %rename(string3) plstring3; %rename(stripa) plstripa; Modified: trunk/bindings/swig-support/plplotcapi.i =================================================================== --- trunk/bindings/swig-support/plplotcapi.i 2011-03-22 22:01:34 UTC (rev 11676) +++ trunk/bindings/swig-support/plplotcapi.i 2011-03-23 21:34:29 UTC (rev 11677) @@ -731,10 +731,8 @@ void plstart( const char *devname, PLINT nx, PLINT ny ); -#if !defined ( SWIG_LUA ) void plstransform( ct_func ctf, PLPointer data ); -#endif void plstring( PLINT n, const PLFLT *Array, const PLFLT *ArrayCk, const char *string ); Modified: trunk/examples/lua/x19.lua =================================================================== --- trunk/examples/lua/x19.lua 2011-03-22 22:01:34 UTC (rev 11676) +++ trunk/examples/lua/x19.lua 2011-03-23 21:34:29 UTC (rev 11677) @@ -25,6 +25,14 @@ -- initialise Lua bindings for PLplot examples. dofile("plplot_examples.lua") +function map_transform(x,y) + radius = 90 - y + xt = radius * math.cos(x * math.pi / 180) + yt = radius * math.sin(x * math.pi / 180) + + return xt, yt +end + -------------------------------------------------------------------------- -- mapform19 -- @@ -35,11 +43,7 @@ function mapform19(n, x, y) for i = 1, n do - radius = 90 - y[i] - xp = radius * math.cos(x[i] * math.pi / 180) - yp = radius * math.sin(x[i] * math.pi / 180) - x[i] = xp - y[i] = yp + x[i], y[i] = map_transform(x[i], y[i]) end return x, y @@ -106,6 +110,9 @@ -- Parse and process command line arguments +x = {} +y = {} + pl.parseopts(arg, pl.PL_PARSE_FULL) -- Longitude (x) and latitude (y) @@ -150,4 +157,33 @@ pl.lsty(2) pl.meridians("mapform19", 10, 10, 0, 360, -10, 80) + +-- Polar, Northern hemisphere, this time with a PLplot-wide transform + +minx = 0 +maxx = 360 + +pl.stransform( "map_transform" ) + +pl.lsty( 1 ) +pl.env( -75., 75., -75., 75., 1, -1 ) +-- No need to set the map transform here as the global transform will be +-- used. +pl.map( nil, "globe", minx, maxx, miny, maxy ) + +pl.lsty( 2 ); +pl.meridians( nil, 10.0, 10.0, 0.0, 360.0, -10.0, 80.0 ) + +-- Show Baltimore, MD on the map +pl.col0( 2 ) +pl.ssym( 0.0, 2.0 ) +x[1] = -76.6125 +y[1] = 39.2902778 +pl.poin( x, y, 18 ) +pl.ssym( 0.0, 1.0 ) +pl.ptex( -76.6125, 43.0, 0.0, 0.0, 0.0, "Baltimore, MD" ) + +-- For C, this is how the global transform is cleared +pl.stransform( ); + pl.plend() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |