• [Model.py 01]Modification for creating terrain matrix.


    apply_matrix_dots函数,用于根据输入的类别,生成对应的坐标矩阵。

    def apply_matrix_dots(self, start_x, start_y, end_x, end_y, attri):
        '''
        Applying Bresenham's line algorithm to find out all the points along the route and updating all the location on matrix as 1.
        Args:
            start_x:
            start_y:
            end_x:
            end_y:
            attri: Utilized for determining which matrix should be modified.
    
        Returns:
    
        '''
    
        # Get the points for the line between start and end
        points = self.bresenham_line(start_x, start_y, end_x, end_y)
        if attri=='road':
            # Update the road_matrix for each point on the line
            for x, y in points:
                self.road_matrix[x][y] = 1
        if attri=='river':
            for x, y in points:
                self.river_matrix[x][y] = 1
        if attri=='wall':
            # Question: what's the meaning of wall? It seems that wall and ditch has the same start point and end point.
            for x,y in points:
                self.wall_matrix[x][y]=1
        if attri=='indoor':
            # Question: what's the meaning of indoor? The output of inoor matrix contains nothing.
            for x,y in points:
                self.indoor_matrix[x][y]=1
        if attri=='pillar':
            # Question: what's the meaning of pillar? The output of pillar matrix contains nothing.
            for x,y in points:
                self.pillar_matrix[x][y]=1
    
    • 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

    函数用法:self.apply_matrix_dots(start_x, start_y, end_x, end_y, 'road')

    上述语句添加位置:self.roads.append({"start_x": start_x, "end_x": end_x, "start_y": start_y, "end_y": end_y})原先列表添加点位坐标语句之后。

                        self.roads.append({"start_x": start_x, "end_x": end_x, "start_y": start_y, "end_y": end_y})
                        '''
                            Applying 1 to self.road_matrix along the route 
                            from the start point(star_x,star_y) to end point (end_x,end_x) 
                        '''
                        self.apply_matrix_dots(start_x, start_y, end_x, end_y, 'road')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
            # 获得沟渠的每个坐标
            self.ditch = self.get_rectangle_coordinates(114, 149, 161, 178)
            self.ditch_inside = self.get_coordinates_in_range((110, 145), (165, 180))
            '''
                Applying 1 to self.ditch_matrix along all the coordinate in list self.ditch_inside
            '''
            for x, y in self.ditch_inside:
                self.ditch_matrix[x][y] = 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
                    self.stream_pos2.append((start_x, start_y))
                    if i>0:
                        '''
                            Applying 1 to self.river_matrix along the route 
                            from the start point(start0_x,start0_y) to end point (start_x,start_y) 
                        '''
                        self.apply_matrix_dots(start0_x,start0_y,start_x,start_y,'river')
                    start0_x,start0_y=start_x,start_y
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
                    elif pos_type == 'indoor':
                        self.indoors.append((start_x, start_y))
                        if i>0:
                            '''
                                Applying 1 to self.indoor_matrix along the route 
                                from the start point(start0_x,start0_y) to end point (start_x,start_y) 
                            '''
                            self.apply_matrix_dots(start0_x,start0_y,start_x,start_y,'indoor')
                        start0_x,start0_y=start_x,start_y
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
                    elif pos_type == "pillar":
                        pillar_positions = {"start_x": start_x, "end_x": end_x, "start_y": start_y, "end_y": end_y}
                        self.pillars.append(pillar_positions)
                        '''
                            Applying 1 to self.pillar_matrix along the route 
                            from the start point(start_x,start_y) to end point (end_x,end_y) 
                        '''
                        self.apply_matrix_dots(start_x,start_y,end_x,end_y,'pillar')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    bresenham_line函数,用于计算两点之间直线上经过的点位坐标。

        def bresenham_line(self,x0, y0, x1, y1):
            """Bresenham's Line Algorithm
            Produces a list of tuples from start and end points
            This  is used to determine the points of an n-dimensional
            raster that should be selected in order to form a close
            approximation to a straight line between two points.
            """
            points = []
            is_steep = abs(y1 - y0) > abs(x1 - x0)
            if is_steep:
                x0, y0 = y0, x0
                x1, y1 = y1, x1
            swapped = False
            if x0 > x1:
                x0, x1 = x1, x0
                y0, y1 = y1, y0
                swapped = True
            dx = x1 - x0
            dy = y1 - y0
            error = int(dx / 2.0)
            y_step = 1 if y0 < y1 else -1
            y = y0
            for x in range(x0, x1 + 1):
                coord = (y, x) if is_steep else (x, y)
                points.append(coord)
                error -= abs(dy)
                if error < 0:
                    y += y_step
                    error += dx
            if swapped:
                points.reverse()
            return points
    
    • 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

    combine_matrices 函数用于拼接不同物体的matrix

        def combine_matrices(self):
            '''
            Initializing terrain matrix.
            Returns:
                Adding different object matrix
                (self.river_matrix, self.road_matrix, self.wall_matrix, self.indoor_matrix, 
                self.exits_matrix, self.pillar_matrix and self.ditch_matrix) into self.terrain_matrix. 
                Different objects have different representation number.
                    river 1
                    road 2
                    wall 3
                    indoor 4
                    exits 5
                    pillar 6
                    ditch 7
            '''
            # Initialize the terrain_matrix with zeros
            # Assuming all matrices have the same shape
            self.terrain_matrix = np.zeros_like(self.river_matrix)
    
            # Add different object matrices into terrain_matrix
            # with their respective representation numbers
            self.terrain_matrix += self.river_matrix * 1
            self.terrain_matrix += self.road_matrix * 2
            self.terrain_matrix += self.wall_matrix * 3
            # self.terrain_matrix += self.indoor_matrix * 4
            # self.terrain_matrix += self.exits_matrix * 5
            self.terrain_matrix += self.pillar_matrix * 6
            self.terrain_matrix += self.ditch_matrix * 7
    
            # Handle overlapping (if any) by choosing the maximum value
            # assuming that in the case of overlap, the higher number takes precedence
            self.terrain_matrix = np.maximum.reduce([
                self.river_matrix * 1,
                self.road_matrix * 2,
                self.wall_matrix * 3,
                # self.indoor_matrix * 4,
                # self.exits_matrix * 5,
                self.pillar_matrix * 6,
                self.ditch_matrix * 7
            ])
    
    
    • 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
  • 相关阅读:
    Git零基础入门(Linux版)
    UnityWebGL UGUI中文不显示问题
    代理IP与网络安全:保障跨境电商和游戏的顺畅运行
    3ds Max 精模数据优化处理参考
    TCP 报文各字段解析
    微软不再允许Windows 11通过1@1.com绕过登录 但还有其他办法可以继续用
    Apache Doris 在小鹅通的应用实践
    CSS3提高: 浏览器私有前缀及C3总结
    软件接口安全设计规范及审计要点
    Django --- API接口规范 Rest Framework DRF序列化与反序列化
  • 原文地址:https://blog.csdn.net/m0_51952128/article/details/133168519