Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotation Matrix given angle and point in X,Y,Z

Tags:

c#

math

matrix

I am doing image manipulation and I want to rotate all of the pixels in xyz space based on an angle, the origin, and an x,y, and z coordinate.

I just need to setup the proper matrix (4x4) and then I will be good from there. The Angle is in degrees, not radians and the x,y,z are all going to be from -1 to 1 (floats)

EDIT:

Ok, here is the code that I whipped up to do the rotation about a given line defined by the origin and an X, Y, Z coorinate.

        float ang = angD * (float)(Math.PI / 180);  // from degrees to radians, if needed
        //U = n*n(t) + cos(a)*(I-n*n(t)) + sin(a)*N(x).

        var u = MatrixDouble.Identity(4);  // 4x4 Identity Matrix
        u = u.Multiply(Math.Cos(ang));

        var n = new MatrixDouble(1, 4, new List<double> { x, y, z, 0 });
        var nt = n.Transpose();

        // This next part is the N(x) matrix.  The data is inputted in Column
        // first order and fills in the 4x4 matrix with the given 16 Doubles
        var nx = new MatrixDouble(4, 4, new List<double> { 0, z, -y, 0, -z, 0, x, 0, y, -x, 0, 0, 0, 0, 0, 1 });

        nx = nx.Multiply(Math.Sin(ang));

        var ret = nt.Multiply(n);
        ret[3, 3] = 1;

        u = u.Subtract(ret);

        u = ret.Add(u.Add(nx));

It's a little complicated and I'm using a custom Matrix library, but nothing up there should be too hard to implement with any functioning Matrix lib.

Phew, lots of math!

like image 335
joe_coolish Avatar asked Mar 04 '11 02:03

joe_coolish


2 Answers

The complete rotation matrices are derived and given at https://sites.google.com/site/glennmurray/Home/rotation-matrices-and-formulas.

From the paper:

5.2 The simplified matrix for rotations about the origin

Note this assumes that (u, v, w) is a direction vector for the axis of rotation and that u^2 + v^2 + w^2 = 1.

Simplified 3D matrix for rotations about the origin.

If you have a point (x, y, z) that you want to rotate, then we can obtain a function of of seven variables that yields the rotated point:

f(x, y, z, u, v, w, theta) =

Formula for rotated point.

The paper also includes matrices and formulas for rotations about an arbitrary axis (not necessarily through the origin), Java code available under the Apache license, and a link to a web app that illustrates rotations.

like image 166
Glenn Avatar answered Oct 24 '22 03:10

Glenn


Use the Matrix3D Structure (MSDN) - Represents a 4 x 4 matrix used for transformations in 3-D space

Take a look here for a tutorial: Building a 3D Engine

Essentially, matrices are built for X, Y, and Z rotations and then you can multiply the rotations in any order.

public static Matrix3D NewRotateAroundX(double radians)
{
    var matrix = new Matrix3D();
    matrix._matrix[1, 1] = Math.Cos(radians);
    matrix._matrix[1, 2] = Math.Sin(radians);
    matrix._matrix[2, 1] = -(Math.Sin(radians));
    matrix._matrix[2, 2] = Math.Cos(radians);
    return matrix;
}
public static Matrix3D NewRotateAroundY(double radians)
{
    var matrix = new Matrix3D();
    matrix._matrix[0, 0] = Math.Cos(radians);
    matrix._matrix[0, 2] = -(Math.Sin(radians));
    matrix._matrix[2, 0] = Math.Sin(radians);
    matrix._matrix[2, 2] = Math.Cos(radians);
    return matrix;
}
public static Matrix3D NewRotateAroundZ(double radians)
{
    var matrix = new Matrix3D();
    matrix._matrix[0, 0] = Math.Cos(radians);
    matrix._matrix[0, 1] = Math.Sin(radians);
    matrix._matrix[1, 0] = -(Math.Sin(radians));
    matrix._matrix[1, 1] = Math.Cos(radians);
    return matrix;
}
like image 42
NakedBrunch Avatar answered Oct 24 '22 03:10

NakedBrunch