Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.5K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Calculating the closest point to a set of lines

Posted 11 years ago

Hi,

as titled, I am trying to calculate the closest point to a set of lines in 3D

I saw here that this guy pointed out some Mathematica code.

I put it into the Mathematica Kernel and I got a very long result.. so I decided to write an algorithm by myself.

Now I would like, instead, given four known lines (each of them defined by two points), to get the closest point..

The lines are the following:

Line[] lines = new Line[]{ new Line(new Vec3(77.007f, 32.46f, 177.79f), new Vec3(139.398f, -309.133f, -116.29f)), //
new Line(new Vec3(-79.504f, 31.969f, -179.158f), new Vec3(31.886f, -367.752f, 99.796f)), //
new Line(new Vec3(-77.401f, 32.855f, 174.843f), new Vec3(25.944f, -350.134f, -129.523f)), //
new Line(new Vec3(80.781f, 32.154f, -176.942f), new Vec3(-149.572f, -326.383f, 84.57f)) };

But I have no knowledge of Mathematica, could someone help me?

2 Replies

Hi Henrik,

thanks for your code, one guy gave me this function:

void MathUtil::CalcBeamsClosestPoint(pfSeg* beams, int numBeams, pfVec3& p)
{
    float m00 = 0.0f;
    float m01 = 0.0f;
    float m02 = 0.0f;
    float m11 = 0.0f;
    float m12 = 0.0f;
    float m22 = 0.0f;
    float v0 = 0.0f;
    float v1 = 0.0f;
    float v2 = 0.0f;

    for (int i = 0; i < numBeams; i++) {
        pfSeg& beam = beams[i];
        pfVec3& dir = beam.dir;
        pfVec3& pos = beam.pos;
        const float ax = dir[0];
        const float ay = dir[1];
        const float az = dir[2];
        const float bx = pos[0];
        const float by = pos[1];
        const float bz = pos[2];

        m00 += ay * ay + az * az;
        m11 += az * az + ax * ax;
        m22 += ax * ax + ay * ay;
        m01 += -ax * ay;
        m02 += -az * ax;
        m12 += -ay * az;
        v0 += az * (az * bx - ax * bz) - ay * (ax * by - ay * bx);
        v1 += ax * (ax * by - ay * bx) - az * (ay * bz - az * by);
        v2 += ay * (ay * bz - az * by) - ax * (az * bx - ax * bz);
    }
    matrix33f m(vector3f(m00, m01, m02), vector3f(m01, m11, m12), vector3f(m02, m12, m22));
    float det = m.det();
    assert(det != 0.0f);
    vector3f pt = inverse(m) * vector3f(v0, v1, v2);
    p.set(pt.x, pt.y, pt.z);
}

it works with simple lines such as

       new Line(new Vec3(0, 0, 0), new Vec3(1, 1, 1)),

       new Line(new Vec3(2, 0, 0), new Vec3(1, 1, 1)),

but not with the ones I gave you previously (I obtain a different result).. I don't know why, I can't get anyway the theory behind his code

By the way, the formula is this one at the end http://math.stackexchange.com/a/61721/64943

I tried to wrote the algorithm, but I didn't make it

I should come to a form such as:

A x = b

but I don't have idea how to deal with sums and dot products..

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard