views:

84

answers:

2

I have coordinates for 4 vectors defining a quad and another one for it's normal. I am trying to get the rotation of the quad. I get good results for rotation on X and Y just using the normal, but I got stuck getting the Z, since I've used just 1 vector.

Here's my basic test using Processing and toxiclibs(Vec3D and heading methods):

import toxi.geom.*;

Vec3D[] face = {new Vec3D(1.1920928955078125e-07, 0.0, 1.4142135381698608),new Vec3D(-1.4142134189605713, 0.0, 5.3644180297851562e-07),new Vec3D(-2.384185791015625e-07, 0.0, -1.4142135381698608),new Vec3D(1.4142136573791504, 0.0, 0.0),};
Vec3D n = new Vec3D(0.0, 1.0, 0.0);

print("xy: " + degrees(n.headingXY())+"\t");
print("xz: " + degrees(n.headingXZ())+"\t");
print("yz: " + degrees(n.headingYZ())+"\n");

println("angleBetween x: " + degrees(n.angleBetween(Vec3D.X_AXIS)));
println("angleBetween y: " + degrees(n.angleBetween(Vec3D.X_AXIS)));
println("angleBetween z: " + degrees(n.angleBetween(Vec3D.X_AXIS)));

println("atan2 x: " + degrees(atan2(n.z,n.y)));
println("atan2 y: " + degrees(atan2(n.z,n.x)));
println("atan2 z: " + degrees(atan2(n.y,n.x)));

And here is the output:

xy: 90.0    xz: 0.0 yz: 90.0
angleBetween x: 90.0
angleBetween y: 90.0
angleBetween z: 90.0
atan2 x: 0.0
atan2 y: 0.0
atan2 z: 90.0

How can I get the rotation(around it's centre/normal) for Z of my quad ?

A: 

Here is the rotation matrix for the z axis

cos(theta) sin(theta) 0

-sin(theta) cos(theta) 0

0 0 1

  • your vector

the result is the rotated vector

Bass
what I need is the opposite...I have a rotated vector(4 of them and a normal actually) and I want to determine the rotation(around the centre) on the z axis of the quad/plane defined by these 4 vectors.
George Profenza
right, that's a rotation matrix, I multiply it to my vector to get the rotated vector. I'm looking for something like the reverse. I have 4 vectors(defining a quadrilateral) and a normal, can I determine the rotation the z axis from that ?
George Profenza
you can solve that if you have the old vector, by solvong three equations with only one variable
Bass
current_vector=matrix*old_vector gives three equations so you can solve that if you have the old vector, by solving three equations with only one variable
Bass
+2  A: 
walkytalky
Very well explained. Thank you! I think Vec3D's angleBetween is something like acos(firstVector.dotProduct(secondVector)). The "from rotation" will be rotation of the quad and the "to rotation" will be whatever rotation will align the quad with a plane(xz for example). What I'm trying to do is to rotate a quad with 3d dimensions so it is aligned with an axis and one dimension could be dropped/the quad could be drawn in 2d(flat, no rotations). I did using a rectangle rotated 10 degrees on X, then used the X rotation determined using atan2(z,y).
George Profenza
Then I created a rotation matrix at -10 degrees and muliplied it to the quad points. The quad was 'flat' again. Repeated the test only with the y axis and it worked. Repeated the test with the z axis and it failed. Also, when I used coordinates of a rectangle rotated 10 on x and 10 on y for the quad, then tried to remove the rotations, it failed. I imagine the major problem is finding the rotation on Z. Sorry, I'm horrible at explaining things sometimes. Let me know what I need to detail.
George Profenza
@George Z rotation should not be materially different from X and Y, so you are doing something wrong - check for trivial coding errors like that persistent `Vec3D.X_AXIS` in your question code. On the 10 x, 10 y question, note that sequences of rotations are *not commutative* - you need to pay attention to the order you apply them. I'll try to give a fuller answer later, but right now I'm off to play in the sunshine...
walkytalky
@walkytalky I've uploaded a sketch here(http://lifesine.eu/processing/RotationMatrix/RemoveRotation/). Again, let me know if the syntax confusing. I have coordinates for 4 quads and their normals: a square rotated at 10 degrees on x, one rotated at 10 on y and another one rotated at 10 on z...10/10/10/...nice and sunny...the last face/quad/square is rotated at 10 on x and 10 on y. If you press 'c' in the sketch, the squares/quads will be cycled. face_rx_10 (face with rotation x of 10 degrees) is rotated fine, so is face_ry_10. face_rz_10 fails...and if I have face_rxy also fails in my approach
George Profenza
@George Having looked at your code: (1) your "rotated around Z" quad is actually around Y -- note the normal; (2) that one won't go straight because the normal is already aligned (I think it actually flips over, so you see the quad reflected) -- if you want to orient the square to the other axes too, you need to look at the edges rather than the normal, but this brings us back to the issue of "natural" orientation; (3) you're treating the rotations as commutative, which is why your X+Y version doesn't work. See my edit from this morning for relevant info, and google "Euler Angles" for more.
walkytalky