-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcanvas.scad
253 lines (232 loc) · 8.47 KB
/
canvas.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
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
/*
* Copyright (c) 2019, Vigibot
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Description: Canvas on which we can draw Grayscale images
* Design: Gilles Bouissac
* Author: Gilles Bouissac
*/
use <agentscad/extensions.scad>
use <agentscad/mesh.scad>
//
// These tools works with folowing convensions:
// images are double array of levels in range [0:255] (0:black, 255:white):
// - First level of array = rows (first row is top of image)
// - Second level of array = columns (first col is left of image)
//
// images don't have any dimension, just a list of points
// dimensions comes with canvas
//
// example for a n*m image:
// [
// //row 0: top of image
// [ <row_0/col_0>, <row_0/col_1> ... <row_0/col_n> ],
// [ <row_1/col_0>, <row_1/col_1> ... <row_1/col_n> ],
// ...
// [ <row_m/col_0>, <row_m/col_1> ... <row_m/col_n> ],
// ]
//
// ----------------------------------------
// Grayscale Images
// ----------------------------------------
//
// Returns a sub-image of input image
//
// image: image levels data
// size: size of sub-image to return, set to image size if undef
// start: starting point of sub-image in image, set to [0,0] if undef
//
function imageCrop ( image, size=undef, start=undef ) = let (
nx = len(image[0]),
ny = len(image),
sx = forceValueInRange(start.x, minv=0, maxv=nx-2),
sy = forceValueInRange(start.y, minv=0, maxv=ny-2),
ex = forceValueInRange(size.x, minv=1, maxv=nx-sx-1, defv=nx-sx-1) + sx,
ey = forceValueInRange(size.y, minv=1, maxv=ny-sy-1, defv=ny-sy-1) + sy
) [ for ( y=[sy:ey] ) [ for ( x=[sx:ex] ) image[y][x] ] ];
//
// Draw an image on a given canvas and returns a new image
//
// Placement of the image in the canvas:
// size: is the size [szx,szy] of the image in the canvas
// set to canvas size if undef
// start: top left point [x,y] of the image in the canvas
// computed to center the image in the canvas if undef
// preserve: controls aspect ratio:
// if true either szx or szy is reduced in order to preserve
// image aspect ratio in the canvas (assume each point in image is a square)
// outerlevel: image level to pick for points of the canvas that are not in the image
// and that do not have a previous value
//
function drawImage ( image, canvas, size=undef, start=undef, preserve=true, outerlevel=255 ) = let (
class = assertClass(canvas,classCanvas()),
nx = len(image[0]),
ny = len(image),
npx = getCanvasNbPointx(canvas)-1,
npy = getCanvasNbPointy(canvas)-1,
cansz = getCanvasSize(canvas),
pixsz = getCanvasPixelSize(canvas),
ratio = nx/ny,
tmpszx = forceValueInRange(size.x, minv=0, maxv=cansz.x, defv=cansz.x),
tmpszy = forceValueInRange(size.y, minv=0, maxv=cansz.y, defv=cansz.y),
xratio = tmpszy*ratio,
yratio = tmpszx/ratio,
szx = preserve?(xratio<tmpszx?xratio:tmpszx):tmpszx,
szy = preserve?(yratio<tmpszy?yratio:tmpszy):tmpszy,
cx = forceValueInRange(start.x,minv=0,maxv=cansz.x-szx,defv=(cansz.x-szx)/2),
cy = forceValueInRange(start.y,minv=0,maxv=cansz.y-szy,defv=(cansz.y-szy)/2)
) [ classCanvas(),
cansz, pixsz, [
for ( iy=[0:npy] ) [
for ( ix=[0:npx] ) let (
x = ix*pixsz.x,
y = iy*pixsz.y
)
[ x, y, __getLevelAt(image,[szx,szy],[x-cx,szy-(y-cy)],outerlevel=outerlevel) ]
]
]
];
// ----------------------------------------
// Canvas
// ----------------------------------------
//
// Builds a mesh from canvas
// Assume canvas pixel data values are in range [0,255]
//
// canvas: canvas filled with image
//
function canvas2mesh (
canvas, minlayer=0.01, thickness=1,
skin=false, positive=true
) = let (
class = assertClass(canvas,classCanvas()),
nx = getCanvasNbPointx(canvas),
ny = getCanvasNbPointy(canvas),
size = getCanvasSize(canvas),
image = getCanvasPoints(canvas),
szx = size.x,
szy = size.y,
szz = thickness,
dx = szx/(nx-1),
dy = szy/(ny-1),
bix = nx*ny, // idx of first base point
lmin = minlayer<0?0:minlayer,
vertices = [
// Curved upper surface
for ( iy=[0:ny-1] )
for ( ix=[0:nx-1] )
let ( elevation = lmin + szz*(positive ? 1-image[iy][ix].z/255 : image[iy][ix].z/255 ))
[ image[iy][ix].x, image[iy][ix].y, elevation ]
,
// Flat lower surface
if ( !skin )
for ( iy=[0:ny-1] )
for ( ix=[0:nx-1] )
[ image[iy][ix].x, image[iy][ix].y, 0 ]
],
volumefaces = skin ? []:[
// Lower surface
for ( iy=[0:ny-2] )
for ( ix=[0:nx-2] ) let ( idx=bix+iy*nx+ix )
[ idx, idx+1, idx+1+nx, idx+nx ],
// Top Wall
for ( ix=[0:nx-2] ) let ( iup=ix, ilo=bix+iup )
[ iup, iup+1, ilo+1, ilo ],
// Right Wall
for ( iy=[0:ny-2] ) let ( iup=(iy+1)*nx-1, ilo=bix+iup )
[ iup, iup+nx, ilo+nx, ilo ],
// Left Wall
for ( iy=[0:ny-2] ) let ( iup=iy*nx, ilo=bix+iup )
[ iup, ilo, ilo+nx, iup+nx ],
// Bottom Wall
for ( ix=[0:nx-2] ) let ( iup=ix+bix-nx, ilo=bix+iup )
[ iup, ilo, ilo+1, iup+1 ]
],
imageface = [
// Upper surface
for ( iy=[0:ny-2] )
for ( ix=[0:nx-2] ) let ( idx=iy*nx+ix )
[ idx, idx+nx, idx+1+nx, idx+1 ]
]
) newMesh ( vertices, concat( imageface, volumefaces ) );
//
// Creates a canvas
// size: Size [szx, szy] of the image in the canvas
// nbpixel: Number of pixels [npx,npy] in the canvas
// level: Default level of the image in the canvas
//
function newCanvas ( size=undef, nbpixel=undef, level=255 ) = let (
szx = forceValueInRange(size.x,1e-6),
szy = forceValueInRange(size.y,1e-6),
npx = forceValueInRange(nbpixel.x,1,1e6),
npy = forceValueInRange(nbpixel.y,1,1e6),
szpx = szx/npx,
szpy = szy/npy
) [ classCanvas(),
[szx,szy], [szpx,szpy], [
for ( iy=[0:npy] ) [
for ( ix=[0:npx] ) let (
x = ix*szpx,
y = iy*szpy
) [ x, y, level ]
]
]
];
function getCanvasSize(canvas) = canvas[1];
function getCanvasPixelSize(canvas) = canvas[2];
function getCanvasPoints(canvas) = canvas[3];
function getCanvasNbPointx(canvas) = len(canvas[3][0]);
function getCanvasNbPointy(canvas) = len(canvas[3]);
// ----------------------------------------
// Implementation
// ----------------------------------------
function classCanvas() = "canvas";
//
// Interpolate image data to compute the level at random position
//
// image: image levels data
// size: size [size.x, size.y] of the image
// pos: position [pos.x, pos.y] in the image where we need the level
// outerlevel: level to return if pos is not in the image
//
function __getLevelAt( image, size, pos, outerlevel=255 ) = let (
nx = len(image[0]),
ny = len(image),
ixc = floor( (nx-1-0.0001)*pos.x/size.x ),
iyc = floor( (ny-1-0.0001)*pos.y/size.y ),
ix = ixc==nx-1?nx-2:ixc,
iy = iyc==ny-1?ny-2:iyc,
dx = size.x/(nx-1),
dy = size.y/(ny-1),
x1 = ix*dx,
y1 = iy*dy,
x2 = x1 + dx,
y2 = y1 + dy
) ix<0||iy<0||ix>nx-1||iy>ny-1?outerlevel:
interpolateBilinear ( pos,
[ x1, y1, image[iy][ix] ],
[ x1, y2, image[iy+1][ix] ],
[ x2, y1, image[iy][ix+1] ],
[ x2, y2, image[iy+1][ix+1] ]
);
// ----------------------------------------
// Showcase
// ----------------------------------------
module showLithophane() {
image = [
[ 100, 90, 70, 140 ],
[ 255, 160, 170, 45 ],
[ 110, 120, 30, 140 ],
[ 160, 170, 120, 60 ]
];
empty = newCanvas( [10,10], [30,30] );
canvas = drawImage ( image, empty );
lithophane = canvas2mesh( canvas, skin=false, thickness=10 );
echo(getCanvasNbPointy(canvas));
meshPolyhedron ( lithophane );
}
showLithophane();