-
Notifications
You must be signed in to change notification settings - Fork 15
/
calc.c
3230 lines (3081 loc) · 113 KB
/
calc.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
/*
Copyright 2014 by Oliver Dippel <oliver@multixmedia.org>
MacOSX - Changes by McUles <mcules@fpv-club.de>
Yosemite (OSX 10.10)
This library 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 library 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 Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in `/usr/share/common-licenses/GPL'.
*/
#include <GL/gl.h>
#include <GL/glu.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <gtksourceview/gtksourceview.h>
#include <gtksourceview/gtksourcelanguagemanager.h>
#ifdef USE_VNC
#include <gtk-vnc.h>
#endif
#include <libgen.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#ifdef USE_G3D
#include <g3d/g3d.h>
#endif
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <pwd.h>
#include <dxf.h>
#include <font.h>
#include <setup.h>
#include <postprocessor.h>
#include <calc.h>
#include <pocket.h>
#define dot(ux,uy,uz,vx,vy,vz) (ux * vx + uy * vy + uz * vz)
extern float size_x;
extern float size_y;
extern double min_x;
extern double min_y;
extern double max_x;
extern double max_y;
extern FILE *fd_out;
extern int object_last;
extern char postcam_plugins[100][1024];
extern int postcam_plugin;
extern int update_post;
extern char *gcode_buffer;
extern char output_extension[128];
extern char output_info[1024];
extern double mill_distance_xy;
extern double mill_distance_z;
extern double move_distance_xy;
extern double move_distance_z;
int MaterialMax = 0;
_MATERIAL Material[100];
int mill_start = 0;
int mill_start_all = 0;
double mill_last_x = 0.0;
double mill_last_y = 0.0;
double mill_last_z = 0.0;
int tool_last = -1;
char *rotary_axis[3] = {"A", "B", "C"};
char cline[1024];
#ifndef CALLBACK
#define CALLBACK
#endif
extern GtkListStore *ListStore[P_LAST];
extern GtkWidget *OutputInfoLabel;
extern GtkWidget *gCodeViewLabel;
void postcam_load_source (char *plugin);
GLuint texture_load (char *filename);
void CALLBACK beginCallback(GLenum which) {
glBegin(which);
}
void CALLBACK errorCallback(GLenum errorCode) {
const GLubyte *estring;
estring = gluErrorString(errorCode);
// fprintf(stderr, "Tessellation Error: %s\n", (char *) estring);
// exit(0);
}
void CALLBACK endCallback(void) {
glEnd();
}
void CALLBACK vertexCallback(GLvoid *vertex) {
const GLdouble *pointer;
pointer = (GLdouble *) vertex;
glColor3dv(pointer+3);
glVertex3dv(pointer);
}
void CALLBACK combineCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut ) {
GLdouble *vertex;
int i;
vertex = (GLdouble *)malloc(6 * sizeof(GLdouble));
vertex[0] = coords[0];
vertex[1] = coords[1];
vertex[2] = coords[2];
for (i = 3; i < 6; i++) {
vertex[i] = weight[0] * vertex_data[0][i] + weight[1] * vertex_data[1][i] + weight[2] * vertex_data[2][i] + weight[3] * vertex_data[3][i];
}
*dataOut = vertex;
}
void object2poly (int object_num, double depth, double depth2, int invert) {
int num = 0;
int nverts = 0;
GLUtesselator *tobj;
GLdouble rect2[MAX_LINES][3];
if (PARAMETER[P_V_TEXTURES].vint == 1) {
glColor4f(1.0, 1.0, 1.0, 1.0);
texture_load(Material[PARAMETER[P_MAT_SELECT].vint].texture);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGend(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
glTexGend(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(0.002, 0.002, 0.002);
} else {
if (PARAMETER[P_O_SELECT].vint == object_num) {
glColor4f(1.0, 0.0, 0.0, 0.5);
} else {
if (invert == 0) {
glColor4f(0.0, 0.5, 0.2, 0.5);
} else {
glColor4f(0.0, 0.75, 0.3, 0.5);
}
}
}
tobj = gluNewTess();
gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid (CALLBACK*) ()) &glVertex3dv);
gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid (CALLBACK*) ()) &beginCallback);
gluTessCallback(tobj, GLU_TESS_END, (GLvoid (CALLBACK*) ()) &endCallback);
gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (CALLBACK*) ()) &errorCallback);
glShadeModel(GL_FLAT);
gluTessBeginPolygon(tobj, NULL);
if (invert == 0) {
if (myOBJECTS[object_num].climb == 0) {
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
} else {
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NEGATIVE);
}
} else {
if (myOBJECTS[object_num].climb == 0) {
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NEGATIVE);
} else {
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
}
}
gluTessNormal(tobj, 0, 0, 1);
gluTessBeginContour(tobj);
if (myLINES[myOBJECTS[object_num].line[0]].type == TYPE_CIRCLE) {
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);
int lnum = myOBJECTS[object_num].line[0];
float an = 0.0;
float r = myLINES[lnum].opt;
float x = myLINES[lnum].cx;
float y = myLINES[lnum].cy;
float last_x = x + r;
float last_y = y;
for (an = 0.0; an < 360.0; an += 4.5) {
float angle1 = toRad(an);
float x1 = r * cos(angle1);
float y1 = r * sin(angle1);
rect2[nverts][0] = (GLdouble)x + x1;
rect2[nverts][1] = (GLdouble)y + y1;
rect2[nverts][2] = (GLdouble)depth;
gluTessVertex(tobj, rect2[nverts], rect2[nverts]);
if (depth != depth2) {
glBegin(GL_QUADS);
glVertex3f((float)last_x, (float)last_y, depth);
glVertex3f((float)x + x1, (float)y + y1, depth);
glVertex3f((float)x + x1, (float)y + y1, depth2);
glVertex3f((float)last_x, (float)last_y, depth2);
glEnd();
}
last_x = (float)x + x1;
last_y = (float)y + y1;
nverts++;
}
} else {
for (num = 0; num < line_last; num++) {
if (myOBJECTS[object_num].line[num] != 0) {
int lnum = myOBJECTS[object_num].line[num];
rect2[nverts][0] = (GLdouble)myLINES[lnum].x1;
rect2[nverts][1] = (GLdouble)myLINES[lnum].y1;
rect2[nverts][2] = (GLdouble)depth;
gluTessVertex(tobj, rect2[nverts], rect2[nverts]);
if (depth != depth2) {
glBegin(GL_QUADS);
glVertex3f((float)myLINES[lnum].x1, (float)myLINES[lnum].y1, depth);
glVertex3f((float)myLINES[lnum].x2, (float)myLINES[lnum].y2, depth);
glVertex3f((float)myLINES[lnum].x2, (float)myLINES[lnum].y2, depth2);
glVertex3f((float)myLINES[lnum].x1, (float)myLINES[lnum].y1, depth2);
glEnd();
}
nverts++;
}
}
}
int num5 = 0;
for (num5 = 0; num5 < object_last; num5++) {
if (num5 != object_num && myOBJECTS[num5].closed == 1 && myOBJECTS[num5].inside == 1) {
int lnum = myOBJECTS[num5].line[0];
int pipret = 0;
double testx = myLINES[lnum].x1;
double testy = myLINES[lnum].y1;
pipret = point_in_object(object_num, -1, testx, testy);
if (pipret == 1) {
gluNextContour(tobj, GLU_INTERIOR);
if (myLINES[lnum].type == TYPE_CIRCLE) {
float an = 0.0;
float r = myLINES[lnum].opt;
float x = myLINES[lnum].cx;
float y = myLINES[lnum].cy;
float last_x = x + r;
float last_y = y;
if (myOBJECTS[object_num].climb == 1) {
for (an = 0.0; an < 360.0; an += 4.5) {
float angle1 = toRad(an);
float x1 = r * cos(angle1);
float y1 = r * sin(angle1);
rect2[nverts][0] = (GLdouble)x + x1;
rect2[nverts][1] = (GLdouble)y + y1;
rect2[nverts][2] = (GLdouble)depth;
gluTessVertex(tobj, rect2[nverts], rect2[nverts]);
if (depth != depth2) {
glBegin(GL_QUADS);
glVertex3f((float)last_x, (float)last_y, depth);
glVertex3f((float)x + x1, (float)y + y1, depth);
glVertex3f((float)x + x1, (float)y + y1, depth2);
glVertex3f((float)last_x, (float)last_y, depth2);
glEnd();
}
last_x = (float)x + x1;
last_y = (float)y + y1;
nverts++;
}
} else {
for (an = 360.0; an > 0.0; an -= 4.5) {
float angle1 = toRad(an);
float x1 = r * cos(angle1);
float y1 = r * sin(angle1);
rect2[nverts][0] = (GLdouble)x + x1;
rect2[nverts][1] = (GLdouble)y + y1;
rect2[nverts][2] = (GLdouble)depth;
gluTessVertex(tobj, rect2[nverts], rect2[nverts]);
if (depth != depth2) {
glBegin(GL_QUADS);
glVertex3f((float)last_x, (float)last_y, depth);
glVertex3f((float)x + x1, (float)y + y1, depth);
glVertex3f((float)x + x1, (float)y + y1, depth2);
glVertex3f((float)last_x, (float)last_y, depth2);
glEnd();
}
last_x = (float)x + x1;
last_y = (float)y + y1;
nverts++;
}
}
} else {
for (num = 0; num < line_last; num++) {
if (myOBJECTS[num5].line[num] != 0) {
int lnum = myOBJECTS[num5].line[num];
rect2[nverts][0] = (GLdouble)myLINES[lnum].x1;
rect2[nverts][1] = (GLdouble)myLINES[lnum].y1;
rect2[nverts][2] = (GLdouble)depth;
gluTessVertex(tobj, rect2[nverts], rect2[nverts]);
if (depth != depth2) {
glBegin(GL_QUADS);
glVertex3f((float)myLINES[lnum].x1, (float)myLINES[lnum].y1, depth);
glVertex3f((float)myLINES[lnum].x2, (float)myLINES[lnum].y2, depth);
glVertex3f((float)myLINES[lnum].x2, (float)myLINES[lnum].y2, depth2);
glVertex3f((float)myLINES[lnum].x1, (float)myLINES[lnum].y1, depth2);
glEnd();
}
nverts++;
}
}
}
}
}
}
gluTessEndPolygon(tobj);
gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid (CALLBACK*) ()) &vertexCallback);
gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid (CALLBACK*) ()) &beginCallback);
gluTessCallback(tobj, GLU_TESS_END, (GLvoid (CALLBACK*) ()) &endCallback);
gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (CALLBACK*) ()) &errorCallback);
gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid (CALLBACK*) ()) &combineCallback);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
gluDeleteTess(tobj);
}
void DrawLine (float x1, float y1, float x2, float y2, float z, float w) {
float angle = atan2(y2 - y1, x2 - x1);
float t2sina1 = w / 2 * sin(angle);
float t2cosa1 = w / 2 * cos(angle);
glBegin(GL_QUADS);
glVertex3f(x1 + t2sina1, y1 - t2cosa1, z);
glVertex3f(x2 + t2sina1, y2 - t2cosa1, z);
glVertex3f(x2 - t2sina1, y2 + t2cosa1, z);
glVertex3f(x1 - t2sina1, y1 + t2cosa1, z);
glEnd();
}
void DrawArrow (float x1, float y1, float x2, float y2, float z, float w) {
float dx = x2 - x1;
float dy = y2 - y1;
float len = sqrt(dx * dx + dy * dy);
float asize = 2.0;
if (len < asize) {
return;
}
float angle = atan2(dy, dx);
float off_x = asize * cos(angle + toRad(45.0 + 90.0));
float off_y = asize * sin(angle + toRad(45.0 + 90.0));
float off2_x = asize * cos(angle + toRad(-45.0 - 90.0));
float off2_y = asize * sin(angle + toRad(-45.0 - 90.0));
float half_x = x1 + (x2 - x1) / 2.0;
float half_y = y1 + (y2 - y1) / 2.0;
glBegin(GL_LINES);
glVertex3f(half_x, half_y, z);
glVertex3f(half_x + off_x, half_y + off_y, z);
glVertex3f(half_x, half_y, z);
glVertex3f(half_x + off2_x, half_y + off2_y, z);
glEnd();
}
void draw_line_wrap_conn (float x1, float y1, float depth1, float depth2) {
float ry = 0.0;
float rz = 0.0;
glBegin(GL_LINES);
point_rotate(y1, depth1, &ry, &rz);
glVertex3f(x1, ry, rz);
point_rotate(y1, depth2, &ry, &rz);
glVertex3f(x1, ry, rz);
glEnd();
}
void draw_line_wrap (float x1, float y1, float x2, float y2, float depth) {
float radius = (PARAMETER[P_MAT_DIAMETER].vdouble / 2.0) + depth;
float dX = x2 - x1;
float dY = y2 - y1;
float dashes = dY;
if (dashes < -1.0) {
dashes *= -1;
}
float an = y1 / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
float rangle = toRad(an - 90.0);
float ry = radius * cos(rangle);
float rz = radius * sin(rangle);
glBegin(GL_LINE_STRIP);
glVertex3f(x1, ry, -rz);
if (dashes > 1.0) {
float dashX = dX / dashes;
float dashY = dY / dashes;
float q = 0.0;
while (q++ < dashes) {
x1 += dashX;
y1 += dashY;
an = y1 / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
rangle = toRad(an - 90.0);
ry = radius * cos(rangle);
rz = radius * sin(rangle);
glVertex3f(x1, ry, -rz);
}
}
an = y2 / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
rangle = toRad(an - 90.0);
ry = radius * cos(rangle);
rz = radius * sin(rangle);
glVertex3f(x2, ry, -rz);
glEnd();
}
void draw_oline (float x1, float y1, float x2, float y2, float depth) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
draw_line_wrap(x1, y1, x2, y2, 0.0);
draw_line_wrap(x1, y1, x2, y2, depth);
draw_line_wrap_conn(x1, y1, 0.0, depth);
draw_line_wrap_conn(x2, y2, 0.0, depth);
} else {
glBegin(GL_LINES);
glVertex3f(x1, y1, 0.02);
glVertex3f(x2, y2, 0.02);
// if (PARAMETER[P_V_HELPLINES].vint == 1) {
glBegin(GL_LINES);
glVertex3f(x1, y1, depth);
glVertex3f(x2, y2, depth);
glVertex3f(x1, y1, depth);
glVertex3f(x1, y1, 0.02);
glVertex3f(x2, y2, depth);
glVertex3f(x2, y2, 0.02);
// }
glEnd();
}
}
void draw_line2 (float x1, float y1, float z1, float x2, float y2, float z2, float width) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
draw_line_wrap(x1, y1, x2, y2, 0.0);
draw_line_wrap(x1, y1, x2, y2, z1);
draw_line_wrap_conn(x1, y1, 0.0, z1);
draw_line_wrap_conn(x2, y2, 0.0, z1);
} else {
if (PARAMETER[P_V_HELPLINES].vint == 1) {
DrawLine(x1, y1, x2, y2, z1, width);
GLUquadricObj *quadric=gluNewQuadric();
gluQuadricNormals(quadric, GLU_SMOOTH);
gluQuadricOrientation(quadric,GLU_OUTSIDE);
glPushMatrix();
glTranslatef(x1, y1, z1);
gluDisk(quadric, 0.0, width / 2.0, 18, 1);
glPopMatrix();
glPushMatrix();
glTranslatef(x2, y2, z1);
gluDisk(quadric, 0.0, width / 2.0, 18, 1);
glPopMatrix();
gluDeleteQuadric(quadric);
}
glBegin(GL_LINES);
glVertex3f(x1, y1, z1);
glVertex3f(x2, y2, z2);
glEnd();
}
}
void draw_line (float x1, float y1, float z1, float x2, float y2, float z2, float width) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
glColor4f(1.0, 0.0, 1.0, 1.0);
draw_line_wrap(x1, y1, x2, y2, 0.0);
draw_line_wrap(x1, y1, x2, y2, z1);
draw_line_wrap_conn(x1, y1, 0.0, z1);
draw_line_wrap_conn(x2, y2, 0.0, z1);
} else {
if (PARAMETER[P_V_HELPDIA].vint == 1) {
glColor4f(1.0, 1.0, 0.0, 1.0);
DrawLine(x1, y1, x2, y2, z1, width);
GLUquadricObj *quadric=gluNewQuadric();
gluQuadricNormals(quadric, GLU_SMOOTH);
gluQuadricOrientation(quadric,GLU_OUTSIDE);
glPushMatrix();
glTranslatef(x1, y1, z1);
gluDisk(quadric, 0.0, width / 2.0, 18, 1);
glPopMatrix();
glPushMatrix();
glTranslatef(x2, y2, z1);
gluDisk(quadric, 0.0, width / 2.0, 18, 1);
glPopMatrix();
gluDeleteQuadric(quadric);
}
glColor4f(1.0, 0.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, z1 + 0.02);
glVertex3f(x2, y2, z2 + 0.02);
glEnd();
DrawArrow(x1, y1, x2, y2, z1 + 0.02, width);
}
}
void draw_line3 (float x1, float y1, float z1, float x2, float y2, float z2) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
glColor4f(1.0, 0.0, 1.0, 1.0);
draw_line_wrap(x1, y1, x2, y2, z1);
} else {
glColor4f(1.0, 0.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, z1 + 0.02);
glVertex3f(x2, y2, z2 + 0.02);
glEnd();
}
}
void append_gcode (char *text) {
#ifdef USE_POSTCAM
return;
#endif
if (gcode_buffer == NULL) {
int len = strlen(text) + 1;
gcode_buffer = (char *)malloc(len);
gcode_buffer[0] = 0;
} else {
int len = strlen(gcode_buffer) + strlen(text) + 1;
gcode_buffer = (char *)realloc((void *)gcode_buffer, len);
}
strcat(gcode_buffer, text);
}
void append_gcode_new (char *text) {
if (gcode_buffer == NULL) {
int len = strlen(text) + 1;
gcode_buffer = (char *)malloc(len);
gcode_buffer[0] = 0;
} else {
int len = strlen(gcode_buffer) + strlen(text) + 1;
gcode_buffer = (char *)realloc((void *)gcode_buffer, len);
}
strcat(gcode_buffer, text);
}
void line_invert (int num) {
double tempx = myLINES[num].x2;
double tempy = myLINES[num].y2;
myLINES[num].x2 = myLINES[num].x1;
myLINES[num].y2 = myLINES[num].y1;
myLINES[num].x1 = tempx;
myLINES[num].y1 = tempy;
myLINES[num].opt *= -1;
}
int point_in_object (int object_num, int object_ex, double testx, double testy) {
int result = 0;
int num = 0;
int onum = object_num;
testx += 0.0001;
testy += 0.0001;
if (object_num == -1) {
for (onum = 0; onum < object_last; onum++) {
if (onum == object_ex) {
continue;
}
if (myOBJECTS[onum].closed == 0) {
continue;
}
for (num = 0; num < line_last; num++) {
if (myOBJECTS[onum].line[num] != 0) {
int lnum = myOBJECTS[onum].line[num];
if (myLINES[lnum].y2 == testy) {
if ((myLINES[lnum].x2 == testx) || (myLINES[lnum].y1 == testy && ((myLINES[lnum].x2 > testx) == (myLINES[lnum].x1 < testx)))) {
fprintf(stderr, "Point on line\n");
return -1;
}
}
if ((myLINES[lnum].y1 < testy) != (myLINES[lnum].y2 < testy)) {
if (myLINES[lnum].x1 >= testx) {
if (myLINES[lnum].x2 > testx) {
result = 1 - result;
} else {
double d = (double)(myLINES[lnum].x1 - testx) * (myLINES[lnum].y2 - testy) - (double)(myLINES[lnum].x2 - testx) * (myLINES[lnum].y1 - testy);
if (!d) {
return -1;
}
if ((d > 0) == (myLINES[lnum].y2 > myLINES[lnum].y1)) {
result = 1 - result;
}
}
} else {
if (myLINES[lnum].x2 > testx) {
double d = (double)(myLINES[lnum].x1 - testx) * (myLINES[lnum].y2 - testy) - (double)(myLINES[lnum].x2 - testx) * (myLINES[lnum].y1 - testy);
if (!d) {
return -1;
}
if ((d > 0) == (myLINES[lnum].y2 > myLINES[lnum].y1)) {
result = 1 - result;
}
}
}
}
}
}
}
} else {
if (myOBJECTS[onum].closed == 0) {
return 0;
}
for (num = 0; num < line_last; num++) {
if (myOBJECTS[onum].line[num] != 0) {
int lnum = myOBJECTS[onum].line[num];
if (myLINES[lnum].y2 == testy) {
if ((myLINES[lnum].x2 == testx) || (myLINES[lnum].y1 == testy && ((myLINES[lnum].x2 > testx) == (myLINES[lnum].x1 < testx)))) {
fprintf(stderr, "Point on line\n");
return -1;
}
}
if ((myLINES[lnum].y1 < testy) != (myLINES[lnum].y2 < testy)) {
if (myLINES[lnum].x1 >= testx) {
if (myLINES[lnum].x2 > testx) {
result = 1 - result;
} else {
double d = (double)(myLINES[lnum].x1 - testx) * (myLINES[lnum].y2 - testy) - (double)(myLINES[lnum].x2 - testx) * (myLINES[lnum].y1 - testy);
if (!d) {
return -1;
}
if ((d > 0) == (myLINES[lnum].y2 > myLINES[lnum].y1)) {
result = 1 - result;
}
}
} else {
if (myLINES[lnum].x2 > testx) {
double d = (double)(myLINES[lnum].x1 - testx) * (myLINES[lnum].y2 - testy) - (double)(myLINES[lnum].x2 - testx) * (myLINES[lnum].y1 - testy);
if (!d) {
return -1;
}
if ((d > 0) == (myLINES[lnum].y2 > myLINES[lnum].y1)) {
result = 1 - result;
}
}
}
}
}
}
}
return result;
}
void point_rotate (float y, float depth, float *ny, float *nz) {
float radius = (PARAMETER[P_MAT_DIAMETER].vdouble / 2.0) + depth;
float an = y / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
float rangle = toRad(an - 90.0);
*ny = radius * cos(rangle);
*nz = radius * -sin(rangle);
}
double _X (double x) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1 && PARAMETER[P_H_ROTARYAXIS].vint == 1) {
return x / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
}
return x;
}
double _Y (double y) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1 && PARAMETER[P_H_ROTARYAXIS].vint == 0) {
return y / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
}
return y;
}
double _Z (double z) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
return z + (PARAMETER[P_MAT_DIAMETER].vdouble / 2.0);
}
return z;
}
void translateAxisX (double x, char *ret_str) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1 && PARAMETER[P_H_ROTARYAXIS].vint == 1) {
double an = x / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
sprintf(ret_str, "%s%f", rotary_axis[PARAMETER[P_H_ROTARYAXIS].vint], an);
} else {
sprintf(ret_str, "X%f", x);
}
}
void translateAxisY (double y, char *ret_str) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1 && PARAMETER[P_H_ROTARYAXIS].vint == 0) {
double an = y / (PARAMETER[P_MAT_DIAMETER].vdouble * PI) * 360;
sprintf(ret_str, "%s%f", rotary_axis[PARAMETER[P_H_ROTARYAXIS].vint], an);
} else {
sprintf(ret_str, "Y%f", y);
}
}
void translateAxisZ (double z, char *ret_str) {
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
sprintf(ret_str, "Z%f", z + (PARAMETER[P_MAT_DIAMETER].vdouble / 2.0));
} else {
sprintf(ret_str, "Z%f", z);
}
}
int object_line_last (int object_num) {
int num = 0;
int ret = 0;
for (num = 0; num < line_last; num++) {
if (myOBJECTS[object_num].line[num] != 0) {
ret = num;
}
}
return ret;
}
double get_len (double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double len = sqrt(dx * dx + dy * dy);
return len;
}
double set_positive (double val) {
if (val < 0.0) {
val *= -1;
}
return val;
}
/* set new first line in object */
void resort_object (int object_num, int start) {
int num4 = 0;
int num5 = 0;
int OTEMPLINE[MAX_LINES];
for (num4 = 0; num4 < line_last; num4++) {
OTEMPLINE[num4] = 0;
}
for (num4 = start; num4 < line_last; num4++) {
if (myOBJECTS[object_num].line[num4] != 0) {
OTEMPLINE[num5++] = myOBJECTS[object_num].line[num4];
}
}
for (num4 = 0; num4 < start; num4++) {
if (myOBJECTS[object_num].line[num4] != 0) {
OTEMPLINE[num5++] = myOBJECTS[object_num].line[num4];
}
}
for (num4 = 0; num4 < num5; num4++) {
myOBJECTS[object_num].line[num4] = OTEMPLINE[num4];
}
}
/* reverse lines in object */
void redir_object (int object_num) {
int num = 0;
int num5 = 0;
int OTEMPLINE[MAX_LINES];
for (num = 0; num < line_last; num++) {
OTEMPLINE[num] = 0;
}
for (num = line_last - 1; num >= 0; num--) {
if (myOBJECTS[object_num].line[num] != 0) {
OTEMPLINE[num5++] = myOBJECTS[object_num].line[num];
int lnum = myOBJECTS[object_num].line[num];
line_invert(lnum);
}
}
for (num = 0; num < num5; num++) {
myOBJECTS[object_num].line[num] = OTEMPLINE[num];
}
}
double line_len (int lnum) {
double dx = myLINES[lnum].x2 - myLINES[lnum].x1;
double dy = myLINES[lnum].y2 - myLINES[lnum].y1;
double len = sqrt(dx * dx + dy * dy);
return len;
}
double line_angle (int lnum) {
double dx = myLINES[lnum].x2 - myLINES[lnum].x1;
double dy = myLINES[lnum].y2 - myLINES[lnum].y1;
double alpha = toDeg(atan(dy / dx));
if (dx < 0 && dy >= 0) {
alpha = alpha + 180;
} else if (dx < 0 && dy < 0) {
alpha = alpha - 180;
}
return alpha;
}
double vector_angle (double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double alpha = toDeg(atan(dy / dx));
if (dx < 0 && dy >= 0) {
alpha = alpha + 180;
} else if (dx < 0 && dy < 0) {
alpha = alpha - 180;
}
return alpha;
}
double line_angle2 (int lnum) {
double dx = myLINES[lnum].x2 - myLINES[lnum].x1;
double dy = myLINES[lnum].y2 - myLINES[lnum].y1;
double alpha = toDeg(atan2(dx, dy));
return alpha;
}
void add_angle_offset (double *check_x, double *check_y, double radius, double alpha) {
double angle = toRad(alpha);
*check_x += radius * cos(angle);
*check_y += radius * sin(angle);
}
/* optimize dir of object / inside=cw, outside=ccw */
void object_optimize_dir (int object_num) {
int pipret = 0;
if (myOBJECTS[object_num].line[0] != 0) {
if (myOBJECTS[object_num].closed == 1) {
int lnum = myOBJECTS[object_num].line[0];
double alpha = line_angle(lnum);
double len = line_len(lnum);
double check_x = myLINES[lnum].x1;
double check_y = myLINES[lnum].y1;
add_angle_offset(&check_x, &check_y, len / 2.0, alpha);
if (myOBJECTS[object_num].climb == 0) {
add_angle_offset(&check_x, &check_y, -0.01, alpha + 90);
} else {
add_angle_offset(&check_x, &check_y, 0.01, alpha + 90);
}
pipret = point_in_object(object_num, -1, check_x, check_y);
if (myOBJECTS[object_num].force == 1) {
if ((pipret == 1 && myOBJECTS[object_num].offset == 2) || (pipret == 0 && myOBJECTS[object_num].offset == 1)) {
redir_object(object_num);
}
} else {
if ((pipret == 1 && myOBJECTS[object_num].inside == 0) || (pipret == 0 && myOBJECTS[object_num].inside == 1)) {
redir_object(object_num);
}
}
}
}
}
int intersect_check (double p0_x, double p0_y, double p1_x, double p1_y, double p2_x, double p2_y, double p3_x, double p3_y, double *i_x, double *i_y) {
double s02_x, s02_y, s10_x, s10_y, s32_x, s32_y, s_numer, t_numer, denom, t;
s10_x = p1_x - p0_x;
s10_y = p1_y - p0_y;
s02_x = p0_x - p2_x;
s02_y = p0_y - p2_y;
s_numer = s10_x * s02_y - s10_y * s02_x;
if (s_numer < 0) {
return 0;
}
s32_x = p3_x - p2_x;
s32_y = p3_y - p2_y;
t_numer = s32_x * s02_y - s32_y * s02_x;
if (t_numer < 0) {
return 0;
}
denom = s10_x * s32_y - s32_x * s10_y;
if (s_numer > denom || t_numer > denom) {
return 0;
}
t = t_numer / denom;
if (i_x != NULL) {
*i_x = p0_x + (t * s10_x);
}
if (i_y != NULL) {
*i_y = p0_y + (t * s10_y);
}
return 1;
}
void intersect (double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, double l2y2, double *x, double *y) {
double a1 = l1x2 - l1x1;
double b1 = l2x1 - l2x2;
double c1 = l2x1 - l1x1;
double a2 = l1y2 - l1y1;
double b2 = l2y1 - l2y2;
double c2 = l2y1 - l1y1;
double t = (b1 * c2 - b2 * c1) / (a2 * b1 - a1 * b2);
*x = l1x1 + t * a1;
*y = l1y1 + t * a2;
return;
}
void mill_begin (void) {
char tmp_str[1024];
// init output
mill_start_all = 0;
tool_last = -1;
mill_distance_xy = 0.0;
mill_distance_z = 0.0;
move_distance_xy = 0.0;
move_distance_z = 0.0;
mill_last_x = 0.0;
mill_last_y = 0.0;
mill_last_z = PARAMETER[P_CUT_SAVE].vdouble;
if (gcode_buffer != NULL) {
free(gcode_buffer);
gcode_buffer = NULL;
}
#ifdef USE_POSTCAM
if (postcam_plugin != PARAMETER[P_H_POST].vint) {
postcam_exit_lua();
strcpy(output_extension, "ngc");
strcpy(output_info, "");
postcam_init_lua(postcam_plugins[PARAMETER[P_H_POST].vint]);
postcam_plugin = PARAMETER[P_H_POST].vint;
gtk_label_set_text(GTK_LABEL(OutputInfoLabel), output_info);
sprintf(tmp_str, "Output (%s)", output_extension);
gtk_label_set_text(GTK_LABEL(gCodeViewLabel), tmp_str);
postcam_load_source(postcam_plugins[PARAMETER[P_H_POST].vint]);
}
postcam_var_push_string("fileName", PARAMETER[P_V_DXF].vstr);
postcam_var_push_string("postName", postcam_plugins[PARAMETER[P_H_POST].vint]);
postcam_var_push_string("date", "---");
postcam_var_push_double("metric", 1.0);
postcam_var_push_int("feedRate", PARAMETER[P_M_PLUNGE_SPEED].vint);
postcam_var_push_int("spindleSpeed", PARAMETER[P_TOOL_SPEED].vint);
postcam_var_push_double("currentX", _X(0.0));
postcam_var_push_double("currentY", _Y(0.0));
postcam_var_push_double("currentZ", _Z(0.0));
postcam_var_push_double("endX", _X(0.0));
postcam_var_push_double("endY", _Y(0.0));
postcam_var_push_double("endZ", _Z(0.0));
postcam_var_push_double("toolOffset", 0.0);
postcam_var_push_int("tool", -1);
postcam_var_push_int("lastinst", 0);
postcam_var_push_int("velocityMode", PARAMETER[P_M_VELOCITYMODE].vint);
postcam_var_push_double("blendingTolerance", PARAMETER[P_M_BLENDINGTOLERANCE].vdouble);
// SetupShowGcode(fd_out);
postcam_call_function("OnInit");
if (PARAMETER[P_M_ROTARYMODE].vint == 1) {
if (PARAMETER[P_H_ROTARYAXIS].vint == 1) {
postcam_var_push_string("axisX", "B");
} else {
postcam_var_push_string("axisX", "X");
}
if (PARAMETER[P_H_ROTARYAXIS].vint == 0) {
postcam_var_push_string("axisY", "A");
} else {
postcam_var_push_string("axisY", "Y");
}
} else {
postcam_var_push_string("axisX", "X");
postcam_var_push_string("axisY", "Y");
}
postcam_var_push_string("axisZ", "Z");
#else
append_gcode("G21 (Metric)\n");
append_gcode("G40 (No Offsets)\n");
append_gcode("G90 (Absolute-Mode)\n");
sprintf(cline, "F%i\n", PARAMETER[P_M_FEEDRATE].vint);
append_gcode(cline);
#endif
}
void mill_z (int gcmd, double z) {
#ifdef USE_POSTCAM
postcam_var_push_int("feedRate", PARAMETER[P_M_PLUNGE_SPEED].vint);
postcam_var_push_double("currentX", _X(mill_last_x));
postcam_var_push_double("currentY", _Y(mill_last_y));
postcam_var_push_double("currentZ", _Z(mill_last_z));
postcam_var_push_double("endX", _X(mill_last_x));
postcam_var_push_double("endY", _Y(mill_last_y));
postcam_var_push_double("endZ", _Z(z));
if (gcmd == 0) {
move_distance_z += set_positive(z - mill_last_z);
postcam_call_function("OnRapid");
} else {
mill_distance_z += set_positive(z - mill_last_z);
postcam_call_function("OnMove");
}
#else
char tz_str[128];
translateAxisZ(z, tz_str);
if (gcmd == 0) {
sprintf(cline, "G0%i %s\n", gcmd, tz_str);