File size: 6,648 Bytes
f9eec05 | 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 | //----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// 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_vcgen_bspline.h"
namespace agg
{
//------------------------------------------------------------------------
vcgen_bspline::vcgen_bspline() :
m_src_vertices(),
m_spline_x(),
m_spline_y(),
m_interpolation_step(1.0/50.0),
m_closed(0),
m_status(initial),
m_src_vertex(0)
{
}
//------------------------------------------------------------------------
void vcgen_bspline::remove_all()
{
m_src_vertices.remove_all();
m_closed = 0;
m_status = initial;
m_src_vertex = 0;
}
//------------------------------------------------------------------------
void vcgen_bspline::add_vertex(double x, double y, unsigned cmd)
{
m_status = initial;
if(is_move_to(cmd))
{
m_src_vertices.modify_last(point_d(x, y));
}
else
{
if(is_vertex(cmd))
{
m_src_vertices.add(point_d(x, y));
}
else
{
m_closed = get_close_flag(cmd);
}
}
}
//------------------------------------------------------------------------
void vcgen_bspline::rewind(unsigned)
{
m_cur_abscissa = 0.0;
m_max_abscissa = 0.0;
m_src_vertex = 0;
if(m_status == initial && m_src_vertices.size() > 2)
{
if(m_closed)
{
m_spline_x.init(m_src_vertices.size() + 8);
m_spline_y.init(m_src_vertices.size() + 8);
m_spline_x.add_point(0.0, m_src_vertices.prev(m_src_vertices.size() - 3).x);
m_spline_y.add_point(0.0, m_src_vertices.prev(m_src_vertices.size() - 3).y);
m_spline_x.add_point(1.0, m_src_vertices[m_src_vertices.size() - 3].x);
m_spline_y.add_point(1.0, m_src_vertices[m_src_vertices.size() - 3].y);
m_spline_x.add_point(2.0, m_src_vertices[m_src_vertices.size() - 2].x);
m_spline_y.add_point(2.0, m_src_vertices[m_src_vertices.size() - 2].y);
m_spline_x.add_point(3.0, m_src_vertices[m_src_vertices.size() - 1].x);
m_spline_y.add_point(3.0, m_src_vertices[m_src_vertices.size() - 1].y);
}
else
{
m_spline_x.init(m_src_vertices.size());
m_spline_y.init(m_src_vertices.size());
}
unsigned i;
for(i = 0; i < m_src_vertices.size(); i++)
{
double x = m_closed ? i + 4 : i;
m_spline_x.add_point(x, m_src_vertices[i].x);
m_spline_y.add_point(x, m_src_vertices[i].y);
}
m_cur_abscissa = 0.0;
m_max_abscissa = m_src_vertices.size() - 1;
if(m_closed)
{
m_cur_abscissa = 4.0;
m_max_abscissa += 5.0;
m_spline_x.add_point(m_src_vertices.size() + 4, m_src_vertices[0].x);
m_spline_y.add_point(m_src_vertices.size() + 4, m_src_vertices[0].y);
m_spline_x.add_point(m_src_vertices.size() + 5, m_src_vertices[1].x);
m_spline_y.add_point(m_src_vertices.size() + 5, m_src_vertices[1].y);
m_spline_x.add_point(m_src_vertices.size() + 6, m_src_vertices[2].x);
m_spline_y.add_point(m_src_vertices.size() + 6, m_src_vertices[2].y);
m_spline_x.add_point(m_src_vertices.size() + 7, m_src_vertices.next(2).x);
m_spline_y.add_point(m_src_vertices.size() + 7, m_src_vertices.next(2).y);
}
m_spline_x.prepare();
m_spline_y.prepare();
}
m_status = ready;
}
//------------------------------------------------------------------------
unsigned vcgen_bspline::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_cur_abscissa >= m_max_abscissa)
{
if(m_closed)
{
m_status = end_poly;
break;
}
else
{
*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_line_to;
}
}
*x = m_spline_x.get_stateful(m_cur_abscissa);
*y = m_spline_y.get_stateful(m_cur_abscissa);
m_src_vertex++;
m_cur_abscissa += m_interpolation_step;
return (m_src_vertex == 1) ? path_cmd_move_to : path_cmd_line_to;
case end_poly:
m_status = stop;
return path_cmd_end_poly | m_closed;
case stop:
return path_cmd_stop;
}
}
return cmd;
}
}
|