summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/featpost/example/splineperspectives.mp
blob: 7637106f611ff1b0fd21e7c74aa8c8f51278acad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
% splineperspectives.mp
% L. Nobre G.
% Troy Henderson
% Przemek Koprowski
% Manjusha Joshi
% 2012

prologues := 1;
color f, vecU, vecV, Perp;
numeric EPS, iterations;
EPS:=1/25;
iterations = 0;

    vardef initializeperspective( expr Focus ) =
      f := Focus;
      vecV := N( (-Y(f), X(f), 0) );
      vecU := ncrossprod( f, vecV );
      Perp := N( f )
    enddef;

    def X(expr A) = redpart A enddef;
  
    def Y(expr A) = greenpart A enddef;
  
    def Z(expr A) = bluepart A enddef;

    def conorm(expr A) = ( X(A) ++ Y(A) ++ Z(A) ) enddef;

    def N(expr A) =
        begingroup
            save M, exitcolor;
            numeric M;
            color exitcolor;
            M = conorm( A );
            if M > 0:
                exitcolor = ( X(A)/M, Y(A)/M, Z(A)/M );
            else:
                exitcolor := black;
            fi;
            ( exitcolor )
        endgroup
    enddef;

    def cdotprod(expr A, B) = 
        ( X(A)*X(B) + Y(A)*Y(B) + Z(A)*Z(B) )
    enddef;

    def ccrossprod(expr A, B) = 
        ( Y(A)*Z(B) - Z(A)*Y(B), 
          Z(A)*X(B) - X(A)*Z(B), 
          X(A)*Y(B) - Y(A)*X(B) )
    enddef;

% The dotproduct of two normalized vectors is the cosine of the angle 
% they form.

    def ndotprod(expr A, B) = 
        begingroup
            save a, b;
            color a, b;
            a = N(A);
            b = N(B);
            ( ( X(a)*X(b) + Y(a)*Y(b) + Z(a)*Z(b) ) )
        endgroup
    enddef;

% The normalized crossproduct of two vectors. 
% Also check getangle below.

    def ncrossprod(expr A, B) = 
        N( ccrossprod( A, B ) )
    enddef;

    def rp(expr R) =
        begingroup
	  numeric verticalcoordinate, horizontalcoordina;
	  f-vecV*horizontalcoordina-vecU*verticalcoordinate=whatever*(f-R);
	  ( 150*(horizontalcoordina,verticalcoordinate) )
        endgroup
    enddef;

    def line( expr Ang ) =
      begingroup
	save a, b, c;
	numeric a, b, c;
	a = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*cosd(Ang);
	b = (2-(1 ++ cosd(Ang))*cosd(3*Ang))*sind(Ang);
	c =1.5+(1 ++ cosd(Ang))*sind(3*Ang);
	( (a,b,c) )
      endgroup
    enddef;
    
    vardef rationalnobreg(expr A,B,C,D) =
      save P,Q,E,wmin,wMax,we;
      color P[],Q[],E;
      numeric we[];
      we[0]:=cdotprod( Perp, f-A );
      we[1]:=cdotprod( Perp, f-B );
      we[2]:=cdotprod( Perp, f-C );
      we[3]:=cdotprod( Perp, f-D );
      wmin:=min(we[0],we[1],we[2],we[3]);
      wMax:=max(we[0],we[1],we[2],we[3]);
      if ((1-wmin/wMax) > EPS):
	P[0]:=A;
	P[1]:=1/2[A,B];
	E:=1/2[B,C];
	Q[2]:=1/2[C,D];
	Q[3]:=D;
	P[2]:=1/2[P[1],E];
	Q[1]:=1/2[E,Q[2]];
	P[3]:=1/2[P[2],Q[1]];
	Q[0]:=P[3];
	iterations := incr( iterations );
	rationalnobreg(P[0],P[1],P[2],P[3]) & rationalnobreg(Q[0],Q[1],Q[2],Q[3])
      else:
	rp(A) .. controls rp(B) and rp(C) .. rp(D)
      fi
    enddef;
  
    def casteljau( expr Za, Zb, Zc, Zd, Pt ) = %%%%%%%%%%%%%%%%%%% 2D or 3D
      begingroup
	save A, B, C, D;
	numeric A, B, C, D;
	A = (1-Pt)**3;
	B = 3*((1-Pt)**2)*Pt;
	C = 3*(1-Pt)*(Pt**2);
	D = Pt**3;
	( (A*Za+B*Zb+C*Zc+D*Zd) )
      endgroup
    enddef;
    
    def twothr( expr Z ) = ( xpart Z, ypart Z, 0 ) enddef;
    
    def twotwo( expr Z ) = rp( twothr( Z ) ) enddef;

    def xoy( expr Z ) = rp( ( X(Z), Y(Z), 0 ) ) enddef;

    def yoz( expr W ) = rp( ( 0, Y(W), Z(W) ) ) enddef;

    def xoz( expr W ) = rp( ( X(W), 0, Z(W) ) ) enddef;

    def nextthirty( expr Za, Zb, Zc, Zd, Pt ) = %%% input 3D and return 2D
      begingroup
	save A, B, C, D, Tot, P;
	numeric A, B, C, D, Tot;
	A = ((1-Pt)**3)*cdotprod( Perp, f-Za );
	B = 3*((1-Pt)**2)*Pt*cdotprod( Perp, f-Zb );
	C = 3*(1-Pt)*(Pt**2)*cdotprod( Perp, f-Zc );
	D = (Pt**3)*cdotprod( Perp, f-Zd );
	Tot = A+B+C+D;
	( (A*rp(Za)+B*rp(Zb)+C*rp(Zc)+D*rp(Zd))/Tot )
      endgroup
    enddef;

