Debian Codegraph @

examples/cpp/test/test_example.cpp

raw
1
2 #include <string>
3 #include <iostream>
4 #include <opencamlib/ocl.hpp>
5 #include <opencamlib/waterline.hpp>
6 #include <opencamlib/adaptivewaterline.hpp>
7 #include <opencamlib/pathdropcutter.hpp>
8 #include <opencamlib/adaptivepathdropcutter.hpp>
9 #include <opencamlib/stlsurf.hpp>
10 #include <opencamlib/stlreader.hpp>
11 #include <opencamlib/cylcutter.hpp>
12 #include <opencamlib/ballcutter.hpp>
13 #include <opencamlib/bullcutter.hpp>
14 #include <opencamlib/conecutter.hpp>
15 #include <opencamlib/point.hpp>
16 #include <opencamlib/line.hpp>
17 #include <opencamlib/path.hpp>
18
19 void printXYZ(ocl::Point point)
20 {
21 // printf("X%g ", round(point.x * 100000.0) / 100000.0);
22 // printf("Y%g ", round(point.y * 100000.0) / 100000.0);
23 // printf("Z%g", round(point.z * 100000.0) / 100000.0);
24 }
25
26 void linear(ocl::Point point)
27 {
28 // printf("G01 ");
29 // printXYZ(point);
30 // printf("\n");
31 }
32
33 void moveSafely(ocl::Point point)
34 {
35 // printf("G00 Z10\n");
36 // printf("G00 ");
37 // printf("X%g ", round(point.x * 100000.0) / 100000.0);
38 // printf("Y%g\n", round(point.y * 100000.0) / 100000.0);
39 // printf("G01 ");
40 // printf("Z%g", round(point.z * 100000.0) / 100000.0);
41 // printf(" F50\n");
42 }
43
44 void printPoints(std::vector<ocl::Point> points)
45 {
46 for (auto j = 0; j < points.size(); j++)
47 {
48 auto point = points[j];
49 if (j == 0)
50 moveSafely(point);
51 else
52 linear(point);
53 }
54 }
55
56 void printPoints(std::vector<ocl::CLPoint> points)
57 {
58 for (auto j = 0; j < points.size(); j++)
59 {
60 auto point = points[j];
61 if (j == 0)
62 moveSafely(point);
63 else
64 linear(point);
65 }
66 }
67
68 void printLoops(std::vector<std::vector<ocl::Point>> loops)
69 {
70 for (auto i = 0; i < loops.size(); i++)
71 {
72 printPoints(loops[i]);
73 }
74 }
75
76 void waterline(ocl::STLSurf surface, ocl::MillingCutter *cutter, double z, double sampling)
77 {
78 ocl::Waterline wl = ocl::Waterline();
79 wl.setSTL(surface);
80 wl.setCutter(cutter);
81 wl.setSampling(sampling);
82 for (double h = 0; h < z; h = h + 0.1)
83 {
84 wl.reset();
85 wl.setZ(h);
86 wl.run();
87 auto loops = wl.getLoops();
88 printLoops(loops);
89 }
90 }
91
92 void adaptiveWaterline(ocl::STLSurf surface, ocl::MillingCutter *cutter, double z, double sampling, double minSampling)
93 {
94 ocl::AdaptiveWaterline awl = ocl::AdaptiveWaterline();
95 awl.setSTL(surface);
96 awl.setCutter(cutter);
97 awl.setSampling(sampling);
98 awl.setMinSampling(minSampling);
99 for (double h = 0; h < z; h = h + 0.1)
100 {
101 awl.reset();
102 awl.setZ(h);
103 awl.run();
104 auto loops = awl.getLoops();
105 printLoops(loops);
106 }
107 }
108
109 void pathDropCutter(ocl::STLSurf surface, ocl::MillingCutter *cutter, double sampling, ocl::Path *path)
110 {
111 ocl::PathDropCutter pdc = ocl::PathDropCutter();
112 pdc.setSTL(surface);
113 pdc.setCutter(cutter);
114 pdc.setPath(path);
115 pdc.setSampling(sampling);
116 pdc.reset();
117 pdc.setZ(0);
118 pdc.run();
119 auto points = pdc.getPoints();
120 printPoints(points);
121 }
122
123 void adaptivePathDropCutter(ocl::STLSurf surface, ocl::MillingCutter *cutter, double sampling, double minSampling, ocl::Path *path)
124 {
125 ocl::AdaptivePathDropCutter apdc = ocl::AdaptivePathDropCutter();
126 apdc.setSTL(surface);
127 apdc.setCutter(cutter);
128 apdc.setPath(path);
129 apdc.setSampling(sampling);
130 apdc.setMinSampling(minSampling);
131 apdc.reset();
132 apdc.setZ(0);
133 apdc.run();
134 auto points = apdc.getPoints();
135 printPoints(points);
136 }
137
138 int main()
139 {
140 std::cout << "ocl version: " << ocl::version() << "\n";
141 std::cout << "max threads: " << ocl::max_threads() << "\n";
142 ocl::STLSurf surface = ocl::STLSurf();
143 std::wstring stlPath = L"../../../../stl/gnu_tux_mod.stl";
144 ocl::STLReader(stlPath, surface);
145 std::cout << "surface size: " << surface.size() << "\n";
146
147 ocl::CylCutter cylCutter = ocl::CylCutter(0.4, 10);
148 ocl::BallCutter ballCutter = ocl::BallCutter(4, 20);
149 ocl::BullCutter bullCutter = ocl::BullCutter(4, 0.05, 20);
150 ocl::ConeCutter coneCutter = ocl::ConeCutter(4, 0.05, 20);
151 std::vector<ocl::MillingCutter *> cutters;
152 cutters.push_back(&cylCutter);
153 cutters.push_back(&ballCutter);
154 cutters.push_back(&bullCutter);
155 cutters.push_back(&coneCutter);
156 double z = 0.5;
157 double sampling = 0.1;
158 for (auto cutter : cutters)
159 {
160 std::cout << "WL + Cutter: " << cutter->str() << "\n";
161 waterline(surface, cutter, z, sampling);
162 }
163 double minSampling = 0.01;
164 for (auto cutter : cutters)
165 {
166 std::cout << "AWL + Cutter: " << cutter->str() << "\n";
167 adaptiveWaterline(surface, cutter, z, sampling, minSampling);
168 }
169 ocl::Path path = ocl::Path();
170 int i = 0;
171 for (double y = 0; y <= 0.2; y = y + 0.1)
172 {
173 bool ltr = ((int)i % 2) == 0;
174 ocl::Point p1 = ocl::Point(ltr ? -2 : 11, y, 0);
175 ocl::Point p2 = ocl::Point(ltr ? 11 : -2, y, 0);
176 ocl::Line l = ocl::Line(p1, p2);
177 path.append(l);
178 ocl::Point p3 = ocl::Point(ltr ? 11 : -2, y + 1, 0);
179 ocl::Line l2 = ocl::Line(p2, p3);
180 path.append(l2);
181 i++;
182 }
183 for (auto cutter : cutters)
184 {
185 std::cout << "PDC + Cutter: " << cutter->str() << "\n";
186 pathDropCutter(surface, cutter, sampling, &path);
187 }
188 for (auto cutter : cutters)
189 {
190 std::cout << "APDC: " << cutter->str() << "\n";
191 adaptivePathDropCutter(surface, cutter, sampling, minSampling, &path);
192 }
193
194 return 0;
195 }