-
Notifications
You must be signed in to change notification settings - Fork 2
/
maker.c
2412 lines (2159 loc) · 83.4 KB
/
maker.c
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "LoftyCAD.h"
#include <CommCtrl.h>
#include <CommDlg.h>
#include <stdio.h>
// Routines to build up faces and volumes.
// Search for a closed chain of connected edges (having coincident endpoints within tol)
// and if found, make a group out of them. The chain may be closed or open.
Group*
group_connected_edges(Edge* edge)
{
Object* obj, * nextobj;
Edge* e;
Group* group, *parent_group;
int n_edges = 0;
int pass;
typedef struct End // an end of the growing chain
{
int which_end; // endpoint 0 or 1
Edge* edge; // which edge it is on
} End;
End end0, end1;
BOOL legit = FALSE;
int max_passes = 0;
if (((Object*)edge)->type != OBJ_EDGE)
return NULL;
// The edge group will go into the parent group of the edges.
// Along the way we will check that they are all in fact in the same group, just to
// be paranoid.
parent_group = ((Object*)edge)->parent_group;
if (parent_group == NULL)
return NULL;
// Put the first edge in the list, removing it from its group.
// It had better be in the group to start with! Check to be sure,
// and while here, find out how many top-level edges there are as an
// upper bound on passes later on.
for (obj = parent_group->obj_list.head; obj != NULL; obj = obj->next)
{
if (obj->type == OBJ_EDGE)
{
max_passes++;
if (obj == (Object*)edge)
legit = TRUE;
}
}
if (!legit)
return NULL;
// make a group for the edges
group = group_new();
delink_group((Object*)edge, parent_group);
link_group((Object*)edge, group);
end0.edge = edge;
end0.which_end = 0;
end1.edge = edge;
end1.which_end = 1;
// Make passes over the tree, greedy-grouping edges as they are found to connect.
// If the chain is closed, we should add at least 2 edges per pass, and could add
// a lot more than that with favourable ordering.
for (pass = 0; pass < max_passes; pass++)
{
BOOL advanced = FALSE;
for (obj = parent_group->obj_list.head; obj != NULL; obj = nextobj)
{
nextobj = obj->next;
if (obj->type != OBJ_EDGE)
continue;
if (obj->parent_group != parent_group)
continue;
e = (Edge*)obj;
if (near_pt(e->endpoints[0], end0.edge->endpoints[end0.which_end], snap_tol))
{
// endpoint 0 of obj connects to end0. Put obj in the list.
delink_group(obj, parent_group);
link_group(obj, group);
// Share the endpoints. Return unused points to the free list.
purge_obj((Object *)e->endpoints[0]);
e->endpoints[0] = end0.edge->endpoints[end0.which_end];
// Update end0 to point to the other end of the new edge we just added
end0.edge = e;
end0.which_end = 1;
n_edges++;
advanced = TRUE;
}
// Check for endpoint 1 connecting to end0 similarly
else if (near_pt(e->endpoints[1], end0.edge->endpoints[end0.which_end], snap_tol))
{
delink_group(obj, parent_group);
link_group(obj, group);
purge_obj((Object*)e->endpoints[1]);
e->endpoints[1] = end0.edge->endpoints[end0.which_end];
end0.edge = e;
end0.which_end = 0;
n_edges++;
advanced = TRUE;
}
// And the same for end1. New edges are linked at the tail.
else if (near_pt(e->endpoints[0], end1.edge->endpoints[end1.which_end], snap_tol))
{
delink_group(obj, parent_group);
link_tail_group(obj, group);
purge_obj((Object*)e->endpoints[0]);
e->endpoints[0] = end1.edge->endpoints[end1.which_end];
end1.edge = e;
end1.which_end = 1;
n_edges++;
advanced = TRUE;
}
else if (near_pt(e->endpoints[1], end1.edge->endpoints[end1.which_end], snap_tol))
{
delink_group(obj, parent_group);
link_tail_group(obj, group);
purge_obj((Object*)e->endpoints[1]);
e->endpoints[1] = end1.edge->endpoints[end1.which_end];
end1.edge = e;
end1.which_end = 0;
n_edges++;
advanced = TRUE;
}
if (near_pt(end0.edge->endpoints[end0.which_end], end1.edge->endpoints[end1.which_end], snap_tol))
{
// We have closed the chain.
purge_obj((Object*)end0.edge->endpoints[end0.which_end]);
end0.edge->endpoints[end0.which_end] = end1.edge->endpoints[end1.which_end];
group->hdr.lock = LOCK_POINTS;
return group;
}
}
// Every pass should advance at least one of the ends. Break out if we can't.
if (!advanced)
break;
}
group->hdr.lock = LOCK_POINTS;
return group;
}
// Check if a group is an edge group. Very cursory check.
BOOL
is_edge_group(Group* group)
{
if (group == NULL)
return FALSE;
if (group->hdr.type != OBJ_GROUP)
return FALSE;
if (group->hdr.lock > LOCK_EDGES)
return FALSE;
if (group->obj_list.head == NULL)
return FALSE;
if (group->obj_list.head->type != OBJ_EDGE)
return FALSE;
return TRUE;
}
// Check if an edge group is closed. Do this by matching up endpoints.
// Take special care with groups having only one or two elements.
BOOL
is_closed_edge_group(Group* group)
{
Edge* first, * last;
if (!is_edge_group(group))
return FALSE;
first = (Edge*)group->obj_list.head;
last = (Edge*)group->obj_list.tail;
if (first == last) // one element, it's always open
return FALSE;
else if ((Edge *)first->hdr.next == last) // two elements
{
if (first->endpoints[0] == last->endpoints[0] && first->endpoints[1] == last->endpoints[1])
return TRUE;
if (first->endpoints[0] == last->endpoints[1] && first->endpoints[1] == last->endpoints[0])
return TRUE;
}
else
{
// Otherwise, just check that there's a match between any endpoints
// of the head and tail edges.
if (first->endpoints[0] == last->endpoints[0])
return TRUE;
if (first->endpoints[0] == last->endpoints[1])
return TRUE;
if (first->endpoints[1] == last->endpoints[0])
return TRUE;
if (first->endpoints[1] == last->endpoints[1])
return TRUE;
}
return FALSE;
}
// Disconnect the shared points of all the edges in an edge group, in preparation
// for ungrouping them.
void
disconnect_edges_in_group(Group* group)
{
Edge* e, *next_edge;
Point* pt;
int initial, final;
if (!is_edge_group(group))
return;
// Find the initial point
e = (Edge*)group->obj_list.head;
next_edge = (Edge*)group->obj_list.head->next;
if (e->endpoints[0] == next_edge->endpoints[0])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[0] == next_edge->endpoints[1])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[1] == next_edge->endpoints[0])
{
initial = 0;
pt = e->endpoints[1];
}
else if (e->endpoints[1] == next_edge->endpoints[1])
{
initial = 0;
pt = e->endpoints[1];
}
else
{
return; // edges are not connected
}
for (; e->hdr.next != NULL; e = next_edge)
{
next_edge = (Edge*)e->hdr.next;
if (next_edge->endpoints[0] == pt)
{
next_edge->endpoints[0] = point_newp(pt);
final = 1;
}
else if (next_edge->endpoints[1] == pt)
{
next_edge->endpoints[1] = point_newp(pt);
final = 0;
}
else
{
return;
}
pt = next_edge->endpoints[final];
}
e = (Edge*)group->obj_list.head;
if (pt == e->endpoints[initial])
e->endpoints[initial] = point_newp(pt);
}
// Make a face object out of a closed group of connected edges.
// If requested, the original group is cleared and should be deleted by the caller.
// Reversing can be automatic (test against facing plane) or user controlled.
Face*
make_face(Group* group, BOOL clear_group, BOOL auto_reverse, BOOL reverse)
{
Face* face = NULL;
Edge* e, * next_edge, * prev_edge;
Plane norm;
int initial, final, n_edges;
ListHead plist = { NULL, NULL };
Point* pt;
int i;
FACE face_type;
EDGE edge_types[4], min_type, max_type;
// Check (quickly) that the group is closed.
if (!is_closed_edge_group(group))
return NULL;
// Determine normal of points gathered up so far. From this we decide
// which order to build the final edge array on the face. Join the
// endpoints up into a list (the next/prev pointers aren't used for
// anything else)
e = (Edge*)group->obj_list.head;
next_edge = (Edge*)group->obj_list.head->next;
if (e->endpoints[0] == next_edge->endpoints[0])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[0] == next_edge->endpoints[1])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[1] == next_edge->endpoints[0])
{
initial = 0;
pt = e->endpoints[1];
}
else if (e->endpoints[1] == next_edge->endpoints[1])
{
initial = 0;
pt = e->endpoints[1];
}
else
{
ASSERT(FALSE, "The edges aren't connected");
return NULL;
}
link_tail((Object*)e->endpoints[initial], &plist);
n_edges = 1;
for (; e->hdr.next != NULL; e = next_edge)
{
next_edge = (Edge*)e->hdr.next;
if (e->hdr.next->type != OBJ_EDGE)
return NULL;
// Strip construction edges, as we can't consistently create them
e->type &= ~EDGE_CONSTRUCTION;
if (next_edge->endpoints[0] == pt)
{
link_tail((Object*)next_edge->endpoints[0], &plist);
final = 1;
}
else if (next_edge->endpoints[1] == pt)
{
link_tail((Object*)next_edge->endpoints[1], &plist);
final = 0;
}
else
{
return NULL;
}
pt = next_edge->endpoints[final];
n_edges++;
}
// Get the normal and see if we need to reverse. This depends on the face being generally
// in one plane (it may be a bit curved, but a full circle cylinder will confuse things..)
// Automatic reversing is done by testing against the facing plane.
polygon_normal((Point*)plist.head, &norm);
if (auto_reverse)
reverse = dot(norm.A, norm.B, norm.C, facing_plane->A, facing_plane->B, facing_plane->C) < 0;
if (reverse)
{
norm.A = -norm.A;
norm.B = -norm.B;
norm.C = -norm.C;
}
norm.refpt = *((Edge*)group->obj_list.head)->endpoints[initial];
// Add any Bezier control points and circle centres into the points list
// so we can test them for being in the same plane later on
for (e = (Edge*)group->obj_list.head; e != NULL; e = (Edge*)e->hdr.next)
{
switch (e->type)
{
case EDGE_ARC:
link_tail((Object*)((ArcEdge*)e)->centre, &plist);
break;
case EDGE_BEZIER:
link_tail((Object*)((BezierEdge*)e)->ctrlpoints[0], &plist);
link_tail((Object*)((BezierEdge*)e)->ctrlpoints[1], &plist);
break;
}
}
// Make the face, of whatever type matches the input edges
// Determine if the face is really flat by checking distance to a plane defined
// by the normal we found above.
max_type = EDGE_STRAIGHT;
min_type = EDGE_BEZIER;
if (n_edges != 4)
{
// More than 4 edges cannot be a curved face, so make it flat
face_type = (n_edges == 3) ? FACE_TRI : FACE_FLAT;
}
else if (polygon_planar((Point*)plist.head, &norm))
{
// If all edges lie in one plane, make it flat too
face_type = FACE_FLAT;
}
else
{
// We have a non-flat face bounded by 4 edges. Find the different edge types
for (i = 0, e = (Edge*)group->obj_list.head; e != NULL; e = (Edge*)e->hdr.next, i++)
{
edge_types[i] = e->type;
if (e->type < min_type)
min_type = e->type;
if (e->type > max_type)
max_type = e->type;
}
ASSERT(i == 4, "Already tested this");
if (edge_types[0] != edge_types[2] || edge_types[1] != edge_types[3] || max_type == EDGE_STRAIGHT)
face_type = FACE_FLAT; // must be flat, even if out of plane
else if (min_type == EDGE_STRAIGHT)
face_type = FACE_CYLINDRICAL; // two straight, other two arc or bez
else if (min_type == EDGE_ARC)
face_type = FACE_BARREL; // two arc, other two arc or bez
else if (min_type == EDGE_BEZIER)
face_type = FACE_BEZIER; // four bez
// Make sure opposing pairs of curved edges have their step counts in agreement
for (i = 0, e = (Edge*)group->obj_list.head; e != NULL && i < 2; e = (Edge*)e->hdr.next, i++)
{
Edge* o = (Edge*)e->hdr.next->next;
int n_steps;
ASSERT(e->type == o->type, "Already tested this");
if (e->type >= EDGE_ARC)
{
// Set step counts to the maximum of the two.
n_steps = e->nsteps;
if (o->nsteps > n_steps)
n_steps = o->nsteps;
e->nsteps = n_steps;
o->nsteps = n_steps;
e->view_valid = FALSE;
o->view_valid = FALSE;
}
}
}
face = face_new(face_type, norm);
face->n_edges = n_edges;
if (face_type == FACE_FLAT)
{
while (face->max_edges <= n_edges)
face->max_edges <<= 1; // round up to a power of 2
if (face->max_edges > 16) // does it need any more than the default 16?
face->edges = realloc(face->edges, face->max_edges * sizeof(Edge*));
}
// Populate the edge list.
if (reverse)
{
face->initial_point = next_edge->endpoints[final];
for (i = 0, e = next_edge; e != NULL; e = prev_edge, i++)
{
prev_edge = (Edge*)e->hdr.prev;
face->edges[i] = e;
if (clear_group)
delink_group((Object*)e, group);
}
}
else
{
face->initial_point = ((Edge*)group->obj_list.head)->endpoints[initial];
for (i = 0, e = (Edge*)group->obj_list.head; e != NULL; e = next_edge, i++)
{
next_edge = (Edge*)e->hdr.next;
face->edges[i] = e;
if (clear_group)
delink_group((Object*)e, group);
}
}
face->view_valid = FALSE;
// Group must be cleared out by now (delete will happen later)
// Update the view list. But if retaining the group, don't do
// anything else (it will be done later)
if (clear_group)
{
ASSERT(group->obj_list.head == NULL, "Edge group is not empty");
// Finally, update the view list for the face
gen_view_list_face(face);
}
return face;
}
// Insert an edge at a corner point; either a single straight (chamfer) or an arc (round).
// Optionally, restrict the radius or chamfer to prevent rounds colliding at short edges.
void
insert_chamfer_round(Point* pt, Face* parent, double size, EDGE edge_type, BOOL restricted)
{
Edge* e[2] = { NULL, NULL };
int i, k, end[2], eindex[2];
Point orig_pt = *pt;
Edge* ne;
double len0, len1, backoff;
if (parent->hdr.type != OBJ_FACE)
return; // We can't do this
// Find the two edges that share this point
k = 0;
for (i = 0; i < parent->n_edges; i++)
{
if (parent->edges[i]->endpoints[0] == pt)
{
e[k] = parent->edges[i];
eindex[k] = i;
end[k] = 0;
k++;
}
else if (parent->edges[i]->endpoints[1] == pt)
{
e[k] = parent->edges[i];
eindex[k] = i;
end[k] = 1;
k++;
}
}
if (k != 2)
{
ASSERT(FALSE, "Point should belong to 2 edges");
return; // something is wrong; the point does not belong to two edges
}
ASSERT(pt == e[0]->endpoints[end[0]], "Wtf?");
ASSERT(pt == e[1]->endpoints[end[1]], "Wtf?");
// Back the original point up by "size" along its edge. Watch short edges.
len0 = length(e[0]->endpoints[0], e[0]->endpoints[1]);
len1 = length(e[1]->endpoints[0], e[1]->endpoints[1]);
backoff = size;
if (restricted)
{
if (backoff > len0 / 3)
backoff = len0 / 3;
if (backoff > len1 / 3)
backoff = len1 / 3;
}
new_length(e[0]->endpoints[1 - end[0]], e[0]->endpoints[end[0]], len0 - backoff);
// Add a new point for the other edge
e[1]->endpoints[end[1]] = point_newp(&orig_pt);
// Back this one up too
new_length(e[1]->endpoints[1 - end[1]], e[1]->endpoints[end[1]], len1 - backoff);
// Add a new edge
ne = edge_new(edge_type);
ne->endpoints[0] = e[0]->endpoints[end[0]];
ne->endpoints[1] = e[1]->endpoints[end[1]];
if (edge_type == EDGE_ARC)
{
ArcEdge* ae = (ArcEdge*)ne;
ae->centre = point_new(0, 0, 0);
ae->normal = parent->normal;
if (!centre_2pt_tangent_circle(ne->endpoints[0], ne->endpoints[1], &orig_pt, &parent->normal, ae->centre, &ae->clockwise))
ne->type = EDGE_STRAIGHT;
}
// Grow the edges array if it won't take the new edge
if (parent->n_edges >= parent->max_edges)
{
while (parent->max_edges <= parent->n_edges)
parent->max_edges <<= 1; // round up to a power of 2
parent->edges = realloc(parent->edges, parent->max_edges * sizeof(Edge*));
}
// Shuffle the edges array down and insert it
// Special case if e[] = 0 and n_edges-1, insert at the end
if (eindex[0] == 0 && eindex[1] == parent->n_edges - 1)
{
parent->edges[parent->n_edges] = ne;
}
else
{
for (k = parent->n_edges - 1; k >= eindex[1]; --k)
parent->edges[k + 1] = parent->edges[k];
parent->edges[eindex[1]] = ne;
}
parent->n_edges++;
ne->corner = TRUE; // mark this as a corner edge
}
// Helper to find the greatest radius to the path within the points of
// an edge's view list.
double
dist_view_list_to_path(Edge* e, Edge* path)
{
double r;
double rad = 0;
Point* p;
Point dummy;
if (e->type == EDGE_ARC || e->type == EDGE_BEZIER)
{
for (p = (Point *)e->view_list.head; p != NULL; p = (Point *)p->hdr.next)
{
r = dist_point_to_perp_line(p, path, &dummy);
if (r > rad)
rad = r;
}
}
return rad;
}
// Make a body of revolution by revolving the given edge group around the
// current path. Open edge groups are completed with circles at the poles.
// If successful, the volume is returned. The edge group is retained.
// A negative volume (hole) may be generated.
Volume *
make_body_of_revolution(Group* group, BOOL negative)
{
ListHead plist = { NULL, NULL };
Group* clone, *orig;
Volume* vol;
Face* f;
Edge* e, *ne, *na, *o, *path, *prev_ne, *prev_na, *first_ne, *first_na;
ArcEdge* nae;
Point* eip, * oip, * old_eip, * old_oip, * first_eip, * first_oip;
Point *pt;
Point top_centre, bottom_centre;
Plane axis, group_norm, top_axis, outward;
int idx, initial, final, n_steps;
BOOL open = !is_closed_edge_group(group);
BOOL wind_reverse;
double r, rad;
// Some checks first.
if (!is_edge_group(group))
return NULL;
if (curr_path == NULL)
return NULL;
if (curr_path->type == OBJ_GROUP)
path = (Edge*)(((Group*)curr_path)->obj_list.head);
else
path = (Edge*)curr_path;
ASSERT(path->hdr.type == OBJ_EDGE && ((Edge *)path)->type == EDGE_STRAIGHT, "Path must be a straight edge");
if (path->hdr.type != OBJ_EDGE || ((Edge*)path)->type != EDGE_STRAIGHT)
return NULL;
// Find the initial point index.
// If there is only one edge, arbitrarily use endpoint 0 for the initial point.
e = (Edge*)group->obj_list.head;
ne = (Edge*)group->obj_list.head->next;
if (ne != NULL)
{
if (e->endpoints[0] == ne->endpoints[0])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[0] == ne->endpoints[1])
{
initial = 1;
pt = e->endpoints[0];
}
else if (e->endpoints[1] == ne->endpoints[0])
{
initial = 0;
pt = e->endpoints[1];
}
else if (e->endpoints[1] == ne->endpoints[1])
{
initial = 0;
pt = e->endpoints[1];
}
else
{
ASSERT(FALSE, "The edges aren't connected");
return NULL;
}
}
else
{
ne = e;
initial = 0;
pt = e->endpoints[1];
final = 1;
}
// Start remembering the radii to the axis, as well as the top and
// bottom points' centres, in case the group is open and we need to
// put in circle faces to close the volume.
rad = dist_point_to_perp_line(e->endpoints[initial], path, &top_centre);
r = dist_view_list_to_path(e, path);
if (r > rad)
rad = r;
// Connect up all the edges by sharing their common points, and remember
// the points in a list so we can easily find their normal and hence their
// winding direction
link_tail((Object*)e->endpoints[initial], &plist);
for (; e->hdr.next != NULL; e = ne)
{
ne = (Edge*)e->hdr.next;
if (e->hdr.next->type != OBJ_EDGE)
return NULL;
// Strip construction edges, as we can't consistently create them
e->type &= ~EDGE_CONSTRUCTION;
if (ne->endpoints[0] == pt)
{
link_tail((Object*)ne->endpoints[0], &plist);
final = 1;
}
else if (ne->endpoints[1] == pt)
{
link_tail((Object*)ne->endpoints[1], &plist);
final = 0;
}
else
{
return FALSE;
}
pt = ne->endpoints[final];
r = dist_point_to_perp_line(pt, path, &bottom_centre);
if (r > rad)
rad = r;
r = dist_view_list_to_path(ne, path);
if (r > rad)
rad = r;
}
r = dist_point_to_perp_line(ne->endpoints[final], path, &bottom_centre);
if (r > rad)
rad = r;
idx = initial;
// Determine the normal of the group points
// If group is open, we need to add in top_centre and bottom_centre
if (open)
{
link_tail((Object*)pt, &plist);
link_tail((Object*)&bottom_centre, &plist);
link_tail((Object*)&top_centre, &plist);
}
polygon_normal((Point*)plist.head, &group_norm);
// The plane derived from the path used as an axis
axis.A = path->endpoints[1]->x - path->endpoints[0]->x;
axis.B = path->endpoints[1]->y - path->endpoints[0]->y;
axis.C = path->endpoints[1]->z - path->endpoints[0]->z;
normalise_plane(&axis);
// Determine winding direction of the faces we are about to generate. It depends on
// the direction of the axis and the group normal (and also on whether we want
// a positive volume or a hole)
plcross(&group_norm, &axis, &outward);
// Find if the outward vector really points outward (away from the axis)
// and hence determine which way to wind the faces' edge ordering.
e = (Edge*)group->obj_list.head;
top_axis.A = e->endpoints[initial]->x - top_centre.x;
top_axis.B = e->endpoints[initial]->y - top_centre.y;
top_axis.C = e->endpoints[initial]->z - top_centre.z;
wind_reverse = (pldot(&outward, &top_axis) < 0) ^ negative;
// Work out the number of steps in all the arcs. They must all be the
// same, so use the worst case (from the largest radius to the axis gathered above)
n_steps = (int)(2 * PI / (2.0 * acos(1.0 - tolerance / rad)));
// Clone the edge list (twice) in the same location, and fix any edges' nsteps.
orig = (Group*)copy_obj((Object*)group, 0, 0, 0, TRUE);
clear_move_copy_flags((Object*)group);
clone = (Group*)copy_obj((Object*)group, 0, 0, 0, TRUE);
clear_move_copy_flags((Object*)group);
e = (Edge*)orig->obj_list.head;
o = (Edge *)clone->obj_list.head;
// Add the first pair of edges (a circle and a zero-width straight)
// and put the closing circle face in if the group is open.
eip = e->endpoints[idx];
oip = o->endpoints[idx];
prev_ne = edge_new(EDGE_STRAIGHT);
prev_ne->endpoints[0] = eip;
prev_ne->endpoints[1] = oip;
prev_na = edge_new(EDGE_ARC);
prev_na->endpoints[0] = oip;
prev_na->endpoints[1] = eip;
prev_na->nsteps = n_steps;
nae = (ArcEdge*)prev_na;
nae->centre = point_new(0, 0, 0);
dist_point_to_perp_line(oip, path, nae->centre);
nae->clockwise = FALSE;
nae->normal = axis;
nae->normal.refpt = *nae->centre;
old_eip = eip;
old_oip = oip;
first_ne = prev_ne;
first_na = prev_na;
first_eip = eip;
first_oip = oip;
idx = 1 - idx; // point to far end of edge in group
ne = na = NULL; // shhh compiler
vol = vol_new();
vol->hdr.lock = LOCK_FACES;
vol->op = negative ? OP_INTERSECTION : OP_UNION;
// Proceed down the edge group adding arc edges and barrel faces to the volume
for
(
e = (Edge *)orig->obj_list.head, o = (Edge*)clone->obj_list.head;
e != NULL;
e = (Edge*)e->hdr.next, o = (Edge*)o->hdr.next
)
{
Plane n = { 0, };
FACE ftype = (e->type == EDGE_STRAIGHT) ? FACE_RECT : FACE_CYLINDRICAL;
eip = e->endpoints[idx];
oip = o->endpoints[idx];
// If were rejoining to the initial point, use the first arc edge we generated
if (e->hdr.next == NULL && !open)
{
ne = first_ne;
na = first_na;
eip = first_eip;
oip = first_oip;
}
else
{
ne = edge_new(EDGE_STRAIGHT);
ne->endpoints[0] = eip;
ne->endpoints[1] = oip;
na = edge_new(EDGE_ARC);
na->endpoints[0] = oip;
na->endpoints[1] = eip;
na->nsteps = n_steps;
nae = (ArcEdge*)na;
nae->centre = point_new(0, 0, 0);
dist_point_to_perp_line(oip, path, nae->centre);
nae->clockwise = FALSE;
nae->normal = axis;
nae->normal.refpt = *nae->centre;
}
f = face_new(ftype, n);
f->n_edges = 4;
if (wind_reverse)
{
f->edges[0] = ne;
f->edges[1] = o;
f->edges[2] = prev_ne;
f->edges[3] = e;
f->initial_point = eip;
}
else
{
f->edges[0] = ne;
f->edges[1] = e;
f->edges[2] = prev_ne;
f->edges[3] = o;
f->initial_point = oip;
}
f->vol = vol;
link((Object*)f, &vol->faces);
if (ftype > vol->max_facetype)
vol->max_facetype = ftype;
ftype = (e->type == EDGE_STRAIGHT) ? FACE_CYLINDRICAL : FACE_BARREL;
f = face_new(ftype, n);
f->n_edges = 4;
if (wind_reverse)
{
f->edges[0] = prev_na;
f->edges[1] = e;
f->edges[2] = na;
f->edges[3] = o;
f->initial_point = old_oip;
}
else
{
f->edges[0] = prev_na;
f->edges[1] = o;
f->edges[2] = na;
f->edges[3] = e;
f->initial_point = old_eip;
}
f->vol = vol;
link((Object*)f, &vol->faces);
if (ftype > vol->max_facetype)
vol->max_facetype = ftype;
delink_group((Object*)e, orig);
delink_group((Object*)o, clone);
prev_ne = ne;
prev_na = na;
old_eip = eip;
old_oip = oip;
// Find the far end of the next pair of edges
if (e->hdr.next == NULL)
break;
ne = (Edge*)e->hdr.next;
if (e->endpoints[idx] == ne->endpoints[0])
idx = 1;
else
idx = 0;
}
// Add the first and last circles if group was open
if (open)
{
Face* circle;
Plane norm;
norm.A = top_centre.x - bottom_centre.x;
norm.B = top_centre.y - bottom_centre.y;
norm.C = top_centre.z - bottom_centre.z;
norm.refpt = top_centre;
normalise_plane(&norm);
if (negative)
{
norm.A = -norm.A;
norm.B = -norm.B;
norm.C = -norm.C;
}
circle = face_new(FACE_CIRCLE, norm);
if (wind_reverse)
{
circle->edges[0] = first_na;
circle->edges[1] = first_ne;
}
else
{
circle->edges[0] = first_ne;
circle->edges[1] = first_na;
}
circle->n_edges = 2;
circle->initial_point = first_eip;
circle->vol = vol;
link((Object*)circle, &vol->faces);
norm.A = -norm.A;
norm.B = -norm.B;
norm.C = -norm.C;
norm.refpt = bottom_centre;
circle = face_new(FACE_CIRCLE, norm);
if (wind_reverse)
{
circle->edges[0] = ne;
circle->edges[1] = na;
}
else
{
circle->edges[0] = na;
circle->edges[1] = ne;
}
circle->n_edges = 2;
circle->initial_point = eip;
circle->vol = vol;
link((Object*)circle, &vol->faces);
}
// Finally, remove the cloned edge groups
ASSERT(orig->obj_list.head == NULL, "Edge group is not empty");
ASSERT(clone->obj_list.head == NULL, "Edge group is not empty");
purge_obj((Object*)orig);
purge_obj((Object*)clone);
return vol;
}
// Struct describing an edge group, its normal and centroid, and distance along the
// path used for sorting.
typedef struct
{
Group* egrp; // Edge group at this section
Plane norm; // Normal of the edge group
double param; // How far (as a fraction) along the path or principal direction it is
Plane principal; // The plane normal to the path at this section
Bbox ebox; // BBox for the edge group
ListHead face_list; // List of faces making up the endcap (for nose and tail edge groups only)
Edge* opp_key; // Points to the opposite edge to the key edge (which is at the head of the list)
} LoftedGroup;
// qsort comparo function.
int