Method for distributing area of any polygon to structured grid unit
By screening the minimum grid range, obtaining intersection points and calculating the coverage area, the problem of high computational cost of polygons in structured grid cells is solved, efficient area allocation is achieved, and simulation efficiency is improved.
Patent Information
- Application Number
- CN202510783300.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-12
- Publication Date
- 2025-09-23
AI Technical Summary
The existing technology has a high computational cost when allocating the area of an arbitrary polygon to structured grid cells, and it is difficult to efficiently reduce the computational complexity.
By screening out the minimum grid range surrounding the original polygon, obtaining the intersection of the polygon and the grid line, forming a new polygon, and traversing the edges of the new polygon to calculate the area of the covered grid cells, the area of the polygon in each grid cell is accumulated.
It effectively reduces the computational cost, quickly obtains the area distribution of each unit of the polygon in the structured grid, and improves the simulation efficiency.
Smart Images

Figure CN120688240A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of multi-physics field simulation, and in particular to a method for allocating the area of an arbitrary polygon to structured grid units. Background Art
[0002] In multi-physics simulation, in order to reduce the amount of calculation, it is usually necessary to simplify complex geometric structures. For example, in the thermal simulation of a chip, since the geometric shape of the simulation object is very complex and there are a large number of structures to be simulated, if meshes are established for all the structures, the number of meshes for the entire simulation object will be very large. Usually in the thermal simulation of a chip, on the one hand, the purpose of thermal simulation is to obtain the overall temperature distribution trend and the temperature of key parts, rather than pursuing the precise temperature value of each location. An overly refined geometric structure has limited improvement in the accuracy of these goals, so the geometric structure can be appropriately simplified to improve the simulation efficiency while meeting actual needs. On the other hand, there are some characteristic parameters in thermal simulation. These parameters are based on the basic principles of heat conduction and actual engineering experience. By comprehensively considering these parameters, the model can be reasonably simplified and the simulation efficiency can be improved while ensuring the simulation accuracy.
[0003] A structured grid is a regular, neat grid formed by a series of parallel or perpendicular straight lines or curves. In a two-dimensional plane, the grid cells are usually rectangular or quadrilateral. The advantages of a structured grid are that the data structure is simple, easy to store and process, the grid generation algorithm is relatively mature, and the generation efficiency is high. If a structured grid is used in a simulation, it is necessary to establish a relationship between an arbitrary shape geometry and a structured grid. The common method to establish this relationship is to calculate the volume ratio of different grid cells in the geometric space, and thereby calculate the equivalent physical parameters. Usually, this type of calculation requires the intersection of all grid cells and the polygon, and then calculate the area separately. This method is computationally expensive. Summary of the Invention
[0004] In order to address the deficiencies of the prior art, the present invention aims to provide a method for allocating the area of an arbitrary polygon to structured grid cells, which can effectively reduce the computational cost.
[0005] To achieve the above object, the present invention provides a method for allocating the area of an arbitrary polygon to structured grid cells, comprising:
[0006] Based on the distribution range of each vertex of the original polygon, a minimum grid range surrounding the original polygon is selected from the structured grid;
[0007] Obtain all intersections between the edges of the original polygon and the grid lines within the minimum grid range;
[0008] Obtaining a new polygon based on all the intersection points and the vertices of the original polygon;
[0009] Traversing the edges of the new polygon, and calculating the area of the grid cell region covered by each edge;
[0010] The area in each grid unit is accumulated and the absolute value is taken to obtain the area of the original polygon allocated to each grid unit.
[0011] Furthermore, the step of selecting the minimum grid range surrounding the original polygon from the structured grid based on the distribution range of each vertex of the original polygon further includes:
[0012] Obtaining a distribution range of the original polygon according to the vertex coordinates of the original polygon;
[0013] Obtaining a minimum grid range surrounding the original polygon according to the distribution range of the original polygon;
[0014] Create an array to store the areas of the grid cells.
[0015] Furthermore, the step of obtaining all intersections of the edges of the original polygon and the grid lines within the minimum grid range further includes:
[0016] Determine an initial edge from the original polygon, traverse the edges of the original polygon in a clockwise or counterclockwise direction starting from the initial edge, and calculate the coordinates of the intersection of each edge with the vertical grid line and the horizontal grid line; form a new polygon based on all the intersection points and the vertices of the original polygon;
[0017] The step of calculating the coordinates of the intersection of each edge with the vertical grid line and the horizontal grid line includes:
[0018] In the direction of the edge, based on the minimum and maximum values of the horizontal coordinates of the ith and i+1th vertices on the edge, determine the vertical grid line that intersects the edge, and calculate the coordinates of the intersection point between the edge and the vertical grid line;
[0019] In the direction of the edge, according to the minimum and maximum values of the vertical coordinates of the i-th vertex and the i+1-th vertex on the edge, the horizontal grid line intersecting the edge is determined, and the coordinates of the intersection point between the edge and the horizontal grid line are calculated.
[0020] Furthermore, the step of forming the new polygon based on all the intersection points and the vertices of the original polygon further includes:
[0021] Sort all the intersection points according to the direction of the edge;
[0022] Insert the vertices of the original polygon into the intersection points in order to obtain a new polygon containing all the intersection points and all the vertices of the original polygon.
[0023] Furthermore, the step of traversing the edges of the new polygon and calculating the area of the grid unit region covered by each edge further includes:
[0024] For each edge:
[0025] Subtracting the abscissa of the starting vertex from the abscissa of the ending vertex to obtain a horizontal displacement between the starting vertex and the ending vertex;
[0026] Calculate the numbers of all grid cells covered by the edge;
[0027] Based on the position of the grid line, the coordinates of the starting vertex and the ending vertex, and the horizontal displacement, the area of each grid unit covered under the edge is calculated.
[0028] Furthermore, the area of each grid cell covered by each edge is calculated according to the following formula:
[0029] When y =j ymax When the grid cell (i x ,j y ), the area that needs to be increased is:
[0030]
[0031] When y <j ymax When the grid cell (i x ,j y ), the area that needs to be increased is:
[0032]
[0033] Among them, (i x ,j y ) represents the number of the grid cell covered by the edge, ΔS(i x ,j y ) represents the area allocated to the corresponding grid cell, j ymax Indicates the maximum number of grid cells covered by the edge, is the ordinate of the starting and ending vertices on the mth edge, Δx m is the horizontal displacement between the starting and ending vertices, is the y-coordinate of the lower horizontal grid line of the largest grid cell number, is the grid unit (i x ,j y)'s y-coordinates of the two horizontal grid lines above and below it.
[0034] Furthermore, before the step of traversing the edges of the new polygon and calculating the area of the grid unit region covered by each edge, the step further includes: calculating the number of each vertex of the new polygon in the grid unit, and the calculation method is as follows:
[0035] Calculate the number of the vertex in the horizontal grid unit by the vertical coordinate of the vertex;
[0036] Calculate the vertex's number in the vertical grid cell using its horizontal coordinate.
[0037] Furthermore, the step of calculating the numbers of all grid cells covered by the edge further includes:
[0038] Based on the numbers of the starting and ending vertices on the edge in the grid cells, the numbers of all the grid cells covered by the edge are calculated as follows:
[0039] In the horizontal direction, the number of the grid unit is the minimum value between the numbers of the starting vertex and the ending vertex;
[0040] In the vertical direction, the grid cell numbers satisfy j1≤j y ≤j ymax =min(l my ,l (m+1)y ), where j y Indicates the number of the grid unit, j ymax Indicates the maximum number of horizontal grid cells covered, l my ,l (m+1)y They represent the numbers of the starting and ending vertices in the horizontal grid unit in the edge direction, and min means taking the minimum value.
[0041] To achieve the above-mentioned objectives, the present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and runnable on the processor, wherein the processor is configured to execute the computer program stored in the memory to implement the method of allocating the area of an arbitrary polygon to a structured grid as described above.
[0042] To achieve the above objectives, the present invention also provides a computer-readable storage medium, in which a computer program is stored. The computer program is loaded and executed by a processor to implement the method of allocating the area of an arbitrary polygon to a structured grid as described above.
[0043] The method for allocating the area of an arbitrary polygon to a structured grid provided by the present invention has the following advantages compared with the prior art:
[0044] By calculating the intersection points of the polygon and the grid lines, then traversing all the edges formed by all the intersection points and the vertices of the polygon and calculating the area of the grid cells covered below, the area of the polygon distributed among all the grid cells can be obtained, which effectively reduces the computational cost and can quickly obtain the area of the polygon allocated to each cell in the structured grid.
[0045] Other features and advantages of the present invention will be set forth in the description which follows, and in part will be obvious from the description, or may be learned by practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings:
[0047] Figure 1 is a flow chart of a method for allocating the area of an arbitrary polygon to a structured grid according to a first embodiment of the present invention;
[0048] Figure 2 is a flowchart of step 101 according to embodiment 1 of the present invention;
[0049] Figure 3 is a flowchart of step 102 according to embodiment 1 of the present invention;
[0050] Figure 4 is a flowchart of step 103 according to embodiment 1 of the present invention;
[0051] Figure 5 is a schematic diagram of the original polygon and the original structured grid according to the second embodiment of the present invention;
[0052] Figure 6 is a schematic diagram of intercepting the original structured grid based on the minimum grid range according to the second embodiment of the present invention;
[0053] Figure 7 is a schematic diagram of the intersection of the edge of the original polygon and the grid line according to the second embodiment of the present invention;
[0054] Figure 8 is a schematic diagram of a new polygon including vertices of the original polygon and intersections with grid lines according to a second embodiment of the present invention;
[0055] Figure 9 is the mesh range covered by the first edge of the new polygon according to the second embodiment of the present invention;
[0056] Figure 10 3. It is a schematic diagram of the area of each grid unit to which the original polygon is finally allocated according to the second embodiment of the present invention;
[0057] Figure 11 FIG. 1 is a structural diagram of an electronic device according to Embodiment 3 of the present invention. DETAILED DESCRIPTION
[0058] The preferred embodiments of the present invention are described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention, and are not used to limit the present invention.
[0059] Embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be construed as limited to the embodiments described herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments of the present invention are for illustrative purposes only and are not intended to limit the scope of protection of the present invention.
[0060] As used herein, the term "including" and its variations are open-ended, i.e., "including but not limited to." The term "based on" means "based, at least in part, on." The term "one embodiment" means "at least one embodiment," the term "another embodiment" means "at least one additional embodiment," and the term "some embodiments" means "at least some embodiments." Other terms are defined in the following description.
[0061] It should be noted that the modifications of "one" and "plurality" mentioned in the present invention are illustrative and non-restrictive. Those skilled in the art will understand that unless the context clearly indicates otherwise, they should be understood as "one or more". "Plurality" should be understood as two or more.
[0062] In an embodiment of the present invention, a method for allocating the area of an arbitrary polygon to structured grid cells is provided, comprising the following steps: based on the distribution range of each vertex of the original polygon, screening out the minimum grid range surrounding the original polygon from the structured grid; in the minimum grid range, obtaining all intersections of the edges of the original polygon and the grid lines; obtaining a new polygon based on all the intersections and the vertices of the original polygon; traversing the edges of the new polygon, calculating the area of the grid cell region covered under each edge; accumulating the area within each grid cell and taking the absolute value to obtain the area of the original polygon allocated to each grid cell.
[0063] Example 1
[0064] Figure 1 This is a flow chart of a method for allocating the area of an arbitrary polygon to a structured grid according to the first embodiment of the present invention. Figure 1 The embodiments of the present invention are described in further detail.
[0065] In the embodiment of the present invention, given a vertex set of an original polygon in a two-dimensional plane: (x i ,y i ), i=1,…,N. At the same time, the grid line sets of the two-dimensional grid in the vertical and horizontal directions are given. Here, x i ,y i represents the horizontal and vertical coordinates of the i-th vertex of the original polygon in the two-dimensional coordinate system. N is the number of vertices in the original polygon, which is also the number of sides. P and Q are the numbers of vertical and horizontal grid lines, respectively. The following is a three-step method for calculating the area of the original polygon allocated to each cell in the grid.
[0066] First, in step 101, the distribution range of each vertex of the original polygon is calculated, and the grid lines that can just surround the original polygon are screened out.
[0067] In the embodiment of the present invention, based on the distribution range of each vertex of the original polygon, the minimum grid range that can surround the original polygon is screened from the structured grid according to the distribution range.
[0068] Figure 2 is a flow chart of step 101 according to the first embodiment of the present invention. Figure 2 As shown, step 101 also includes: 11) calculating the distribution range of the original polygon; 12) calculating the grid range that can just surround the original polygon; 13) establishing an array for storing the grid unit area.
[0069] Specifically, in step 11), the distribution range of the original polygon is calculated according to the vertex coordinates of the original polygon, and the formula is as follows:
[0070] x min =min(X), x max =max(X); where X = {x i ,i=1,…,N}.
[0071] y min =min(Y),y max =max(Y); where Y = {y i ,i=1,…,N}.
[0072] In step 12), based on the distribution range of the original polygon calculated in step 11), the minimum grid range containing the original polygon is obtained.
[0073] In the vertical direction:
[0074] Find the lower bound of the grid line, the horizontal coordinate is satisfy at the same time
[0075] Find the upper bound of the grid line, the horizontal coordinate is satisfy at the same time
[0076] In the horizontal direction:
[0077] Find the lower bound of the grid line, the ordinate is satisfy at the same time
[0078] Find the upper bound of the grid line, the ordinate is satisfy at the same time
[0079] In step 13), by and Create a two-dimensional array S for storing the grid cell areas, with dimensions (i2-i1) and (j2-j1). Here, (i2-i1) is the number of grid cells in the x-direction, (j2-j1) is the number of grid cells in the y-direction, and the total number of grid cells is (i2-i1)×(j2-j1).
[0080] In a structured grid, the area enclosed by two adjacent vertical grid lines and two adjacent horizontal grid lines is a grid unit.
[0081] In step 102, each edge of the given polygon is traversed and the intersection of each edge with the grid line is calculated. The purpose of this step is to obtain a new polygon that contains all vertices of the original polygon and its intersections with the grid line.
[0082] Figure 3 is a flow chart of step 102 according to the first embodiment of the present invention. Figure 3 As shown, step 102 also includes: 21) determining an initial edge from the original polygon; 22) determining the vertical grid line intersecting the edge; 23) calculating the intersection of the edge and the vertical grid line; 24) determining the horizontal grid line intersecting the edge; 25) calculating the intersection of the edge and the horizontal grid line; 26) sorting all intersections according to the direction of the edge; 27) traversing all edges of the original polygon and inserting all vertices into the intersections in order; 28) calculating the numbers of all vertices and intersections in the grid unit.
[0083] Specifically, in step 21), an initial edge is determined from the original polygon. i ,y i ) starts, then (x i ,y i ) and (x i+1 ,y i+1) forms an edge e of the original polygon i .
[0084] In step 22), the vertical grid line that intersects with the edge of the original polygon is determined based on the minimum and maximum values of the horizontal coordinates of the ith vertex and the i+1th vertex. i ,x i+1 ) and max(x i ,x i+1 ) determines the intersecting vertical grid lines Right now satisfy
[0085]
[0086] Here, K is the number of vertical grid lines intersecting an edge, Represents the x-coordinate of the kth vertical grid line that intersects the edge.
[0087] In step 23), calculate the vertical grid lines and e i The intersection point is as follows:
[0088] c=-x i -by i
[0089]
[0090] The vertical intersection point is
[0091] In step 24), according to min(y i ,y i+1 ) and max(y i ,y i+1 ) determines the horizontal grid lines that intersect the edges Right now satisfy
[0092]
[0093] Here L is the number of horizontal grid lines that intersect the edge, Indicates the y-coordinate of the lth horizontal grid line that intersects the edge.
[0094] In step 25), calculate the horizontal grid lines and e i The intersection point is as follows:
[0095] c=-x i -by i
[0096]
[0097] The horizontal intersection point is
[0098] In step 26), the K vertical intersection points and the L horizontal intersection points are arranged according to e i Sort in the direction of:
[0099] If x i >x i+1 , then and Sort by the horizontal axis from large to small, and we get satisfy
[0100] If x i <x i+1 , then and Sort by the horizontal axis from small to large, and we get satisfy
[0101] If x i =x i+1 , then sort by the size of the vertical axis.
[0102] In step 27), all edges of the original polygon are traversed in a counterclockwise direction, and steps 22)-25) are repeated to obtain all intersections of the original polygon with the vertical and horizontal grids; and the vertices of the original polygon are inserted in a counterclockwise order, thus obtaining a new polygon with more vertices, which is described as:
[0103]
[0104] Here M is the sum of the number of vertices and all intersections of the original polygon. This new polygon is geometrically identical to the original polygon, except that it has more intersections with the grid lines.
[0105] In other exemplary embodiments, the edges of the original polygon may be traversed clockwise.
[0106] In step 28), the number of each vertex of the new polygon in the grid unit is calculated.
[0107] For the mth vertex By its y-coordinate Calculate the number of the horizontal grid cell it is located in, that is, find the number l my (representing the number of the m-th vertex in the horizontal grid unit) satisfies:
[0108]
[0109] here and are the y-coordinates of two adjacent horizontal grid lines in the original y direction, satisfying:
[0110] j1≤l my <l my +1≤j2
[0111] Here, j1 and j2 are the range indices of the desired polygon on the horizontal grid line obtained in step 12.
[0112] For the mth vertex Through its x-coordinate Calculate the number of the vertical grid cell where it is located, that is, find l mx ,satisfy:
[0113]
[0114] here and are the x-coordinates of two adjacent vertical grid lines in the original x-direction, satisfying:
[0115] i1≤l mx <l mx +1≤i2
[0116] Here, i1 and i2 are the range indices of the desired polygon on the vertical grid lines obtained in step 12.
[0117] In step 103, the area of the original polygon allocated to each grid cell in the structured grid is calculated.
[0118] In this embodiment of the present invention, for a new polygon with an additional intersection point with a grid line, each of its edges is traversed counterclockwise. The area of the grid cell covered by each edge is calculated, stored, and accumulated. After traversing all edges, the area of the original polygon allocated to each grid cell of the structured grid is obtained.
[0119] Figure 4 is a flow chart of step 103 according to the first embodiment of the present invention. Figure 4 Step 103 further includes: 31) selecting an edge of the new polygon and calculating its horizontal displacement; 32) calculating the numbers of the grid cells covered by the edge; 33) calculating the areas of the grid cells covered by the edge; 34) traversing all edges of the new polygon, accumulating the areas in all grid cells and taking the absolute values to obtain the areas of the original polygon allocated to each grid cell.
[0120] Specifically, for the mth edge of the new polygon (the line segment between two adjacent vertices or intersections or vertices and intersections on the new polygon), the starting vertex is The ending vertex is
[0121] In step 31), calculate P m and P m+1 Horizontal displacement: Subtract the abscissa of the starting vertex from the abscissa of the ending vertex
[0122]
[0123] In step 32), according to P m and P m+1 The number of the grid cell in the grid cell, calculate the number of the grid cell covered by the edge (i x ,j y ). Horizontal number i x =min(l mx ,l (m+1)x ), vertical number j y Satisfies: j1≤j y ≤j ymax =min(l my ,l (m+1)y ). Here l mx ,l (m+1)x They are P m and P m+1 Number.
[0124] In step 33), based on the numbers of the grid cells where the vertices on the edge are located, the areas of the grid cells covered by the edge are calculated according to the vertex coordinates and the horizontal displacement between the vertices. x ,j ymax ) requires special treatment and is calculated as follows:
[0125] When y =j ymax When the grid cell (i x ,j y ), the area that needs to be increased is:
[0126]
[0127] here is the y-coordinate of the lower horizontal grid line of the largest grid cell.
[0128] When y <j ymax When the grid cell (i x ,j y ), the area that needs to be increased is:
[0129]
[0130] here is the grid unit (i x ,j y )'s y-coordinates of the two horizontal grid lines above and below it.
[0131] In step 34), after traversing all edges of the new polygon, the area of each grid cell in the structured grid allocated to the original polygon is obtained by accumulating all areas to be increased in each grid cell and then taking the absolute value.
[0132] In embodiments of the present invention, by calculating the intersection points between a structured grid's grid lines and a polygon, the area of the polygon allocated to its grid cell can be quickly determined. By traversing the edges of the polygon, the number of the grid cells covered by each line segment in the structured grid can be quickly determined. This method primarily involves finding the intersection points between the polygon and the grid lines, and its complexity is O(N), where N is the number of sides of the original polygon.
[0133] Example 2
[0134] In Example 2, the method and steps in Example 1 are followed to calculate the area of a polygon projected onto each cell of the structured grid. The operation is as follows:
[0135] In the embodiment of the present invention, an original polygon is selected such as Figure 5 As shown by the red line in the middle. This is a 12-sided polygon, which means the number of sides of the original polygon is N = 12. Since the width is very small compared to the length, some sides appear to overlap. Figure 5 In the example, there are five lines. The structured grid is represented by blue lines, with 11 lines in both the x and y directions, i.e., P = 11 and Q = 11. The lines of this structured grid are evenly spaced.
[0136] First, according to step 101, the range of each vertex of the original polygon is calculated, and then the grid lines that can exactly enclose the original polygon are selected. The purpose of this step is to only consider the smallest grid range that exactly encloses the original polygon. Because the area of grid cells that do not contain the polygon is always zero, this step reduces the computational effort of subsequent area calculations.
[0137] In step 11), calculate the approximate range of the original polygon:
[0138] x min =min(X), x max = max(X), where X = {x i ,i=1,…,12};
[0139] y min=min(Y),y max = max(Y), where Y = {y i ,i=1,…,12};
[0140] Here [x min ,x max ] gives the distribution range of the original polygon in the x direction, [y min ,y max ] gives the distribution range of the original polygon in the y direction.
[0141] In step 12), calculate the range of the grid that can just contain the original polygon:
[0142] In the vertical direction, find the lower bound of the grid line and upper bound from Figure 5 As you can see, The corresponding grid line is the first one from left to right. The corresponding grid line is the 7th grid line, i.e. i1=1, i2=7.
[0143] In the horizontal direction, find the lower bound of the grid line and upper bound from Figure 5 As you can see, The corresponding grid line is the first one from bottom to top. The corresponding grid line is the 11th grid line, i.e. j1=1, j2=11.
[0144] exist Figure 6 In (a), a green box is given to mark the range of the grid lines that just surround the original polygon. Figure 6 (b) in the figure shows the truncated grid.
[0145] In step 13), by and Create a two-dimensional array S to store polygon areas, with dimensions (i2-i1) and (j2-j1). The array size used to record the grid cell area is 6×10, and the total number of grid cells is 60.
[0146] According to step 102, each side of the original polygon is traversed to calculate the intersection of each side and the grid line. This original polygon has a total of 12 sides, so the calculation needs to be done 12 times.
[0147] In step 21), an initial edge is determined. Starting from a vertex (x i ,y i ), then (x i ,y i ) and (x i+1 ,y i+1) forms an edge e i In this example, if Figure 7 As shown in the black box in (a), we start from the top and rightmost vertex and traverse counterclockwise, i=1.
[0148] In step 22), according to min(x i ,x i+1 ) and max(x i ,x i+1 ) to determine the vertical grid lines that may intersect In this example, e1 is a horizontal edge, and the vertical grid lines that may intersect are the 4th and 5th, that is, and K=2.
[0149] In step 23), the intersection of the vertical grid line obtained in step 22) and e1 is calculated:
[0150] c=-x i -by i
[0151]
[0152] The intersection is
[0153] In this embodiment, since there are two vertical lines and It will intersect with e1, so we can get two intersection points and These two intersection points are Figure 7 Indicated by arrow in (b).
[0154] In step 24), according to min(y i ,y i+1 ) and max(y i ,y i+1 ) determines the horizontal grid lines that may intersect Since e1 is horizontal and and Therefore, there is no horizontal grid line that intersects it. At this time, L = 0. Skip step 25).
[0155] In step 26), the K vertical intersection points and the L horizontal intersection points are sorted according to the direction of e1, that is:
[0156] If x i >x i+1 , then and Sort by the horizontal axis from large to small, and we get satisfy
[0157] If x i <x i+1 , then and Sort by the horizontal axis from small to large, and we get satisfy In this embodiment, e1 has two intersections with the vertical grid lines and no intersections with the horizontal grid lines, for a total of two intersections. Since the direction of e1 is from right to left, that is, x1>x2, we also sort the intersections from large to small. The intersections obtained from right to left are numbered as follows Figure 7 As shown in (b) in and here
[0158] In step 27), all edges of the original polygon are traversed in a counterclockwise direction to obtain all intersections of the original polygon with the vertical and horizontal grid lines. The vertices of the original polygon are then inserted in a counterclockwise order to obtain a description of the original polygon containing more vertices:
[0159]
[0160] This new polygon is exactly the same as the original polygon in geometric shape and size, except that its vertices have more intersections with the grid lines than the original polygon.
[0161] In this embodiment, the new polygon obtained by traversing all edges is as follows: Figure 8 As shown, you can see that the new polygon is relative to Figure 5 Many more gridline intersections are added. These intersections, plus the original 12 vertices, give the new polygon a total of 48 vertices, or M = 48. This new polygon is geometrically identical to the original, except for the 36 additional gridline intersections.
[0162] In step 28), the number of each vertex of the new polygon in the grid cell is calculated. The vertex numbering is established here so that the subsequent steps can more quickly find the number of the grid cell covered by the line segment based on the vertex number.
[0163] For the mth vertex of the new polygon By its y-coordinate Calculate the number it is in the horizontal grid, that is, find l my ,satisfy:
[0164]
[0165] here and are two adjacent horizontal grid lines in the original y direction, satisfying:
[0166] j1≤l my <l my +1≤j2
[0167] For the mth vertex of the new polygon Through its x-coordinate Calculate its position in the vertical grid, that is, find l mx ,satisfy:
[0168]
[0169] here and are two adjacent vertical grid lines in the original x direction, satisfying:
[0170] i1≤l mx <l mx +1≤i2
[0171] Take the first edge of the original polygon as an example. It contains two vertices of the original polygon and two intersection points obtained by the intersection of the edge and the vertical grid line, a total of four vertices. In counterclockwise direction (from right to left), such as Figure 7 As shown in (b):
[0172] The coordinates of the first vertex are (x1, y1) and the number in the grid cell is l 1x =5,l 1y =10;
[0173] The coordinates of the second vertex are The number in the grid cell is l 2x =5,l 1y =10;
[0174] The coordinates of the third vertex are The number in the grid cell is l 3x =4,l 3y =10;
[0175] The coordinates of the fourth vertex are (x2, y2) and the number in the grid cell is l 4x =3,l 4y =10.
[0176] According to step 103, the area of each grid cell in the structured grid to which the new polygon is allocated is calculated.
[0177] In this embodiment, we perform step 103 on the new polygon to obtain Figure 5 The new polygon has 48 more edges than the original polygon, so 48 calculations are required.
[0178] Specifically, for the mth edge of the new polygon, the starting vertex is The ending vertex is
[0179] In step 31), the horizontal coordinate of the starting vertex is subtracted from the horizontal coordinate of the ending vertex to obtain P m and P m+1 Horizontal displacement:
[0180]
[0181] Let's take the first edge of the new polygon as an example, which corresponds to Figure 7 In (b), P1=(x1,y1), The horizontal displacement between them is calculated as follows:
[0182]
[0183] Note that Δx1<0.
[0184] In step 32), the number of the grid cell covered by the first edge is calculated (i x ,i y ). The horizontal direction is numbered i x =min(l mx ,l (m+1)x ), the vertical number is a range, that is, j1≤j y ≤j ymax =min(l my ,l (m+1)y ).
[0185] In this embodiment, step 28) gives the first vertex and the second vertex as being both numbered (5, 10) in the grid unit. x =5,j y The range is [1,10], that is, j ymax =10.
[0186] In step 33), the area of each grid cell covered by the first edge is calculated. The cell with the largest number in the vertical direction (i x ,j ymax ) need special treatment. Figure 9 , the area covered by the first edge is represented in gray, and we need to distribute this area to the grid cells covered by this edge.
[0187] Wheny =j ymax When the grid cell (i x ,j y ), the area that needs to be increased is:
[0188]
[0189] In this embodiment, the area of the grid cell numbered (5,10) needs to be calculated specifically. However, in this embodiment, the first side is a horizontal line, and the height is determined by the y coordinate of this side and the position of the 10th horizontal grid line. Joint decision.
[0190] When y <j ymax When the grid cell (j x ,j y ), the area that needs to be increased is:
[0191]
[0192] In this embodiment, the above formula is used to calculate the nine grid cells numbered (5, 1) to (5, 9), and the height of each covered grid cell is the distance between the horizontal grid lines.
[0193] It should be noted that, since Δx1<0 in this embodiment, the area obtained by the grid cell is less than 0. When traversing other edges, the area obtained by this grid cell may be greater than 0.
[0194] In step 34), after traversing all edges of the new polygon, the areas of all grid cells to be increased are accumulated and then the absolute values are taken to obtain the areas of the new polygon (i.e., the original polygon) allocated to each grid cell in the structured grid.
[0195] During the traversal process, the same grid cell may receive areas assigned by different edges, both positive and negative. By summing the positive and negative areas, the accumulated value accurately gives the area assigned to the original polygon in each grid cell. Figure 10 The accumulated area of each grid cell is given. You can see that it accurately gives all the grid cells that the polygon passes through and the area allocated in each grid cell.
[0196] Example 3
[0197] In an embodiment of the present invention, an electronic device is further provided. Figure 11 FIG. 1 is a schematic diagram of the structure of an electronic device according to Embodiment 3 of the present invention. Figure 11 As shown, the electronic device of the present invention includes a processor 1101 and a memory 1102, wherein:
[0198] The memory 1102 stores a computer program. When the computer program is read and executed by the processor 1101 , the computer program executes the steps in the above-mentioned method embodiment of allocating the area of an arbitrary polygon to a structured grid.
[0199] Example 4
[0200] In an embodiment of the present invention, a computer-readable storage medium is further provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of the method embodiment of allocating the area of an arbitrary polygon to a structured grid as described above when running.
[0201] In this embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.
[0202] Those skilled in the art will understand that the foregoing descriptions are merely preferred embodiments of the present invention and are not intended to limit the present invention. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art will be able to modify the technical solutions described in the foregoing embodiments or substitute equivalents for some of the technical features. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.
Claims
1. A method for allocating the area of an arbitrary polygon to structured grid cells, characterized in that: The following steps are involved: Based on the distribution range of each vertex of the original polygon, a minimum grid range surrounding the original polygon is selected from the structured grid; Obtain all intersections between the edges of the original polygon and the grid lines within the minimum grid range; Obtaining a new polygon based on all the intersection points and the vertices of the original polygon; Traversing the edges of the new polygon, and calculating the area of the grid cell region covered by each edge; The area in each grid unit is accumulated and the absolute value is taken to obtain the area of the original polygon allocated to each grid unit.
2. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 1, characterized in that: The step of selecting the minimum grid range surrounding the original polygon from the structured grid based on the distribution range of each vertex of the original polygon further includes: Obtaining a distribution range of the original polygon according to the vertex coordinates of the original polygon; Obtaining a minimum grid range surrounding the original polygon according to the distribution range of the original polygon; Create an array to store the areas of the grid cells.
3. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 1, wherein: The step of obtaining all intersection points between the edges of the original polygon and the grid lines within the minimum grid range further includes: Determine an initial edge from the original polygon, traverse the edges of the original polygon in a clockwise or counterclockwise direction starting from the initial edge, and calculate the coordinates of the intersection of each edge with the vertical grid line and the horizontal grid line; form a new polygon based on all the intersection points and the vertices of the original polygon; The step of calculating the coordinates of the intersection of each edge with the vertical grid line and the horizontal grid line includes: In the direction of the edge, based on the minimum and maximum values of the horizontal coordinates of the ith and i+1th vertices on the edge, determine the vertical grid line that intersects the edge, and calculate the coordinates of the intersection point between the edge and the vertical grid line; In the direction of the edge, according to the minimum and maximum values of the vertical coordinates of the i-th vertex and the i+1-th vertex on the edge, the horizontal grid line intersecting the edge is determined, and the coordinates of the intersection point between the edge and the horizontal grid line are calculated.
4. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 3, wherein: The step of forming the new polygon based on all the intersection points and the vertices of the original polygon further comprises: Sort all the intersection points according to the direction of the edge; Insert the vertices of the original polygon into the intersection points in order to obtain a new polygon containing all the intersection points and all the vertices of the original polygon.
5. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 1, wherein: The step of traversing the edges of the new polygon and calculating the area of the grid unit region covered by each edge further includes: For each edge: Subtracting the abscissa of the starting vertex from the abscissa of the ending vertex to obtain a horizontal displacement between the starting vertex and the ending vertex; Calculate the numbers of all grid cells covered by the edge; Based on the position of the grid line, the coordinates of the starting vertex and the ending vertex, and the horizontal displacement, the area of each grid unit covered under the edge is calculated.
6. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 5, characterized in that: The area of each grid cell covered by each edge is calculated according to the following formula: When y =j ymax When the grid cell (i x ,j y ), the area that needs to be increased is: When y <j ymax When the grid cell (i x ,j y ), the area that needs to be increased is: Among them, (i x ,j y ) represents the number of the grid cell covered by the edge, ΔS(i x ,j y ) represents the area allocated to the corresponding grid cell, j ymax Indicates the maximum number of grid cells covered by the edge, is the ordinate of the starting and ending vertices on the mth edge, Δx m is the horizontal displacement between the starting and ending vertices, is the y-coordinate of the lower horizontal grid line of the largest grid cell number, is the grid unit (i x ,j y )'s y-coordinates of the two horizontal grid lines above and below it.
7. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 5, characterized in that: Before the step of traversing the edges of the new polygon and calculating the area of the grid unit region covered by each edge, the method further includes: calculating the number of each vertex of the new polygon in the grid unit, and the calculation method is as follows: Calculate the number of the vertex in the horizontal grid unit by the vertical coordinate of the vertex; Calculate the vertex's number in the vertical grid cell using its horizontal coordinate.
8. The method for allocating the area of an arbitrary polygon to a structured grid unit according to claim 7, characterized in that: The step of calculating the numbers of all grid cells covered by the edge further includes: Based on the numbers of the starting and ending vertices on the edge in the grid cells, the numbers of all the grid cells covered by the edge are calculated as follows: In the horizontal direction, the number of the grid unit is the minimum value between the numbers of the starting vertex and the ending vertex; In the vertical direction, the grid cell numbers satisfy j1≤j y ≤j ymax =min(l my ,l (m+1)y ), where j y Indicates the number of the grid unit, j ymax Indicates the maximum number of horizontal grid cells covered, l my ,l (m+1)y They represent the numbers of the starting and ending vertices in the horizontal grid unit in the edge direction, and min means taking the minimum value.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: The processor is configured to execute the computer program stored in the memory to implement the method of allocating the area of an arbitrary polygon to a structured grid according to any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that The storage medium stores a computer program, which is loaded and executed by a processor to implement the method of allocating the area of an arbitrary polygon to a structured grid according to any one of claims 1 to 8.