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 #ifndef AGG_BASICS_INCLUDED
17 #define AGG_BASICS_INCLUDED
18 #ifndef AGG_INT8
19 #define AGG_INT8 signed char
20 #endif
21 #ifndef AGG_INT8U
22 #define AGG_INT8U unsigned char
23 #endif
24 #ifndef AGG_INT16
25 #define AGG_INT16 short
26 #endif
27 #ifndef AGG_INT16U
28 #define AGG_INT16U unsigned short
29 #endif
30 #ifndef AGG_INT32
31 #define AGG_INT32 int
32 #endif
33 #ifndef AGG_INT32U
34 #define AGG_INT32U unsigned
35 #endif
36 #ifndef AGG_INT64
37 #define AGG_INT64 signed long long
38 #endif
39 #ifndef AGG_INT64U
40 #define AGG_INT64U unsigned long long
41 #endif
42 #define AGG_INLINE inline
43 namespace agg
44 {
45 typedef AGG_INT8 int8;
46 typedef AGG_INT8U int8u;
47 typedef AGG_INT16 int16;
48 typedef AGG_INT16U int16u;
49 typedef AGG_INT32 int32;
50 typedef AGG_INT32U int32u;
51 typedef AGG_INT64 int64;
52 typedef AGG_INT64U int64u;
53 typedef unsigned char cover_type;
54 enum cover_scale_e {
55 cover_shift = 8,
56 cover_size = 1 << cover_shift,
57 cover_mask = cover_size - 1,
58 cover_none = 0,
59 cover_full = cover_mask
60 };
61 template<class T> struct rect_base {
62 typedef rect_base<T> self_type;
63 T x1;
64 T y1;
65 T x2;
66 T y2;
rect_baserect_base67 rect_base() {}
rect_baserect_base68 rect_base(T x1_, T y1_, T x2_, T y2_) :
69 x1(x1_), y1(y1_), x2(x2_), y2(y2_) {}
normalizerect_base70 const self_type& normalize()
71 {
72 T t;
73 if(x1 > x2) {
74 t = x1;
75 x1 = x2;
76 x2 = t;
77 }
78 if(y1 > y2) {
79 t = y1;
80 y1 = y2;
81 y2 = t;
82 }
83 return *this;
84 }
cliprect_base85 bool clip(const self_type& r)
86 {
87 if(x2 > r.x2) {
88 x2 = r.x2;
89 }
90 if(y2 > r.y2) {
91 y2 = r.y2;
92 }
93 if(x1 < r.x1) {
94 x1 = r.x1;
95 }
96 if(y1 < r.y1) {
97 y1 = r.y1;
98 }
99 return x1 <= x2 && y1 <= y2;
100 }
is_validrect_base101 bool is_valid() const
102 {
103 return x1 <= x2 && y1 <= y2;
104 }
105 };
106 template<class Rect>
intersect_rectangles(const Rect & r1,const Rect & r2)107 inline Rect intersect_rectangles(const Rect& r1, const Rect& r2)
108 {
109 Rect r = r1;
110 if(r.x2 > r2.x2) {
111 r.x2 = r2.x2;
112 }
113 if(r.y2 > r2.y2) {
114 r.y2 = r2.y2;
115 }
116 if(r.x1 < r2.x1) {
117 r.x1 = r2.x1;
118 }
119 if(r.y1 < r2.y1) {
120 r.y1 = r2.y1;
121 }
122 return r;
123 }
124 template<class Rect>
unite_rectangles(const Rect & r1,const Rect & r2)125 inline Rect unite_rectangles(const Rect& r1, const Rect& r2)
126 {
127 Rect r = r1;
128 if(r.x2 < r2.x2) {
129 r.x2 = r2.x2;
130 }
131 if(r.y2 < r2.y2) {
132 r.y2 = r2.y2;
133 }
134 if(r.x1 > r2.x1) {
135 r.x1 = r2.x1;
136 }
137 if(r.y1 > r2.y1) {
138 r.y1 = r2.y1;
139 }
140 return r;
141 }
142 typedef rect_base<int> rect;
143 typedef rect_base<FX_FLOAT> rect_d;
144 enum path_commands_e {
145 path_cmd_stop = 0,
146 path_cmd_move_to = 1,
147 path_cmd_line_to = 2,
148 path_cmd_curve3 = 3,
149 path_cmd_curve4 = 4,
150 path_cmd_curveN = 5,
151 path_cmd_catrom = 6,
152 path_cmd_ubspline = 7,
153 path_cmd_end_poly = 0x0F,
154 path_cmd_mask = 0x0F
155 };
156 enum path_flags_e {
157 path_flags_none = 0,
158 path_flags_ccw = 0x10,
159 path_flags_cw = 0x20,
160 path_flags_close = 0x40,
161 path_flags_jr = 0x80,
162 path_flags_mask = 0xF0
163 };
is_vertex(unsigned c)164 inline bool is_vertex(unsigned c)
165 {
166 c &= ~path_flags_jr;
167 return c >= path_cmd_move_to && c < path_cmd_end_poly;
168 }
is_drawing(unsigned c)169 inline bool is_drawing(unsigned c)
170 {
171 c &= ~path_flags_jr;
172 return c >= path_cmd_line_to && c < path_cmd_end_poly;
173 }
is_stop(unsigned c)174 inline bool is_stop(unsigned c)
175 {
176 c &= ~path_flags_jr;
177 return c == path_cmd_stop;
178 }
is_move_to(unsigned c)179 inline bool is_move_to(unsigned c)
180 {
181 c &= ~path_flags_jr;
182 return c == path_cmd_move_to;
183 }
is_line_to(unsigned c)184 inline bool is_line_to(unsigned c)
185 {
186 c &= ~path_flags_jr;
187 return c == path_cmd_line_to;
188 }
is_curve(unsigned c)189 inline bool is_curve(unsigned c)
190 {
191 c &= ~path_flags_jr;
192 return c == path_cmd_curve3 || c == path_cmd_curve4;
193 }
is_curve3(unsigned c)194 inline bool is_curve3(unsigned c)
195 {
196 c &= ~path_flags_jr;
197 return c == path_cmd_curve3;
198 }
is_curve4(unsigned c)199 inline bool is_curve4(unsigned c)
200 {
201 c &= ~path_flags_jr;
202 return c == path_cmd_curve4;
203 }
is_end_poly(unsigned c)204 inline bool is_end_poly(unsigned c)
205 {
206 c &= ~path_flags_jr;
207 return (c & path_cmd_mask) == path_cmd_end_poly;
208 }
is_close(unsigned c)209 inline bool is_close(unsigned c)
210 {
211 c &= ~path_flags_jr;
212 return (c & ~(path_flags_cw | path_flags_ccw)) ==
213 (path_cmd_end_poly | path_flags_close);
214 }
is_next_poly(unsigned c)215 inline bool is_next_poly(unsigned c)
216 {
217 c &= ~path_flags_jr;
218 return is_stop(c) || is_move_to(c) || is_end_poly(c);
219 }
is_cw(unsigned c)220 inline bool is_cw(unsigned c)
221 {
222 c &= ~path_flags_jr;
223 return (c & path_flags_cw) != 0;
224 }
is_ccw(unsigned c)225 inline bool is_ccw(unsigned c)
226 {
227 c &= ~path_flags_jr;
228 return (c & path_flags_ccw) != 0;
229 }
is_oriented(unsigned c)230 inline bool is_oriented(unsigned c)
231 {
232 c &= ~path_flags_jr;
233 return (c & (path_flags_cw | path_flags_ccw)) != 0;
234 }
is_closed(unsigned c)235 inline bool is_closed(unsigned c)
236 {
237 c &= ~path_flags_jr;
238 return (c & path_flags_close) != 0;
239 }
get_close_flag(unsigned c)240 inline unsigned get_close_flag(unsigned c)
241 {
242 c &= ~path_flags_jr;
243 return c & path_flags_close;
244 }
clear_orientation(unsigned c)245 inline unsigned clear_orientation(unsigned c)
246 {
247 c &= ~path_flags_jr;
248 return c & ~(path_flags_cw | path_flags_ccw);
249 }
get_orientation(unsigned c)250 inline unsigned get_orientation(unsigned c)
251 {
252 c &= ~path_flags_jr;
253 return c & (path_flags_cw | path_flags_ccw);
254 }
set_orientation(unsigned c,unsigned o)255 inline unsigned set_orientation(unsigned c, unsigned o)
256 {
257 c &= ~path_flags_jr;
258 return clear_orientation(c) | o;
259 }
260 struct point_type {
261 FX_FLOAT x, y;
262 unsigned flag;
point_typepoint_type263 point_type() {}
xpoint_type264 point_type(FX_FLOAT x_, FX_FLOAT y_, unsigned flag_ = 0) : x(x_), y(y_), flag(flag_) {}
265 };
266 struct point_type_flag : public point_type {
267 unsigned flag;
point_type_flagpoint_type_flag268 point_type_flag()
269 {
270 flag = 0;
271 }
point_typepoint_type_flag272 point_type_flag(FX_FLOAT x_, FX_FLOAT y_, unsigned flag_ = 0) : point_type(x_, y_), flag(flag_) {}
273 };
274 struct vertex_type {
275 FX_FLOAT x, y;
276 unsigned cmd;
vertex_typevertex_type277 vertex_type() {}
vertex_typevertex_type278 vertex_type(FX_FLOAT x_, FX_FLOAT y_, unsigned cmd_) :
279 x(x_), y(y_), cmd(cmd_) {}
280 };
281 }
282 #endif
283