summaryrefslogtreecommitdiff
path: root/agg/source/agg_trans_single_path.cpp
blob: 012788625389ae3c7cb71cc732537d2e49652ef6 (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
//----------------------------------------------------------------------------
// 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
//----------------------------------------------------------------------------

#include "agg_math.h"
#include "agg_vertex_sequence.h"
#include "agg_trans_single_path.h"

namespace agg
{

    //------------------------------------------------------------------------
    trans_single_path::trans_single_path() :
        m_base_length(0.0),
        m_kindex(0.0),
        m_status(initial),
        m_preserve_x_scale(true)
    {
    }

    //------------------------------------------------------------------------
    void trans_single_path::reset()
    {
        m_src_vertices.remove_all();
        m_kindex = 0.0;
        m_status = initial;
    }

    //------------------------------------------------------------------------
    void trans_single_path::move_to(double x, double y)
    {
        if(m_status == initial)
        {
            m_src_vertices.modify_last(vertex_dist(x, y));
            m_status = making_path;
        }
        else
        {
            line_to(x, y);
        }
    }

    //------------------------------------------------------------------------
    void trans_single_path::line_to(double x, double y)
    {
        if(m_status == making_path)
        {
            m_src_vertices.add(vertex_dist(x, y));
        }
    }


    //------------------------------------------------------------------------
    void trans_single_path::finalize_path()
    {
        if(m_status == making_path && m_src_vertices.size() > 1)
        {
            unsigned i;
            double dist;
            double d;

            if(m_src_vertices.size() > 2)
            {
                if(m_src_vertices[m_src_vertices.size() - 2].dist * 10.0 < 
                   m_src_vertices[m_src_vertices.size() - 3].dist)
                {
                    d = m_src_vertices[m_src_vertices.size() - 3].dist + 
                        m_src_vertices[m_src_vertices.size() - 2].dist;

                    m_src_vertices[m_src_vertices.size() - 2] = 
                        m_src_vertices[m_src_vertices.size() - 1];

                    m_src_vertices.remove_last();
                    m_src_vertices[m_src_vertices.size() - 2].dist = d;
                }
            }

            dist = 0.0;
            m_src_vertices.close(false);
            for(i = 0; i < m_src_vertices.size(); i++)
            {
                vertex_dist& v = m_src_vertices[i];
                double _d = v.dist;
                v.dist = dist;
                dist += _d;
            }
            m_kindex = (m_src_vertices.size() - 1) / dist;
            m_status = ready;
        }
    }



    //------------------------------------------------------------------------
    double trans_single_path::total_length() const
    {
        if(m_base_length >= 1e-10) return m_base_length;
        return (m_status == ready) ? 
            m_src_vertices[m_src_vertices.size() - 1].dist :
            0.0;
    }


    //------------------------------------------------------------------------
    void trans_single_path::transform(double *x, double *y) const
    {
        if(m_status == ready)
        {
            if(m_base_length > 1e-10)
            {
                *x *= m_src_vertices[m_src_vertices.size() - 1].dist / 
                      m_base_length;
            }

            double x1 = 0.0;
            double y1 = 0.0;
            double dx = 1.0;
            double dy = 1.0;
            double d  = 0.0;
            double dd = 1.0;
            if(*x < 0.0)
            {
                // Extrapolation on the left
                //--------------------------
                x1 = m_src_vertices[0].x;
                y1 = m_src_vertices[0].y;
                dx = m_src_vertices[1].x - x1;
                dy = m_src_vertices[1].y - y1;
                dd = m_src_vertices[1].dist - m_src_vertices[0].dist;
                d  = *x;
            }
            else
            if(*x > m_src_vertices[m_src_vertices.size() - 1].dist)
            {
                // Extrapolation on the right
                //--------------------------
                unsigned i = m_src_vertices.size() - 2;
                unsigned j = m_src_vertices.size() - 1;
                x1 = m_src_vertices[j].x;
                y1 = m_src_vertices[j].y;
                dx = x1 - m_src_vertices[i].x;
                dy = y1 - m_src_vertices[i].y;
                dd = m_src_vertices[j].dist - m_src_vertices[i].dist;
                d  = *x - m_src_vertices[j].dist;
            }
            else
            {
                // Interpolation
                //--------------------------
                unsigned i = 0;
                unsigned j = m_src_vertices.size() - 1;
                if(m_preserve_x_scale)
                {
                    unsigned k;
                    for(i = 0; (j - i) > 1; ) 
                    {
                        if(*x < m_src_vertices[k = (i + j) >> 1].dist) 
                        {
                            j = k; 
                        }
                        else 
                        {
                            i = k;
                        }
                    }
                    d  = m_src_vertices[i].dist;
                    dd = m_src_vertices[j].dist - d;
                    d  = *x - d;
                }
                else
                {
                    i = (unsigned)floor(*x * m_kindex);
                    j = i + 1;
                    dd = m_src_vertices[j].dist - m_src_vertices[i].dist;
                    d = ((*x * m_kindex) - i) * dd;
                }
                x1 = m_src_vertices[i].x;
                y1 = m_src_vertices[i].y;
                dx = m_src_vertices[j].x - x1;
                dy = m_src_vertices[j].y - y1;
            }
            double x2 = x1 + dx * d / dd;
            double y2 = y1 + dy * d / dd;
            *x = x2 - *y * dy / dd;
            *y = y2 + *y * dx / dd;
        }
    }


}