David Lambert/HXF

From J Wiki
Jump to navigation Jump to search

The file of transformations loaded by the lab. I name this HomoXFormStandard.ijs .

DEBUG=:0
debug=:([13!:0)@:".@:('DEBUG=:-.DEBUG'"_)

Note'use'
 Specify angles in degrees.

 NEW_COORDINATES=: OLD_COORDINATES mp TRANSFORMATION_MATRIX

 COORDINATES given N points, the shape is (N , 4)
  x0 y0 z0 h0
  x1 y1 z1 h1
  x2 y2 z2 h2
  ...
)

require'trig'

Monad=: :[:
Dyad=: [: :
Describe=: 2 : 'u&:([n"_)"u'
Description=: Describe

mp=: ($: |:) : (+/ .*)
round=: (1&$: : ([ * [: <. 1r2 + %~)) Description'rounds y to nearest x'
assert 3 3.1 3.14 3.142 -: (10 ^ - i. 4) round 1p1

identity=: =@:i.
det=: -/ .* Describe 'Determinant'
normalize=: (% %:@:mp) Monad Description'scales vector length to unity'
cross=: 1 |. ([ * 1 |. ]) - ] * 1 |. [ NB. j forum
cross=: (1 |. (* 1&|.) - (* 1&|.)~) Description 'x cross y yields their cross product'
assert 0 0  1 -: 1 0 0 cross  0 1 0
assert 0 0 _1 -: 1 0 0 cross~ 0 1 0

'I0 I1 I2 I3 I4' =: identity&.> i. 5
'X1 Y1 Z1 ORIGIN' =: ,:"1 |: I3 , 4#1
'XX XY XZ YX YY YZ ZX ZY ZZ' =: <"0 , |: { ,~ < i. 3  NB. X is influenced by the value of Y

Transformation=: conjunction define
 (v m) mp y
:
 x mp (v m) mp y
)

translation=: (I3 , 1 ,~ 3 {. -) Description 'translation shifts the origin, not the body!'
scaling=: (I4 * 4 {.!.1 ]) Description'scaling 1 3 yields a scaling matrix intended to scale the second coordinate by 3'

Translate=: Transformation translation (Description'(COORDINATES or MATRIX) translate (X [Y [Z]])')
Scale=: Transformation scaling (Description'COORDS (X[Y[Z]]factor vector)  Scale')

Shearing=: adverb def 'm}&I4'
shearingxx=: XX Shearing
shearingxy=: XY Shearing   NB. how X changes depending on Y
shearingxz=: XZ Shearing
shearingyx=: YX Shearing
shearingyy=: YY Shearing
shearingyz=: YZ Shearing
shearingzx=: ZX Shearing
shearingzy=: ZY Shearing
shearingzz=: ZZ Shearing

NB. modify individual components
ShearXX=: Transformation (shearingxx Description 'Same as Fx Scale')
ShearXY=: Transformation (shearingxy Description 'Coordinate X accumulates a scaled contribution of Y')
ShearXZ=: Transformation (shearingxz Description 'Coordinate X accumulates a scaled contribution of Z')
ShearYX=: Transformation (shearingyx Description 'Coordinate Y accumulates a scaled contribution of X')
ShearYY=: Transformation (shearingyy Description 'Same as (1 , Fy) Scale')
ShearYZ=: Transformation (shearingyz Description 'Coordinate Y accumulates a scaled contribution of Z')
ShearZX=: Transformation (shearingzx Description 'Coordinate Z accumulates a scaled contribution of X')
ShearZY=: Transformation (shearingzy Description 'Coordinate Z accumulates a scaled contribution of Y')
ShearZZ=: Transformation (shearingzz Description 'Same as (1 1 , Fz) Scale')

reflection=: monad def 'y Shearing _1'
Reflect=: Transformation (reflection Description'eg.  COORDINATES AXIS reflect MATRIX  Axis is XX, YY, or ZZ')

axisRotation=: I2&(((-.+:>/~i.2) * -.@:[ * sind@:]) + (* cosd))
rotationx=: ORIGIN ,~ 1 0 0 , 0 ,. axisRotation
rotationy=: (,@:(I2&(((-.+:</~i.2) * -.@:[ * sind@:]) + (* cosd)))@:[`((XX,ZX,XZ,ZZ)"_)`]})&I4
rotationz=: ORIGIN ,~ 0 0 1 ,~ 0 ,.~ axisRotation

RotateX=: Transformation (rotationx Description'COORDINATES DEGREES Rotatex MATRIX')
RotateY=: Transformation (rotationy Description'COORDS Rotatey ANGLE_IN_DEGREES')
RotateZ=: Transformation (rotationz Description'COORDINATES DEGREES Rotatez MATRIX')
Note 'sample'
   round 90 RotateZ I4
 0 1 0 0
_1 0 0 0
 0 0 1 0
 0 0 0 1
)

rotationu=: 4 : 0&:>/ Describe 'rotationu ANGLE ; VECTOR yields the right handed rotation matrix about a vector'
 ANGLE=. x
 'A B C'=. - 'X Y Z' =. U =. normalize 3 {. y
 UUT=. (,.mp,:)U
 ORIGIN,~|:(((0,C,Y),(Z,0,A),:(B,X))*sind ANGLE)+((I3-UUT)*cosd ANGLE)+UUT
)
RotateU=: Transformation (rotationu Description 'rotation about U, COORDS (ANGLE ; VECTOR ) RotateU MATRIX')
Note'sample'
 1r1024 round (X1,Y1,Z1)(90;100 1)RotateU I4
    1 5r512 _5r512 1
5r512     0      1 1
5r512    _1      0 1
)

NB. Intrinsic Euler rotations
rotationxzx=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$C2,(C1*S2),(S1*S2),(-C3*S2),(C-(S1*S3)),((C1*S3)+(C2*C3*S1)),(S2*S3),(-((C3*S1)+(C1*C2*S3))),((C1*C3)-(C2*S1*S3))
)
rotationxyx=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$C2,(S1*S2),(-C1*S2),(S2*S3),((C1*C3)-(C2*S1*S3)),((C3*S1)+(C1*C2*S3)),(C3*S2),(-(C1*S3)+(C2*C3*S1)),(C-(S1*S3))
)
rotationyxy=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$((C1*C3)-(C2*S1*S3)),(S2*S3),(-(C3*S1)+(C1*C2*S3)),(S1*S2),(C2),(C1*S2),((C1*S3)+(C2*C3*S1)),(-C3*S2),(C-(S1*S3))
)
rotationyzy=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$(C-(S1*S3)),(C3*S2),(-(C1*S3)+(C2*C3*S1)),(-C1*S2),(C2),(S1*S2),((C3*S1)+(C1*C2*S3)),(S2*S3),(C1*C3)-(C2*S1*S3)
)
rotationzyz=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$(C-(S1*S3)),((C1*S3)+(C2*C3*S1)),(-C3*S2),(-(C3*S1)+(C1*C2*S3)),((C1*C3)-(C2*S1*S3)),(S2*S3),(C1*S2),(S1*S2),(C2)
)
rotationzxz=: monad define
 ANGLE =. 3 {. y
 'S1 S2 S3' =. sind ANGLE
 C=. */ 'C1 C2 C3' =. cosd ANGLE
 ORIGIN,~3 3$((C1*C3)-(C2*S1*S3)),((C3*S1)+(C1*C2*S3)),(S2*S3),(-(C1*S3)+(C2*C3*S1)),(C-(S1*S3)),(C3*S2),(S1*S2),(-C1*S2),(C2)
)
RotateXZX=: Transformation rotationxzx(Description'successive rotation angles about these axes')
RotateXYX=: Transformation rotationxyx(Description'successive rotation angles about these axes')
RotateYXY=: Transformation rotationyxy(Description'successive rotation angles about these axes')
RotateYZY=: Transformation rotationyzy(Description'successive rotation angles about these axes')
RotateZYZ=: Transformation rotationzyz(Description'successive rotation angles about these axes')
RotateZXZ=: Transformation rotationzxz(Description'successive rotation angles about these axes')

