forked from Ultimaker/CuraEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.cpp
56 lines (49 loc) · 1.64 KB
/
bridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/** Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License */
#include "bridge.h"
#include "utils/polygondebug.h"
int bridgeAngle(SliceLayerPart* part, SliceLayer* prevLayer)
{
//To detect if we have a bridge, first calculate the intersection of the current layer with the previous layer.
// This gives us the islands that the layer rests on.
Polygons islands;
for(unsigned int n=0; n<prevLayer->parts.size(); n++)
{
if (!part->boundaryBox.hit(prevLayer->parts[n].boundaryBox)) continue;
islands.add(part->outline.intersection(prevLayer->parts[n].outline));
}
if (islands.size() > 5)
return -1;
//Next find the 2 largest islands that we rest on.
double area1 = 0;
double area2 = 0;
int idx1 = -1;
int idx2 = -1;
for(unsigned int n=0; n<islands.size(); n++)
{
//Skip internal holes
if (!islands[n].orientation())
continue;
double area = fabs(islands[n].area());
if (area > area1)
{
if (area1 > area2)
{
area2 = area1;
idx2 = idx1;
}
area1 = area;
idx1 = n;
}else if (area > area2)
{
area2 = area;
idx2 = n;
}
}
if (idx1 < 0 || idx2 < 0)
return -1;
Point center1 = islands[idx1].centerOfMass();
Point center2 = islands[idx2].centerOfMass();
double angle = atan2(center2.X - center1.X, center2.Y - center1.Y) / M_PI * 180;
if (angle < 0) angle += 360;
return angle;
}