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.

346 lines
11 KiB

  1. /* Copyright (c) 2000, 2010 Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #ifndef GCALC_TOOLS_INCLUDED
  13. #define GCALC_TOOLS_INCLUDED
  14. #include "gcalc_slicescan.h"
  15. /*
  16. The Gcalc_function class objects are used to check for a binary relation.
  17. The relation can be constructed with the prefix notation using predicates as
  18. op_not (as !A)
  19. op_union ( A || B || C... )
  20. op_intersection ( A && B && C ... )
  21. op_symdifference ( A+B+C+... == 1 )
  22. op_difference ( A && !(B||C||..))
  23. with the calls of the add_operation(operation, n_operands) method.
  24. The relation is calculated over a set of shapes, that in turn have
  25. to be added with the add_new_shape() method. All the 'shapes' can
  26. be set to 0 with clear_shapes() method and single value
  27. can be changed with the invert_state() method.
  28. Then the value of the relation can be calculated with the count() method.
  29. Frequently used method is find_function(Gcalc_scan_iterator it) that
  30. iterates through the 'it' until the relation becomes TRUE.
  31. */
  32. class Gcalc_function
  33. {
  34. private:
  35. String shapes_buffer;
  36. String function_buffer;
  37. int *i_states;
  38. int *b_states;
  39. uint32 cur_object_id;
  40. uint n_shapes;
  41. int count_internal(const char *cur_func, uint set_type,
  42. const char **end);
  43. public:
  44. enum value
  45. {
  46. v_empty= 0x0000000,
  47. v_find_t= 0x1000000,
  48. v_find_f= 0x2000000,
  49. v_t_found= 0x3000000,
  50. v_f_found= 0x4000000,
  51. v_mask= 0x7000000
  52. };
  53. enum op_type
  54. {
  55. op_not= 0x80000000,
  56. op_shape= 0x00000000,
  57. op_union= 0x10000000,
  58. op_intersection= 0x20000000,
  59. op_symdifference= 0x30000000,
  60. op_difference= 0x40000000,
  61. op_repeat= 0x50000000,
  62. op_border= 0x60000000,
  63. op_internals= 0x70000000,
  64. op_false= 0x08000000,
  65. op_any= 0x78000000 /* The mask to get any of the operations */
  66. };
  67. enum shape_type
  68. {
  69. shape_point= 0,
  70. shape_line= 1,
  71. shape_polygon= 2,
  72. shape_hole= 3
  73. };
  74. Gcalc_function() : n_shapes(0) {}
  75. gcalc_shape_info add_new_shape(uint32 shape_id, shape_type shape_kind);
  76. /*
  77. Adds the leaf operation that returns the shape value.
  78. Also adds the shape to the list of operands.
  79. */
  80. int single_shape_op(shape_type shape_kind, gcalc_shape_info *si);
  81. void add_operation(uint operation, uint32 n_operands);
  82. void add_not_operation(op_type operation, uint32 n_operands);
  83. uint32 get_next_expression_pos() { return function_buffer.length(); }
  84. void add_operands_to_op(uint32 operation_pos, uint32 n_operands);
  85. int repeat_expression(uint32 exp_pos);
  86. void set_cur_obj(uint32 cur_obj) { cur_object_id= cur_obj; }
  87. int reserve_shape_buffer(uint n_shapes);
  88. int reserve_op_buffer(uint n_ops);
  89. uint get_nshapes() const { return n_shapes; }
  90. shape_type get_shape_kind(gcalc_shape_info si) const
  91. {
  92. return (shape_type) uint4korr(shapes_buffer.ptr() + (si*4));
  93. }
  94. void set_states(int *shape_states) { i_states= shape_states; }
  95. int alloc_states();
  96. void invert_i_state(gcalc_shape_info shape) { i_states[shape]^= 1; }
  97. void set_i_state(gcalc_shape_info shape) { i_states[shape]= 1; }
  98. void clear_i_state(gcalc_shape_info shape) { i_states[shape]= 0; }
  99. void set_b_state(gcalc_shape_info shape) { b_states[shape]= 1; }
  100. void clear_b_state(gcalc_shape_info shape) { b_states[shape]= 0; }
  101. int get_state(gcalc_shape_info shape)
  102. { return i_states[shape] | b_states[shape]; }
  103. int get_i_state(gcalc_shape_info shape) { return i_states[shape]; }
  104. int get_b_state(gcalc_shape_info shape) { return b_states[shape]; }
  105. int count()
  106. { return count_internal(function_buffer.ptr(), 0, 0); }
  107. void clear_i_states();
  108. void clear_b_states();
  109. void reset();
  110. int check_function(Gcalc_scan_iterator &scan_it);
  111. };
  112. /*
  113. Gcalc_operation_transporter class extends the Gcalc_shape_transporter.
  114. In addition to the parent's functionality, it fills the Gcalc_function
  115. object so it has the function that determines the proper shape.
  116. For example Multipolyline will be represented as an union of polylines.
  117. */
  118. class Gcalc_operation_transporter : public Gcalc_shape_transporter
  119. {
  120. protected:
  121. Gcalc_function *m_fn;
  122. gcalc_shape_info m_si;
  123. public:
  124. Gcalc_operation_transporter(Gcalc_function *fn, Gcalc_heap *heap) :
  125. Gcalc_shape_transporter(heap), m_fn(fn) {}
  126. int single_point(double x, double y);
  127. int start_line();
  128. int complete_line();
  129. int start_poly();
  130. int complete_poly();
  131. int start_ring();
  132. int complete_ring();
  133. int add_point(double x, double y);
  134. int start_collection(int n_objects);
  135. int empty_shape();
  136. };
  137. /*
  138. When we calculate the result of an spatial operation like
  139. Union or Intersection, we receive vertexes of the result
  140. one-by-one, and probably need to treat them in variative ways.
  141. So, the Gcalc_result_receiver class designed to get these
  142. vertexes and construct shapes/objects out of them.
  143. and to store the result in an appropriate format
  144. */
  145. class Gcalc_result_receiver
  146. {
  147. String buffer;
  148. uint32 n_points;
  149. Gcalc_function::shape_type common_shapetype;
  150. bool collection_result;
  151. uint32 n_shapes;
  152. uint32 n_holes;
  153. Gcalc_function::shape_type cur_shape;
  154. uint32 shape_pos;
  155. double first_x, first_y, prev_x, prev_y;
  156. double shape_area;
  157. public:
  158. Gcalc_result_receiver() : collection_result(FALSE), n_shapes(0), n_holes(0)
  159. {}
  160. int start_shape(Gcalc_function::shape_type shape);
  161. int add_point(double x, double y);
  162. int complete_shape();
  163. int single_point(double x, double y);
  164. int done();
  165. void reset();
  166. const char *result() { return buffer.ptr(); }
  167. uint length() { return buffer.length(); }
  168. int get_nshapes() { return n_shapes; }
  169. int get_nholes() { return n_holes; }
  170. int get_result_typeid();
  171. uint32 position() { return buffer.length(); }
  172. int move_hole(uint32 dest_position, uint32 source_position,
  173. uint32 *position_shift);
  174. };
  175. /*
  176. Gcalc_operation_reducer class incapsulates the spatial
  177. operation functionality. It analyses the slices generated by
  178. the slicescan and calculates the shape of the result defined
  179. by some Gcalc_function.
  180. */
  181. class Gcalc_operation_reducer : public Gcalc_dyn_list
  182. {
  183. public:
  184. enum modes
  185. {
  186. /* Numeric values important here - careful with changing */
  187. default_mode= 0,
  188. prefer_big_with_holes= 1,
  189. polygon_selfintersections_allowed= 2, /* allowed in the result */
  190. line_selfintersections_allowed= 4 /* allowed in the result */
  191. };
  192. Gcalc_operation_reducer(size_t blk_size=8192);
  193. void init(Gcalc_function *fn, modes mode= default_mode);
  194. Gcalc_operation_reducer(Gcalc_function *fn, modes mode= default_mode,
  195. size_t blk_size=8192);
  196. int count_slice(Gcalc_scan_iterator *si);
  197. int count_all(Gcalc_heap *hp);
  198. int get_result(Gcalc_result_receiver *storage);
  199. void reset();
  200. #ifndef GCALC_DBUG_OFF
  201. int n_res_points;
  202. #endif /*GCALC_DBUG_OFF*/
  203. class res_point : public Gcalc_dyn_list::Item
  204. {
  205. public:
  206. int intersection_point;
  207. union
  208. {
  209. const Gcalc_heap::Info *pi;
  210. const Gcalc_heap::Intersection_info *ii;
  211. res_point *first_poly_node;
  212. };
  213. union
  214. {
  215. res_point *outer_poly;
  216. uint32 poly_position;
  217. };
  218. res_point *up;
  219. res_point *down;
  220. res_point *glue;
  221. Gcalc_function::shape_type type;
  222. Gcalc_dyn_list::Item **prev_hook;
  223. #ifndef GCALC_DBUG_OFF
  224. int point_n;
  225. #endif /*GCALC_DBUG_OFF*/
  226. void set(const Gcalc_scan_iterator *si);
  227. res_point *get_next() { return (res_point *)next; }
  228. };
  229. class active_thread : public Gcalc_dyn_list::Item
  230. {
  231. public:
  232. res_point *rp;
  233. res_point *thread_start;
  234. const Gcalc_heap::Info *p1, *p2;
  235. res_point *enabled() { return rp; }
  236. active_thread *get_next() { return (active_thread *)next; }
  237. };
  238. class poly_instance : public Gcalc_dyn_list::Item
  239. {
  240. public:
  241. uint32 *after_poly_position;
  242. poly_instance *get_next() { return (poly_instance *)next; }
  243. };
  244. class line : public Gcalc_dyn_list::Item
  245. {
  246. public:
  247. active_thread *t;
  248. int incoming;
  249. const Gcalc_scan_iterator::point *p;
  250. line *get_next() { return (line *)next; }
  251. };
  252. class poly_border : public Gcalc_dyn_list::Item
  253. {
  254. public:
  255. active_thread *t;
  256. int incoming;
  257. int prev_state;
  258. const Gcalc_scan_iterator::point *p;
  259. poly_border *get_next() { return (poly_border *)next; }
  260. };
  261. line *m_lines;
  262. Gcalc_dyn_list::Item **m_lines_hook;
  263. poly_border *m_poly_borders;
  264. Gcalc_dyn_list::Item **m_poly_borders_hook;
  265. line *new_line() { return (line *) new_item(); }
  266. poly_border *new_poly_border() { return (poly_border *) new_item(); }
  267. int add_line(int incoming, active_thread *t,
  268. const Gcalc_scan_iterator::point *p);
  269. int add_poly_border(int incoming, active_thread *t, int prev_state,
  270. const Gcalc_scan_iterator::point *p);
  271. protected:
  272. Gcalc_function *m_fn;
  273. Gcalc_dyn_list::Item **m_res_hook;
  274. res_point *m_result;
  275. int m_mode;
  276. res_point *result_heap;
  277. active_thread *m_first_active_thread;
  278. res_point *add_res_point(Gcalc_function::shape_type type);
  279. active_thread *new_active_thread() { return (active_thread *)new_item(); }
  280. poly_instance *new_poly() { return (poly_instance *) new_item(); }
  281. private:
  282. int start_line(active_thread *t, const Gcalc_scan_iterator::point *p,
  283. const Gcalc_scan_iterator *si);
  284. int end_line(active_thread *t, const Gcalc_scan_iterator *si);
  285. int connect_threads(int incoming_a, int incoming_b,
  286. active_thread *ta, active_thread *tb,
  287. const Gcalc_scan_iterator::point *pa,
  288. const Gcalc_scan_iterator::point *pb,
  289. active_thread *prev_range,
  290. const Gcalc_scan_iterator *si,
  291. Gcalc_function::shape_type s_t);
  292. int add_single_point(const Gcalc_scan_iterator *si);
  293. poly_border *get_pair_border(poly_border *b1);
  294. int continue_range(active_thread *t, const Gcalc_heap::Info *p,
  295. const Gcalc_heap::Info *p_next);
  296. int continue_i_range(active_thread *t,
  297. const Gcalc_heap::Intersection_info *ii);
  298. int end_couple(active_thread *t0, active_thread *t1, const Gcalc_heap::Info *p);
  299. int get_single_result(res_point *res, Gcalc_result_receiver *storage);
  300. int get_result_thread(res_point *cur, Gcalc_result_receiver *storage,
  301. int move_upward, res_point *first_poly_node);
  302. int get_polygon_result(res_point *cur, Gcalc_result_receiver *storage,
  303. res_point *first_poly_node);
  304. int get_line_result(res_point *cur, Gcalc_result_receiver *storage);
  305. void free_result(res_point *res);
  306. };
  307. #endif /*GCALC_TOOLS_INCLUDED*/