-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathimage-to-object.scad
167 lines (147 loc) · 4.61 KB
/
image-to-object.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
163
164
165
166
167
/*
plane and cylindrical images
creates a modulated plane surface or a partial cylindrical object using included data generated from a JPG file
to create a relief or intaglio (as for a lithophane) surface
image file is generated by image_to_openscad.py
Kit Wallace March 2015
- need to handle closed curve
Code licensed under the Creative Commons - Attribution - Share Alike license.
*/
function reverse(l) =
// reverse the elements of an array
[for (i=[1:len(l)]) l[len(l)-i]];
// ------------------ generic dual surface faces --------------
function vt(i,j,nx,ny) = i +j*nx ;
function vb(i,j,nx,ny) = nx*ny + i +j*nx;
function surface_faces (nx,ny) =
concat( // top
[for (i=[0:nx-2])
for (j=[0:ny-2])
reverse([vt(i,j,nx,ny),
vt(i+1,j,nx,ny),
vt(i+1,j+1,nx,ny),
vt(i,j+1,nx,ny)])
], // bottom
[for (i=[0:nx-2])
for (j=[0:ny-2])
[vb(i,j,nx,ny),
vb(i+1,j,nx,ny),
vb(i+1,j+1,nx,ny),
vb(i,j+1,nx,ny)
]
], // sides
[for (i=[0:nx-2])
[vt(i,0,nx,ny),
vt(i+1,0,nx,ny),
vb(i+1,0,,nx,ny),
vb(i,0,,nx,ny)
]
],
[for (i=[0:nx-2])
reverse( [vt(i,ny-1,nx,ny),
vt(i+1,ny-1,nx,ny),
vb(i+1,ny-1,nx,ny),
vb(i,ny-1,nx,ny)
])
],
[for (j=[0:ny-2])
reverse( [vt(0,j,nx,ny),
vt(0,j+1,nx,ny),
vb(0,j+1,nx,ny),
vb(0,j,nx,ny)])
],
[for (j=[0:ny-2])
[vt(nx-1,j,nx,ny),
vt(nx-1,j+1,nx,ny),
vb(nx-1,j+1,nx,ny),
vb(nx-1,j,nx,ny)]
]
);
// ----------------- surface with functional modulation --------------
function fsurface(x,y) = 10* sin(y*180);
function ftop(x,y) = fsurface(x,y);
function fbottom(x,y) = ftop(x,y);
// function fsurface(x,y) = 0;
function image_plane(width,height,nx,ny,surface,depth=1,offset=0,sign) =
[for (j=[0:ny-1])
for (i=[0:nx-1])
let (eij= surface[i][ny-1-j] * depth,
x= i/nx,
y= j/ny,
z = ftop(x,y))
[width * x,
height* y,
offset + z + sign*eij]
];
function clear_plane(width,height,nx,ny,offset) =
[for (j=[0:ny-1])
for (i=[0:nx-1])
let (x= i/nx,
y= j/ny,
z = fbottom(x,y))
[width * i/nx,
height* j/ny,
offset + z]
];
module plane_surface(height,surface,depth=1,thickness,relief) {
sign = relief ? +1 : -1;
nx= len(surface);
ny= len(surface[0]);
echo ("nx=",nx,"ny=",ny);
width= height* nx/ny ;
echo("height",height,"width",width);
image_points = image_plane(width,height,nx,ny,surface,depth,thickness,sign);
clear_points =clear_plane(width,height,nx,ny,0);
faces=surface_faces(nx,ny);
polyhedron(points = concat(image_points,clear_points),
faces = faces);
};
function image_arc(radius,height,nx,ny,surface,depth=1,degrees=360,sign) =
[for (j=[0:ny-1])
for (i=[0:nx-1])
let (eij= surface[i][ny-1-j] * depth)
let(a = degrees * i/nx)
[(radius + sign* eij) * cos(a),
(radius + sign* eij) * sin(a),
height * j/ny]
];
function clear_arc(radius,height,nx,ny,degrees=360) =
[for (j=[0:ny-1])
for (i=[0:nx-1])
let(a = degrees * i/nx)
[radius * cos(a),
radius * sin(a),
height * j/ny]
];
module cylinder_surface(height,surface,depth=1,degrees=360,thickness,relief) {
sign = relief ? +1 : -1;
nx= len(surface);
ny= len(surface[0]);
echo ("nx=",nx,"ny=",ny);
circumference= height* nx/ny * 360/degrees;
radius =circumference/(2* PI);
echo("height",height,"circumference",circumference,"radius",radius);
image_points = image_arc(radius,height,nx,ny,surface,depth,degrees,sign);
clear_points =clear_arc(radius-thickness,height,nx,ny,degrees);
faces=surface_faces(nx,ny);
polyhedron(points = concat(image_points,clear_points),
faces = faces);
};
/* ------------------- main ------------------ */
include <alfredo2.scad>
echo(image_file);
cylinder_surface(
height=100,
surface=image_data,
depth=6,
degrees=90,
thickness=6,
relief=false);
/*
plane_surface(
height=100,
surface=image_data,
depth=6,
thickness=6,
relief=false);
*/