• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //----------------------------------------------------------------------------
3 // Anti-Grain Geometry - Version 2.3
4 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
5 //
6 // Permission to copy, use, modify, sell and distribute this software
7 // is granted provided this copyright notice appears in all copies.
8 // This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
10 //
11 //----------------------------------------------------------------------------
12 // Contact: mcseem@antigrain.com
13 //          mcseemagg@yahoo.com
14 //          http://www.antigrain.com
15 //----------------------------------------------------------------------------
16 //
17 // Stroke math
18 //
19 //----------------------------------------------------------------------------
20 #ifndef AGG_STROKE_MATH_INCLUDED
21 #define AGG_STROKE_MATH_INCLUDED
22 #include "agg_math.h"
23 #include "agg_vertex_sequence.h"
24 namespace agg
25 {
26 enum line_cap_e {
27     butt_cap,
28     square_cap,
29     round_cap
30 };
31 enum line_join_e {
32     miter_join         = 0,
33     miter_join_revert  = 1,
34     miter_join_round   = 4,
35     round_join         = 2,
36     bevel_join         = 3
37 };
38 enum inner_join_e {
39     inner_bevel,
40     inner_miter,
41     inner_jag,
42     inner_round
43 };
44 const FX_FLOAT stroke_theta = 1.0f / 1000.0f;
45 template<class VertexConsumer>
stroke_calc_arc(VertexConsumer & out_vertices,FX_FLOAT x,FX_FLOAT y,FX_FLOAT dx1,FX_FLOAT dy1,FX_FLOAT dx2,FX_FLOAT dy2,FX_FLOAT width,FX_FLOAT approximation_scale)46 void stroke_calc_arc(VertexConsumer& out_vertices,
47                      FX_FLOAT x,   FX_FLOAT y,
48                      FX_FLOAT dx1, FX_FLOAT dy1,
49                      FX_FLOAT dx2, FX_FLOAT dy2,
50                      FX_FLOAT width,
51                      FX_FLOAT approximation_scale)
52 {
53     typedef typename VertexConsumer::value_type coord_type;
54     FX_FLOAT a1 = FXSYS_atan2(dy1, dx1);
55     FX_FLOAT a2 = FXSYS_atan2(dy2, dx2);
56     FX_FLOAT da = a1 - a2;
57     bool ccw = da > 0 && da < FX_PI;
58     if(width < 0) {
59         width = -width;
60     }
61     da = FXSYS_acos(width / (width + ((1.0f / 8) / approximation_scale))) * 2;
62     out_vertices.add(coord_type(x + dx1, y + dy1));
63     if (da > 0) {
64       if (!ccw) {
65         if (a1 > a2) {
66           a2 += 2 * FX_PI;
67         }
68         a2 -= da / 4;
69         a1 += da;
70         while (a1 < a2) {
71           out_vertices.add(coord_type(x + (width * FXSYS_cos(a1)),
72                                       y + (width * FXSYS_sin(a1))));
73           a1 += da;
74         }
75       } else {
76         if (a1 < a2) {
77           a2 -= 2 * FX_PI;
78         }
79         a2 += da / 4;
80         a1 -= da;
81         while (a1 > a2) {
82           out_vertices.add(coord_type(x + (width * FXSYS_cos(a1)),
83                                       y + (width * FXSYS_sin(a1))));
84           a1 -= da;
85         }
86       }
87     }
88     out_vertices.add(coord_type(x + dx2, y + dy2));
89 }
90 template<class VertexConsumer>
stroke_calc_miter(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,const vertex_dist & v2,FX_FLOAT dx1,FX_FLOAT dy1,FX_FLOAT dx2,FX_FLOAT dy2,FX_FLOAT width,line_join_e line_join,FX_FLOAT miter_limit,FX_FLOAT approximation_scale)91 void stroke_calc_miter(VertexConsumer& out_vertices,
92                        const vertex_dist& v0,
93                        const vertex_dist& v1,
94                        const vertex_dist& v2,
95                        FX_FLOAT dx1, FX_FLOAT dy1,
96                        FX_FLOAT dx2, FX_FLOAT dy2,
97                        FX_FLOAT width,
98                        line_join_e line_join,
99                        FX_FLOAT miter_limit,
100                        FX_FLOAT approximation_scale)
101 {
102     typedef typename VertexConsumer::value_type coord_type;
103     FX_FLOAT xi = v1.x;
104     FX_FLOAT yi = v1.y;
105     bool miter_limit_exceeded = true;
106     if(calc_intersection(v0.x + dx1, v0.y - dy1,
107                          v1.x + dx1, v1.y - dy1,
108                          v1.x + dx2, v1.y - dy2,
109                          v2.x + dx2, v2.y - dy2,
110                          &xi, &yi)) {
111         FX_FLOAT d1 = calc_distance(v1.x, v1.y, xi, yi);
112         FX_FLOAT lim = width * miter_limit;
113         if(d1 <= lim) {
114             out_vertices.add(coord_type(xi, yi));
115             miter_limit_exceeded = false;
116         }
117     } else {
118         FX_FLOAT x2 = v1.x + dx1;
119         FX_FLOAT y2 = v1.y - dy1;
120         if ((((x2 - v0.x) * dy1) - ((v0.y - y2) * dx1) < 0) !=
121             (((x2 - v2.x) * dy1) - ((v2.y - y2) * dx1) < 0)) {
122             out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
123             miter_limit_exceeded = false;
124         }
125     }
126     if(miter_limit_exceeded) {
127         switch(line_join) {
128             case miter_join_revert:
129                 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
130                 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
131                 break;
132             case miter_join_round:
133                 stroke_calc_arc(out_vertices,
134                                 v1.x, v1.y, dx1, -dy1, dx2, -dy2,
135                                 width, approximation_scale);
136                 break;
137             default:
138               out_vertices.add(coord_type(v1.x + dx1 + (dy1 * miter_limit),
139                                           v1.y - dy1 + (dx1 * miter_limit)));
140               out_vertices.add(coord_type(v1.x + dx2 - (dy2 * miter_limit),
141                                           v1.y - dy2 - (dx2 * miter_limit)));
142                 break;
143         }
144     }
145 }
146 template<class VertexConsumer>
stroke_calc_cap(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,FX_FLOAT len,line_cap_e line_cap,FX_FLOAT width,FX_FLOAT approximation_scale)147 void stroke_calc_cap(VertexConsumer& out_vertices,
148                      const vertex_dist& v0,
149                      const vertex_dist& v1,
150                      FX_FLOAT len,
151                      line_cap_e line_cap,
152                      FX_FLOAT width,
153                      FX_FLOAT approximation_scale)
154 {
155     typedef typename VertexConsumer::value_type coord_type;
156     out_vertices.remove_all();
157     FX_FLOAT dx1 = (v1.y - v0.y) / len;
158     FX_FLOAT dy1 = (v1.x - v0.x) / len;
159     FX_FLOAT dx2 = 0;
160     FX_FLOAT dy2 = 0;
161     dx1 = dx1 * width;
162     dy1 = dy1 * width;
163     if(line_cap != round_cap) {
164         if(line_cap == square_cap) {
165             dx2 = dy1;
166             dy2 = dx1;
167         }
168         out_vertices.add(coord_type(v0.x - dx1 - dx2, v0.y + dy1 - dy2));
169         out_vertices.add(coord_type(v0.x + dx1 - dx2, v0.y - dy1 - dy2));
170     } else {
171         FX_FLOAT a1 = FXSYS_atan2(dy1, -dx1);
172         FX_FLOAT a2 = a1 + FX_PI;
173         FX_FLOAT da =
174             FXSYS_acos(width / (width + ((1.0f / 8) / approximation_scale))) *
175             2;
176         out_vertices.add(coord_type(v0.x - dx1, v0.y + dy1));
177         a1 += da;
178         a2 -= da / 4;
179         while(a1 < a2) {
180           out_vertices.add(coord_type(v0.x + (width * FXSYS_cos(a1)),
181                                       v0.y + (width * FXSYS_sin(a1))));
182             a1 += da;
183         }
184         out_vertices.add(coord_type(v0.x + dx1, v0.y - dy1));
185     }
186 }
187 template<class VertexConsumer>
stroke_calc_join(VertexConsumer & out_vertices,const vertex_dist & v0,const vertex_dist & v1,const vertex_dist & v2,FX_FLOAT len1,FX_FLOAT len2,FX_FLOAT width,line_join_e line_join,inner_join_e inner_join,FX_FLOAT miter_limit,FX_FLOAT inner_miter_limit,FX_FLOAT approximation_scale)188 void stroke_calc_join(VertexConsumer& out_vertices,
189                       const vertex_dist& v0,
190                       const vertex_dist& v1,
191                       const vertex_dist& v2,
192                       FX_FLOAT len1,
193                       FX_FLOAT len2,
194                       FX_FLOAT width,
195                       line_join_e line_join,
196                       inner_join_e inner_join,
197                       FX_FLOAT miter_limit,
198                       FX_FLOAT inner_miter_limit,
199                       FX_FLOAT approximation_scale)
200 {
201     typedef typename VertexConsumer::value_type coord_type;
202     FX_FLOAT dx1, dy1, dx2, dy2;
203     dx1 = width * (v1.y - v0.y) / len1;
204     dy1 = width * (v1.x - v0.x) / len1;
205     dx2 = width * (v2.y - v1.y) / len2;
206     dy2 = width * (v2.x - v1.x) / len2;
207     out_vertices.remove_all();
208     if(calc_point_location(v0.x, v0.y, v1.x, v1.y, v2.x, v2.y) > 0) {
209         switch(inner_join) {
210             default:
211                 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
212                 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
213                 break;
214             case inner_miter:
215                 stroke_calc_miter(out_vertices,
216                                   v0, v1, v2, dx1, dy1, dx2, dy2,
217                                   width,
218                                   miter_join_revert,
219                                   inner_miter_limit,
220                                   1.0f);
221                 break;
222             case inner_jag:
223             case inner_round: {
224                     FX_FLOAT d = (dx1 - dx2) * (dx1 - dx2) + (dy1 - dy2) * (dy1 - dy2);
225                     if(d < len1 * len1 && d < len2 * len2) {
226                         stroke_calc_miter(out_vertices,
227                                           v0, v1, v2, dx1, dy1, dx2, dy2,
228                                           width,
229                                           miter_join_revert,
230                                           inner_miter_limit,
231                                           1.0f);
232                     } else {
233                         if(inner_join == inner_jag) {
234                             out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
235                             out_vertices.add(coord_type(v1.x,       v1.y      ));
236                             out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
237                         } else {
238                             out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
239                             out_vertices.add(coord_type(v1.x,       v1.y      ));
240                             stroke_calc_arc(out_vertices,
241                                             v1.x, v1.y, dx2, -dy2, dx1, -dy1,
242                                             width, approximation_scale);
243                             out_vertices.add(coord_type(v1.x,       v1.y      ));
244                             out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
245                         }
246                     }
247                 }
248                 break;
249         }
250     } else {
251         switch(line_join) {
252             case miter_join:
253             case miter_join_revert:
254             case miter_join_round:
255                 stroke_calc_miter(out_vertices,
256                                   v0, v1, v2, dx1, dy1, dx2, dy2,
257                                   width,
258                                   line_join,
259                                   miter_limit,
260                                   approximation_scale);
261                 break;
262             case round_join:
263                 stroke_calc_arc(out_vertices,
264                                 v1.x, v1.y, dx1, -dy1, dx2, -dy2,
265                                 width, approximation_scale);
266                 break;
267             default:
268                 out_vertices.add(coord_type(v1.x + dx1, v1.y - dy1));
269                 out_vertices.add(coord_type(v1.x + dx2, v1.y - dy2));
270                 break;
271         }
272     }
273 }
274 }
275 #endif
276