Added GIFPlot example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@219 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-02-11 05:37:31 +00:00
commit a59e9a4405
49 changed files with 8487 additions and 0 deletions

View file

@ -0,0 +1,23 @@
TOP = ../../..
SWIG = $(TOP)/../swig
SWIGOPT = -I../../Include
SRCS =
TARGET = gifplot
INTERFACE = gifplot.i
LIBS = -L../.. -lgifplot
INCLUDE = -I../../Include
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o *~ *.so mytclsh .~* core *.gif

View file

@ -0,0 +1,8 @@
This example runs the entire gifplot.h header file through SWIG without
any changes. The script 'runme.tcl' does something a little more
interesting. You'll have to go look at the header file to get a complete
listing of the functions.

Binary file not shown.

View file

@ -0,0 +1,15 @@
/* Oh what the heck, let's just grab the whole darn header file
and see what happens. */
%module gifplot
%{
/* Note: You still need this part because the %include directive
merely causes SWIG to interpret the contents of a file. It doesn't
include the right include headers for the resulting C code */
#include "gifplot.h"
%}
%include gifplot.h

View file

@ -0,0 +1,68 @@
# Plot a 3D function
catch { load ./gifplot.so gifplot}
catch { load ./gifplot.dll gifplot} ;# Windows
# Here is the function to plot
proc func { x y } {
return [expr { 5*cos(2*sqrt($x*$x+$y*$y))*exp(-0.3*sqrt($x*$x+$y*$y)) }]
}
# Here are some plotting parameters
set xmin -5.0
set xmax 5.0
set ymin -5.0
set ymax 5.0
set zmin -5.0
set zmax 5.0
# Grid resolution
set nxpoints 60
set nypoints 60
set cmap [new_ColorMap cmap]
set frame [new_FrameBuffer 500 500]
FrameBuffer_clear $frame $BLACK
set p3 [new_Plot3D $frame $xmin $ymin $zmin $xmax $ymax $zmax ]
Plot3D_lookat $p3 [expr {2*($zmax-$zmin)}]
Plot3D_autoperspective $p3 40
Plot3D_rotu $p3 60
Plot3D_rotr $p3 30
Plot3D_rotd $p3 10
proc drawsolid { } {
global xmin ymin zmin xmax ymax zmax nxpoints nypoints p3 BLACK
Plot3D_clear $p3 $BLACK
Plot3D_start $p3
set dx [expr {1.0*($xmax-$xmin)/$nxpoints}]
set dy [expr {1.0*($ymax-$ymin)/$nypoints}]
set cscale [expr {240.0/($zmax-$zmin)}]
set x $xmin
for {set i 0} {$i < $nxpoints} {incr i 1} {
set y $ymin
for {set j 0} {$j < $nypoints} {incr j 1} {
set z1 [func $x $y]
set z2 [func [expr {$x+$dx}] $y]
set z3 [func [expr {$x+$dx}] [expr {$y+$dy}]]
set z4 [func $x [expr {$y+$dy}]]
set c1 [expr {$cscale*($z1-$zmin)}]
set c2 [expr {$cscale*($z2-$zmin)}]
set c3 [expr {$cscale*($z3-$zmin)}]
set c4 [expr {$cscale*($z4-$zmin)}]
set c [expr {int(($c1+$c2+$c3+$c4)/4)}]
if {$c < 0} { set c 0 }
if {$c > 239} { set c 239 }
Plot3D_solidquad $p3 $x $y $z1 [expr {$x+$dx}] $y $z2 [expr {$x+$dx}] [expr {$y+$dy}] $z3 $x [expr {$y+$dy}] $z4 [expr {$c+16}]
set y [expr {$y + $dy}]
}
set x [expr {$x + $dx}]
}
}
puts "Making a nice 3D plot..."
drawsolid
FrameBuffer_writeGIF $frame $cmap "image.gif"
puts "Wrote image.gif"

View file

@ -0,0 +1,23 @@
TOP = ../../..
SWIG = $(TOP)/../swig
SWIGOPT = -I../../Interface
SRCS =
TARGET = gifplot
INTERFACE = mandel.i
LIBS = -L../.. -lgifplot
INCLUDE = -I../../Include
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='mywish' INTERFACE='$(INTERFACE)' wish
clean::
rm -f *_wrap* *.o *~ *.so mywish .~* core *.gif

