Getting a point rotation or normal from 4 vectors

Hello,

I'm trying to get a rotation of a spefic point in a 3D space using 3 or 4 coordinate/vector.

For example, I want to know the rotation of (5,5,5)

Using 4 vectors for example (0,0,0), (10,10,10), or 10,0,10, etc. (3 minimum)

Do anyone have a suggestion?

Vivienne

Hello,

How can I convert this to use three locations? (XYZ, Y up)

Vivienne

http://snipplr.com/view/44716/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    struct Vector
    {
    double x, y;
    };
     
     
    double getRotateAngle(Vector vec1, Vector vec2)
    {
    const double epsilon = 1.0e-6;
    const double PI = acos(-1.0); // 180 degree
    double angle = 0;
     
    // normalize
    Vector norVec1, norVec2;
    norVec1.x = vec1.x / sqrt(pow(vec1.x, 2) + pow(vec1.y, 2));
    norVec1.y = vec1.y / sqrt(pow(vec1.x, 2) + pow(vec1.y, 2));
    norVec2.x = vec2.x / sqrt(pow(vec2.x, 2) + pow(vec2.y, 2));
    norVec2.y = vec2.y / sqrt(pow(vec2.x, 2) + pow(vec2.y, 2));
     
    // dot product
    double dotProd = (norVec1.x * norVec2.x) + (norVec1.y * norVec2.y);
    if ( abs(dotProd - 1.0) <= epsilon )
    angle = 0;
    else if ( abs(dotProd + 1.0) <= epsilon )
    angle = PI;
    else {
    double cross = 0;
    angle = acos(dotProd);
    //cross product (clockwise or counter-clockwise)
    cross = (norVec1.x * norVec2.y) - (norVec2.x * norVec1.y);
     
    if (cross < 0) // vec1 rotate clockwise to vec2
    angle = 2 * PI - angle;
    }
     
    return angle*(180 / PI);
     
    }
Hi,

I am guessing you want to work out the centroid of your 3 or more points. If so, just take the average of each x, y, z for all the points. For example:

CentroidX = (Pt1.x + Pt2.x + pt3.x) / 3.0;

Obviously you should generalise this by using a suitable container / variables.

Edit: Not sure about more than 3 points. 3 points are always co-planar, where as more than that might not be. Not sure if that is different or not.

When I learnt vectors at Uni, we didn't use an XYZ convention, because a coord system might not coincide with the World XYZ system - it could have a different origin & rotation. So we numbered our subscripts 1,2,3 instead of x,y,z. Boost does that too, except it is 0,1,2.


With your code:

I have never managed to figure out why people call the process of calculating a unit vector "Normalising" - What do you call your normal (to a plane, circle etc) vectors?

Why do you want Y up? Z up is the normal convention.

Cross product doesn't make sense for 2d. It returns a vector that is normal to the plane that it's two argument vectors are in.

Your Scalar, Dot & Cross products, Unit Vector, Magnitude should be in functions, or better as overloaded operators for the products and adding etc.

The Cross product for 3d is a much longer formula that what you have for 2d. You can find it on wiki, but that Page is overly complicated IMO.

Values like 180.0 / PI are constant - You should make them so, preferably with C++11 constexpr and auto (otherwise const and double) make sure you include the decimal point in the 180.0 so the compiler knows it's a double - I always do that with any double literal anyway:


constexpr auto RadiansToDegree 180.0 / PI;

The Boost 3rd party library has a bunch of math constants in there somewhere.

Hope all goes well, and Happy New Year :+)

PS I am travelling today, so it may be awhile before I can reply.
Last edited on
Topic archived. No new replies allowed.