NB. demonstrate plausability of the rotation matrices.
NB. Lengths of rows and columns of rotation matrix is 1.
NB. See the rotations lab.
assert ((8#1) -: mp"1 , (mp"1)@:|:) (?360) RotateX I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (?360) RotateY I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (?360) RotateZ I4

assert ((8#1) -: mp"1 , (mp"1)@:|:) ((?360);(-.+:3?@#0)) RotateU I4

assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateXZX I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateXYX I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateYXY I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateYZY I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateZYZ I4
assert ((8#1) -: mp"1 , (mp"1)@:|:) (3?@#360) RotateZXZ I4

Anagram=: Transformation (ORIGIN ,~ I3 A.~ 6&|) (Description '0 Anagram  identity; 1 Anagram  y<->z;  2 Anagram  x<->y; 3 Anagram  x->y, y->z, z->x; 4 Anagram  x->z, y->x, z->y; 5 Anagram  x<->z.')

NB.focus=: (I3 , 4 {. _3 {. %) Dyad Transformation
NB.
NB.perspective=: dyad define NB. (ANGLE;EYE;FOCUS;FOCAL_DISTANCE)perspective POINTS
NB. 'ANGLE EYE FOCUS FOCAL_DISTANCE' =. x NB. scalar vector  vector scalar_to_screen
NB. TM =. FOCAL_DISTANCE focus ((-CAMERA_ANGLE);CAMERA_VECTOR)urotate translation EYE - FOCUS
NB. HOMOGE_COORD =.  TM mp y
NB. NORMAL_COORD =. (3&{. %"1 _1&{.) HOMOGE_COORD
NB.)
NB. m Perspect
NB.

assert (2 3 2 1,:0 6 2 1) -: (X1,Y1) (-i.3) Translate 2 3 Scale I4
assert (2 3 2 1,:0 6 2 1) -: (X1,Y1) (-i.3) Translate scaling 2 3
assert (1 0 0 1,:2 1 0 1)-:(X1,Y1) 2 ShearXY I4
assert (,:_1 0 0 1) -: X1 XX Reflect I4 
assert *./ 1e_16 > , Z1 - Y1 90 RotateX I4