| |
| /pliant/math/transform.pli |
| |
| 1 |
submodule "point.pli" | |
| 2 |
submodule "vector.pli" | |
| 3 |
submodule "functions.pli" | |
| 4 |
module "matrix.pli" | |
| 5 |
| |
| 6 |
doc | |
| 7 |
[If zero is a bottom left, then rotate is counterclockwise, just as in PostScript coordinate system.] ; eol | |
| 8 |
[If zero is a top left, than rotate is clockwise] | |
| 9 |
| |
| 10 |
public | |
| 11 |
constant transform_identity 0 | |
| 12 |
constant transform_translate 1 | |
| 13 |
constant transform_scale 2 | |
| 14 |
constant transform_rotate 3 | |
| 15 |
| |
| 16 |
type Transform2 | |
| 17 |
field Float xx xy xt | |
| 18 |
field Float yx yy yt | |
| 19 |
field Int level | |
| 20 |
| |
| 21 |
| |
| 22 |
method t compute | |
| 23 |
arg_rw Transform2 t | |
| 24 |
if t:xy<>0 or t:yx<>0 | |
| 25 |
t level := transform_rotate | |
| 26 |
eif t:xx<>1 or t:yy<>1 | |
| 27 |
t level := transform_scale | |
| 28 |
eif t:xt<>0 or t:yt<>0 | |
| 29 |
t level := transform_translate | |
| 30 |
else | |
| 31 |
t level := transform_identity | |
| 32 |
| |
| 33 |
| |
| 34 |
function transform -> t | |
| 35 |
arg Transform2 t | |
| 36 |
t xx := 1 | |
| 37 |
t xy := 0 | |
| 38 |
t xt := 0 | |
| 39 |
t yx := 0 | |
| 40 |
t yy := 1 | |
| 41 |
t yt := 0 | |
| 42 |
t level := transform_identity | |
| 43 |
| |
| 44 |
function transform tx ty sx sy rx ry -> t | |
| 45 |
arg Float tx ty sx sy rx ry ; arg Transform2 t | |
| 46 |
if (rx<>0 or ry<>0) | |
| 47 |
t xx := sx*cos:rx | |
| 48 |
t xy := -sy*sin:ry | |
| 49 |
t xt := tx | |
| 50 |
t yx := sx*sin:rx | |
| 51 |
t yy := sy*cos:ry | |
| 52 |
t yt := ty | |
| 53 |
else | |
| 54 |
t xx := sx | |
| 55 |
t xy := 0 | |
| 56 |
t xt := tx | |
| 57 |
t yx := 0 | |
| 58 |
t yy := sy | |
| 59 |
t yt := ty | |
| 60 |
t compute | |
| 61 |
| |
| 62 |
| |
| 63 |
method t matrix -> m | |
| 64 |
arg Transform2 t ; arg Matrix m | |
| 65 |
m resize 3 3 | |
| 66 |
m 0 0 := t xx | |
| 67 |
m 0 1 := t xy | |
| 68 |
m 0 2 := t xt | |
| 69 |
m 1 0 := t yx | |
| 70 |
m 1 1 := t yy | |
| 71 |
m 1 2 := t yt | |
| 72 |
m 2 0 := 0 | |
| 73 |
m 2 1 := 0 | |
| 74 |
m 2 2 := 1 | |
| 75 |
| |
| 76 |
function transform m -> t | |
| 77 |
arg Matrix m ; arg Transform2 t | |
| 78 |
check m:nb_lines=3 and m:nb_columns=3 | |
| 79 |
t xx := m 0 0 | |
| 80 |
t xy := m 0 1 | |
| 81 |
t xt := m 0 2 | |
| 82 |
t yx := m 1 0 | |
| 83 |
t yy := m 1 1 | |
| 84 |
t yt := m 1 2 | |
| 85 |
t compute | |
| 86 |
| |
| 87 |
function compose t1 t2 -> t | |
| 88 |
arg Transform2 t1 t2 t | |
| 89 |
if t1:level<=transform_translate and t2:level<=transform_translate | |
| 90 |
t xx := 1 | |
| 91 |
t xy := 0 | |
| 92 |
t xt := t1:xt+t2:xt | |
| 93 |
t yx := 0 | |
| 94 |
t yy := 1 | |
| 95 |
t yt := t1:yt+t2:yt | |
| 96 |
t level := shunt t:xt<>0 or t:yt<>0 transform_translate transform_identity | |
| 97 |
else | |
| 98 |
t := transform t2:matrix*t1:matrix | |
| 99 |
| |
| 100 |
function compose t1 t2 t3 -> t | |
| 101 |
arg Transform2 t1 t2 t3 t | |
| 102 |
t := transform t3:matrix*t2:matrix*t1:matrix | |
| 103 |
| |
| 104 |
function reverse t -> inv | |
| 105 |
arg Transform2 t inv | |
| 106 |
if t:level<=transform_translate | |
| 107 |
inv := t | |
| 108 |
inv xt := -(t xt) | |
| 109 |
inv yt := -(t yt) | |
| 110 |
else | |
| 111 |
inv := transform t:matrix^(-1) | |
| 112 |
| |
| 113 |
| |
| 114 |
method t '' p -> q | |
| 115 |
arg Transform2 t ; arg Point2 p q | |
| 116 |
if t:level=transform_identity | |
| 117 |
q := p | |
| 118 |
eif t:level=transform_translate | |
| 119 |
q := point p:x+t:xt p:y+t:yt | |
| 120 |
eif t:level=transform_scale | |
| 121 |
q := point p:x*t:xx+t:xt p:y*t:yy+t:yt | |
| 122 |
else | |
| 123 |
q := point p:x*t:xx+p:y*t:xy+t:xt p:x*t:yx+p:y*t:yy+t:yt | |
| 124 |
| |
| 125 |
method t '' v -> w | |
| 126 |
arg Transform2 t ; arg Vector2 v w | |
| 127 |
if t:level<=transform_translate | |
| 128 |
w := v | |
| 129 |
eif t:level=transform_scale | |
| 130 |
w := vector v:x*t:xx v:y*t:yy | |
| 131 |
else | |
| 132 |
w := vector v:x*t:xx+v:y*t:xy v:x*t:yx+v:y*t:yy | |
| 133 |
| |
| 134 |
| |
| 135 |
export Transform2 '. compute' transform '' '. matrix' compose reverse | |
| |