discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

module for rotating an object around any axis is required

GS
Guenther Sohler
Sun, Mar 26, 2023 11:58 AM

Hi all together

I believe, that openscad can YET already  rotate an object around an
arbitrary axis!

for(angle=[0,120,240])
{
rotate(v=[1,1,1],a=angle)
cylinder(r=1,h=10);
}

this turns an cylinder showing up into 3 different directions showing up,
front,  and right.
i can imagine this like rotating an  object around a cube's corner

[image: image.png]

code for that is already present in src/core/Transform.cc

On Fri, Mar 24, 2023 at 10:24 PM Kevin Toppenberg kdtop3@gmail.com wrote:

The math is too dense for me to pick apart, but it seems that when I
worked on this in a different engine, that the math involves quaternions.

Does this solution use quaternions?

Kevin

On Wed, Mar 22, 2023 at 10:45 PM Sanjeev Prabhakar <
sprabhakar2006@gmail.com> wrote:

I would propose if a module can be developed to rotate an object around
an axis.

e.g. for any sequence of rotations an equivalent axis and angle can be
found from the functions written below

function cvar(a)=
let(
text=a[0],

b=search("-",a)!=[]&&search(".",a)!=[]?[for(i=[2:len(a)-1])if(a[i]!=".")a[i]]:
search("-",a)!=[]&&search(".",a)==[]?[for(i=[2:len(a)-1])a[i]]:

search("-",a)==[]&&search(".",a)!=[]?[for(i=[1:len(a)-1])if(a[i]!=".")a[i]]:[for(i=[1:len(a)-1])a[i]],
n=[["0"],["1"],["2"],["3"],["4"],["5"],["6"],["7"],["8"],["9"]],
l=len(b),
c=[for(i=[0:l-1])each search(b[i],n)],
d=[for(i=[0:len(c)-1])10^(l-i-1)],
e=c*d,

f=search(".",a)!=[]&&search("-",a)!=[]?e10^-(l-(search(".",a)[0]-2)):search(".",a)!=[]&&search("-",a)==[]?e10^-(l-(search(".",a)[0]-1)):e,
g=search("-",a)!=[]?f*-1:f
)[text,g];

//function to rotate a point around a vector(axis) with angle theta

function q(vector=[1,0,0],point=[0,5,0],theta=0)=

let(t=theta,
v=vector/norm(vector),
p=[cos(t/2),vsin(t/2)],
p1=[p.x,-p.y],
q=[0,len(point)==2?[point.x,point.y,0]:point],
pq=[p.x
q.x-p.yq.y,p.xq.y+p.yq.x+cross(p.y,q.y)],
pqp1=[pq.x
p1.x-pq.yp1.y,pq.xp1.y+pq.y*p1.x+cross(pq.y,p1.y)],
transformation=pqp1.y
)

transformation
;
// function is input to another function q_rot
function qmr1(s,r,pl,n=0)= n==len(s)?pl:
qmr1(s,r,
let(
v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1],
r1=r[n]==undef?0:r[n])
[for(p=pl)q(v1,p,r1)],n+1);
//function is input to another function q_rot
function qmr2(s,r,pl,n=0)= n==len(s)?pl:qmr2(s,r,let(
v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1],
r1=r[n]==undef?0:r[n])
[for(i=[0:len(pl)-1])[for(p=pl[i])q(v1,p,r1)]],n+1);

