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

« back to all changes in this revision

Viewing changes to xs/src/MultiPoint.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 "MultiPoint.hpp"
 
2
#include "BoundingBox.hpp"
 
3
 
 
4
namespace Slic3r {
 
5
 
 
6
MultiPoint::operator Points() const
 
7
{
 
8
    return this->points;
 
9
}
 
10
 
 
11
void
 
12
MultiPoint::scale(double factor)
 
13
{
 
14
    for (Points::iterator it = points.begin(); it != points.end(); ++it) {
 
15
        (*it).scale(factor);
 
16
    }
 
17
}
 
18
 
 
19
void
 
20
MultiPoint::translate(double x, double y)
 
21
{
 
22
    for (Points::iterator it = points.begin(); it != points.end(); ++it) {
 
23
        (*it).translate(x, y);
 
24
    }
 
25
}
 
26
 
 
27
void
 
28
MultiPoint::rotate(double angle, const Point &center)
 
29
{
 
30
    for (Points::iterator it = points.begin(); it != points.end(); ++it) {
 
31
        (*it).rotate(angle, center);
 
32
    }
 
33
}
 
34
 
 
35
void
 
36
MultiPoint::reverse()
 
37
{
 
38
    std::reverse(this->points.begin(), this->points.end());
 
39
}
 
40
 
 
41
Point
 
42
MultiPoint::first_point() const
 
43
{
 
44
    return this->points.front();
 
45
}
 
46
 
 
47
double
 
48
MultiPoint::length() const
 
49
{
 
50
    Lines lines = this->lines();
 
51
    double len = 0;
 
52
    for (Lines::iterator it = lines.begin(); it != lines.end(); ++it) {
 
53
        len += it->length();
 
54
    }
 
55
    return len;
 
56
}
 
57
 
 
58
bool
 
59
MultiPoint::is_valid() const
 
60
{
 
61
    return this->points.size() >= 2;
 
62
}
 
63
 
 
64
int
 
65
MultiPoint::find_point(const Point &point) const
 
66
{
 
67
    for (Points::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
 
68
        if (it->coincides_with(point)) return it - this->points.begin();
 
69
    }
 
70
    return -1;  // not found
 
71
}
 
72
 
 
73
void
 
74
MultiPoint::bounding_box(BoundingBox* bb) const
 
75
{
 
76
    *bb = BoundingBox(this->points);
 
77
}
 
78
 
 
79
Points
 
80
MultiPoint::_douglas_peucker(const Points &points, const double tolerance)
 
81
{
 
82
    Points results;
 
83
    double dmax = 0;
 
84
    size_t index = 0;
 
85
    Line full(points.front(), points.back());
 
86
    for (Points::const_iterator it = points.begin() + 1; it != points.end(); ++it) {
 
87
        double d = it->distance_to(full);
 
88
        if (d > dmax) {
 
89
            index = it - points.begin();
 
90
            dmax = d;
 
91
        }
 
92
    }
 
93
    if (dmax >= tolerance) {
 
94
        Points dp0;
 
95
        dp0.reserve(index + 1);
 
96
        dp0.insert(dp0.end(), points.begin(), points.begin() + index + 1);
 
97
        Points dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
 
98
        results.reserve(results.size() + dp1.size() - 1);
 
99
        results.insert(results.end(), dp1.begin(), dp1.end() - 1);
 
100
        
 
101
        dp0.clear();
 
102
        dp0.reserve(points.size() - index + 1);
 
103
        dp0.insert(dp0.end(), points.begin() + index, points.end());
 
104
        dp1 = MultiPoint::_douglas_peucker(dp0, tolerance);
 
105
        results.reserve(results.size() + dp1.size());
 
106
        results.insert(results.end(), dp1.begin(), dp1.end());
 
107
    } else {
 
108
        results.push_back(points.front());
 
109
        results.push_back(points.back());
 
110
    }
 
111
    return results;
 
112
}
 
113
 
 
114
#ifdef SLIC3RXS
 
115
void
 
116
MultiPoint::from_SV(SV* poly_sv)
 
117
{
 
118
    AV* poly_av = (AV*)SvRV(poly_sv);
 
119
    const unsigned int num_points = av_len(poly_av)+1;
 
120
    this->points.resize(num_points);
 
121
    
 
122
    for (unsigned int i = 0; i < num_points; i++) {
 
123
        SV** point_sv = av_fetch(poly_av, i, 0);
 
124
        this->points[i].from_SV_check(*point_sv);
 
125
    }
 
126
}
 
127
 
 
128
void
 
129
MultiPoint::from_SV_check(SV* poly_sv)
 
130
{
 
131
    if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
 
132
        *this = *(MultiPoint*)SvIV((SV*)SvRV( poly_sv ));
 
133
    } else {
 
134
        this->from_SV(poly_sv);
 
135
    }
 
136
}
 
137
 
 
138
SV*
 
139
MultiPoint::to_AV() {
 
140
    const unsigned int num_points = this->points.size();
 
141
    AV* av = newAV();
 
142
    av_extend(av, num_points-1);
 
143
    for (unsigned int i = 0; i < num_points; i++) {
 
144
        av_store(av, i, perl_to_SV_ref(this->points[i]));
 
145
    }
 
146
    return newRV_noinc((SV*)av);
 
147
}
 
148
 
 
149
SV*
 
150
MultiPoint::to_SV_pureperl() const {
 
151
    const unsigned int num_points = this->points.size();
 
152
    AV* av = newAV();
 
153
    av_extend(av, num_points-1);
 
154
    for (unsigned int i = 0; i < num_points; i++) {
 
155
        av_store(av, i, this->points[i].to_SV_pureperl());
 
156
    }
 
157
    return newRV_noinc((SV*)av);
 
158
}
 
159
#endif
 
160
 
 
161
}