View file

@ -0,0 +1,6 @@
Kill lots of time exploring the Mandelbrot set. This example uses
the full SWIG interface file located in ../../Interface. To run
the program, type 'wish mandel.tcl'.

Binary file not shown.

View file

@ -0,0 +1,68 @@
# Tcl code to display a image in a widget
set __imageno 0
proc dismiss {t i} {
destroy $t
image delete $i
}
proc BoxInit { c p2 mxmin mymin mxmax mymax func } {
bind $c <Button-1> {BoxBegin %W %x %y}
bind $c <B1-Motion> {BoxDrag %W %x %y}
bind $c <ButtonRelease-1> "BoxFinish %W %x %y $p2 $mxmin $mymin $mxmax $mymax $func"
}
proc BoxBegin {w x y} {
global box
set box(anchor) [list $x $y]
catch {unset box(last)}
}
proc BoxDrag { w x y} {
global box
catch {$w delete $box(last)}
set box(last) [eval {$w create rect} $box(anchor) {$x $y -tag box -outline white}]
}
proc BoxFinish {w x y p2 mxmin mymin mxmax mymax func } {
global box
set start $box(anchor)
set x1 [lrange $start 0 0]
set y1 [lrange $start 1 1]
catch {$w delete $box(last)}
# Call the handler function
$func $p2 $mxmin $mymin $mxmax $mymax $x1 $y1 $x $y
}
proc display_image {filename p2 handler} {
global __imageno __images
set i [image create photo -file $filename]
set tl .image$__imageno
toplevel $tl
frame $tl.img
frame $tl.button
set width [image width $i]
set height [image height $i]
canvas $tl.img.c -width [expr {$width+0}] -height [expr {$height+0}]
pack $tl.img.c
$tl.img.c create image 0 0 -image $i -anchor nw
label $tl.button.label -text $filename
pack $tl.button.label -side left
button $tl.button.dismiss -text "Dismiss" -command "dismiss $tl $i" -width 10
pack $tl.button.dismiss -side right
pack $tl.img $tl.button -side top -fill x
BoxInit $tl.img.c $p2 [$p2 cget -xmin] [$p2 cget -ymin] [$p2 cget -xmax] [$p2 cget -ymax] $handler
bind $tl <Key-q> "dismiss $tl $i"
bind $tl <Key-Escape> "dismiss $tl $i"
# Bind some actions to the canvas
incr __imageno 1
}
proc test {} {
puts "hello"
}

View file

@ -0,0 +1,47 @@
// Special module to run the mandlebrot set
%module gifplot
%include gifplot.i
%inline %{
void mandel(Plot2D *p2, int tol) {
double scalingx;
double scalingy;
double zr,zi,ztr,zti,cr,ci;
double cscale;
int i,j,n;
FrameBuffer *f;
f = p2->frame;
scalingx = (p2->xmax-p2->xmin)/f->width;
scalingy = (p2->ymax-p2->ymin)/f->height;
cscale = 239.0/tol;
printf("working...\n");
for (i = 0; i < f->width; i++) {
for (j = 0; j < f->height; j++) {
zr = scalingx*i + p2->xmin;
zi = scalingy*j + p2->ymin;
cr = zr;
ci = zi;
n = 0;
while (n < tol) {
ztr = zr*zr-zi*zi + cr;
zti = 2*zr*zi + ci;
zr = ztr;
zi = zti;
if (ztr*ztr + zti*zti > 20) break;
n = n + 1;
}
if (n >= tol) FrameBuffer_plot(f,i,j,BLACK);
else FrameBuffer_plot(f,i,j,16+(int) (n*cscale));
}
if ((i % 10) == 0) printf("%d\n",i);
}
}
%}

View file

