Debian Codegraph @

extern/agg24-svn/src/ctrl/agg_polygon_ctrl.cpp

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // classes polygon_ctrl_impl
17 //
18 //----------------------------------------------------------------------------
19
20 #include "ctrl/agg_polygon_ctrl.h"
21
22 namespace agg
23 {
24
25 polygon_ctrl_impl::polygon_ctrl_impl(unsigned np, double point_radius) :
26 ctrl(0, 0, 1, 1, false),
27 m_polygon(np * 2),
28 m_num_points(np),
29 m_node(-1),
30 m_edge(-1),
31 m_vs(&m_polygon[0], m_num_points, false),
32 m_stroke(m_vs),
33 m_point_radius(point_radius),
34 m_status(0),
35 m_dx(0.0),
36 m_dy(0.0),
37 m_in_polygon_check(true)
38 {
39 m_stroke.width(1.0);
40 }
41
42
43 void polygon_ctrl_impl::rewind(unsigned)
44 {
45 m_status = 0;
46 m_stroke.rewind(0);
47 }
48
49 unsigned polygon_ctrl_impl::vertex(double* x, double* y)
50 {
51 unsigned cmd = path_cmd_stop;
52 double r = m_point_radius;
53 if(m_status == 0)
54 {
55 cmd = m_stroke.vertex(x, y);
56 if(!is_stop(cmd))
57 {
58 transform_xy(x, y);
59 return cmd;
60 }
61 if(m_node >= 0 && m_node == int(m_status)) r *= 1.2;
62 m_ellipse.init(xn(m_status), yn(m_status), r, r, 32);
63 ++m_status;
64 }
65 cmd = m_ellipse.vertex(x, y);
66 if(!is_stop(cmd))
67 {
68 transform_xy(x, y);
69 return cmd;
70 }
71 if(m_status >= m_num_points) return path_cmd_stop;
72 if(m_node >= 0 && m_node == int(m_status)) r *= 1.2;
73 m_ellipse.init(xn(m_status), yn(m_status), r, r, 32);
74 ++m_status;
75 cmd = m_ellipse.vertex(x, y);
76 if(!is_stop(cmd))
77 {
78 transform_xy(x, y);
79 }
80 return cmd;
81 }
82
83
84 bool polygon_ctrl_impl::check_edge(unsigned i, double x, double y) const
85 {
86 bool ret = false;
87
88 unsigned n1 = i;
89 unsigned n2 = (i + m_num_points - 1) % m_num_points;
90 double x1 = xn(n1);
91 double y1 = yn(n1);
92 double x2 = xn(n2);
93 double y2 = yn(n2);
94
95 double dx = x2 - x1;
96 double dy = y2 - y1;
97
98 if(sqrt(dx*dx + dy*dy) > 0.0000001)
99 {
100 double x3 = x;
101 double y3 = y;
102 double x4 = x3 - dy;
103 double y4 = y3 + dx;
104
105 double den = (y4-y3) * (x2-x1) - (x4-x3) * (y2-y1);
106 double u1 = ((x4-x3) * (y1-y3) - (y4-y3) * (x1-x3)) / den;
107
108 double xi = x1 + u1 * (x2 - x1);
109 double yi = y1 + u1 * (y2 - y1);
110
111 dx = xi - x;
112 dy = yi - y;
113
114 if (u1 > 0.0 && u1 < 1.0 && sqrt(dx*dx + dy*dy) <= m_point_radius)
115 {
116 ret = true;
117 }
118 }
119 return ret;
120 }
121
122
123
124 bool polygon_ctrl_impl::in_rect(double x, double y) const
125 {
126 return false;
127 }
128
129
130 bool polygon_ctrl_impl::on_mouse_button_down(double x, double y)
131 {
132 unsigned i;
133 bool ret = false;
134 m_node = -1;
135 m_edge = -1;
136 inverse_transform_xy(&x, &y);
137 for (i = 0; i < m_num_points; i++)
138 {
139 if(sqrt( (x-xn(i)) * (x-xn(i)) + (y-yn(i)) * (y-yn(i)) ) < m_point_radius)
140 {
141 m_dx = x - xn(i);
142 m_dy = y - yn(i);
143 m_node = int(i);
144 ret = true;
145 break;
146 }
147 }
148
149 if(!ret)
150 {
151 for (i = 0; i < m_num_points; i++)
152 {
153 if(check_edge(i, x, y))
154 {
155 m_dx = x;
156 m_dy = y;
157 m_edge = int(i);
158 ret = true;
159 break;
160 }
161 }
162 }
163
164 if(!ret)
165 {
166 if(point_in_polygon(x, y))
167 {
168 m_dx = x;
169 m_dy = y;
170 m_node = int(m_num_points);
171 ret = true;
172 }
173 }
174 return ret;
175 }
176
177
178 bool polygon_ctrl_impl::on_mouse_move(double x, double y, bool button_flag)
179 {
180 bool ret = false;
181 double dx;
182 double dy;
183 inverse_transform_xy(&x, &y);
184 if(m_node == int(m_num_points))
185 {
186 dx = x - m_dx;
187 dy = y - m_dy;
188 unsigned i;
189 for(i = 0; i < m_num_points; i++)
190 {
191 xn(i) += dx;
192 yn(i) += dy;
193 }
194 m_dx = x;
195 m_dy = y;
196 ret = true;
197 }
198 else
199 {
200 if(m_edge >= 0)
201 {
202 unsigned n1 = m_edge;
203 unsigned n2 = (n1 + m_num_points - 1) % m_num_points;
204 dx = x - m_dx;
205 dy = y - m_dy;
206 xn(n1) += dx;
207 yn(n1) += dy;
208 xn(n2) += dx;
209 yn(n2) += dy;
210 m_dx = x;
211 m_dy = y;
212 ret = true;
213 }
214 else
215 {
216 if(m_node >= 0)
217 {
218 xn(m_node) = x - m_dx;
219 yn(m_node) = y - m_dy;
220 ret = true;
221 }
222 }
223 }
224 return ret;
225 }
226
227 bool polygon_ctrl_impl::on_mouse_button_up(double x, double y)
228 {
229 bool ret = (m_node >= 0) || (m_edge >= 0);
230 m_node = -1;
231 m_edge = -1;
232 return ret;
233 }
234
235
236 bool polygon_ctrl_impl::on_arrow_keys(bool left, bool right, bool down, bool up)
237 {
238 return false;
239 }
240
241
242 //======= Crossings Multiply algorithm of InsideTest ========================
243 //
244 // By Eric Haines, 3D/Eye Inc, [email protected]
245 //
246 // This version is usually somewhat faster than the original published in
247 // Graphics Gems IV; by turning the division for testing the X axis crossing
248 // into a tricky multiplication test this part of the test became faster,
249 // which had the additional effect of making the test for "both to left or
250 // both to right" a bit slower for triangles than simply computing the
251 // intersection each time. The main increase is in triangle testing speed,
252 // which was about 15% faster; all other polygon complexities were pretty much
253 // the same as before. On machines where division is very expensive (not the
254 // case on the HP 9000 series on which I tested) this test should be much
255 // faster overall than the old code. Your mileage may (in fact, will) vary,
256 // depending on the machine and the test data, but in general I believe this
257 // code is both shorter and faster. This test was inspired by unpublished
258 // Graphics Gems submitted by Joseph Samosky and Mark Haigh-Hutchinson.
259 // Related work by Samosky is in:
260 //
261 // Samosky, Joseph, "SectionView: A system for interactively specifying and
262 // visualizing sections through three-dimensional medical image data",
263 // M.S. Thesis, Department of Electrical Engineering and Computer Science,
264 // Massachusetts Institute of Technology, 1993.
265 //
266 // Shoot a test ray along +X axis. The strategy is to compare vertex Y values
267 // to the testing point's Y and quickly discard edges which are entirely to one
268 // side of the test ray. Note that CONVEX and WINDING code can be added as
269 // for the CrossingsTest() code; it is left out here for clarity.
270 //
271 // Input 2D polygon _pgon_ with _numverts_ number of vertices and test point
272 // _point_, returns 1 if inside, 0 if outside.
273 bool polygon_ctrl_impl::point_in_polygon(double tx, double ty) const
274 {
275 if(m_num_points < 3) return false;
276 if(!m_in_polygon_check) return false;
277
278 unsigned j;
279 int yflag0, yflag1, inside_flag;
280 double vtx0, vty0, vtx1, vty1;
281
282 vtx0 = xn(m_num_points - 1);
283 vty0 = yn(m_num_points - 1);
284
285 // get test bit for above/below X axis
286 yflag0 = (vty0 >= ty);
287
288 vtx1 = xn(0);
289 vty1 = yn(0);
290
291 inside_flag = 0;
292 for (j = 1; j <= m_num_points; ++j)
293 {
294 yflag1 = (vty1 >= ty);
295 // Check if endpoints straddle (are on opposite sides) of X axis
296 // (i.e. the Y's differ); if so, +X ray could intersect this edge.
297 // The old test also checked whether the endpoints are both to the
298 // right or to the left of the test point. However, given the faster
299 // intersection point computation used below, this test was found to
300 // be a break-even proposition for most polygons and a loser for
301 // triangles (where 50% or more of the edges which survive this test
302 // will cross quadrants and so have to have the X intersection computed
303 // anyway). I credit Joseph Samosky with inspiring me to try dropping
304 // the "both left or both right" part of my code.
305 if (yflag0 != yflag1)
306 {
307 // Check intersection of pgon segment with +X ray.
308 // Note if >= point's X; if so, the ray hits it.
309 // The division operation is avoided for the ">=" test by checking
310 // the sign of the first vertex wrto the test point; idea inspired
311 // by Joseph Samosky's and Mark Haigh-Hutchinson's different
312 // polygon inclusion tests.
313 if ( ((vty1-ty) * (vtx0-vtx1) >=
314 (vtx1-tx) * (vty0-vty1)) == yflag1 )
315 {
316 inside_flag ^= 1;
317 }
318 }
319
320 // Move to the next pair of vertices, retaining info as possible.
321 yflag0 = yflag1;
322 vtx0 = vtx1;
323 vty0 = vty1;
324
325 unsigned k = (j >= m_num_points) ? j - m_num_points : j;
326 vtx1 = xn(k);
327 vty1 = yn(k);
328 }
329 return inside_flag != 0;
330 }
331 }
332