Texture packing method and system based on genetic algorithm and critical polygon method
By combining genetic algorithms and critical polygon methods, the method of optimizing texture sorting is solved, and the problem of difficult to obtain global optimal solutions in the prior art is improved, and the efficiency and space utilization of texture sorting are improved.
Patent Information
- Application Number
- CN202410991418.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-23
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2044-07-23
AI Technical Summary
The existing texture sorting methods are difficult to obtain global optimal solutions, resulting in a gap between the effect of texture sorting and the ideal effect.
The texture sorting method based on genetic algorithm and critical polygon method is adopted. By reading three-dimensional model files, extracting texture coordinates, calculating critical polygons, and using genetic algorithms to optimize the sorting position of texture data to improve space utilization.
Realize global search in the search space, approaches global optimal solution, improves overall space utilization, is suitable for complex, nonlinear, non-convex and high-dimensional sorting optimization problems, and improves texture sorting efficiency .
Smart Images

Figure CN118781275B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of three-dimensional modeling, and in particular relates to a texture packing method and system based on a genetic algorithm and a critical polygon method. Background Art
[0002] Large-scale, high-quality 3D city model data has become an important component of the basic geographic spatial framework of the city. The application of high-precision 3D modeling methods has improved the realism of the 3D scenes of smart cities, but has also led to a sharp increase in the amount of data in 3D city models. The amount of texture data in a model is generally much larger than the geometric data. Texture data occupies a large amount of storage space and resources of drawing devices. These texture data are characterized by large quantities but small amounts of individual texture data. If a single texture is simplified and compressed, the large number of textures will significantly increase the number of texture loading times and drawing batches in 3D city modeling, seriously affecting modeling efficiency.
[0003] Texture atlasing is an important field in computer graphics and 3D modeling, which aims to improve model rendering efficiency, reduce storage requirements and optimize resource utilization. Texture atlasing refers to merging multiple small textures into a large texture to reduce the number of texture switches and draw calls, thereby improving rendering efficiency. This technology originated in game development and is now widely used in various 3D modeling and rendering tasks. The original texture atlasing methods focused on simple 2D image stitching, mainly used to reduce the performance overhead caused by texture switching. With the improvement of hardware performance and application requirements, modern texture atlasing technology not only focuses on stitching, but also involves compression, optimization, dynamic modeling and other aspects to adapt to more complex application scenarios.
[0004] Texture reconstruction involves a variety of key technologies and methods, including texture nesting, texture compression, texture mapping, and real-time reconstruction. Texture nesting is mainly to efficiently arrange multiple texture images in a large texture to reduce waste and improve utilization. Texture compression is the use of compression algorithms to reduce the storage space and transmission bandwidth of texture data, such as compression formats such as DXT and ASTC. Texture mapping is to optimize texture coordinates and mapping methods to ensure that the merged texture is correctly displayed on the model surface. Real-time reconstruction is to dynamically merge and adjust textures at runtime to achieve dynamic modeling to adapt to the changing needs of scenes and resources.
[0005] Texture nesting in texture reconstruction involves the problem of cutting and packing irregular shapes. This problem is not limited to 3D modeling, but is widely used in many industries, including clothing manufacturing, metal plate cutting, furniture manufacturing, and shoemaking. Texture nesting in texture reconstruction involves the problem of cutting and packing irregular shapes. Its technical core lies in the nesting problem, that is, how to efficiently arrange irregular shapes in a limited space. In the currently available literature, texture nesting methods for 3D modeling include texture nesting improvements based on greedy algorithms, greedy nesting algorithms for small texture blocks, building texture optimization processing methods based on hypersurfaces, and adaptive texture atlas generation and remapping.
[0006] The methods used in the above literature are nothing more than using greedy algorithms to solve the local optimal solution of texture nesting, the optimal insertion algorithm of the lowest horizontal line, the simulated annealing algorithm, and the lowest horizontal line search algorithm. At present, these algorithms can achieve certain results when performing texture nesting, but if the effect of texture nesting needs to be further improved, there are basically great difficulties. This is due to certain defects in the algorithm itself. These methods basically obtain local optimal solutions when solving the nesting position, and the local optimal solution does not necessarily represent the global optimal solution. Therefore, the overall nesting effect is far from the ideal effect. Summary of the invention
[0007] In view of this, the present invention proposes a texture packing method and system based on a genetic algorithm and a critical polygon method, which are used to solve the problem that it is difficult to obtain a global optimal solution when solving the packing position.
[0008] In a first aspect, the present invention discloses a texture packing method based on a genetic algorithm and a critical polygon method, the method comprising:
[0009] Read the 3D model file and perform preprocessing to extract the texture coordinates of the texture data to be arranged;
[0010] Based on the texture coordinates, a critical polygon method is used to calculate the critical polygons of any two texture data to be arranged among all the texture data to be arranged;
[0011] Polygon docking and positioning are performed according to the critical polygon, and the corresponding space utilization is calculated. The inverse of the space utilization corresponding to the polygon docking and positioning is used as the fitness function, and a genetic algorithm is used to optimize the layout of the texture data to be arranged.
[0012] On the basis of the above technical solution, preferably, the reading of the three-dimensional model file and preprocessing to extract the texture coordinates of the texture data to be arranged specifically includes:
[0013] Read the 3D model file in OBJ format, obtain the model's vertex coordinates, original vertex texture coordinates, triangle patch information, and texture image size;
[0014] Find all the faces that share the same edge with the current triangle face, and convert the original vertex texture coordinates into actual pixel coordinates according to the corresponding original vertex texture coordinates and texture image size;
[0015] Merge all the faces that share the same edge with the current triangle to form a polygon;
[0016] Extract the vertex coordinates of the polygon from the actual pixel coordinates, convert all the vertex coordinates of the polygon into strings and write them into a CSV file;
[0017] The vertex coordinates in the CSV file are cleaned to remove invalid points and self-intersecting polygons, and the texture coordinates of the texture data to be sampled are obtained.
[0018] On the basis of the above technical solution, preferably, the step of calculating the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates by using the critical polygon method specifically includes:
[0019] According to the texture coordinates, obtain any two polygons corresponding to the texture data to be arranged, one of which is a fixed polygon A and the other is a moving polygon B;
[0020] An initial contact point is selected on the fixed polygon A, and the moving polygon B takes the initial contact point as a starting point and slides along the boundary of the fixed polygon A;
[0021] During the sliding process, the initial translation vector is determined by detecting the contact edges of the two polygons, and the initial translation vector is trimmed by intersection detection. The critical polygon is obtained by moving according to the trimmed initial translation vector.
[0022] Based on the above technical solution, preferably, determining the initial translation vector by detecting the contact edges of two polygons during the sliding process, and trimming the translation vector by intersection detection specifically includes:
[0023] Detect the contact edges of two polygons, determine the type of the paired contact edges, and determine the initial translation vector in the counterclockwise direction;
[0024] Among them, when two edges touch at a vertex, a translation vector rule set is established to determine potential translation vectors; translation vectors that do not lead to direct intersection are selected from potential translation vectors as feasible translation vectors; if there are multiple feasible translation vectors, a feasible translation vector that is closest to the previous contact edge is selected in counterclockwise order as the initial translation vector;
[0025] Project each vertex of the moving polygon B to the initial translation vector, detect the intersection of the projection result with all the edges of the fixed polygon A, trim the initial translation vector according to the position of the intersection point, and obtain the first translation vector;
[0026] The calculation formula of the first translation vector is:
[0027] L new_1 =P intersect_1 -P start
[0028] Where L new_1 is the first translation vector, P intersect_1 is the intersection point with the fixed polygon A, P start is the starting point of the translation.
[0029] Project each vertex of the fixed polygon A to the initial translation vector, detect the intersection of the projection result and all the edges of the moving polygon B, if they do not intersect, use the first translation vector as the final pruned translation vector; if they intersect, pruned the initial translation vector according to the position of the intersection point to obtain the second translation vector, and use the second translation vector as the final pruned translation vector;
[0030] The calculation formula of the second translation vector is:
[0031] L new_2 =P end -P intersect_2
[0032] Among them, L new_2 is the second translation vector, P end is the end point of the translation, P intersect_2 It is the intersection point with the moving polygon B.
[0033] Based on the above technical solution, preferably, after trimming the translation vector by intersection detection, the method further includes:
[0034] The moving polygon B performs a surround detection along the fixed polygon A, and in the process of the surround detection, each vertex of the moving polygon B is aligned with the starting vertex of the fixed polygon A;
[0035] For each vertex of the moving polygon B, if the fixed polygon A and the moving polygon B do not intersect after alignment, the vertex is recorded as the starting position of the two polygons;
[0036] If the fixed polygon A and the moving polygon B intersect after alignment, the moving polygon B slides along the contact edge of the fixed polygon A and performs traversal until a non-intersecting position is found or the end of the edge is reached; if there is still an intersection after the traversal, go to the next vertex of the moving polygon B until all the vertices of the moving polygon B are detected, and the non-intersecting position is used as the potential starting position;
[0037] From all potential starting positions, find a position that is closest to the highest point of the fixed polygon distance A and the lowest point of the moving polygon B as the starting position to generate a complete critical polygon.
[0038] On the basis of the above technical solution, preferably, the polygon docking positioning is performed according to the critical polygon, and the corresponding space utilization is calculated, and the inverse of the space utilization corresponding to the polygon docking positioning is used as the fitness function, and the genetic algorithm is used to optimize the layout of the texture data to be packed, which specifically includes:
[0039] The texture data to be arranged is texture numbered, and a chromosome encoding rule is formulated according to the texture number, the corresponding texture coordinates, and the position and rotation angle of the texture data to be arranged in a preset container;
[0040] Randomly generate the position and rotation angle of each polygon, and initialize the population according to the chromosome encoding rules;
[0041] Perform individual selection, crossover and mutation operations to update individual positions; in the mutation operation, when the random number is less than the preset mutation rate threshold, the positions and rotation angles of two polygons are randomly exchanged, and a random rotation angle is regenerated with a certain probability;
[0042] According to the individual position and the corresponding critical polygon, polygon docking positioning is performed, and the space utilization rate is calculated;
[0043] The inverse of the space utilization rate corresponding to the polygon docking positioning is used as the fitness function, the fitness value of each individual is evaluated, and the global optimal solution is obtained through iterative calculation;
[0044] The position and rotation angle of each texture data to be arranged in the container are determined according to the global optimal solution to achieve arrangement optimization.
[0045] On the basis of the above technical solution, preferably, the polygon docking positioning according to the individual position and the corresponding critical polygon and calculating the space utilization rate specifically includes:
[0046] The current individual's position is used as the initial position of the polygon to be placed;
[0047] According to the critical polygon of the polygon to be placed and the polygon already placed, the minimum circumscribed rectangle of the two polygons is calculated;
[0048] The optimal placement position is solved with the goal of minimizing the area and width of the minimum circumscribed rectangle of the two polygons;
[0049] Place the polygon to be placed at the optimal placement position;
[0050] According to the position of the polygon placed in the container, calculate the area of the placed polygon;
[0051] Calculates space utilization based on the area of the placed polygons and the area of the container.
[0052] In a second aspect, the present invention discloses a texture packing system based on a genetic algorithm and a critical polygon method, the system comprising:
[0053] Texture coordinate extraction module: used to read the 3D model file and perform preprocessing to obtain the texture data to be arranged and extract the texture coordinates;
[0054] A critical polygon generation module: used for calculating the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates by using a critical polygon method;
[0055] Texture packing optimization module: used to perform polygon docking positioning according to the critical polygon, and calculate the corresponding space utilization, taking the inverse of the space utilization corresponding to the polygon docking positioning as the fitness function, and using genetic algorithm to optimize the packing of texture data to be packed.
[0056] In a third aspect of the present invention, an electronic device is disclosed, comprising: at least one processor, at least one memory, a communication interface and a bus;
[0057] Wherein, the processor, memory, and communication interface communicate with each other via the bus;
[0058] The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the method according to the first aspect of the present invention.
[0059] According to a fourth aspect of the present invention, a computer-readable storage medium is disclosed, wherein the computer-readable storage medium stores computer instructions, and the computer instructions enable a computer to implement the method described in the first aspect of the present invention.
[0060] Compared with the prior art, the present invention has the following beneficial effects:
[0061] 1) In the layout optimization problem, the present invention generates critical polygons through the critical polygon method, which can simplify the calculation process, and realizes the layout optimization through the genetic algorithm. It can perform a global search in the entire search space to obtain an optimal solution that is close to the global one, rather than being limited to a local area, thereby improving the overall space utilization rate and being applicable to processing various complex, nonlinear, non-convex and high-dimensional layout optimization problems.
[0062] 2) During the preprocessing process, the present invention searches for all faces that share the same edge in the three-dimensional model and merges the faces to form polygons. Through data cleaning, invalid points and self-intersecting polygons are eliminated to obtain the texture coordinates of the texture data to be sampled, thereby avoiding errors in reading data and calculating critical polygons, and improving the robustness of texture data processing.
[0063] 3) In the process of generating critical polygons, the present invention determines the initial translation vector by detecting the contact edges of two polygons, and trims the translation vector through intersection detection. When the polygon structure is relatively complex, the moving polygon intersects with the fixed polygon, thereby improving the reliability of the generated critical polygon.
[0064] 4) The present invention addresses the problem that complex polygons cannot completely generate critical polygons. It allows a moving polygon to surround a fixed polygon and finds a non-intersecting starting position along the edge of the fixed polygon, which can avoid the problem of interlocking concave polygons, thereby generating a complete critical polygon.
[0065] 5) In the process of optimizing the layout by genetic algorithm, the random angle selection introduced by the present invention can rotate the polygon, which can increase the diversity of solutions and avoid falling into the local optimal solution; the mutation optimization introduced can re-select a random angle for the polygon with a certain probability, and call the mutation function in the main loop of the genetic algorithm to ensure that a certain number of individuals mutate in each generation to maintain the diversity of the population and improve the global search capability of the algorithm; after many tests, the efficiency of the present invention in layout optimization is improved by about 45%. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0067] Figure 1 It is a flow chart of the texture packing method based on genetic algorithm and critical polygon method;
[0068] Figure 2Flowchart for texture coordinate extraction;
[0069] Figure 3 A flowchart for data cleaning;
[0070] Figure 4 is a schematic diagram of a critical polygon;
[0071] Figure 5 It is the connection diagram of two polygons under the sliding track method;
[0072] Figure 6 Schematic diagram of the contact edge generated when the moving polygon B contacts the fixed polygon A;
[0073] Figure 7 It is a schematic diagram of the selection of the translation vector of the mobile polygon B when it moves around the fixed polygon A;
[0074] Figure 8 Schematic diagram of the types of paired contact edges;
[0075] Fig. 9 To determine the example of feasible translation angles;
[0076] Fig.10 To determine an example of a feasible translation vector;
[0077] Fig.11 An example of the case of a polygon with holes;
[0078] Fig.12 A schematic diagram of pruning based on the translation vector of the projection of the moving polygon B;
[0079] Fig.13 A schematic diagram of translation vector trimming based on the projection of a fixed polygon A;
[0080] Fig.14 A schematic diagram for aligning the mobile polygon B to the vertex a5 of the fixed polygon A;
[0081] Fig.15 Flowchart for solving the fitness value;
[0082] Fig.16 This is an example of the texture polygon packing results of a certain experiment;
[0083] Fig.17 This is an example of the texture polygon nesting results from another experiment. DETAILED DESCRIPTION
[0084] The following will be combined with the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0085] See also Figure 1 The present invention discloses a texture packing method based on a genetic algorithm and a critical polygon method, the method comprising:
[0086] S1. Read the 3D model file and perform preprocessing to extract the texture coordinates of the texture data to be arranged.
[0087] The three-dimensional format of the three-dimensional model file includes multiple types, such as OBJ, MTL, OSGB and other files. The embodiment of the present invention mainly targets the OBJ file in the three-dimensional model and extracts the texture coordinates by reading the three-dimensional model file in the OBJ format.
[0088] Figure 2 The flowchart of texture coordinate extraction is shown. Preprocessing the 3D model file and extracting texture coordinates specifically include:
[0089] Read the 3D model file in OBJ format, obtain the model's vertex coordinates, original vertex texture coordinates, triangle patch information, and texture image size;
[0090] Find all the faces that share the same edge with the current triangle face, and convert the original vertex texture coordinates into actual pixel coordinates according to the corresponding original vertex texture coordinates and texture image size;
[0091] Merge all the faces that share the same edge with the current triangle to form a polygon;
[0092] Extract the vertex coordinates of the polygon from the actual pixel coordinates, convert all the vertex coordinates of the polygon into strings and write them into a CSV file;
[0093] The vertex coordinates in the CSV file are cleaned to remove invalid points and self-intersecting polygons, and the texture coordinates of the texture data to be sampled are obtained.
[0094] Figure 3The data cleaning flow chart of the present invention is shown. Since there are a small part of invalid points in the extracted CSV file, if these invalid points are not eliminated, the subsequent program for calculating critical polygons will crash, so it is necessary to clean the texture coordinate data after extracting the texture coordinates. At present, the texture nesting results of a considerable part of the literature only involve large-area texture polygons. However, when designing to very small texture polygons, there will be a part of invalid points, which will directly cause the program to crash if read directly, so data cleaning is an important step in the step of texture nesting. When reading the CSV file, the data of the CSV file is read line by line, and the data of each line is parsed. When processing the data of each line, it is necessary to split the point string, parse the point data, and directly eliminate the invalid points that cannot be parsed. Convert all parsed points into the form of floating-point coordinates, and add them to the shapes list to wait for the thread pool to be initialized. After the thread is created, submit the task to the thread pool and multi-threaded batch processing of data can be performed.
[0095] Since self-intersecting polygons cannot generate critical polygons, the present invention also removes self-intersecting polygons during data cleaning to ensure the generation of closed critical polygons.
[0096] S2. Based on the texture coordinates, a critical polygon method is used to calculate critical polygons of any two texture data to be arranged among all the texture data to be arranged.
[0097] The present invention adopts the method of critical polygon (No-Fit Polygon) to generate critical polygon. The definition of critical polygon is as follows: Figure 4 As shown: Given two polygons A and B, fix polygon A and use polygon B to make a non-rotating rigid body motion around polygon A. During the motion, B maintains contact with A but does not overlap. Then the trajectory formed by a reference point on B during the motion constitutes the critical polygon of B to A.
[0098] The critical polygon method is used to calculate the area where two shapes do not overlap, and can find all possible matching positions between the two shapes. By finding all relative positions of the two shapes in different postures, the best placement can be obtained, thus providing an accurate solution.
[0099] The critical polygon method of the present invention is based on the track sliding method proposed by Mahadevan. Figure 5 The connection diagram of two polygons under the sliding track method mainly includes the following steps:
[0100] S21 . Obtain any two polygons corresponding to the texture data to be arranged according to the texture coordinates, one of which is a fixed polygon A and the other is a moving polygon B.
[0101] S22, selecting an initial contact point on the fixed polygon A, and the moving polygon B takes the initial contact point as a starting point and slides along the boundary of the fixed polygon A, maintaining contact but not penetrating or overlapping.
[0102] S23. During the sliding process, the initial translation vector is determined by detecting the contact edges of the two polygons, and the initial translation vector is trimmed by intersection detection. The polygon moves according to the trimmed initial translation vector, and the trajectory of the reference point of the moving polygon is recorded. The critical polygon is obtained according to the trajectory of the reference point.
[0103] The critical polygon method of the present invention adds the concept and identification of the starting position on the basis of the track sliding method, and can find the remaining tracks except the critical polygon in the track of the reference point, and these tracks contain some internal holes of the polygon.
[0104] Step S23 specifically includes the following sub-steps:
[0105] S231. Detect the contact edges of two polygons, determine the types of the paired contact edges, and determine an initial translation vector in a counterclockwise direction.
[0106] S231-1. Detect the contact edges of two polygons.
[0107] In the contact process of two polygons sliding, it is crucial to correctly detect the contact edge. The contact edge detection is mainly achieved by detecting each edge of the fixed polygon A and each edge of the corresponding moving polygon B. Figure 6 It is a schematic diagram of the contact edge generated when the moving polygon B contacts the fixed polygon A.
[0108] When the mobile polygon B moves along the fixed polygon A, the two polygons need to be vectorized in a counterclockwise direction to determine the direction of movement. The purpose is to facilitate the subsequent determination of the direction of movement and reduce the computing resource consumption of subsequent moving polygons and rotating polygons.
[0109] In actual engineering problems, the vector used when the mobile polygon B moves along the fixed polygon A must be derived from the edges of polygons A and B according to the specific situation. Figure 7 The figure shows the selection of the translation vector of the moving polygon B when it moves around the fixed polygon A. a1 to a6 are the edges of the fixed polygon A, and the directions of the edges are defined in counterclockwise order. b1 to b4 are the edges of the moving polygon B. Figure 7As can be seen in the example on the right, the fixed polygon B moves along one of its sides in some cases, so the side of the fixed polygon A is defined as the fixed side and the side of the moving polygon B is defined as the track side. Since the vector of the moving polygon B is counterclockwise, the translation vector must be selected in the opposite direction of this side b1. Polygon A is fixed, and polygon B must be translated, so the translation vector is opposite in this case.
[0110] By using pairs of contact edges, we can get a set of potential translation vectors. There are three possible types of pairs of contact edges. Figure 8 shown.
[0111] ① Two edges touch at the same vertex;
[0112] ②One vertex of the moving edge touches the middle of the fixed edge;
[0113] ③One vertex of the fixed edge touches the middle of the track edge.
[0114] S231-2. Determine a potential translation vector.
[0115] Each pair of contacting edges generates a potential translation vector, Figure 8 In the first case, it is necessary to correctly identify whether the potential translation vector is derived from the edge of the fixed polygon or the edge of the track polygon, so as to determine the accurate translation vector. Therefore, the present invention establishes a translation vector rule set, as shown in Table 1, that is, detecting whether the track edge b is on the left or right side of the fixed edge a, so as to correctly identify the translation vector.
[0116] Table 1. Translation vector rules for two edges touching at a vertex
[0117]
[0118] When deriving a translation vector from an edge, the vector is defined from the contact point to the end point, and this vector is the opposite track edge. The reason why some translation vectors in Table 1 are empty "—" is that the edges of the fixed polygon and the moving polygon both touch the end vertex, so an empty translation vector is derived.
[0119] exist Figure 8 In the second case, the translation vector is simply defined using the contact point of the two edges and the terminal vertex of the fixed edge.
[0120] exist Figure 8 In the third case, the translation vector uses the contact point of the two edges and the end vertex of the moved edge, and reverses the direction of the vector.
[0121] S231-3. Determine the initial translation vector.
[0122] When a potential translation vector is extracted, it may be that the fixed polygon A or the moving polygon B is a concave polygon. If the polygon B is moved directly along the potential translation vector, it will cause the two polygons to directly intersect, and the critical polygon generation condition will not be met. Therefore, after each potential translation vector is determined, they are placed at the contact position of each pair of contact edges. The translation vectors that will not cause direct intersection are selected from the potential translation vectors as feasible translation vectors; if there are multiple feasible translation vectors, select a feasible translation vector that is closest to the previous contact edge in counterclockwise order as the initial translation vector.
[0123] Based on the union of the left / right regions, a positional relationship can be defined to describe the positional relationship of the contact edges of the fixed polygon and the moving polygon. This positional relationship can determine whether the potential translation vector fits these edges. Fig. 9 are example cases of some positional relationships where potential translation vectors within the arc region of these unions are feasible, and potential translation vectors outside this region are not feasible.
[0124] Specifically, Fig.10 As shown, the fixed polygon A and the moving polygon B are in contact with each other according to Fig. 9 All potential translation vectors can be found according to the rule of the left / right area union. By finding the potential translation vectors in the union area, it can be judged that these translation vectors are feasible, and the potential translation vectors not in this area are infeasible.
[0125] Normally, only one feasible translation vector can be found for moving polygon B when it is fixed, but in special cases, there may be several translation vectors, such as Fig.11 The case of a hole polygon is shown. For clarity, the center of the polygon is selected as the reference point. When the polygon enters the hole, there are multiple feasible translation directions. Therefore, when there are several feasible translation vectors, a feasible translation vector with the closest distance to the previous contact edge is selected in counterclockwise order as the initial translation vector. This approach can significantly improve the versatility of the Mahadevan method.
[0126] S232, project each vertex of the moving polygon B to the initial translation vector, detect the intersection of the projection result with all edges of the fixed polygon A, trim the initial translation vector according to the position of the intersection point, and obtain the first translation vector.
[0127] In the case of complex polygons, there may be other edges that interfere with the translation of the track polygon. Fig.12As shown in the figure, if the translation is performed directly according to the length of the translation vector, the two polygons will intersect. In order to prevent the moving polygon from intersecting with the fixed polygon, the translation vector must be trimmed. After the trimming is completed, the translation can be performed normally according to the trimmed translation vector.
[0128] To find the correct non-intersecting translation, the present invention projects each vertex of the moving polygon B to the initial translation vector and checks its intersection with all edges of the fixed polygon A. This ensures that we correctly identify all vertices of polygon B that will intersect polygon A, resulting in a non-intersecting first translation vector. Fig.12 The figure shows a schematic diagram of translation vector trimming based on the projection of the moving polygon B. The calculation formula of the first translation vector is:
[0129] L new_1 =P intersect_1 -P start
[0130] Where L new_1 is the first translation vector, P intersect_1 is the intersection point with the fixed polygon A, P start is the starting point of the translation.
[0131] S233, project each vertex of the fixed polygon A to the initial translation vector, detect the intersection of the projection result with all edges of the moving polygon B, and determine a non-intersecting translation vector.
[0132] Corresponding to step S232, it is also necessary to project back from all vertices of the fixed polygon A and perform intersection detection with all edges of the moving polygon B to test whether there is an edge that intersects with the moving polygon B, such as Fig.13 The figure shows a schematic diagram of translation vector trimming based on the projection of fixed polygon A.
[0133] If the two do not intersect, the first translation vector is used as the final trimmed translation vector; if they intersect, the initial translation vector is trimmed according to the position of the intersection point to obtain the second translation vector, and the second translation vector is used as the final trimmed translation vector. The calculation formula of the second translation vector is:
[0134] L new_2 =P end -P intersec__2
[0135] Among them, L new_2 is the second translation vector, P end is the end point of the translation, P intersect_2 It is the intersection point with the moving polygon B.
[0136] The present invention trims the translation vector at each intersection, and after trimming, the translation vector becomes shorter. This detection process can reduce the number of intersections, and the reason for the intersection is the fast elimination detection performed using the bounding box. This method of detecting intersections using the bounding box is much faster than the standard line intersection. Compared with the Mahadevan method, the method of the present invention performs fewer detections, which improves the detection efficiency.
[0137] S234. Generate critical polygons.
[0138] The traditional track sliding method has certain limitations, namely, it is impossible to generate complete critical polygons for complex polygons involving interlocking concave polygons, puzzle pieces or hole shapes. In order to solve this problem, the present invention adopts a method based on feasible contact but non-intersecting starting positions.
[0139] The principle of the method based on feasible contact but non-intersecting starting positions is as follows: searching for a starting position along the edge of polygon A. In order to create all feasible starting positions, the moving polygon B can perform surround detection along the fixed polygon A. During the surround detection process, each vertex of the moving polygon B is aligned with the starting vertex of the fixed polygon A. For each vertex of the moving polygon B, if the fixed polygon A and the moving polygon B do not intersect after alignment, the vertex is recorded as the starting position of the two polygons. If the fixed polygon A and the moving polygon B intersect after alignment, the moving polygon B slides along the contact edge of the fixed polygon A and performs traversal until a non-intersecting position is found or the end of the edge is reached. If there is still an intersection after the traversal, go to the next vertex of the moving polygon B until all vertices of the moving polygon B are detected, and the non-intersecting position is used as a potential starting position. From all potential starting positions, a position that is closest to the highest point of the fixed polygon distance A and the lowest point of the moving polygon B is found as the starting position. At this point, the problem of concave polygon interlocking can be solved and a complete critical polygon NFP can be generated.
[0140] It should be noted that, usually, the two contact edges of the moving polygon B and the fixed polygon A are on the right side of the contact edge of polygon A or parallel to it. In this case, it is a valid alignment. If any connected edge of polygon B is on the left side of the contact edge of polygon A, it is an invalid alignment, such as Fig.14The figure shown is a schematic diagram of aligning the vertex of the moving polygon B to the edge a5 of the fixed polygon A. During the traversal, when the two contact edges of the moving polygon B are located to the right of the contact edge of the fixed polygon A and the two polygons intersect, the intersection problem can be solved by sliding along the contact edge of the fixed polygon A. By repeatedly trimming the translation vector and intersection detection, the length of the appropriate translation vector can be calculated. If the intersection problem is solved, the contact point of the moving polygon B is a potential starting position.
[0141] S3. Perform polygon docking and positioning according to the critical polygon, and calculate the corresponding space utilization rate. Take the inverse of the space utilization rate corresponding to the polygon docking and positioning as the fitness function, and use a genetic algorithm to optimize the layout of the texture data to be arranged.
[0142] The present invention is based on the idea of genetic algorithm, optimizes and improves the process of solving polygon docking positioning, and provides a general calculation framework and effective solution path for the problem of texture packing, which has many target types and huge solution scale.
[0143] S31, texture numbering the texture data to be arranged, and formulating chromosome encoding rules according to the texture number, the corresponding texture coordinates, and the position and rotation angle of the texture data to be arranged in a preset container.
[0144] Assume that there are n texture data to be arranged, and treat each texture data to be arranged as a separate individual and number the texture data to be arranged from "0 to n-1". The chromosome encoding method adopts the method of position encoding and rotation angle encoding. The position encoding indicates the specific position of each polygon in the container. The part of the chromosome position encoding is a list, in which each element represents a polygon and its translation in the container. The specific position encoding expression is as follows:
[0145] β=[l i ,shape_data_i,x i ,y i ]
[0146] Among them, l i is the sequence number of each texture data placed, shape_data_i represents the coordinate list of the polygon vertices, x i is the x-axis translation of the corresponding polygon in the container, y i It is the translation amount of the corresponding polygon on the y-axis in the container. The container of the present invention is also a polygon, which is used to store the texture data to be arranged.
[0147] The rotation angle code identifies the rotation angle of each polygon in the container. This part is also a list, and each element represents the rotation angle of a polygon.
[0148] S32. Randomly generate the position and rotation angle of each polygon, initialize the population according to the chromosome encoding rule, and design a fitness function.
[0149] The positions of each polygon are randomly generated within the spatial range of the container, and the initial rotation angle is generated by random initialization. Each row of texture data is rotated according to the initial rotation angle to reduce the operation of repeated rotation paths in subsequent algorithms. In the generated initial population, each individual represents a possible solution.
[0150] The fitness function is used to evaluate the quality of each individual. In the present invention, the fitness function usually measures the compactness and effective space utilization of the polygon arrangement, and the expression is:
[0151]
[0152] Among them, the Fit(·) function represents the spatial utilization of the polygon arrangement of the texture data after the critical polygon calculation, and Fitness represents the fitness of the texture data after the arrangement.
[0153] S33, perform individual selection, crossover and mutation operations, and update individual positions.
[0154] The design methods of the selection operator usually include roulette selection, tournament selection and random selection. The present invention adopts a weighted random selection method, including random selection of position and angle. The random selection of angle can increase the genetic algorithm's ability to search for the global optimal solution. The specific idea is: sort the individuals in the population in ascending order according to the value of fitness, and then assign a weight to each individual. The weight is inversely proportional to the fitness value. The lower the fitness value, the better the solution, and the higher the weight. Finally, the weighted random selection method is used to select individuals from the population, and individuals with low fitness values are guaranteed to have a higher probability of being selected in a probabilistic manner.
[0155] The operation of crossover is to combine the genes of the parent individuals to generate offspring. Through crossover, the excellent characteristics of the parent individuals can be passed to the offspring, thereby generating new solutions. The specific idea is: randomly select an intersection point and split the genes of the parent individuals at the intersection point. Then, the gene fragments of the parent individuals before the intersection point are exchanged to generate new offspring. Finally, the gene integrity of the offspring is ensured to avoid duplication and omission. Through inspection and completion, the present invention can ensure that each polygon has and only has one position and rotation angle in the offspring.
[0156] The mutation operation is to increase the diversity of the population by randomly changing the genes of individuals, which can avoid falling into the local optimal solution. The specific idea is: when the random number is less than the preset mutation rate threshold (for example, 1%), a mutation will be performed, and the positions and rotation angles of two polygons will be randomly exchanged with a certain probability. In addition, the mutation operator will be called in the main loop of the genetic algorithm to ensure that a certain number of individual mutations will occur in each generation to ensure the diversity of the population.
[0157] S34, polygon docking positioning is performed according to the individual positions and the corresponding critical polygons, and the space utilization rate is calculated.
[0158] The value of fitness is determined based on the space utilization, which is calculated after solving the graphic distribution. When solving the graphic distribution, based on the BL principle, the map space and the left space of the polygon are placed first to improve the space utilization. Different from the BL principle, the present invention uses NFP and Boolean operations to calculate the feasibility of the placement position, which is more complex and accurate than the BL principle. The present invention uses the position of the current individual as the initial position of the polygon to be placed; calculates the minimum circumscribed rectangle of the two polygons based on the critical polygons of the polygon to be placed and the polygon already placed; solves the optimal placement position with the minimum area and minimum width of the minimum circumscribed rectangle of the two polygons as the goal; places the polygon to be placed at the optimal placement position; calculates the area of the placed polygon based on the position of the placed polygon in the container; calculates the space utilization based on the area of the placed polygon and the area of the container.
[0159] Specifically, Fig.15 As shown, when solving the polygon distribution, the present invention initializes the objects to be placed. The objects passed in include the path of the polygon to be placed, the container polygon, the rotation angle, the critical polygon cache, etc., and initializes the placement path list. Then, preprocessing is performed, mainly to rotate the specified polygon to a specific angle according to the rotation angle and store specific position information. Then, each input polygon is placed, mainly based on the calculated NFP cache and checking its non-overlap with the placed polygons. If the check passes, the best placement position is calculated. When calculating the optimal placement position, the strategy adopted by the present invention is to find the position with the smallest placement area and the smallest width of the minimum circumscribed rectangle of the two polygons. Finally, the occupied area of the placed polygons is divided by the area of the container to obtain the space utilization.
[0160] S35, taking the inverse of the space utilization rate corresponding to the polygon docking positioning as the fitness function, evaluating the fitness value of each individual, and obtaining the global optimal solution through iterative calculation.
[0161] After evaluating the fitness value of each individual, the current optimal solution is saved. If the preset number of iterations is reached, the iteration is terminated and the optimal solution is output. Otherwise, the process returns to step S33 until the preset number of iterations is reached.
[0162] The position and rotation angle of each texture data to be arranged in the container are determined according to the global optimal solution to achieve arrangement optimization.
[0163] The present invention adopts the critical polygon method and the genetic algorithm and optimizes the algorithm principle, so that when the texture of the real scene three-dimensional model is arranged, the efficiency of the texture arrangement is greatly improved under the premise of ensuring the arrangement effect.
[0164] The method of the present invention is verified in conjunction with specific experiments below.
[0165] The experimental conditions of the present invention are: CPU: Intel i5-10500, GPU: NVIDIA GeForce GTX 1650. All the following results are completed under this experimental condition.
[0166] The texture nesting was performed on 302 texture polygons extracted from a single real-life 3D model. Because the result is random, it took about 240 seconds after multiple tests. The specific texture nesting effect is as follows: Fig.16 shown.
[0167] The texture nesting was performed on 877 texture polygons extracted from a single real-life 3D model. Because the result is random, it took about 1400 seconds after multiple tests. The specific texture nesting effect is as follows: Fig.17 shown. Fig.16 , 17 In the diagram, both the horizontal and vertical axes represent the size of the container.
[0168] Corresponding to the above method embodiment, the present invention also proposes a texture packing system based on a genetic algorithm and a critical polygon method, the system comprising:
[0169] Texture coordinate extraction module: used to read the 3D model file and perform preprocessing to obtain the texture data to be arranged and extract the texture coordinates;
[0170] A critical polygon generation module: used for calculating the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates by using a critical polygon method;
[0171] Texture packing optimization module: used to perform polygon docking positioning according to the critical polygon, and calculate the corresponding space utilization, taking the inverse of the space utilization corresponding to the polygon docking positioning as the fitness function, and using genetic algorithm to optimize the packing of texture data to be packed.
[0172] The above system embodiments and method embodiments correspond one to one, and for a brief description of the system embodiments, please refer to the method embodiments.
[0173] The present invention also discloses an electronic device, comprising: at least one processor, at least one memory, a communication interface and a bus; wherein the processor, memory, and communication interface communicate with each other through the bus; the memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the aforementioned method of the present invention.
[0174] The present invention also discloses a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, wherein the computer instructions enable the computer to implement all or part of the steps of the method described in the embodiment of the present invention. The storage medium includes: a USB flash drive, a mobile hard disk, a read-only memory ROM, a random access memory RAM, a magnetic disk or an optical disk, and other media that can store program codes.
[0175] The system embodiment described above is merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be distributed to multiple network units. A person skilled in the art may select some or all of the modules according to actual needs to achieve the purpose of the solution of this embodiment without creative effort.
[0176] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A texture packing method based on genetic algorithm and critical polygon method, characterized in that: The method comprises: Read the 3D model file and perform preprocessing to extract the texture coordinates of the texture data to be arranged; Based on the texture coordinates, a critical polygon method is used to calculate the critical polygons of any two texture data to be arranged among all the texture data to be arranged; Perform polygon docking and positioning according to the critical polygon, and calculate the corresponding space utilization rate, take the inverse of the space utilization rate corresponding to the polygon docking and positioning as the fitness function, and use the genetic algorithm to optimize the packing of the texture data to be packed; The method of calculating the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates specifically includes: According to the texture coordinates, obtain any two polygons corresponding to the texture data to be arranged, one of which is a fixed polygon A and the other is a moving polygon B; An initial contact point is selected on the fixed polygon A, and the moving polygon B takes the initial contact point as a starting point and slides along the boundary of the fixed polygon A; During the sliding process, the initial translation vector is determined by detecting the contact edges of the two polygons, and the initial translation vector is trimmed by intersection detection, and the critical polygon is obtained by moving according to the trimmed initial translation vector; In the sliding process, determining the initial translation vector by detecting the contact edges of two polygons, and trimming the translation vector by intersection detection specifically includes: Detect the contact edges of two polygons, determine the type of the paired contact edges, and determine the initial translation vector in the counterclockwise direction; Among them, when two edges touch at a vertex, a translation vector rule set is established to determine potential translation vectors; translation vectors that do not lead to direct intersection are selected from potential translation vectors as feasible translation vectors; if there are multiple feasible translation vectors, a feasible translation vector that is closest to the previous contact edge is selected in counterclockwise order as the initial translation vector; Project each vertex of the moving polygon B to the initial translation vector, detect the intersection of the projection result with all the edges of the fixed polygon A, trim the initial translation vector according to the position of the intersection point, and obtain the first translation vector; The calculation formula of the first translation vector is: L new_1 =P intersect_1 -P start Where L new_1 is the first translation vector, P intersect_1 is the intersection point with the fixed polygon A, P start is the starting point of the translation; Project each vertex of the fixed polygon A to the initial translation vector, detect the intersection of the projection result and all the edges of the moving polygon B, if they do not intersect, use the first translation vector as the final pruned translation vector; if they intersect, pruned the initial translation vector according to the position of the intersection point to obtain the second translation vector, and use the second translation vector as the final pruned translation vector; The calculation formula of the second translation vector is: L new_2 =P end -P intersect_2 Among them, L new_2 is the second translation vector, P end is the end point of the translation, P intersect_2 It is the intersection point with the moving polygon B.
2. The texture packing method based on genetic algorithm and critical polygon method according to claim 1, characterized in that: The method of reading the three-dimensional model file and performing preprocessing to extract the texture coordinates of the texture data to be arranged specifically includes: Read the 3D model file in OBJ format, obtain the model's vertex coordinates, original vertex texture coordinates, triangle patch information, and texture image size; Find all the faces that share the same edge with the current triangle face, and convert the original vertex texture coordinates into actual pixel coordinates according to the corresponding original vertex texture coordinates and texture image size; Merge all the faces that share the same edge with the current triangle to form a polygon; Extract the vertex coordinates of the polygon from the actual pixel coordinates, convert all the vertex coordinates of the polygon into strings and write them into a CSV file; The vertex coordinates in the CSV file are cleaned to remove invalid points and self-intersecting polygons, and the texture coordinates of the texture data to be sampled are obtained.
3. The texture packing method based on genetic algorithm and critical polygon method according to claim 1, characterized in that: After trimming the translation vector by intersection detection, the method further includes: The moving polygon B performs a surround detection along the fixed polygon A, and in the process of the surround detection, each vertex of the moving polygon B is aligned with the starting vertex of the fixed polygon A; For each vertex of the moving polygon B, if the fixed polygon A and the moving polygon B do not intersect after alignment, the vertex is recorded as the starting position of the two polygons; If the fixed polygon A and the moving polygon B intersect after alignment, the moving polygon B slides along the contact edge of the fixed polygon A and performs traversal until a non-intersecting position is found or the end of the edge is reached; if there is still an intersection after the traversal, go to the next vertex of the moving polygon B until all the vertices of the moving polygon B are detected, and the non-intersecting position is used as the potential starting position; From all potential starting positions, find a position that is closest to the highest point of the fixed polygon distance A and the lowest point of the moving polygon B as the starting position to generate a complete critical polygon.
4. The texture packing method based on genetic algorithm and critical polygon method according to claim 1, characterized in that: The method of performing polygon docking positioning according to the critical polygon and calculating the corresponding space utilization rate, taking the inverse of the space utilization rate corresponding to the polygon docking positioning as the fitness function, and using the genetic algorithm to perform layout optimization on the texture data to be packed specifically includes: The texture data to be arranged is texture numbered, and a chromosome encoding rule is formulated according to the texture number, the corresponding texture coordinates, and the position and rotation angle of the texture data to be arranged in a preset container; Randomly generate the position and rotation angle of each polygon, and initialize the population according to the chromosome encoding rules; Perform individual selection, crossover and mutation operations to update individual positions; in the mutation operation, when the random number is less than the preset mutation rate threshold, the positions and rotation angles of two polygons are randomly exchanged, and a random rotation angle is regenerated with a certain probability; According to the individual position and the corresponding critical polygon, polygon docking positioning is performed, and the space utilization rate is calculated; The inverse of the space utilization rate corresponding to the polygon docking positioning is used as the fitness function, the fitness value of each individual is evaluated, and the global optimal solution is obtained through iterative calculation; The position and rotation angle of each texture data to be arranged in the container are determined according to the global optimal solution to achieve arrangement optimization.
5. The texture packing method based on genetic algorithm and critical polygon method according to claim 4, characterized in that: The step of performing texture arrangement according to the individual positions and the corresponding critical polygons and calculating the space utilization rate specifically includes: The current individual's position is used as the initial position of the polygon to be placed; According to the critical polygon of the polygon to be placed and the polygon already placed, the minimum circumscribed rectangle of the two polygons is calculated; The optimal placement position is solved with the goal of minimizing the area and width of the minimum circumscribed rectangle of the two polygons; Place the polygon to be placed at the optimal placement position, and calculate the area of the placed polygon according to the position of the polygon already placed in the container; Calculates space utilization based on the area of the placed polygons and the area of the container.
6. A texture packing system based on genetic algorithm and critical polygon method, characterized in that: The system comprises: Texture coordinate extraction module: used to read the 3D model file and perform preprocessing to obtain the texture data to be arranged and extract the texture coordinates; A critical polygon generation module is used to calculate the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates by using the critical polygon method; the critical polygon generation module uses the critical polygon method to calculate the critical polygons of any two texture data to be arranged among all the texture data to be arranged based on the texture coordinates, specifically including: According to the texture coordinates, obtain any two polygons corresponding to the texture data to be arranged, one of which is a fixed polygon A and the other is a moving polygon B; An initial contact point is selected on the fixed polygon A, and the moving polygon B takes the initial contact point as a starting point and slides along the boundary of the fixed polygon A; During the sliding process, the initial translation vector is determined by detecting the contact edges of the two polygons, and the initial translation vector is trimmed by intersection detection, and the critical polygon is obtained by moving according to the trimmed initial translation vector; In the sliding process, determining the initial translation vector by detecting the contact edges of two polygons, and trimming the translation vector by intersection detection specifically includes: Detect the contact edges of two polygons, determine the type of the paired contact edges, and determine the initial translation vector in the counterclockwise direction; Among them, when two edges touch at a vertex, a translation vector rule set is established to determine potential translation vectors; translation vectors that do not lead to direct intersection are selected from potential translation vectors as feasible translation vectors; if there are multiple feasible translation vectors, a feasible translation vector that is closest to the previous contact edge is selected in counterclockwise order as the initial translation vector; Project each vertex of the moving polygon B to the initial translation vector, detect the intersection of the projection result with all the edges of the fixed polygon A, trim the initial translation vector according to the position of the intersection point, and obtain the first translation vector; The calculation formula of the first translation vector is: L new_1 =P intersect_1 -P start Where L new_1 is the first translation vector, P intersect_1 is the intersection point with the fixed polygon A, P start is the starting point of the translation; Project each vertex of the fixed polygon A to the initial translation vector, detect the intersection of the projection result and all the edges of the moving polygon B, if they do not intersect, use the first translation vector as the final pruned translation vector; if they intersect, pruned the initial translation vector according to the position of the intersection point to obtain the second translation vector, and use the second translation vector as the final pruned translation vector; The calculation formula of the second translation vector is: L new_2 =P end -P intersect_2 Among them, L new_2 is the second translation vector, P end is the end point of the translation, P intersect_2 is the intersection point with the moving polygon B; Texture packing optimization module: used to perform polygon docking positioning according to the critical polygon, and calculate the corresponding space utilization, taking the inverse of the space utilization corresponding to the polygon docking positioning as the fitness function, and using genetic algorithm to optimize the packing of texture data to be packed.
7. An electronic device, characterized in that: include: at least one processor, at least one memory, a communication interface, and a bus; Wherein, the processor, memory, and communication interface communicate with each other via the bus; The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the method according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions enable a computer to implement the method according to any one of claims 1 to 5.