//function to rotate a group of points "pl" around a series of axis with
defined angles e.g q_rot(s=["z20","x40","y80"],pl=[[2,0],[10,2]])=> will
rotate the line first around z axis by 20 deg then around x axis by 40
degrees and then around y axis by 80 degrees.
function q_rot(s,pl)=
is_num(pl[0][0])?qmr1([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl):qmr2([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl);

// sequence of rotation defined by r1
r1=["x30","y40","z100"];
vz=[0,0,1];
v1=q_rot(r1,[vz])[0];

v2=cross(vz,v1);
theta=acos(vz*v1/norm(v1));

// equivalent rotation axis and angle of rotation w.r.t vector vz
echo(v2,theta);


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Hi all together I believe, that openscad can YET already rotate an object around an arbitrary axis! for(angle=[0,120,240]) { rotate(v=[1,1,1],a=angle) cylinder(r=1,h=10); } this turns an cylinder showing up into 3 different directions showing up, front, and right. i can imagine this like rotating an object around a cube's corner [image: image.png] code for that is already present in src/core/Transform.cc On Fri, Mar 24, 2023 at 10:24 PM Kevin Toppenberg <kdtop3@gmail.com> wrote: > The math is too dense for me to pick apart, but it seems that when I > worked on this in a different engine, that the math involves quaternions. > > Does this solution use quaternions? > > Kevin > > On Wed, Mar 22, 2023 at 10:45 PM Sanjeev Prabhakar < > sprabhakar2006@gmail.com> wrote: > >> I would propose if a module can be developed to rotate an object around >> an axis. >> >> e.g. for any sequence of rotations an equivalent axis and angle can be >> found from the functions written below >> >> function cvar(a)= >> let( >> text=a[0], >> >> b=search("-",a)!=[]&&search(".",a)!=[]?[for(i=[2:len(a)-1])if(a[i]!=".")a[i]]: >> search("-",a)!=[]&&search(".",a)==[]?[for(i=[2:len(a)-1])a[i]]: >> >> search("-",a)==[]&&search(".",a)!=[]?[for(i=[1:len(a)-1])if(a[i]!=".")a[i]]:[for(i=[1:len(a)-1])a[i]], >> n=[["0"],["1"],["2"],["3"],["4"],["5"],["6"],["7"],["8"],["9"]], >> l=len(b), >> c=[for(i=[0:l-1])each search(b[i],n)], >> d=[for(i=[0:len(c)-1])10^(l-i-1)], >> e=c*d, >> >> f=search(".",a)!=[]&&search("-",a)!=[]?e*10^-(l-(search(".",a)[0]-2)):search(".",a)!=[]&&search("-",a)==[]?e*10^-(l-(search(".",a)[0]-1)):e, >> g=search("-",a)!=[]?f*-1:f >> )[text,g]; >> >> //function to rotate a point around a vector(axis) with angle theta >> >> function q(vector=[1,0,0],point=[0,5,0],theta=0)= >> >> let(t=theta, >> v=vector/norm(vector), >> p=[cos(t/2),v*sin(t/2)], >> p1=[p.x,-p.y], >> q=[0,len(point)==2?[point.x,point.y,0]:point], >> pq=[p.x*q.x-p.y*q.y,p.x*q.y+p.y*q.x+cross(p.y,q.y)], >> pqp1=[pq.x*p1.x-pq.y*p1.y,pq.x*p1.y+pq.y*p1.x+cross(pq.y,p1.y)], >> transformation=pqp1.y >> ) >> >> transformation >> ; >> // function is input to another function q_rot >> function qmr1(s,r,pl,n=0)= n==len(s)?pl: >> qmr1(s,r, >> let( >> v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1], >> r1=r[n]==undef?0:r[n]) >> [for(p=pl)q(v1,p,r1)],n+1); >> //function is input to another function q_rot >> function qmr2(s,r,pl,n=0)= n==len(s)?pl:qmr2(s,r,let( >> v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1], >> r1=r[n]==undef?0:r[n]) >> [for(i=[0:len(pl)-1])[for(p=pl[i])q(v1,p,r1)]],n+1); >> >> //function to rotate a group of points "pl" around a series of axis with >> defined angles e.g q_rot(s=["z20","x40","y80"],pl=[[2,0],[10,2]])=> will >> rotate the line first around z axis by 20 deg then around x axis by 40 >> degrees and then around y axis by 80 degrees. >> function q_rot(s,pl)= >> is_num(pl[0][0])?qmr1([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl):qmr2([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl); >> >> // sequence of rotation defined by r1 >> r1=["x30","y40","z100"]; >> vz=[0,0,1]; >> v1=q_rot(r1,[vz])[0]; >> >> v2=cross(vz,v1); >> theta=acos(vz*v1/norm(v1)); >> >> // equivalent rotation axis and angle of rotation w.r.t vector vz >> echo(v2,theta); >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
KT
Kevin Toppenberg
Sun, Mar 26, 2023 11:04 PM

Wow!  I have been using OpenSCAD for a few years now, and didn't realize
this other way using Rotate.

Here is some documentation for it:
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#rotate

Thanks all!

KT

On Sun, Mar 26, 2023 at 7:59 AM Guenther Sohler guenther.sohler@gmail.com
wrote:

Hi all together

I believe, that openscad can YET already  rotate an object around an
arbitrary axis!

for(angle=[0,120,240])
{
rotate(v=[1,1,1],a=angle)
cylinder(r=1,h=10);
}

this turns an cylinder showing up into 3 different directions showing up,
front,  and right.
i can imagine this like rotating an  object around a cube's corner

[image: image.png]

code for that is already present in src/core/Transform.cc

On Fri, Mar 24, 2023 at 10:24 PM Kevin Toppenberg kdtop3@gmail.com
wrote:

The math is too dense for me to pick apart, but it seems that when I
worked on this in a different engine, that the math involves quaternions.

Does this solution use quaternions?

Kevin

On Wed, Mar 22, 2023 at 10:45 PM Sanjeev Prabhakar <
sprabhakar2006@gmail.com> wrote:

I would propose if a module can be developed to rotate an object around
an axis.

e.g. for any sequence of rotations an equivalent axis and angle can be
found from the functions written below

function cvar(a)=
let(
text=a[0],

b=search("-",a)!=[]&&search(".",a)!=[]?[for(i=[2:len(a)-1])if(a[i]!=".")a[i]]:
search("-",a)!=[]&&search(".",a)==[]?[for(i=[2:len(a)-1])a[i]]:

search("-",a)==[]&&search(".",a)!=[]?[for(i=[1:len(a)-1])if(a[i]!=".")a[i]]:[for(i=[1:len(a)-1])a[i]],
n=[["0"],["1"],["2"],["3"],["4"],["5"],["6"],["7"],["8"],["9"]],
l=len(b),
c=[for(i=[0:l-1])each search(b[i],n)],
d=[for(i=[0:len(c)-1])10^(l-i-1)],
e=c*d,

f=search(".",a)!=[]&&search("-",a)!=[]?e10^-(l-(search(".",a)[0]-2)):search(".",a)!=[]&&search("-",a)==[]?e10^-(l-(search(".",a)[0]-1)):e,
g=search("-",a)!=[]?f*-1:f
)[text,g];

//function to rotate a point around a vector(axis) with angle theta

function q(vector=[1,0,0],point=[0,5,0],theta=0)=

let(t=theta,
v=vector/norm(vector),
p=[cos(t/2),vsin(t/2)],
p1=[p.x,-p.y],
q=[0,len(point)==2?[point.x,point.y,0]:point],
pq=[p.x
q.x-p.yq.y,p.xq.y+p.yq.x+cross(p.y,q.y)],
pqp1=[pq.x
p1.x-pq.yp1.y,pq.xp1.y+pq.y*p1.x+cross(pq.y,p1.y)],
transformation=pqp1.y
)

transformation
;
// function is input to another function q_rot
function qmr1(s,r,pl,n=0)= n==len(s)?pl:
qmr1(s,r,
let(
v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1],
r1=r[n]==undef?0:r[n])
[for(p=pl)q(v1,p,r1)],n+1);
//function is input to another function q_rot
function qmr2(s,r,pl,n=0)= n==len(s)?pl:qmr2(s,r,let(
v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1],
r1=r[n]==undef?0:r[n])
[for(i=[0:len(pl)-1])[for(p=pl[i])q(v1,p,r1)]],n+1);

