From: <sm...@us...> - 2009-02-12 20:44:28
|
Revision: 9515 http://plplot.svn.sourceforge.net/plplot/?rev=9515&view=rev Author: smekal Date: 2009-02-12 20:44:18 +0000 (Thu, 12 Feb 2009) Log Message: ----------- Added Lua examples 20 and 21 (run, but no correct results yet). Modified Paths: -------------- trunk/examples/lua/CMakeLists.txt Added Paths: ----------- trunk/examples/lua/x20.lua trunk/examples/lua/x21.lua Modified: trunk/examples/lua/CMakeLists.txt =================================================================== --- trunk/examples/lua/CMakeLists.txt 2009-02-12 05:30:35 UTC (rev 9514) +++ trunk/examples/lua/CMakeLists.txt 2009-02-12 20:44:18 UTC (rev 9515) @@ -38,6 +38,8 @@ "17" "18" "19" +"20" +"21" "22" "23" "24" Added: trunk/examples/lua/x20.lua =================================================================== --- trunk/examples/lua/x20.lua (rev 0) +++ trunk/examples/lua/x20.lua 2009-02-12 20:44:18 UTC (rev 9515) @@ -0,0 +1,304 @@ +--[[ $Id$ + + plimage demo +--]] + +-- initialise Lua bindings for PLplot examples. +dofile("plplot_examples.lua") + +XDIM = 260 +YDIM = 220 + +dbg = 0 +nosombrero = 0 +nointeractive = 1 +f_name="" + +--stretch_data = { +-- xmin, xmax, ymin, ymax +-- stretch +--} + + +-- Transformation function +function mypltr( x, y, s) + x0 = (s["xmin"] + s["xmax"])*0.5 + y0 = (s["ymin"] + s["ymax"])*0.5 + dy = (s["ymax"]-s["ymin"])*0.5 + tx = x0 + (x0-x)*(1.0 - s["stretch"]*math.cos((y-y0)/dy*math.pi*0.5)) + ty = y + + return tx, ty +end + + +-- read image from file in binary ppm format +function read_img(fname) + -- naive grayscale binary ppm reading. If you know how to, improve it + local fp = io.open(fname, "rb") + if fp==nil then + return 1 + end + + -- version + ver = fp:read("*line") + + if ver==nil then -- version + fp:close() + return 1 + end + + if ver~="P5" then -- I only understand this! + fp:close() + return 1 + end + + c = fp:read(1) + while c=="#" do + com = fp:read("*line") + if com==nil then -- version + fp:close() + return 1 + end + c = fp:read(1) + end + fp:seek("cur", -1) + + w, h, num_col = fp:read("*number", "*number", "*number") + if w==nil or h==nil or num_col==nil then -- width, height num colors + fp:close() + return 1 + end + + img = {} + imf = {} + + img = fp:read(w*h) + if string.len(img)~=w*h then + fp:close() + return 1 + end + fp:close() + + for i = 1, w do + imf[i] = {} + for j = 1, h do + index = (h-j)*w+i + imf[i][j] = string.byte(img, index) -- flip image up-down + end + end + + return 0, imf, w, h, num_col +end + + +-- save plot +function save_plot(fname) + cur_strm = pl.gstrm() -- get current stream + new_strm = pl.mkstrm() -- create a new one + + pl.sdev("psc") -- new device type. Use a known existing driver + pl.sfnam(fname) -- file name + + pl.cpstrm(cur_strm, 0) -- copy old stream parameters to new stream + pl.replot() -- do the save + pl.end1() -- close new device + + pl.sstrm(cur_strm) -- and return to previous one +end + + +-- get selection square interactively +function get_clip(xi, xe, yi, ye) + return 0, xi, xe, yi, ye +end + + +-- set gray colormap +function gray_cmap(num_col) + r = { 0, 1 } + g = { 0, 1 } + b = { 0, 1 } + pos = { 0, 1 } + + pl.scmap1n(num_col) + pl.scmap1l(1, pos, r, g, b, { 0 }) +end + + +x = {} +y = {} +z = {} +r = {} +img_f = {} +cgrid2 = {} + + +-- Bugs in plimage(): +-- + at high magnifications, the left and right edge are ragged, try +-- ./x20c -dev xwin -wplt 0.3,0.3,0.6,0.6 -ori 0.5 + +-- Bugs in x20c.c: +-- + if the window is resized after a selection is made on "lena", when + --making a new selection the old one will re-appear. + + +-- Parse and process command line arguments +pl.parseopts(arg, pl.PL_PARSE_FULL) + +-- Initialize plplot +pl.init() + +-- view image border pixels +if dbg~=0 then + pl.env(1, XDIM, 1, YDIM, 1, 1) -- no plot box + + -- build a one pixel square border, for diagnostics + for i = 1, XDIM do + z[i] = {} + z[i][1] = 1 -- left + z[i][YDIM] = 1 -- right + end + + for i = 1, YDIM do + z[1][i] = 1 -- top + z[XDIM][i] = 1 -- botton + end + + pl.lab("...around a blue square."," ","A red border should appear...") + + pl.image(z, 1, XDIM, 1, YDIM, 0, 0, 1, XDIM, 1, YDIM) +end + +-- sombrero-like demo +if nosombrero==0 then + pl.col0(2) -- draw a yellow plot box, useful for diagnostics! :( + pl.env(0, 2*math.pi, 0, 3*math.pi, 1, -1) + + for i = 1, XDIM do + x[i] = (i-1)*2*math.pi/(XDIM-1) + end + for i = 1, YDIM do + y[i] = (i-1)*3*math.pi/(YDIM-1) + end + + for i = 1, XDIM do + r[i] = {} + z[i] = {} + for j=1, YDIM do + r[i][j] = math.sqrt(x[i]^2+y[j]^2)+1e-3 + z[i][j] = math.sin(r[i][j])/(r[i][j]) + end + end + + pl.lab("No, an amplitude clipped \"sombrero\"", "", "Saturn?") + pl.ptex(2, 2, 3, 4, 0, "Transparent image") + pl.image(z, 0, 2*math.pi, 0, 3*math.pi, 0.05, 1, 0, 2*math.pi, 0, 3*math.pi) + + -- save the plot + if f_name~="" then + save_plot(f_name) + end +end + +-- read Lena image +-- Note we try two different locations to cover the case where this +-- examples is being run from the test_c.sh script +status, img_f, width, height, num_col = read_img("lena.pgm") +if status~=0 then + status, img_f, width, height, num_col = read_img("../lena.pgm") + if status~=0 then + -- pl.abort("No such file") TODO + print("No such file") + print( status, width, height ) + pl.plend() + os.exit() + end +end + +-- set gray colormap +gray_cmap(num_col) + +-- display Lena +pl.env(1., width, 1., height, 1, -1) + +if nointeractive==0 then + pl.lab("Set and drag Button 1 to (re)set selection, Button 2 to finish."," ","Lena...") +else + pl.lab(""," ","Lena...") +end + +pl.image(img_f, 1, width, 1, height, 0, 0, 1, width, 1, height) + +-- selection/expansion demo +if nointeractive==0 then + xi = 200 + xe = 330 + yi = 280 + ye = 220 + + status, xi, xe, yi, ye = get_clip(xi, xe, yi, ye) + if status~=0 then -- get selection rectangle + pl.plend() + os.exit() + end + + pl.spause(0) + pl.adv(0) + + -- display selection only + pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi) + + pl.spause(1) + + -- zoom in selection + pl.env(xi, xe, ye, yi, 1, -1) + pl.image(img_f, 1, width, 1, height, 0, 0, xi, xe, ye, yi) +end + +-- Base the dynamic range on the image contents. +img_max, img_min = pl.MinMax2dGrid(img_f) + +-- Draw a saturated version of the original image. Only use the middle 50% +-- of the image's full dynamic range. +pl.col0(2) +pl.env(0, width, 0, height, 1, -1) +pl.lab("", "", "Reduced dynamic range image example") +--pl.imagefr(img_f, 0, width, 0, height, 0, 0, img_min + img_max*0.25, img_max - img_max*0.25) + +-- Draw a distorted version of the original image, showing its full dynamic range. +pl.env(0, width, 0, height, 1, -1) +pl.lab("", "", "Distorted image example") + +stretch = {} +stretch["xmin"] = 0 +stretch["xmax"] = width +stretch["ymin"] = 0 +stretch["ymax"] = height +stretch["stretch"] = 0.5 + +-- In C / C++ the following would work, with plimagefr directly calling +-- mypltr. For compatibilty with other language bindings the same effect +-- can be achieved by generating the transformed grid first and then +-- using pltr2. +-- plimagefr(img_f, width, height, 0., width, 0., height, 0., 0., img_min, img_max, mypltr, (PLPointer) &stretch) + +cgrid2 = {} +cgrid2["xg"] = {} +cgrid2["yg"] = {} +cgrid2["nx"] = width+1 +cgrid2["ny"] = height+1 + +for i = 1, width+1 do + cgrid2["xg"][i] = {} + cgrid2["yg"][i] = {} + for j = 1, height+1 do + xx, yy = mypltr(i, j, stretch) + cgrid2["xg"][i][j] = xx + cgrid2["yg"][i][j] = yy + end +end + +--plimagefr(img_f, 0, width, 0, height, 0, 0, img_min, img_max, "pltr2", cgrid2) + +pl.plend() Property changes on: trunk/examples/lua/x20.lua ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/examples/lua/x21.lua =================================================================== --- trunk/examples/lua/x21.lua (rev 0) +++ trunk/examples/lua/x21.lua 2009-02-12 20:44:18 UTC (rev 9515) @@ -0,0 +1,263 @@ +--[[ $Id$ + Grid data demo + + Copyright (C) 200 Werner Smekal + + This file is part of PLplot. + + PLplot is free software you can redistribute it and/or modify + it under the terms of the GNU General Library Public License as published + by the Free Software Foundation either version 2 of the License, or + (at your option) any later version. + + PLplot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with PLplot if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +--]] + + +-- initialise Lua bindings for PLplot examples. +dofile("plplot_examples.lua") + +-- bitwise or operator from http://lua-users.org/wiki/BaseSixtyFour +-- (c) 2006-2008 by Alex Kloss +-- licensed under the terms of the LGPL2 + +-- return single bit (for OR) +function bit(x,b) + return (math.mod(x, 2^b) - math.mod(x,2^(b-1)) > 0) +end + +-- logic OR for number values +function lor(x,y) + result = 0 + for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end + return result +end + +-- Options data structure definition. +pts = 500 +xp = 25 +yp = 20 +nl = 16 +knn_order = 20 +threshold = 1.001 +wmin = -1e3 +randn = 0 +rosen = 0 + + +function cmap1_init() + i = { 0, 1 } -- left and right boundary + + h = { 240, 0 } -- blue -> green -> yellow -> red + l = { 0.6, 0.6 } + s = { 0.8, 0.8 } + + pl.scmap1n(256) + pl.scmap1l(0, i, h, l, s, { 0 } ) -- TODO: last table should actually be empty +end + + +function create_grid(px, py) + local x = {} + local y = {} + + for i = 1, px do + x[i] = xm + (xM-xm)*(i-1)/(px-1) + end + + for i = 1, py do + y[i] = ym + (yM-ym)*(i-1)/(py-1) + end + + return x, y +end + + +function create_data(pts) + x = {} + y = {} + z = {} + + for i = 1, pts do + xt = (xM-xm)*pl.randd() + yt = (yM-ym)*pl.randd() + if randn==0 then + x[i] = xt + xm + y[i] = yt + ym + else -- std=1, meaning that many points are outside the plot range + x[i] = math.sqrt(-2*math.log(xt)) * math.cos(2*math.pi*yt) + xm + y[i] = math.sqrt(-2*math.log(xt)) * math.sin(2*math.pi*yt) + ym + end + if rosen==0 then + r = math.sqrt(x[i]^2 + y[i]^2) + z[i] = math.exp(-r^2) * math.cos(2*math.pi*r) + else + z[i] = math.log((1-x[i])^2 + 100*(y[i] - x[i]^2)^2) + end + end + + return x, y, z +end + + +title = { "Cubic Spline Approximation", + "Delaunay Linear Interpolation", + "Natural Neighbors Interpolation", + "KNN Inv. Distance Weighted", + "3NN Linear Interpolation", + "4NN Around Inv. Dist. Weighted" } + + + +xm = -0.2 +ym = -0.2 +xM = 0.6 +yM = 0.6 + +pl.parseopts(arg, pl.PL_PARSE_FULL) + +opt = { 0, 0, wmin, knn_order, threshold, 0 } + +-- Initialize plplot +pl.init() + +-- Initialise random number generator +pl.seed(5489) + +x, y, z = create_data(pts) -- the sampled data +zmin = z[1] +zmax = z[1] +for i=2, pts do + if z[i]>zmax then zmax = z[i] end + if z[i]<zmin then zmin = z[i] end +end + +xg, yg = create_grid(xp, yp) -- grid the data at +zg = {} +for i = 1, xp do + zg[i] = {} + for j = 1, yp do + zg[i][j] = 0 + end +end +clev = {} + +pl.col0(1) +pl.env(xm, xM, ym, yM, 2, 0) +pl.col0(15) +pl.lab("X", "Y", "The original data sampling") +pl.col0(2) +pl.poin(x, y, 5) +pl.adv(0) + +pl.ssub(3, 2) + +for k = 1, 2 do + pl.adv(0) + for alg=1, 6 do + pl.griddata(x, y, z, xg, yg, zg, alg, opt[alg]) + + --[[ + - CSA can generate NaNs (only interpolates?!). + - DTLI and NNI can generate NaNs for points outside the convex hull + of the data points. + - NNLI can generate NaNs if a sufficiently thick triangle is not found + + PLplot should be NaN/Inf aware, but changing it now is quite a job... + so, instead of not plotting the NaN regions, a weighted average over + the neighbors is done. --]] + + + if alg==pl.GRID_CSA or alg==pl.GRID_DTLI or alg==pl.GRID_NNLI or alg==pl.GRID_NNI then + for i = 1, xp do + for j = 1, yp do + if zg[i][j]==nil then -- average (IDW) over the 8 neighbors + zg[i][j] = 0 + dist = 0 + + for ii=i-1, i+1 do + if ii<=xp then + for jj=j-1, j+1 do + if jj<=yp then + if ii>=1 and jj>=1 and zg[ii][jj]~=nil then + if (math.abs(ii-i) + math.abs(jj-j)) == 1 then + d = 1 + else + d = 1.4141 + end + zg[i][j] = zg[i][j] + zg[ii][jj]/(d^2) + dist = dist + d + end + end + end + end + end + if dist~=0 then + zg[i][j] = zg[i][j]/dist + else + zg[i][j] = zmin + end + end + end + end + end + + lzM, lzm = pl.MinMax2dGrid(zg) + + lzm = math.min(lzm, zmin) + lzM = math.max(lzM, zmax) + + -- Increase limits slightly to prevent spurious contours + -- due to rounding errors + lzm = lzm-0.01 + lzM = lzM+0.01 + + pl.col0(1) + + pl.adv(alg) + + if k==1 then + for i = 1, nl do + clev[i] = lzm + (lzM-lzm)/(nl-1)*(i-1) + end + + pl.env0(xm, xM, ym, yM, 2, 0) + pl.col0(15) + pl.lab("X", "Y", title[alg]) + pl.shades(zg, xm, xM, ym, yM, clev, 1, 0, 1, 1) + pl.col0(2) + else + for i = 1, nl do + clev[i] = lzm + (lzM-lzm)/(nl-1)*(i-1) + end + + cmap1_init() + pl.vpor(0, 1, 0, 0.9) + pl.wind(-1.1, 0.75, -0.65, 1.20) + + -- For the comparison to be fair, all plots should have the + -- same z values, but to get the max/min of the data generated + -- by all algorithms would imply two passes. Keep it simple. + -- + -- pl.w3d(1, 1, 1, xm, xM, ym, yM, zmin, zmax, 30, -60) + + + pl.w3d(1, 1, 1, xm, xM, ym, yM, lzm, lzM, 30, -40) + pl.box3("bntu", "X", 0, 0, + "bntu", "Y", 0, 0, + "bcdfntu", "Z", 0.5, 0) + pl.col0(15) + pl.lab("", "", title[alg]) + pl.plot3dc(xg, yg, zg, lor(lor(pl.DRAW_LINEXY, pl.MAG_COLOR), pl.BASE_CONT), clev) + end + end +end + +pl.plend() Property changes on: trunk/examples/lua/x21.lua ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |