[OpenSCAD] How to avoid the 'flatten' function

doug moen doug at moens.org
Wed May 6 15:18:47 EDT 2015


Since we introduced list comprehensions, people have been using them to
generate arguments for polyhedron. But they've run into a problem where the
list is nested too deeply. So a convention of using the function 'flatten'
has arisen.

I've been thinking about different ways to eliminate the need for an
auxiliary flatten function, and I've come up with an idiom that works
today, without any language changes.

Here's the "before" code, using flatten (I forget the original author):

r = 10;
h = 20;
w = 2;
s = 2;
step = 4;
vertices = [for (a=[0:step:360]) [
  [r*cos(a), r*sin(a), 0],
  [r*cos(a), r*sin(a), h + s*sin(a*6)],
  [(r-w)*cos(a), (r-w)*sin(a), h + s*sin(a*6)],
  [(r-w)*cos(a), (r-w)*sin(a), 0]
]];
nv = 4*360/step;
indices = [for (a=[0:360/step]) [
  [(4*a+0)%nv, (4*a+1)%nv, (4*a+5)%nv, (4*a+4)%nv],
  [(4*a+1)%nv, (4*a+2)%nv, (4*a+6)%nv, (4*a+5)%nv],
  [(4*a+2)%nv, (4*a+3)%nv, (4*a+7)%nv, (4*a+6)%nv],
  [(4*a+3)%nv, (4*a+0)%nv, (4*a+4)%nv, (4*a+7)%nv]]];
function flatten(list) = [ for (i = list, v = i) v ];
polyhedron(flatten(vertices), flatten(indices));

Now here is the "after" code, which eliminates the need for 'flatten'.

r = 10;
h = 20;
w = 2;
s = 2;
step = 4;
vertices = [
  for (a=[0:step:360])
    for (pt = [
      [r*cos(a), r*sin(a), 0],
      [r*cos(a), r*sin(a), h + s*sin(a*6)],
      [(r-w)*cos(a), (r-w)*sin(a), h + s*sin(a*6)],
      [(r-w)*cos(a), (r-w)*sin(a), 0]
    ]) pt
];
nv = 4*360/step;
indices = [
  for (a=[0:360/step])
    for (pt = [
      [(4*a+0)%nv, (4*a+1)%nv, (4*a+5)%nv, (4*a+4)%nv],
      [(4*a+1)%nv, (4*a+2)%nv, (4*a+6)%nv, (4*a+5)%nv],
      [(4*a+2)%nv, (4*a+3)%nv, (4*a+7)%nv, (4*a+6)%nv],
      [(4*a+3)%nv, (4*a+0)%nv, (4*a+4)%nv, (4*a+7)%nv]
    ]) pt
];
polyhedron(vertices, indices);

I guess it is a matter of personal taste, but I hope some people find this
useful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150506/c48aa6e2/attachment-0002.html>


More information about the Discuss mailing list