1package com.example.mazatrol.optimization;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class PocketMillingOptimizer {
7
8
9public static class CuttingPass {
10private double xOffset;
11private double yOffset;
12private double depth;
13
14public CuttingPass(double xOffset, double yOffset, double depth) {
15this.xOffset = xOffset;
16this.yOffset = yOffset;
17this.depth = depth;
18}
19
20public double getXOffset() { return xOffset; }
21public double getYOffset() { return yOffset; }
22public double getDepth() { return depth; }
23}
24
25
26
27
28
29
30
31
32
33
34
35
36public static List<CuttingPass> optimizePocketPath(double pocketWidth, double pocketHeight, double toolDiameter, double maxDepthPerPass, double totalDepth) {
37List<CuttingPass> passes = new ArrayList<>();
38
39
40if (pocketWidth <= 0 || pocketHeight <= 0 || toolDiameter <= 0 || maxDepthPerPass <= 0 || totalDepth <= 0) {
41
42return passes;
43}
44
45
46int numPasses = (int) Math.ceil(totalDepth / maxDepthPerPass);
47if (numPasses == 0) numPasses = 1;
48
49
50double actualDepthPerPass = totalDepth / numPasses;
51
52
53
54double pocketCenterX = pocketWidth / 2.0;
55double pocketCenterY = pocketHeight / 2.0;
56
57
58
59double currentDepth = 0.0;
60for (int i = 0; i < numPasses; i++) {
61double passDepth = currentDepth + actualDepthPerPass;
62if (passDepth > totalDepth) {
63passDepth = totalDepth;
64}
65
66
67
68double toolRadius = toolDiameter / 2.0;
69double remainingWidth = pocketWidth - toolDiameter;
70double remainingHeight = pocketHeight - toolDiameter;
71
72
73if (remainingWidth < 0 || remainingHeight < 0) {
74
75break;
76}
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91double edgeOffset = i * (toolDiameter / 2.0);
92double currentPassXOffset = pocketCenterX;
93double currentPassYOffset = pocketCenterY;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113passes.add(new CuttingPass(pocketCenterX, pocketCenterY, passDepth));
114
115currentDepth = passDepth;
116if (currentDepth >= totalDepth) break;
117}
118
119
120if (passes.isEmpty() && totalDepth > 0) {
121passes.add(new CuttingPass(pocketCenterX, pocketCenterY, totalDepth));
122}
123
124return passes;
125}
126
127public static void main(String[] args) {
128double pocketWidth = 50.0;
129double pocketHeight = 40.0;
130double toolDiameter = 10.0;
131double maxDepthPerPass = 5.0;
132double totalDepth = 20.0;
133
134List<CuttingPass> optimizedPath = optimizePocketPath(pocketWidth, pocketHeight, toolDiameter, maxDepthPerPass, totalDepth);
135
136System.out.println("Optimized Pocket Milling Passes:");
137for (CuttingPass pass : optimizedPath) {
138System.out.println(" Depth: " + pass.getDepth() + ", Center X Offset: " + pass.getXOffset() + ", Center Y Offset: " + pass.getYOffset());
139}
140
141
142System.out.println("\nExample with pocket too small:");
143List<CuttingPass> smallPocketPath = optimizePocketPath(5.0, 5.0, 10.0, 5.0, 10.0);
144System.out.println("Number of passes: " + smallPocketPath.size());
145}
146}