sourcelib/Geometry3D.xtl

1⍝│ Geometry3D: points in space as arrays (a standard library, built into 2⍝│ xetal). Import it with an alias of your choice: "g:" u_se< "Geometry3D". 3⍝│ Points are a 3-row matrix, x over y over z, one column per point, as 4⍝│ []P_ATH takes 2 rows; a rotation is a 3 x 3 matrix, and turning the 5⍝│ points is one inner product. A face is a 3 x 4 matrix of corners, 6⍝│ and a solid is faces stacked along a new first axis (F x 3 x 4), so 7⍝│ one expression orders them far to near for painting. 8⍝│ Names with l: are exported; names with h: are private to this file. 9 10⍝│ Degrees as radians, a Float. 11ʰr̲adians ← { (f̲loat ⍵) × (p̲i @) ÷ 180 } ⍝ private 12 13⍝━━ Rotations 14 15⍝│ The rotation of d degrees about the x axis (y toward z), 3 x 3. 16⍝│ >> "g:" u_se< "Geometry3D" 17⍝│ >> f_loor 0.5 + g:r_otX 90 18⍝│ 1 0 0 19⍝│ 0 0 -1 20⍝│ 0 1 0 21ˡr̲otX ← { d → 22 c ← c̲os ʰr̲adians d 23 s ← s̲in ʰr̲adians d 24 3 3 r̲eshape 1.0 0.0 0.0 0.0 c̲at c c̲at (n̲eg s) c̲at 0.0 c̲at s c̲at c 25} 26 27⍝│ The rotation of d degrees about the y axis (z toward x), 3 x 3. 28⍝│ >> "g:" u_se< "Geometry3D" 29⍝│ >> f_loor 0.5 + g:r_otY 90 30⍝│ 0 0 1 31⍝│ 0 1 0 32⍝│ -1 0 0 33ˡr̲otY ← { d → 34 c ← c̲os ʰr̲adians d 35 s ← s̲in ʰr̲adians d 36 3 3 r̲eshape c c̲at 0.0 c̲at s c̲at 0.0 1.0 0.0 c̲at (n̲eg s) c̲at 0.0 c̲at c 37} 38 39⍝│ The rotation of d degrees about the z axis (x toward y), 3 x 3. 40⍝│ >> "g:" u_se< "Geometry3D" 41⍝│ >> f_loor 0.5 + g:r_otZ 90 42⍝│ 0 -1 0 43⍝│ 1 0 0 44⍝│ 0 0 1 45ˡr̲otZ ← { d → 46 c ← c̲os ʰr̲adians d 47 s ← s̲in ʰr̲adians d 48 3 3 r̲eshape c c̲at (n̲eg s) c̲at 0.0 c̲at s c̲at c c̲at 0.0 0.0 0.0 1.0 49} 50 51⍝│ The points turned by a rotation (or moved by any 3 x 3 matrix): 52⍝│ m ᵍt̲urn points. Rotations compose by g:t_urn too, the rightmost 53⍝│ applied first: (ᵍr̲otX 30) ᵍt̲urn ᵍr̲otY 45. 54⍝│ >> "g:" u_se< "Geometry3D" 55⍝│ >> f_loor 0.5 + (g:r_otZ 90) g:t_urn 3 1 r_eshape 1 0 0 56⍝│ 0 57⍝│ 1 58⍝│ 0 59ˡt̲urn ← { m points → m '+ '× i̲nner points } 60 61⍝━━ Viewing 62 63⍝│ The points as seen by a viewer at distance d up the z axis, looking 64⍝│ at the origin, as 2 rows (x over y) for []P_ATH: a nearer point (a 65⍝│ larger z) is drawn farther from the center. d ᵍp̲roject points. 66⍝│ >> "g:" u_se< "Geometry3D" 67⍝│ >> 10 g:p_roject 3 2 r_eshape 1 1 1 1 0 5 68⍝│ 1.0 2.0 69⍝│ 1.0 2.0 70ˡp̲roject ← { d points → 71 scale ← (f̲loat d) ÷ (f̲loat d) − 3 s̲elect points 72 (2 t̲ake points) × (2 c̲at t̲ally scale) r̲eshape scale 73} 74 75⍝│ How near each face of a solid is to the viewer: the mean z of its 76⍝│ corners, one number per face (F x 3 x 4 to F); larger is nearer. 77⍝│ >> "g:" u_se< "Geometry3D" 78⍝│ >> g:f_ar 2 3 1 r_eshape 0 0 -3 0 0 2 79⍝│ -3.0 2.0 80ˡf̲ar ← { faces → (f̲loat '+ r̲/ o̲\ 3 s̲elect₂ faces) ÷ f̲loat 1 s̲elect r̲ev s̲hape faces } 81 82⍝│ The faces of a solid from the farthest to the nearest, as positions 83⍝│ (the order to draw them in, so nearer faces cover farther ones). 84⍝│ >> "g:" u_se< "Geometry3D" 85⍝│ >> g:o_rder 2 3 1 r_eshape 0 0 2 0 0 -3 86⍝│ 2 1 87ˡo̲rder ← { faces → g̲rade ˡf̲ar faces } 88 89⍝━━ Solids 90 91⍝│ The 8 corners of a cube of side s centered on the origin, 3 x 8, in 92⍝│ the order near face (z = s/2, toward the viewer) then far face, each 93⍝│ anticlockwise from the top left. 94⍝│ >> "g:" u_se< "Geometry3D" 95⍝│ >> s_hape g:c_ube 2 96⍝│ 3 8 97ˡc̲ube ← { s → 98 h ← (f̲loat s) ÷ 2 99 h × 3 8 r̲eshape -1 1 1 -1 -1 1 1 -1 1 1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 -1 -1 100} 101 102⍝│ The six faces of the cube corners g:c_ube gives, as corner positions, 103⍝│ 6 x 4: front, back, top, bottom, left, right. 104⍝│ >> "g:" u_se< "Geometry3D" 105⍝│ >> 1 s_elect g:c_ubeFaces @ 106⍝│ 1 2 3 4 107ˡc̲ubeFaces ← { @ → 6 4 r̲eshape 1 2 3 4 5 6 7 8 1 2 6 5 4 3 7 8 1 4 8 5 2 3 7 6 } 108 109⍝│ One face of a solid's points: the corners at the given positions, 110⍝│ as a 3 x n matrix. positions ᵍf̲ace points. 111⍝│ >> "g:" u_se< "Geometry3D" 112⍝│ >> 1 2 g:f_ace g:c_ube 2 113⍝│ -1.0 1.0 114⍝│ 1.0 1.0 115⍝│ 1.0 1.0 116ˡf̲ace ← { positions points → positions s̲elect₂ points } 117 118⍝│ Every face of a solid as corners: F x 3 x n from the points and the 119⍝│ F x n positions of g:c_ubeFaces. faces ᵍs̲olid points. 120⍝│ >> "g:" u_se< "Geometry3D" 121⍝│ >> s_hape (g:c_ubeFaces @) g:s_olid g:c_ube 2 122⍝│ 6 3 4 123ˡs̲olid ← { faces points → 2 1 3 t̲ranspose (3 c̲at s̲hape faces) r̲eshape (r̲avel faces) s̲elect₂ points }