Search code examples
openscad

Manually building Hexagonal Torus


I am interested in building a hexagonal Torus using a mesh of points?

I think I can start with a 2-d polygon, and then iterate 360 times (1 deg resolution) to build a complete solid.

Is this the best way to do this? What I'm really after is building wing profiles with variable cross section geometry over it's span.


Solution

  • In Your way You can do this with polyhedron(). Add an appropriate number of points per profile in defined order to a vector „points“, define faces by the indices of the points in a second vector „faces“ and set both vectors as parameter in polyhedron(), see documentation. You can control the quality of the surface by the number of points per profile and the distance between the profiles (sectors in torus).

    Here an example code:

    // parameter:
    r1 = 20;            // radius of torus
    r2 = 4;         // radius of polygon/ thickness of torus
    s = 360;            // sections per 360 deg
    p = 6;          // points on polygon
    a = 30;                 // angle of the first point on Polygon
    
    // points on cross-section
    // angle = 360*i/p + startangle, x = r2*cos(angle), y = 0, z = r2*sin(angle)
    function cs_point(i) = [r1 + r2*cos(360*i/p + a), 0, r2*sin(360*i/p + a)];
    
    // returns to the index in the points - vector the section number and the number of the point on this section
    function point_index(i) = [floor(i/p), i - p*floor(i/p)];
    
    // returns the points  x-, y-, z-coordinates by rotatating the corresponding point from crossection around the z-axis 
    function iterate_cs(i) = [cs[point_index(i)[1]][0]*cos(360*floor(i/p)/s), cs[point_index(i)[1]][0]*sin(360*floor(i/p)/s), cs[point_index(i)[1]][2]];
    
    // for every point find neighbour points to build faces, ( + p: point on the next cross-section), points ordered clockwise
    // to connect point on last section to corresponding points on first section 
    function item_add1(i) = i >= (s - 1)*p ? -(s)*p : 0; 
    // to connect last point on section to first points on the same and the next section
    function item_add2(i) = i - p*floor(i/p) >= p-1 ? -p : 0;
    // build faces
    function find_neighbours1(i) = [i, i + 1 + item_add2(i), i + 1 + item_add2(i) + p + item_add1(i)];
    function find_neighbours2(i) = [i, i + 1 + + item_add2(i) + p + item_add1(i), i + p + item_add1(i)];
    
    cs = [for (i = [0:p-1]) cs_point(i)];
    points = [for (i = [0:s*p - 1]) iterate_cs(i)];
    faces1 = [for (i = [0:s*p - 1]) find_neighbours1(i)];
    faces2 = [for (i = [0:s*p - 1]) find_neighbours2(i)];
    faces = concat(faces1, faces2);
    
    polyhedron(points = points, faces = faces);
    

    here the result:

    enter image description here

    Since openscad 2015-03 faces can have more than 3 points, if all points of the face are on the same plane. So in this case faces could be build in one step too.