David Lambert/HXFLab

From J Wiki
Jump to navigation Jump to search

The lab itself, HomoXForm.ijt . I stored the lab under the labs/math folder and was able to run it in jqt. Change the file where you find load'/home/lambertdw/src/j/HomoXFormStandard.ijs' within. There seems to be something I don't understand about evaluating modifiers in the right locale. The runtime examples work without the lab. 2018-MAR-22: Redefining CENTER solved the problem, the cause was undetermined.

LABTITLE=: 'Homogeneous Coordinates'

NB. =========================================================
Lab Section Overview

 This lab introduces utilities for linear transformations using homogeneous coordinates,  These will rotate, shear, scale, and translate three dimensional points.

 Using homogeneous coordinates many such steps can be taken at once by dot product with a single transformation matrix.

 Homogeneous coordinates describe projective geometry and can represent points at infinity.  However, that interesting aspect is deferred to a graphics lab.

 Here the intent is to show how to use these techniques to help  construct finite element meshes which can be plotted as wire frame  graphics or become part of a physical simulation with CalculiX or  other solvers.

)

NB. =========================================================
Lab Section Coordinates

 The coordinates subject to transform are specified as an array consisting of a vector of points.  A homogeneous coordinate represents a point and has the components X, Y, Z, H.  Hence n points are stored in as a rank 2 array having shape n,4.  H is typically 1 and combines with the translational parts of the transformation matrix.  Setting H=0 defines a point at infinity.

 The coordinates are the x argument of a transformation.

 For example, the point at (3,2,0) can be specified with itemize as

)

POINT=: ,: 3 2 0 1

NB. =========================================================
Lab Section Matrix review: order of operations matters!

 Matrix multiplication associates, but does not commute.  Given matrices A, B, and C,

    (A B) C equals A (B C)  Associative property

 but

    A B unequal B A          Does not commute.

 Using POINT as commutation example, 

   Let A shift the origin to left 1 unit, and
   Let B scale the coordinate by a factor of 2.

   The translation A causes 3 2 0 1 to become 4 2 0 1,
   followed by scaling B becomes 8 4 0 1.
   (Or 4 2 0 1r2, were you wondering about H.)

   With scaling by 2 first, 3 2 0 1 becomes 6 4 0 1, then
   translating the origin by _1 becomes 7 4 0 1.

   These differ.

)
PREPARE

load'/tmp/h.ijs'

PREPARE

POINT  _1 Translate  2 2 2 Scale  I4

POINT  2 2 2 Scale  _1 Translate  I4

NB. =========================================================
Lab Section Matrix review, efficiency.

 With several matrices of different shape to multiply, the order in which they associate affects the number of multiplications and additions required for the computation.  In our case, the transformation matrices have shape 4 4, while the points can have shape (large , 4).  There is one array of points, and are many  transformation matrices.  The HomoXFormStandard.ijs design multiplies the transformation operations together, lastly taking the dot product of the points with the transformation.

)

NB. =========================================================
Lab Section Demonstrate efficiency

 To assist with code development verbs can report some information.  In this sample there are 3 transformations applied to 500 points.  The long list of 4 4 shapes come from building the transformation matrix.  The data transformation follows.  RotateU which performs a right handed rotation about an arbitrary vector uses mp resulting in the other values interspersed.

)

save_mp=: mp f.

mp=: +/ .*&:([ echo@:$)~~"(+/ .*)  NB. dot product that first prints shape of x and y

CENTER=. _2 3
empty (1 ,.~ i.500 3)  CENTER Translate  (30;2 3 4) RotateU  (-CENTER) Translate  I4

mp=: save_mp f.  NB. restore mp

NB. =========================================================
Lab Section Exercise

 The example also demonstrates the common operation of Shifting the origin to a center, rotating, then restoring the origin.  Challenge: implement a verb to rotate under translation to enable the composition

   rotation&.:under_translation

)

NB. =========================================================
Lab Section Notation

 Global noun names are written in UPPER case.

 Adverbs and conjuctions are Capitalized, and

 verbs have lower case names.

 The single requests are implemented as adverbs where m specifies the amount.  As a monad the resultant verb applies the transformation to y, intended as a 4x4 matrix.  As a dyad, it first updates the y matrix, then computes x mp this matrix which it returns.  In the examples so far the adverb chain monadically updates an initial matrix, I4, which is an identity matrix.  The left most adverb has coordinates  for x, and operates as a dyad.

)

CENTER=. _2 3

