Polygon in 3D space?

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

Polygon in 3D space?

thomas-hn
Hello,

I'm relatively new to OpenScad and are working on my first bigger project
right now.
There the following question came up for which I tried to search for
existing discussions here, without success

Is it possible to place a polygon in 3D space and not only on the x/y-plane?

I would like to have a polygon with its vertices (points) at defined
coordinates in 3D space (x,y,z) and then to linearextrude this polygon. Do I
have to use a polyhedron instead? If I got it correctly from the polyhedron
description it cannot be flat, so I have to "linearextrude" it myself by
placing all the vertices at the correct positions in 3D space and not only
my known vertices of the base face.

Any ideas?

Thanks a lot in advance,

Thomas



--
Sent from: http://forum.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: Polygon in 3D space?

nophead
You can linear_extrude() a polygon to make a flat polyhedron that can be placed in 3D space.

On 11 October 2017 at 21:39, thomas-hn <[hidden email]> wrote:
Hello,

I'm relatively new to OpenScad and are working on my first bigger project
right now.
There the following question came up for which I tried to search for
existing discussions here, without success

Is it possible to place a polygon in 3D space and not only on the x/y-plane?

I would like to have a polygon with its vertices (points) at defined
coordinates in 3D space (x,y,z) and then to linearextrude this polygon. Do I
have to use a polyhedron instead? If I got it correctly from the polyhedron
description it cannot be flat, so I have to "linearextrude" it myself by
placing all the vertices at the correct positions in 3D space and not only
my known vertices of the base face.

Any ideas?

Thanks a lot in advance,

Thomas



--
Sent from: http://forum.openscad.org/

_______________________________________________
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: Polygon in 3D space?

fred_dot_u
In reply to this post by thomas-hn
I'm far from a wizard when it comes to openSCAD, but terminology and
application is important. For the purposes of this discussion, one must
consider that a polygon is a multi-sided shape on a flat plane. Once created
using the polygon command, one can rotate it in 3-space. If you extrude it
as part of the operation, you can rotate it as well.

Moving to another part of your question, if your objective is a solid that
is going to have specific points in space, you can pick any three to make a
plane and use those points to create polyhedra.

I'm not sure about your reference suggesting that the polyhedron description
not be flat, as the wiki specifies that "faces" must be in the same plane,
which implies that they are flat.

When you suggest to use linear_extrude, do you require/desire to have
vertical sides prior to any rotation?



--
Sent from: http://forum.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: Polygon in 3D space?

Hypher
In reply to this post by thomas-hn
Thomas,

You can only define polygons in 2d space, but this shouldn’t be too much of a limitation. All points in a polygon must be on the same plane, so if you defined it in 3d space, it would be easy to have an invalid polygon.

The best way to do what you want is probably:
  1. Given your set of 3d points, compute a transform so they all lay on the plane
  2. Use polygon() to render that set of points
  3. Use linear_extrude() to create a 3d shape from those points
  4. Apply the inverse of the original transform to put the 3d shape into place



On Oct 11, 2017, at 13:39, thomas-hn <[hidden email]> wrote:

Hello,

I'm relatively new to OpenScad and are working on my first bigger project
right now.
There the following question came up for which I tried to search for
existing discussions here, without success

Is it possible to place a polygon in 3D space and not only on the x/y-plane?

I would like to have a polygon with its vertices (points) at defined
coordinates in 3D space (x,y,z) and then to linearextrude this polygon. Do I
have to use a polyhedron instead? If I got it correctly from the polyhedron
description it cannot be flat, so I have to "linearextrude" it myself by
placing all the vertices at the correct positions in 3D space and not only
my known vertices of the base face.

Any ideas?

Thanks a lot in advance,

Thomas



--
Sent from: http://forum.openscad.org/

_______________________________________________
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: Polygon in 3D space?

thomas-hn
In reply to this post by fred_dot_u
Maybe let's start my question in another way. For example, we have four
vertices [2, 2, 2], [2, 8, 8], [7, 2, 2], [7, 8, 8] and let's create a face
between them and "linearextrude" this.
It is important to have such four vertices at exact the mentioned positions.

