View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
3D Rotation about Shifted Axis

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0 | 6 ratings Rate this file 42 Downloads (last 30 days) File Size: 2.49 KB File ID: #30864 Version: 1.3

3D Rotation about Shifted Axis


Matt J (view profile)


26 Mar 2011 (Updated )

Computes/applies rotation about arbitrary 3D line.

| Watch this File

File Information

Generates the roto-translation matrix for the rotation around an arbitrary line in 3D. The line need not pass through the origin. Optionally, also, applies this transformation to a list of 3D coordinates.
   u, x0: 3D vectors specifying the line in parametric form x(t)=x0+t*u
          Default for x0 is [0,0,0] corresponding to pure rotation (no shift).
          If x0=[] is passed as input, this is also equivalent to passing x0=[0,0,0].
   deg: The counter-clockwise rotation about the line in degrees. Counter-clockwise is defined using the
        right hand rule in reference to the direction of u.
  M: A 4x4 affine transformation matrix representing
     the roto-translation. Namely, M will have the form
                  M=[R,t;0 0 0 1]
      where R is a 3x3 rotation and t is a 3x1 translation vector.
Same as Syntax 1 except that R and t are returned as separate arguments.
This syntax requires 4 input arguments be specified,
    [XYZnew, R, t] = AxelRot(XYZold, deg, u, x0)
where the columns of the 3xN matrix XYZold specify a set of N point coordinates in 3D space. The output XYZnew is the transformation of the columns of XYZold, i.e., the original coordinates rotated appropriately about the axis. All other input/output arguments have the same meanings as before.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (33)
24 Aug 2016 Fritz

Fritz (view profile)

29 Dec 2015 Matt J

Matt J (view profile)


Ultimately, the problem you are having is not directly within the scope of what is offered here. This FEX submission is designed for coordinate rotation, rather than image rotation. Except that, of course, you could use AxelRot to generate input for maketform() which could then be used in imtransform(), tformarray(), etc...

The negative values you are getting from imrotate() is because cubic interpolation is an approximation of sinc interpolation. It therefore need not inherit positivity from the interpolated data points. It is typically a trade-off you have to make in order to interpolate with C1 smoothness.

An alternative would be to use raised cosine interpolation, which is both C1-continuous and positivity-preserving. However, it is an unconventional interpolation method and I can't be sure the rotated image would look the way you hope. Moreover, to implement this, you would have to use some advanced features of imtransfrom(). In particular, you would need to specify your own customized interpolation method using makeresampler().

Comment only
28 Dec 2015 Michael Young

My issue has been, for some reason, I am getting some negative values from an entirely positive matrix when using bicubic interpolation for imrotate(). I'm not sure why this is occuring, and I don't want to degrade the results by relying on bilinear.

Comment only
28 Dec 2015 Matt J

Matt J (view profile)

It is not clear to me what distinction you draw between an NxN matrix and and NxN image.

Comment only
27 Dec 2015 Michael Young

I was under the impression you needed to upload an image to use imrotate(), instead of merely rotating a matrix. Is this incorrect? Thanks for the recommendation regardless!

Comment only
27 Dec 2015 Matt J

Matt J (view profile)


Sounds like what you really need is imrotate() from the Image Processing Toolbox.

Comment only
26 Dec 2015 Michael Young

Matt J:
I am working on a problem that I believe can utilize your technique, though I am not sure how exactly to do so. I wish to rotate an n x n matrix by x degrees, and then determine the appropriate values of the rotated matrix in regards to the original matrix. Visually, it will be a matrix overlapping with that same matrix rotated, and render the corresponding values on the original matrix from the rotated matrix. Thank you for any help you can provide!

Comment only
08 Oct 2015 Matt J

Matt J (view profile)

Hi Evangelos,

You are correct. The purpose of AxelRot is to rotate about an axis that doesn't necessarily pass through the origin. As I think you'll find in the help doc, the inputs x0 and u define the desired rotation axis through the parametric equation x(t)=x0+t*u where x(t) is a point on the axis parametrized by t.

Comment only
08 Oct 2015 Evangelos Mazomenos

Hi Matt,thanks for replying to my comment. After your reply and a little bit of more coding I think I know what is happening. Please verify my thoughts and correct me if I am wrong.

The AxelRot function you coded is function that rotates a point (or set of points) by "deg" about an axis. The axis is defined originally by "u" but the axis (of rotation) might be also shifted by "x0" so the term translation in your code comments refers to the shifting of the axis. My initial understanding of translation and the one that is implemented with the alternative code i wrote:

