need some help with polyhedron()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

need some help with polyhedron()

Mekko
I've been trying to learn polyhedron() by making a cylinder the hard way and I used Trygon's code for the spring washer to get me started (thanks Trygon!)

The object renders fine in OpenSCAD but it looks very messed up in MeshLab. Here are some pictures:
http://imgur.com/a/SCj2R

Is the order of points in a face important? Is the order of faces important? I'm kind of floundering.

Here's the code:

cyl_rad=30; // outside radius
core_rad=10; // hole radius
height=80; // tube height
layers=20; // layers
segments=20;


function cyl(h) = let(
    outer_points=[for(l=[0:layers-1]) for(i=[0:segments-1]) let(a=i*360/segments) [cyl_rad*cos(a), cyl_rad*sin(a), l*(h/layers)]],
    inner_points=[for(l=[0:layers-1]) for(i=[0:segments-1]) let(a=i*360/segments) [core_rad*cos(a), core_rad*sin(a), l*(h/layers)]])
   
    [concat(outer_points, inner_points)];
   

function tops_and_bottoms() = let(
    bottoms1=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i, j, j+layers*segments]],
    bottoms2=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i, i+layers*segments, j+layers*segments]],
    tops1=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i+(layers-1)*segments, j+(layers-1)*segments, j+layers*segments+(layers-1)*segments]],
    tops2=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i+(layers-1)*segments, i+layers*segments+(layers-1)*segments, j+layers*segments+(layers-1)*segments]])
       
    [concat(bottoms1, bottoms2, tops1, tops2)];
   
function sides() = [for(l=[0:layers-2]) [for(i=[0:segments-1]) let(j=(i+1)%segments) [
        [i+l*segments, j+l*segments, j+(l+1)*segments], [i+l*segments, i+(l+1)*segments, j+(l+1)*segments],
        [i+(l+layers)*segments, j+(l+layers)*segments, j+(l+1+layers)*segments], [i+(l+layers)*segments, i+(l+1+layers)*segments, j+(l+1+layers)*segments]]]];
   

function Unpack(p) = [for(i=p) for(j=i) j];

cyl_points=Unpack(cyl(height));

allfaces=concat(Unpack(tops_and_bottoms()), Unpack(Unpack(sides())));
 
polyhedron(cyl_points, allfaces);
Reply | Threaded
Open this post in threaded view
|

Re: need some help with polyhedron()

nophead
From the manual: "Point ordering for faces When looking at the face from the outside inwards, the points must be clockwise."

The order of the faces does not matter.

On 17 December 2015 at 15:08, Mekko <[hidden email]> wrote:
I've been trying to learn polyhedron() by making a cylinder the hard way and
I used Trygon's code for the spring washer to get me started (thanks
Trygon!)

The object renders fine in OpenSCAD but it looks very messed up in MeshLab.
Here are some pictures:
http://imgur.com/a/SCj2R

Is the order of points in a face important? Is the order of faces important?
I'm kind of floundering.

Here's the code:

cyl_rad=30; // outside radius
core_rad=10; // hole radius
height=80; // tube height
layers=20; // layers
segments=20;


function cyl(h) = let(
    outer_points=[for(l=[0:layers-1]) for(i=[0:segments-1])
let(a=i*360/segments) [cyl_rad*cos(a), cyl_rad*sin(a), l*(h/layers)]],
    inner_points=[for(l=[0:layers-1]) for(i=[0:segments-1])
let(a=i*360/segments) [core_rad*cos(a), core_rad*sin(a), l*(h/layers)]])

    [concat(outer_points, inner_points)];


function tops_and_bottoms() = let(
    bottoms1=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i, j,
j+layers*segments]],
    bottoms2=[for(i=[0:segments-1]) let (j=(i+1)%segments) [i,
i+layers*segments, j+layers*segments]],
    tops1=[for(i=[0:segments-1]) let (j=(i+1)%segments)
[i+(layers-1)*segments, j+(layers-1)*segments,
j+layers*segments+(layers-1)*segments]],
    tops2=[for(i=[0:segments-1]) let (j=(i+1)%segments)
[i+(layers-1)*segments, i+layers*segments+(layers-1)*segments,
j+layers*segments+(layers-1)*segments]])

    [concat(bottoms1, bottoms2, tops1, tops2)];

function sides() = [for(l=[0:layers-2]) [for(i=[0:segments-1])
let(j=(i+1)%segments) [
        [i+l*segments, j+l*segments, j+(l+1)*segments], [i+l*segments,
i+(l+1)*segments, j+(l+1)*segments],
        [i+(l+layers)*segments, j+(l+layers)*segments,
j+(l+1+layers)*segments], [i+(l+layers)*segments, i+(l+1+layers)*segments,
j+(l+1+layers)*segments]]]];


function Unpack(p) = [for(i=p) for(j=i) j];

cyl_points=Unpack(cyl(height));

allfaces=concat(Unpack(tops_and_bottoms()), Unpack(Unpack(sides())));

polyhedron(cyl_points, allfaces);



--
View this message in context: http://forum.openscad.org/need-some-help-with-polyhedron-tp15196.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: need some help with polyhedron()

Mekko
Aargh! Thanks for your patience, nophead - that's the politest RTFM I think I've ever read.

Sorry to waste everyone's time!

I should have read the manual more carefully and I would have if OpenSCAD had flagged that something was wrong. Or if it had rendered all messed up like MeshLab.
Reply | Threaded
Open this post in threaded view
|

Re: need some help with polyhedron()

nophead
If you press F12 it will show the reversed faces in purple in the "Thrown together view". Not sure why it doesn't always do that, presumably it's a performance issue.

On 17 December 2015 at 17:45, Mekko <[hidden email]> wrote:
Aargh! Thanks for your patience, nophead - that's the politest RTFM I think
I've ever read.

Sorry to waste everyone's time!

I should have read the manual more carefully and I would have if OpenSCAD
had flagged that something was wrong. Or if it had rendered all messed up
like MeshLab.



--
View this message in context: http://forum.openscad.org/need-some-help-with-polyhedron-tp15196p15198.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org