Mark twist-free linear extrusions of convex polygons convex

master
Oskar Linde 2014-09-18 17:48:32 +02:00
parent 2e1f24b4dc
commit b62079a632
3 changed files with 20 additions and 1 deletions

View File

@ -633,7 +633,8 @@ static void add_slice(PolySet *ps, const Polygon2d &poly,
*/
static Geometry *extrudePolygon(const LinearExtrudeNode &node, const Polygon2d &poly)
{
PolySet *ps = new PolySet(3);
bool cvx = poly.is_convex();
PolySet *ps = new PolySet(3, !cvx ? boost::tribool(false) : node.twist == 0 ? boost::tribool(true) : unknown);
ps->setConvexity(node.convexity);
if (node.height <= 0) return ps;

View File

@ -92,3 +92,20 @@ void Polygon2d::resize(Vector2d newsize, const Eigen::Matrix<bool,2,1> &autosize
this->transform(t);
}
bool Polygon2d::is_convex() const {
if (theoutlines.size() > 1) return false;
if (theoutlines.empty()) return true;
std::vector<Vector2d> const& pts = theoutlines[0].vertices;
int N = pts.size();
// Check for a right turn. This assumes the polygon is simple.
for (int i = 0; i < N; i++) {
Vector2d d1 = pts[(i+1)%N] - pts[i];
Vector2d d2 = pts[(i+2)%N] - pts[(i+1)%N];
double zcross = d1[0]*d2[1]-d1[1]*d2[0];
if (zcross < 0) return false;
}
return true;
}

View File

@ -36,6 +36,7 @@ public:
bool isSanitized() const { return this->sanitized; }
void setSanitized(bool s) { this->sanitized = s; }
bool is_convex() const;
private:
Outlines2d theoutlines;
bool sanitized;