Skip to content

Commit a45505e

Browse files
committed
Added 2d minkowski
1 parent 848b486 commit a45505e

7 files changed

+117
-3
lines changed

xcsg/clipper_boolean.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@ bool clipper_boolean::compute(std::shared_ptr<clipper_profile> b, ClipperLib::Cl
5252
return success;
5353
}
5454

55+
bool clipper_boolean::minkowski_sum(std::shared_ptr<clipper_profile> a, std::shared_ptr<clipper_profile> b_brush )
56+
{
57+
ClipperLib::Clipper clipper;
58+
ClipperLib::Paths& a_paths = a->paths();
59+
ClipperLib::Paths& b_paths = b_brush->paths();
60+
if(b_paths.size() != 1) {
61+
throw std::logic_error("clipper_boolean::minkowski_sum, 'b' parameter must contain exactly one path");
62+
}
63+
64+
boost::posix_time::ptime p1 = boost::posix_time::microsec_clock::universal_time();
65+
66+
ClipperLib::Path& pattern = b_paths[0];
67+
std::shared_ptr<clipper_profile> result(new clipper_profile);
68+
bool pathIsClosed = true;
69+
ClipperLib::MinkowskiSum(pattern,a_paths,result->paths(),pathIsClosed);
70+
bool success = result->paths().size() > 0;
71+
if(success) {
72+
ClipperLib::CleanPolygons(result->paths());
73+
m_profile = result;
74+
}
75+
76+
boost::posix_time::time_duration ptime_diff = boost::posix_time::microsec_clock::universal_time() - p1;
77+
double elapsed_sec = 0.001*ptime_diff.total_milliseconds();
78+
79+
boolean_timer::singleton().add_elapsed(elapsed_sec);
80+
81+
return success;
82+
}
83+
5584
std::shared_ptr<clipper_profile> clipper_boolean::profile()
5685
{
5786
return m_profile;

xcsg/clipper_boolean.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// Public License version 2 or 3 (at your option) as published by the
88
// Free Software Foundation and appearing in the files LICENSE.GPL2
99
// and LICENSE.GPL3 included in the packaging of this file.
10-
//
10+
//
1111
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
1212
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
1313
// A PARTICULAR PURPOSE. ALL COPIES OF THIS FILE MUST INCLUDE THIS LICENSE.
1414
// EndLicense:
15-
15+
1616
#ifndef CLIPPER_BOOLEAN_H
1717
#define CLIPPER_BOOLEAN_H
1818

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

33+
// compute minkowski sum of a and b_brush
34+
// a is assumed to be the main object and "b_brush" is "brushed" along the a path
35+
bool minkowski_sum(std::shared_ptr<clipper_profile> a, std::shared_ptr<clipper_profile> b_brush );
36+
3337
private:
3438
std::shared_ptr<clipper_profile> m_profile;
3539
};

xcsg/xcsg.cbp

+6
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,12 @@
442442
<Unit filename="xlinear_extrude.h">
443443
<Option virtualFolder="boolean/3d/" />
444444
</Unit>
445+
<Unit filename="xminkowski2d.cpp">
446+
<Option virtualFolder="boolean/2d/" />
447+
</Unit>
448+
<Unit filename="xminkowski2d.h">
449+
<Option virtualFolder="boolean/2d/" />
450+
</Unit>
445451
<Unit filename="xminkowski3d.cpp">
446452
<Option virtualFolder="boolean/3d/" />
447453
</Unit>

xcsg/xcsg_factory.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "xhull2d.h"
4545
#include "xfill2d.h"
4646
#include "xoffset2d.h"
47+
#include "xminkowski2d.h"
4748

4849
xcsg_factory::xcsg_factory()
4950
{
@@ -74,6 +75,7 @@ xcsg_factory::xcsg_factory()
7475
m_shape2d_map.insert(std::make_pair("hull2d",xcsg_factory::make_hull2d));
7576
m_shape2d_map.insert(std::make_pair("fill2d",xcsg_factory::make_fill2d));
7677
m_shape2d_map.insert(std::make_pair("offset2d",xcsg_factory::make_offset2d));
78+
m_shape2d_map.insert(std::make_pair("minkowski2d",xcsg_factory::make_minkowski2d));
7779
}
7880