//function to rotate a group of points "pl" around a series of axis with
defined angles e.g q_rot(s=["z20","x40","y80"],pl=[[2,0],[10,2]])=> will
rotate the line first around z axis by 20 deg then around x axis by 40
degrees and then around y axis by 80 degrees.
function q_rot(s,pl)=
is_num(pl[0][0])?qmr1([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl):qmr2([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl);

// sequence of rotation defined by r1
r1=["x30","y40","z100"];
vz=[0,0,1];
v1=q_rot(r1,[vz])[0];

v2=cross(vz,v1);
theta=acos(vz*v1/norm(v1));

// equivalent rotation axis and angle of rotation w.r.t vector vz
echo(v2,theta);


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Wow! I have been using OpenSCAD for a few years now, and didn't realize this other way using Rotate. Here is some documentation for it: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#rotate Thanks all! KT On Sun, Mar 26, 2023 at 7:59 AM Guenther Sohler <guenther.sohler@gmail.com> wrote: > Hi all together > > I believe, that openscad can YET already rotate an object around an > arbitrary axis! > > for(angle=[0,120,240]) > { > rotate(v=[1,1,1],a=angle) > cylinder(r=1,h=10); > } > > this turns an cylinder showing up into 3 different directions showing up, > front, and right. > i can imagine this like rotating an object around a cube's corner > > [image: image.png] > > > code for that is already present in src/core/Transform.cc > > > > > > On Fri, Mar 24, 2023 at 10:24 PM Kevin Toppenberg <kdtop3@gmail.com> > wrote: > >> The math is too dense for me to pick apart, but it seems that when I >> worked on this in a different engine, that the math involves quaternions. >> >> Does this solution use quaternions? >> >> Kevin >> >> On Wed, Mar 22, 2023 at 10:45 PM Sanjeev Prabhakar < >> sprabhakar2006@gmail.com> wrote: >> >>> I would propose if a module can be developed to rotate an object around >>> an axis. >>> >>> e.g. for any sequence of rotations an equivalent axis and angle can be >>> found from the functions written below >>> >>> function cvar(a)= >>> let( >>> text=a[0], >>> >>> b=search("-",a)!=[]&&search(".",a)!=[]?[for(i=[2:len(a)-1])if(a[i]!=".")a[i]]: >>> search("-",a)!=[]&&search(".",a)==[]?[for(i=[2:len(a)-1])a[i]]: >>> >>> search("-",a)==[]&&search(".",a)!=[]?[for(i=[1:len(a)-1])if(a[i]!=".")a[i]]:[for(i=[1:len(a)-1])a[i]], >>> n=[["0"],["1"],["2"],["3"],["4"],["5"],["6"],["7"],["8"],["9"]], >>> l=len(b), >>> c=[for(i=[0:l-1])each search(b[i],n)], >>> d=[for(i=[0:len(c)-1])10^(l-i-1)], >>> e=c*d, >>> >>> f=search(".",a)!=[]&&search("-",a)!=[]?e*10^-(l-(search(".",a)[0]-2)):search(".",a)!=[]&&search("-",a)==[]?e*10^-(l-(search(".",a)[0]-1)):e, >>> g=search("-",a)!=[]?f*-1:f >>> )[text,g]; >>> >>> //function to rotate a point around a vector(axis) with angle theta >>> >>> function q(vector=[1,0,0],point=[0,5,0],theta=0)= >>> >>> let(t=theta, >>> v=vector/norm(vector), >>> p=[cos(t/2),v*sin(t/2)], >>> p1=[p.x,-p.y], >>> q=[0,len(point)==2?[point.x,point.y,0]:point], >>> pq=[p.x*q.x-p.y*q.y,p.x*q.y+p.y*q.x+cross(p.y,q.y)], >>> pqp1=[pq.x*p1.x-pq.y*p1.y,pq.x*p1.y+pq.y*p1.x+cross(pq.y,p1.y)], >>> transformation=pqp1.y >>> ) >>> >>> transformation >>> ; >>> // function is input to another function q_rot >>> function qmr1(s,r,pl,n=0)= n==len(s)?pl: >>> qmr1(s,r, >>> let( >>> v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1], >>> r1=r[n]==undef?0:r[n]) >>> [for(p=pl)q(v1,p,r1)],n+1); >>> //function is input to another function q_rot >>> function qmr2(s,r,pl,n=0)= n==len(s)?pl:qmr2(s,r,let( >>> v1=s[n]=="x"?[1,0,0]:s[n]=="y"?[0,1,0]:[0,0,1], >>> r1=r[n]==undef?0:r[n]) >>> [for(i=[0:len(pl)-1])[for(p=pl[i])q(v1,p,r1)]],n+1); >>> >>> //function to rotate a group of points "pl" around a series of axis with >>> defined angles e.g q_rot(s=["z20","x40","y80"],pl=[[2,0],[10,2]])=> will >>> rotate the line first around z axis by 20 deg then around x axis by 40 >>> degrees and then around y axis by 80 degrees. >>> function q_rot(s,pl)= >>> is_num(pl[0][0])?qmr1([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl):qmr2([for(p=s)cvar(p)[0]],[for(p=s)cvar(p)[1]],pl); >>> >>> // sequence of rotation defined by r1 >>> r1=["x30","y40","z100"]; >>> vz=[0,0,1]; >>> v1=q_rot(r1,[vz])[0]; >>> >>> v2=cross(vz,v1); >>> theta=acos(vz*v1/norm(v1)); >>> >>> // equivalent rotation axis and angle of rotation w.r.t vector vz >>> echo(v2,theta); >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >>> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >