Rojan Gh.
Rojan Gh.

Reputation: 1540

How to calculate Normals Vector based on surface angles using javascript?

I have these 3 (a, b and c) angles for a surface shown in the picture and I need to find the (x,y,z) for these two normal vectors.

One of the vectors is laying over the surface and the other one is perpendicular to it.

And vectors are pointing outward the origin (0,0,0).

Can anyone help please?

enter image description here

Upvotes: 1

Views: 2841

Answers (2)

Cimbali
Cimbali

Reputation: 11395

Since you only use basic rotations, you have the straightforward formulas for each rotation :

wikipedia's formulas for basic rotations

The rotation you want to operate is the multiplication of these matrices. The order is important, but because you get your angles from a gyroscopic device, I will assume a conventional order of : roll (around x axis, a or gamma in this post's notations), pitch (around y axis, b or beta), and finally yaw (around z-axis, c or alpha).

Here is the general explicit (and pretty ugly) matrix formulation for you, showing in which order the three rotations have to be performed (from http://planning.cs.uiuc.edu/node102.html).

direct formulation of R

You then multiply your vectors by this matrix R (with your vector as a column vector on the right-hand side), and you're done.

So you javascript code for the matrix is :

var ca = Math.cos(a), sa = Math.sin(a);
var cb = Math.cos(b), sb = Math.sin(b);
var cc = Math.cos(c), sc = Math.sin(c);

var mat = [[cc*cb, cc*sb*sa-sc*ca, cc*sb*ca+sc*sa],
           [sc*cb, sc*sb*sa+cc*ca, sc*sb*ca-cc*sa],
           [-sb,   cb*sa,          cb*ca]];

// x is any vector you want to rotate
var x = [/* value on x axis */, /* value on y axis */, /* value on z axis */];

// this is the value of x once it is rotated
var rot_x = [mat[0][0]*x[0]+mat[0][1]*x[1]+mat[0][2]*x[2],
             mat[1][0]*x[0]+mat[1][1]*x[1]+mat[1][2]*x[2],
             mat[2][0]*x[0]+mat[2][1]*x[1]+mat[2][2]*x[2]];

You can see from the general formula that if you rotate the vectors of the standard basis, you get the first column of the matrix for [1,0,0], the second column for [0,1,0] and the third column for [0,0,1], which is simpler. However the above formula is general and always works.

Upvotes: 2

Rewind
Rewind

Reputation: 2814

Answer:

[x1] = [cos(b)cos(c)]
[y1] = [sin(c)cos(b)]
[z1] = [-sin(b)]


[x2] = [sin(b)cos(a)cos(c) + sin(a)sin(c)]
[y2] = [sin(b)sin(c)cos(a) - sin(a)cos(c)]
[z2] = [cos(a)cos(b)]

where a=rotation around x-axis, b=rotation around y-axis and c=rotation around z-axis.

Extra Credit Answer:

If (x3,y3,z3) is another vector ON the plane perpendicular to (x1,y1,z1), then it is as follows:

[x3] = [sin(a)sin(b)cos(c) - sin(c)cos(a)]
[y3] = [sin(a)sin(b)sin(c) + cos(a)cos(c)]
[z3] = [sin(a)cos(b)]

Explination:

(I am using UK definitions of matrices in my explination, so US people remember you typically do it back to front. It does not effect the result, just the explination.)

I am assuming your plane starts flat on the table, it is the x-y plane. The z-axis points out of the page. You rotate a-degrees around the x-axis, b-degrees around the y-axis and c-degrees around the z-axis.

So think of your (x1,y1,z1) vector as the vector (1,0,0) rotated a,b,c in order. Similarly (x2,y2,z2) is the vector (0,0,1) rotated through a,b,c in order. (If you wanted another vector on the plane itself perpendicular to (x1,y1,z1) just do the rotations on (0,1,0)).

Here are the matrices that rotates around the x-axis, y-axis and z-axis in order. Where c = cos of the angle and s = sin of the angle

[1 0 0]
[0 c -s]
[0 s c]

[c 0 s]
[0 1 0]
[-s 0 c]

[c -s 0]
[s c 0]
[0 0 1]

So all we have to do is apply them one after the other (so US people do this in reverse order)

Final matrix = [cos(c) -sin(c) 0] [cos(b)  0   sin(b)] [ 1      0       0   ]
               [sin(c) cos(c)  0] [   0    1     0   ] [ 0    cos(a) -sin(a)]
               [0       0      1] [-sin(b) 0   cos(b)] [ 0    sin(a)  cos(a)]

             = [cos(c) -sin(c) 0] [cos(b)   (sin(a)sin(b))   (sin(b)cos(a))] 
               [sin(c) cos(c)  0] [  0           cos(a)         -sin(a)    ]
               [0       0      1] [-sin(b)  (sin(a)cos(b))   (cos(a)cos(b))]

             = [cos(b)cos(c)       (sin(a)sin(b)cos(c) - sin(c)cos(a))     (sin(b)cos(a)cos(c) + sin(a)sin(c))]
               [(sin(c)cos(b))     (sin(a)sin(b)sin(c) + cos(a)cos(c))     (sin(b)sin(c)cos(a) - sin(a)cos(c))]
               [-sin(b)                   (sin(a)cos(b))              (cos(a)cos(b))]

So then you multiply this matix by your vectors, which gives the answers above.

Upvotes: 1

Related Questions