forked from Embroidermodder/libembroidery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emb-pattern.c
1068 lines (954 loc) · 33.6 KB
/
emb-pattern.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
#ifdef ARDUINO
#include "utility/ino-event.h"
#endif
static EmbColor black = { 0, 0, 0 };
/*! Returns a pointer to an EmbPattern. It is created on the heap.
* The caller is responsible for freeing the allocated memory with
* embPattern_free(). */
EmbPattern* embPattern_create(void)
{
EmbPattern* p = 0;
p = (EmbPattern*)malloc(sizeof(EmbPattern));
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_create(), unable to allocate memory for p\n");
return 0;
}
p->settings = embSettings_init();
p->currentColorIndex = 0;
p->stitchList = embArray_create(EMB_STITCH);
p->threads = embArray_create(EMB_THREAD);
p->hoop.height = 0.0;
p->hoop.width = 0.0;
p->arcs = 0;
p->circles = 0;
p->ellipses = 0;
p->lines = 0;
p->paths = 0;
p->points = 0;
p->polygons = 0;
p->polylines = 0;
p->rects = 0;
p->splines = 0;
p->lastPosition.x = 0.0;
p->lastPosition.y = 0.0;
return p;
}
void embPattern_hideStitchesOverLength(EmbPattern* p, int length)
{
double prevX, prevY;
int i;
EmbStitch st;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_hideStitchesOverLength(), p argument is null\n");
return;
}
prevX = 0;
prevY = 0;
for (i = 0; i < p->stitchList->count; i++) {
st = p->stitchList->stitch[i];
if ((fabs(st.x - prevX) > length) || (fabs(st.y - prevY) > length)) {
st.flags |= TRIM;
st.flags &= ~NORMAL;
}
prevX = st.x;
prevY = st.y;
}
}
int embPattern_addThread(EmbPattern* p, EmbThread thread)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addThread(), p argument is null\n");
return 0;
}
if (!p->threads) {
p->threads = embArray_create(EMB_THREAD);
}
embArray_addThread(p->threads, thread);
return 1;
}
void embPattern_fixColorCount(EmbPattern* p)
{
/* fix color count to be max of color index. */
int maxColorIndex = 0, i;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_fixColorCount(), p argument is null\n");
return;
}
for (i = 0; i < p->stitchList->count; i++) {
maxColorIndex = EMB_MAX_2(maxColorIndex, p->stitchList->stitch[i].color);
}
while (p->threads->count <= maxColorIndex) {
embPattern_addThread(p, embThread_getRandom());
}
/*
while (p->threadLists->count > (maxColorIndex + 1)) {
TODO: erase last color p->threadList.pop_back();
}
*/
}
/*! Copies all of the EmbStitchList data to EmbPolylineObjectList data for pattern (\a p). */
void embPattern_copyStitchListToPolylines(EmbPattern* p)
{
EmbPointObject* point;
int breakAtFlags, i;
EmbStitch st;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_copyStitchListToPolylines(), p argument is null\n");
return;
}
#ifdef EMB_DEBUG_JUMP
breakAtFlags = (STOP | TRIM);
#else /* EMB_DEBUG_JUMP */
breakAtFlags = (STOP | JUMP | TRIM);
#endif /* EMB_DEBUG_JUMP */
for (i = 0; i < p->stitchList->count; i++) {
EmbArray* pointList;
EmbColor color;
pointList = 0;
for (i = 0; i < p->stitchList->count; i++) {
st = p->stitchList->stitch[i];
if (st.flags & breakAtFlags) {
break;
}
if (!(st.flags & JUMP)) {
if (!pointList) {
pointList = embArray_create(EMB_POINT);
color = p->threads->thread[st.color].color;
}
point = (EmbPointObject*)malloc(sizeof(EmbPointObject));
point->point.x = st.x;
point->point.y = st.y;
embArray_addPoint(pointList, point);
}
}
/* NOTE: Ensure empty polylines are not created. This is critical. */
if (pointList) {
EmbPolylineObject* currentPolyline = (EmbPolylineObject*)malloc(sizeof(EmbPolylineObject));
if (!currentPolyline) {
embLog("ERROR: emb-pattern.c embPattern_copyStitchListToPolylines(), cannot allocate memory for currentPolyline\n");
return;
}
currentPolyline->pointList = pointList;
currentPolyline->color = color;
currentPolyline->lineType = 1;
/* TODO: Determine what the correct value should be. */
if (!p->polylines) {
p->polylines = embArray_create(EMB_POLYLINE);
}
embArray_addPolyline(p->polylines, currentPolyline);
}
embArray_free(pointList);
}
}
/*! Copies all of the EmbPolylineObjectList data to EmbStitchList data for pattern (\a p). */
void embPattern_copyPolylinesToStitchList(EmbPattern* p)
{
int firstObject = 1, i, j;
/*int currentColor = polyList->polylineObj->color TODO: polyline color */
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_copyPolylinesToStitchList(), p argument is null\n");
return;
}
if (!p->polylines) {
embLog("ERROR: emb-pattern.c embPattern_copyPolylinesToStitchList(), p argument is null\n");
return;
}
for (i = 0; i < p->polylines->count; i++) {
EmbPolylineObject* currentPoly = 0;
EmbArray* currentPointList = 0;
EmbThread thread;
currentPoly = p->polylines->polyline[i];
if (!currentPoly) {
embLog("ERROR: emb-pattern.c embPattern_copyPolylinesToStitchList(), currentPoly is null\n");
return;
}
currentPointList = currentPoly->pointList;
if (!currentPointList) {
embLog("ERROR: emb-pattern.c embPattern_copyPolylinesToStitchList(), currentPointList is null\n");
return;
}
thread.catalogNumber = 0;
thread.color = currentPoly->color;
thread.description = 0;
embPattern_addThread(p, thread);
if (!firstObject) {
embPattern_addStitchAbs(p, currentPointList->point[0].point.x, currentPointList->point[0].point.y, TRIM, 1);
embPattern_addStitchRel(p, 0.0, 0.0, STOP, 1);
}
embPattern_addStitchAbs(p, currentPointList->point[0].point.x, currentPointList->point[0].point.y, JUMP, 1);
for (j = 1; j < currentPointList->count; j++) {
embPattern_addStitchAbs(p, currentPointList->point[j].point.x, currentPointList->point[j].point.y, NORMAL, 1);
}
firstObject = 0;
}
embPattern_addStitchRel(p, 0.0, 0.0, END, 1);
}
/*! Moves all of the EmbStitchList data to EmbPolylineObjectList data for pattern (\a p). */
void embPattern_moveStitchListToPolylines(EmbPattern* p)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_moveStitchListToPolylines(), p argument is null\n");
return;
}
embPattern_copyStitchListToPolylines(p);
/* Free the stitchList and threadList since their data has now been transferred to polylines */
embArray_free(p->stitchList);
embArray_free(p->threads);
}
/*! Moves all of the EmbPolylineObjectList data to EmbStitchList data for pattern (\a p). */
void embPattern_movePolylinesToStitchList(EmbPattern* p)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_movePolylinesToStitchList(), p argument is null\n");
return;
}
embPattern_copyPolylinesToStitchList(p);
embArray_free(p->polylines);
}
/*! Adds a stitch to the pattern (\a p) at the absolute position (\a x,\a y). Positive y is up. Units are in millimeters. */
void embPattern_addStitchAbs(EmbPattern* p, double x, double y, int flags, int isAutoColorIndex)
{
EmbStitch s;
EmbVector home;
if (!p || !p->stitchList) {
embLog("ERROR: emb-pattern.c embPattern_addStitchAbs(), p argument is null\n");
return;
}
if (flags & END) {
if (p->stitchList->count == 0) {
return;
}
/* Prevent unnecessary multiple END stitches */
if (p->stitchList->stitch[p->stitchList->count - 1].flags & END) {
embLog("ERROR: emb-pattern.c embPattern_addStitchAbs(), found multiple END stitches\n");
return;
}
embPattern_fixColorCount(p);
/* HideStitchesOverLength(127); TODO: fix or remove this */
}
if (flags & STOP) {
if (p->stitchList->count == 0) {
return;
}
if (isAutoColorIndex) {
p->currentColorIndex++;
}
}
/* NOTE: If the stitchList is empty, we will create it before adding stitches to it.
* The first coordinate will be the HOME position. */
if (!p->stitchList) {
/* NOTE: Always HOME the machine before starting any stitching */
home = embSettings_home(&(p->settings));
s.x = home.x;
s.y = home.y;
s.flags = JUMP;
s.color = p->currentColorIndex;
p->stitchList = embArray_create(EMB_STITCH);
embArray_addStitch(p->stitchList, s);
}
s.x = x;
s.y = y;
s.flags = flags;
s.color = p->currentColorIndex;
embArray_addStitch(p->stitchList, s);
p->lastPosition.x = s.x;
p->lastPosition.y = s.y;
}
/*! Adds a stitch to the pattern (\a p) at the relative position (\a dx,\a dy) to the previous stitch. Positive y is up. Units are in millimeters. */
void embPattern_addStitchRel(EmbPattern* p, double dx, double dy, int flags, int isAutoColorIndex)
{
EmbVector home;
double x, y;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addStitchRel(), p argument is null\n");
return;
}
if (p->stitchList) {
x = p->lastPosition.x + dx;
y = p->lastPosition.y + dy;
} else {
/* NOTE: The stitchList is empty, so add it to the HOME position.
* The embStitchList_create function will ensure the first coordinate is at the HOME position. */
home = embSettings_home(&(p->settings));
x = home.x + dx;
y = home.y + dy;
}
embPattern_addStitchAbs(p, x, y, flags, isAutoColorIndex);
}
void embPattern_changeColor(EmbPattern* p, int index)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_changeColor(), p argument is null\n");
return;
}
p->currentColorIndex = index;
}
/*! Reads a file with the given \a fileName and loads the data into \a pattern.
* Returns \c true if successful, otherwise returns \c false. */
int embPattern_readAuto(EmbPattern* pattern, const char* fileName) /* TODO: Write test case using this convenience function. */
{
int reader;
reader = embReaderWriter_getByFileName(fileName);
if (reader < 0) {
embLog("emb-pattern.c embPattern_read(), unsupported read file type:");
embLog(fileName);
return 0;
}
return embPattern_read(pattern, fileName, reader);
}
/*! Writes the data from \a pattern to a file with the given \a fileName.
* Returns \c true if successful, otherwise returns \c false. */
int embPattern_writeAuto(EmbPattern* pattern, const char* fileName) /* TODO: Write test case using this convenience function. */
{
int writer;
writer = embReaderWriter_getByFileName(fileName);
if (writer < 0) {
embLog("ERROR: emb-pattern.c embPattern_write(), unsupported write file type:");
embLog(fileName);
return 0;
}
return embPattern_write(pattern, fileName, writer);
}
/* Very simple scaling of the x and y axis for every point.
* Doesn't insert or delete stitches to preserve density. */
void embPattern_scale(EmbPattern* p, double scale)
{
int i;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_scale(), p argument is null\n");
return;
}
for (i = 0; i < p->stitchList->count; i++) {
p->stitchList->stitch[i].x *= scale;
p->stitchList->stitch[i].y *= scale;
}
}
/*! Returns an EmbRect that encapsulates all stitches and objects in the pattern (\a p). */
EmbRect embPattern_calcBoundingBox(EmbPattern* p)
{
EmbRect boundingRect;
EmbStitch pt;
EmbBezier bezier;
int i, j;
boundingRect.left = 0;
boundingRect.right = 0;
boundingRect.top = 0;
boundingRect.bottom = 0;
if (!p || !p->stitchList) {
embLog("ERROR: emb-pattern.c embPattern_calcBoundingBox(), p argument is null\n");
return boundingRect;
}
/* Calculate the bounding rectangle. It's needed for smart repainting. */
/* TODO: Come back and optimize this mess so that after going thru all objects
and stitches, if the rectangle isn't reasonable, then return a default rect */
if (!p->stitchList && !(p->arcs || p->circles || p->ellipses || p->lines || p->points || p->polygons || p->polylines || p->rects || p->splines)) {
boundingRect.top = 0.0;
boundingRect.left = 0.0;
boundingRect.bottom = 1.0;
boundingRect.right = 1.0;
return boundingRect;
}
boundingRect.left = 99999.0;
boundingRect.top = 99999.0;
boundingRect.right = -99999.0;
boundingRect.bottom = -99999.0;
for (i = 0; i < p->stitchList->count; i++) {
/* If the point lies outside of the accumulated bounding
* rectangle, then inflate the bounding rect to include it. */
pt = p->stitchList->stitch[i];
if (!(pt.flags & TRIM)) {
boundingRect.left = EMB_MIN_2(boundingRect.left, pt.x);
boundingRect.top = EMB_MIN_2(boundingRect.top, pt.y);
boundingRect.right = EMB_MAX_2(boundingRect.right, pt.x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, pt.y);
}
}
if (p->arcs) {
/* TODO: embPattern_calcBoundingBox for arcs, for now just checks the start point */
for (i = 0; i < p->arcs->count; i++) {
EmbArc arc = p->arcs->arc[i].arc;
boundingRect.left = EMB_MIN_2(boundingRect.left, arc.start.x);
boundingRect.top = EMB_MIN_2(boundingRect.top, arc.start.y);
boundingRect.right = EMB_MAX_2(boundingRect.right, arc.start.x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, arc.start.y);
}
}
if (p->circles) {
for (i = 0; i < p->circles->count; i++) {
EmbCircle circle = p->circles->circle[i].circle;
boundingRect.left = EMB_MIN_2(boundingRect.left, circle.center.x - circle.radius);
boundingRect.top = EMB_MIN_2(boundingRect.top, circle.center.y - circle.radius);
boundingRect.right = EMB_MAX_2(boundingRect.right, circle.center.x + circle.radius);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, circle.center.y + circle.radius);
}
}
if (p->ellipses) {
for (i = 0; i < p->ellipses->count; i++) {
/* TODO: account for rotation */
EmbEllipse ellipse = p->ellipses->ellipse[i].ellipse;
boundingRect.left = EMB_MIN_2(boundingRect.left, ellipse.center.x - ellipse.radius.x);
boundingRect.top = EMB_MIN_2(boundingRect.top, ellipse.center.y - ellipse.radius.y);
boundingRect.right = EMB_MAX_2(boundingRect.right, ellipse.center.x + ellipse.radius.x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, ellipse.center.y + ellipse.radius.y);
}
}
if (p->lines) {
for (i = 0; i < p->lines->count; i++) {
EmbLine line = p->lines->line[i].line;
boundingRect.left = EMB_MIN_3(boundingRect.left, line.start.x, line.end.x);
boundingRect.top = EMB_MIN_3(boundingRect.top, line.start.y, line.end.y);
boundingRect.right = EMB_MAX_3(boundingRect.right, line.start.x, line.end.x);
boundingRect.bottom = EMB_MAX_3(boundingRect.bottom, line.start.y, line.end.y);
}
}
if (p->points) {
for (i = 0; i < p->points->count; i++) {
EmbVector point = p->points->point[i].point;
boundingRect.left = EMB_MIN_2(boundingRect.left, point.x);
boundingRect.top = EMB_MIN_2(boundingRect.top, point.y);
boundingRect.right = EMB_MAX_2(boundingRect.right, point.x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, point.y);
}
}
if (p->polygons) {
for (i = 0; i < p->polygons->count; i++) {
EmbArray* polygon;
polygon = p->polygons->polygon[i]->pointList;
for (j = 0; j < polygon->count; j++) {
boundingRect.left = EMB_MIN_2(boundingRect.left, polygon[i].vector[j].x);
boundingRect.top = EMB_MIN_2(boundingRect.top, polygon[i].vector[j].y);
boundingRect.right = EMB_MAX_2(boundingRect.right, polygon[i].vector[j].x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, polygon[i].vector[j].y);
}
}
}
if (p->polylines) {
for (i = 0; i < p->polylines->count; i++) {
EmbArray* polyline;
polyline = p->polylines->polyline[i]->pointList;
for (j = 0; j < polyline->count; j++) {
boundingRect.left = EMB_MIN_2(boundingRect.left, polyline[i].vector[j].x);
boundingRect.top = EMB_MIN_2(boundingRect.top, polyline[i].vector[j].y);
boundingRect.right = EMB_MAX_2(boundingRect.right, polyline[i].vector[j].x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, polyline[i].vector[j].y);
}
}
}
if (p->rects) {
for (i = 0; i < p->rects->count; i++) {
EmbRect rect = p->rects->rect[i].rect;
/* TODO: other points */
boundingRect.left = EMB_MIN_2(boundingRect.left, rect.left);
boundingRect.top = EMB_MIN_2(boundingRect.top, rect.left);
boundingRect.right = EMB_MAX_2(boundingRect.right, rect.left);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, rect.left);
}
}
if (p->splines) {
for (i = 0; i < p->splines->count; i++) {
bezier = p->splines->spline[i].bezier;
/* TODO: other points */
boundingRect.left = EMB_MIN_2(boundingRect.left, bezier.start.x);
boundingRect.top = EMB_MIN_2(boundingRect.top, bezier.start.y);
boundingRect.right = EMB_MAX_2(boundingRect.right, bezier.start.x);
boundingRect.bottom = EMB_MAX_2(boundingRect.bottom, bezier.start.y);
}
}
return boundingRect;
}
/*! Flips the entire pattern (\a p) horizontally about the y-axis. */
void embPattern_flipHorizontal(EmbPattern* p)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_flipHorizontal(), p argument is null\n");
return;
}
embPattern_flip(p, 1, 0);
}
/*! Flips the entire pattern (\a p) vertically about the x-axis. */
void embPattern_flipVertical(EmbPattern* p)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_flipVertical(), p argument is null\n");
return;
}
embPattern_flip(p, 0, 1);
}
/*! Flips the entire pattern (\a p) horizontally about the x-axis if (\a horz) is true.
* Flips the entire pattern (\a p) vertically about the y-axis if (\a vert) is true. */
void embPattern_flip(EmbPattern* p, int horz, int vert)
{
int i, j;
/* EmbVector flip; */
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_flip(), p argument is null\n");
return;
}
/*
flip.x = 1.0;
flip.y = 1.0;
if (horz) {
flip.x = -1.0;
}
if (vert) {
flip.y = -1.0;
}
then use embVector_product
*/
if (p->stitchList) {
for (i = 0; i < p->stitchList->count; i++) {
if (horz) {
p->stitchList->stitch[i].x *= -1.0;
}
if (vert) {
p->stitchList->stitch[i].y *= -1.0;
}
}
}
if (p->arcs) {
for (i = 0; i < p->arcs->count; i++) {
if (horz) {
p->arcs->arc[i].arc.start.x *= -1.0;
p->arcs->arc[i].arc.mid.x *= -1.0;
p->arcs->arc[i].arc.end.x *= -1.0;
}
if (vert) {
p->arcs->arc[i].arc.start.y *= -1.0;
p->arcs->arc[i].arc.mid.y *= -1.0;
p->arcs->arc[i].arc.end.y *= -1.0;
}
}
}
if (p->circles) {
for (i = 0; i < p->circles->count; i++) {
if (horz) {
p->circles->circle[i].circle.center.x *= -1.0;
}
if (vert) {
p->circles->circle[i].circle.center.y *= -1.0;
}
}
}
if (p->ellipses) {
for (i = 0; i < p->ellipses->count; i++) {
if (horz) {
p->ellipses->ellipse[i].ellipse.center.x *= -1.0;
}
if (vert) {
p->ellipses->ellipse[i].ellipse.center.y *= -1.0;
}
}
}
if (p->lines) {
for (i = 0; i < p->lines->count; i++) {
if (horz) {
p->lines->line[i].line.start.x *= -1.0;
p->lines->line[i].line.end.x *= -1.0;
}
if (vert) {
p->lines->line[i].line.start.y *= -1.0;
p->lines->line[i].line.end.y *= -1.0;
}
}
}
if (p->paths) {
for (i = 0; i < p->paths->count; i++) {
EmbArray* path = p->paths->path[i]->pointList;
for (j = 0; j < path->count; j++) {
if (horz) {
path->point[j].point.x *= -1.0;
}
if (vert) {
path->point[j].point.y *= -1.0;
}
}
}
}
if (p->points) {
for (i = 0; i < p->points->count; i++) {
if (horz) {
p->points->point[i].point.x *= -1.0;
}
if (vert) {
p->points->point[i].point.y *= -1.0;
}
}
}
if (p->polygons) {
for (i = 0; i < p->polygons->count; i++) {
EmbArray* polygon;
polygon = p->polygons->polygon[i]->pointList;
for (j = 0; j < polygon->count; j++) {
if (horz) {
polygon->point[j].point.x *= -1.0;
}
if (vert) {
polygon->point[j].point.y *= -1.0;
}
}
}
}
if (p->polylines) {
for (i = 0; i < p->polylines->count; i++) {
EmbArray* polyline;
polyline = p->polylines->polygon[i]->pointList;
for (j = 0; j < polyline->count; j++) {
if (horz) {
polyline->point[j].point.x *= -1.0;
}
if (vert) {
polyline->point[j].point.y *= -1.0;
}
}
}
}
if (p->rects) {
for (i = 0; i < p->rects->count; i++) {
if (horz) {
p->rects->rect[i].rect.left *= -1.0;
p->rects->rect[i].rect.right *= -1.0;
}
if (vert) {
p->rects->rect[i].rect.top *= -1.0;
p->rects->rect[i].rect.bottom *= -1.0;
}
}
}
if (p->splines) {
for (i = 0; i < p->splines->count; i++) {
/* TODO: embPattern_flip for splines */
}
}
}
void embPattern_combineJumpStitches(EmbPattern* p)
{
int jump = 0, i, j;
EmbStitch st;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_combineJumpStitches(), p argument is null\n");
return;
}
j = 0;
for (i = 0; i < p->stitchList->count; i++) {
st = p->stitchList->stitch[i];
p->stitchList->stitch[j] = st;
if (st.flags & JUMP) {
if (jump == 0) {
j++;
jump = 1;
}
} else {
j++;
jump = 0;
}
}
p->stitchList->count = j + 1;
}
/* TODO: The params determine the max XY movement rather than the length.
* They need renamed or clarified further. */
void embPattern_correctForMaxStitchLength(EmbPattern* p, double maxStitchLength, double maxJumpLength)
{
int j = 0, splits, i;
double maxXY, maxLen;
EmbVector st, diff, add;
EmbArray* newList;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_correctForMaxStitchLength(), p argument is null\n");
return;
}
if (p->stitchList->count > 1) {
newList = embArray_create(EMB_STITCH);
embArray_addStitch(newList, p->stitchList->stitch[0]);
for (i = 1; i < p->stitchList->count; i++) {
st.x = p->stitchList->stitch[i - 1].x;
st.y = p->stitchList->stitch[i - 1].y;
diff.x = p->stitchList->stitch[i].x - st.x;
diff.y = p->stitchList->stitch[i].y - st.y;
maxXY = embVector_getLength(diff);
if (maxXY > maxStitchLength) {
if (p->stitchList->stitch[i].flags & (JUMP | TRIM))
maxLen = maxJumpLength;
else
maxLen = maxStitchLength;
splits = (int)ceil((double)maxXY / maxLen);
if (splits > 1) {
embVector_multiply(diff, (double)(1.0 / splits), &add);
for (j = 1; j < splits; j++) {
EmbStitch s;
s = p->stitchList->stitch[i - 1];
s.x += add.x * j;
s.y += add.y * j;
embArray_addStitch(newList, s);
}
}
}
embArray_addStitch(newList, p->stitchList->stitch[i]);
}
embArray_free(p->stitchList);
p->stitchList = newList;
}
embPattern_end(p);
}
void embPattern_center(EmbPattern* p)
{
/* TODO: review this. currently not used in anywhere. Also needs to handle various design objects */
int moveLeft, moveTop, i;
EmbRect boundingRect;
EmbStitch s;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_center(), p argument is null\n");
return;
}
boundingRect = embPattern_calcBoundingBox(p);
moveLeft = (int)(boundingRect.left - (embRect_width(boundingRect) / 2.0));
moveTop = (int)(boundingRect.top - (embRect_height(boundingRect) / 2.0));
for (i = 0; i < p->stitchList->count; i++) {
s = p->stitchList->stitch[i];
s.x -= moveLeft;
s.y -= moveTop;
}
}
/* TODO: Description needed. */
void embPattern_loadExternalColorFile(EmbPattern* p, const char* fileName)
{
char *dotPos, *extractName;
char hasRead;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_loadExternalColorFile(), p argument is null\n");
return;
}
if (!fileName) {
embLog("ERROR: emb-pattern.c embPattern_loadExternalColorFile(), fileName argument is null\n");
return;
}
extractName = (char*)malloc(strlen(fileName) + 5);
if (!extractName) {
embLog("ERROR: emb-pattern.c embPattern_loadExternalColorFile(), cannot allocate memory for extractName\n");
return;
}
hasRead = 0;
strcpy(extractName, fileName);
dotPos = strrchr(fileName, '.');
*dotPos = 0;
strcat(extractName, ".edr");
hasRead = embPattern_readAuto(p, extractName);
if (!hasRead) {
strcpy(extractName, fileName);
*dotPos = 0;
strcat(extractName, ".rgb");
hasRead = embPattern_readAuto(p, extractName);
}
if (!hasRead) {
strcpy(extractName, fileName);
*dotPos = 0;
strcat(extractName, ".col");
hasRead = embPattern_readAuto(p, extractName);
}
if (!hasRead) {
strcpy(extractName, fileName);
*dotPos = 0;
strcat(extractName, ".inf");
hasRead = embPattern_readAuto(p, extractName);
}
free(extractName);
}
/*! Frees all memory allocated in the pattern (\a p). */
void embPattern_free(EmbPattern* p)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_free(), p argument is null\n");
return;
}
embArray_free(p->stitchList);
embArray_free(p->threads);
embArray_free(p->arcs);
embArray_free(p->circles);
embArray_free(p->ellipses);
embArray_free(p->lines);
embArray_free(p->paths);
embArray_free(p->points);
embArray_free(p->polygons);
embArray_free(p->polylines);
embArray_free(p->rects);
embArray_free(p->splines);
free(p);
}
/*! Adds a circle object to pattern (\a p) with its center at the absolute
* position (\a cx,\a cy) with a radius of (\a r). Positive y is up.
* Units are in millimeters. */
void embPattern_addCircleObjectAbs(EmbPattern* p, double cx, double cy, double r)
{
EmbCircle circle;
circle.center.x = cx;
circle.center.y = cy;
circle.radius = r;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addCircleObjectAbs(), p argument is null\n");
return;
}
if (p->circles == 0) {
p->circles = embArray_create(EMB_CIRCLE);
}
embArray_addCircle(p->circles, circle, 0, black);
}
/*! Adds an ellipse object to pattern (\a p) with its center at the
* absolute position (\a cx,\a cy) with radii of (\a rx,\a ry). Positive y is up.
* Units are in millimeters. */
void embPattern_addEllipseObjectAbs(EmbPattern* p, double cx, double cy, double rx, double ry)
{
EmbEllipse ellipse;
ellipse.center.x = cx;
ellipse.center.y = cy;
ellipse.radius.x = rx;
ellipse.radius.y = ry;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addEllipseObjectAbs(), p argument is null\n");
return;
}
if (!p->ellipses) {
p->ellipses = embArray_create(EMB_ELLIPSE);
}
embArray_addEllipse(p->ellipses, ellipse, 0.0, 0, black);
}
/*! Adds a line object to pattern (\a p) starting at the absolute position
* (\a x1,\a y1) and ending at the absolute position (\a x2,\a y2).
* Positive y is up. Units are in millimeters.
*/
void embPattern_addLineObjectAbs(EmbPattern* p, double x1, double y1, double x2, double y2)
{
EmbLineObject lineObj;
lineObj.line.start.x = x1;
lineObj.line.start.y = y1;
lineObj.line.end.x = x2;
lineObj.line.end.y = y2;
lineObj.color.r = 0;
lineObj.color.g = 0;
lineObj.color.b = 0;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addLineObjectAbs(), p argument is null\n");
return;
}
if (p->circles == 0) {
p->lines = embArray_create(EMB_LINE);
}
embArray_addLine(p->lines, lineObj);
}
void embPattern_addPathObjectAbs(EmbPattern* p, EmbPathObject* obj)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addPathObjectAbs(), p argument is null\n");
return;
}
if (!obj) {
embLog("ERROR: emb-pattern.c embPattern_addPathObjectAbs(), obj argument is null\n");
return;
}
if (!obj->pointList) {
embLog("ERROR: emb-pattern.c embPattern_addPathObjectAbs(), obj->pointList is empty\n");
return;
}
if (!p->paths) {
p->paths = embArray_create(EMB_PATH);
}
embArray_addPath(p->paths, obj);
}
/*! Adds a point object to pattern (\a p) at the absolute position (\a x,\a y). Positive y is up. Units are in millimeters. */
void embPattern_addPointObjectAbs(EmbPattern* p, double x, double y)
{
EmbPointObject pointObj;
pointObj.point.x = x;
pointObj.point.y = y;
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addPointObjectAbs(), p argument is null\n");
return;
}
if (!p->points) {
p->points = embArray_create(EMB_POINT);
}
embArray_addPoint(p->points, &pointObj);
}
void embPattern_addPolygonObjectAbs(EmbPattern* p, EmbPolygonObject* obj)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addPolygonObjectAbs(), p argument is null\n");
return;
}
if (!obj) {
embLog("ERROR: emb-pattern.c embPattern_addPolygonObjectAbs(), obj argument is null\n");
return;
}
if (!obj->pointList) {
embLog("ERROR: emb-pattern.c embPattern_addPolygonObjectAbs(), obj->pointList is empty\n");
return;
}
if (!p->polygons) {
p->polygons = embArray_create(EMB_POLYGON);
}
embArray_addPolygon(p->polygons, obj);
}
void embPattern_addPolylineObjectAbs(EmbPattern* p, EmbPolylineObject* obj)
{
if (!p) {
embLog("ERROR: emb-pattern.c embPattern_addPolylineObjectAbs(), p argument is null\n");
return;
}