Skip to content

Commit

Permalink
Added 2d minkowski
Browse files Browse the repository at this point in the history
  • Loading branch information
arnholm committed May 21, 2018
1 parent 848b486 commit a45505e
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 3 deletions.
29 changes: 29 additions & 0 deletions xcsg/clipper_boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ bool clipper_boolean::compute(std::shared_ptr<clipper_profile> b, ClipperLib::Cl
return success;
}

bool clipper_boolean::minkowski_sum(std::shared_ptr<clipper_profile> a, std::shared_ptr<clipper_profile> b_brush )
{
ClipperLib::Clipper clipper;
ClipperLib::Paths& a_paths = a->paths();
ClipperLib::Paths& b_paths = b_brush->paths();
if(b_paths.size() != 1) {
throw std::logic_error("clipper_boolean::minkowski_sum, 'b' parameter must contain exactly one path");
}

boost::posix_time::ptime p1 = boost::posix_time::microsec_clock::universal_time();

ClipperLib::Path& pattern = b_paths[0];
std::shared_ptr<clipper_profile> result(new clipper_profile);
bool pathIsClosed = true;
ClipperLib::MinkowskiSum(pattern,a_paths,result->paths(),pathIsClosed);
bool success = result->paths().size() > 0;
if(success) {
ClipperLib::CleanPolygons(result->paths());
m_profile = result;
}

boost::posix_time::time_duration ptime_diff = boost::posix_time::microsec_clock::universal_time() - p1;
double elapsed_sec = 0.001*ptime_diff.total_milliseconds();

boolean_timer::singleton().add_elapsed(elapsed_sec);

return success;
}

