Three-dimensional model normal vector calculation and precautions

[Copyright Statement]
This article is an original article by the blogger. Reproduction without the permission of the blogger is strictly prohibited. We will conduct infringement searches regularly.

For more algorithm summaries, please follow my blog: https://blog.csdn.net/suiyingy, or the "Lele Perception School" official account.
This article comes from the series of articles in the column "Python 3D Model Processing Basics". The column address is: https://blog.csdn.net/suiyingy/category_12462636.html.

        The previous blog post introduced in detail the volume calculation of the three-dimensional model and its precautions. During the volume calculation process, we need to pay attention to the surface orientation of the three-dimensional model. This orientation can be observed through the normal vector, and the direction of the normal vector is positive, which is the part that we can see normally by default. Of course, we can also observe by setting the camera or perspective position, which can be viewed in subsequent chapters of the blog post.

1 Surface normal vector

        The normal vector refers to the vector perpendicular to the plane, and the direction of the normal vector satisfies the right-hand rule. The following will take a triangular surface as an example. When calculating the normal vector of a three-dimensional point cloud, a plane is estimated based on nearby points, and then the normal vector is calculated for the plane. For the three-dimensional point cloud normal vector calculation method and its program, please refer to the blog post "Point Cloud Normal Vector" at the address "https://blog.csdn.net/suiyingy/article/details/124360873".

        Two non-collinear vectors can determine a plane. Three vertices in a triangular surface can form three vectors, any two of which can be used to represent the plane in which they are located. Assume that the three vertices of the triangle are A[0.0, 0.0, 1.0], B[3.0, 0.0, 1.0], and C[0.0, 4.0, 1.0]. This is a right triangle with z-height 1.0. Its three vectors are AB[3.0, 0.0, 0.0], AC[0.0, 4.0, 0.0], BC[-3.0, 4.0, 0.0]. The normal vector of two perpendicular vectors can be calculated by vector cross product. The corresponding calculation function in the python numpy library is numpy.cross. Normally, the plane normal vector is normalized, that is, the modulus length is 1. The starting point of the direct calculation result of the normal vector is the coordinate origin, that is, [0.0, 0.0, 0.0].

AB × AC -> [0.0, 0.0, 12.0] -> Normalization: [0.0, 0.0, 1.0]

AB × BC -> [0.0, 0.0, 12.0] -> Normalization: [0.0, 0.0, 1.0]

AC × BC -> [0.0, 0.0, 12.0] -> Normalization: [0.0, 0.0, 1.0]

AC × AB -> [0.0, 0.0, -12.0] -> Normalization: [0.0, 0.0, -1.0]

BC × AB -> [0.0, 0.0, -12.0] -> Normalization: [0.0, 0.0, -1.0]

BC × AC -> [0.0, 0.0, -12.0] -> Normalization: [0.0, 0.0, -1.0]

        The above vector cross product also satisfies the right-hand rule and is consistent with the plane normal vector. AB × AC, AB × BC and AC × BC indicate that the order of vertices is A->B->C->A, which is in the counterclockwise direction, and the normal vector is in the positive direction of the z-axis. AC × AB, BC × AB and BC × AC indicate that the vertex order is A->C->B->A, and the normal vector is oriented in the negative direction of the z-axis, which is clockwise. This is completely consistent with the previous introduction, that is, triangular faces are directional, and vertex order affects surface orientation.

        The detailed Python sample program download address for the above calculation results is "https://download.csdn.net/download/suiyingy/88439241 ”, or just reply to “Basics of 3D Processing” in “Lele Perception School”.

2 open3d calculate normal vector

        The normal vector of the three-dimensional point cloud can also be calculated directly using open3d. Here we mainly introduce the calculation process of the normal vector of the triangular surface and its vertices. Open3d's triangle surface normal vector calculation function is open3d.geometry.TriangleMesh.compute_triangle_normals [Python method, in open3d.geometry.TriangleMesh], and the vertex normal vector calculation function is open3d.geometry.TriangleMesh.compute_vertex_normals [Python method, in open3d.geometry. TriangleMesh].

        The main function process is as follows. The detailed Python sample program download address is "https://download.csdn.net/download/suiyingy/88439241", or reply to "3D Processing Basics" in "Lele Perception School". The normal vector of A->B->C->A triangle ([1, 2, 3]) is [0.0, 0.0, 1.0], A->C->B->A triangle The normal vector of the face ([1, 3, 2]) is [0.0, 0.0, -1.0]. The calculation results are consistent with the numpy results.

mesh.compute_triangle_normals()
print('triangle normals : ', np.array(mesh.triangle_normals))
mesh.compute_vertex_normals()
print('vertex normals: ', np.array(mesh.vertex_normals))

3 Vertex normal vector

        The calculation of the vertex normal vector of a three-dimensional model is used to describe the orientation or normal direction of the model surface at a certain point. It is very important in rendering and lighting calculations. A common method is to use the triangle patch information of the model to calculate the vertex normal vector. Specific steps are as follows:

        (1) First, traverse all the patches of the model (triangles composed of vertices).

        (2) For each patch, calculate the normal vector of the patch. You can use cross product and other methods to calculate the normal vector of the patch.

        (3) Add the calculated normal vector of the patch to the normal vector of each vertex of the patch.

        (4) After traversing all the patches, normalize the normal vector of each vertex (that is, scale its length to 1).

        Through this method, the normal vector of each vertex can be obtained for lighting calculation, shadow generation and other operations during the rendering process. It should be noted that in a three-dimensional model, the calculation of vertex normal vectors may be affected by factors such as the triangular patch topology of the model and normal vector smoothing. Sometimes, in order to obtain a smoother surface effect, other normal vector calculation methods are used, such as weighted average.

        The calculation of vertex normal vectors is very important for achieving realistic rendering and lighting effects. It can affect the appearance of the model, light reflection and shadow effects, etc. When using 3D models for rendering, game development and other fields, it is necessary to understand and correctly calculate vertex normal vectors.

        In Open3d, you can directly compute_vertex_normals() to obtain the vertex normal vector. The normal vectors of the three vertices of the triangular surface A->B->C->A are consistent with the plane, and are all [0.0, 0.0, 1.0]. There is only one plane here. It has been verified that the calculation method of vertex normal vector in open3d is weighted summation and then normalization. The weighted weight is the surface area of ​​all triangle patches related to the vertex, that is, the larger the surface area, the greater the weight. The following is the vertex normal vector result of a triangular prism. The detailed Python sample program download address is "https://download.csdn.net/download/suiyingy/88439241", or reply to "Basic of 3D Processing" in "Lele Perception School".

