#
luxiaotao1123
2024-10-24 69869a21a127d2951f83421dbb6315f2997187e3
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
package com.zy.acs.manager.core;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.framework.common.R;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.manager.core.domain.Lane;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.Route;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.manager.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 10/24/2024
 */
@RestController
@RequestMapping("/test")
public class DispatcherTestController {
 
    @Autowired
    private CodeService codeService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
 
    @GetMapping("/dfs")
    public R dfs() {
        List<Lane> lanes = new ArrayList<>();
        List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1));
        Map<String, List<String>> adjacencyCodeMap = new HashMap<>();
        for (Code code : codeList) {
            List<Long> adjacencyNode = routeService.getAdjacencyNode(code.getId());
            adjacencyCodeMap.put(code.getData(), adjacencyNode.stream().map(node -> {
                return codeService.getById(node).getData();
            }).collect(Collectors.toList()));
        }
        List<String> codeDataList = codeList.stream().map(Code::getData).collect(Collectors.toList());
        System.out.println(codeDataList.toString());
        System.out.println(adjacencyCodeMap.toString());
 
 
        Set<String> visited = new HashSet<>();
 
        // 遍历所有节点,寻找度数不等于2的节点作为起点
        for (String codeData : codeDataList) {
            if (adjacencyCodeMap.get(codeData).size() != 2) {
                List<String> neighbors = adjacencyCodeMap.get(codeData);
                for (String neighbor : neighbors) {
                    if (adjacencyCodeMap.get(neighbor).size() == 2 && !visited.contains(neighbor)) {
                        Lane lane = new Lane(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
                        lane.getCodes().add(codeData); // 包含起点
                        dfsCalcIncludingEnd(codeData, neighbor, lane, visited, adjacencyCodeMap);
                        lanes.add(lane);
                    }
                }
            }
        }
 
        // 处理独立的度数为2的环路或未连接到度数不等于2的节点的部分
        for (String codeData : codeDataList) {
            if (adjacencyCodeMap.get(codeData).size() == 2 && !visited.contains(codeData)) {
                // 检查是否为环路的一部分
                Lane lane = new Lane(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
                dfsCalcForLoop(codeData, null, lane, visited, adjacencyCodeMap);
                lanes.add(lane);
            }
        }
 
 
        System.out.println(GsonUtils.toJson(lanes));
        return R.ok().add(lanes);
    }
 
    private void dfsCalcIncludingEnd(String start, String current, Lane lane, Set<String> visited, Map<String, List<String>> adjacencyCodeMap) {
        // 添加当前节点
        lane.getCodes().add(current);
        visited.add(current);
 
        List<String> neighbors = adjacencyCodeMap.get(current);
        if (neighbors == null || neighbors.isEmpty()) {
            return;
        }
 
        for (String neighbor : neighbors) {
            if (neighbor.equals(start)) {
                continue;
            }
 
            if (!visited.contains(neighbor)) {
                int degree = adjacencyCodeMap.get(neighbor).size();
                if (degree == 2) {
                    if (isSameDirection(current, neighbor, start)) {
                        dfsCalcIncludingEnd(current, neighbor, lane, visited, adjacencyCodeMap);
                    }
                } else {
                    // 终点或拐弯点,包含并停止
                    lane.getCodes().add(neighbor);
                    visited.add(neighbor);
                }
            }
        }
    }
    private void dfsCalcForLoop(String current, String parent, Lane lane, Set<String> visited, Map<String, List<String>> adjacencyCodeMap) {
        lane.getCodes().add(current);
        visited.add(current);
 
        List<String> neighbors = adjacencyCodeMap.get(current);
        if (neighbors == null || neighbors.isEmpty()) {
            return;
        }
 
        for (String neighbor : neighbors) {
            if (neighbor.equals(parent)) {
                continue;
            }
 
            if (!visited.contains(neighbor)) {
                int degree = adjacencyCodeMap.get(neighbor).size();
                if (degree == 2) {
                    if (isSameDirection(current, neighbor, parent)) {
                        dfsCalcForLoop(current, neighbor, lane, visited, adjacencyCodeMap);
                    }
                } else {
                    lane.getCodes().add(neighbor);
                    visited.add(neighbor);
                }
            }
        }
    }
 
    public boolean isSameDirection(String current, String neighbor, String parent) {
        if (parent == null) {
            return true;
        }
 
        Code parentCode = codeService.selectByData(parent);
        Code currentCode = codeService.selectByData(current);
        Code neighborCode = codeService.selectByData(neighbor);
 
        double direction1 = calculateDirection(parentCode, currentCode);
        double direction2 = calculateDirection(currentCode, neighborCode);
        double angleDifference = Math.abs(direction1 - direction2);
 
        // 规范化角度差
        angleDifference = Math.min(angleDifference, 2 * Math.PI - angleDifference);
 
        // 设置角度差阈值为3度
        return angleDifference < Math.toRadians(3);
    }
 
    /**
     * 计算两个点之间的方向角
     *
     * @param from 起点
     * @param to 终点
     * @return 方向角(弧度)
     */
    public double calculateDirection(Code from, Code to) {
        double deltaX = to.getX() - from.getX();
        double deltaY = to.getY() - from.getY();
        return Math.atan2(deltaY, deltaX);
    }
 
 
}