davideberly / GeometricTools

A collection of source code for computing in the fields of mathematics, geometry, graphics, image analysis and physics.
Boost Software License 1.0
1.14k stars 214 forks source link

segDirection should be normalize ?it looks like a bug. #65

Closed zyxia closed 1 year ago

zyxia commented 1 year ago

https://github.com/davideberly/GeometricTools/blob/4a8f9d6803ed2210cfe8bb47343d3bb468b7dd34/GTL/Mathematics/Distance/3D/DistSegment3Triangle3.h#L42C55-L42C55

davideberly commented 1 year ago

I believe the code works correctly. The GTL file DistLine3Triangle3.h has a comment on line 12

// The line is P + t * D, where D is not required to be unit length.

The following sample code shows that it does not matter that the segment direction is unnormalized.

    Triangle3<double> triangle{};
    triangle.v[0] = { 1.0, 0.0, 0.0 };
    triangle.v[1] = { 0.0, 2.0, 0.0 };
    triangle.v[2] = { 0.0, 0.0, 3.0 };

    // The segment has endpoints {4,5,-1} and {6,7,1}. The non-unit-length
    // direction vector is the difference {2,2,2}.
    Line3<double> line{};
    line.origin = { 4.0, 5.0, -1.0 };
    line.direction = { 2.0, 2.0, 2.0 };

    DCPQuery<double, Line3<double>, Triangle3<double>> query{};
    auto result = query(line, triangle);
    // result.distance = 3.7416573867739413
    // result.sqrDistance = 14.0
    // result.parameter = -1.0
    // result.barycentric[0] = 0.0
    // result.barycentric[1] = 1.0
    // result.barycentric[2] = 0.0
    // result.closest[0] = { 2.0, 3.0, -3.0 }
    // result.closest[1] = { 0.0, 2.0, 0.0 }

    Normalize(line.direction);
    result = query(line, triangle);
    // result.distance = 3.7416573867739413
    // result.sqrDistance = 14.0
    // result.parameter = -3.4641016151377539
    // result.barycentric[0] = 0.0
    // result.barycentric[1] = 1.0
    // result.barycentric[2] = 0.0
    // result.closest[0] = { 2.0, 3.0, -3.0 }
    // result.closest[1] = { 0.0, 2.0, 0.0 }