forked from KitWallace/openscad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_surface.scad
162 lines (135 loc) · 3.52 KB
/
poly_surface.scad
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
/*
computed surface construction by Kit Wallace
Code licensed under the Creative Commons - Attribution - Share Alike license.
The project is being documented in my blog
http://kitwallace.tumblr.com/tagged/openscad
*/
function flatten(l) =
// remove one level of brackets
[ for (a = l) for (b = a) b ] ;
function reverse(l) =
// reverse the elements of an array
[ for (i=[1:len(l)]) l[len(l)-i]];
// vertex indexes
function vt(i,j,nx,ny) = i*nx + j;
function vb(i,j,nx,ny) = nx*ny +i * nx + j;
function surface_vertices (minx,maxx,miny,maxy,nx,ny) =
// vertices
concat(
flatten([for (i=[0:nx-1])
let (x= minx + (maxx-minx)*i/nx)
[for (j=[0:ny-1])
let (y= miny + (maxy-miny)*j/ny)
[x,y,ftop(x,y)]
]
]),
flatten([for (i=[0:nx-1])
let (x= minx + (maxx-minx)*i/nx)
[for (j=[0:ny-1])
let (y= miny + (maxy-miny)*j/ny)
[x,y,fbottom(x,y)]
]
])
);
function surface_faces (nx,ny) =
concat(
flatten(
[for (i=[0:nx-2])
[for (j=[0:ny-2])
reverse(
[vt(i,j,nx,nx),
vt(i+1,j,nx,nx),
vt(i+1,j+1,nx,nx),
vt(i,j+1,nx,nx)]
)
]
]),
flatten(
[for (i=[0:nx-2])
[for (j=[0:ny-2])
[vb(i,j,nx,nx),
vb(i+1,j,nx,nx),
vb(i+1,j+1,nx,nx),
vb(i,j+1,nx,nx)
]
]
]),
[for (i=[0:nx-2])
[vt(i,0,nx,nx),
vt(i+1,0,nx,nx),
vb(i+1,0,,nx,nx),
vb(i,0,,nx,nx)
]
],
[for (i=[0:nx-2])
reverse(
[vt(i,ny-1,nx,nx),
vt(i+1,ny-1,nx,nx),
vb(i+1,ny-1,nx,nx),
vb(i,ny-1,nx,nx)
])
],
[for (j=[0:ny-2])
reverse(
[vt(0,j,nx,nx),
vt(0,j+1,nx,nx),
vb(0,j+1,nx,nx),
vb(0,j,nx,nx)])
],
[for (j=[0:ny-2])
[vt(nx-1,j,nx,nx),
vt(nx-1,j+1,nx,nx),
vb(nx-1,j+1,nx,nx),
vb(nx-1,j,nx,nx)]
]
);
module poly_surface (minx,maxx,miny,maxy,nx,ny) {
// uses ftop() and fbottom() to computer surface heights
sv=surface_vertices(minx,maxx,miny,maxy,nx,ny);
// echo("vertices",sv);
sf=surface_faces(nx,ny);
// echo("faces",sf);
polyhedron(sv,sf);
};
module ruler(n) {
color([0,0,1,0.5])
for (i=[0:n-1])
translate([(i-n/2 +0.5)* 10,0,0]) cube([9.8,5,2], center=true);
}
module ground(z=200) {
translate([0,0,-z]) cube(z*2,center=true);
}
module sky(height,z=200) {
translate([0,0,height])
rotate([0,180,0]) ground(z);
}
// example uses
/*
function ftop(x,y) = sin(60*x)*sin(90*y) + 2 ;
function fbottom(x,y) =ftop(x,y)-0.5 ;
color("pink")
scale([5,5,2.5]) poly_surface(-4,4,-4,4,100,100);
*/
/*
function fsinc(x,y) =
// eps eliminates a hole at the peak
let (eps=0.001,r = sqrt(x*x + y*y + eps))
sin(180*r)/r;
function ftop(x,y) = fsinc(x,y);
function fbottom(x,y) = ftop(x,y)-0.2 ;
color("forestgreen")
scale([5,5,2.5]) poly_surface(-4,4,-4,4,100,100);
*/
/*
function fquartic(x,y) = x*x+y*y;
function ftop(x,y) = fquartic(x,y)+5;
function fbottom(x,y) = ftop(x,y)-5 ;
color("forestgreen")
scale([5,5,0.5]) poly_surface(-4,4,-4,4,100,100);
*/
function fquartic(x,y,a,b) = a*x*x-b*y*y;
function ftop(x,y) = fquartic(x,y,1,1)+20;
function fbottom(x,y) = 0 ;
color("cornsilk")
scale([5,5,0.5]) poly_surface(-4,4,-4,4,100,100);
* ruler(10);