@ -0,0 +1,171 @@
catch { load ./gifplot.so }
catch { load ./gifplot.dll } ; # Windows
source display.tcl
set tcl_precision 17
set f [FrameBuffer -args 400 400]
set cmap [ColorMap -args cmap]
set p2 [Plot2D -args $f -3 -2 1 2]
set xmin -3
set xmax 1
set ymin -2.0
set ymax 2.0
set tolerance 240
set filename mandel.gif
# Make a plot from the above parms
proc make_plot {} {
global p2 cmap tolerance
global xmin ymin xmax ymax filename
$p2 setrange $xmin $ymin $xmax $ymax
$p2 start
. config -cursor watch
update
mandel $p2 $tolerance
. config -cursor arrow
[$p2 cget -frame] writeGIF $cmap $filename
display_image $filename $p2 set_zoom
}
# Take some screen coordinates and set global min and max values
proc set_zoom {p2 mxmin mymin mxmax mymax x1 y1 x2 y2} {
global xmin ymin xmax ymax
set frame [$p2 cget -frame]
set width [$frame cget -width]
set height [$frame cget -height]
if {$x1 < 0} {set x1 0}
if {$x1 > ($width)} {set x1 $width}
if {$x2 < 0} {set x2 0}
if {$x2 > ($width)} {set x2 $width}
if {$x1 < $x2} {set ixmin $x1; set ixmax $x2} {set ixmin $x2; set ixmax $x1}
if {$y1 < 0} {set y1 0}
if {$y1 > ($height)} {set y1 $height}
if {$y2 < 0} {set y2 0}
if {$y2 > ($height)} {set y2 $height}
if {$y1 < $y2} {set iymin $y1; set iymax $y2} {set iymin $y2; set iymax $y1}
# Now determine new min and max values based on screen location
set xmin [expr {$mxmin + ($mxmax-$mxmin)*($ixmin)/($width)}]
set xmax [expr {$mxmin + ($mxmax-$mxmin)*($ixmax)/($width)}]
set ymin [expr {$mymin + ($mymax-$mymin)*(($height)-($iymax))/($height)}]
set ymax [expr {$mymin + ($mymax-$mymin)*(($height)-($iymin))/($height)}]
catch {make_plot}
}
# Box drag constrained to a square
proc BoxDrag { w x y} {
global box
catch {$w delete $box(last)}
set x1 [lrange $box(anchor) 0 0]
set y1 [lrange $box(anchor) 1 1]
set dx [expr {$x - $x1}]
set dy [expr {$y - $y1}]
if {abs($dy) > abs($dx)} {set dx $dy}
set newx [expr {$x1 + $dx}]
set newy [expr {$y1 + $dx}]
set box(last) [eval {$w create rect} $box(anchor) {$newx $newy -tag box -outline white}]
}
proc BoxFinish {w x y p2 mxmin mymin mxmax mymax func } {
global box
set start $box(anchor)
set x1 [lrange $box(anchor) 0 0]
set y1 [lrange $box(anchor) 1 1]
set dx [expr {$x - $x1}]
set dy [expr {$y - $y1}]
if {($dx == 0) || ($dy == 0)} {
catch {$w delete $box(last)}
return
}
if {abs($dy) > abs($dx)} {set dx $dy}
set newx [expr {$x1 + $dx}]
set newy [expr {$y1 + $dx}]
$w config -cursor watch
update
# Call the handler function
$func $p2 $mxmin $mymin $mxmax $mymax $x1 $y1 $newx $newy
catch {$w delete $box(last)}
$w config -cursor arrow
}
# Create a few frames
wm title . Mandelbrot
frame .title -relief groove -borderwidth 1
label .title.name -text "Mandelbrot Set"
button .title.quit -text "Quit" -command "exit"
button .title.about -text "About" -command "about"
pack .title.name -side left
pack .title.quit .title.about -side right
frame .func -relief groove -borderwidth 1
frame .func.xrange
label .func.xrange.xrlabel -text "X range" -width 12
entry .func.xrange.xmin -textvar xmin -width 8
label .func.xrange.xtolabel -text "to"
entry .func.xrange.xmax -textvar xmax -width 8
pack .func.xrange.xrlabel .func.xrange.xmin .func.xrange.xtolabel .func.xrange.xmax -side left
frame .func.yrange
label .func.yrange.yrlabel -text "Y range" -width 12
entry .func.yrange.ymin -textvar ymin -width 8
label .func.yrange.ytolabel -text "to"
entry .func.yrange.ymax -textvar ymax -width 8
pack .func.yrange.yrlabel .func.yrange.ymin .func.yrange.ytolabel .func.yrange.ymax -side left
frame .func.npoints
label .func.npoints.label -text "Tolerance " -width 12
entry .func.npoints.npoints -textvar tolerance -width 8
scale .func.npoints.scale -from 0 -to 2500 -variable tolerance -orient horizontal -showvalue false \
-sliderlength 13 -bigincrement 10 -resolution 10
pack .func.npoints.label .func.npoints.npoints .func.npoints.scale -side left
pack .func.xrange .func.yrange .func.npoints -side top -fill x
# Filename dialog
frame .save -relief groove -borderwidth 1
frame .save.file
label .save.file.label -text "Save as" -width 12
entry .save.file.filename -textvar filename -width 20
pack .save.file.label .save.file.filename -side left
pack .save.file -side left -fill x
button .save.go -text "Plot" -command "make_plot"
pack .save.go -side right
bind .save.file.filename <Return> {make_plot}
pack .title .func .save -side top -fill both
proc about { } {
toplevel .about -width 350
message .about.m -text "\
Mandelbrot Set\n\n\
Copyright (c) 1997\n\
Dave Beazley\n\
University of Utah\n\n\
Creates a plot of the Mandelbrot set. Any displayed image can be zoomed by clicking and \
dragging. Although the main calculation is written in C, it may take awhile for each \
image to be calculated (be patient). Image quality can be improved at the expense of speed \
by increasing the tolerance value.\n"
button .about.okay -text "Ok" -command {destroy .about}
pack .about.m .about.okay -side top
focus .about.okay
}
make_plot

