1 /* Copyright (c) 2000, 2010 Oracle and/or its affiliates. All rights reserved.
2    Copyright (C) 2011 Monty Program Ab.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
16 
17 
18 #ifndef GCALC_SLICESCAN_INCLUDED
19 #define GCALC_SLICESCAN_INCLUDED
20 
21 #ifndef DBUG_OFF
22 // #define GCALC_CHECK_WITH_FLOAT
23 #else
24 #define GCALC_DBUG_OFF
25 #endif /*DBUG_OFF*/
26 
27 #ifndef GCALC_DBUG_OFF
28 #define GCALC_DBUG_PRINT(b) DBUG_PRINT("Gcalc", b)
29 #define GCALC_DBUG_ENTER(a) DBUG_ENTER("Gcalc " a)
30 #define GCALC_DBUG_RETURN(r) DBUG_RETURN(r)
31 #define GCALC_DBUG_VOID_RETURN DBUG_VOID_RETURN
32 #define GCALC_DBUG_ASSERT(r) DBUG_ASSERT(r)
33 #else
34 #define GCALC_DBUG_PRINT(b)     do {} while(0)
35 #define GCALC_DBUG_ENTER(a)     do {} while(0)
36 #define GCALC_DBUG_RETURN(r)    return (r)
37 #define GCALC_DBUG_VOID_RETURN  do {} while(0)
38 #define GCALC_DBUG_ASSERT(r)    do {} while(0)
39 #endif /*GCALC_DBUG_OFF*/
40 
41 #define GCALC_TERMINATED(state_var) (state_var && (*state_var))
42 #define GCALC_SET_TERMINATED(state_var, val) state_var= val
43 #define GCALC_DECL_TERMINATED_STATE(varname) \
44   volatile int *varname;
45 
46 /*
47   Gcalc_dyn_list class designed to manage long lists of same-size objects
48   with the possible efficiency.
49   It allocates fixed-size blocks of memory (blk_size specified at the time
50   of creation). When new object is added to the list, it occupies part of
51   this block until it's full. Then the new block is allocated.
52   Freed objects are chained to the m_free list, and if it's not empty, the
53   newly added object is taken from this list instead the block.
54 */
55 
56 class Gcalc_dyn_list
57 {
58 public:
59   class Item
60   {
61   public:
62     Item *next;
63   };
64 
65   Gcalc_dyn_list(size_t blk_size, size_t sizeof_item);
66   Gcalc_dyn_list(const Gcalc_dyn_list &dl);
67   ~Gcalc_dyn_list();
new_item()68   Item *new_item()
69   {
70     Item *result;
71     if (m_free)
72     {
73       result= m_free;
74       m_free= m_free->next;
75     }
76     else
77       result= alloc_new_blk();
78 
79     return result;
80   }
free_item(Item * item)81   inline void free_item(Item *item)
82   {
83     item->next= m_free;
84     m_free= item;
85   }
free_list(Item ** list,Item ** hook)86   inline void free_list(Item **list, Item **hook)
87   {
88     *hook= m_free;
89     m_free= *list;
90   }
91 
free_list(Item * list)92   void free_list(Item *list)
93   {
94     Item **hook= &list;
95     while (*hook)
96       hook= &(*hook)->next;
97     free_list(&list, hook);
98   }
99 
100   void reset();
101   void cleanup();
102 
103 protected:
104   size_t m_blk_size;
105   size_t m_sizeof_item;
106   unsigned int m_points_per_blk;
107   void *m_first_blk;
108   void **m_blk_hook;
109   Item *m_free;
110   Item *m_keep;
111 
112   Item *alloc_new_blk();
113   void format_blk(void* block);
ptr_add(Item * ptr,int n_items)114   inline Item *ptr_add(Item *ptr, int n_items)
115   {
116     return (Item *)(((char*)ptr) + n_items * m_sizeof_item);
117   }
118 };
119 
120 /* Internal Gcalc coordinates to provide the precise calculations */
121 
122 #define GCALC_DIG_BASE     1000000000
123 typedef uint32 gcalc_digit_t;
124 typedef unsigned long long gcalc_coord2;
125 typedef gcalc_digit_t Gcalc_internal_coord;
126 #define GCALC_COORD_BASE 2
127 #define GCALC_COORD_BASE2 4
128 #define GCALC_COORD_BASE3 6
129 #define GCALC_COORD_BASE4 8
130 #define GCALC_COORD_BASE5 10
131 
132 typedef gcalc_digit_t Gcalc_coord1[GCALC_COORD_BASE];
133 typedef gcalc_digit_t Gcalc_coord2[GCALC_COORD_BASE*2];
134 typedef gcalc_digit_t Gcalc_coord3[GCALC_COORD_BASE*3];
135 
136 
137 void gcalc_mul_coord(Gcalc_internal_coord *result, int result_len,
138                      const Gcalc_internal_coord *a, int a_len,
139                      const Gcalc_internal_coord *b, int b_len);
140 
141 void gcalc_add_coord(Gcalc_internal_coord *result, int result_len,
142                      const Gcalc_internal_coord *a,
143                      const Gcalc_internal_coord *b);
144 
145 void gcalc_sub_coord(Gcalc_internal_coord *result, int result_len,
146                      const Gcalc_internal_coord *a,
147                      const Gcalc_internal_coord *b);
148 
149 int gcalc_cmp_coord(const Gcalc_internal_coord *a,
150                     const Gcalc_internal_coord *b, int len);
151 
152 /* Internal coordinates declarations end. */
153 
154 
155 typedef uint gcalc_shape_info;
156 
157 /*
158   Gcalc_heap represents the 'dynamic list' of Info objects, that
159   contain information about vertexes of all the shapes that take
160   part in some spatial calculation. Can become quite long.
161   After filled, the list is usually sorted and then walked through
162   in the slicescan algorithm.
163   The Gcalc_heap and the algorithm can only operate with two
164   kinds of shapes - polygon and polyline. So all the spatial
165   objects should be represented as sets of these two.
166 */
167 
168 class Gcalc_heap : public Gcalc_dyn_list
169 {
170 public:
171   enum node_type
172   {
173     nt_shape_node,
174     nt_intersection,
175     nt_eq_node
176   };
177   class Info : public Gcalc_dyn_list::Item
178   {
179   public:
180     node_type type;
181     union
182     {
183       struct
184       {
185         /* nt_shape_node */
186         gcalc_shape_info shape;
187         Info *left;
188         Info *right;
189         double x,y;
190         Gcalc_coord1 ix, iy;
191         int top_node;
192       } shape;
193       struct
194       {
195         /* nt_intersection */
196         /* Line p1-p2 supposed to intersect line p3-p4 */
197         const Info *p1;
198         const Info *p2;
199         const Info *p3;
200         const Info *p4;
201         void *data;
202         int equal;
203       } intersection;
204       struct
205       {
206         /* nt_eq_node */
207         const Info *node;
208         void *data;
209       } eq;
210     } node;
211 
is_bottom()212     bool is_bottom() const
213       { GCALC_DBUG_ASSERT(type == nt_shape_node); return !node.shape.left; }
is_top()214     bool is_top() const
215       { GCALC_DBUG_ASSERT(type == nt_shape_node); return node.shape.top_node; }
is_single_node()216     bool is_single_node() const
217       { return is_bottom() && is_top(); }
218 
219     void calc_xy(double *x, double *y) const;
220     int equal_pi(const Info *pi) const;
221 #ifdef GCALC_CHECK_WITH_FLOAT
222     void calc_xy_ld(long double *x, long double *y) const;
223 #endif /*GCALC_CHECK_WITH_FLOAT*/
224 
get_next()225     Info *get_next() { return (Info *)next; }
get_next()226     const Info *get_next() const { return (const Info *)next; }
227   };
228 
229   Gcalc_heap(size_t blk_size=8192) :
Gcalc_dyn_list(blk_size,sizeof (Info))230     Gcalc_dyn_list(blk_size, sizeof(Info)),
231     m_hook(&m_first), m_n_points(0)
232   {}
233 
Gcalc_heap(const Gcalc_heap & gh)234   Gcalc_heap(const Gcalc_heap &gh) :
235     Gcalc_dyn_list(gh),
236     m_hook(&m_first), m_n_points(0)
237   {}
238 
239   void set_extent(double xmin, double xmax, double ymin, double ymax);
240   Info *new_point_info(double x, double y, gcalc_shape_info shape);
241   void free_point_info(Info *i, Gcalc_dyn_list::Item **i_hook);
242   Info *new_intersection(const Info *p1, const Info *p2,
243                          const Info *p3, const Info *p4);
244   void prepare_operation();
ready()245   inline bool ready() const { return m_hook == NULL; }
get_first()246   Info *get_first() { return (Info *)m_first; }
get_first()247   const Info *get_first() const { return (const Info *)m_first; }
get_last_hook()248   Gcalc_dyn_list::Item **get_last_hook() { return m_hook; }
249   void reset();
250 #ifdef GCALC_CHECK_WITH_FLOAT
251   long double get_double(const Gcalc_internal_coord *c) const;
252 #endif /*GCALC_CHECK_WITH_FLOAT*/
253   double coord_extent;
get_cur_hook()254   Gcalc_dyn_list::Item **get_cur_hook() { return m_hook; }
255 
256 private:
257   Gcalc_dyn_list::Item *m_first;
258   Gcalc_dyn_list::Item **m_hook;
259   int m_n_points;
260 };
261 
262 
263 /*
264   the spatial object has to be represented as a set of
265   simple polygones and polylines to be sent to the slicescan.
266 
267   Gcalc_shape_transporter class and his descendants are used to
268   simplify storing the information about the shape into necessary structures.
269   This base class only fills the Gcalc_heap with the information about
270   shapes and vertices.
271 
272   Normally the Gcalc_shape_transporter family object is sent as a parameter
273   to the 'get_shapes' method of an 'spatial' object so it can pass
274   the spatial information about itself. The virtual methods are
275   treating this data in a way the caller needs.
276 */
277 
278 class Gcalc_shape_transporter
279 {
280 private:
281   Gcalc_heap::Info *m_first;
282   Gcalc_heap::Info *m_prev;
283   Gcalc_dyn_list::Item **m_prev_hook;
284   int m_shape_started;
285   void int_complete();
286 protected:
287   Gcalc_heap *m_heap;
288   int int_single_point(gcalc_shape_info Info, double x, double y);
289   int int_add_point(gcalc_shape_info Info, double x, double y);
int_start_line()290   void int_start_line()
291   {
292     DBUG_ASSERT(!m_shape_started);
293     m_shape_started= 1;
294     m_first= m_prev= NULL;
295   }
int_complete_line()296   void int_complete_line()
297   {
298     DBUG_ASSERT(m_shape_started== 1);
299     int_complete();
300     m_shape_started= 0;
301   }
int_start_ring()302   void int_start_ring()
303   {
304     DBUG_ASSERT(m_shape_started== 2);
305     m_shape_started= 3;
306     m_first= m_prev= NULL;
307   }
int_complete_ring()308   void int_complete_ring()
309   {
310     DBUG_ASSERT(m_shape_started== 3);
311     int_complete();
312     m_shape_started= 2;
313   }
int_start_poly()314   void int_start_poly()
315   {
316     DBUG_ASSERT(!m_shape_started);
317     m_shape_started= 2;
318   }
int_complete_poly()319   void int_complete_poly()
320   {
321     DBUG_ASSERT(m_shape_started== 2);
322     m_shape_started= 0;
323   }
line_started()324   bool line_started() { return m_shape_started == 1; };
325 public:
Gcalc_shape_transporter(Gcalc_heap * heap)326   Gcalc_shape_transporter(Gcalc_heap *heap) :
327     m_shape_started(0), m_heap(heap) {}
328 
329   virtual int single_point(double x, double y)=0;
330   virtual int start_line()=0;
331   virtual int complete_line()=0;
332   virtual int start_poly()=0;
333   virtual int complete_poly()=0;
334   virtual int start_ring()=0;
335   virtual int complete_ring()=0;
336   virtual int add_point(double x, double y)=0;
start_collection(int n_objects)337   virtual int start_collection(int n_objects) { return 0; }
empty_shape()338   virtual int empty_shape() { return 0; }
start_simple_poly()339   int start_simple_poly()
340   {
341     return start_poly() || start_ring();
342   }
complete_simple_poly()343   int complete_simple_poly()
344   {
345     return complete_ring() || complete_poly();
346   }
~Gcalc_shape_transporter()347   virtual ~Gcalc_shape_transporter() {}
348 };
349 
350 
351 enum Gcalc_scan_events
352 {
353   scev_none= 0,
354   scev_point= 1,         /* Just a new point in thread */
355   scev_thread= 2,        /* Start of the new thread */
356   scev_two_threads= 4,   /* A couple of new threads started */
357   scev_intersection= 8,  /* Intersection happened */
358   scev_end= 16,          /* Single thread finished */
359   scev_two_ends= 32,     /* A couple of threads finished */
360   scev_single_point= 64  /* Got single point */
361 };
362 
363 
364 /*
365    Gcalc_scan_iterator incapsulates the slicescan algorithm.
366    It takes filled Gcalc_heap as a datasource. Then can be
367    iterated through the vertexes and intersection points with
368    the step() method. After the 'step()' one usually observes
369    the current 'slice' to do the necessary calculations, like
370    looking for intersections, calculating the area, whatever.
371 */
372 
373 class Gcalc_scan_iterator : public Gcalc_dyn_list
374 {
375 public:
376   class point : public Gcalc_dyn_list::Item
377   {
378   public:
379     Gcalc_coord1 dx;
380     Gcalc_coord1 dy;
381     Gcalc_heap::Info *pi;
382     Gcalc_heap::Info *next_pi;
383     Gcalc_heap::Info *ev_pi;
384     const Gcalc_coord1 *l_border;
385     const Gcalc_coord1 *r_border;
386     point *ev_next;
387 
388     Gcalc_scan_events event;
389 
c_get_next()390     inline const point *c_get_next() const
391       { return (const point *)next; }
is_bottom()392     inline bool is_bottom() const { return !next_pi; }
get_shape()393     gcalc_shape_info get_shape() const { return pi->node.shape.shape; }
get_next()394     inline point *get_next() { return (point *)next; }
get_next()395     inline const point *get_next() const { return (const point *)next; }
396     /* Compare the dx_dy parameters regarding the horiz_dir */
397     /* returns -1 if less, 0 if equal, 1 if bigger          */
398     static int cmp_dx_dy(const Gcalc_coord1 dx_a,
399                          const Gcalc_coord1 dy_a,
400                          const Gcalc_coord1 dx_b,
401                          const Gcalc_coord1 dy_b);
402     static int cmp_dx_dy(const Gcalc_heap::Info *p1,
403                          const Gcalc_heap::Info *p2,
404                          const Gcalc_heap::Info *p3,
405                          const Gcalc_heap::Info *p4);
406     int cmp_dx_dy(const point *p) const;
next_ptr()407     point **next_ptr() { return (point **) &next; }
408 #ifndef GCALC_DBUG_OFF
409     unsigned int thread;
410 #endif /*GCALC_DBUG_OFF*/
411 #ifdef GCALC_CHECK_WITH_FLOAT
412     void calc_x(long double *x, long double y, long double ix) const;
413 #endif /*GCALC_CHECK_WITH_FLOAT*/
414   };
415 
416   /* That class introduced mostly for the 'typecontrol' reason.      */
417   /* only difference from the point classis the get_next() function. */
418   class event_point : public point
419   {
420   public:
get_next()421     inline const event_point *get_next() const
422     { return (const event_point*) ev_next; }
simple_event()423     int simple_event() const
424     {
425       return !ev_next ? (event & (scev_point | scev_end)) :
426         (!ev_next->ev_next && event == scev_two_ends);
427     }
428   };
429 
430   class intersection_info : public Gcalc_dyn_list::Item
431   {
432   public:
433     point *edge_a;
434     point *edge_b;
435 
436     Gcalc_coord2 t_a;
437     Gcalc_coord2 t_b;
438     int t_calculated;
439     Gcalc_coord3 x_exp;
440     int x_calculated;
441     Gcalc_coord3 y_exp;
442     int y_calculated;
calc_t()443     void calc_t()
444     {if (!t_calculated) do_calc_t(); }
calc_y_exp()445     void calc_y_exp()
446     { if (!y_calculated) do_calc_y(); }
calc_x_exp()447     void calc_x_exp()
448     { if (!x_calculated) do_calc_x(); }
449 
450     void do_calc_t();
451     void do_calc_x();
452     void do_calc_y();
453   };
454 
455 
456   class slice_state
457   {
458   public:
459     point *slice;
460     point **event_position_hook;
461     point *event_end;
462     const Gcalc_heap::Info *pi;
463   };
464 
465 public:
466   Gcalc_scan_iterator(size_t blk_size= 8192);
467 
468   GCALC_DECL_TERMINATED_STATE(killed)
469 
470   void init(Gcalc_heap *points); /* Iterator can be reused */
471   void reset();
472   int step();
473 
more_points()474   Gcalc_heap::Info *more_points() { return m_cur_pi; }
more_trapezoids()475   bool more_trapezoids()
476     { return m_cur_pi && m_cur_pi->next; }
477 
get_bottom_points()478   const point *get_bottom_points() const
479     { return m_bottom_points; }
get_event_position()480   const point *get_event_position() const
481     { return *state.event_position_hook; }
get_event_end()482   const point *get_event_end() const
483     { return state.event_end; }
get_events()484   const event_point *get_events() const
485     { return (const event_point *)
486         (*state.event_position_hook == state.event_end ?
487             m_bottom_points : *state.event_position_hook); }
get_b_slice()488   const point *get_b_slice() const { return state.slice; }
489   double get_h() const;
490   double get_y() const;
491   double get_event_x() const;
492   double get_sp_x(const point *sp) const;
intersection_step()493   int intersection_step() const
494     { return state.pi->type == Gcalc_heap::nt_intersection; }
get_cur_pi()495   const Gcalc_heap::Info *get_cur_pi() const
496   {
497     return state.pi;
498   }
499 
500 private:
501   Gcalc_heap *m_heap;
502   Gcalc_heap::Info *m_cur_pi;
503   slice_state state;
504 
505 #ifndef GCALC_DBUG_OFF
506   unsigned int m_cur_thread;
507 #endif /*GCALC_DBUG_OFF*/
508 
509   point *m_bottom_points;
510   point **m_bottom_hook;
511 
512   int node_scan();
513   void eq_scan();
514   void intersection_scan();
515   void remove_bottom_node();
516   int insert_top_node();
517   int add_intersection(point *sp_a, point *sp_b,
518                        Gcalc_heap::Info *pi_from);
519   int add_eq_node(Gcalc_heap::Info *node, point *sp);
520   int add_events_for_node(point *sp_node);
521 
new_slice_point()522   point *new_slice_point()
523   {
524     point *new_point= (point *)new_item();
525     return new_point;
526   }
new_intersection_info(point * a,point * b)527   intersection_info *new_intersection_info(point *a, point *b)
528   {
529     intersection_info *ii= (intersection_info *)new_item();
530     ii->edge_a= a;
531     ii->edge_b= b;
532     ii->t_calculated= ii->x_calculated= ii->y_calculated= 0;
533     return ii;
534   }
535   int arrange_event(int do_sorting, int n_intersections);
536   static double get_pure_double(const Gcalc_internal_coord *d, int d_len);
537 };
538 
539 
540 /*
541    Gcalc_trapezoid_iterator simplifies the calculations on
542    the current slice of the Gcalc_scan_iterator.
543    One can walk through the trapezoids formed between
544    previous and current slices.
545 */
546 
547 #ifdef TMP_BLOCK
548 class Gcalc_trapezoid_iterator
549 {
550 protected:
551   const Gcalc_scan_iterator::point *sp0;
552   const Gcalc_scan_iterator::point *sp1;
553 public:
Gcalc_trapezoid_iterator(const Gcalc_scan_iterator * scan_i)554   Gcalc_trapezoid_iterator(const Gcalc_scan_iterator *scan_i) :
555     sp0(scan_i->get_b_slice()),
556     sp1(scan_i->get_t_slice())
557     {}
558 
more()559   inline bool more() const { return sp1 && sp1->next; }
560 
lt()561   const Gcalc_scan_iterator::point *lt() const { return sp1; }
lb()562   const Gcalc_scan_iterator::point *lb() const { return sp0; }
rb()563   const Gcalc_scan_iterator::point *rb() const
564   {
565     const Gcalc_scan_iterator::point *result= sp0;
566     while ((result= result->c_get_next())->is_bottom())
567     {}
568     return result;
569   }
rt()570   const Gcalc_scan_iterator::point *rt() const
571     { return sp1->c_get_next(); }
572 
573   void operator++()
574   {
575     sp0= rb();
576     sp1= rt();
577   }
578 };
579 #endif /*TMP_BLOCK*/
580 
581 
582 /*
583    Gcalc_point_iterator simplifies the calculations on
584    the current slice of the Gcalc_scan_iterator.
585    One can walk through the points on the current slice.
586 */
587 
588 class Gcalc_point_iterator
589 {
590 protected:
591   const Gcalc_scan_iterator::point *sp;
592 public:
Gcalc_point_iterator(const Gcalc_scan_iterator * scan_i)593   Gcalc_point_iterator(const Gcalc_scan_iterator *scan_i):
594     sp(scan_i->get_b_slice())
595     {}
596 
more()597   inline bool more() const { return sp != NULL; }
598   inline void operator++() { sp= sp->c_get_next(); }
point()599   inline const Gcalc_scan_iterator::point *point() const { return sp; }
get_pi()600   inline const Gcalc_heap::Info *get_pi() const { return sp->pi; }
get_shape()601   inline gcalc_shape_info get_shape() const { return sp->get_shape(); }
restart(const Gcalc_scan_iterator * scan_i)602   inline void restart(const Gcalc_scan_iterator *scan_i)
603   { sp= scan_i->get_b_slice(); }
604 };
605 
606 #endif /*GCALC_SLICESCAN_INCLUDED*/
607 
608