xref: /haiku/headers/libs/agg/agg_basics.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.2
3 // Copyright (C) 2002-2004 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: mcseem@antigrain.com
12 //          mcseemagg@yahoo.com
13 //          http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 
16 #ifndef AGG_BASICS_INCLUDED
17 #define AGG_BASICS_INCLUDED
18 
19 namespace agg
20 {
21     //-------------------------------------------------------------------------
22     typedef signed char    int8;         //----int8
23     typedef unsigned char  int8u;        //----int8u
24     typedef signed short   int16;        //----int16
25     typedef unsigned short int16u;       //----int16u
26     typedef signed int     int32;        //----int32
27     typedef unsigned int   int32u;       //----int32u
28 
29 
30     //-------------------------------------------------------------------------
31     typedef unsigned char cover_type;    //----cover_type
32     enum
33     {
34         cover_shift = 8,                 //----cover_shift
35         cover_size  = 1 << cover_shift,  //----cover_size
36         cover_mask  = cover_size - 1,    //----cover_mask
37         cover_none  = 0,                 //----cover_none
38         cover_full  = cover_mask         //----cover_full
39     };
40 
41 
42     //-----------------------------------------------------------------------pi
43     const double pi = 3.14159265358979323846;
44 
45     //------------------------------------------------------------------deg2rad
46     inline double deg2rad(double deg)
47     {
48         return deg * pi / 180.0;
49     }
50 
51     //------------------------------------------------------------------rad2deg
52     inline double rad2deg(double rad)
53     {
54         return rad * 180.0 / pi;
55     }
56 
57     //----------------------------------------------------------------rect_base
58     template<class T> struct rect_base
59     {
60         typedef rect_base<T> self_type;
61         T x1;
62         T y1;
63         T x2;
64         T y2;
65 
66         rect_base() {}
67         rect_base(T x1_, T y1_, T x2_, T y2_) :
68             x1(x1_), y1(y1_), x2(x2_), y2(y2_) {}
69 
70         const self_type& normalize()
71         {
72             T t;
73             if(x1 > x2) { t = x1; x1 = x2; x2 = t; }
74             if(y1 > y2) { t = y1; y1 = y2; y2 = t; }
75             return *this;
76         }
77 
78         bool clip(const self_type& r)
79         {
80             if(x2 > r.x2) x2 = r.x2;
81             if(y2 > r.y2) y2 = r.y2;
82             if(x1 < r.x1) x1 = r.x1;
83             if(y1 < r.y1) y1 = r.y1;
84             return x1 <= x2 && y1 <= y2;
85         }
86 
87         bool is_valid() const
88         {
89             return x1 <= x2 && y1 <= y2;
90         }
91     };
92 
93     //-----------------------------------------------------intersect_rectangles
94     template<class Rect>
95     inline Rect intersect_rectangles(const Rect& r1, const Rect& r2)
96     {
97         Rect r = r1;
98 
99         // First process x2,y2 because the other order
100         // results in Internal Compiler Error under
101         // Microsoft Visual C++ .NET 2003 69462-335-0000007-18038 in
102         // case of "Maximize Speed" optimization option.
103         //-----------------
104         if(r.x2 > r2.x2) r.x2 = r2.x2;
105         if(r.y2 > r2.y2) r.y2 = r2.y2;
106         if(r.x1 < r2.x1) r.x1 = r2.x1;
107         if(r.y1 < r2.y1) r.y1 = r2.y1;
108         return r;
109     }
110 
111 
112     //---------------------------------------------------------unite_rectangles
113     template<class Rect>
114     inline Rect unite_rectangles(const Rect& r1, const Rect& r2)
115     {
116         Rect r = r1;
117         if(r.x2 < r2.x2) r.x2 = r2.x2;
118         if(r.y2 < r2.y2) r.y2 = r2.y2;
119         if(r.x1 > r2.x1) r.x1 = r2.x1;
120         if(r.y1 > r2.y1) r.y1 = r2.y1;
121         return r;
122     }
123 
124     typedef rect_base<int>    rect;   //----rect
125     typedef rect_base<double> rect_d; //----rect_d
126 
127     //---------------------------------------------------------path_commands_e
128     enum path_commands_e
129     {
130         path_cmd_stop     = 0,        //----path_cmd_stop
131         path_cmd_move_to  = 1,        //----path_cmd_move_to
132         path_cmd_line_to  = 2,        //----path_cmd_line_to
133         path_cmd_curve3   = 3,        //----path_cmd_curve3
134         path_cmd_curve4   = 4,        //----path_cmd_curve4
135         path_cmd_end_poly = 6,        //----path_cmd_end_poly
136         path_cmd_mask     = 0x0F      //----path_cmd_mask
137     };
138 
139     //------------------------------------------------------------path_flags_e
140     enum path_flags_e
141     {
142         path_flags_none  = 0,         //----path_flags_none
143         path_flags_ccw   = 0x10,      //----path_flags_ccw
144         path_flags_cw    = 0x20,      //----path_flags_cw
145         path_flags_close = 0x40,      //----path_flags_close
146         path_flags_mask  = 0xF0       //----path_flags_mask
147     };
148 
149     //---------------------------------------------------------------is_vertex
150     inline bool is_vertex(unsigned c)
151     {
152         return c >= path_cmd_move_to && c < path_cmd_end_poly;
153     }
154 
155     //-----------------------------------------------------------------is_stop
156     inline bool is_stop(unsigned c)
157     {
158         return c == path_cmd_stop;
159     }
160 
161     //--------------------------------------------------------------is_move_to
162     inline bool is_move_to(unsigned c)
163     {
164         return c == path_cmd_move_to;
165     }
166 
167     //--------------------------------------------------------------is_line_to
168     inline bool is_line_to(unsigned c)
169     {
170         return c == path_cmd_line_to;
171     }
172 
173     //----------------------------------------------------------------is_curve
174     inline bool is_curve(unsigned c)
175     {
176         return c == path_cmd_curve3 || c == path_cmd_curve4;
177     }
178 
179     //---------------------------------------------------------------is_curve3
180     inline bool is_curve3(unsigned c)
181     {
182         return c == path_cmd_curve3;
183     }
184 
185     //---------------------------------------------------------------is_curve4
186     inline bool is_curve4(unsigned c)
187     {
188         return c == path_cmd_curve4;
189     }
190 
191     //-------------------------------------------------------------is_end_poly
192     inline bool is_end_poly(unsigned c)
193     {
194         return (c & path_cmd_mask) == path_cmd_end_poly;
195     }
196 
197     //----------------------------------------------------------------is_close
198     inline bool is_close(unsigned c)
199     {
200         return (c & ~(path_flags_cw | path_flags_ccw)) ==
201                (path_cmd_end_poly | path_flags_close);
202     }
203 
204     //------------------------------------------------------------is_next_poly
205     inline bool is_next_poly(unsigned c)
206     {
207         return is_stop(c) || is_move_to(c) || is_end_poly(c);
208     }
209 
210     //-------------------------------------------------------------------is_cw
211     inline bool is_cw(unsigned c)
212     {
213         return (c & path_flags_cw) != 0;
214     }
215 
216     //------------------------------------------------------------------is_ccw
217     inline bool is_ccw(unsigned c)
218     {
219         return (c & path_flags_ccw) != 0;
220     }
221 
222     //-------------------------------------------------------------is_oriented
223     inline bool is_oriented(unsigned c)
224     {
225         return (c & (path_flags_cw | path_flags_ccw)) != 0;
226     }
227 
228     //---------------------------------------------------------------is_closed
229     inline bool is_closed(unsigned c)
230     {
231         return (c & path_flags_close) != 0;
232     }
233 
234     //----------------------------------------------------------get_close_flag
235     inline unsigned get_close_flag(unsigned c)
236     {
237         return c & path_flags_close;
238     }
239 
240     //-------------------------------------------------------clear_orientation
241     inline unsigned clear_orientation(unsigned c)
242     {
243         return c & ~(path_flags_cw | path_flags_ccw);
244     }
245 
246     //---------------------------------------------------------get_orientation
247     inline unsigned get_orientation(unsigned c)
248     {
249         return c & (path_flags_cw | path_flags_ccw);
250     }
251 
252     //---------------------------------------------------------set_orientation
253     inline unsigned set_orientation(unsigned c, unsigned o)
254     {
255         return clear_orientation(c) | o;
256     }
257 
258     //--------------------------------------------------------------point_type
259     struct point_type
260     {
261         double x, y;
262 
263         point_type() {}
264         point_type(double x_, double y_) : x(x_), y(y_) {}
265     };
266 
267     //-------------------------------------------------------------vertex_type
268     struct vertex_type
269     {
270         double   x, y;
271         unsigned cmd;
272 
273         vertex_type() {}
274         vertex_type(double x_, double y_, unsigned cmd_) :
275             x(x_), y(y_), cmd(cmd_) {}
276     };
277 
278 
279 }
280 
281 
282 #endif
283 
284