Can I apply a transformation to a point (list)?

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

Can I apply a transformation to a point (list)?

nicnacs
Sometimes it seems like it would be useful to be able to take a given
transformation to just a point, and then use that point as a reference
later.  For example, let's say I have a transformation:
translate([1,2,3]) rotate([4,5,6]) { objects...}

it would be nice to be able to do something like:
newpt = translate([1,2,3]) rotate([4,5,6]) [x,y,z]

and then use newpt for other purposes.  Is something like this possible?
Thanks,
Nick




--
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: Can I apply a transformation to a point (list)?

kintel
Administrator
You’d have to do it manually.
See here for some helper functions:
https://github.com/openscad/scad-utils/blob/master/transformations.scad

 -Marius


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

Re: Can I apply a transformation to a point (list)?

runsun
In reply to this post by nicnacs
Alsp check this out:
https://github.com/runsun/OpenSCAD_Tips/blob/master/snippets.md#rotate_anyaxis



-----

$  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer ); $ Tips ; $ Snippets

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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: Can I apply a transformation to a point (list)?

thehans
I also have a library which you might find useful.  It re-implements
all the builtin transformations(among other things) as functions that
can work with with list of points or "polyhedron data": [points,
faces], passed as the "poly" parameter, with all other parameters
acting the same as in OpenSCAD builtins:
https://github.com/thehans/functionalopenscad
On Sat, Oct 13, 2018 at 7:28 PM runsun <[hidden email]> wrote:

>
> Alsp check this out:
> https://github.com/runsun/OpenSCAD_Tips/blob/master/snippets.md#rotate_anyaxis
>
>
>
> -----
>
> $  Runsun Pan, PhD $ libs: scadx , doctest , faces ( git ), offline doc ( git ), runscad.py ( 2 , git ), editor of choice: CudaText  ( OpenSCAD lexer );&nbsp;$ Tips ;&nbsp;$ Snippets
>
> --
> 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: Can I apply a transformation to a point (list)?

Troberg
In reply to this post by nicnacs
Some very simple functions I made for the purpose. You need to use the two
first, but they rely on some of the others, so you need to include them as
well.

//Translate coordinate
function xtrancoord(dx,co)=[co[0]+dx,co[1],co[2]];

//Rotate coordinate
function
rotcoord(rot,co)=zrotcoord(rot[2],yrotcoord(rot[1],xrotcoord(rot[0],co)));

//Translate along one axis only
function ytrancoord(dy,co)=[co[0],co[1]+dy,co[2]];
function ztrancoord(dz,co)=[co[0],co[1],co[2]+dz];
function trancoord(d,co)=[co[0]+d[0],co[1]+d[1],co[2]+d[2]];

//Rotate around one axis only
function xrotcoord(rx,co)=[co[0],
                                                        rotcoord_yz(co[1],co[2],rx)[1],
                                                        rotcoord_yz(co[1],co[2],rx)[2]];
function yrotcoord(ry,co)=[rotcoord_xz(co[0],co[2],-ry)[0],
                                                        co[1],
                                                        rotcoord_xz(co[0],co[2],-ry)[2]];
function zrotcoord(rz,co)=[rotcoord_xy(co[0],co[1],rz)[0],
                                                        rotcoord_xy(co[0],co[1],rz)[1],
                                                        co[2]];

//Angle between origin and 2D point
function
angle2d(x2,z2)=-(-90+((0<z2)?0:180)+((0<x2)?-1:1)*atan(sqrt(pow(0-x2,2)+pow(0-0,2))/(0-z2)));

Distance between origin and 2D point
function distance2d(x2,y2)=sqrt(pow(0-x2,2)+pow(0-y2,2));

//Returns rotation vector between two points, assuming the object to be
rotated is along the X axis
function angle(x1,y1,z1,x2,y2,z2)=[0,
                                 
-90+((z1<z2)?0:180)+((x1<x2)?-1:1)*atan(sqrt(pow(x1-x2,2)+pow(y1-y2,2))/(z1-z2)),
                                   atan((y1-y2)/(x1-x2))];

//Distance between two points in 3D
function
distance(x1,y1,z1,x2,y2,z2)=sqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2));

//These are just helpers
function rotcoord_yz(y,z,rot)=pol2rec_yz(distance2d(y,z),angle2d(y,z)+rot);
function rotcoord_xz(x,z,rot)=pol2rec_xz(distance2d(x,z),angle2d(x,z)+rot);
function rotcoord_xy(x,y,rot)=pol2rec_xy(distance2d(x,y),angle2d(x,y)+rot);
function pol2rec_yz(dist,rot)=[0,
                                                                dist*cos(rot),
                                                                dist*sin(rot)];
function pol2rec_xz(dist,rot)=[dist*cos(rot),
                                                                0,
                                                                dist*sin(rot)];
function pol2rec_xy(dist,rot)=[dist*cos(rot),
                                                                dist*sin(rot),
                                                                0];


Note: For convenience and clearer code, I use modules to do xtran, ytran,
ztran, xrot, yrot, zrot, with counterparts for coordinates. Makes things
much clearer when you only move/rotate in one direction.



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

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