How would you create the 3D object? Please also consider that it is not
ensured that all four vertices are placed so "friendly" as in the example,
they could also be somehow rotatet in space.

Is there a way to create the 3D object which has its four bottom vertices at
the given coordinates without a lot of calculations and
transforming/rotating a cube?



fred_dot_u wrote

> I'm not sure about your reference suggesting that the polyhedron
> description
> not be flat, as the wiki specifies that "faces" must be in the same plane,
> which implies that they are flat.
>
> When you suggest to use linear_extrude, do you require/desire to have
> vertical sides prior to any rotation?
>
>
>
> --
> Sent from: http://forum.openscad.org/
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.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: Polygon in 3D space?

thomas-hn
In reply to this post by Hypher
This sounds like an interesting solution, thanks
However, has anyone an even simpler solution?


Hypher wrote
> The best way to do what you want is probably:
> Given your set of 3d points, compute a transform so they all lay on the
> plane
> Use polygon() to render that set of points
> Use linear_extrude() to create a 3d shape from those points
> Apply the inverse of the original transform to put the 3d shape into place





--
Sent from: http://forum.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: Polygon in 3D space?

atnbueno
Well, if you don't mind the extra faces, this is pretty simple:

hull()
for(p=[[2,2,2], [2,8,8], [7,2,2], [7,8,8]])
translate(p) cube(0.000002, true);

Something funny. My first try was without the for, and it worked with cubes as small as 1E-99, but with the for loop, it doesn't work with cubes of size 1E-6.


2017-10-11 23:30 GMT+02:00 thomas-hn <[hidden email]>:
This sounds like an interesting solution, thanks
However, has anyone an even simpler solution?


Hypher wrote
> The best way to do what you want is probably:
> Given your set of 3d points, compute a transform so they all lay on the
> plane
> Use polygon() to render that set of points
> Use linear_extrude() to create a 3d shape from those points
> Apply the inverse of the original transform to put the 3d shape into place





--
Sent from: http://forum.openscad.org/

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

-- 
Regards from Spain,
Antonio

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

Re: Polygon in 3D space?

Ronaldo
In reply to this post by thomas-hn
A polygon in OpenSCAD is always a 2D shape on the XY plane. You should not apply geometric transformations on it that take it away from the XY plane or you will be in trouble. For instance:

linear_extrude(40)
rotate([90,0,0])
square(20);

Additionally, you are allowed to linear_extrude 2D polygons only.

Following @Appletree, you should define your polygon in the XY plane, extrude it and then move/rotate the 3D shape to the position you want it. You can't avoid geometric transformations in this case. If you find the normal vector of the plane containing the input 3D points ( a cross product) the geometric transformations will be simple to compute:

// input points
p=[[2,2,2], [2,8,8], [7,8,8], [7,2,2]];

nrm = normal(p);
rot = frotFromTo(nrm,[0,0,1]); // compute the rotation to the XY plane
pxy = [for(pi=p) rot*pi]; // rotate the points to XY plane
p2d = [for(pxyi=pxy) [pxyi[0],pxyi[1]]]; // projects on 2D

rotFromTo([0,0,1], nrm)
  linear_extrude(20)
    polygon(p2d);

// normal to the triangle [ p[0], p[1], p[2] ]
function normal(p) = cross(p[1]-p[0], p[2]-p[1]);
// matrix of the transformation of mirroring along a direction
function fmirror(v) =
    norm(v)==0 ? [[1,0,0],[0,1,0],[0,0,1]] :
    let(u = v/norm(v))
    [ [1,0,0] - 2*u[0]*u, [0,1,0] - 2*u[1]*u, [0,0,1] - 2*u[2]*u ];
// matrix of the minimum rotation that brings the vector di to the direction do
function frotFromTo(di,do) =
    norm(di-do)==0 || norm(di)==0 || norm(do)==0 ? 
        [[1,0,0],[0,1,0],[0,0,1]] :
        fmirror(do/norm(do)+di/norm(di)) * fmirror(di);
