added plot3d example; some cleanups

cvs
Slava Pestov 2005-05-23 02:54:14 +00:00
parent d43710eece
commit 97af819ffb
6 changed files with 179 additions and 130 deletions

View File

@ -9,31 +9,19 @@
- single-stepper and variable access: wrong namespace?
- [ over ] generics no-method
- investigate if COPYING_GEN needs a fix
- simplifier:
- kill tag-fixnum/untag-fixnum
- kill replace after a peek
- merge inc-d's across VOPs that don't touch the stack
- faster layout
- tiled window manager
- c primitive arrays: or just specialized arrays
float, complex, byte, char, cell...
- add a socket timeout
- virtual hosts
- keep alive
- sleep word
- redo new compiler backend for PowerPC
- plugin: supportsBackspace
- if external factor is down, don't add tons of random shit to the
dictionary
- SDL_Rect** type
- fix i/o on generic x86/ppc unix
- alien primitives need a more general input type
- 2map slow with lists
- nappend: instead of using push, enlarge the sequence with set-length
then add set the elements with set-nth
- faster sequence operations
- generic some? all? memq? all=?
- generic some? all? memq? fiber?
- index and index* are very slow with lists
- code walker & exceptions
- if two tasks write to a unix stream, the buffer can overflow
@ -42,7 +30,6 @@
- dipping seq-2nmap, seq-2each
- array sort
- images saved from plugin do not work
- making an image from plugin hangs
- generic skip
- inference needs to be more robust with heavily recursive code
- investigate orphans
@ -50,9 +37,13 @@
+ plugin:
- word preview for parsing words
- plugin: supportsBackspace
- if external factor is down, don't add tons of random shit to the
dictionary
+ ui:
- tiled window manager
- faster repaint
- console with presentations
- ui browser
@ -76,6 +67,10 @@
+ compiler:
- simplifier:
- kill tag-fixnum/untag-fixnum
- kill replace after a peek
- merge inc-d's across VOPs that don't touch the stack
- [ EAX 0 ] --> [ EAX ]
- intrinsic char-slot set-char-slot integer-slot set-integer-slot
- optimize the generic word prologue
@ -93,6 +88,7 @@
+ sequences
- specialized arrays
- list map, subset: not tail recursive
- phase out sbuf-append

View File

