| 1 |
divert(-1) |
| 2 |
lib3D.m4 Macros for rotation, projection, and other |
| 3 |
operations on argument triples representing |
| 4 |
3D vectors or colors. |
| 5 |
|
| 6 |
* Circuit_macros Version 11.0, copyright (c) 2026 J. D. Aplevich under * |
| 7 |
* the LaTeX Project Public Licence in file Licence.txt. The files of * |
| 8 |
* this distribution may be redistributed or modified provided that this * |
| 9 |
* copyright notice is included and provided that modifications are clearly * |
| 10 |
* marked to distinguish them from this distribution. There is no warranty * |
| 11 |
* whatsoever for these files. * |
| 12 |
|
| 13 |
define(`lib3D_') |
| 14 |
ifdef(`libgen_',,`include(libgen.m4)divert(-1)') |
| 15 |
|
| 16 |
============================================================================= |
| 17 |
|
| 18 |
`setview(azimuth, elevation, rotation) |
| 19 |
Set view angles (degrees) for projection onto |
| 20 |
a plane. The view vector is obtained by looking |
| 21 |
in along the x axis, then rotating about -x, |
| 22 |
-y, and z in that order. 3D vectors are |
| 23 |
projected onto the resulting yz plane |
| 24 |
using the project() macro. If rotation = 0, |
| 25 |
the projection matrix P is |
| 26 |
P =( -sin(az), cos(az), 0 ) |
| 27 |
(-sin(el)cos(az),-sin(az)sin(el),cos(el))' |
| 28 |
define(`setview',` |
| 29 |
`# setview($@)' |
| 30 |
define(`m4azim',ifelse(`$1',,0,prod_($1,dtor_)))dnl |
| 31 |
define(`m4elev',ifelse(`$2',,0,prod_($2,dtor_)))dnl |
| 32 |
define(`m4rot', ifelse(`$3',,0,prod_($3,dtor_)))dnl |
| 33 |
define(`m4azimr',`ifelse(`$1',,0,`prod_(`$1',dtor_)')')dnl |
| 34 |
define(`m4elevr',`ifelse(`$2',,0,`prod_(`$2',dtor_)')')dnl |
| 35 |
define(`m4rotx',`ifelse(`$3',,0,`prod_(`$3',dtor_)')')dnl |
| 36 |
define(`m4caz', |
| 37 |
`ifelse(`$1',,1,`$1',0,1,`$1',90,0,`$1',-90, 0,m4cos(m4azim))')dnl |
| 38 |
define(`m4saz', |
| 39 |
`ifelse(`$1',,0,`$1',0,0,`$1',90,1,`$1',-90,-1,m4sin(m4azim))')dnl |
| 40 |
define(`m4cel', |
| 41 |
`ifelse(`$2',,1,`$2',0,1,`$2',90,0,`$2',-90, 0,m4cos(m4elev))')dnl |
| 42 |
define(`m4sel', |
| 43 |
`ifelse(`$2',,0,`$2',0,0,`$2',90,1,`$2',-90,-1,m4sin(m4elev))')dnl |
| 44 |
define(`m4cro',`ifelse(`$3',,1,`$3',0,1,`$3',90,0,`$3',-90, 0,m4cos(m4rot))')dnl |
| 45 |
define(`m4sro',`ifelse(`$3',,0,`$3',0,0,`$3',90,1,`$3',-90,-1,m4sin(m4rot))')dnl |
| 46 |
view3D1=m4caz*m4cel;dnl |
| 47 |
view3D2=m4saz*m4cel;dnl |
| 48 |
view3D3=m4sel |
| 49 |
ifdef(`setlight_',,`light3D1=view3D1; light3D2=view3D2; light3D3=view3D3') |
| 50 |
dnl Unit vectors projected on the view plane |
| 51 |
UPx_: project(1,0,0) |
| 52 |
UPy_: project(0,1,0) |
| 53 |
UPz_: project(0,0,1) |
| 54 |
`# setview end' |
| 55 |
') |
| 56 |
define(`m4cos',`ifelse(`$1',0,1,cos(`$1'))') |
| 57 |
define(`m4sin',`ifelse(`$1',0,0,sin(`$1'))') |
| 58 |
|
| 59 |
The resulting view vector |
| 60 |
#efine(`View3D',`PtoBase3D(1,0,0)') |
| 61 |
define(`View3D',`view3D1,view3D2,view3D3') |
| 62 |
|
| 63 |
`Extract the x-y, x-z, or y-z coordinate pair |
| 64 |
from a triple' |
| 65 |
define(`Pr_xy',`$1,$2') |
| 66 |
define(`Pr_xz',`$1,$3') |
| 67 |
define(`Pr_yz',`$2,$3') |
| 68 |
|
| 69 |
Projection coords back to orig 3D coords |
| 70 |
define(`PtoBase3D', |
| 71 |
`rot3Dz(m4azim,rot3Dy(-m4elev,rot3Dx(-m4rot,`$1',`$2',`$3')))') |
| 72 |
|
| 73 |
This does the 3D to 2D axonometric projection |
| 74 |
i.e. project(x,y,z) produces coordinate pair |
| 75 |
u,v on the 2D plane defined by the view angles |
| 76 |
and Project(x,y,z) produces position (u,v) |
| 77 |
ifdpic( |
| 78 |
`define(`project', |
| 79 |
`Pr_yz(rot3Dx(m4rot,rot3Dy(m4elev,rot3Dz(-m4azim,`$1',`$2',`$3'))))') |
| 80 |
define(`Project',`(ifelse(`$1',0, |
| 81 |
`ifelse(`$2',0,`ifelse(`$3',0,`(0,0)',UPz_*(`$3'))', |
| 82 |
`UPy_*(`$2')`'ifelse(`$3',0,,+UPz_*(`$3'))')', |
| 83 |
`UPx_*(`$1')`'ifelse(`$2',0,,+UPy_*(`$2'))`'ifelse(`$3',0,,+UPz_*(`$3'))'))')', |
| 84 |
`define(`project', |
| 85 |
`Pr_yz(rot3Dx(m4rot,rot3Dy(m4elev,rot3Dz(-m4azim,`$1',`$2',`$3'))))') |
| 86 |
define(`Project',`(project($@))')') |
| 87 |
|
| 88 |
`Rotation about x axis rot3Dx(angle,x1,x2,x3)' |
| 89 |
define(`rot3Dx',``$2',diff_(prod_(m4cos(`$1'),`$3'),prod_(m4sin(`$1'),`$4')),dnl |
| 90 |
sum_(prod_(m4sin(`$1'),`$3'),prod_(m4cos(`$1'),`$4'))') |
| 91 |
|
| 92 |
`Rotation about y axis rot3Dy(angle,x1,x2,x3)' |
| 93 |
define(`rot3Dy',`sum_(prod_(m4cos(`$1'),`$2'),prod_(m4sin(`$1'),`$4')),`$3',dnl |
| 94 |
diff_(prod_(m4cos(`$1'),`$4'),prod_(m4sin(`$1'),`$2'))') |
| 95 |
|
| 96 |
`Rotation about z axis rot3Dz(angle,x1,x2,x3)' |
| 97 |
define(`rot3Dz',`diff_(prod_(m4cos(`$1'),`$2'),prod_(m4sin(`$1'),`$3')),dnl |
| 98 |
sum_(prod_(m4sin(`$1'),`$2'),prod_(m4cos(`$1'),`$3')),`$4'') |
| 99 |
|
| 100 |
`Cross product cross3D(x1,y1,z1,x2,y2,z2)' |
| 101 |
define(`cross3D',`diff_(prod_(`$2',`$6'),prod_(`$3',`$5')),dnl |
| 102 |
diff_(prod_(`$3',`$4'),prod_(`$1',`$6')),dnl |
| 103 |
diff_(prod_(`$1',`$5'),prod_(`$2',`$4'))') |
| 104 |
|
| 105 |
`Dot product dot3D(x1,y1,z1,x2,y2,z2)' |
| 106 |
define(`dot3D',`(sum_( |
| 107 |
sum_(prod_(`$1',`$4'),prod_(`$2',`$5')),prod_(`$3',`$6')))') |
| 108 |
Vector addition, subtraction, scalar product |
| 109 |
define(`sum3D',`sum_(`$1',`$4'),sum_(`$2',`$5'),sum_(`$3',`$6')') |
| 110 |
define(`diff3D',`diff_(`$1',`$4'),diff_(`$2',`$5'),diff_(`$3',`$6')') |
| 111 |
define(`sprod3D',`prod_(`$1',`$2'),prod_(`$1',`$3'),prod_(`$1',`$4')') |
| 112 |
Extract direction cosine |
| 113 |
`eg v = dcosine3D(1,x,y,z) assigns x to v' |
| 114 |
define(`dcosine3D',`(ifelse(`$1',1,`$2',`$1',2,`$3',`$4'))') |
| 115 |
Euclidian length |
| 116 |
define(`length3D',`sqrt((`$1')^2+(`$2')^2+(`$3')^2)') |
| 117 |
Unit vector |
| 118 |
define(`unit3D',`sprod3D(1/length3D(`$1',`$2',`$3'),`$1',`$2',`$3')') |
| 119 |
Proportion: between3D(x,Vec1,Vec2), i.e. |
| 120 |
Vec1 * (1-x) + Vec2 * x |
| 121 |
define(`between3D',`sum3D(sprod3D((1-(`$1')),`$2',`$3',`$4'), |
| 122 |
sprod3D( `$1',`$5',`$6',`$7'))') |
| 123 |
|
| 124 |
`assign3D([u],[v],[w],x,y,z); eg |
| 125 |
assign3D(u,v,w,cross3D(x1,y1,z1,x2,y2,z2)) |
| 126 |
assigns u = 4th arg, v = 5th arg, w = 6th arg, |
| 127 |
for nonblank u, v, or w' |
| 128 |
define(`assign3D',`assign3($@)') |
| 129 |
|
| 130 |
`vassign3D(name,x,y,z); eg |
| 131 |
vassign3D(u,x,y,z) |
| 132 |
assigns u[1] = x, u[2] = y, u[3] = z' |
| 133 |
define(`vassign3D',`for i_vassign3D = 1 to 3 do { |
| 134 |
exec sprintf("`$1'[i_vassign3D] = $%g",i_vassign3D+3) }') |
| 135 |
|
| 136 |
Write out the 3 arguments for debug |
| 137 |
define(`print3D',`print sprintf("`$1'(%g,%g,%g)",`$2',`$3',`$4')') |
| 138 |
|
| 139 |
`setlight (azimuth, elevation, rotation) |
| 140 |
Set angles (degrees) for 3D highlighting. |
| 141 |
Defaults are the previous values for |
| 142 |
setview(). The Light3D vector is defined |
| 143 |
as for View3D.' |
| 144 |
define(`setlight',`define(`setlight_') |
| 145 |
define(`m4hzim',`ifelse(`$1',,m4azimr,`prod_(`$1',dtor_)')')dnl |
| 146 |
define(`m4hlev',`ifelse(`$2',,m4elevr,`prod_(`$2',dtor_)')')dnl |
| 147 |
define(`m4hot',`ifelse(`$3',,m4rotx,`prod_(`$3',dtor_)')')dnl |
| 148 |
define(`m4chz',`ifelse(`$1',0,1,`$1',90,0,`$1',-90, 0,cos(m4hzim))')dnl |
| 149 |
define(`m4shz',`ifelse(`$1',0,0,`$1',90,1,`$1',-90,-1,sin(m4hzim))')dnl |
| 150 |
define(`m4chl',`ifelse(`$2',0,1,`$2',90,0,`$2',-90, 0,cos(m4hlev))')dnl |
| 151 |
define(`m4shl',`ifelse(`$2',0,0,`$2',90,1,`$2',-90,-1,sin(m4hlev))')dnl |
| 152 |
define(`m4cho',`ifelse(`$3',0,1,`$3',90,0,`$3',-90, 0,cos(m4hot))')dnl |
| 153 |
define(`m4sho',`ifelse(`$3',0,0,`$3',90,1,`$3',-90,-1,sin(m4hot))')dnl |
| 154 |
light3D1=m4chz*m4chl |
| 155 |
light3D2=m4shz*m4chl |
| 156 |
light3D3=m4shl |
| 157 |
') |
| 158 |
The resulting vector |
| 159 |
#efine(`Light3D',`PtoBase3D(1,0,0)') |
| 160 |
define(`Light3D', |
| 161 |
`ifdef(`setlight_',`light3D1,light3D2,light3D3',`view3D1,view3D2,view3D3')') |
| 162 |
|
| 163 |
`Fector(x,y,z,nx,ny,nz) with .Origin at pos |
| 164 |
Arrow with flat 3D head. The second vector, |
| 165 |
(i.e. args nx,ny,nz) is the normal to the |
| 166 |
head flat surface' |
| 167 |
define(`Fector', |
| 168 |
`[ Origin: 0,0 |
| 169 |
define(`M4F_V',``$1',`$2',`$3'')dnl the whole vector V |
| 170 |
lV = length3D(M4F_V) |
| 171 |
define(`M4F_T',``$4',`$5',`$6'')dnl normal to the top surface |
| 172 |
lT = length3D(M4F_T) |
| 173 |
define(`M4F_Vn',`sprod3D(1/lV,M4F_V)')dnl unit vector Vn |
| 174 |
aln = 0.15*scale ;dnl arrowhead length |
| 175 |
awd = 0.09*scale ;dnl " width |
| 176 |
adp = 0.0375*scale ;dnl " depth (thickness) |
| 177 |
define(`M4F_Vt',`sprod3D((lV-aln),M4F_Vn)')dnl head base vector |
| 178 |
|
| 179 |
Start: Origin |
| 180 |
End: project(M4F_V) |
| 181 |
rpoint_(from Origin to End) |
| 182 |
lTdp = adp/2/lT |
| 183 |
vtx = dcosine3D(1,M4F_Vt); vty = dcosine3D(2,M4F_Vt) # Vt coords |
| 184 |
vtz = dcosine3D(3,M4F_Vt) |
| 185 |
dnl half-thickness vector in direction of T |
| 186 |
tx = prod_(lTdp,`$4'); ty = prod_(lTdp,`$5') |
| 187 |
tz = prod_(lTdp,`$6') |
| 188 |
dnl half-width vector right |
| 189 |
rf = awd/2/lT/lV |
| 190 |
rx = rf*dcosine3D(1,cross3D(M4F_V,M4F_T)) |
| 191 |
ry = rf*dcosine3D(2,cross3D(M4F_V,M4F_T)) |
| 192 |
rz = rf*dcosine3D(3,cross3D(M4F_V,M4F_T)) |
| 193 |
dnl top and bottom points of V |
| 194 |
TV: project(sum3D(M4F_V, tx,ty,tz)) |
| 195 |
BV: project(diff3D(M4F_V, tx,ty,tz)) |
| 196 |
dnl top, bottom right, left of base |
| 197 |
TR: project(sum3D(vtx,vty,vtz, sum3D(tx,ty,tz,rx,ry,rz))) |
| 198 |
BR: project(sum3D(vtx,vty,vtz, diff3D(rx,ry,rz,tx,ty,tz))) |
| 199 |
BL: project(diff3D(vtx,vty,vtz, sum3D(rx,ry,rz,tx,ty,tz))) |
| 200 |
TL: project(diff3D(vtx,vty,vtz, diff3D(rx,ry,rz,tx,ty,tz))) |
| 201 |
lthickness = linethick |
| 202 |
dnl base |
| 203 |
if dot3D(M4F_V,View3D) < 0 then { |
| 204 |
thinlines_ |
| 205 |
ifgpic(`gshade(0.5,BR,BL,TL,TR,BR,BL)', |
| 206 |
`line thick 0 fill_(0.5) from BR to BL then to TL then to TR then to BR') |
| 207 |
line from BR to BL ; line to TL ; line to TR ; line to BR |
| 208 |
linethick_(lthickness) |
| 209 |
} |
| 210 |
dnl shaft |
| 211 |
linethick_(1.2) |
| 212 |
psset_(arrows=c-c) |
| 213 |
line from Origin to project(vtx,vty,vtz) |
| 214 |
psset_(arrows=-) |
| 215 |
thinlines_ |
| 216 |
dnl top or bottom |
| 217 |
if dot3D(M4F_T,View3D) > 0 then { |
| 218 |
ifgpic(`gshade(1,TR,TL,TV,TR,TL)', |
| 219 |
`line thick 0 fill_(1) from TV to TR then to TL then to TV') |
| 220 |
line from TV to TR ; line to TL ; line to TV |
| 221 |
} else { |
| 222 |
ifgpic(`gshade(0,BR,BL,BV,BR,BL)', |
| 223 |
`line thick 0 fill_(0) from BV to BR then to BL then to BV') |
| 224 |
line from BV to BR ; line to BL ; line to BV |
| 225 |
} |
| 226 |
dnl starboard normal; draw right face |
| 227 |
define(`M4F_S', |
| 228 |
`cross3D(diff3D(sprod3D(aln,M4F_Vn),rx,ry,rz),M4F_T)')dnl |
| 229 |
if dot3D(M4F_S,View3D) > 0 then { |
| 230 |
ifgpic(`gshade(1,TV,BV,BR,TR,TV,BV)', |
| 231 |
`line thick 0 fill_(1) from TV to BV then to BR then to TR then to TV') |
| 232 |
line from TV to BV ; line to BR ; line to TR ; line to TV |
| 233 |
} |
| 234 |
dnl port normal; draw left face |
| 235 |
define(`M4F_P', |
| 236 |
`cross3D(M4F_T,sum3D(sprod3D(aln,M4F_Vn),rx,ry,rz))')dnl |
| 237 |
if dot3D(M4F_P,View3D) > 0 then { |
| 238 |
ifgpic(`gshade(1,TV,BV,BL,TL,TV,BV)', |
| 239 |
`line thick 0 fill_(1) from TV to BV then to BL then to TL then to TV') |
| 240 |
line from TV to BV ; line to BL ; line to TL ; line to TV |
| 241 |
} |
| 242 |
linethick_(lthickness) |
| 243 |
`$7'] ') # End Fector |
| 244 |
|
| 245 |
`shadedball( rad, |
| 246 |
highlight rad, highlight degrees, |
| 247 |
initial gray, final gray | (rf,gf,bf) ) |
| 248 |
The highlight is by default at |
| 249 |
radius rad*3/5 and angle 110 deg |
| 250 |
(or arg2 deg); if setlight has been |
| 251 |
invoked then its azimuth and elevation arguments |
| 252 |
determine highlight position. |
| 253 |
Arg5 can be a parenthesized rgb color ' |
| 254 |
define(`shadedball',`[ C: (0,0); r = ifelse(`$1',,circlerad,`$1') |
| 255 |
ifdef(`setlight_', |
| 256 |
`H: (project(sprod3D(r,light3D1,light3D2,light3D3))) |
| 257 |
hr = distance(C,H)', |
| 258 |
`hr = ifelse(`$2',,r*3/5,`$2'); ha = ifelse(`$3',,110,`$3') |
| 259 |
H: (cosd(ha)*hr,sind(ha)*hr)') |
| 260 |
rgbtohsv(ifelse(`$4',,`1,1,1',``$4',`$4',`$4''),h0,s0,v0) |
| 261 |
pushdef(`rgbf',`ifelse(`$5',,`(0.25,0.25,0.25)pushdef(`oneshade',1)', |
| 262 |
substr(`$5',0,1)substr(`$5',decr(len(`$5')),1),(),`$5'pushdef(`oneshade',0), |
| 263 |
`(`$5',`$5',`$5')pushdef(`oneshade',1)')') |
| 264 |
rgbtohsv(patsubst(rgbf,`^ *('\|`) *$'),hf,sf,vf) |
| 265 |
rm = r+hr; n = int(rm/(linethick bp__)) |
| 266 |
for i=1 to n-1 do { x = i/n*rm; ifelse(oneshade,1, |
| 267 |
`hs = h0+(i/n)^2*(hf-h0); ss = s0+(i/n)^2*(sf-s0); vs = v0+(i/n)^2*(vf-v0) |
| 268 |
hsvtorgb(hs,ss,vs,ri,gi,bi)', |
| 269 |
`hsvtorgb(hf,(i/n)^2,vf,ri,gi,bi)') |
| 270 |
if x <= (r-hr) then { |
| 271 |
circle rad x thick linethick*1.6 outlined rgbstring(ri,gi,bi) at H } \ |
| 272 |
else { arc cw thick linethick*ifpgf(2,1.6) outlined rgbstring(ri,gi,bi) \ |
| 273 |
from Cintersect(H,x,C,r) to Cintersect(H,x,C,r,R) with .c at H } } |
| 274 |
circle rad r ifpgf(+linethick bp__/2) outlined rgbstring(ri,gi,bi) at C |
| 275 |
`$6' popdef(`rgbf',`oneshade') ]') |
| 276 |
|
| 277 |
divert(0)dnl |