• [Model.py 03]Modification for creating terrain matrix3.


    Modification for wall and ditch matrix

    Presentation:
    在这里插入图片描述

    Modification

    Delete: Except for ditch_matrix, all varibles and code relevant with ditch are deleted.

    Modify:

    For initial_positon

                    if pos_type == 'wall':
                        '''
                            Applying: 1 to self.wall_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, 'wall')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    For apply_matrix_dots

            if attri=='wall':
                '''
                    Considering wall is initialized with two dots, finding out all the dots lying in the rectangular first, 
                    then acquiring all the dots lying in the outer line. 
                '''
                rectangle_dots=self.get_coordinates_in_range((start_x,start_y),(end_x,end_y))
                ditch=self.get_rectangle_coordinates(start_x-1,start_y-1,end_x+1,end_y+1)
                for x,y in rectangle_dots:
                    self.wall_matrix[x][y]=1
                for x,y in ditch:
                    self.ditch_matrix[x][y]=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Full code

    # 获取坐标文件
    import numpy as np
    import pandas as pd
    
    class Evaluation():
    
    
    
    
        def __init__(self):
            # 获取偏移转化参数
            x_shifting, y_shifting, x_scale, y_scale = 2850, 1500, 10, 10
            road_positions_filename = f'./road.xlsx'
            wall_positions_filename = f'./east_station.xlsx'
            river_positions_filename = f'./river3.xlsx'
            self.width,self.height=350,240
            self.ditch_matrix = np.zeros((self.width, self.height))  # initializing ditch matrix
            self.wall_matrix = np.zeros((self.width, self.height))  # initializing wall matrix
            self.initial_position(wall_positions_filename, x_shifting, y_shifting, x_scale, y_scale, 'wall')
    
            ditch_df = pd.DataFrame(self.ditch_matrix)
            ditch_df.to_csv('.\\ditch_matrix.csv', index=False, header=False)
            ditch_df = pd.DataFrame(self.wall_matrix)
            ditch_df.to_csv('.\\wall_matrix.csv', index=False, header=False)
    
    
        def initial_position(self, file_name, x_shifting, y_shifting, x_scale, y_scale, pos_type):
            """
            参数:file_name(文件路径名字), pos_type(坐标类型)
            进行坐标转换,初始化墙、内门以及出口坐标
            """
            # 读取文件
            if pos_type == 'road':
                df = pd.read_excel(file_name, header=None)
                tuple_list = []
    
                # 遍历每一行
                for index, row in df.iterrows():
                    tuple_row = []
                    col_index = 1  # 初始列索引
                    # 在每行内部,每次读取两列数据,直到读完所有列
                    while col_index < len(row):
                        data1 = row.iloc[col_index]  # 第一列的数据
                        data2 = row.iloc[col_index + 1]  # 第二列的数据
                        if pd.notna(data1) and pd.notna(data2):
                            tuple_row.append((data1, data2))
                        col_index += 2  # 更新列索引,跳过已读取的两列
                    if tuple_row:
                        tuple_list.append(tuple_row)
                for sublist in tuple_list:
                    # 遍历每一行
                    maxy = -100000
                    col_index = 0
                    while col_index + 1 < len(sublist):
                        # 获取两列数据
                        data1 = sublist[col_index]
                        data2 = sublist[col_index + 1]
                        start_x, start_y, end_x, end_y = data1[0], data1[1], data2[0], data2[1]
    
                        end_x += x_shifting
                        end_x /= x_scale
                        # end_x *= self.grid.width
                        end_x = round(end_x)
                        end_y += y_shifting
                        end_y /= y_scale
                        # end_y *= self.grid.height
                        end_y = round(end_y)
                        start_x += x_shifting
                        start_x /= x_scale
                        start_x = round(start_x)
                        start_y += y_shifting
                        start_y /= y_scale
                        start_y = round(start_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')
                        col_index += 1
            elif pos_type == "river":
                df = pd.read_excel(file_name)
                num = df['x1'].notna().sum()
                print(pos_type, "数量:", num)
                # 坐标变化
                for i in range(num):
                    start_x, start_y = df['x1'][i], df['y1'][i]
                    start_x += x_shifting
                    start_x /= x_scale
                    start_x = round(start_x)
                    # align with roads
                    start_x = start_x - 38
                    start_y += y_shifting
                    start_y /= y_scale
                    start_y = round(start_y)
                    # align with roads
                    start_y = start_y + 28
                    self.stream_pos.append((start_x, start_y))
                    if i > 0:
                        '''
                            Applyiing 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
                num2 = df['x2'].notna().sum()
                print(pos_type, "add 数量:", num2)
                # 坐标变化
                for i in range(num2):
                    start_x, start_y = df['x2'][i], df['y2'][i]
                    start_x += x_shifting
                    start_x /= x_scale
                    start_x = round(start_x)
                    # align with roads
                    start_x = start_x - 38
                    start_y += y_shifting
                    start_y /= y_scale
                    start_y = round(start_y)
                    # align with roads
                    start_y = start_y + 28
                    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
            else:
                df = pd.read_excel(file_name)
                num = len(df['x1'])
                print(pos_type, "数量:", num)
                # 坐标变化
                for i in range(num):
                    if pos_type == 'wall':
                        start_x, start_y, end_x, end_y = df['x1'][i], df['y1'][i], df['x2'][i], df['y2'][i]
                        end_x += x_shifting
                        end_x /= x_scale
                        end_x = round(end_x)
                        end_y += y_shifting
                        end_y /= y_scale
                        end_y = round(end_y)
    
                    else:
                        start_x, start_y = df['x1'][i], df['y1'][i]
    
                    start_x += x_shifting
                    start_x /= x_scale
    
                    start_x = round(start_x)
                    start_y += y_shifting
                    start_y /= y_scale
    
                    start_y = round(start_y)
    
                    if pos_type == 'wall':
                        '''
                            Applying: 1 to self.wall_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, 'wall')
                    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
                    elif pos_type == 'exit':
                        if start_x == end_x:
                            for i in range(start_y, end_y + 1):
                                self.pos_exits.append((start_x, i))
                                # 920 apply coordinates to exits_matrix
                                self.exits_matrix[start_x][i] = 1
                        elif start_y == end_y:
                            for i in range(start_x, end_x + 1):
                                self.pos_exits.append((i, start_y))
                                # 920 apply coordinates to exits_matrix
                                self.exits_matrix[i][start_y] = 1
                        else:
                            continue
                    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')
                    else:
                        pass
                        # self.water_initial_pos.append((start_x, start_y))
        def get_rectangle_coordinates(self, x1, y1, x2, y2):
            '''
            acquiring all the dots of outer line which is composed with two dots(x1,y1) (x2,y2)
            :param x1:
            :param y1:
            :param x2:
            :param y2:
            :return:
            '''
            # Ensure (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner
            x_min = min(x1, x2)
            x_max = max(x1, x2)
            y_min = min(y1, y2)
            y_max = max(y1, y2)
    
            # Calculate coordinates of the four corners
            bottom_left = (x_min, y_min)
            bottom_right = (x_max, y_min)
            top_left = (x_min, y_max)
            top_right = (x_max, y_max)
    
            # Generate points along each edge
            left_edge = [(x_min, y) for y in range(y_min, y_max + 1)]
            right_edge = [(x_max, y) for y in range(y_min, y_max + 1)]
            bottom_edge = [(x, y_min) for x in range(x_min, x_max + 1)]
            top_edge = [(x, y_max) for x in range(x_min, x_max + 1)]
    
            # Combine all the points
            all_points = [bottom_left, bottom_right, top_left, top_right]
            all_points.extend(left_edge)
            all_points.extend(right_edge)
            all_points.extend(bottom_edge)
            all_points.extend(top_edge)
    
            return list(set(all_points))
    
        def get_coordinates_in_range(self, bottom_left, top_right):
            '''
            find the rectangle between the two dots
            :param bottom_left:(x0,y0) left dot
            :param top_right:(x1,y1) right dot
            :return: a list containing all dots within the rectangle which is composed with two dots
            '''
            coordinates = []
            for x in range(bottom_left[0], top_right[0] + 1):
                for y in range(bottom_left[1], top_right[1] + 1):
                    coordinates.append((x, y))
            return coordinates
    
        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
            if attri!='wall':
                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':
                '''
                    Considering wall is initialized with two dots, finding out all the dots lying in the rectangular first, 
                    then acquiring all the dots lying in the outer line. 
                '''
                rectangle_dots=self.get_coordinates_in_range((start_x,start_y),(end_x,end_y))
                ditch=self.get_rectangle_coordinates(start_x-1,start_y-1,end_x+1,end_y+1)
                for x,y in rectangle_dots:
                    self.wall_matrix[x][y]=1
                for x,y in ditch:
                    self.ditch_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
    
        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
    
    if __name__ == '__main__':
        evauation=Evaluation()
    
    • 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
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
  • 相关阅读:
    原生Hadoop3.X高可用配置方式
    _cpp 位图
    colmap Died with <Signals.SIGKILL: 9>解决方法
    杂谈:花样滑冰智能解析系统与基于骨骼点数据的串烧
    C++ Qt开发:CheckBox多选框组件
    Linux 常用命令学习笔记
    直流有刷电机驱动基于STM32F302R8+X-NUCLEO-IHM07M1(三)
    mysql数据库增量备份方案、备份计划(InsCode AI 创作助手)
    MAC在网络结构中的位置:深入解析
    【Python脚本进阶】2.1、端口扫描器(下):NMAP端口扫描
  • 原文地址:https://blog.csdn.net/m0_51952128/article/details/133175970