std::shared_ptr<clipper_profile> clipper_boolean::profile()
{
return m_profile;
Expand Down
8 changes: 6 additions & 2 deletions xcsg/clipper_boolean.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// Public License version 2 or 3 (at your option) as published by the
// Free Software Foundation and appearing in the files LICENSE.GPL2
// and LICENSE.GPL3 included in the packaging of this file.
//
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE. ALL COPIES OF THIS FILE MUST INCLUDE THIS LICENSE.
// EndLicense:

#ifndef CLIPPER_BOOLEAN_H
#define CLIPPER_BOOLEAN_H

Expand All @@ -30,6 +30,10 @@ class clipper_boolean {
// return the current profile
std::shared_ptr<clipper_profile> profile();

// compute minkowski sum of a and b_brush
// a is assumed to be the main object and "b_brush" is "brushed" along the a path
bool minkowski_sum(std::shared_ptr<clipper_profile> a, std::shared_ptr<clipper_profile> b_brush );

private:
std::shared_ptr<clipper_profile> m_profile;
};
Expand Down
6 changes: 6 additions & 0 deletions xcsg/xcsg.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@
<Unit filename="xlinear_extrude.h">
<Option virtualFolder="boolean/3d/" />
</Unit>
<Unit filename="xminkowski2d.cpp">
<Option virtualFolder="boolean/2d/" />
</Unit>
<Unit filename="xminkowski2d.h">
<Option virtualFolder="boolean/2d/" />
</Unit>
<Unit filename="xminkowski3d.cpp">
<Option virtualFolder="boolean/3d/" />
</Unit>
Expand Down
3 changes: 3 additions & 0 deletions xcsg/xcsg_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "xhull2d.h"
#include "xfill2d.h"
#include "xoffset2d.h"
#include "xminkowski2d.h"

xcsg_factory::xcsg_factory()
{
Expand Down Expand Up @@ -74,6 +75,7 @@ xcsg_factory::xcsg_factory()
m_shape2d_map.insert(std::make_pair("hull2d",xcsg_factory::make_hull2d));
m_shape2d_map.insert(std::make_pair("fill2d",xcsg_factory::make_fill2d));
m_shape2d_map.insert(std::make_pair("offset2d",xcsg_factory::make_offset2d));
m_shape2d_map.insert(std::make_pair("minkowski2d",xcsg_factory::make_minkowski2d));
}

xcsg_factory::~xcsg_factory()
Expand Down Expand Up @@ -141,3 +143,4 @@ std::shared_ptr<xshape2d> xcsg_factory::make_union2d(const cf_xmlNode& node)
std::shared_ptr<xshape2d> xcsg_factory::make_hull2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xhull2d(node)); }
std::shared_ptr<xshape2d> xcsg_factory::make_fill2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xfill2d(node)); }
std::shared_ptr<xshape2d> xcsg_factory::make_offset2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xoffset2d(node)); }
std::shared_ptr<xshape2d> xcsg_factory::make_minkowski2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xminkowski2d(node)); }
2 changes: 1 addition & 1 deletion xcsg/xcsg_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class xcsg_factory {
static std::shared_ptr<xshape2d> make_hull2d(const cf_xmlNode& node);
static std::shared_ptr<xshape2d> make_fill2d(const cf_xmlNode& node);
static std::shared_ptr<xshape2d> make_offset2d(const cf_xmlNode& node);
// static std::shared_ptr<xshape2d> make_soffset2d(const cf_xmlNode& node);
static std::shared_ptr<xshape2d> make_minkowski2d(const cf_xmlNode& node);

private:
typedef std::map<std::string,solid_factory> solid_factory_map;
Expand Down
50 changes: 50 additions & 0 deletions xcsg/xminkowski2d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "xminkowski2d.h"
#include "cf_xmlNode.h"
#include "xshape2d_collector.h"

#include "clipper_boolean.h"
#include "boolean_timer.h"

xminkowski2d::xminkowski2d()
{}

xminkowski2d::xminkowski2d(const cf_xmlNode& node)
{
if(node.tag() != "minkowski2d")throw logic_error("Expected xml tag minkowski2d, but found " + node.tag());
set_transform(node);
xshape2d_collector::collect_children(node,m_incl);

if(m_incl.size() != 2) throw logic_error("Expected 2 parameters for minkowski2d, but got " + std::to_string(m_incl.size()));
}


xminkowski2d::~xminkowski2d()
{}

size_t xminkowski2d::nbool()
{
size_t nbool = 0;
for(auto i=m_incl.begin(); i!=m_incl.end(); i++) {
std::shared_ptr<xshape2d> obj = *i;
nbool += (obj->nbool()+1);
}
return nbool-1;
}


std::shared_ptr<clipper_profile> xminkowski2d::create_clipper_profile(const carve::math::Matrix& t) const
{
std::shared_ptr<clipper_profile> a = m_incl[0]->create_clipper_profile(t*get_transform());
std::shared_ptr<clipper_profile> b_brush = m_incl[1]->create_clipper_profile(t*get_transform());

clipper_boolean csg;
csg.minkowski_sum(a,b_brush);
return csg.profile();
}

std::shared_ptr<carve::mesh::MeshSet<3>> xminkowski2d::create_carve_mesh(const carve::math::Matrix& t) const
{
std::shared_ptr<carve::mesh::MeshSet<3>> mesh;

return mesh;
}
22 changes: 22 additions & 0 deletions xcsg/xminkowski2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef xminkowski2d_H
#define xminkowski2d_H

#include "xshape2d.h"

class xminkowski2d : public xshape2d {
public:
xminkowski2d();
xminkowski2d(const cf_xmlNode& node);
virtual ~xminkowski2d();

virtual size_t nbool();

std::shared_ptr<clipper_profile> create_clipper_profile(const carve::math::Matrix& t = carve::math::Matrix()) const;
std::shared_ptr<carve::mesh::MeshSet<3>> create_carve_mesh(const carve::math::Matrix& t = carve::math::Matrix()) const;
protected:

private:
std::vector<std::shared_ptr<xshape2d>> m_incl;
};

#endif // xminkowski2d_H

0 comments on commit a45505e

Please sign in to comment.