2 USING: kernel namespaces
3 math math.constants math.functions math.matrices math.vectors
4 sequences splitting self ;
12 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14 ! Temporarily defined here until math-contrib gets moved to extra/
16 : deg>rad pi * 180 / ; inline
17 : rad>deg 180 * pi / ; inline
19 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 : ori> ( -- val ) self> ori-val ;
23 : >ori ( val -- ) self> set-ori-val ;
25 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 : make-matrix ( quot width -- matrix ) >r { } make r> group ;
29 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
31 ! These rotation matrices are from
32 ! `Computer Graphics: Principles and Practice'
34 : Rz ( angle -- Rx ) deg>rad
35 [ dup cos , dup sin neg , 0 ,
36 dup sin , dup cos , 0 ,
37 0 , 0 , 1 , ] 3 make-matrix nip ;
39 : Ry ( angle -- Ry ) deg>rad
40 [ dup cos , 0 , dup sin ,
42 dup sin neg , 0 , dup cos , ] 3 make-matrix nip ;
44 : Rx ( angle -- Rz ) deg>rad
46 0 , dup cos , dup sin neg ,
47 0 , dup sin , dup cos , ] 3 make-matrix nip ;
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51 : apply-rotation ( rotation -- ) ori> swap m. >ori ;
53 : rotate-x ( angle -- ) Rx apply-rotation ;
54 : rotate-y ( angle -- ) Ry apply-rotation ;
55 : rotate-z ( angle -- ) Rz apply-rotation ;
57 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59 : pitch-up ( angle -- ) neg rotate-x ;
60 : pitch-down ( angle -- ) rotate-x ;
62 : turn-left ( angle -- ) rotate-y ;
63 : turn-right ( angle -- ) neg rotate-y ;
65 : roll-left ( angle -- ) neg rotate-z ;
66 : roll-right ( angle -- ) rotate-z ;
68 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
69 ! roll-until-horizontal
70 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
72 : V ( -- V ) { 0 1 0 } ;
74 : X ( -- 3array ) ori> [ first ] map ;
75 : Y ( -- 3array ) ori> [ second ] map ;
76 : Z ( -- 3array ) ori> [ third ] map ;
78 : set-X ( seq -- ) ori> [ set-first ] 2each ;
79 : set-Y ( seq -- ) ori> [ set-second ] 2each ;
80 : set-Z ( seq -- ) ori> [ set-third ] 2each ;
82 : roll-until-horizontal ( -- )
83 V Z cross normalize set-X
84 Z X cross normalize set-Y ;