| 1 |
% splineperspectives.mp |
| 2 |
% L. Nobre G. |
| 3 |
% Troy Henderson |
| 4 |
% Przemek Koprowski |
| 5 |
% Manjusha Joshi |
| 6 |
% 2012 |
| 7 |
|
| 8 |
prologues := 1; |
| 9 |
color f, vecU, vecV, Perp; |
| 10 |
numeric EPS, iterations; |
| 11 |
EPS:=1/25; |
| 12 |
iterations = 0; |
| 13 |
|
| 14 |
vardef initializeperspective( expr Focus ) = |
| 15 |
f := Focus; |
| 16 |
vecV := N( (-Y(f), X(f), 0) ); |
| 17 |
vecU := ncrossprod( f, vecV ); |
| 18 |
Perp := N( f ) |
| 19 |
enddef; |
| 20 |
|
| 21 |
def X(expr A) = redpart A enddef; |
| 22 |
|
| 23 |
def Y(expr A) = greenpart A enddef; |
| 24 |
|
| 25 |
def Z(expr A) = bluepart A enddef; |
| 26 |
|
| 27 |
def conorm(expr A) = ( X(A) ++ Y(A) ++ Z(A) ) enddef; |
| 28 |
|
| 29 |
def N(expr A) = |
| 30 |
begingroup |
| 31 |
save M, exitcolor; |
| 32 |
numeric M; |
| 33 |
color exitcolor; |
| 34 |
M = conorm( A ); |
| 35 |
if M > 0: |
| 36 |
exitcolor = ( X(A)/M, Y(A)/M, Z(A)/M ); |
| 37 |
else: |
| 38 |
exitcolor := black; |
| 39 |
fi; |
| 40 |
( exitcolor ) |
| 41 |
endgroup |
| 42 |
enddef; |
| 43 |
|
| 44 |
def cdotprod(expr A, B) = |
| 45 |
( X(A)*X(B) + Y(A)*Y(B) + Z(A)*Z(B) ) |
| 46 |
enddef; |
| 47 |
|
| 48 |
def ccrossprod(expr A, B) = |
| 49 |
( Y(A)*Z(B) - Z(A)*Y(B), |
| 50 |
Z(A)*X(B) - X(A)*Z(B), |
| 51 |
X(A)*Y(B) - Y(A)*X(B) ) |
| 52 |
enddef; |
| 53 |
|
| 54 |
% The dotproduct of two normalized vectors is the cosine of the angle |
| 55 |
% they form. |
| 56 |
|
| 57 |
def ndotprod(expr A, B) = |
| 58 |
begingroup |
| 59 |
save a, b; |
| 60 |
color a, b; |
| 61 |
a = N(A); |
| 62 |
b = N(B); |
| 63 |
( ( X(a)*X(b) + Y(a)*Y(b) + Z(a)*Z(b) ) ) |
| 64 |
endgroup |
| 65 |
enddef; |
| 66 |
|
| 67 |
% The normalized crossproduct of two vectors. |
| 68 |
% Also check getangle below. |
| 69 |
|
| 70 |
def ncrossprod(expr A, B) = |
| 71 |
N( ccrossprod( A, B ) ) |
| 72 |
enddef; |
| 73 |
|
| 74 |
def rp(expr R) = |
| 75 |
begingroup |
| 76 |
numeric verticalcoordinate, horizontalcoordina; |
| 77 |
f-vecV*horizontalcoordina-vecU*verticalcoordinate=whatever*(f-R); |
| 78 |
( 150*(horizontalcoordina,verticalcoordinate) ) |
| 79 |
endgroup |
| 80 |
enddef; |
| 81 |
|
| 82 |
def line( expr Ang ) = |
| 83 |
begingroup |
| 84 |
save a, b, c; |
| 85 |
numeric a, b, c; |
| 86 |
a = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*cosd(Ang); |
| 87 |
b = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*sind(Ang); |
| 88 |
c =1.5+(1 ++ cosd(Ang))*sind(3*Ang); |
| 89 |
( (a,b,c) ) |
| 90 |
endgroup |
| 91 |
enddef; |
| 92 |
|
| 93 |
vardef rationalnobreg(expr A,B,C,D) = |
| 94 |
save P,Q,E,wmin,wMax,we; |
| 95 |
color P[],Q[],E; |
| 96 |
numeric we[]; |
| 97 |
we[0]:=cdotprod( Perp, f-A ); |
| 98 |
we[1]:=cdotprod( Perp, f-B ); |
| 99 |
we[2]:=cdotprod( Perp, f-C ); |
| 100 |
we[3]:=cdotprod( Perp, f-D ); |
| 101 |
wmin:=min(we[0],we[1],we[2],we[3]); |
| 102 |
wMax:=max(we[0],we[1],we[2],we[3]); |
| 103 |
if ((1-wmin/wMax) > EPS): |
| 104 |
P[0]:=A; |
| 105 |
P[1]:=1/2[A,B]; |
| 106 |
E:=1/2[B,C]; |
| 107 |
Q[2]:=1/2[C,D]; |
| 108 |
Q[3]:=D; |
| 109 |
P[2]:=1/2[P[1],E]; |
| 110 |
Q[1]:=1/2[E,Q[2]]; |
| 111 |
P[3]:=1/2[P[2],Q[1]]; |
| 112 |
Q[0]:=P[3]; |
| 113 |
iterations := incr( iterations ); |
| 114 |
rationalnobreg(P[0],P[1],P[2],P[3]) & rationalnobreg(Q[0],Q[1],Q[2],Q[3]) |
| 115 |
else: |
| 116 |
rp(A) .. controls rp(B) and rp(C) .. rp(D) |
| 117 |
fi |
| 118 |
enddef; |
| 119 |
|
| 120 |
def casteljau( expr Za, Zb, Zc, Zd, Pt ) = %%%%%%%%%%%%%%%%%%% 2D or 3D |
| 121 |
begingroup |
| 122 |
save A, B, C, D; |
| 123 |
numeric A, B, C, D; |
| 124 |
A = (1-Pt)**3; |
| 125 |
B = 3*((1-Pt)**2)*Pt; |
| 126 |
C = 3*(1-Pt)*(Pt**2); |
| 127 |
D = Pt**3; |
| 128 |
( (A*Za+B*Zb+C*Zc+D*Zd) ) |
| 129 |
endgroup |
| 130 |
enddef; |
| 131 |
|
| 132 |
def twothr( expr Z ) = ( xpart Z, ypart Z, 0 ) enddef; |
| 133 |
|
| 134 |
def twotwo( expr Z ) = rp( twothr( Z ) ) enddef; |
| 135 |
|
| 136 |
def xoy( expr Z ) = rp( ( X(Z), Y(Z), 0 ) ) enddef; |
| 137 |
|
| 138 |
def yoz( expr W ) = rp( ( 0, Y(W), Z(W) ) ) enddef; |
| 139 |
|
| 140 |
def xoz( expr W ) = rp( ( X(W), 0, Z(W) ) ) enddef; |
| 141 |
|
| 142 |
def nextthirty( expr Za, Zb, Zc, Zd, Pt ) = %%% input 3D and return 2D |
| 143 |
begingroup |
| 144 |
save A, B, C, D, Tot, P; |
| 145 |
numeric A, B, C, D, Tot; |
| 146 |
A = ((1-Pt)**3)*cdotprod( Perp, f-Za ); |
| 147 |
B = 3*((1-Pt)**2)*Pt*cdotprod( Perp, f-Zb ); |
| 148 |
C = 3*(1-Pt)*(Pt**2)*cdotprod( Perp, f-Zc ); |
| 149 |
D = (Pt**3)*cdotprod( Perp, f-Zd ); |
| 150 |
Tot = A+B+C+D; |
| 151 |
( (A*rp(Za)+B*rp(Zb)+C*rp(Zc)+D*rp(Zd))/Tot ) |
| 152 |
endgroup |
| 153 |
enddef; |
| 154 |
|
| 155 |
beginfig(1); |
| 156 |
% initializeperspective(0.35*(3,5,2)); |
| 157 |
initializeperspective((1.3,1.31,1.32)); |
| 158 |
color w[]; |
| 159 |
numeric num, i; |
| 160 |
pen pencontrol, penalytic; |
| 161 |
color colcontrol, colorytic; |
| 162 |
pencontrol = pencircle scaled 4pt; |
| 163 |
penalytic = pencircle scaled 2pt; |
| 164 |
colcontrol = black; |
| 165 |
colorytic = blue+green; |
| 166 |
num = 50; |
| 167 |
w1 = (1,0,0); |
| 168 |
w2 = (0,0,1); |
| 169 |
w3 = (0,1,0); |
| 170 |
w4 = (1,1,1); |
| 171 |
w5 = (1,1,0); |
| 172 |
w6 = (1,0,1); |
| 173 |
w7 = (0,1,1); |
| 174 |
for i=1 upto 3: draw rp(black)--rp(w[i]); endfor; |
| 175 |
draw rp(w1)--rp(w2)--rp(w3)--rp(w4)--rp(w5)--rp(w1)--rp(w6)-- |
| 176 |
rp(w2)--rp(w7)--rp(w3)--rp(w5) dashed withdots; |
| 177 |
draw rp(w6)--rp(w4)--rp(w7) dashed withdots; |
| 178 |
draw xoy(w1) for i=1 upto num: ..xoy(casteljau(w1,w2,w3,w4,i/num)) endfor; |
| 179 |
draw xoz(w1) for i=1 upto num: ..xoz(casteljau(w1,w2,w3,w4,i/num)) endfor; |
| 180 |
draw rp(w1) for i=1 upto num: ..nextthirty(w1,w2,w3,w4,i/num) endfor |
| 181 |
withpen pencontrol withcolor colcontrol; |
| 182 |
draw rp(w1) for i=1 upto num: ..rp(casteljau(w1,w2,w3,w4,i/num)) endfor |
| 183 |
withpen penalytic withcolor colorytic; |
| 184 |
draw rationalnobreg(w1,w2,w3,w4); |
| 185 |
show iterations; |
| 186 |
draw nextthirty(w1,w2,w3,w4,0.5) withpen pencontrol withcolor red; |
| 187 |
endfig; |
| 188 |
|
| 189 |
beginfig(2); |
| 190 |
initializeperspective((3,5,4)); |
| 191 |
color node[], pre[], pos[], a, b, c; |
| 192 |
numeric i, j, k, l; |
| 193 |
path td[]; |
| 194 |
pen penmark; |
| 195 |
pair aux[]; |
| 196 |
penmark = pencircle scaled 2mm; |
| 197 |
j = 0; |
| 198 |
draw for i=1 step 3 until 360: xoy(line(i)).. endfor cycle; |
| 199 |
draw for i=1 step 3 until 360: rp(line(i)).. endfor cycle; |
| 200 |
for i=10 step 23 until 360: |
| 201 |
j := incr( j ); |
| 202 |
node[j] = line( i ); |
| 203 |
draw rp(node[j]) withpen penmark; |
| 204 |
endfor; |
| 205 |
td1 = for i=1 upto j: (X(node[i]),Y(node[i])).. endfor cycle; |
| 206 |
td2 = for i=1 upto j: (X(node[i]),Z(node[i])).. endfor cycle; |
| 207 |
td3 = for i=1 upto j: (Y(node[i]),Z(node[i])).. endfor cycle; |
| 208 |
for i=1 upto j: |
| 209 |
l := i-1; |
| 210 |
for k=1 upto 3: |
| 211 |
aux[k] := precontrol l of td[k]; |
| 212 |
endfor; |
| 213 |
a := (xpart (aux1 - point l of td1),ypart (aux1 - point l of td1),0); |
| 214 |
b := (xpart (aux2 - point l of td2),0,ypart (aux2 - point l of td2)); |
| 215 |
c := (0,xpart (aux3 - point l of td3),ypart (aux3 - point l of td3)); |
| 216 |
pre[i] = node[i]+0.5*(a+b+c); |
| 217 |
draw rp(pre[i]) withpen penmark withcolor blue; |
| 218 |
for k=1 upto 3: |
| 219 |
aux[k] := postcontrol l of td[k]; |
| 220 |
endfor; |
| 221 |
a := (xpart (aux1 - point l of td1),ypart (aux1 - point l of td1),0); |
| 222 |
b := (xpart (aux2 - point l of td2),0,ypart (aux2 - point l of td2)); |
| 223 |
c := (0,xpart (aux3 - point l of td3),ypart (aux3 - point l of td3)); |
| 224 |
pos[i] = node[i]+0.5*(a+b+c); |
| 225 |
draw rp(pos[i]) withpen penmark withcolor red; |
| 226 |
draw rp(pre[i])--rp(pos[i]) withcolor green; |
| 227 |
endfor; |
| 228 |
endfig; |
| 229 |
|
| 230 |
beginfig(3); |
| 231 |
initializeperspective((7,11,9)); |
| 232 |
color node[], pre[], pos[]; |
| 233 |
numeric i, j, param, k, num; |
| 234 |
pen penmark; |
| 235 |
param = 0.333; |
| 236 |
num = 12; |
| 237 |
penmark = pencircle scaled 2mm; |
| 238 |
j = 1; |
| 239 |
draw for i=1 step 3 until 360: xoy(line(i)).. endfor cycle; |
| 240 |
draw for i=1 step 3 until 360: rp(line(i)).. endfor cycle; |
| 241 |
for i=10 step 23 until 360: |
| 242 |
j := incr( j ); |
| 243 |
node[j] = line( i ); |
| 244 |
draw rp(node[j]) withpen penmark; |
| 245 |
endfor; |
| 246 |
node[1] = node[j]; |
| 247 |
node[0] = node[j-1]; |
| 248 |
node[j+1] = node[2]; |
| 249 |
for i=2 upto j: |
| 250 |
% |
| 251 |
pre[i] = node[i]-param*conorm(node[i]-node[i-1])*N(node[i+1]-node[i-1]); |
| 252 |
% |
| 253 |
endfor; |
| 254 |
pre[1] = pre[j]; |
| 255 |
for i=1 upto j-1: |
| 256 |
% |
| 257 |
pos[i] = node[i]+param*conorm(node[i+1]-node[i])*N(node[i+1]-node[i-1]); |
| 258 |
% |
| 259 |
endfor; |
| 260 |
pos[j] = pos[1]; |
| 261 |
for i=1 upto j-1: |
| 262 |
draw rp(pre[i]) withpen penmark withcolor blue; |
| 263 |
draw rp(pos[i]) withpen penmark withcolor red; |
| 264 |
draw rp(pre[i])--rp(pos[i]) withcolor green; |
| 265 |
draw |
| 266 |
for k=0 upto num-1: |
| 267 |
rp(casteljau(node[i],pos[i],pre[i+1],node[i+1],k/num)).. |
| 268 |
endfor rp(node[i+1]) withcolor (red+blue); |
| 269 |
endfor; |
| 270 |
endfig; |
| 271 |
|
| 272 |
end. |