Mtrans = mkaff(eye(3), x0);
Mroto = mkaff(R3d(deg,u));
M = Mroto * Mtrans (considering translation first)

is that a point (or set of points) is initially translate by x0 (the point itself not the rotation axis) and then rotated by "deg" around u. This is also what MATLAB's inbuild function makehgttform implements. Basically this is independent transformations of points in 3D space (rotations, translations, scalings, etc ...)group together in a transformation matrix. On the other hand your intention is to provide a code only for rotation about an axis which might be shifted from his unit vector "u".

To support my claim I tried to rotate a point ([1 1 1]) for deg=360 and x0=[0 1 0] about axis u=[1 0 0].

With the initial AxelRot code the point since it is only rotated around an axis (albeit a shifted one by x0) for 360 it will return to its original location of [1 1 1]. On the other hand with the code (alternative code) in which x0 corresponds to a translation of the point in 3D the point will move to [1 2 1] as the result of the helical motion that corresponds to a rotation about an axis and a translation.

So I think both approaches (and codes) are correct but they implement a different operation. I got confused because I thought that "translation" in your comments corresponded to point translation and not axis shift. I must admit that you use the term shift in your code and I should be more careful.

Anyway let me know if you agree. Thanks for your time

Comment only
07 Oct 2015 Matt J

Matt J (view profile)

Hi Evangelos, and thanks for your feedback. There is no specific literature reference that the code was built on. I derived the expressions on my own. However, I imagine most books on robotics, for example, would teach the principles behind deriving all the M matrices.

The alternative code that you have presented gives the wrong result, so I'm not sure what motivates it. You can see this by testing it with simple input data such as

x0=[0 1 0].'; u=[1 0 0 ].'; deg=90;

We know that the transformation specified by these values for x0,u,deg should rotate the origin to the point [0;1;-1]. However, your code maps the origin to [0;0;1].

