From: <and...@us...> - 2008-08-31 17:12:56
|
Revision: 8733 http://plplot.svn.sourceforge.net/plplot/?rev=8733&view=rev Author: andrewross Date: 2008-08-31 17:13:04 +0000 (Sun, 31 Aug 2008) Log Message: ----------- ANR for Hezekiah Carty. Add ocaml versions of example 16 and 20. Modified Paths: -------------- trunk/examples/ocaml/CMakeLists.txt trunk/examples/ocaml/Makefile.examples.in trunk/plplot_test/test_ocaml.sh.in Added Paths: ----------- trunk/examples/ocaml/x16.ml trunk/examples/ocaml/x20.ml Modified: trunk/examples/ocaml/CMakeLists.txt =================================================================== --- trunk/examples/ocaml/CMakeLists.txt 2008-08-31 12:29:08 UTC (rev 8732) +++ trunk/examples/ocaml/CMakeLists.txt 2008-08-31 17:13:04 UTC (rev 8733) @@ -35,8 +35,10 @@ "12" "13" "15" + "16" "18" "19" + "20" "21" "22" "24" @@ -66,6 +68,7 @@ add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME} COMMAND ${OCAMLC} + -g -I ${CMAKE_BINARY_DIR}/bindings/ocaml/${OCAML_BUILD_DIR} -ccopt "-L ${CMAKE_BINARY_DIR}/src -Wl,-rpath -Wl,${CMAKE_BINARY_DIR}/src " plplot.cma Modified: trunk/examples/ocaml/Makefile.examples.in =================================================================== --- trunk/examples/ocaml/Makefile.examples.in 2008-08-31 12:29:08 UTC (rev 8732) +++ trunk/examples/ocaml/Makefile.examples.in 2008-08-31 17:13:04 UTC (rev 8733) @@ -42,8 +42,10 @@ x12$(EXEEXT) \ x13$(EXEEXT) \ x15$(EXEEXT) \ + x16$(EXEEXT) \ x18$(EXEEXT) \ x19$(EXEEXT) \ + x20$(EXEEXT) \ x21$(EXEEXT) \ x22$(EXEEXT) \ x24$(EXEEXT) \ @@ -60,6 +62,6 @@ .ml$(EXEEXT): - $(OCAMLC) -I @OCAML_INSTALL_DIR@/plplot -ccopt "-L @CMAKE_INSTALL_LIBDIR@" plplot.cma -o $@ $< + $(OCAMLC) -g -I @OCAML_INSTALL_DIR@/plplot -ccopt "-L @CMAKE_INSTALL_LIBDIR@" plplot.cma -o $@ $< .SUFFIXES: .ml $(EXEEXT) Added: trunk/examples/ocaml/x16.ml =================================================================== --- trunk/examples/ocaml/x16.ml (rev 0) +++ trunk/examples/ocaml/x16.ml 2008-08-31 17:13:04 UTC (rev 8733) @@ -0,0 +1,251 @@ +(* $Id$ + + plshade demo, using color fill. + + Maurice LeBrun + IFS, University of Texas at Austin + 20 Mar 1994 +*) + +open Plplot + +let pi = atan 1.0 *. 4.0 + +(* Fundamental settings. See notes[] for more info. *) + +let ns = 20 (* Default number of shade levels *) +let nx = 35 (* Default number of data points in x *) +let ny = 46 (* Default number of data points in y *) +let exclude = false (* By default do not plot a page illustrating + exclusion. *) + +(* polar plot data *) +let perimeterpts = 100 + +(* Transformation function *) +let mypltr x y tr = + tr.(0) *. x +. tr.(1) *. y +. tr.(2), + tr.(3) *. x +. tr.(4) *. y +. tr.(5) + +let zdefined x y = + let z = sqrt (x *. x +. y *. y) in + if z < 0.4 || z > 0.6 then 1 else 0 + +(*--------------------------------------------------------------------------*\ + * f2mnmx + * + * Returns min & max of input 2d array. +\*--------------------------------------------------------------------------*) +let f2mnmx f = + let fmax = ref f.(0).(0) in + let fmin = ref f.(0).(0) in + for i = 0 to Array.length f - 1 do + for j = 0 to Array.length f.(i) - 1 do + fmax := max !fmax f.(i).(j); + fmin := min !fmin f.(i).(j); + done + done; + !fmin, !fmax + +(*--------------------------------------------------------------------------*\ + * Does several shade plots using different coordinate mappings. +\*--------------------------------------------------------------------------*) +let () = + let fill_width = 2 in + let cont_color = 0 in + let cont_width = 0 in + + (* Parse and process command line arguments *) + ignore (plparseopts Sys.argv [|PL_PARSE_FULL|]); + + (* Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display *) + plscmap0n 3; + + (* Initialize plplot *) + plinit (); + + (* Set up transformation function *) + let tr = + [| + 2.0 /. float_of_int (nx - 1); 0.0; -1.0; + 0.0; 2.0 /. float_of_int (ny-1); -1.0; + |] + in + + (* Set up data arrays *) + let z = Array.make_matrix nx ny 0.0 in + let w = Array.make_matrix nx ny 0.0 in + for i = 0 to nx - 1 do + let x = float_of_int (i - (nx / 2)) /. float_of_int (nx / 2) in + for j = 0 to ny - 1 do + let y = float_of_int (j - (ny / 2)) /. float_of_int (ny / 2) -. 1.0 in + z.(i).(j) <- -. sin (7.0 *. x) *. cos (7.0 *. y) +. x *. x -. y *. y; + w.(i).(j) <- -. cos (7.0 *. x) *. sin (7.0 *. y) +. 2.0 *. x *. y; + done + done; + + let zmin, zmax = f2mnmx z in + let clevel = + Array.init ns ( + fun i -> + zmin +. (zmax -. zmin) *. (float_of_int i +. 0.5) /. float_of_int ns + ) + in + let shedge = + Array.init (ns + 1) ( + fun i -> + zmin +. (zmax -. zmin) *. float_of_int i /. float_of_int ns + ) + in + + (* Set up coordinate grids *) + let xg1 = Array.make nx 0.0 in + let yg1 = Array.make ny 0.0 in + let xg2 = Array.make_matrix nx ny 0.0 in + let yg2 = Array.make_matrix nx ny 0.0 in + + for i = 0 to nx - 1 do + for j = 0 to ny - 1 do + let x, y = mypltr (float_of_int i) (float_of_int j) tr in + + let argx = x *. pi /. 2.0 in + let argy = y *. pi /. 2.0 in + let distort = 0.4 in + + xg1.(i) <- x +. distort *. cos argx; + yg1.(j) <- y -. distort *. cos argy; + + xg2.(i).(j) <- x +. distort *. cos argx *. cos argy; + yg2.(i).(j) <- y -. distort *. cos argx *. cos argy; + done + done; + + (* Plot using identity transform *) + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + + plpsty 0; + + plshades z (-1.0) 1.0 (-1.0) 1.0 shedge fill_width cont_color cont_width 1; + + plcol0 1; + plbox "bcnst" 0.0 0 "bcnstv" 0.0 0; + plcol0 2; + pllab "distance" "altitude" "Bogon density"; + + (* Plot using 1d coordinate transform *) + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + + plpsty 0; + + set_pltr (fun x y -> pltr1 x y xg1 yg1); + plshades z (-1.0) 1.0 (-1.0) 1.0 shedge fill_width cont_color cont_width 1; + + plcol0 1; + plbox "bcnst" 0.0 0 "bcnstv" 0.0 0; + plcol0 2; + pllab "distance" "altitude" "Bogon density"; + + (* Plot using 2d coordinate transform *) + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + + plpsty 0; + + set_pltr (fun x y -> pltr2 x y xg2 yg2); + plshades z (-1.0) 1.0 (-1.0) 1.0 shedge fill_width cont_color cont_width 0; + + plcol0 1; + plbox "bcnst" 0.0 0 "bcnstv" 0.0 0; + plcol0 2; + plcont w 1 nx 1 ny clevel; + + pllab "distance" "altitude" "Bogon density, with streamlines"; + + (* Plot using 2d coordinate transform *) + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + + plpsty 0; + + plshades z (-1.0) 1.0 (-1.) 1.0 shedge fill_width 2 3 0; + + plcol0 1; + plbox "bcnst" 0.0 0 "bcnstv" 0.0 0; + plcol0 2; + + pllab "distance" "altitude" "Bogon density"; + + (* Note this exclusion API will probably change. *) + + (* Plot using 2d coordinate transform and exclusion*) + if exclude then ( + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + + plpsty 0; + + set_defined zdefined; + plshades z (-1.0) 1.0 (-1.0) 1.0 shedge fill_width cont_color cont_width 0; + unset_defined (); + + plcol0 1; + plbox "bcnst" 0.0 0 "bcnstv" 0.0 0; + + pllab "distance" "altitude" "Bogon density with exclusion"; + ); + (* Example with polar coordinates. *) + pladv 0; + plvpor 0.1 0.9 0.1 0.9; + plwind (-1.0) 1.0 (-1.0) 1.0; + plpsty 0; + + (* Build new coordinate matrices. *) + for i = 0 to nx - 1 do + let r = float_of_int i /. float_of_int (nx - 1) in + for j = 0 to ny - 1 do + let t = (2.0 *. pi /. (float_of_int ny -. 1.0)) *. float_of_int j in + xg2.(i).(j) <- r *. cos t; + yg2.(i).(j) <- r *. sin t; + z.(i).(j) <- exp (~-.r *. r) *. cos (5.0 *. pi *. r) *. cos (5.0 *. t); + done + done; + + (* Need a new shedge to go along with the new data set. *) + let zmin, zmax = f2mnmx z in + + let shedge = + Array.init (ns + 1) ( + fun i -> + zmin +. (zmax -. zmin) *. float_of_int i /. float_of_int ns + ) + in + + (* Now we can shade the interior region. *) + plshades z (-1.0) 1.0 (-1.0) 1.0 shedge fill_width cont_color cont_width 0; + + (* Now we can draw the perimeter. (If do before, shade stuff may overlap.) *) + let px = Array.make perimeterpts 0.0 in + let py = Array.make perimeterpts 0.0 in + for i = 0 to perimeterpts - 1 do + let t = (2.0 *. pi /. float_of_int (perimeterpts - 1)) *. float_of_int i in + px.(i) <- cos t; + py.(i) <- sin t; + done; + plcol0 1; + plline px py; + + (* And label the plot.*) + plcol0 2; + pllab "" "" "Tokamak Bogon Instability"; + + (* Clean up *) + plend (); + () + Property changes on: trunk/examples/ocaml/x16.ml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/examples/ocaml/x20.ml =================================================================== --- trunk/examples/ocaml/x20.ml (rev 0) +++ trunk/examples/ocaml/x20.ml 2008-08-31 17:13:04 UTC (rev 8733) @@ -0,0 +1,252 @@ +(* $Id$ + + plimage demo + +*) + +open Plplot + +let pi = atan 1.0 *. 4.0 + +let xdim = 260 +let ydim = 220 + +(* Change this to 'Some "outfile.psc"' if you want to test the plot saving + option. *) +let filename = None + +type stretch_data = { + xmin : float; + xmax : float; + ymin : float; + ymax : float; + stretch : float; +} + +(* Transformation function *) +let mypltr x y pltr_data = + let x0 = (pltr_data.xmin +. pltr_data.xmax) *. 0.5 in + let y0 = (pltr_data.ymin +. pltr_data.ymax) *. 0.5 in + let dy = (pltr_data.ymax -. pltr_data.ymin) *. 0.5 in + x0 +. (x0 -. x) *. + (1.0 -. pltr_data.stretch *. cos ((y -. y0) /. dy *. pi *. 0.5)), + y + +(* read image from file in binary ppm format *) +let read_img fname = + (* naive grayscale binary ppm reading. If you know how to, improve it *) + let fp = open_in fname in + + let ver = input_line fp in (* version *) + + if ver <> "P5" then + raise (Invalid_argument ("Unable to read image: " ^ fname)) + else + (); + + (* This will skip comments. *) + let rec ignore_comments initial_pos = + let maybe_comment = input_line fp in + if maybe_comment.[0] = '#' then + ignore_comments (pos_in fp) + else + (* Skip back to the start of the first post-comment line. *) + seek_in fp initial_pos + in + ignore_comments (pos_in fp); + + (* width, height num colors *) + let w_h_line = input_line fp in + let num_col_line = input_line fp in + let w, h = Scanf.sscanf w_h_line "%d %d" (fun w h -> w, h) in + let num_col = Scanf.sscanf num_col_line "%d" (fun n -> n) in + + let img = String.make (w * h) ' ' in + let imf = Array.make_matrix w h 0.0 in + + (* Note that under 32bit OCaml, this will only work when reading strings up + to ~16 megabytes. *) + really_input fp img 0 (w * h - 2); + + close_in fp; + + for i = 0 to w - 1 do + for j = 0 to h - 1 do + imf.(i).(j) <- + (* flip image up-down *) + float_of_int (int_of_char (img.[(h - 1 - j ) * w + i])); + done + done; + imf, w, h, num_col + +(* save plot *) +let save_plot fname = + let cur_strm = plgstrm () in (* get current stream *) + let new_strm = plmkstrm () in (* create a new one *) + + plsdev "psc"; (* new device type. Use a known existing driver *) + plsfnam fname; (* file name *) + + plcpstrm cur_strm 0; (* copy old stream parameters to new stream *) + plreplot (); (* do the save *) + plend1 (); (* close new device *) + + plsstrm cur_strm; (* and return to previous one *) + () + +(* set gray colormap *) +let gray_cmap num_col = + let r = [|0.0; 1.0|] in + let g = [|0.0; 1.0|] in + let b = [|0.0; 1.0|] in + let pos = [|0.0; 1.0|] in + plscmap1n num_col; + plscmap1l 1 pos r g b None; + () + +let () = + (* Parse and process command line arguments *) + ignore (plparseopts Sys.argv [|PL_PARSE_FULL|]); + + (* Initialize plplot *) + plinit (); + + let z = Array.make_matrix xdim ydim 0.0 in + let r = Array.make_matrix xdim ydim 0.0 in + + (* sombrero-like demo *) + plcol0 2; (* draw a yellow plot box, useful for diagnostics *) + plenv 0.0 (2.0 *. pi) 0.0 (3.0 *. pi) 1 (-1); + + let x = + Array.init xdim ( + fun i -> float_of_int i *. 2.0 *. pi /. float_of_int (xdim - 1) + ) + in + let y = + Array.init ydim ( + fun i -> float_of_int i *. 3.0 *. pi /. float_of_int (ydim - 1) + ) + in + + for i = 0 to xdim - 1 do + for j = 0 to ydim - 1 do + r.(i).(j) <- sqrt (x.(i) *. x.(i) +. y.(j) *. y.(j)) +. 1e-3; + z.(i).(j) <- sin r.(i).(j) /. r.(i).(j); + done + done; + + pllab "No, an amplitude clipped \"sombrero\"" "" "Saturn?"; + plptex 2.0 2.0 3.0 4.0 0.0 "Transparent image"; + plimage + z 0.0 (2.0 *. pi) 0.0 (3.0 *. pi) 0.05 1.0 0.0 (2.0 *. pi) 0.0 (3.0 *. pi); + + (* save the plot *) + let () = + match filename with + Some f -> save_plot f + | None -> () + in + + pladv 0; + + (* read Lena image *) + (* Note we try two different locations to cover the case where this + * examples is being run from the test_ocaml.sh script *) + let img_f, width, height, num_col = + try + read_img "lena.pgm" + with + _ -> ( + try + read_img "../lena.pgm" + with + Invalid_argument _ -> + plend (); + raise (Failure "No such file") + ) + in + + let width = float_of_int width in + let height = float_of_int height in + + (* set gray colormap *) + gray_cmap num_col; + + (* display Lena *) + plenv 1.0 width 1.0 height 1 (-1); + + pllab "Set and drag Button 1 to (re)set selection, Button 2 to finish." " " "Lena..."; + + plimage img_f 1.0 width 1.0 height 0.0 0.0 1.0 width 1.0 height; + + (* selection/expansion demo *) + let xi = 200.0 in + let xe = 330.0 in + let yi = 280.0 in + let ye = 220.0 in + + plspause 0; + pladv 0; + + (* display selection only *) + plimage img_f 1.0 width 1.0 height 0.0 0.0 xi xe ye yi; + + plspause 1; + pladv 0; + + (* zoom in selection *) + plenv xi xe ye yi 1 (-1); + plimage img_f 1.0 width 1.0 height 0.0 0.0 xi xe ye yi; + pladv 0; + + (* Base the dynamic range on the image contents. *) + let img_max, img_min = plMinMax2dGrid img_f in + + (* Draw a saturated version of the original image. Only use the middle 50% + of the image's full dynamic range. *) + plcol0 2; + plenv 0.0 width 0.0 height 1 (-1); + pllab "" "" "Reduced dynamic range image example"; + plimagefr + img_f 0.0 width 0.0 height 0.0 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. *) + plenv 0.0 width 0.0 height 1 (-1); + pllab "" "" "Distorted image example"; + + let stretch = + { + xmin = 0.0; + xmax = width; + ymin = 0.0; + ymax = height; + stretch = 0.5; + } + in + + (* OCaml supports arbitrary coordinate transform callbacks, so take advantage + of that to use mypltr directly and skip the use of pltr2. *) + set_pltr (fun x y -> mypltr x y stretch); + (* Comment the above line, and uncomment the following section to use pltr2 *) + (* + let xg = Array.make_matrix (int_of_float width + 1) (int_of_float height + 1) 0.0 in + let yg = Array.make_matrix (int_of_float width + 1) (int_of_float height + 1) 0.0 in + for i = 0 to int_of_float width do + for j = 0 to int_of_float height do + let xx, yy = mypltr (float_of_int i) (float_of_int j) stretch in + xg.(i).(j) <- xx; + yg.(i).(j) <- yy; + done + done; + set_pltr (fun x y -> pltr2 x y xg yg); + *) + plimagefr img_f 0.0 width 0.0 height 0.0 0.0 img_min img_max; + + pladv 0; + + plend (); + () + Property changes on: trunk/examples/ocaml/x20.ml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/plplot_test/test_ocaml.sh.in =================================================================== --- trunk/plplot_test/test_ocaml.sh.in 2008-08-31 12:29:08 UTC (rev 8732) +++ trunk/plplot_test/test_ocaml.sh.in 2008-08-31 17:13:04 UTC (rev 8733) @@ -23,7 +23,7 @@ # $options, and possibly $verbose_test defined. # Do the standard non-interactive examples. -for index in 01 02 03 04 05 06 07 08 09 10 11 12 13 15 18 19 21 22 24 25 26 27 29 30; do +for index in 01 02 03 04 05 06 07 08 09 10 11 12 13 15 16 18 19 20 21 22 24 25 26 27 29 30; do if [ "$verbose_test" ]; then echo "x${index}ocaml" fi This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |