OpenWrt packages
Sign in or create your account | Project List | Help
OpenWrt packages Git Source Tree
Root/
| 1 | --[[ $Id: x08.lua 9533 2009-02-16 22:18:37Z smekal $ |
| 2 | |
| 3 | 3-d plot demo. |
| 4 | |
| 5 | Copyright (C) 2008 Werner Smekal |
| 6 | |
| 7 | This file is part of PLplot. |
| 8 | |
| 9 | PLplot is free software you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Library Public License as published |
| 11 | by the Free Software Foundation either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | PLplot is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU Library General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU Library General Public License |
| 20 | along with PLplot if not, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | --]] |
| 23 | |
| 24 | -- initialise Lua bindings for PLplot examples. |
| 25 | dofile("plplot_examples.lua") |
| 26 | |
| 27 | |
| 28 | -- bitwise or operator from http://lua-users.org/wiki/BaseSixtyFour |
| 29 | -- (c) 2006-2008 by Alex Kloss |
| 30 | -- licensed under the terms of the LGPL2 |
| 31 | |
| 32 | -- return single bit (for OR) |
| 33 | function bit(x,b) |
| 34 | return (math.mod(x, 2^b) - math.mod(x,2^(b-1)) > 0) |
| 35 | end |
| 36 | |
| 37 | -- logic OR for number values |
| 38 | function lor(x,y) |
| 39 | result = 0 |
| 40 | for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end |
| 41 | return result |
| 42 | end |
| 43 | |
| 44 | |
| 45 | ---------------------------------------------------------------------------- |
| 46 | -- cmap1_init1 |
| 47 | -- |
| 48 | -- Initializes color map 1 in HLS space. |
| 49 | -- Basic grayscale variation from half-dark (which makes more interesting |
| 50 | -- looking plot compared to dark) to light. |
| 51 | -- An interesting variation on this: |
| 52 | -- s[1] = 1.0 |
| 53 | ---------------------------------------------------------------------------- |
| 54 | |
| 55 | function cmap1_init(gray) |
| 56 | i = { 0, 1 } -- left and right boundary |
| 57 | |
| 58 | if gray ~= 0 then |
| 59 | h = { 0, 0 } -- hue -- low: red (arbitrary if s=0), high: red (arbitrary if s=0) |
| 60 | l = { 0.5, 1 } -- lightness -- low: half-dark, high: light |
| 61 | s = { 0, 0 } -- minimum saturation |
| 62 | else |
| 63 | h = { 240, 0 } -- blue -> green -> yellow -> red |
| 64 | l = { 0.6, 0.6 } |
| 65 | s = { 0.8, 0.8 } |
| 66 | end |
| 67 | |
| 68 | pl.scmap1n(256) |
| 69 | pl.scmap1l(0, i, h, l, s) |
| 70 | end |
| 71 | |
| 72 | |
| 73 | ---------------------------------------------------------------------------- |
| 74 | -- main |
| 75 | -- |
| 76 | -- Does a series of 3-d plots for a given data set, with different |
| 77 | -- viewing options in each plot. |
| 78 | ---------------------------------------------------------------------------- |
| 79 | |
| 80 | XPTS = 35 -- Data points in x |
| 81 | YPTS = 46 -- Data points in y |
| 82 | LEVELS = 10 |
| 83 | |
| 84 | alt = { 60, 20 } |
| 85 | az = { 30, 60 } |
| 86 | |
| 87 | title = { |
| 88 | "#frPLplot Example 8 - Alt=60, Az=30", |
| 89 | "#frPLplot Example 8 - Alt=20, Az=60" |
| 90 | } |
| 91 | |
| 92 | clevel = {} |
| 93 | nlevel = LEVELS |
| 94 | rosen = 1 |
| 95 | sombrero = 0 |
| 96 | |
| 97 | -- Parse and process command line arguments |
| 98 | pl.parseopts(arg, pl.PL_PARSE_FULL) |
| 99 | if sombrero ~= 0 then rosen=0 end |
| 100 | |
| 101 | -- Initialize plplot |
| 102 | pl.init() |
| 103 | |
| 104 | -- Allocate data structures |
| 105 | x = {} |
| 106 | y = {} |
| 107 | z = {} |
| 108 | |
| 109 | for i=1, XPTS do |
| 110 | x[i] = (i-1-math.floor(XPTS/2)) / math.floor(XPTS/2) |
| 111 | if rosen~=0 then x[i]=x[i]*1.5 end |
| 112 | end |
| 113 | |
| 114 | for i=1, YPTS do |
| 115 | y[i] = (i-1-math.floor(YPTS/2)) / math.floor(YPTS/2) |
| 116 | if rosen~=0 then y[i]=y[i]+0.5 end |
| 117 | end |
| 118 | |
| 119 | for i=1, XPTS do |
| 120 | xx = x[i] |
| 121 | z[i]= {} |
| 122 | for j=1, YPTS do |
| 123 | yy = y[j] |
| 124 | if rosen~=0 then |
| 125 | z[i][j] = (1-xx)^2 + 100*(yy-xx^2)^2 |
| 126 | -- The log argument may be zero for just the right grid. |
| 127 | if z[i][j] > 0 then |
| 128 | z[i][j] = math.log(z[i][j]) |
| 129 | else |
| 130 | z[i][j] = -5 -- MAXFLOAT would mess-up up the scale |
| 131 | end |
| 132 | else |
| 133 | r = math.sqrt(xx^2 + yy^2) |
| 134 | z[i][j] = math.exp(-r^2) * math.cos(2*math.pi*r) |
| 135 | end |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | zmax, zmin = pl.MinMax2dGrid(z) |
| 140 | step = (zmax-zmin)/(nlevel+1) |
| 141 | for i=1, nlevel do |
| 142 | clevel[i] = zmin + step + step*(i-1) |
| 143 | end |
| 144 | |
| 145 | pl.lightsource(1, 1, 1) |
| 146 | |
| 147 | for k=1, 2 do |
| 148 | for ifshade = 1, 4 do |
| 149 | pl.adv(0) |
| 150 | pl.vpor(0, 1, 0, 0.9) |
| 151 | pl.wind(-1, 1, -0.9, 1.1) |
| 152 | pl.col0(3) |
| 153 | pl.mtex("t", 1, 0.5, 0.5, title[k]) |
| 154 | pl.col0(1) |
| 155 | if rosen~=0 then |
| 156 | pl.w3d(1, 1, 1, -1.5, 1.5, -0.5, 1.5, zmin, zmax, alt[k], az[k]) |
| 157 | else |
| 158 | pl.w3d(1, 1, 1, -1, 1, -1, 1, zmin, zmax, alt[k], az[k]) |
| 159 | end |
| 160 | |
| 161 | pl.box3("bnstu", "x axis", 0, 0, |
| 162 | "bnstu", "y axis", 0, 0, |
| 163 | "bcdmnstuv", "z axis", 0, 0) |
| 164 | pl.col0(2) |
| 165 | |
| 166 | if ifshade==1 then -- diffuse light surface plot |
| 167 | cmap1_init(1) |
| 168 | pl.surf3d(x, y, z, 0, clevel) |
| 169 | end |
| 170 | |
| 171 | if ifshade==2 then -- magnitude colored plot |
| 172 | cmap1_init(0) |
| 173 | pl.surf3d(x, y, z, pl.MAG_COLOR, {}) |
| 174 | end |
| 175 | |
| 176 | if ifshade==3 then -- magnitude colored plot with faceted squares |
| 177 | cmap1_init(0) |
| 178 | pl.surf3d(x, y, z, lor(pl.MAG_COLOR, pl.FACETED), {}) |
| 179 | end |
| 180 | |
| 181 | if ifshade==4 then -- magnitude colored plot with contours |
| 182 | cmap1_init(0) |
| 183 | pl.surf3d(x, y, z, lor(lor(pl.MAG_COLOR, pl.SURF_CONT), pl.BASE_CONT), clevel) |
| 184 | end |
| 185 | end |
| 186 | end |
| 187 | |
| 188 | -- Clean up |
| 189 | pl.plend() |
| 190 |
