summaryrefslogtreecommitdiff
path: root/graphics/asymptote/examples/splitpatch.asy
blob: 1fa9325f4ce0bb16646c83d3bd048f180d26520c (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
import three;

size(300);

// A structure to subdivide two intersecting patches about their intersection.
struct split
{
  surface[] S={new surface};
  surface[] T={new surface};

  struct tree {
    tree[] tree=new tree[2];
  }
  // Default subdivision depth.
  int n=20;

  // Subdivide p and q to depth n if they overlap.
  void write(tree pt, tree qt, triple[][] p, triple[][] q, int depth=n) {
    --depth;
    triple[][][] Split(triple[][] P, real u=0)=depth % 2 == 0 ? hsplit : vsplit;
    triple[][][] P=Split(p);
    triple[][][] Q=Split(q);

    for(int i=0; i < 2; ++i) {
      triple[][] Pi=P[i];
      for(int j=0; j < 2; ++j) {
        triple[][] Qj=Q[j];
        if(overlap(Pi,Qj)) {
          if(!pt.tree.initialized(i))
            pt.tree[i]=new tree;
          if(!qt.tree.initialized(j))
            qt.tree[j]=new tree;
          if(depth > 0)
            write(pt.tree[i],qt.tree[j],Pi,Qj,depth);
        }
      }
    }
  }

  // Output the subpatches of p from subdivision.
  void read(surface[] S, tree t, triple[][] p, int depth=n) {
    --depth;
    triple[][][] Split(triple[][] P, real u=0)=depth % 2 == 0 ? hsplit : vsplit;
    triple[][][] P=Split(p);

    for(int i=0; i < 2; ++i) {
      if(t.tree.initialized(i))
        read(S,t.tree[i],P[i],depth);
      else {
        S[0].push(patch(P[i]));
      }
    }
  }

  void operator init(triple[][] p, triple[][] q, int depth=n) {
    tree ptrunk,qtrunk;
    write(ptrunk,qtrunk,p,q,depth);
    read(T,ptrunk,p,depth);
    read(S,qtrunk,q,depth);
  }
}

currentprojection=orthographic(0,0,1);

triple[][] A=
  {
   {(0,0,0),(1,0,0),(1,0,0),(2,0,0)},
   {(0,4/3,0),(2/3,4/3,2),(4/3,4/3,2),(2,4/3,0)},
   {(0,2/3,0),(2/3,2/3,0),(4/3,2/3,0),(2,2/3,0)},
   {(0,2,0),(2/3,2,0),(4/3,2,0),(2,2,0)}
  };

triple[][] B=
  {
   {(0.5,0,-1),(0.5,1,-1),(0.5,2,-1),(0.5,3,-1)},
   {(0.5,0,0),(0.5,1,0),(0.5,2,0),(0.5,3,0)},
   {(0.5,0,1),(0.5,1,1),(0.5,2,1),(0.5,3,1)},
   {(0.5,0,2),(0.5,1,2),(0.5,2,2),(0.5,3,2)}
  };

split S=split(B,A);

defaultrender.merge=true;

for(int i=0; i < S.S[0].s.length; ++i)
  draw(surface(S.S[0].s[i]),Pen(i));

for(int i=0; i < S.T[0].s.length; ++i)
  draw(surface(S.T[0].s[i]),Pen(i));