#include <Rcpp.h>
// Copyright (c) 2005-2008 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v5.6.1/Minkowski_sum_3/include/CGAL/minkowski_sum_3.h $
// $Id: minkowski_sum_3.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     :  Peter Hachenberger <hachenberger@mpi-sb.mpg.de>
#ifndef CGAL_MINKOWSKI_SUM_3_H
#define CGAL_MINKOWSKI_SUM_3_H

#include <CGAL/license/Minkowski_sum_3.h>


#include <CGAL/convex_decomposition_3.h>
#include <CGAL/Minkowski_sum_3/bipartite_nary_union_sorted_combined.h>
#include <CGAL/Is_extended_kernel.h>

/// \file minkowski_sum_3.h

namespace CGAL {

/*!
\ingroup PkgMinkowskiSum3Ref

The function `minkowski_sum_3()` computes the Minkowski sum of two
given 3D Nef polyhedra \f$ N0\f$ and \f$ N1\f$. Note that the function runs in
\f$ O(n^3m^3)\f$ time in the worst case, where \f$ n\f$ and
\f$ m\f$ are the complexities of the two input polyhedra (the complexity of
a `Nef_polyhedron_3` is the sum of its `Vertices`,
`Halfedges` and `SHalfedges`).

An input polyhedron may consist of:
<OL>
<LI>singular vertices
<LI>singular edges
<LI>singular convex facets without holes
<LI>surfaces with convex facets that have no holes.
<LI>three-dimensional features, whose coplanar facets have
common selection marks (this includes open and closed solids)
</OL>

Taking a different viewpoint, the implementation is restricted as
follows:
<OL>
<LI>The input polyhedra must be bounded (selected outer volume is ignored).
<LI>All sets of coplanar facets of a full-dimensional
feature must have the same selection mark (in case of different
selection marks, unselected is assumed).
<LI>All facets of lower-dimensional features need to be convex and
must not have holes (non-convex facets and holes are ignored).
</OL>

\post If either of the input polyhedra is non-convex, it is modified during the computation, i.e., it is decomposed into convex pieces.

\sa `CGAL::Nef_polyhedron_3<Traits>`
\sa \link CGAL::convex_decomposition_3 `CGAL::convex_decomposition_3()`\endlink

*/
template<typename Nef_polyhedron_3>
Nef_polyhedron_3
minkowski_sum_3(Nef_polyhedron_3& N0, Nef_polyhedron_3& N1)
{
  typedef typename Nef_polyhedron_3::Kernel Kernel;
  typedef typename Is_extended_kernel<Kernel>::value_type Is_extended_kernel;
  if(check_tag(Is_extended_kernel())) {
    Rcpp::Rcerr << "extended kernel is not supported" << std::endl;
    return N0;
  }

  if(N0.volumes_begin()->mark()) {
    Rcpp::Rcerr << "first parameter is an infinite point set" << std::endl;
    return N0;
  }

  if(N1.volumes_begin()->mark()) {
    Rcpp::Rcerr << "second parameter is an infinite point set" << std::endl;
    return N1;
  }


  CGAL::convex_decomposition_3<Nef_polyhedron_3>(N0);
  CGAL::convex_decomposition_3<Nef_polyhedron_3>(N1);
  CGAL_assertion(N0.is_valid());
  CGAL_assertion(N1.is_valid());

  Nef_polyhedron_3 result =
    CGAL::bipartite_nary_union_sorted_combined(N0, N1);
  CGAL_assertion(result.is_valid());
  return result;
}

} //namespace CGAL
#endif // CGAL_MINKOWSKI_SUM_3_H

