Debian Codegraph @

pencil/include/geometry/geometry_connector.inl

raw
1 /* File: geometry_connector.inl; Copyright and License: see below */
2
3 #include "u8/u8_trace.h"
4 #include "u8/u8_log.h"
5 #include "u8/u8_interval.h"
6 #include <math.h>
7
8 static inline void geometry_connector_init_vertical ( geometry_connector_t *this_,
9 double source_end_x,
10 double source_end_y,
11 double destination_end_x,
12 double destination_end_y,
13 double main_line_x )
14 {
15 (*this_).source_end_x = source_end_x;
16 (*this_).source_end_y = source_end_y;
17 (*this_).main_line_source_x = main_line_x;
18 (*this_).main_line_source_y = source_end_y;
19 (*this_).main_line_destination_x = main_line_x;
20 (*this_).main_line_destination_y = destination_end_y;
21 (*this_).destination_end_x = destination_end_x;
22 (*this_).destination_end_y = destination_end_y;
23 }
24
25 static inline void geometry_connector_init_horizontal ( geometry_connector_t *this_,
26 double source_end_x,
27 double source_end_y,
28 double destination_end_x,
29 double destination_end_y,
30 double main_line_y )
31 {
32 (*this_).source_end_x = source_end_x;
33 (*this_).source_end_y = source_end_y;
34 (*this_).main_line_source_x = source_end_x;
35 (*this_).main_line_source_y = main_line_y;
36 (*this_).main_line_destination_x = destination_end_x;
37 (*this_).main_line_destination_y = main_line_y;
38 (*this_).destination_end_x = destination_end_x;
39 (*this_).destination_end_y = destination_end_y;
40 }
41
42 static inline void geometry_connector_reinit_vertical ( geometry_connector_t *this_,
43 double source_end_x,
44 double source_end_y,
45 double destination_end_x,
46 double destination_end_y,
47 double main_line_x )
48 {
49 (*this_).source_end_x = source_end_x;
50 (*this_).source_end_y = source_end_y;
51 (*this_).main_line_source_x = main_line_x;
52 (*this_).main_line_source_y = source_end_y;
53 (*this_).main_line_destination_x = main_line_x;
54 (*this_).main_line_destination_y = destination_end_y;
55 (*this_).destination_end_x = destination_end_x;
56 (*this_).destination_end_y = destination_end_y;
57 }
58
59 static inline void geometry_connector_reinit_horizontal ( geometry_connector_t *this_,
60 double source_end_x,
61 double source_end_y,
62 double destination_end_x,
63 double destination_end_y,
64 double main_line_y )
65 {
66 (*this_).source_end_x = source_end_x;
67 (*this_).source_end_y = source_end_y;
68 (*this_).main_line_source_x = source_end_x;
69 (*this_).main_line_source_y = main_line_y;
70 (*this_).main_line_destination_x = destination_end_x;
71 (*this_).main_line_destination_y = main_line_y;
72 (*this_).destination_end_x = destination_end_x;
73 (*this_).destination_end_y = destination_end_y;
74 }
75
76 static inline void geometry_connector_copy ( geometry_connector_t *this_, const geometry_connector_t *original )
77 {
78 (*this_) = (*original);
79 }
80
81 static inline void geometry_connector_replace ( geometry_connector_t *this_, const geometry_connector_t *original )
82 {
83 (*this_) = (*original);
84 }
85
86 static inline void geometry_connector_init_empty ( geometry_connector_t *this_ )
87 {
88 (*this_).source_end_x = 0.0;
89 (*this_).source_end_y = 0.0;
90 (*this_).main_line_source_x = 0.0;
91 (*this_).main_line_source_y = 0.0;
92 (*this_).main_line_destination_x = 0.0;
93 (*this_).main_line_destination_y = 0.0;
94 (*this_).destination_end_x = 0.0;
95 (*this_).destination_end_y = 0.0;
96 }
97
98 static inline void geometry_connector_reinit_empty ( geometry_connector_t *this_ )
99 {
100 (*this_).source_end_x = 0.0;
101 (*this_).source_end_y = 0.0;
102 (*this_).main_line_source_x = 0.0;
103 (*this_).main_line_source_y = 0.0;
104 (*this_).main_line_destination_x = 0.0;
105 (*this_).main_line_destination_y = 0.0;
106 (*this_).destination_end_x = 0.0;
107 (*this_).destination_end_y = 0.0;
108 }
109
110 static inline void geometry_connector_destroy ( geometry_connector_t *this_ )
111 {
112 }
113
114 static inline double geometry_connector_get_source_end_x ( const geometry_connector_t *this_ )
115 {
116 return (*this_).source_end_x;
117 }
118
119 static inline double geometry_connector_get_source_end_y ( const geometry_connector_t *this_ )
120 {
121 return (*this_).source_end_y;
122 }
123
124 static inline double geometry_connector_get_main_line_source_x ( const geometry_connector_t *this_ )
125 {
126 return (*this_).main_line_source_x;
127 }
128
129 static inline double geometry_connector_get_main_line_source_y ( const geometry_connector_t *this_ )
130 {
131 return (*this_).main_line_source_y;
132 }
133
134 static inline double geometry_connector_get_main_line_destination_x ( const geometry_connector_t *this_ )
135 {
136 return (*this_).main_line_destination_x;
137 }
138
139 static inline double geometry_connector_get_main_line_destination_y ( const geometry_connector_t *this_ )
140 {
141 return (*this_).main_line_destination_y;
142 }
143
144 static inline double geometry_connector_get_destination_end_x ( const geometry_connector_t *this_ )
145 {
146 return (*this_).destination_end_x;
147 }
148
149 static inline double geometry_connector_get_destination_end_y ( const geometry_connector_t *this_ )
150 {
151 return (*this_).destination_end_y;
152 }
153
154 static inline geometry_point_t geometry_connector_calculate_waypoint ( const geometry_connector_t *this_,
155 double distance_covered )
156 {
157 geometry_point_t result;
158
159 const double source_end_length = fabs( (*this_).source_end_x - (*this_).main_line_source_x )
160 + fabs( (*this_).source_end_y - (*this_).main_line_source_y );
161 const double main_line_length = fabs( (*this_).main_line_source_x - (*this_).main_line_destination_x )
162 + fabs( (*this_).main_line_source_y - (*this_).main_line_destination_y );
163 const double dest_end_length = fabs( (*this_).main_line_destination_x - (*this_).destination_end_x )
164 + fabs( (*this_).main_line_destination_y - (*this_).destination_end_y );
165
166 if ( distance_covered < source_end_length ) {
167 if (( distance_covered <= 0.0 )||( source_end_length < 0.000000001 ))
168 {
169 geometry_point_init ( &result, (*this_).source_end_x, (*this_).source_end_y );
170 }
171 else
172 {
173 geometry_point_init ( &result, (*this_).source_end_x, (*this_).source_end_y );
174 const double segment_part1 = distance_covered / source_end_length;
175 geometry_point_shift( &result,
176 segment_part1 * ( (*this_).main_line_source_x - (*this_).source_end_x ),
177 segment_part1 * ( (*this_).main_line_source_y - (*this_).source_end_y )
178 );
179 }
180 }
181 else {
182 const double shifted_distance = distance_covered - source_end_length;
183 if (( shifted_distance < main_line_length )&&( main_line_length >= 0.000000001 ))
184 {
185 geometry_point_init ( &result, (*this_).main_line_source_x, (*this_).main_line_source_y );
186 const double segment_part2 = shifted_distance / main_line_length;
187 geometry_point_shift( &result,
188 segment_part2 * ( (*this_).main_line_destination_x - (*this_).main_line_source_x ),
189 segment_part2 * ( (*this_).main_line_destination_y - (*this_).main_line_source_y )
190 );
191 }
192 else
193 {
194 const double shifted2_distance = shifted_distance - main_line_length;
195 if (( shifted2_distance < dest_end_length )&&( dest_end_length >= 0.000000001 ))
196 {
197 geometry_point_init ( &result, (*this_).main_line_destination_x, (*this_).main_line_destination_y );
198 const double segment_part3 = shifted2_distance / dest_end_length;
199 geometry_point_shift( &result,
200 segment_part3 * ( (*this_).destination_end_x - (*this_).main_line_destination_x ),
201 segment_part3 * ( (*this_).destination_end_y - (*this_).main_line_destination_y )
202 );
203 }
204 else
205 {
206 geometry_point_init ( &result, (*this_).destination_end_x, (*this_).destination_end_y );
207 }
208 }
209 }
210
211 return result;
212 }
213
214 static inline double geometry_connector_get_length ( const geometry_connector_t *this_ )
215 {
216 const double source_end_length
217 = fabs( (*this_).source_end_x - (*this_).main_line_source_x )
218 + fabs( (*this_).source_end_y - (*this_).main_line_source_y );
219 const double main_line_length
220 = fabs( (*this_).main_line_source_x - (*this_).main_line_destination_x )
221 + fabs( (*this_).main_line_source_y - (*this_).main_line_destination_y );
222 const double dest_end_length
223 = fabs( (*this_).main_line_destination_x - (*this_).destination_end_x )
224 + fabs( (*this_).main_line_destination_y - (*this_).destination_end_y );
225 return source_end_length + main_line_length + dest_end_length;
226 }
227
228 static inline double geometry_connector_get_source_length ( const geometry_connector_t *this_ )
229 {
230 const double source_end_length
231 = fabs( (*this_).source_end_x - (*this_).main_line_source_x )
232 + fabs( (*this_).source_end_y - (*this_).main_line_source_y );
233 return source_end_length;
234 }
235
236 static inline double geometry_connector_get_main_length ( const geometry_connector_t *this_ )
237 {
238 const double main_line_length
239 = fabs( (*this_).main_line_source_x - (*this_).main_line_destination_x )
240 + fabs( (*this_).main_line_source_y - (*this_).main_line_destination_y );
241 return main_line_length;
242 }
243
244 static inline double geometry_connector_get_destination_length ( const geometry_connector_t *this_ )
245 {
246 const double dest_end_length
247 = fabs( (*this_).main_line_destination_x - (*this_).destination_end_x )
248 + fabs( (*this_).main_line_destination_y - (*this_).destination_end_y );
249 return dest_end_length;
250 }
251
252 static inline bool geometry_connector_is_close ( const geometry_connector_t *this_, double x, double y, double max_distance )
253 {
254 const bool close_to_source_end_line
255 = ((( (*this_).source_end_x - max_distance < x ) && ( x < (*this_).main_line_source_x + max_distance ))
256 || (( (*this_).main_line_source_x - max_distance < x ) && ( x < (*this_).source_end_x + max_distance )))
257 && ((( (*this_).source_end_y - max_distance < y ) && ( y < (*this_).main_line_source_y + max_distance ))
258 || (( (*this_).main_line_source_y - max_distance < y ) && ( y < (*this_).source_end_y + max_distance )));
259 const bool close_to_main_line
260 = ((( (*this_).main_line_destination_x - max_distance < x ) && ( x < (*this_).main_line_source_x + max_distance ))
261 || (( (*this_).main_line_source_x - max_distance < x ) && ( x < (*this_).main_line_destination_x + max_distance )))
262 && ((( (*this_).main_line_destination_y - max_distance < y ) && ( y < (*this_).main_line_source_y + max_distance ))
263 || (( (*this_).main_line_source_y - max_distance < y ) && ( y < (*this_).main_line_destination_y + max_distance )));
264 const bool close_to_destination_end_line
265 = ((( (*this_).main_line_destination_x - max_distance < x ) && ( x < (*this_).destination_end_x + max_distance ))
266 || (( (*this_).destination_end_x - max_distance < x ) && ( x < (*this_).main_line_destination_x + max_distance )))
267 && ((( (*this_).main_line_destination_y - max_distance < y ) && ( y < (*this_).destination_end_y + max_distance ))
268 || (( (*this_).destination_end_y - max_distance < y ) && ( y < (*this_).main_line_destination_y + max_distance )));
269 return ( close_to_source_end_line || close_to_main_line || close_to_destination_end_line );
270 }
271
272 static inline bool geometry_connector_is_intersecting_rectangle ( const geometry_connector_t *this_,
273 const geometry_rectangle_t *rect )
274 {
275 geometry_rectangle_t source;
276 geometry_rectangle_t main_line;
277 geometry_rectangle_t destination;
278 geometry_rectangle_init_by_corners( &source,
279 (*this_).source_end_x, (*this_).source_end_y,
280 (*this_).main_line_source_x, (*this_).main_line_source_y
281 );
282 geometry_rectangle_init_by_corners( &main_line,
283 (*this_).main_line_source_x, (*this_).main_line_source_y,
284 (*this_).main_line_destination_x, (*this_).main_line_destination_y
285 );
286 geometry_rectangle_init_by_corners( &destination,
287 (*this_).main_line_destination_x, (*this_).main_line_destination_y,
288 (*this_).destination_end_x, (*this_).destination_end_y
289 );
290
291 const bool result
292 = geometry_rectangle_is_intersecting( &source, rect )
293 || geometry_rectangle_is_intersecting( &main_line, rect )
294 || geometry_rectangle_is_intersecting( &destination, rect );
295
296 geometry_rectangle_destroy( &source );
297 geometry_rectangle_destroy( &main_line );
298 geometry_rectangle_destroy( &destination );
299
300 return result;
301 }
302
303 static inline double geometry_connector_get_transit_length ( const geometry_connector_t *this_,
304 const geometry_rectangle_t *rect )
305 {
306 double result = 0.0;
307
308 geometry_rectangle_t source;
309 geometry_rectangle_t main_line;
310 geometry_rectangle_t destination;
311 geometry_rectangle_init_by_corners( &source,
312 (*this_).source_end_x, (*this_).source_end_y,
313 (*this_).main_line_source_x, (*this_).main_line_source_y
314 );
315 geometry_rectangle_init_by_corners( &main_line,
316 (*this_).main_line_source_x, (*this_).main_line_source_y,
317 (*this_).main_line_destination_x, (*this_).main_line_destination_y
318 );
319 geometry_rectangle_init_by_corners( &destination,
320 (*this_).main_line_destination_x, (*this_).main_line_destination_y,
321 (*this_).destination_end_x, (*this_).destination_end_y
322 );
323 const int src_err = geometry_rectangle_init_by_intersect( &source, &source, rect );
324 const int main_err = geometry_rectangle_init_by_intersect( &main_line, &main_line, rect );
325 const int dst_err = geometry_rectangle_init_by_intersect( &destination, &destination, rect );
326 if ( src_err == 0 )
327 {
328 result += geometry_rectangle_get_width( &source ) + geometry_rectangle_get_height( &source );
329 }
330 if ( main_err == 0 )
331 {
332 result += geometry_rectangle_get_width( &main_line ) + geometry_rectangle_get_height( &main_line );
333 }
334 if ( dst_err == 0 )
335 {
336 result += geometry_rectangle_get_width( &destination ) + geometry_rectangle_get_height( &destination );
337 }
338 geometry_rectangle_destroy( &source );
339 geometry_rectangle_destroy( &main_line );
340 geometry_rectangle_destroy( &destination );
341
342 return result;
343 }
344
345 static inline uint32_t geometry_connector_count_connector_intersects ( const geometry_connector_t *this_,
346 const geometry_connector_t *that )
347 {
348 uint32_t result = 0;
349
350 /* do some simple prechecks */
351 geometry_rectangle_t this_bounds;
352 geometry_rectangle_t that_bounds;
353
354 this_bounds = geometry_connector_get_bounding_rectangle( this_ );
355 that_bounds = geometry_connector_get_bounding_rectangle( that );
356
357 if ( geometry_rectangle_is_contiguous( &this_bounds, &that_bounds ) )
358 {
359 /* do some more complicated checks */
360 geometry_rectangle_t this_source_end;
361 geometry_rectangle_t this_main_line;
362 geometry_rectangle_t this_destination_end;
363 geometry_rectangle_t that_source_end;
364 geometry_rectangle_t that_main_line;
365 geometry_rectangle_t that_destination_end;
366
367 geometry_rectangle_init_by_corners( &this_source_end,
368 (*this_).main_line_source_x, (*this_).main_line_source_y, (*this_).source_end_x, (*this_).source_end_y
369 );
370 geometry_rectangle_init_by_corners( &this_main_line,
371 (*this_).main_line_destination_x, (*this_).main_line_destination_y, (*this_).main_line_source_x, (*this_).main_line_source_y
372 );
373 geometry_rectangle_init_by_corners( &this_destination_end,
374 (*this_).destination_end_x, (*this_).destination_end_y, (*this_).main_line_destination_x, (*this_).main_line_destination_y
375 );
376 geometry_rectangle_init_by_corners( &that_source_end,
377 (*that).main_line_source_x, (*that).main_line_source_y, (*that).source_end_x, (*that).source_end_y
378 );
379 geometry_rectangle_init_by_corners( &that_main_line,
380 (*that).main_line_destination_x, (*that).main_line_destination_y, (*that).main_line_source_x, (*that).main_line_source_y
381 );
382 geometry_rectangle_init_by_corners( &that_destination_end,
383 (*that).destination_end_x, (*that).destination_end_y, (*that).main_line_destination_x, (*that).main_line_destination_y
384 );
385
386 if ( geometry_rectangle_is_contiguous( &this_source_end, &that_source_end ) )
387 {
388 result += 1;
389 }
390 if ( geometry_rectangle_is_contiguous( &this_main_line, &that_source_end ) )
391 {
392 result += 1;
393 }
394 if ( geometry_rectangle_is_contiguous( &this_destination_end, &that_source_end ) )
395 {
396 result += 1;
397 }
398 if ( geometry_rectangle_is_contiguous( &this_source_end, &that_main_line ) )
399 {
400 result += 1;
401 }
402 if ( geometry_rectangle_is_contiguous( &this_main_line, &that_main_line ) )
403 {
404 result += 1;
405 }
406 if ( geometry_rectangle_is_contiguous( &this_destination_end, &that_main_line ) )
407 {
408 result += 1;
409 }
410 if ( geometry_rectangle_is_contiguous( &this_source_end, &that_destination_end ) )
411 {
412 result += 1;
413 }
414 if ( geometry_rectangle_is_contiguous( &this_main_line, &that_destination_end ) )
415 {
416 result += 1;
417 }
418 if ( geometry_rectangle_is_contiguous( &this_destination_end, &that_destination_end ) )
419 {
420 result += 1;
421 }
422
423 geometry_rectangle_destroy( &this_source_end );
424 geometry_rectangle_destroy( &this_main_line );
425 geometry_rectangle_destroy( &this_destination_end );
426 geometry_rectangle_destroy( &that_source_end );
427 geometry_rectangle_destroy( &that_main_line );
428 geometry_rectangle_destroy( &that_destination_end );
429 }
430 /* else no intersects, result = 0 */
431
432 geometry_rectangle_destroy( &this_bounds );
433 geometry_rectangle_destroy( &that_bounds );
434
435 return result;
436 }
437
438 static inline double geometry_connector_get_same_path_length_rect( const geometry_connector_t *this_,
439 const geometry_rectangle_t *rect,
440 double max_distance )
441 {
442 double result = 0.0;
443
444 const double left = geometry_rectangle_get_left( rect );
445 const double top = geometry_rectangle_get_top( rect );
446 const double right = geometry_rectangle_get_right( rect );
447 const double bottom = geometry_rectangle_get_bottom( rect );
448 const u8_interval_t x_length = u8_interval_new( left, right );
449 const u8_interval_t y_length = u8_interval_new( top, bottom );
450
451 /* check same path with left side of rectangle */
452 {
453 const double left_lo = left - max_distance;
454 const double left_hi = left + max_distance;
455
456 const bool source_end_is_on_left_side
457 = ( left_lo < (*this_).source_end_x )&&( (*this_).source_end_x < left_hi );
458 const bool main_line_source_is_on_left_side
459 = ( left_lo < (*this_).main_line_source_x )&&( (*this_).main_line_source_x < left_hi );
460 const bool main_line_end_is_on_left_side
461 = ( left_lo < (*this_).main_line_destination_x )&&( (*this_).main_line_destination_x < left_hi );
462 const bool destination_end_is_on_left_side
463 = ( left_lo < (*this_).destination_end_x )&&( (*this_).destination_end_x < left_hi );
464 if ( source_end_is_on_left_side && main_line_source_is_on_left_side )
465 {
466 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).source_end_y, (*this_).main_line_source_y );
467 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
468 result += u8_interval_get_size( &intersect );
469 }
470 if ( main_line_source_is_on_left_side && main_line_end_is_on_left_side )
471 {
472 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).main_line_source_y, (*this_).main_line_destination_y );
473 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
474 result += u8_interval_get_size( &intersect );
475 }
476 if ( main_line_end_is_on_left_side && destination_end_is_on_left_side )
477 {
478 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).main_line_destination_y, (*this_).destination_end_y );
479 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
480 result += u8_interval_get_size( &intersect );
481 }
482 }
483
484 /* check same path with top side of rectangle */
485 {
486 const double top_lo = top - max_distance;
487 const double top_hi = top + max_distance;
488
489 const bool source_end_is_on_top_side
490 = ( top_lo < (*this_).source_end_y )&&( (*this_).source_end_y < top_hi );
491 const bool main_line_source_is_on_top_side
492 = ( top_lo < (*this_).main_line_source_y )&&( (*this_).main_line_source_y < top_hi );
493 const bool main_line_end_is_on_top_side
494 = ( top_lo < (*this_).main_line_destination_y )&&( (*this_).main_line_destination_y < top_hi );
495 const bool destination_end_is_on_top_side
496 = ( top_lo < (*this_).destination_end_y )&&( (*this_).destination_end_y < top_hi );
497 if ( source_end_is_on_top_side && main_line_source_is_on_top_side )
498 {
499 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).source_end_x, (*this_).main_line_source_x );
500 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
501 result += u8_interval_get_size( &intersect );
502 }
503 if ( main_line_source_is_on_top_side && main_line_end_is_on_top_side )
504 {
505 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).main_line_source_x, (*this_).main_line_destination_x );
506 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
507 result += u8_interval_get_size( &intersect );
508 }
509 if ( main_line_end_is_on_top_side && destination_end_is_on_top_side )
510 {
511 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).main_line_destination_x, (*this_).destination_end_x );
512 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
513 result += u8_interval_get_size( &intersect );
514 }
515 }
516 /* check same path with right side of rectangle */
517 {
518 const double right_lo = right - max_distance;
519 const double right_hi = right + max_distance;
520
521 const bool source_end_is_on_right_side
522 = ( right_lo < (*this_).source_end_x )&&( (*this_).source_end_x < right_hi );
523 const bool main_line_source_is_on_right_side
524 = ( right_lo < (*this_).main_line_source_x )&&( (*this_).main_line_source_x < right_hi );
525 const bool main_line_end_is_on_right_side
526 = ( right_lo < (*this_).main_line_destination_x )&&( (*this_).main_line_destination_x < right_hi );
527 const bool destination_end_is_on_right_side
528 = ( right_lo < (*this_).destination_end_x )&&( (*this_).destination_end_x < right_hi );
529 if ( source_end_is_on_right_side && main_line_source_is_on_right_side )
530 {
531 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).source_end_y, (*this_).main_line_source_y );
532 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
533 result += u8_interval_get_size( &intersect );
534 }
535 if ( main_line_source_is_on_right_side && main_line_end_is_on_right_side )
536 {
537 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).main_line_source_y, (*this_).main_line_destination_y );
538 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
539 result += u8_interval_get_size( &intersect );
540 }
541 if ( main_line_end_is_on_right_side && destination_end_is_on_right_side )
542 {
543 const u8_interval_t section_y = u8_interval_new_unordered( (*this_).main_line_destination_y, (*this_).destination_end_y );
544 const u8_interval_t intersect = u8_interval_new_intersect( &y_length, &section_y );
545 result += u8_interval_get_size( &intersect );
546 }
547 }
548 /* check same path with bottom side of rectangle */
549 {
550 const double bottom_lo = bottom - max_distance;
551 const double bottom_hi = bottom + max_distance;
552
553 const bool source_end_is_on_bottom_side
554 = ( bottom_lo < (*this_).source_end_y )&&( (*this_).source_end_y < bottom_hi );
555 const bool main_line_source_is_on_bottom_side
556 = ( bottom_lo < (*this_).main_line_source_y )&&( (*this_).main_line_source_y < bottom_hi );
557 const bool main_line_end_is_on_bottom_side
558 = ( bottom_lo < (*this_).main_line_destination_y )&&( (*this_).main_line_destination_y < bottom_hi );
559 const bool destination_end_is_on_bottom_side
560 = ( bottom_lo < (*this_).destination_end_y )&&( (*this_).destination_end_y < bottom_hi );
561 if ( source_end_is_on_bottom_side && main_line_source_is_on_bottom_side )
562 {
563 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).source_end_x, (*this_).main_line_source_x );
564 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
565 result += u8_interval_get_size( &intersect );
566 }
567 if ( main_line_source_is_on_bottom_side && main_line_end_is_on_bottom_side )
568 {
569 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).main_line_source_x, (*this_).main_line_destination_x );
570 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
571 result += u8_interval_get_size( &intersect );
572 }
573 if ( main_line_end_is_on_bottom_side && destination_end_is_on_bottom_side )
574 {
575 const u8_interval_t section_x = u8_interval_new_unordered( (*this_).main_line_destination_x, (*this_).destination_end_x );
576 const u8_interval_t intersect = u8_interval_new_intersect( &x_length, &section_x );
577 result += u8_interval_get_size( &intersect );
578 }
579 }
580
581 return result;
582 }
583
584 static inline double geometry_connector_get_same_path_length_conn( const geometry_connector_t *this_,
585 const geometry_connector_t *that,
586 double max_distance )
587 {
588 double result = 0.0;
589
590 const double source_x = (*that).main_line_source_x;
591 const double source_y = (*that).main_line_source_y;
592 const double destination_x = (*that).main_line_destination_x;
593 const double destination_y = (*that).main_line_destination_y;
594
595 const double source_x_lo = source_x - max_distance;
596 const double source_x_hi = source_x + max_distance;
597 const double source_y_lo = source_y - max_distance;
598 const double source_y_hi = source_y + max_distance;
599 const double destination_x_lo = destination_x - max_distance;
600 const double destination_x_hi = destination_x + max_distance;
601 const double destination_y_lo = destination_y - max_distance;
602 const double destination_y_hi = destination_y + max_distance;
603 const bool source_x_on_source_x
604 = ( source_x_lo < (*this_).main_line_source_x )&&( (*this_).main_line_source_x < source_x_hi );
605 const bool source_y_on_source_y
606 = ( source_y_lo < (*this_).main_line_source_y )&&( (*this_).main_line_source_y < source_y_hi );
607 const bool destination_x_on_source_x
608 = ( source_x_lo < (*this_).main_line_destination_x )&&( (*this_).main_line_destination_x < source_x_hi );
609 const bool destination_y_on_source_y
610 = ( source_y_lo < (*this_).main_line_destination_y )&&( (*this_).main_line_destination_y < source_y_hi );
611 const bool source_x_on_destination_x
612 = ( destination_x_lo < (*this_).main_line_source_x )&&( (*this_).main_line_source_x < destination_x_hi );
613 const bool source_y_on_destination_y
614 = ( destination_y_lo < (*this_).main_line_source_y )&&( (*this_).main_line_source_y < destination_y_hi );
615 const bool destination_x_on_destination_x
616 = ( destination_x_lo < (*this_).main_line_destination_x )&&( (*this_).main_line_destination_x < destination_x_hi );
617 const bool destination_y_on_destination_y
618 = ( destination_y_lo < (*this_).main_line_destination_y )&&( (*this_).main_line_destination_y < destination_y_hi );
619
620 const geometry_3dir_t this_dir = geometry_connector_get_directions( this_ );
621 const geometry_3dir_t that_dir = geometry_connector_get_directions( that );
622
623 /* check same path with source segment of that */
624 if ( geometry_3dir_is_first_v( &that_dir ) )
625 {
626 const u8_interval_t source_dy = u8_interval_new_unordered( (*that).source_end_y, (*that).main_line_source_y );
627
628 if ( geometry_3dir_is_first_v( &this_dir ) && source_x_on_source_x )
629 {
630 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).source_end_y, (*this_).main_line_source_y );
631 const u8_interval_t intersect = u8_interval_new_intersect( &source_dy, &section_dy );
632 result += u8_interval_get_size( &intersect );
633 }
634 if ( geometry_3dir_is_second_v( &this_dir ) && source_x_on_source_x )
635 {
636 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_source_y, (*this_).main_line_destination_y );
637 const u8_interval_t intersect = u8_interval_new_intersect( &source_dy, &section_dy );
638 result += u8_interval_get_size( &intersect );
639 }
640 if ( geometry_3dir_is_third_v( &this_dir ) && destination_x_on_source_x )
641 {
642 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_destination_y, (*this_).destination_end_y );
643 const u8_interval_t intersect = u8_interval_new_intersect( &source_dy, &section_dy );
644 result += u8_interval_get_size( &intersect );
645 }
646 }
647 else /* geometry_3dir_is_first_h( &that_dir ) */
648 {
649 const u8_interval_t source_dx = u8_interval_new_unordered( (*that).source_end_x, (*that).main_line_source_x );
650
651 if ( geometry_3dir_is_first_h( &this_dir ) && source_y_on_source_y )
652 {
653 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).source_end_x, (*this_).main_line_source_x );
654 const u8_interval_t intersect = u8_interval_new_intersect( &source_dx, &section_dx );
655 result += u8_interval_get_size( &intersect );
656 }
657 if ( geometry_3dir_is_second_h( &this_dir ) && source_y_on_source_y )
658 {
659 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_source_x, (*this_).main_line_destination_x );
660 const u8_interval_t intersect = u8_interval_new_intersect( &source_dx, &section_dx );
661 result += u8_interval_get_size( &intersect );
662 }
663 if ( geometry_3dir_is_third_h( &this_dir ) && destination_y_on_source_y )
664 {
665 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_destination_x, (*this_).destination_end_x );
666 const u8_interval_t intersect = u8_interval_new_intersect( &source_dx, &section_dx );
667 result += u8_interval_get_size( &intersect );
668 }
669 }
670
671 /* check same path with main segment of that */
672 if ( geometry_3dir_is_second_v( &that_dir ) )
673 {
674 const u8_interval_t main_dy = u8_interval_new_unordered( (*that).main_line_source_y, (*that).main_line_destination_y );
675
676 if ( geometry_3dir_is_first_v( &this_dir ) && source_x_on_source_x )
677 {
678 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).source_end_y, (*this_).main_line_source_y );
679 const u8_interval_t intersect = u8_interval_new_intersect( &main_dy, &section_dy );
680 result += u8_interval_get_size( &intersect );
681 }
682 if ( geometry_3dir_is_second_v( &this_dir ) && source_x_on_source_x )
683 {
684 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_source_y, (*this_).main_line_destination_y );
685 const u8_interval_t intersect = u8_interval_new_intersect( &main_dy, &section_dy );
686 result += u8_interval_get_size( &intersect );
687 }
688 if ( geometry_3dir_is_third_v( &this_dir ) && destination_x_on_source_x )
689 {
690 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_destination_y, (*this_).destination_end_y );
691 const u8_interval_t intersect = u8_interval_new_intersect( &main_dy, &section_dy );
692 result += u8_interval_get_size( &intersect );
693 }
694 }
695 else /* geometry_3dir_is_second_h( &that_dir ) */
696 {
697 const u8_interval_t main_dx = u8_interval_new_unordered( (*that).main_line_source_x, (*that).main_line_destination_x );
698
699 if ( geometry_3dir_is_first_h( &this_dir ) && source_y_on_source_y )
700 {
701 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).source_end_x, (*this_).main_line_source_x );
702 const u8_interval_t intersect = u8_interval_new_intersect( &main_dx, &section_dx );
703 result += u8_interval_get_size( &intersect );
704 }
705 if ( geometry_3dir_is_second_h( &this_dir ) && source_y_on_source_y )
706 {
707 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_source_x, (*this_).main_line_destination_x );
708 const u8_interval_t intersect = u8_interval_new_intersect( &main_dx, &section_dx );
709 result += u8_interval_get_size( &intersect );
710 }
711 if ( geometry_3dir_is_third_h( &this_dir ) && destination_y_on_source_y )
712 {
713 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_destination_x, (*this_).destination_end_x );
714 const u8_interval_t intersect = u8_interval_new_intersect( &main_dx, &section_dx );
715 result += u8_interval_get_size( &intersect );
716 }
717 }
718
719 /* check same path with destination segment of that */
720 if ( geometry_3dir_is_third_v( &that_dir ) )
721 {
722 const u8_interval_t destination_dy = u8_interval_new_unordered( (*that).main_line_destination_y, (*that).destination_end_y );
723
724 if ( geometry_3dir_is_first_v( &this_dir ) && source_x_on_destination_x )
725 {
726 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).source_end_y, (*this_).main_line_source_y );
727 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dy, &section_dy );
728 result += u8_interval_get_size( &intersect );
729 }
730 if ( geometry_3dir_is_second_v( &this_dir ) && source_x_on_destination_x )
731 {
732 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_source_y, (*this_).main_line_destination_y );
733 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dy, &section_dy );
734 result += u8_interval_get_size( &intersect );
735 }
736 if ( geometry_3dir_is_third_v( &this_dir ) && destination_x_on_destination_x )
737 {
738 const u8_interval_t section_dy = u8_interval_new_unordered( (*this_).main_line_destination_y, (*this_).destination_end_y );
739 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dy, &section_dy );
740 result += u8_interval_get_size( &intersect );
741 }
742 }
743 else /* geometry_3dir_is_third_h( &that_dir ) */
744 {
745 const u8_interval_t destination_dx = u8_interval_new_unordered( (*that).main_line_destination_x, (*that).destination_end_x );
746
747 if ( geometry_3dir_is_first_h( &this_dir ) && source_y_on_destination_y )
748 {
749 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).source_end_x, (*this_).main_line_source_x );
750 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dx, &section_dx );
751 result += u8_interval_get_size( &intersect );
752 }
753 if ( geometry_3dir_is_second_h( &this_dir ) && source_y_on_destination_y )
754 {
755 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_source_x, (*this_).main_line_destination_x );
756 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dx, &section_dx );
757 result += u8_interval_get_size( &intersect );
758 }
759 if ( geometry_3dir_is_third_h( &this_dir ) && destination_y_on_destination_y )
760 {
761 const u8_interval_t section_dx = u8_interval_new_unordered( (*this_).main_line_destination_x, (*this_).destination_end_x );
762 const u8_interval_t intersect = u8_interval_new_intersect( &destination_dx, &section_dx );
763 result += u8_interval_get_size( &intersect );
764 }
765 }
766
767 return result;
768 }
769
770 static inline geometry_rectangle_t geometry_connector_get_bounding_rectangle ( const geometry_connector_t *this_ )
771 {
772 geometry_rectangle_t result;
773
774 const double left = fmin ( (*this_).main_line_source_x, fmin( (*this_).source_end_x, (*this_).destination_end_x ) );
775 const double right = fmax ( (*this_).main_line_source_x, fmax( (*this_).source_end_x, (*this_).destination_end_x ) );
776 const double top = fmin ( (*this_).main_line_source_y, fmin( (*this_).source_end_y, (*this_).destination_end_y ) );
777 const double bottom = fmax ( (*this_).main_line_source_y, fmax( (*this_).source_end_y, (*this_).destination_end_y ) );
778
779 geometry_rectangle_init( &result,
780 left,
781 top,
782 right - left,
783 bottom - top
784 );
785
786 return result;
787 }
788
789 static inline geometry_rectangle_t geometry_connector_get_segment_bounds ( const geometry_connector_t *this_,
790 geometry_connector_segment_t segment_id )
791 {
792 assert( ( segment_id == GEOMETRY_CONNECTOR_SEGMENT_SOURCE )
793 ||( segment_id == GEOMETRY_CONNECTOR_SEGMENT_MAIN )||( segment_id == GEOMETRY_CONNECTOR_SEGMENT_DESTINATION ) );
794 geometry_rectangle_t result;
795
796 switch ( segment_id )
797 {
798 case GEOMETRY_CONNECTOR_SEGMENT_SOURCE:
799 {
800 const double left = fmin ( (*this_).main_line_source_x, (*this_).source_end_x );
801 const double right = fmax ( (*this_).main_line_source_x, (*this_).source_end_x );
802 const double top = fmin ( (*this_).main_line_source_y, (*this_).source_end_y );
803 const double bottom = fmax ( (*this_).main_line_source_y, (*this_).source_end_y );
804 geometry_rectangle_init( &result, left, top, right - left, bottom - top );
805 }
806 break;
807
808 case GEOMETRY_CONNECTOR_SEGMENT_MAIN:
809 default:
810 {
811 const double left = fmin ( (*this_).main_line_source_x, (*this_).main_line_destination_x );
812 const double right = fmax ( (*this_).main_line_source_x, (*this_).main_line_destination_x );
813 const double top = fmin ( (*this_).main_line_source_y, (*this_).main_line_destination_y );
814 const double bottom = fmax ( (*this_).main_line_source_y, (*this_).main_line_destination_y );
815 geometry_rectangle_init( &result, left, top, right - left, bottom - top );
816 }
817 break;
818
819 case GEOMETRY_CONNECTOR_SEGMENT_DESTINATION:
820 {
821 const double left = fmin ( (*this_).destination_end_x, (*this_).main_line_destination_x );
822 const double right = fmax ( (*this_).destination_end_x, (*this_).main_line_destination_x );
823 const double top = fmin ( (*this_).destination_end_y, (*this_).main_line_destination_y );
824 const double bottom = fmax ( (*this_).destination_end_y, (*this_).main_line_destination_y );
825 geometry_rectangle_init( &result, left, top, right - left, bottom - top );
826 }
827 break;
828 }
829
830 return result;
831 }
832
833 static inline geometry_3dir_t geometry_connector_get_directions ( const geometry_connector_t *this_ )
834 {
835 const double very_small = 0.000000001;
836
837 const double src_dx = (*this_).main_line_source_x - (*this_).source_end_x;
838 const double src_dy = (*this_).main_line_source_y - (*this_).source_end_y;
839 const double abs_src_dx = fabs( src_dx );
840 const double abs_src_dy = fabs( src_dy );
841 const bool src_exists = (( abs_src_dx > very_small )||( abs_src_dy > very_small ));
842 const bool src_is_horizontal = ( abs_src_dx > abs_src_dy );
843
844 const geometry_direction_t src_dir
845 = ( ! src_exists )
846 ? GEOMETRY_DIRECTION_CENTER
847 : src_is_horizontal
848 ? (( src_dx > 0.0 ) ? GEOMETRY_DIRECTION_RIGHT : GEOMETRY_DIRECTION_LEFT )
849 : (( src_dy > 0.0 ) ? GEOMETRY_DIRECTION_DOWN : GEOMETRY_DIRECTION_UP );
850
851 const double main_dx = (*this_).main_line_destination_x - (*this_).main_line_source_x;
852 const double main_dy = (*this_).main_line_destination_y - (*this_).main_line_source_y;
853 const double abs_main_dx = fabs( main_dx );
854 const double abs_main_dy = fabs( main_dy );
855 const bool main_exists = (( abs_main_dx > very_small )||( abs_main_dy > very_small ));
856 const bool main_is_horizontal = ( abs_main_dx > abs_main_dy );
857
858 const geometry_direction_t main_dir
859 = ( ! main_exists )
860 ? GEOMETRY_DIRECTION_CENTER
861 : main_is_horizontal
862 ? (( main_dx > 0.0 ) ? GEOMETRY_DIRECTION_RIGHT : GEOMETRY_DIRECTION_LEFT )
863 : (( main_dy > 0.0 ) ? GEOMETRY_DIRECTION_DOWN : GEOMETRY_DIRECTION_UP );
864
865 const double dst_dx = (*this_).destination_end_x - (*this_).main_line_destination_x;
866 const double dst_dy = (*this_).destination_end_y - (*this_).main_line_destination_y;
867 const double abs_dst_dx = fabs( dst_dx );
868 const double abs_dst_dy = fabs( dst_dy );
869 const bool dst_exists = (( abs_dst_dx > very_small )||( abs_dst_dy > very_small ));
870 const bool dst_is_horizontal = ( abs_dst_dx > abs_dst_dy );
871
872 const geometry_direction_t dst_dir
873 = ( ! dst_exists )
874 ? GEOMETRY_DIRECTION_CENTER
875 : dst_is_horizontal
876 ? (( dst_dx > 0.0 ) ? GEOMETRY_DIRECTION_RIGHT : GEOMETRY_DIRECTION_LEFT )
877 : (( dst_dy > 0.0 ) ? GEOMETRY_DIRECTION_DOWN : GEOMETRY_DIRECTION_UP );
878
879 const geometry_3dir_t result = geometry_3dir_new( src_dir, main_dir, dst_dir );
880 return result;
881 }
882
883 static inline void geometry_connector_trace ( const geometry_connector_t *this_ )
884 {
885 static const char geometry_mnemomic[GEOMETRY_DIRECTION_MAX]
886 = { '.', '<', 'F', '^', '7', '>', 'J', 'v', 'L' };
887
888 const geometry_3dir_t pattern = geometry_connector_get_directions( this_ );
889 const char pattern_as_str[6]
890 = { [0] = geometry_mnemomic[pattern.first],
891 [1] = ' ',
892 [2] = geometry_mnemomic[pattern.second],
893 [3] = ' ',
894 [4] = geometry_mnemomic[pattern.third],
895 [5] = '\0' };
896
897 U8_TRACE_INFO( "geometry_connector_t" );
898 U8_TRACE_INFO_INT( "- source_end_x:", (*this_).source_end_x );
899 U8_TRACE_INFO_INT( "- source_end_y:", (*this_).source_end_y );
900 U8_TRACE_INFO_INT( "- main_line_source_x:", (*this_).main_line_source_x );
901 U8_TRACE_INFO_INT( "- main_line_source_y:", (*this_).main_line_source_y );
902 U8_TRACE_INFO_INT( "- main_line_destination_x:", (*this_).main_line_destination_x );
903 U8_TRACE_INFO_INT( "- main_line_destination_y:", (*this_).main_line_destination_y );
904 U8_TRACE_INFO_INT( "- destination_end_x:", (*this_).destination_end_x );
905 U8_TRACE_INFO_INT( "- destination_end_y:", (*this_).destination_end_y );
906 U8_TRACE_INFO_STR( "- get_directions:", pattern_as_str );
907 }
908
909
910 /*
911 Copyright 2017-2026 Andreas Warnke
912
913 Licensed under the Apache License, Version 2.0 (the "License");
914 you may not use this file except in compliance with the License.
915 You may obtain a copy of the License at
916
917 http://www.apache.org/licenses/LICENSE-2.0
918
919 Unless required by applicable law or agreed to in writing, software
920 distributed under the License is distributed on an "AS IS" BASIS,
921 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
922 See the License for the specific language governing permissions and
923 limitations under the License.
924 */