summaryrefslogtreecommitdiff
path: root/agg/source/agg_vcgen_smooth_poly1.cpp
blob: 1df1edb5c72c993318ac3e53a717c3792f06ad49 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
//
// Smooth polygon generator
//
//----------------------------------------------------------------------------

#include "agg_vcgen_smooth_poly1.h"

namespace agg
{

    //------------------------------------------------------------------------
    vcgen_smooth_poly1::vcgen_smooth_poly1() :
        m_src_vertices(),
        m_smooth_value(0.5),
        m_closed(0),
        m_status(initial),
        m_src_vertex(0)
    {
    }


    //------------------------------------------------------------------------
    void vcgen_smooth_poly1::remove_all()
    {
        m_src_vertices.remove_all();
        m_closed = 0;
        m_status = initial;
    }


    //------------------------------------------------------------------------
    void vcgen_smooth_poly1::add_vertex(double x, double y, unsigned cmd)
    {
        m_status = initial;
        if(is_move_to(cmd))
        {
            m_src_vertices.modify_last(vertex_dist(x, y));
        }
        else
        {
            if(is_vertex(cmd))
            {
                m_src_vertices.add(vertex_dist(x, y));
            }
            else
            {
                m_closed = get_close_flag(cmd);
            }
        }
    }


    //------------------------------------------------------------------------
    void vcgen_smooth_poly1::rewind(unsigned)
    {
        if(m_status == initial)
        {
            m_src_vertices.close(m_closed != 0);
        }
        m_status = ready;
        m_src_vertex = 0;
    }


    //------------------------------------------------------------------------
    void vcgen_smooth_poly1::calculate(const vertex_dist& v0,
                                       const vertex_dist& v1,
                                       const vertex_dist& v2,
                                       const vertex_dist& v3)
    {

        double k1 = v0.dist / (v0.dist + v1.dist);
        double k2 = v1.dist / (v1.dist + v2.dist);

        double xm1 = v0.x + (v2.x - v0.x) * k1;
        double ym1 = v0.y + (v2.y - v0.y) * k1;
        double xm2 = v1.x + (v3.x - v1.x) * k2;
        double ym2 = v1.y + (v3.y - v1.y) * k2;

        m_ctrl1_x = v1.x + m_smooth_value * (v2.x - xm1);
        m_ctrl1_y = v1.y + m_smooth_value * (v2.y - ym1);
        m_ctrl2_x = v2.x + m_smooth_value * (v1.x - xm2);
        m_ctrl2_y = v2.y + m_smooth_value * (v1.y - ym2);
    }


    //------------------------------------------------------------------------
    unsigned vcgen_smooth_poly1::vertex(double* x, double* y)
    {
        unsigned cmd = path_cmd_line_to;
        while(!is_stop(cmd))
        {
            switch(m_status)
            {
            case initial:
                rewind(0);

            case ready:
                if(m_src_vertices.size() <  2)
                {
                    cmd = path_cmd_stop;
                    break;
                }

                if(m_src_vertices.size() == 2)
                {
                    *x = m_src_vertices[m_src_vertex].x;
                    *y = m_src_vertices[m_src_vertex].y;
                    m_src_vertex++;
                    if(m_src_vertex == 1) return path_cmd_move_to;
                    if(m_src_vertex == 2) return path_cmd_line_to;
                    cmd = path_cmd_stop;
                    break;
                }

                cmd = path_cmd_move_to;
                m_status = polygon;
                m_src_vertex = 0;

            case polygon:
                if(m_closed)
                {
                    if(m_src_vertex >= m_src_vertices.size())
                    {
                        *x = m_src_vertices[0].x;
                        *y = m_src_vertices[0].y;
                        m_status = end_poly;
                        return path_cmd_curve4;
                    }
                }
                else
                {
                    if(m_src_vertex >= m_src_vertices.size() - 1)
                    {
                        *x = m_src_vertices[m_src_vertices.size() - 1].x;
                        *y = m_src_vertices[m_src_vertices.size() - 1].y;
                        m_status = end_poly;
                        return path_cmd_curve3;
                    }
                }

                calculate(m_src_vertices.prev(m_src_vertex),
                          m_src_vertices.curr(m_src_vertex),
                          m_src_vertices.next(m_src_vertex),
                          m_src_vertices.next(m_src_vertex + 1));

                *x = m_src_vertices[m_src_vertex].x;
                *y = m_src_vertices[m_src_vertex].y;
                m_src_vertex++;

                if(m_closed)
                {
                    m_status = ctrl1;
                    return ((m_src_vertex == 1) ?
                             path_cmd_move_to :
                             path_cmd_curve4);
                }
                else
                {
                    if(m_src_vertex == 1)
                    {
                        m_status = ctrl_b;
                        return path_cmd_move_to;
                    }
                    if(m_src_vertex >= m_src_vertices.size() - 1)
                    {
                        m_status = ctrl_e;
                        return path_cmd_curve3;
                    }
                    m_status = ctrl1;
                    return path_cmd_curve4;
                }
                // statement unreachable
                //break;

            case ctrl_b:
                *x = m_ctrl2_x;
                *y = m_ctrl2_y;
                m_status = polygon;
                return path_cmd_curve3;

            case ctrl_e:
                *x = m_ctrl1_x;
                *y = m_ctrl1_y;
                m_status = polygon;
                return path_cmd_curve3;

            case ctrl1:
                *x = m_ctrl1_x;
                *y = m_ctrl1_y;
                m_status = ctrl2;
                return path_cmd_curve4;

            case ctrl2:
                *x = m_ctrl2_x;
                *y = m_ctrl2_y;
                m_status = polygon;
                return path_cmd_curve4;

            case end_poly:
                m_status = stop;
                return path_cmd_end_poly | m_closed;

            case stop:
                return path_cmd_stop;
            }
        }
        return cmd;
    }

}