beginfig(1);
%  initializeperspective(0.35*(3,5,2));
  initializeperspective((1.3,1.31,1.32));
  color w[];
  numeric num, i;
  pen pencontrol, penalytic;
  color colcontrol, colorytic;
  pencontrol = pencircle scaled 4pt;
  penalytic = pencircle scaled 2pt;
  colcontrol = black;
  colorytic = blue+green;
  num = 50;
  w1 = (1,0,0);
  w2 = (0,0,1);
  w3 = (0,1,0);
  w4 = (1,1,1);
  w5 = (1,1,0);
  w6 = (1,0,1);
  w7 = (0,1,1);
  for i=1 upto 3: draw rp(black)--rp(w[i]); endfor;
  draw rp(w1)--rp(w2)--rp(w3)--rp(w4)--rp(w5)--rp(w1)--rp(w6)--
       rp(w2)--rp(w7)--rp(w3)--rp(w5) dashed withdots;
  draw rp(w6)--rp(w4)--rp(w7) dashed withdots;
  draw xoy(w1) for i=1 upto num: ..xoy(casteljau(w1,w2,w3,w4,i/num)) endfor;
  draw xoz(w1) for i=1 upto num: ..xoz(casteljau(w1,w2,w3,w4,i/num)) endfor;
  draw rp(w1) for i=1 upto num: ..nextthirty(w1,w2,w3,w4,i/num) endfor
                                  withpen pencontrol withcolor colcontrol;
  draw rp(w1) for i=1 upto num: ..rp(casteljau(w1,w2,w3,w4,i/num)) endfor
                                  withpen penalytic withcolor colorytic;
  draw rationalnobreg(w1,w2,w3,w4);
  show iterations;
  draw nextthirty(w1,w2,w3,w4,0.5) withpen pencontrol withcolor red;	      
endfig;

beginfig(2);
  initializeperspective((3,5,4));
  color node[], pre[], pos[], a, b, c;
  numeric i, j, k, l;
  path td[];
  pen penmark;
  pair aux[];
  penmark = pencircle scaled 2mm;
  j = 0;
  draw for i=1 step 3 until 360: xoy(line(i)).. endfor cycle;
  draw for i=1 step 3 until 360: rp(line(i)).. endfor cycle;
  for i=10 step 23 until 360:
    j := incr( j );
    node[j] = line( i );
    draw rp(node[j]) withpen penmark;
  endfor;
  td1 = for i=1 upto j: (X(node[i]),Y(node[i])).. endfor cycle;
  td2 = for i=1 upto j: (X(node[i]),Z(node[i])).. endfor cycle;
  td3 = for i=1 upto j: (Y(node[i]),Z(node[i])).. endfor cycle;
  for i=1 upto j:
    l := i-1;
    for k=1 upto 3:
      aux[k] := precontrol l of td[k];
    endfor;
    a := (xpart (aux1 - point l of td1),ypart (aux1 - point l of td1),0);
    b := (xpart (aux2 - point l of td2),0,ypart (aux2 - point l of td2));
    c := (0,xpart (aux3 - point l of td3),ypart (aux3 - point l of td3));
    pre[i] = node[i]+0.5*(a+b+c);
    draw rp(pre[i]) withpen penmark withcolor blue;
    for k=1 upto 3:
      aux[k] := postcontrol l of td[k];
    endfor;
    a := (xpart (aux1 - point l of td1),ypart (aux1 - point l of td1),0);
    b := (xpart (aux2 - point l of td2),0,ypart (aux2 - point l of td2));
    c := (0,xpart (aux3 - point l of td3),ypart (aux3 - point l of td3));
    pos[i] = node[i]+0.5*(a+b+c);    
    draw rp(pos[i]) withpen penmark withcolor red;
    draw rp(pre[i])--rp(pos[i]) withcolor green;
  endfor;
endfig;

beginfig(3);
  initializeperspective((7,11,9));
  color node[], pre[], pos[];
  numeric i, j, param, k, num;
  pen penmark;
  param = 0.333;
  num = 12;
  penmark = pencircle scaled 2mm;
  j = 1;
  draw for i=1 step 3 until 360: xoy(line(i)).. endfor cycle;
  draw for i=1 step 3 until 360: rp(line(i)).. endfor cycle;
  for i=10 step 23 until 360:
    j := incr( j );
    node[j] = line( i );
    draw rp(node[j]) withpen penmark;
  endfor;
  node[1] = node[j];
  node[0] = node[j-1];
  node[j+1] = node[2];
  for i=2 upto j:
    %
    pre[i] = node[i]-param*conorm(node[i]-node[i-1])*N(node[i+1]-node[i-1]);
    %
  endfor;
  pre[1] = pre[j];
  for i=1 upto j-1:
    %
    pos[i] = node[i]+param*conorm(node[i+1]-node[i])*N(node[i+1]-node[i-1]);
    %
  endfor;
  pos[j] = pos[1];
  for i=1 upto j-1: 
    draw rp(pre[i]) withpen penmark withcolor blue;
    draw rp(pos[i]) withpen penmark withcolor red;
    draw rp(pre[i])--rp(pos[i]) withcolor green;
    draw
      for k=0 upto num-1:
        rp(casteljau(node[i],pos[i],pre[i+1],node[i+1],k/num))..
      endfor rp(node[i+1]) withcolor (red+blue);
  endfor;
endfig;

end.