You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
17 KiB

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