1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 #ifndef AGG_BASICS_INCLUDED
17 #define AGG_BASICS_INCLUDED
19 #include "agg_config.h"
21 //-------------------------------------------------------- Default basic types
23 // If the compiler has different capacity of the basic types you can redefine
24 // them via the compiler command line or by generating agg_config.h that is
28 #define AGG_INT8 signed char
32 #define AGG_INT8U unsigned char
36 #define AGG_INT16 short
40 #define AGG_INT16U unsigned short
48 #define AGG_INT32U unsigned
53 #define AGG_INT64 signed __int64
55 #define AGG_INT64 signed long long
61 #define AGG_INT64U unsigned __int64
63 #define AGG_INT64U unsigned long long
67 //------------------------------------------------ Some fixes for MS Visual C++
69 #pragma warning(disable:4786) // Identifier was truncated...
73 #define AGG_INLINE __forceinline
75 #define AGG_INLINE inline
80 //-------------------------------------------------------------------------
81 typedef AGG_INT8 int8
; //----int8
82 typedef AGG_INT8U int8u
; //----int8u
83 typedef AGG_INT16 int16
; //----int16
84 typedef AGG_INT16U int16u
; //----int16u
85 typedef AGG_INT32 int32
; //----int32
86 typedef AGG_INT32U int32u
; //----int32u
87 typedef AGG_INT64 int64
; //----int64
88 typedef AGG_INT64U int64u
; //----int64u
90 //-------------------------------------------------------------------------
91 typedef unsigned char cover_type
; //----cover_type
94 cover_shift
= 8, //----cover_shift
95 cover_size
= 1 << cover_shift
, //----cover_size
96 cover_mask
= cover_size
- 1, //----cover_mask
97 cover_none
= 0, //----cover_none
98 cover_full
= cover_mask
//----cover_full
102 //-----------------------------------------------------------------------pi
103 const double pi
= 3.14159265358979323846;
105 //------------------------------------------------------------------deg2rad
106 inline double deg2rad(double deg
)
108 return deg
* pi
/ 180.0;
111 //------------------------------------------------------------------rad2deg
112 inline double rad2deg(double rad
)
114 return rad
* 180.0 / pi
;
117 //----------------------------------------------------------------rect_base
118 template<class T
> struct rect_base
120 typedef rect_base
<T
> self_type
;
127 rect_base(T x1_
, T y1_
, T x2_
, T y2_
) :
128 x1(x1_
), y1(y1_
), x2(x2_
), y2(y2_
) {}
130 const self_type
& normalize()
133 if(x1
> x2
) { t
= x1
; x1
= x2
; x2
= t
; }
134 if(y1
> y2
) { t
= y1
; y1
= y2
; y2
= t
; }
138 bool clip(const self_type
& r
)
140 if(x2
> r
.x2
) x2
= r
.x2
;
141 if(y2
> r
.y2
) y2
= r
.y2
;
142 if(x1
< r
.x1
) x1
= r
.x1
;
143 if(y1
< r
.y1
) y1
= r
.y1
;
144 return x1
<= x2
&& y1
<= y2
;
147 bool is_valid() const
149 return x1
<= x2
&& y1
<= y2
;
153 //-----------------------------------------------------intersect_rectangles
155 inline Rect
intersect_rectangles(const Rect
& r1
, const Rect
& r2
)
159 // First process x2,y2 because the other order
160 // results in Internal Compiler Error under
161 // Microsoft Visual C++ .NET 2003 69462-335-0000007-18038 in
162 // case of "Maximize Speed" optimization option.
164 if(r
.x2
> r2
.x2
) r
.x2
= r2
.x2
;
165 if(r
.y2
> r2
.y2
) r
.y2
= r2
.y2
;
166 if(r
.x1
< r2
.x1
) r
.x1
= r2
.x1
;
167 if(r
.y1
< r2
.y1
) r
.y1
= r2
.y1
;
172 //---------------------------------------------------------unite_rectangles
174 inline Rect
unite_rectangles(const Rect
& r1
, const Rect
& r2
)
177 if(r
.x2
< r2
.x2
) r
.x2
= r2
.x2
;
178 if(r
.y2
< r2
.y2
) r
.y2
= r2
.y2
;
179 if(r
.x1
> r2
.x1
) r
.x1
= r2
.x1
;
180 if(r
.y1
> r2
.y1
) r
.y1
= r2
.y1
;
184 typedef rect_base
<int> rect
; //----rect
185 typedef rect_base
<double> rect_d
; //----rect_d
187 //---------------------------------------------------------path_commands_e
190 path_cmd_stop
= 0, //----path_cmd_stop
191 path_cmd_move_to
= 1, //----path_cmd_move_to
192 path_cmd_line_to
= 2, //----path_cmd_line_to
193 path_cmd_curve3
= 3, //----path_cmd_curve3
194 path_cmd_curve4
= 4, //----path_cmd_curve4
195 path_cmd_end_poly
= 6, //----path_cmd_end_poly
196 path_cmd_mask
= 0x0F //----path_cmd_mask
199 //------------------------------------------------------------path_flags_e
202 path_flags_none
= 0, //----path_flags_none
203 path_flags_ccw
= 0x10, //----path_flags_ccw
204 path_flags_cw
= 0x20, //----path_flags_cw
205 path_flags_close
= 0x40, //----path_flags_close
206 path_flags_mask
= 0xF0 //----path_flags_mask
209 //---------------------------------------------------------------is_vertex
210 inline bool is_vertex(unsigned c
)
212 return c
>= path_cmd_move_to
&& c
< path_cmd_end_poly
;
215 //-----------------------------------------------------------------is_stop
216 inline bool is_stop(unsigned c
)
218 return c
== path_cmd_stop
;
221 //--------------------------------------------------------------is_move_to
222 inline bool is_move_to(unsigned c
)
224 return c
== path_cmd_move_to
;
227 //--------------------------------------------------------------is_line_to
228 inline bool is_line_to(unsigned c
)
230 return c
== path_cmd_line_to
;
233 //----------------------------------------------------------------is_curve
234 inline bool is_curve(unsigned c
)
236 return c
== path_cmd_curve3
|| c
== path_cmd_curve4
;
239 //---------------------------------------------------------------is_curve3
240 inline bool is_curve3(unsigned c
)
242 return c
== path_cmd_curve3
;
245 //---------------------------------------------------------------is_curve4
246 inline bool is_curve4(unsigned c
)
248 return c
== path_cmd_curve4
;
251 //-------------------------------------------------------------is_end_poly
252 inline bool is_end_poly(unsigned c
)
254 return (c
& path_cmd_mask
) == path_cmd_end_poly
;
257 //----------------------------------------------------------------is_close
258 inline bool is_close(unsigned c
)
260 return (c
& ~(path_flags_cw
| path_flags_ccw
)) ==
261 (((bool)path_cmd_end_poly
) | ((bool)path_flags_close
));
264 //------------------------------------------------------------is_next_poly
265 inline bool is_next_poly(unsigned c
)
267 return is_stop(c
) || is_move_to(c
) || is_end_poly(c
);
270 //-------------------------------------------------------------------is_cw
271 inline bool is_cw(unsigned c
)
273 return (c
& path_flags_cw
) != 0;
276 //------------------------------------------------------------------is_ccw
277 inline bool is_ccw(unsigned c
)
279 return (c
& path_flags_ccw
) != 0;
282 //-------------------------------------------------------------is_oriented
283 inline bool is_oriented(unsigned c
)
285 return (c
& (path_flags_cw
| path_flags_ccw
)) != 0;
288 //---------------------------------------------------------------is_closed
289 inline bool is_closed(unsigned c
)
291 return (c
& path_flags_close
) != 0;
294 //----------------------------------------------------------get_close_flag
295 inline unsigned get_close_flag(unsigned c
)
297 return c
& path_flags_close
;
300 //-------------------------------------------------------clear_orientation
301 inline unsigned clear_orientation(unsigned c
)
303 return c
& ~(path_flags_cw
| path_flags_ccw
);
306 //---------------------------------------------------------get_orientation
307 inline unsigned get_orientation(unsigned c
)
309 return c
& (path_flags_cw
| path_flags_ccw
);
312 //---------------------------------------------------------set_orientation
313 inline unsigned set_orientation(unsigned c
, unsigned o
)
315 return clear_orientation(c
) | o
;
318 //--------------------------------------------------------------point_type
324 point_type(double x_
, double y_
) : x(x_
), y(y_
) {}
327 //-------------------------------------------------------------vertex_type
334 vertex_type(double x_
, double y_
, unsigned cmd_
) :
335 x(x_
), y(y_
), cmd(cmd_
) {}