Finding smallest distance between two 3D Objects
#2
There is no such thing as getting the smallest distance between two objects in Ab3d.PowerToys library.

But if you have ModelVisual3D objects with child models, it could help you to use model iterator to the library - for example Ab3d.Utilities.ModelIterator.IterateGeometryModel3DObjects method takes a ModelVisual3D and a callback delegate - it then iterates through all GeometryModel3D objects inside ModelVisual3D and for each call the delegate. The delegate also gets the transformation of all parent objects.

This can be used to generate a List of all Point3D positions from both ModelVisual3D objects. The following code does the trick:

Code:
var allPositions = new List<Point3D>();

ModelIterator.IterateGeometryModel3DObjects(modelVisual3D, delegate(GeometryModel3D geometryModel3D, Transform3D transform3D)
{
    // This code is called for each GeometryModel3D inside modelVisual3D

    var meshGeometry3D = (MeshGeometry3D)geometryModel3D.Geometry;

    if (transform3D == null || transform3D.Value.IsIdentity)
    {
        allPositions.AddRange(meshGeometry3D.Positions);
    }
    else
    {
        var transformedPositions = new Point3D[meshGeometry3D.Positions.Count];
        meshGeometry3D.Positions.CopyTo(transformedPositions, 0); // This is faster then .ToArray()

        transform3D.Transform(transformedPositions);

        allPositions.AddRange(transformedPositions);
    }
});


Then you can compare all positions from the array of the first ModelVisual3D to the second array and calculate the smallest distance (use LengthSquared to prevent calling sqrt). If you do not have very complex objects this should be done quickly (processors are insanely fast these days).

If you have very complex objects, then you will need to find some kind of data reduction technique to prevent comparing everything with everything.

Also note that if you will only compare positions, this will not always give you the smallest distance from two objects - for example in your image the gray and blue objects have the closest point that lies on the left vertical edge of the gray object. If you want to get such results, you will need to compare distances from all positions form first object to all triangles on the second object (see internet for formula). Then do the same for the second object - compare all positions with all triangles on the first object. This is much more intensive as checking only positions.

Your problem may be also solved with some object collision algorithm - check internet for more resources about that.
Andrej Benedik
  


Messages In This Thread
RE: Finding smallest distance between two 3D Objects - by abenedik - 11-10-2017, 12:50 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)