~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to xs/src/Polygon.cpp

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <myinit.h>
 
2
#include "ClipperUtils.hpp"
 
3
#include "Polygon.hpp"
 
4
#include "Polyline.hpp"
 
5
 
 
6
namespace Slic3r {
 
7
 
 
8
Polygon::operator Polygons() const
 
9
{
 
10
    Polygons pp;
 
11
    pp.push_back(*this);
 
12
    return pp;
 
13
}
 
14
 
 
15
Polygon::operator Polyline() const
 
16
{
 
17
    Polyline polyline;
 
18
    this->split_at_first_point(&polyline);
 
19
    return polyline;
 
20
}
 
21
 
 
22
Point&
 
23
Polygon::operator[](Points::size_type idx)
 
24
{
 
25
    return this->points[idx];
 
26
}
 
27
 
 
28
const Point&
 
29
Polygon::operator[](Points::size_type idx) const
 
30
{
 
31
    return this->points[idx];
 
32
}
 
33
 
 
34
Point
 
35
Polygon::last_point() const
 
36
{
 
37
    return this->points.front();  // last point == first point for polygons
 
38
}
 
39
 
 
40
Lines
 
41
Polygon::lines() const
 
42
{
 
43
    Lines lines;
 
44
    this->lines(&lines);
 
45
    return lines;
 
46
}
 
47
 
 
48
void
 
49
Polygon::lines(Lines* lines) const
 
50
{
 
51
    lines->reserve(lines->size() + this->points.size());
 
52
    for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
 
53
        lines->push_back(Line(*it, *(it + 1)));
 
54
    }
 
55
    lines->push_back(Line(this->points.back(), this->points.front()));
 
56
}
 
57
 
 
58
void
 
59
Polygon::split_at_vertex(const Point &point, Polyline* polyline) const
 
60
{
 
61
    // find index of point
 
62
    for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
 
63
        if (it->coincides_with(point)) {
 
64
            this->split_at_index(it - this->points.begin(), polyline);
 
65
            return;
 
66
        }
 
67
    }
 
68
    CONFESS("Point not found");
 
69
}
 
70
 
 
71
void
 
72
Polygon::split_at_index(int index, Polyline* polyline) const
 
73
{
 
74
    polyline->points.reserve(this->points.size() + 1);
 
75
    for (Points::const_iterator it = this->points.begin() + index; it != this->points.end(); ++it)
 
76
        polyline->points.push_back(*it);
 
77
    for (Points::const_iterator it = this->points.begin(); it != this->points.begin() + index + 1; ++it)
 
78
        polyline->points.push_back(*it);
 
79
}
 
80
 
 
81
void
 
82
Polygon::split_at_first_point(Polyline* polyline) const
 
83
{
 
84
    this->split_at_index(0, polyline);
 
85
}
 
86
 
 
87
void
 
88
Polygon::equally_spaced_points(double distance, Points* points) const
 
89
{
 
90
    Polyline polyline;
 
91
    this->split_at_first_point(&polyline);
 
92
    polyline.equally_spaced_points(distance, points);
 
93
}
 
94
 
 
95
double
 
96
Polygon::area() const
 
97
{
 
98
    ClipperLib::Path p;
 
99
    Slic3rMultiPoint_to_ClipperPath(*this, p);
 
100
    return ClipperLib::Area(p);
 
101
}
 
102
 
 
103
bool
 
104
Polygon::is_counter_clockwise() const
 
105
{
 
106
    ClipperLib::Path p;
 
107
    Slic3rMultiPoint_to_ClipperPath(*this, p);
 
108
    return ClipperLib::Orientation(p);
 
109
}
 
110
 
 
111
bool
 
112
Polygon::is_clockwise() const
 
113
{
 
114
    return !this->is_counter_clockwise();
 
115
}
 
116
 
 
117
bool
 
118
Polygon::make_counter_clockwise()
 
119
{
 
120
    if (!this->is_counter_clockwise()) {
 
121
        this->reverse();
 
122
        return true;
 
123
    }
 
124
    return false;
 
125
}
 
