| | |
| | | contentPanel.add(createTextAreaSection(obstacleTitle, obstacleArea)); |
| | | |
| | | // 割草宽度 |
| | | widthField = createInfoTextField(widthValue != null ? widthValue : "", true); |
| | | contentPanel.add(createTextFieldSection("割草宽度 (cm)", widthField)); |
| | | String displayWidth = ""; |
| | | if (widthValue != null && !widthValue.trim().isEmpty() && !"-1".equals(widthValue.trim())) { |
| | | try { |
| | | double val = Double.parseDouble(widthValue.trim()); |
| | | // 简单的启发式转换:如果值大于5,假设是厘米,转换为米 |
| | | if (val > 5) { |
| | | val = val / 100.0; |
| | | } |
| | | displayWidth = String.format(Locale.US, "%.2f", val); |
| | | } catch (NumberFormatException e) { |
| | | displayWidth = widthValue; |
| | | } |
| | | } |
| | | widthField = createInfoTextField(displayWidth, true); |
| | | contentPanel.add(createTextFieldSection("割草宽度 (m)", widthField)); |
| | | |
| | | // 割草安全距离(只读显示) |
| | | // 优先从Dikuai对象获取,如果Dikuai中没有,再从Device获取 |
| | |
| | | String sanitizedWidth = sanitizeWidthString(widthField.getText()); |
| | | if (sanitizedWidth != null) { |
| | | try { |
| | | double widthCm = Double.parseDouble(sanitizedWidth); |
| | | widthField.setText(formatWidthForStorage(widthCm)); |
| | | sanitizedWidth = formatWidthForStorage(widthCm); |
| | | double widthMeters = Double.parseDouble(sanitizedWidth); |
| | | widthField.setText(formatWidthForStorage(widthMeters)); |
| | | sanitizedWidth = formatWidthForStorage(widthMeters); |
| | | } catch (NumberFormatException ex) { |
| | | widthField.setText(sanitizedWidth); |
| | | } |
| | |
| | | String rawWidthInput = widthField.getText() != null ? widthField.getText().trim() : ""; |
| | | String widthSanitized = sanitizeWidthString(widthField.getText()); |
| | | if (widthSanitized == null) { |
| | | String message = rawWidthInput.isEmpty() ? "请先设置割草宽度(cm)" : "割草宽度格式不正确"; |
| | | String message = rawWidthInput.isEmpty() ? "请先设置割草宽度(m)" : "割草宽度格式不正确"; |
| | | JOptionPane.showMessageDialog(this, message, "提示", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | double widthCm; |
| | | double widthMeters; |
| | | try { |
| | | widthCm = Double.parseDouble(widthSanitized); |
| | | widthMeters = Double.parseDouble(widthSanitized); |
| | | } catch (NumberFormatException ex) { |
| | | JOptionPane.showMessageDialog(this, "割草宽度格式不正确", "提示", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | if (widthCm <= 0) { |
| | | if (widthMeters <= 0) { |
| | | JOptionPane.showMessageDialog(this, "割草宽度必须大于0", "提示", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | String widthNormalized = formatWidthForStorage(widthCm); |
| | | String widthNormalized = formatWidthForStorage(widthMeters); |
| | | widthField.setText(widthNormalized); |
| | | |
| | | String pathNormalized = normalizeCoordinateInput(pathArea.getText()); |
| | |
| | | String widthStr = sanitizeWidthString(widthCmInput); |
| | | if (widthStr == null) { |
| | | if (showMessages) { |
| | | String message = rawWidth.isEmpty() ? "请先设置割草宽度(cm)" : "割草宽度格式不正确"; |
| | | String message = rawWidth.isEmpty() ? "请先设置割草宽度(m)" : "割草宽度格式不正确"; |
| | | JOptionPane.showMessageDialog(parentComponent, message, "提示", JOptionPane.WARNING_MESSAGE); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | double widthCm; |
| | | double widthVal; |
| | | try { |
| | | widthCm = Double.parseDouble(widthStr); |
| | | widthVal = Double.parseDouble(widthStr); |
| | | } catch (NumberFormatException ex) { |
| | | if (showMessages) { |
| | | JOptionPane.showMessageDialog(parentComponent, "割草宽度格式不正确", |
| | |
| | | return null; |
| | | } |
| | | |
| | | if (widthCm <= 0) { |
| | | if (widthVal <= 0) { |
| | | if (showMessages) { |
| | | JOptionPane.showMessageDialog(parentComponent, "割草宽度必须大于0", |
| | | "提示", JOptionPane.WARNING_MESSAGE); |
| | |
| | | return null; |
| | | } |
| | | |
| | | double widthMeters = widthCm / 100.0d; |
| | | double widthMeters = widthVal; |
| | | String plannerWidth = BigDecimal.valueOf(widthMeters) |
| | | .setScale(3, RoundingMode.HALF_UP) |
| | | .stripTrailingZeros() |
| | |
| | | } else if (grassType == 2) { |
| | | // 异形地块,有障碍物 -> 调用 YixinglujingHaveObstacel |
| | | // 传入参数:boundary(A), obstacles(B), plannerWidth(C), safetyMarginStr(D) |
| | | // 注意:YixinglujingHaveObstacel.planPath 返回 String |
| | | generated = YixinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr); |
| | | List<YixinglujingHaveObstacel.PathSegment> segments = |
| | | YixinglujingHaveObstacel.planPath(boundary, obstacles, plannerWidth, safetyMarginStr); |
| | | generated = formatYixingHaveObstaclePathSegments(segments); |
| | | } else { |
| | | // 无法判断地块类型,默认按凸形处理或提示 |
| | | if (showMessages) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 格式化 YixinglujingHaveObstacel.PathSegment 列表为坐标字符串 |
| | | */ |
| | | private String formatYixingHaveObstaclePathSegments(List<YixinglujingHaveObstacel.PathSegment> segments) { |
| | | if (segments == null || segments.isEmpty()) { |
| | | return ""; |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | YixinglujingHaveObstacel.Point last = null; |
| | | for (YixinglujingHaveObstacel.PathSegment segment : segments) { |
| | | // 如果是第一段,或者当前段起点与上一段终点不连续,则添加起点 |
| | | if (last == null || !equalsYixingHaveObstaclePoint(last, segment.start)) { |
| | | appendYixingHaveObstaclePoint(sb, segment.start); |
| | | } |
| | | // 添加终点 |
| | | appendYixingHaveObstaclePoint(sb, segment.end); |
| | | last = segment.end; |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | private boolean equalsYixingHaveObstaclePoint(YixinglujingHaveObstacel.Point p1, YixinglujingHaveObstacel.Point p2) { |
| | | if (p1 == null || p2 == null) return false; |
| | | double tolerance = 1e-6; |
| | | return Math.abs(p1.x - p2.x) < tolerance && Math.abs(p1.y - p2.y) < tolerance; |
| | | } |
| | | |
| | | private void appendYixingHaveObstaclePoint(StringBuilder sb, YixinglujingHaveObstacel.Point point) { |
| | | if (sb.length() > 0) { |
| | | sb.append(";"); |
| | | } |
| | | sb.append(String.format(Locale.US, "%.6f,%.6f", point.x, point.y)); |
| | | } |
| | | |
| | | /** |
| | | * 比较两个点是否相同(使用小的容差) |
| | | */ |
| | | private boolean equals2D(AoxinglujingNoObstacle.Point p1, AoxinglujingNoObstacle.Point p2) { |
| | |
| | | return cleaned.isEmpty() ? null : cleaned; |
| | | } |
| | | |
| | | private String formatWidthForStorage(double widthCm) { |
| | | return BigDecimal.valueOf(widthCm) |
| | | .setScale(2, RoundingMode.HALF_UP) |
| | | .stripTrailingZeros() |
| | | .toPlainString(); |
| | | private String formatWidthForStorage(double widthVal) { |
| | | return String.format(Locale.US, "%.2f", widthVal); |
| | | } |
| | | |
| | | private String formatMowingPatternForDialog(String patternValue) { |