Discussion:
[OpenSCAD] Can I apply a transformation to a point (list)?
nicnacs
2018-10-13 20:38:54 UTC
Permalink
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/
Marius Kintel
2018-10-13 20:43:33 UTC
Permalink
You’d have to do it manually.
See here for some helper functions:
https://github.com/openscad/scad-utils/blob/master/transformations.scad

-Marius
runsun
2018-10-14 00:27:24 UTC
Permalink
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/
Hans L
2018-10-14 02:16:54 UTC
Permalink
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
Post by runsun
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
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Troberg
2018-10-15 07:22:25 UTC
Permalink
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/

Loading...