-
Notifications
You must be signed in to change notification settings - Fork 0
/
htgeom_types.cpp
662 lines (594 loc) · 13.6 KB
/
htgeom_types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/* -----------------------------------------------------------------------------
* The Cyberiada Hierarchical Tree Geometry library implemention
*
* The the hierarchiceal tree geometry library
*
* Copyright (C) 2024 Alexey Fedoseev <aleksey@fedoseev.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "htgeom.h"
#include "htgeom_types.h"
#define MAX_STR_LEN 4096
#define OSTREAM std::cerr
int htree_copy_string(char** target, size_t* size, const char* source)
{
char* target_str;
size_t strsize;
if (!source) {
*target = NULL;
*size = 0;
return HTREE_OK;
}
strsize = strlen(source);
if (strsize > MAX_STR_LEN - 1) {
strsize = MAX_STR_LEN - 1;
}
target_str = (char*)malloc(strsize + 1);
strcpy(target_str, source);
target_str[strsize] = 0;
*target = target_str;
if (size) {
*size = strsize;
}
return HTREE_OK;
}
HTreePoint* htree_new_point(void)
{
HTreePoint* p = (HTreePoint*)malloc(sizeof(HTreePoint));
memset(p, 0, sizeof(HTreePoint));
return p;
}
int htree_set_point(HTreePoint* dst, HTreePoint* src)
{
if (!dst || !src) {
return HTREE_BAD_PARAMETER;
}
dst->x = src->x;
dst->y = src->y;
return HTREE_OK;
}
HTreePoint* htree_copy_point(HTreePoint* src)
{
HTreePoint* dst;
if (!src) {
return NULL;
}
dst = htree_new_point();
htree_set_point(dst, src);
return dst;
}
static void htree_update_point(HTreePoint* p, double dx, double dy)
{
if (p) {
p->x += dx;
p->y += dy;
}
}
static double round_number(double num, unsigned int signs)
{
double factor = 1.0;
for (unsigned int i = 0; i < signs; i++) {
factor *= 10.0;
}
double value = (int)(num * factor + .5);
return (double)value / factor;
}
int htree_round_point(HTreePoint* p, unsigned int signs)
{
if (!p) {
return HTREE_BAD_PARAMETER;
}
p->x = round_number(p->x, signs);
p->y = round_number(p->y, signs);
return HTREE_OK;
}
int htree_destroy_point(HTreePoint* p)
{
if (!p) {
return HTREE_BAD_PARAMETER;
}
free(p);
return HTREE_OK;
}
HTreePoint* htree_rect_center_point(const HTreeRect* r, HTCoordFormat geometry_format)
{
if (!r) {
return NULL;
}
HTreePoint* point = htree_new_point();
if (geometry_format == coordLocalCenter) {
point->x = r->x;
point->y = r->y;
} else {
point->x = r->x + r->width / 2.0;
point->y = r->y + r->height / 2.0;
}
return point;
}
int htree_print_point(const HTreeRect* r)
{
OSTREAM << r;
return HTREE_OK;
}
HTreeRect* htree_new_rect(void)
{
HTreeRect* r = (HTreeRect*)malloc(sizeof(HTreeRect));
memset(r, 0, sizeof(HTreeRect));
return r;
}
int htree_set_rect(HTreeRect* dst, HTreeRect* src)
{
if (!src || !dst) {
return HTREE_BAD_PARAMETER;
}
dst->x = src->x;
dst->y = src->y;
dst->width = src->width;
dst->height = src->height;
return HTREE_OK;
}
HTreeRect* htree_copy_rect(HTreeRect* src)
{
HTreeRect* dst;
if (!src) {
return NULL;
}
dst = htree_new_rect();
htree_set_rect(dst, src);
return dst;
}
int htree_init_rect(HTreeRect* rect)
{
if (!rect) {
return HTREE_BAD_PARAMETER;
}
memset(rect, 0, sizeof(HTreeRect));
return HTREE_OK;
}
int htree_empty_rect(const HTreeRect* r)
{
if (!r) return 0;
return r->x == 0.0 && r->y == 0.0 && r->width == 0.0 && r->height == 0.0;
}
int htree_compare_rects(HTreeRect* a, HTreeRect* b)
{
if (!a || !b) return -1;
return a->x != b->x || a->y != b->y || a->width != b->width || a->height != b->height;
}
static void htree_update_rect(HTreeRect* r, double dx, double dy)
{
if (!r) return ;
r->x += dx;
r->y += dy;
}
int htree_round_rect(HTreeRect* r, unsigned int signs)
{
if (!r) return HTREE_BAD_PARAMETER;
r->x = round_number(r->x, signs);
r->y = round_number(r->y, signs);
r->width = round_number(r->width, signs);
r->height = round_number(r->height, signs);
return HTREE_OK;
}
int htree_destroy_rect(HTreeRect* r)
{
if (!r) return HTREE_BAD_PARAMETER;
free(r);
return HTREE_OK;
}
int htree_print_rect(const HTreeRect* r)
{
OSTREAM << r;
return HTREE_OK;
}
HTreePolyline* htree_new_polyline(void)
{
HTreePolyline* pl = (HTreePolyline*)malloc(sizeof(HTreePolyline));
memset(pl, 0, sizeof(HTreePolyline));
return pl;
}
int htree_set_polyline(HTreePolyline* dst, HTreePolyline* src)
{
HTreePolyline *dst_pl, *dst_prev, *src_pl;
if (!dst || !src) {
return HTREE_BAD_PARAMETER;
}
src_pl = src;
dst_pl = dst;
dst_prev = NULL;
while (src_pl) {
if (!dst_pl) {
dst_pl = htree_new_polyline();
dst_prev->next = dst_pl;
}
dst_prev = dst_pl;
htree_set_point(&(dst_pl->point), &(src_pl->point));
dst_pl = dst_pl->next;
src_pl = src_pl->next;
}
if (dst_pl) {
dst_prev->next = NULL;
htree_destroy_polyline(dst_pl);
}
return HTREE_OK;
}
HTreePolyline* htree_copy_polyline(HTreePolyline* src)
{
HTreePolyline *dst = NULL, *prev, *pl;
if (!src) {
return NULL;
}
do {
pl = htree_new_polyline();
htree_set_point(&(pl->point), &(src->point));
if (dst) {
prev->next = pl;
} else {
dst = pl;
}
prev = pl;
src = src->next;
} while (src);
return dst;
}
static void htree_update_polyline(HTreePolyline* pl, double dx, double dy)
{
if (!pl) return ;
do {
htree_update_point(&(pl->point), dx, dy);
pl = pl->next;
} while (pl);
}
int htree_destroy_polyline(HTreePolyline* polyline)
{
if (!polyline) return HTREE_BAD_PARAMETER;
HTreePolyline* pl;
do {
pl = polyline;
polyline = polyline->next;
free(pl);
} while (polyline);
return HTREE_OK;
}
HTreeNode* htree_new_node(HTNodeType node_type, const char* _id)
{
HTreeNode* new_node = (HTreeNode*)malloc(sizeof(HTreeNode));
memset(new_node, 0, sizeof(HTreeNode));
htree_copy_string(&(new_node->id), &(new_node->id_len), _id);
new_node->type = node_type;
return new_node;
}
HTreeNode* htree_copy_node(HTreeNode* src)
{
HTreeNode *dst, *n, *dst_child, *src_child;
if (!src || !(src->id)) {
return NULL;
}
dst = htree_new_node(src->type, src->id);
if (src->point) {
dst->point = htree_copy_point(src->point);
}
if (src->rect) {
dst->rect = htree_copy_rect(src->rect);
}
if (src->children) {
for (src_child = src->children; src_child; src_child = src_child->next) {
dst_child = htree_copy_node(src_child);
dst_child->parent = dst;
if (dst->children) {
n = dst->children;
while (n->next) n = n->next;
n->next = dst_child;
} else {
dst->children = dst_child;
}
}
}
return dst;
}
HTreeNode* htree_find_node_by_id(HTreeNode* root, const char* id)
{
HTreeNode* node;
HTreeNode* found;
for (node = root; node; node = node->next) {
if (strcmp(node->id, id) == 0) {
return node;
}
if (node->children) {
found = htree_find_node_by_id(node->children, id);
if (found) return found;
}
}
return NULL;
}
static int htree_destroy_all_nodes(HTreeNode* node);
int htree_destroy_node(HTreeNode* node)
{
if(node != NULL) {
if (node->id) free(node->id);
if (node->children) {
htree_destroy_all_nodes(node->children);
}
if (node->point) free(node->point);
if (node->rect) free(node->rect);
free(node);
}
return HTREE_OK;
}
static int htree_destroy_all_nodes(HTreeNode* node)
{
HTreeNode* n;
if(node != NULL) {
do {
n = node;
node = node->next;
htree_destroy_node(n);
} while (node);
}
return HTREE_OK;
}
HTreeEdge* htree_new_edge(const char* _id, const char* source_id, const char* target_id)
{
HTreeEdge* new_edge = (HTreeEdge*)malloc(sizeof(HTreeEdge));
memset(new_edge, 0, sizeof(HTreeEdge));
htree_copy_string(&(new_edge->id), &(new_edge->id_len), _id);
htree_copy_string(&(new_edge->source_id), &(new_edge->source_id_len), source_id);
htree_copy_string(&(new_edge->target_id), &(new_edge->target_id_len), target_id);
return new_edge;
}
HTreeEdge* htree_copy_edge(HTreeEdge* src)
{
HTreeEdge* dst;
if (!src) {
return NULL;
}
dst = htree_new_edge(src->id, src->source_id, src->target_id);
/* if (src->abs_source_rect) {
dst->abs_source_rect = htree_copy_rect(src->abs_source_rect);
}
if (src->abs_target_rect) {
dst->abs_target_rect = htree_copy_rect(src->abs_target_rect);
}*/
if (src->polyline) {
dst->polyline = htree_copy_polyline(src->polyline);
}
if (src->source_point) {
dst->source_point = htree_copy_point(src->source_point);
}
if (src->target_point) {
dst->target_point = htree_copy_point(src->target_point);
}
if (src->label_point) {
dst->label_point = htree_copy_point(src->label_point);
}
if (src->label_rect) {
dst->label_rect = htree_copy_rect(src->label_rect);
}
return dst;
}
int htree_destroy_edge(HTreeEdge* e)
{
if (!e) {
return HTREE_BAD_PARAMETER;
}
if (e->id) free(e->id);
if (e->source_id) free(e->source_id);
if (e->target_id) free(e->target_id);
// if (e->abs_source_rect) free(e->abs_source_rect);
// if (e->abs_target_rect) free(e->abs_target_rect);
if (e->polyline) {
htree_destroy_polyline(e->polyline);
}
if (e->source_point) htree_destroy_point(e->source_point);
if (e->target_point) htree_destroy_point(e->target_point);
if (e->label_point) htree_destroy_point(e->label_point);
if (e->label_rect) htree_destroy_rect(e->label_rect);
free(e);
return HTREE_OK;
}
HTree* htree_new_tree(void)
{
HTree* tree = (HTree*)malloc(sizeof(HTree));
memset(tree, 0, sizeof(HTree));
return tree;
}
HTree* htree_copy_tree(HTree* src)
{
HTree *result = NULL, *dst;
HTreeNode* node, *new_node, *prev_node;
HTreeEdge *edge, *new_edge, *prev_edge;
while (src) {
dst = htree_new_tree();
if (result) {
HTree* t = result;
while (t->next) t = t->next;
t->next = dst;
} else {
result = dst;
}
if (src->nodes) {
node = src->nodes;
while (node) {
new_node = htree_copy_node(node);
if (dst->nodes) {
prev_node->next = new_node;
} else {
dst->nodes = new_node;
}
prev_node = new_node;
node = node->next;
}
}
if (src->edges) {
edge = src->edges;
while (edge) {
new_edge = htree_copy_edge(edge);
if (dst->edges) {
prev_edge->next = new_edge;
} else {
dst->edges = new_edge;
}
prev_edge = new_edge;
edge = edge->next;
}
}
edge = dst->edges;
/* reconstruct source/target nodes */
while (edge) {
HTreeNode* source = htree_find_node_by_id(dst->nodes, edge->source_id);
HTreeNode* target = htree_find_node_by_id(dst->nodes, edge->target_id);
if (!source || !target) {
htree_destroy_tree(result);
return NULL;
}
edge->source = source;
edge->target = target;
edge = edge->next;
}
src = src->next;
}
return dst;
}
int htree_destroy_tree(HTree* tree)
{
HTree *t;
HTreeEdge *edge, *e;
while (tree) {
if (tree->nodes) {
htree_destroy_all_nodes(tree->nodes);
}
if (tree->edges) {
edge = tree->edges;
do {
e = edge;
edge = edge->next;
htree_destroy_edge(e);
} while (edge);
}
t = tree;
tree = tree->next;
free(t);
}
return HTREE_OK;
}
int htree_node_has_geometry(const HTreeNode* node)
{
while (node) {
if (node->point || node->rect) {
return 1;
}
if (node->children) {
if (htree_node_has_geometry(node->children)) {
return 1;
}
}
node = node->next;
}
return 0;
}
int htree_node_has_toplevel_geometry(const HTreeNode* node)
{
int found = 0;
for (const HTreeNode* n = node; n; n = n->next) {
if (n->point || n->rect) {
if (found) return 0;
found = 1;
}
}
if (found) return 1;
for (const HTreeNode* n = node; n; n = n->next) {
if (n->children) {
if (htree_node_has_toplevel_geometry(n->children)) {
if (found) return 0;
found = 1;
}
}
}
return found;
}
int htree_tree_has_geometry(const HTree* tree)
{
if (!tree) {
return 0;
}
for (; tree; tree = tree->next) {
if (htree_node_has_geometry(tree->nodes)) {
return 1;
}
for (const HTreeEdge* edge = tree->edges; edge; edge = edge->next) {
if (edge->polyline ||
edge->source_point ||
edge->target_point ||
edge->label_point ||
edge->label_rect) {
return 1;
}
}
}
return 0;
}
HTDocument* htree_new_document(HTCoordFormat _node_coord_format,
HTCoordFormat _edge_coord_format,
HTCoordFormat _edge_pl_coord_format,
HTEdgeFormat _edge_format)
{
HTDocument* doc = (HTDocument*)malloc(sizeof(HTDocument));
memset(doc, 0, sizeof(HTDocument));
doc->node_coord_format = _node_coord_format;
doc->edge_coord_format = _edge_coord_format;
doc->edge_pl_coord_format = _edge_pl_coord_format;
doc->edge_format = _edge_format;
return doc;
}
HTDocument* htree_copy_document(HTDocument* src)
{
HTDocument* dst;
if (!src) {
return NULL;
}
dst = htree_new_document(src->node_coord_format,
src->edge_coord_format,
src->edge_pl_coord_format,
src->edge_format);
if (src->trees) {
dst->trees = htree_copy_tree(src->trees);
}
if (src->bounding_rect) {
dst->bounding_rect = htree_copy_rect(src->bounding_rect);
}
return dst;
}
int htree_destroy_document(HTDocument* doc)
{
if (doc) {
if (doc->trees) {
htree_destroy_tree(doc->trees);
}
if (doc->bounding_rect) {
htree_destroy_rect(doc->bounding_rect);
}
free(doc);
}
return HTREE_OK;
}
int htree_print_document(const HTDocument* doc)
{
OSTREAM << doc << std::endl;
return HTREE_OK;
}