One thing I will mention though is that the choice of the value for AxisShift is not unique. I could also have chosen AxisShift=x0 or any other vector on the desired axis, u. You will see by testing them directly that all such choices produce the same result. However, x0-(x0.'*u).*u is the shortest vector from the origin to the axis u and so this choice is somehow "cleaner" in my mind.

Hope that helps.

Comment only
07 Oct 2015 Evangelos Mazomenos

Hi Matt J.

The code is really good thanks for sharing. Is it possible to provide a reference and briefly explain the way you construct the transformation matrix in your code and particularly the translation part. This is the part I do not understand why needs to be done:

AxisShift=x0-(x0.'*u).*u; %%
Mshift=mkaff(eye(3),-AxisShift); Mroto=mkaff(R3d(deg,u)); %% I get this

Why is the AxisShift variable calculated and why Mshift and M are defined like this??

As far I know the 4x4 transformation matrix M, for rotation about u for deg degrees and translation x0 (in 3D) should be defined as(given you code):

Mtrans = mkaff(eye(3), x0);
Mroto = mkaff(R3d(deg,u));
M = Mroto * Mtrans (considering translation first)

This is what matlab gives with the makehgttform command and I do not understand why you do it in a different way.

Thanks for your time.

Comment only
14 Jul 2015 Matt J

Matt J (view profile)

Thanks, Karl. Yes, the code is from the days before I was better educated about backslash, but for such a small matrix, it is unlikely to matter.

Comment only
13 Jul 2015 Karl

Karl (view profile)

Hi Matt,

Very helpful script!
Matlab suggests replacing 'M=inv(Mshift)*Mroto*Mshift' with the likes of 'M=(Mshift\Mroto)*Mshift' for speed and accuracy (it will also remove the warning from the editor and code analysis).

Comment only
13 Jul 2015 Karl

Karl (view profile)

12 Aug 2014 Matt J

Matt J (view profile)

Not sure I understand your question. You can rototranslate one or more 3D column vectors using SYNTAX 3 of the routine.

Comment only
12 Aug 2014 Michael

How could you modify this code to rotate/translate a 3D velocity vector instead of just a coordinate frame?

Comment only
31 Mar 2014 Sagar

Sagar (view profile)

23 Jul 2012 Alan Jennings

Alan Jennings (view profile)

Good work. I'd recomend adding the copyright restrictions to the source code to keep them together.

13 Jun 2012 Matt J

Matt J (view profile)

Hi Florian,

I don't/can't know why you're getting a different result from your CAD software, but I'm pretty convinced that P3_CATIA is incorrect.

One check you can do is to find the perpendicular projection of the different points onto the rotation axis, as with the code below. They should all agree, but I get a significant discrepancy for prj_CATIA

axproj=@(z) u*(u\(z-x0))+x0;


Once you've computed the perpendicular projection it's also easy to verify the angular separation between P3_old and P3new_1, as with the code below. I get 5 degrees to very high precision



Comment only
13 Jun 2012 Florian

Hi Matt,

thanks for the code. But I've a little problem.

I tried to use your function with this points

P1 = [33.319 -862.139 420.373]
P2 = [550.303 -721.267 667.915]

P3 = [113 -870.591 483.66]

%% Variablendeklaration
deg = -5;
k = [P1(1,1) P2(1,1)
P1(1,2) P2(1,2)
P1(1,3) P2(1,3) ];
x0 = [P1(1,1);P1(1,2);P1(1,3)];
x1 = [P2(1,1);P2(1,2);P3(1,3)];
u = x1 - x0;

%% test
P3_old = [P3(1,1);P3(1,2);P3(1,3)]


[P3new_1, R, t] = AxelRot(P3_old, deg, u, x0)

the result is

P3new_1 =


I'm making the same rotation in our CAD software (CATIA)-> I'll get this result


Do you have any idea about the differences?

Comment only
12 Jun 2012 Matt J

Matt J (view profile)

@insa: I don't know what you mean exactly by a "circular node". Do you mean a single point in 3D space? Is each "node" defined simply by an (x,y,z) coordinate triple which you want to rotate to a different location? If so, this is a straightforward application of SYNTAX 3 of the tool and you shouldn't have any problem.

Comment only
12 Jun 2012 insa lyon

Dear Mr.Matt J,
simply, we have a circular nodes lying on xy plane, i want to rotate theses nodes with a specific angle and get the rotated circular points and their coordinates ??what do you think?

Comment only
08 Jun 2012 Matt J

Matt J (view profile)

Hmmm. No, I guess not. It's sounding less and less like a rigid transformation.

Comment only
08 Jun 2012 insa lyon

Dear Mr.Matt J,Thank you for your answer.
Actually, i have a series of '3D circular' coordinates( Nodes) lying on a straight line , this is the initial form.What i need is to propagate every single point(node)through the curved centerline points.
So, the final form i need will be a series of 3D circular coordinates( Nodes)lying on a curved centerline points , these 3D circular coordinates have to be perpendicular on this curved line ,so you think that i can transform those coordinates using SYNTAX 3?
My kind regards,

Comment only
08 Jun 2012 Matt J

Matt J (view profile)

@insa: your question is unclear to me, I'm afraid. You mean the data you have is a series of 3D coordinates lying on some kind of spiral space curve? If so, you can transform those coordinates using SYNTAX 3.

Comment only
08 Jun 2012 insa lyon

i have the axis of a Cylinder straight,and the curved axis along-that i want to calculate the new curved cylinder withe roto translation?
Do you thing that i can do it ??

06 Mar 2012 Melissa  
27 Feb 2012 Matt J

Matt J (view profile)

@Melissa - finding the axis of your cylinder, if you don't already know it, is a data fitting problem. You should probably pose that question in the Newsgroup, since it doesn't concern this FEX submission directly.

Comment only
27 Feb 2012 Melissa

How do I find a tilted axis so that I can use this?

Comment only
09 Aug 2011 Matt J

Matt J (view profile)

@Melissa - You can use SYNTAX 3 to undo the tilt in your cylinder, assuming you already know its tilted z-axis, Ztilt. The cylinder becomes untilted when you rotate it about the rotation axis

u=cross(Ztilt/norm(Ztilt), [0 0 1])

by a rotation angle of deg=asind(u).

Comment only
09 Aug 2011 Melissa

Can I use this to take a set of 3d cylinder cartesian coordinates and make a profile of polar coordinates by each slice of the cylinder?
Check this: for more details of what I need.

Comment only
07 Jun 2011 Matt J

Matt J (view profile)

@pink, you might want to explain to me what you're trying to do. I can't tell what elaboration of the help documentation you're looking for.

Comment only
07 Jun 2011 pink

pink (view profile)

how to use?

Comment only
30 Mar 2011 1.1

Clarified description and help doc only. No new code.

05 Jul 2011 1.3

Added option to transform a set of coordinates.

Contact us