Problem: Test if a 3d set of points fits into a container
Testing if a 3d point cloud can be loaded into a rectangular container is a suprisingly complex task. The best known exact algorithm has O(n3) complexity, which is of little practical use. G# provides a very efficient approximative solution for the 3d container loading problem. It bases on the empirical observation that the principal axes of the mass moment of inertia establish a pretty good basis for a somewhat minimal bounding box.
Note that an exact solution can be found in O(n·log(n)) time for the 2d container loading problem.
Solution using G#
- public static void LoadingExample()
- {
- // Generate a "container" with dimensions 12 x 2.3 x 2.3
- // The orientation in space does not matter, so the
- // orientation along the global axes is just as good.
- CoordinateSystem.Global());
- // Generate an "item" to load into the container:
- // Create some points and transform them to somewhere in space
- PointSet item = SamplePoints.RandomInSphere(1000, 1);
- item.TransformCoordinates(CoordinateSystem.Global(),
- CoordinateSystem.RandomCoordinateSystem());
- // Now the item is a point cloud somewhere in space. We will now
- // find a pretty tight bounding box for the item.
- // Instantiate a convex hull
- // Compute the convex hull for the item to load:
- convexHullOfItem.InitialPoints = item;
- convexHullOfItem.ComputeHull();
- // Get the bounding box
- BoundingBox boundingBoxOfItem =
- convexHullOfItem.PrincipalAxesBoundingBox();
- // Some properties of the bounding box:
- Console.WriteLine("BB length: " + boundingBoxOfItem.Length);
- Console.WriteLine("BB width : " + boundingBoxOfItem.Width);
- Console.WriteLine("BB height: " + boundingBoxOfItem.Height);
- Console.WriteLine("BB volume: " + boundingBoxOfItem.Volume());
- // Test if the item fits into the container:
- Console.WriteLine(boundingBoxOfItem.FitsInto(container));
- Console.ReadLine();
- // Visualize: ----------------------------------------------------
- // Make a scene
- // Make a layer for the triangles
- // Make a layer for the bounding box of the item
- // Add the surface of the convex hull of the item
- sc.Add(convexHullOfItem.Triangles, itemLayer);
- // Add the surface of the bounding of the item
- sc.Add(boundingBoxOfItem.GetSurface(), itembBoxLayer);
- // Output the scene as dxf
- sc.WriteDxf(@"c:\hull.dxf");
- }
Output
Here's another example:
- PointSet item = SamplePoints.RandomInCube(500, 11, 2.2, 1.5);



