We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › 3D rotation around arbitrary axis
Page Index Toggle Pages: 1
3D rotation around arbitrary axis (Read 1485 times)
3D rotation around arbitrary axis
Feb 20th, 2008, 9:48am
 
I'm teaching a workshop on digital fabrication and we need to figure out a way to sweep a profile along a path in 3 dimensions. To do this it is necessary to be able to rotate points around an arbitrary axis, given by a 3D vector.

Does anyone have any code that demonstrates a way to do this that even math illiterates could utilize I know this is a classic question in 3D graphics, but sadly my math skills are easily defeated.

Some references:
http://answers.google.com/answers/threadview?id=361441
http://gandalf-library.sourceforge.net/tutorial/report/node126.html
Re: 3D rotation around arbitrary axis
Reply #1 - Feb 20th, 2008, 12:07pm
 
Have you tried the rotate(angle, x, y, z) method?
Re: 3D rotation around arbitrary axis
Reply #2 - Feb 20th, 2008, 2:04pm
 
I've got some code that does that. It's part of my Vector library but hopefully should enable you to knock up a version that works.
Code:
// rotate v around _axis by ang radians.
Vector rotate(Vector v, Vector _axis,float ang)
{
//use normalised values
Vector axis=new Vector(_axis.nx(),_axis.ny(),_axis.nz());
Vector vnorm=new Vector(v.nx(),v.ny(),v.nz());
float _parallel=mDot(axis,v); //dot product
Vector parallel=mul(axis,_parallel); //multiply all elements by a value
Vector perp=sub(parallel,v); //subtract one vector from another
Vector Cross=cross(v,axis); //cross product
Vector result=add(parallel,add(mul(Cross,sin(-ang)),mul(perp,cos(-ang))));
return result;
}
Re: 3D rotation around arbitrary axis
Reply #3 - Feb 20th, 2008, 2:32pm
 
Thanks, John, we're trying to get it working now...
Re: 3D rotation around arbitrary axis
Reply #4 - Feb 20th, 2008, 2:58pm
 
I've just checked the code for one potential pitfall, in my code sub subtracts the first vector from the second, e.g. sub(a,b) = b-a
Re: 3D rotation around arbitrary axis
Reply #5 - Feb 20th, 2008, 7:12pm
 
If you're using JDK 1.5 with J3D (or if you manually dump the older J3D 1.4 into Processing libraries folders) you can do something like this:

Quote:


import javax.media.j3d.*; // for Transform3D
import javax.vecmath.*; // for everything else
Vector3f axis = new Vector3f(random(-1f,1f), random(-1f,1f), random(-1f,1f));
axis.normalize();
Vector3f loc = new Vector3f(random(-100,100), random(-100,100), random(-100,100));
float theta = random(TWO_PI);
AxisAngle4f axang = new AxisAngle4f(axis, theta);
Transform3D tran = new Transform3D();
tran.setRotation(axang);
Vector3f rotloc = new Vector3f(loc);
tran.transform(rotloc);

Re: 3D rotation around arbitrary axis
Reply #6 - Feb 21st, 2008, 6:06pm
 
here's an alternative technique that might be of use:
http://www.davebollinger.com/works/p5/curvelathe/
(has potentially wide application once you get it)
Re: 3D rotation around arbitrary axis
Reply #7 - Feb 22nd, 2008, 5:52pm
 
Dave, that example looks pretty much exactly like what I was thinking of. It would be nice to have a stand-alone function for doing the rotation, but the modelX/Y/Z hack is actually way more flexible since it allows the whole range of transformations to be used.

Thanks for posting this!
Re: 3D rotation around arbitrary axis
Reply #8 - Feb 23rd, 2008, 2:35am
 
Quote:
Thanks for posting this!


welcome, though most of the thanks should go to fry for making all the right tools available to us Smiley

Quote:
It would be nice to have a stand-alone function


you could also create your own instances of a PMatrix and approach the hack in that manner, swapping them in/out for the camera matrix (and inverse) as needed: store the camera, set camera to your matrix, perform model transforms, call modelXYZ for your vertices, update your model transform from the camera, set back the original camera transform.  sounds tedious, but it works.  (use a class to own the matrices and do the swaps - makes it cleaner)

the advantage is that you could maintain your model transform independent of the camera, so that you could more easily "grow" an animated lathe/extrude/whatever during subsequent frames, instead of building the entire thing static as that demo does.
Page Index Toggle Pages: 1