@ -2359,7 +2359,7 @@ Outputs a new list containing all elements of the \texttt{list} except \texttt{o
Outputs a list of elements present in both lists.
\wordtable{
\vocabulary{lists}
\ordinaryword{intersection}{difference ( l1 l2 -- list )}
\ordinaryword{difference}{difference ( l1 l2 -- list )}
}
Outputs a list of elements present in \texttt{l2} but not \texttt{l1}.
@ -2533,6 +2533,11 @@ String buffers support the stream output protocol (\ref{stream-protocol}).
\subsection{\label{virtual-seq}Virtual sequences}
\glossary{name=virtual sequence,
description={a sequence that is not backed by actual storage, but instead either computes its values, or take them from an underlying sequence}}
\glossary{name=repeated sequence,
description={an instance of the \texttt{repeated} class, which is a virtual, immutable sequence consisting of a fixed element repeated a certain number of times}}
Virtual sequences are not backed by actual storage, but instead either compute their values, or take them from an underlying sequence.
\wordtable{
@ -2547,6 +2552,8 @@ Creates an immutable sequence consisting of \verb|object| repeated $n$ times. No
[ "hey" "hey" "hey" "hey" "hey" ]
\end{alltt}
\glossary{name=range sequence,
description={an instance of the \texttt{range} class, which is a virtual sequence of integers}}
\wordtable{
\vocabulary{sequences}
\ordinaryword{<range>}{<range> ( a b -- seq )}
@ -2560,6 +2567,8 @@ Creates an immutable sequence consisting of all integers in the interval $[a,b)$
\textbf{ok} CHAR: z CHAR: a 1 - <range> >string .
"zyxwvutsrqponmlkjihgfedcba"
\end{alltt}
\glossary{name=slice,
description={an instance of the \texttt{slice} class, which is a virtual sequence sharing structure with a subrange of some underlying sequence}}
\wordtable{
\vocabulary{sequences}
@ -2583,16 +2592,19 @@ Creates a slice viewing all elements of the sequence, starting at the $n$th inde
}
Creates a slice viewing the last $n$ elements of the input sequence.
There is a natural duality between the four slicing words above, and the four subsequence words from \ref{subseq}:
There is a correspondence between the four slicing words above, and the subsequence words from \ref{subseq}:
\begin{tabular}[t]{l|l|l}
\begin{tabular}[t]{l|l}
Subsequence&Slice\\
\hline
\verb|subseq|&\verb|<slice>|\\
\verb|head|&\verb|head-slice|\\
\verb|tail|&\verb|tail-slice|\\
\verb|tail*|&\verb|tail-slice*|
\end{tabular}
The slice words output a new virtual sequence that shares structure with the original sequence, whereas the subsequence words output a fresh copied sequence.
\subsection{\label{make-seq}Constructing sequences}
The library supports an idiom where sequences can be constructed without passing the partial sequence being built on the stack. This reduces stack noise, and thus simplifies code and makes it easier to understand.

View File

@ -1,112 +0,0 @@
! Rotating 3d cube.
!
! To run this code, bootstrap Factor like so:
!
! ./f boot.image.le32
! -libraries:sdl:name=libSDL.so
! -libraries:sdl-gfx:name=libSDL_gfx.so
!
! (But all on one line)
!
! Then, start Factor as usual (./f factor.image) and enter this
! at the listener:
!
! "examples/cube3d.factor" run-file
IN: cube3d
USING: compiler kernel lists math matrices namespaces sdl
sequences ;
! A 2x2x2 cube.
: points
[
[[ { 1 1 1 } { 1 1 -1 } ]]
[[ { 1 1 1 } { 1 -1 1 } ]]
[[ { 1 1 1 } { -1 1 1 } ]]
[[ { -1 1 1 } { -1 1 -1 } ]]
[[ { -1 1 1 } { -1 -1 1 } ]]
[[ { 1 -1 1 } { -1 -1 1 } ]]
[[ { 1 -1 1 } { 1 -1 -1 } ]]
[[ { 1 1 -1 } { -1 1 -1 } ]]
[[ { 1 1 -1 } { 1 -1 -1 } ]]
[[ { -1 1 -1 } { -1 -1 -1 } ]]
[[ { -1 -1 1 } { -1 -1 -1 } ]]
[[ { 1 -1 -1 } { -1 -1 -1 } ]]
] ;
: 3vector ( x y z -- { x y z } )
[ rot , swap , , ] make-vector ;
: rotation-matrix-1 ( theta -- )
[
dup cos , dup sin , 0 ,
dup sin neg , cos , 0 ,
0 , 0 , 1 ,
] make-vector 3 3 rot <matrix> ;
: rotation-matrix-2 ( theta -- )
[
1 , 0 , 0 ,
0 , dup cos , dup sin ,
0 , dup sin neg , cos ,
] make-vector 3 3 rot <matrix> ;
: rotation-matrix-3 ( theta -- )
[
dup cos , 0 , dup sin neg ,
0 , 1 , 0 ,
dup sin , 0 , cos ,
] make-vector 3 3 rot <matrix> ;
SYMBOL: theta
SYMBOL: phi
SYMBOL: psi
SYMBOL: rotation
: update-matrix
theta get rotation-matrix-1
phi get rotation-matrix-2 m.
psi get rotation-matrix-3 m. rotation set ;
: >scene ( { x y z } -- { x y z } )
rotation get swap m.v ;
: >screen ( { x y z } -- x y )
200 swap n*v width get 2 / height get 2 / 0 3vector v+
0 over nth 1 rot nth ;
: redraw ( -- )
surface get 0 0 width get height get black rgb boxColor
points [
uncons >r >r surface get
r> >scene >screen
r> >scene >screen
red rgb lineColor
] each ;
: event-loop ( event -- )
theta [ 0.01 + ] change
phi [ 0.02 + ] change
psi [ 0.03 + ] change
update-matrix
[ redraw ] with-surface
dup SDL_PollEvent [
dup event-type SDL_QUIT = [
drop
] [
event-loop
] ifte
] [
event-loop
] ifte ;
: cube3d ( -- )
800 600 0 SDL_HWSURFACE [
0 theta set
0 phi set
0 psi set
<event> event-loop SDL_Quit
] with-screen ;
cube3d

154
examples/plot3d.factor Normal file
View File

@ -0,0 +1,154 @@
! 3d surface plotter.
!
! To run this code, bootstrap Factor like so:
!
! ./f boot.image.le32
! -libraries:sdl:name=libSDL.so
! -libraries:sdl-gfx:name=libSDL_gfx.so
!
! (But all on one line)
!
! Then, start Factor as usual (./f factor.image) and enter this
! at the listener:
!
! "contrib/gl/load.factor" run-file
! "examples/plot3d.factor" run-file
IN: plot3d
USING: alien compiler errors gl kernel lists math matrices
namespaces prettyprint sdl sequences ;
: display-list 1 ;
: plot-vertex ( matrix i j -- )
rot matrix-get 3unlist glVertex3f ;
: plot-face ( matrix i j -- face )
GL_QUADS glBegin
[ rot matrix-get ] 3keep
[ 1 + rot matrix-get v- ] 3keep
[ rot matrix-get ] 3keep
[ >r 1 + r> rot matrix-get v- cross normalize >list 3unlist glNormal3f ] 3keep
[ plot-vertex ] 3keep
[ 1 + plot-vertex ] 3keep
[ >r 1 + r> 1 + plot-vertex ] 3keep
>r 1 + r> plot-vertex
glEnd ;
: plot-faces ( points -- )
dup matrix-rows 1 - over matrix-cols 1 - [
3dup plot-face
] 2repeat drop ;
SYMBOL: theta
: plot-axes ( -- )
GL_LIGHTING glDisable
1.0 1.0 1.0 glColor3f
GL_LINES glBegin
0 0 0 glVertex3f
1 0 0 glVertex3f
0 0 0 glVertex3f
-1 0 0 glVertex3f
0 0 0 glVertex3f
0 1 0 glVertex3f
0 0 0 glVertex3f
0 -1 0 glVertex3f
0 0 0 glVertex3f
0 0 1 glVertex3f
0 0 0 glVertex3f
0 0 -1 glVertex3f
glEnd
GL_LIGHTING glEnable ;
: i/j>x/y ( i j -- x y )
swap 15 - 30 / swap 15 - 30 / ;
: max-z ( seq -- z )
0.1 swap [ 2 swap nth max ] each ;
: min-z ( seq -- z )
-0.1 swap [ 2 swap nth min ] each ;
: normalize-points ( seq -- )
dup min-z over [ over >r 3unlist r> - 3list ] nmap drop
dup max-z swap [ over >r 3unlist r> / 3list ] nmap drop ;
: valuate-points ( quot -- matrix )
>r 30 30 r>
[ i/j>x/y ] swap unit [ 2keep rot 3list ] append3
make-matrix ;
: make-plot
[ rect> sq exp real ] valuate-points
dup matrix-sequence normalize-points
display-list GL_COMPILE glNewList
plot-faces
plot-axes
glEndList ;
: flags
SDL_OPENGL SDL_RESIZABLE bitor SDL_HWSURFACE bitor SDL_DOUBLEBUF bitor ;
: fov 60.0 ; inline
: near 0.1 ; inline
: far 100.0 ; inline
: >float-array ( seq -- float-array )
dup length <float-array> over length [
[ tuck >r >r swap nth r> r> swap set-float-nth ] 3keep
] repeat nip ;
: init-gl
GL_PROJECTION glMatrixMode
GL_DEPTH_TEST glEnable
GL_LIGHTING glEnable
GL_LIGHT0 glEnable
GL_LIGHT1 glEnable
glLoadIdentity
fov width get height get /f near far gluPerspective
GL_LIGHT0 GL_POSITION [ 1.0 1.0 -2.0 1.0 ] >float-array glLightfv
GL_LIGHT0 GL_DIFFUSE [ 1.0 0.5 0.0 1.0 ] >float-array glLightfv
GL_LIGHT0 GL_SPECULAR [ 1.0 0.5 1.0 1.0 ] >float-array glLightfv
GL_LIGHT0 GL_AMBIENT [ 1.0 1.0 0.5 1.0 ] >float-array glLightfv
GL_LIGHT1 GL_POSITION [ 1.0 3.0 2.0 -1.0 ] >float-array glLightfv
GL_LIGHT1 GL_DIFFUSE [ 1.0 0.5 0.3 1.0 ] >float-array glLightfv
GL_LIGHT1 GL_SPECULAR [ 1.0 1.0 0.5 1.0 ] >float-array glLightfv
GL_LIGHT1 GL_AMBIENT [ 0.0 0.0 1.0 1.0 ] >float-array glLightfv
GL_MODELVIEW glMatrixMode
glLoadIdentity
GL_SMOOTH glShadeModel
0.0 0.0 0.0 0.0 glClearColor
1.0 0.0 0.0 glColor3f ;
: render ( -- )
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
GL_MODELVIEW glMatrixMode
glLoadIdentity
0.0 -0.5 -1.5 glTranslatef
-45 1 0 0 glRotatef
theta get 0 0 1 glRotatef
display-list glCallList
SDL_GL_SwapBuffers ;
: event-loop ( event -- )
theta [ 1 + ] change
render
dup SDL_PollEvent [
dup event-type SDL_QUIT = [
drop
] [
event-loop
] ifte
] [
event-loop
] ifte ;
: plot3d ( -- )
1024 768 16 flags [
init-gl
0 theta set
make-plot
<event> event-loop SDL_Quit
] with-screen ;

View File

@ -65,7 +65,6 @@ t [
"/library/tools/profiler.factor"
"/library/tools/walker.factor"
"/library/tools/annotations.factor"
"/library/tools/dump.factor"
"/library/bootstrap/image.factor"
"/library/io/logging.factor"