NB. compute the transformation matrix
CENTER Translate  (30;2 3 4) RotateU  (-CENTER) Translate  I4


NB. use it on the point x=1, which is predefined.
X1 CENTER Translate  (30;2 3 4) RotateU  (-CENTER) Translate  I4

NB. =========================================================
Lab Section Similar verbs

 The y  transformation matrix can be I4, the shape 4 4 identity matrix, it could specify the final transformations to which preceding transformations apply, or in most cases---all but the Anagram transformation---are verbs similarly named to the adverb which generate the matrix.  For example in place of

    (-CENTER) Translate I4

 one can instead specify

    translation -CENTER

 The individual transformations apply in order from left to right.

    m First   m Second  ...  m Final  I4

)

FINAL=: scaling 2   NB. also  2 Scale I4

POINT  _1 Translate  FINAL


FIRST=: translation _1  NB. also  _1 translate I4

POINT mp FIRST 2 Scale I4

NB. =========================================================
Lab Section Default values

 Rotation angles are measured in degrees.

 Translations shift the origin thus may be the negative of what you expect.

 Where a vector is called for, as in m or y from m Translate or translation y thee values may be specified.  The program uses 3&{. hence the values you don't supply default to 0 (1 for scale factors), and the first value is x, the first two are x and y.  To specify y=4 you can use either 0 4 or 0 4 0.

 Reflections are specified with XX, YY, or ZZ.

    ZZ Reflect   or   reflection ZZ

 Other predefined nouns are defined; it may be easier to think of these as negative shear.  These names can be determined from the 9 shears.  In a shear the coordinate of the first axis receives a contribution of the second axis.  There are many ways to achieve the same effect, particularly with rotations.

 For  m RotateU  m is a vector of 2 boxes.  The first is the angle, the second specifies the up to three component vector which defaults as 3&{.

)

(; 2 3 4 Scale & I4) X1, Y1, Z1

NB. =========================================================
Lab Section Many ways

 Already seen that _1 ShearYY is the same as YY Reflect.  A third way to achieve the same is 1 _1 Scale .  Use whichever method is easiest to think about or best matches your use.

 The module specifies rotations about each axis, rotation about an arbitrary vector direction, and six Euler rotations.  These provide a great deal of overlap.

 Finally there's an Anagram transformation with 6 possibilities to swap coordinates.  The 3 and 4 anagrams are inverses, while 1, 2, and 5 are self-inverting.  0 Anagram is identity.  Many of the names are self documenting.  J displays the definition, and the definitions include descriptive literals.

)

NB. if A matches B and B matches C then A matches C
NB. (Carpenters don't assume this.)
2 -:/\ (_1 ShearYY , 1 _1 Scale ,: YY Reflect) I4

NB. get documentation
ShearXY

NB. =========================================================
Lab Section Verification

 The matrix product mp defined in the homogenious transformation module has a monadic definition that returns the square of the length.  1 = 1^2 .  According to the rotation lab the length of each row and of each column of a rotation matrix is 1.  While these tests don't prove that the rotations are the correct rotations, it does confirm that the adverbs produce rotation matrices.  Another property of a rotation matrix is that that determinant (-/ .*) is also 1.

)

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 1 -: det ((?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

Lab Section Collected definitions

Nouns:

   I4                            4x4 identity matrix, noun.
   X1, Y1, Z1,  ORIGIN           Nouns for these points.
   XX XY XZ YX YY YZ ZX ZY ZZ    Nouns to index the transformation index


Utilities:

   mp           Matrix product
   normalize    Scale a vector to unit length
   cross        Cross product of x and y
   round        round y to the nearest x.
   debug        toggles debugging with global DEBUG variable.

Adverb and verb:

   Translate and translation

   Scale and scaling

   ShearXX and shearingxx
   ShearXY and shearingxy
   ShearXZ and shearingxz
   ShearYX and shearingyx
   ShearYY and shearingyy
   ShearYZ and shearingyz
   ShearZX and shearingzx
   ShearZY and shearingzy
   ShearZZ and shearingzz

   Reflect and reflection
   RotateX and rotationx
   RotateY and rotationy
   RotateZ and rotationz

   RotateU and rotationu

   RotateXZX and rotationxzx
   RotateXYX and rotationxyx
   RotateYXY and rotationyxy
   RotateYZY and rotationyzy
   RotateZYZ and rotationzyz
   RotateZXZ and rotationzxz

   Anagram

unimplemented: focus, perspective

)