7981
xcsg_factory::~xcsg_factory()
@@ -141,3 +143,4 @@ std::shared_ptr<xshape2d> xcsg_factory::make_union2d(const cf_xmlNode& node)
141143
std::shared_ptr<xshape2d> xcsg_factory::make_hull2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xhull2d(node)); }
142144
std::shared_ptr<xshape2d> xcsg_factory::make_fill2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xfill2d(node)); }
143145
std::shared_ptr<xshape2d> xcsg_factory::make_offset2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xoffset2d(node)); }
146+
std::shared_ptr<xshape2d> xcsg_factory::make_minkowski2d(const cf_xmlNode& node) { return std::shared_ptr<xshape2d>(new xminkowski2d(node)); }

xcsg/xcsg_factory.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class xcsg_factory {
7979
static std::shared_ptr<xshape2d> make_hull2d(const cf_xmlNode& node);
8080
static std::shared_ptr<xshape2d> make_fill2d(const cf_xmlNode& node);
8181
static std::shared_ptr<xshape2d> make_offset2d(const cf_xmlNode& node);
82-
// static std::shared_ptr<xshape2d> make_soffset2d(const cf_xmlNode& node);
82+
static std::shared_ptr<xshape2d> make_minkowski2d(const cf_xmlNode& node);
8383

8484
private:
8585
typedef std::map<std::string,solid_factory> solid_factory_map;

xcsg/xminkowski2d.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "xminkowski2d.h"
2+
#include "cf_xmlNode.h"
3+
#include "xshape2d_collector.h"
4+
5+
#include "clipper_boolean.h"
6+
#include "boolean_timer.h"
7+
8+
xminkowski2d::xminkowski2d()
9+
{}
10+
11+
xminkowski2d::xminkowski2d(const cf_xmlNode& node)
12+
{
13+
if(node.tag() != "minkowski2d")throw logic_error("Expected xml tag minkowski2d, but found " + node.tag());
14+
set_transform(node);
15+
xshape2d_collector::collect_children(node,m_incl);
16+
17+
if(m_incl.size() != 2) throw logic_error("Expected 2 parameters for minkowski2d, but got " + std::to_string(m_incl.size()));
18+
}
19+
20+
21+
xminkowski2d::~xminkowski2d()
22+
{}
23+
24+
size_t xminkowski2d::nbool()
25+
{
26+
size_t nbool = 0;
27+
for(auto i=m_incl.begin(); i!=m_incl.end(); i++) {
28+
std::shared_ptr<xshape2d> obj = *i;
29+
nbool += (obj->nbool()+1);
30+
}
31+
return nbool-1;
32+
}
33+
34+
35+
std::shared_ptr<clipper_profile> xminkowski2d::create_clipper_profile(const carve::math::Matrix& t) const
36+
{
37+
std::shared_ptr<clipper_profile> a = m_incl[0]->create_clipper_profile(t*get_transform());
38+
std::shared_ptr<clipper_profile> b_brush = m_incl[1]->create_clipper_profile(t*get_transform());
39+
40+
clipper_boolean csg;
41+
csg.minkowski_sum(a,b_brush);
42+
return csg.profile();
43+
}
44+
45+
std::shared_ptr<carve::mesh::MeshSet<3>> xminkowski2d::create_carve_mesh(const carve::math::Matrix& t) const
46+
{
47+
std::shared_ptr<carve::mesh::MeshSet<3>> mesh;
48+
49+
return mesh;
50+
}

xcsg/xminkowski2d.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef xminkowski2d_H
2+
#define xminkowski2d_H
3+
4+
#include "xshape2d.h"
5+
6+
class xminkowski2d : public xshape2d {
7+
public:
8+
xminkowski2d();
9+
xminkowski2d(const cf_xmlNode& node);
10+
virtual ~xminkowski2d();
11+
12+
virtual size_t nbool();
13+
14+
std::shared_ptr<clipper_profile> create_clipper_profile(const carve::math::Matrix& t = carve::math::Matrix()) const;
15+
std::shared_ptr<carve::mesh::MeshSet<3>> create_carve_mesh(const carve::math::Matrix& t = carve::math::Matrix()) const;
16+
protected:
17+
18+
private:
19+
std::vector<std::shared_ptr<xshape2d>> m_incl;
20+
};
21+
22+
#endif // xminkowski2d_H

0 commit comments

Comments
 (0)