Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Merging entities that do not have any overlap in CGAL requires creating a new entity that encompasses both of the original entities. This can be done by defining a new bounding box or convex hull that includes both entities.

Here are the general steps to follow:

  1. Define the two entities that need to be merged, whether they are two polygons, two sets of points, or some other type of entity.
  2. Determine the extent of each entity in all three dimensions, whether it is a bounding box or a convex hull.
  3. Use CGAL functions to compute the union of the two extents to create a new encompassing extent that includes both entities.
  4. Compute the set of points or vertices that lie within the new extent.
  5. Use these points or vertices to create a new entity, whether it is a new polygon or a new set of points.

Here is some C++ code that demonstrates the creation of an encompassing bounding box for two sets of points:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
#include <CGAL/OBB_tree_3.h>
#include <CGAL/point_generators_3.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef K::Vector_3 Vector_3;

int main()
{
    std::vector<Point_3> points1, points2;
    // populate points1 and points2 with the sets of points

    // compute the extents of each set of points
    CGAL::Min_max_diameter_d::Point_with_id<Point_3> min_max1 = CGAL::min_max_diameter(points1.begin(), points1.end());
    CGAL::Min_max_diameter_d::Point_with_id<Point_3> min_max2 = CGAL::min_max_diameter(points2.begin(), points2.end());

    // compute the union of the two extents to create a new encompassing extent
    Point_3 lower_left(std::min(min_max1.first.x(), min_max2.first.x()),
                       std::min(min_max1.first.y(), min_max2.first.y()),
                       std::min(min_max1.first.z(), min_max2.first.z()));
    Point_3 upper_right(std::max(min_max1.second.x(), min_max2.second.x()),
                        std::max(min_max1.second.y(), min_max2.second.y()),
                        std::max(min_max1.second.z(), min_max2.second.z()));

    // compute the set of points that lie within the new extent
    std::vector<Point_3> all_points;
    all_points.reserve(points1.size() + points2.size());
    all_points.insert(all_points.end(), points1.begin(), points1.end());
    all_points.insert(all_points.end(), points2.begin(), points2.end());
    CGAL::OBB_tree_3<K> obb(all_points.begin(), all_points.end());
    std::vector<Point_3> new_points;
    obb.all_points(new_points);

    // Create a new CGAL object that encompasses both original sets of points.
    CGAL::convex_hull_3(new_points.begin(), new_points.end(), hull);
}