[[-0.72121845 -0.54091383  0.43273107]
 [ 0.72121845 -0.54091383  0.43273107]
 [ 0.          0.92847669  0.37139068]
 [-0.77790984 -0.58343238  0.23337295]
 [ 0.90144064  0.33804024  0.27043219]
 [ 0.          0.78086881  0.62469505]]

4 Normal vector visualization

4.1 open3d visualization

        Point cloud normal vector visualization can be realized directly through the visualization function of open3d, but the normal vector of the mesh model requires additional processing. It can be seen from the above settlement results that the starting points of the surface normal vector and the vertex normal vector are both the coordinate origin, so changing the starting point to the surface center or vertex can achieve better visualization effects. The key parts of the corresponding visualization program for Open3d are shown below. The detailed Python sample program download address is "https://download.csdn.net/download/suiyingy/88439241", or at "Lele Perception School" Just reply to "Basics of 3D Processing".

# 创建法向量可视化的 LineSet 对象
line_set = o3d.geometry.LineSet()
line_set.points = o3d.utility.Vector3dVector(np.concatenate([arrow_start_points, arrow_end_points], axis=0))
line_set.lines = o3d.utility.Vector2iVector(np.stack([np.arange(len(arrow_start_points)), np.arange(len(arrow_start_points)) + len(arrow_start_points)], axis=1))
line_colors = np.array([[0.0, 0.0, 1.0]] * len(arrow_start_points))
line_set.colors = o3d.utility.Vector3dVector(line_colors)

        The visualization results are as shown in the figure below. The blue part is the normal vector. The sample program provides two ways to display normal vectors, with arrows and without arrows.

Figure 1 Open3d normal vector visualization

4.2 MeshLab software

        MeshLab can directly calculate and display vertex normal vectors and triangle patch normal vectors. The calculation method is Render -> Show Normal. The result is shown below, the blue line segment is the normal vector.

Figure 2 MeshLab normal vector calculation and visualization

5 Normal vector application

        The normal vector of a three-dimensional model plays an important role and significance in computer graphics and computer vision. The following are several aspects of its application and importance:

        (1) Surface rendering: Normal vectors can be used to determine the orientation and normal direction of the model surface, thereby producing realistic lighting effects during the rendering process. Through the relationship between the direction of the normal vector and the direction of illumination, the illumination intensity of each pixel can be calculated to achieve shadow, reflection, refraction and other effects.

        (2) Surface smoothing: The vertex normal vector is very important for the surface smoothing effect of the model. By calculating the normal vectors of surrounding triangular patches, a smoother surface effect can be obtained. This is particularly important when rendering surface models, as it can eliminate the jagged edges and unnatural angle transitions of the model and improve the visual quality.

        (3) Lighting calculation: Lighting models usually use normal vectors to calculate effects such as reflection, refraction and scattering of light. Based on the incident light, surface normal vector, and material properties, the light intensity and color of each pixel can be calculated.

        (4) Occlusion detection: Normal vectors can help determine occlusions on the model surface, such as shadow generation and detection. By comparing the direction of the normal vector with the direction of the light, you can determine which areas are blocked by other objects and cannot receive direct illumination from the light source.

        (5) Object recognition and segmentation: In computer vision, normal vectors can be used to identify and segment different objects. The normal vector of each object has unique patterns and characteristics, and object recognition and segmentation can be performed by calculating the similarity of the normal vectors.

        (6) Geometric shape analysis: Normal vectors can be used to analyze the geometric shape of the three-dimensional model. For example, by calculating the rate of change of the vertex normal vector, you can determine the curvature of the model and the concave and convex nature of the surface. This is useful for tasks such as shape matching, shape comparison, and shape classification.

        In short, the normal vector of three-dimensional models has wide applications in the fields of computer graphics and computer vision. They play an important role in achieving realistic rendering effects, object recognition and segmentation, shape analysis, etc., and are one of the key information for processing three-dimensional data and graphics.

        In summary, the normal vector is a vector perpendicular to the plane and is used to describe the orientation of the surface of the three-dimensional model. The vertex normal vector can be obtained by calculating the normal vector of the triangular surface, which has a wide range of applications in computer graphics and computer vision, including tasks such as surface rendering, lighting calculation, object recognition and segmentation. Understanding and calculating normal vectors is important for achieving photorealistic rendering effects and geometry analysis.

[Copyright Statement]
This article is an original article by the blogger. Reproduction without the permission of the blogger is strictly prohibited. We will conduct infringement searches regularly.

For more algorithm summaries, please follow my blog: https://blog.csdn.net/suiyingy, or the "Lele Perception School" official account.
This article comes from the series of articles in the column "Python 3D Model Processing Basics". The column address is: https://blog.csdn.net/suiyingy/category_12462636.html.

Guess you like

Origin blog.csdn.net/suiyingy/article/details/133816356