// the minimum rotation that brings the vector di to the direction do
module rotFromTo(di,do) 
    if( norm(di-do)==0 || norm(di)==0 || norm(do)==0 ) 
        children();
    else 
        mirror(do/norm(do)+di/norm(di)) mirror(di) children();

This code does not check if the input points are coplanar. Anyway, it generates a valid solid provided the first three input points are not colinear.



2017-10-11 17:39 GMT-03:00 thomas-hn <[hidden email]>:
Hello,

I'm relatively new to OpenScad and are working on my first bigger project
right now.
There the following question came up for which I tried to search for
existing discussions here, without success

Is it possible to place a polygon in 3D space and not only on the x/y-plane?

I would like to have a polygon with its vertices (points) at defined
coordinates in 3D space (x,y,z) and then to linearextrude this polygon. Do I
have to use a polyhedron instead? If I got it correctly from the polyhedron
description it cannot be flat, so I have to "linearextrude" it myself by
placing all the vertices at the correct positions in 3D space and not only
my known vertices of the base face.

Any ideas?

Thanks a lot in advance,

Thomas



--
Sent from: http://forum.openscad.org/

_______________________________________________
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: Polygon in 3D space?

cacb
In reply to this post by thomas-hn
On 2017-10-11 22:39, thomas-hn wrote:
> Is it possible to place a polygon in 3D space and not only on the
> x/y-plane?

As others have indicated, a polygon is a 2d entity that by definition in
OpenSCAD exists in the XY plane. This could be seen as a specialization
of the mathematical term "polygon".

You may perhaps want to look at OpenSCAD polyhedron instead, which uses
3d coordinates. It is supposed to have flat faces that are in fact
mathematical polygons.

Carsten Arnholm

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

Re: Polygon in 3D space?

Ronaldo
In reply to this post by Ronaldo
​My last code works because the ​plane through the input points contains the origin. In a general case, we need to do translations besides the rotations. Two additional code lines solves this issue.

// input points
p=[[2,2,2+10], [2,8,8+10], [7,8,8+10], [7,2,2+10]]; // changed to not be coplanar with the origin

nrm = normal(p);
rot = frotFromTo(nrm,[0,0,1]); // compute the rotation to the XY plane
pxy = [for(pi=p) rot*pi]; // rotate the points to XY plane
p2d = [for(pxyi=pxy) [pxyi[0],pxyi[1]]]; // projects on 2D
dis = pxy[0][2]; // compute the displacement of the projection

rotFromTo([0,0,1], nrm)
  translate([0,0,dis]) // translate it back
    linear_extrude(20)
      polygon(p2d);

%color("red") poly(p);

module poly(p)
  hull() for(pi=p) translate(pi) cube(0.0001,center=true);

The red polygon is drawn here as a check reference of the input polygon.

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

Re: Polygon in 3D space?

herdima
In reply to this post by thomas-hn
Hi Thomas,

degenerate faces and degenerate polyhedra are quite useful for what you want
to do.
A flat, degenerate polyhedron can be generated from even more degenerate
triangles like this:

module polyhedrify_points(points) {
    faces = [for(i=[0:len(points)-1]) [i,i,i]];      
    hull()
    polyhedron(points, faces);
}

polyhedrify_points([[2,2,2], [2,8,8], [7,2,2], [7,8,8]]);
// Or for a non-degenerate result:
//polyhedrify_points([[2,2,2], [2,8,8], [7,2,2], [7,8,8], [0,0,10]]);

From the above it is trivial to linear extrude by some vector v.
One way for this is to hull with the translated result:

hull()
for(i=[0,1])
translate(i*[-2,-6,0])
polyhedrify_points([[2,2,2], [2,8,8], [7,2,2], [7,8,8]]);

Degenerate faces and degenerate polyhedra are not documented in OpenSCAD.
So I am not sure if the above will remain possible in all future versions.
Maybe someone can tell. I am using 2015.03-2.

Martin
www.panohero.de




--
Sent from: http://forum.openscad.org/

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