View file

@ -0,0 +1,23 @@
TOP = ../../..
SWIG = $(TOP)/../swig
SWIGOPT =
SRCS =
TARGET = simple
INTERFACE = simple.i
LIBS = -L../.. -lgifplot
INCLUDE = -I../../Include
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl
static::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
INCLUDE='$(INCLUDE)' LIBS='$(LIBS)' SWIGOPT='$(SWIGOPT)' \
TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh
clean::
rm -f *_wrap* *.o *~ *.so mytclsh .~* core *.gif

View file

@ -0,0 +1,5 @@
This is a very minimalistic example in which just a few functions
and constants from library are wrapped and used to draw some simple
shapes. The script 'runme.tcl' runs the example.

View file

@ -0,0 +1,28 @@
# Draw some simple shapes
puts "Drawing some basic shapes"
catch { load ./simple.so simple}
catch { load ./simple.dll simple} ;# Windows
set cmap [new_ColorMap]
set f [new_FrameBuffer 400 400]
# Clear the picture
FrameBuffer_clear $f $BLACK
# Make a red box
FrameBuffer_box $f 40 40 200 200 $RED
# Make a blue circle
FrameBuffer_circle $f 200 200 40 $BLUE
# Make green line
FrameBuffer_line $f 10 390 390 200 $GREEN
# Write an image out to disk
FrameBuffer_writeGIF $f $cmap image.gif
puts "Wrote image.gif"
delete_FrameBuffer $f
delete_ColorMap $cmap

View file

@ -0,0 +1,38 @@
/* This example shows a very simple interface wrapping a few
primitive declarations */
%module simple
%{
#include "gifplot.h"
%}
typedef unsigned char Pixel;
/* Here are a few useful functions */
ColorMap *new_ColorMap(char *filename = 0);
void delete_ColorMap(ColorMap *cmap);
FrameBuffer *new_FrameBuffer(unsigned int width, unsigned int height);
void delete_FrameBuffer(FrameBuffer *frame);
void FrameBuffer_clear(FrameBuffer *frame, Pixel color);
void FrameBuffer_line(FrameBuffer *frame, int x1, int y1, int x2, int y2, Pixel color);
void FrameBuffer_box(FrameBuffer *frame, int x1, int y1, int x2, int y2, Pixel color);
void FrameBuffer_circle(FrameBuffer *frame, int x1, int y1, int radius, Pixel color);
int FrameBuffer_writeGIF(FrameBuffer *f, ColorMap *c, char *filename);
/* And some useful constants */
#define BLACK 0
#define WHITE 1
#define RED 2
#define GREEN 3
#define BLUE 4
#define YELLOW 5
#define CYAN 6
#define MAGENTA 7