126
 
 
127
bool
 
128
Polygon::make_clockwise()
 
129
{
 
130
    if (this->is_counter_clockwise()) {
 
131
        this->reverse();
 
132
        return true;
 
133
    }
 
134
    return false;
 
135
}
 
136
 
 
137
bool
 
138
Polygon::is_valid() const
 
139
{
 
140
    return this->points.size() >= 3;
 
141
}
 
142
 
 
143
bool
 
144
Polygon::contains_point(const Point &point) const
 
145
{
 
146
    // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
 
147
    bool result = false;
 
148
    Points::const_iterator i = this->points.begin();
 
149
    Points::const_iterator j = this->points.end() - 1;
 
150
    for (; i != this->points.end(); j = i++) {
 
151
        if ( ((i->y > point.y) != (j->y > point.y))
 
152
            && ((double)point.x < (double)(j->x - i->x) * (double)(point.y - i->y) / (double)(j->y - i->y) + (double)i->x) )
 
153
            result = !result;
 
154
    }
 
155
    return result;
 
156
}
 
157
 
 
158
Polygons
 
159
Polygon::simplify(double tolerance) const
 
160
{
 
161
    Polygon p = *this;
 
162
    p.points = MultiPoint::_douglas_peucker(p.points, tolerance);
 
163
    
 
164
    Polygons pp;
 
165
    pp.push_back(p);
 
166
    simplify_polygons(pp, pp);
 
167
    return pp;
 
168
}
 
169
 
 
170
void
 
171
Polygon::simplify(double tolerance, Polygons &polygons) const
 
172
{
 
173
    Polygons pp = this->simplify(tolerance);
 
174
    polygons.reserve(polygons.size() + pp.size());
 
175
    polygons.insert(polygons.end(), pp.begin(), pp.end());
 
176
}
 
177
 
 
178
// Only call this on convex polygons or it will return invalid results
 
179
void
 
180
Polygon::triangulate_convex(Polygons* polygons) const
 
181
{
 
182
    for (Points::const_iterator it = this->points.begin() + 2; it != this->points.end(); ++it) {
 
183
        Polygon p;
 
184
        p.points.reserve(3);
 
185
        p.points.push_back(this->points.front());
 
186
        p.points.push_back(*(it-1));
 
187
        p.points.push_back(*it);
 
188
        
 
189
        // this should be replaced with a more efficient call to a merge_collinear_segments() method
 
190
        if (p.area() > 0) polygons->push_back(p);
 
191
    }
 
192
}
 
193
 
 
194
// center of mass
 
195
Point
 
196
Polygon::centroid() const
 
197
{
 
198
    double area_temp = this->area();
 
199
    double x_temp = 0;
 
200
    double y_temp = 0;
 
201
    
 
202
    Polyline polyline;
 
203
    this->split_at_first_point(&polyline);
 
204
    for (Points::const_iterator point = polyline.points.begin(); point != polyline.points.end() - 1; ++point) {
 
205
        x_temp += (double)( point->x + (point+1)->x ) * ( (double)point->x*(point+1)->y - (double)(point+1)->x*point->y );
 
206
        y_temp += (double)( point->y + (point+1)->y ) * ( (double)point->x*(point+1)->y - (double)(point+1)->x*point->y );
 
207
    }
 
208
    
 
209
    return Point(x_temp/(6*area_temp), y_temp/(6*area_temp));
 
210
}
 
211
 
 
212
#ifdef SLIC3RXS
 
213
REGISTER_CLASS(Polygon, "Polygon");
 
214
 
 
215
void
 
216
Polygon::from_SV_check(SV* poly_sv)
 
217
{
 
218
    if (sv_isobject(poly_sv) && !sv_isa(poly_sv, perl_class_name(this)) && !sv_isa(poly_sv, perl_class_name_ref(this)))
 
219
        CONFESS("Not a valid %s object", perl_class_name(this));
 
220
    
 
221
    MultiPoint::from_SV_check(poly_sv);
 
222
}
 
223
#endif
 
224
 
 
225
}