-
Notifications
You must be signed in to change notification settings - Fork 2
/
mover.c
1625 lines (1443 loc) · 49 KB
/
mover.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 <stdio.h>
// Rotation matrix cached.
static double rotate_3x3[9];
static BOOL rotate_3x3_valid = FALSE;
// Anything to do with moving and copying objects.
// Clear the moved and copied_to flags, and the window-coordinate valid flag,
// on all points referenced by the object.
// Call this after move_obj, copy_obj or the rotate/reflect functions.
void
clear_move_copy_flags(Object* obj)
{
int i;
Point* p;
EDGE type;
Edge* edge;
ArcEdge* ae;
BezierEdge* be;
Face* face;
Volume* vol;
Group* grp;
Object* o;
rotate_3x3_valid = FALSE;
switch (obj->type)
{
case OBJ_POINT:
p = (Point*)obj;
p->moved = FALSE;
p->win_valid = FALSE;
obj->copied_to = NULL;
break;
case OBJ_EDGE:
type = ((Edge*)obj)->type & ~EDGE_CONSTRUCTION;
edge = (Edge*)obj;
clear_move_copy_flags((Object*)edge->endpoints[0]);
clear_move_copy_flags((Object*)edge->endpoints[1]);
obj->copied_to = NULL;
switch (type)
{
case EDGE_ARC:
ae = (ArcEdge*)obj;
clear_move_copy_flags((Object*)ae->centre);
clear_move_copy_flags((Object*)&ae->normal.refpt);
break;
case EDGE_BEZIER:
be = (BezierEdge*)obj;
clear_move_copy_flags((Object*)be->ctrlpoints[0]);
clear_move_copy_flags((Object*)be->ctrlpoints[1]);
break;
}
break;
case OBJ_FACE:
face = (Face*)obj;
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
clear_move_copy_flags((Object*)edge);
}
for (p = (Point*)face->view_list.head; p != NULL; p = (Point*)p->hdr.next)
clear_move_copy_flags((Object*)p);
if (face->text != NULL) // and the text positions
{
clear_move_copy_flags((Object*)&face->text->origin);
clear_move_copy_flags((Object*)&face->text->endpt);
}
obj->copied_to = NULL;
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
clear_move_copy_flags((Object*)face);
obj->copied_to = NULL;
break;
case OBJ_GROUP:
grp = (Group*)obj;
for (o = grp->obj_list.head; o != NULL; o = o->next)
clear_move_copy_flags(o);
obj->copied_to = NULL;
break;
}
}
// Copy any object, with an offset on all its point coordinates. Optionally if cloning,
// fix any arc/bez step counts on both source and dest edges
// (like clone_face_reverse does).
// Make sure to call clear_move_copy_flags afterwards.
Object*
copy_obj(Object* obj, double xoffset, double yoffset, double zoffset, BOOL cloning)
{
int i;
Object* new_obj = NULL;
Point* p;
EDGE type;
Edge* edge, * new_edge;
ArcEdge* ae, * nae;
BezierEdge* be, * nbe;
Face* face, * new_face;
Volume* vol, * new_vol;
Object* o;
Group* grp, * new_grp;
switch (obj->type)
{
case OBJ_POINT:
p = (Point*)obj;
if (obj->copied_to != NULL)
{
new_obj = obj->copied_to;
}
else
{
new_obj = (Object*)point_new(p->x + xoffset, p->y + yoffset, p->z + zoffset);
new_obj->lock = obj->lock;
obj->copied_to = new_obj;
}
break;
case OBJ_EDGE:
if (obj->copied_to != NULL)
{
new_obj = obj->copied_to;
}
else
{
// Copy the edge.
new_obj = (Object*)edge_new(((Edge*)obj)->type);
new_obj->lock = obj->lock;
obj->copied_to = new_obj;
// Copy the points
edge = (Edge*)obj;
new_edge = (Edge*)new_obj;
new_edge->endpoints[0] = (Point*)copy_obj((Object*)edge->endpoints[0], xoffset, yoffset, zoffset, cloning);
new_edge->endpoints[1] = (Point*)copy_obj((Object*)edge->endpoints[1], xoffset, yoffset, zoffset, cloning);
type = ((Edge*)obj)->type & ~EDGE_CONSTRUCTION;
switch (type)
{
case EDGE_ARC:
ae = (ArcEdge*)edge;
nae = (ArcEdge*)new_edge;
nae->centre = (Point*)copy_obj((Object*)ae->centre, xoffset, yoffset, zoffset, cloning);
nae->clockwise = ae->clockwise;
nae->normal = ae->normal;
move_obj((Object *)&nae->normal.refpt, xoffset, yoffset, zoffset);
new_edge->nsteps = edge->nsteps;
if (cloning)
{
edge->view_valid = FALSE;
}
new_edge->view_valid = FALSE;
break;
case EDGE_BEZIER:
be = (BezierEdge*)edge;
nbe = (BezierEdge*)new_edge;
nbe->ctrlpoints[0] = (Point*)copy_obj((Object*)be->ctrlpoints[0], xoffset, yoffset, zoffset, cloning);
nbe->ctrlpoints[1] = (Point*)copy_obj((Object*)be->ctrlpoints[1], xoffset, yoffset, zoffset, cloning);
new_edge->nsteps = edge->nsteps;
if (cloning)
{
edge->view_valid = FALSE;
}
new_edge->view_valid = FALSE;
break;
}
}
break;
case OBJ_FACE:
face = (Face*)obj;
new_face = face_new(face->type, face->normal);
new_obj = (Object*)new_face;
new_obj->lock = obj->lock;
// Realloc the edge array if we need a big one
if (face->n_edges >= new_face->max_edges)
{
new_face->max_edges = face->max_edges;
new_face->edges = realloc(new_face->edges, new_face->max_edges * sizeof(Edge*));
}
new_face->n_edges = face->n_edges;
new_face->paired = face->paired;
new_face->extrude_height = face->extrude_height;
// Alloc and copy any contour array. Don't worry about the power of 2 thing as it will
// not be extended again.
if (face->n_contours != 0)
{
new_face->n_contours = face->n_contours;
new_face->contours = calloc(new_face->n_contours, sizeof(Contour));
memcpy(new_face->contours, face->contours, face->n_contours * sizeof(Contour));
}
// Copy the edges
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
new_edge = (Edge*)copy_obj((Object*)edge, xoffset, yoffset, zoffset, cloning);
new_face->edges[i] = new_edge;
}
// Set the initial point corresponding to the original
edge = face->edges[0];
new_edge = new_face->edges[0];
if (face->initial_point == edge->endpoints[0])
{
new_face->initial_point = new_edge->endpoints[0];
}
else
{
ASSERT(face->initial_point == edge->endpoints[1], "Point order messed up");
new_face->initial_point = new_edge->endpoints[1];
}
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
new_vol = vol_new();
new_obj = (Object*)new_vol;
new_obj->lock = obj->lock;
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
{
new_face = (Face*)copy_obj((Object*)face, xoffset, yoffset, zoffset, cloning);
new_face->vol = new_vol;
link_tail((Object*)new_face, &new_vol->faces);
}
new_vol->op = vol->op;
new_vol->material = vol->material;
new_vol->measured = vol->measured;
new_vol->max_facetype = vol->max_facetype;
break;
case OBJ_GROUP:
grp = (Group*)obj;
new_grp = group_new();
for (o = grp->obj_list.head; o != NULL; o = o->next)
{
new_obj = copy_obj(o, xoffset, yoffset, zoffset, cloning);
link_tail_group(new_obj, new_grp);
}
new_obj = (Object*)new_grp;
new_obj->lock = obj->lock;
new_grp->op = grp->op;
if (grp->loft != NULL)
{
size_t loft_size = sizeof(LoftParams) + grp->loft->n_bays * sizeof(double);
new_grp->loft = malloc(loft_size);
memcpy_s(new_grp->loft, loft_size, grp->loft, loft_size);
}
break;
}
return new_obj;
}
// Copy a face, but reverse all the edges so the normal points the opposite
// way. Make sure the edges containing the initial points still line up.
Face
* clone_face_reverse(Face* face)
{
Face* clone = face_new(face->type, face->normal);
Object* e;
Object* ne = NULL;
Edge* edge;
ArcEdge* ae, * nae;
Point* last_point;
int i, idx, c;
//char buf[256];
// Swap the normal around
clone->normal.A = -clone->normal.A;
clone->normal.B = -clone->normal.B;
clone->normal.C = -clone->normal.C;
clone->n_edges = face->n_edges;
if (face->max_edges > clone->max_edges) // in case it has been grown before being cloned
clone->edges = realloc(clone->edges, face->max_edges * sizeof(Edge*));
clone->max_edges = face->max_edges;
// associate the face with its clone by setting the extrude flag
clone->paired = TRUE;
face->paired = TRUE;
// If the face does not have a contour array, create one here for the face and its clone.
// The extrusion code needs to use it later, and it makes everything simpler here as well.
if (face->contours == NULL)
{
face->contours = calloc(1, sizeof(Contour));
face->n_contours = 1;
face->contours[0].edge_index = 0;
if (face->initial_point == face->edges[0]->endpoints[0])
face->contours[0].ip_index = 0;
else
face->contours[0].ip_index = 1;
face->contours[0].n_edges = face->n_edges;
}
// Copy the contour array to the clone. Its IP's will be swapped later.
clone->n_contours = face->n_contours;
clone->contours = calloc(clone->n_contours, sizeof(Contour));
memcpy(clone->contours, face->contours, face->n_contours * sizeof(Contour));
// Clone and reverse each contour separately
for (c = 0; c < face->n_contours; c++)
{
int ei = face->contours[c].edge_index;
// Set the initial point for the contour.
last_point = face->edges[ei]->endpoints[face->contours[c].ip_index];
// Copy the edges, reversing the order
for (i = 0; i < face->contours[c].n_edges; i++)
{
e = (Object*)face->edges[ei + i];
ne = copy_obj(e, 0, 0, 0, TRUE);
if (i == 0)
idx = 0;
else
idx = face->contours[c].n_edges - i;
clone->edges[ei + idx] = (Edge*)ne;
// Follow the chain of points from e->initial_point
edge = (Edge*)e;
if (last_point == edge->endpoints[0])
{
idx = 1;
}
else
{
ASSERT(last_point == edge->endpoints[1], "Cloned edges don't join up");
idx = 0;
}
last_point = edge->endpoints[idx];
if (i == 0)
clone->contours[c].ip_index = idx;
//clone->initial_point = ((Edge *)ne)->endpoints[idx];
switch (face->edges[ei + i]->type)
{
case EDGE_ARC:
ae = (ArcEdge*)e;
nae = (ArcEdge*)ne;
// Reverse the arc
nae->clockwise = !ae->clockwise;
nae->normal.A = -ae->normal.A;
nae->normal.B = -ae->normal.B;
nae->normal.C = -ae->normal.C;
break;
}
}
}
clone->initial_point = clone->edges[clone->contours[0].edge_index]->endpoints[clone->contours[0].ip_index];
#ifdef DEBUG_REVERSE_RECT_FACE
sprintf_s(buf, 256, "Clone %d IP %d\r\n", clone->hdr.ID, clone->initial_point->hdr.ID);
Log(buf);
sprintf_s(buf, 256, "%d %d\r\n", ((StraightEdge*)clone->edges[0])->endpoints[0]->hdr.ID, ((StraightEdge*)clone->edges[0])->endpoints[1]->hdr.ID);
Log(buf);
sprintf_s(buf, 256, "%d %d\r\n", ((StraightEdge*)clone->edges[1])->endpoints[0]->hdr.ID, ((StraightEdge*)clone->edges[1])->endpoints[1]->hdr.ID);
Log(buf);
sprintf_s(buf, 256, "%d %d\r\n", ((StraightEdge*)clone->edges[2])->endpoints[0]->hdr.ID, ((StraightEdge*)clone->edges[2])->endpoints[1]->hdr.ID);
Log(buf);
sprintf_s(buf, 256, "%d %d\r\n", ((StraightEdge*)clone->edges[3])->endpoints[0]->hdr.ID, ((StraightEdge*)clone->edges[3])->endpoints[1]->hdr.ID);
Log(buf);
#endif
return clone;
}
// Move any object by an offset on all its point coordinates.
// Make sure to call clear_move_copy_flags afterwards.
void
move_obj(Object* obj, double xoffset, double yoffset, double zoffset)
{
int i;
Point* p;
EDGE type;
Edge* edge;
ArcEdge* ae;
BezierEdge* be;
Face* face;
Volume* vol;
Group* grp;
Object* o;
switch (obj->type)
{
case OBJ_POINT:
p = (Point*)obj;
if (!p->moved)
{
p->x += xoffset;
p->y += yoffset;
p->z += zoffset;
p->moved = TRUE;
}
break;
case OBJ_EDGE:
edge = (Edge*)obj;
move_obj((Object*)edge->endpoints[0], xoffset, yoffset, zoffset);
move_obj((Object*)edge->endpoints[1], xoffset, yoffset, zoffset);
type = ((Edge*)obj)->type & ~EDGE_CONSTRUCTION;
switch (type)
{
case EDGE_ARC:
ae = (ArcEdge*)obj;
move_obj((Object*)ae->centre, xoffset, yoffset, zoffset);
move_obj((Object*)&ae->normal.refpt, xoffset, yoffset, zoffset);
edge->view_valid = FALSE;
break;
case EDGE_BEZIER:
be = (BezierEdge*)obj;
move_obj((Object*)be->ctrlpoints[0], xoffset, yoffset, zoffset);
move_obj((Object*)be->ctrlpoints[1], xoffset, yoffset, zoffset);
edge->view_valid = FALSE;
break;
}
break;
case OBJ_FACE:
face = (Face*)obj;
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
move_obj((Object*)edge, xoffset, yoffset, zoffset);
}
face->normal.refpt.x += xoffset; // don't forget to move the normal refpt too
face->normal.refpt.y += yoffset;
face->normal.refpt.z += zoffset;
if (face->text != NULL) // and the text positions
{
face->text->origin.x += xoffset;
face->text->origin.y += yoffset;
face->text->origin.z += zoffset;
face->text->endpt.x += xoffset;
face->text->endpt.y += yoffset;
face->text->endpt.z += zoffset;
}
face->view_valid = FALSE;
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
move_obj((Object*)face, xoffset, yoffset, zoffset);
break;
case OBJ_GROUP:
grp = (Group*)obj;
for (o = grp->obj_list.head; o != NULL; o = o->next)
move_obj(o, xoffset, yoffset, zoffset);
break;
}
}
// Move a face by local normals (each boundary point is assumed to have a local normal
// in the face's PlaneRef array)
void
extrude_local(Face* face, double length)
{
int i;
for (i = 0; i < face->n_local; i++)
{
PlaneRef* n = &face->local_norm[i];
move_obj((Object *)n->refpt, n->A * length, n->B * length, n->C * length);
}
// If the face contains arcs, their normals need to be recalculated.
}
// Find any adjacent round/chamfer corner edges to the given edge or face
// that need moving along with it. If a face is being picked, return faces that
// need moving (not just edges) since they will be used for highlighting.
// Return TRUE if some corners were found and added to the list.
BOOL
find_corner_edges(Object* obj, Object* parent, ListHead* halo)
{
int i;
Face* face, *f;
Volume* vol;
BOOL rc = FALSE;
if (parent == NULL) // no parent, nothing to do
return FALSE;
switch (parent->type)
{
case OBJ_FACE:
if (obj->type == OBJ_EDGE)
{
// Picked edge, with parent face. Put adjacent corner edges in the halo list.
face = (Face*)parent;
for (i = 0; i < face->n_edges; i++)
{
if ((Object *)face->edges[i] == obj)
{
int next = (i < face->n_edges - 1) ? i + 1 : 0;
int prev = (i > 0) ? i - 1 : face->n_edges - 1;
if (face->edges[prev]->corner)
{
rc = TRUE;
link_single((Object*)face->edges[prev], halo);
}
if (face->edges[next]->corner)
{
rc = TRUE;
link_single((Object*)face->edges[next], halo);
}
break;
}
}
}
break;
case OBJ_VOLUME:
vol = (Volume*)parent;
if (obj->type == OBJ_EDGE)
{
// Picked edge, with parent volume. Find corner edges adjacent to the
// picked edge in all the faces in the volume's face list. Stop when
// they are found (the picked edge will only occur once with adjacent corners)
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
{
for (i = 0; i < face->n_edges; i++)
{
if ((Object*)face->edges[i] == obj)
{
rc = find_corner_edges(obj, (Object *)face, halo);
if (rc)
goto finished;
}
}
}
}
else if (obj->type == OBJ_FACE)
{
// Picked face, with parent volume. Find corner _faces_ adjacent to the
// picked face in the volume's face list. Note that:
// - if the picked face has corner edges, there will be no corner faces edge-adjacent to it.
// - that leaves the picked face being a side face, and any corner faces will be adjacent
// to it in the volume's face list.
face = (Face*)obj;
if (face->has_corners)
return FALSE;
for (f = (Face*)vol->faces.head; f != NULL; f = (Face*)f->hdr.next)
{
if (f == face)
{
Face* fnext, * fprev;
fprev = (Face*)f->hdr.prev;
if (fprev == NULL)
fprev = (Face*)vol->faces.tail;
fnext = (Face*)f->hdr.next;
if (fnext == NULL)
fnext = (Face*)vol->faces.head;
if (fprev->corner)
{
rc = TRUE;
link_single((Object*)fprev, halo);
}
if (fnext->corner)
{
rc = TRUE;
link_single((Object*)fnext, halo);
}
break;
}
}
}
break;
}
finished:
return rc;
}
// Move any points, edges or faces that have been put in the halo list
// by find_corner_edges or find_adjacent_points.
// Ignore any smooth factors, and ignore any groups in the halo list.
void
move_corner_edges(ListHead *halo, double xoffset, double yoffset, double zoffset)
{
Object* obj;
for (obj = halo->head; obj != NULL; obj = obj->next)
{
if (obj->prev->type != OBJ_GROUP)
move_obj(obj->prev, xoffset, yoffset, zoffset);
}
}
// Find a suitable (x,y,z) point about which to rotate any object.
void
find_obj_pivot(Object* obj, double* x, double* y, double* z)
{
Point* p;
Edge* edge;
Face* face;
Volume* vol;
Group* grp;
switch (obj->type)
{
case OBJ_POINT: // will never be used, but here for completeness
p = (Point*)obj;
*x = p->x;
*y = p->y;
*z = p->z;
break;
case OBJ_EDGE:
edge = (Edge*)obj;
*x = edge->endpoints[0]->x;
*y = edge->endpoints[0]->y;
*z = edge->endpoints[0]->z;
break;
case OBJ_FACE:
face = (Face*)obj;
*x = face->normal.refpt.x;
*y = face->normal.refpt.y;
*z = face->normal.refpt.z;
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
*x = vol->bbox.xc;
*y = vol->bbox.yc;
*z = vol->bbox.zc;
break;
case OBJ_GROUP:
grp = (Group*)obj;
*x = grp->bbox.xc;
*y = grp->bbox.yc;
*z = grp->bbox.zc;
break;
}
}
// Rotate a coordinate, in the facing plane, by 90 degrees in the positive direction.
void
rotate_coord_90_facing(double* x, double* y, double* z, double xc, double yc, double zc)
{
double x0 = *x - xc;
double y0 = *y - yc;
double z0 = *z - zc;
switch (facing_index)
{
case PLANE_XY:
case PLANE_MINUS_XY:
*x = xc - y0;
*y = yc + x0;
break;
case PLANE_XZ:
case PLANE_MINUS_XZ:
*x = xc - z0;
*z = zc + x0;
break;
case PLANE_YZ:
case PLANE_MINUS_YZ:
*y = yc - z0;
*z = zc + y0;
break;
}
}
// Rotate the direction of a plane, in the facing plane, by 90 degrees in the positive direction.
void
rotate_plane_90_facing(Plane* pl)
{
double A0 = pl->A;
double B0 = pl->B;
double C0 = pl->C;
switch (facing_index)
{
case PLANE_XY:
case PLANE_MINUS_XY:
pl->A = -B0;
pl->B = A0;
break;
case PLANE_XZ:
case PLANE_MINUS_XZ:
pl->A = -C0;
pl->C = A0;
break;
case PLANE_YZ:
case PLANE_MINUS_YZ:
pl->B = -C0;
pl->C = B0;
break;
}
}
// Rotate any object, in the facing plane, by 90 degrees in the positive direction.
// Make sure to call clear_move_copy_flags afterwards.
void
rotate_obj_90_facing(Object* obj, double xc, double yc, double zc)
{
int i;
Point* p;
EDGE type;
Edge* edge;
ArcEdge* ae;
BezierEdge* be;
Face* face;
Volume* vol;
Group* grp;
Object* o;
switch (obj->type)
{
case OBJ_POINT:
p = (Point*)obj;
if (!p->moved)
{
rotate_coord_90_facing(&p->x, &p->y, &p->z, xc, yc, zc);
p->moved = TRUE;
}
break;
case OBJ_EDGE:
edge = (Edge*)obj;
rotate_obj_90_facing((Object*)edge->endpoints[0], xc, yc, zc);
rotate_obj_90_facing((Object*)edge->endpoints[1], xc, yc, zc);
type = ((Edge*)obj)->type & ~EDGE_CONSTRUCTION;
switch (type)
{
case EDGE_ARC:
ae = (ArcEdge*)obj;
if (!ae->centre->moved) // don't do it twice
rotate_plane_90_facing(&ae->normal);
rotate_obj_90_facing((Object*)ae->centre, xc, yc, zc);
rotate_obj_90_facing((Object*)&ae->normal.refpt, xc, yc, zc);
edge->view_valid = FALSE;
break;
case EDGE_BEZIER:
be = (BezierEdge*)obj;
rotate_obj_90_facing((Object*)be->ctrlpoints[0], xc, yc, zc);
rotate_obj_90_facing((Object*)be->ctrlpoints[1], xc, yc, zc);
edge->view_valid = FALSE;
break;
}
break;
case OBJ_FACE:
face = (Face*)obj;
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
rotate_obj_90_facing((Object*)edge, xc, yc, zc);
}
// don't forget to rotate the normal refpt too
rotate_coord_90_facing(&face->normal.refpt.x, &face->normal.refpt.y, &face->normal.refpt.z, xc, yc, zc);
if (face->text != NULL) // and the text positions
{
rotate_coord_90_facing(&face->text->origin.x, &face->text->origin.y, &face->text->origin.z, xc, yc, zc);
rotate_coord_90_facing(&face->text->endpt.x, &face->text->endpt.y, &face->text->endpt.z, xc, yc, zc);
}
face->view_valid = FALSE;
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
rotate_obj_90_facing((Object*)face, xc, yc, zc);
break;
case OBJ_GROUP:
grp = (Group*)obj;
for (o = grp->obj_list.head; o != NULL; o = o->next)
rotate_obj_90_facing(o, xc, yc, zc);
break;
}
}
// 2x2 rotation helper
static void
mat_mult_2x2_xy(double m[4], double x0, double y0, double* x, double* y)
{
*x = m[0] * x0 + m[1] * y0;
*y = m[2] * x0 + m[3] * y0;
}
// Rotate a coordinate, in the facing plane, by angle alpha in the positive direction.
void
rotate_coord_free_facing(double* x, double* y, double* z, double alpha, double xc, double yc, double zc)
{
double x0 = *x - xc;
double y0 = *y - yc;
double z0 = *z - zc;
double co = cos(alpha / RAD);
double si = sin(alpha / RAD);
double m[4] = { co, si, -si, co };
switch (facing_index)
{
case PLANE_XY:
case PLANE_MINUS_XY:
mat_mult_2x2_xy(m, x0, y0, x, y);
*x += xc;
*y += yc;
break;
case PLANE_XZ:
case PLANE_MINUS_XZ:
mat_mult_2x2_xy(m, x0, z0, x, z);
*x += xc;
*z += zc;
break;
case PLANE_YZ:
case PLANE_MINUS_YZ:
mat_mult_2x2_xy(m, y0, z0, y, z);
*y += yc;
*z += zc;
break;
}
}
// Rotate the direction of a plane, in the facing plane, by angle alpha in the positive direction.
void
rotate_plane_free_facing(Plane* pl, double alpha)
{
double A0 = pl->A;
double B0 = pl->B;
double C0 = pl->C;
double co = cos(alpha / RAD);
double si = sin(alpha / RAD);
double m[4] = { co, si, -si, co };
switch (facing_index)
{
case PLANE_XY:
case PLANE_MINUS_XY:
mat_mult_2x2_xy(m, A0, B0, &pl->A, &pl->B);
break;
case PLANE_XZ:
case PLANE_MINUS_XZ:
mat_mult_2x2_xy(m, A0, C0, &pl->A, &pl->C);
break;
case PLANE_YZ:
case PLANE_MINUS_YZ:
mat_mult_2x2_xy(m, B0, C0, &pl->B, &pl->C);
break;
}
}
// Rotate any object, in the facing plane, by angle alpha in the positive direction.
// Make sure to call clear_move_copy_flags afterwards.
void
rotate_obj_free_facing(Object* obj, double alpha, double xc, double yc, double zc)
{
int i;
Point* p;
EDGE type;
Edge* edge;
ArcEdge* ae;
BezierEdge* be;
Face* face;
Volume* vol;
Group* grp;
Object* o;
switch (obj->type)
{
case OBJ_POINT:
p = (Point*)obj;
if (!p->moved)
{
rotate_coord_free_facing(&p->x, &p->y, &p->z, alpha, xc, yc, zc);
p->moved = TRUE;
}
break;
case OBJ_EDGE:
edge = (Edge*)obj;
rotate_obj_free_facing((Object*)edge->endpoints[0], alpha, xc, yc, zc);
rotate_obj_free_facing((Object*)edge->endpoints[1], alpha, xc, yc, zc);
type = ((Edge*)obj)->type & ~EDGE_CONSTRUCTION;
switch (type)
{
case EDGE_ARC:
ae = (ArcEdge*)obj;
if (!ae->centre->moved) // don't do it twice
rotate_plane_free_facing(&ae->normal, alpha);
rotate_obj_free_facing((Object*)ae->centre, alpha, xc, yc, zc);
rotate_obj_free_facing((Object*)&ae->normal.refpt, alpha, xc, yc, zc);
edge->view_valid = FALSE;
break;
case EDGE_BEZIER:
be = (BezierEdge*)obj;
rotate_obj_free_facing((Object*)be->ctrlpoints[0], alpha, xc, yc, zc);
rotate_obj_free_facing((Object*)be->ctrlpoints[1], alpha, xc, yc, zc);
edge->view_valid = FALSE;
break;
}
break;
case OBJ_FACE:
face = (Face*)obj;
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
rotate_obj_free_facing((Object*)edge, alpha, xc, yc, zc);
}
// don't forget to rotate the normal refpt too
rotate_coord_free_facing(&face->normal.refpt.x, &face->normal.refpt.y, &face->normal.refpt.z, alpha, xc, yc, zc);
if (face->text != NULL) // and the text positions
{
rotate_coord_free_facing(&face->text->origin.x, &face->text->origin.y, &face->text->origin.z, alpha, xc, yc, zc);
rotate_coord_free_facing(&face->text->endpt.x, &face->text->endpt.y, &face->text->endpt.z, alpha, xc, yc, zc);
}
face->view_valid = FALSE;
break;
case OBJ_VOLUME:
vol = (Volume*)obj;
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
rotate_obj_free_facing((Object*)face, alpha, xc, yc, zc);
break;
case OBJ_GROUP:
grp = (Group*)obj;
for (o = grp->obj_list.head; o != NULL; o = o->next)
rotate_obj_free_facing(o, alpha, xc, yc, zc);
break;
}
}
// Calculate the 3x3 rotation matrix that takes direction v1 onto v2.
// Cache it in a global (clear_move_copy_flags will clear it)
// From kevinmoran/noacos_derivation.md
// (https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724)
void
rotate_matrix_free_abc(Plane v1, Plane v2)
{
Plane axis;
double cosA, k;
// Pass v1 and v2 by value so they can be normalised in here.
normalise_plane(&v1);
normalise_plane(&v2);
cosA = pldot(&v1, &v2);
plcross(&v1, &v2, &axis);
k = 1.0 / (1.0 + cosA);
rotate_3x3[0] = (axis.A * axis.A * k) + cosA;
rotate_3x3[1] = (axis.B * axis.A * k) - axis.C;
rotate_3x3[2] = (axis.C * axis.A * k) + axis.B;
rotate_3x3[3] = (axis.A * axis.B * k) + axis.C;
rotate_3x3[4] = (axis.B * axis.B * k) + cosA;
rotate_3x3[5] = (axis.C * axis.B * k) - axis.A;
rotate_3x3[6] = (axis.A * axis.C * k) - axis.B;
rotate_3x3[7] = (axis.B * axis.C * k) + axis.A;
rotate_3x3[8] = (axis.C * axis.C * k) + cosA;
}
// Rotate a point by the angle between two Planes in 3D.
// The centre of rotation is the refpt of v2.
void
rotate_coord_free_abc(double* x, double* y, double* z, Plane* v1, Plane* v2)
{