VTK study notes (24) vtk space geometric transformation (2)

For attitude, space transformation generally includes: translation and rotation.
Let's first sort out some vector inner and outer products that are necessary to implement the transformation, first sort out some implementations, and then find out the corresponding vtk implementation.
Generally, there is a good implementation in Eigen in C++, but it is not very clear in python.

1. Inner and outer products of vectors

1.1, numpy implementation

cross = np.cross(tx, ty)
np.cross(tangents_x[:, None, :], tangents_y[None, :, :])

1.2. Implement it according to the formula

insert image description here

def cross(a, b):
    c = [a[1]*b[2] - a[2]*b[1],
         a[2]*b[0] - a[0]*b[2],
         a[0]*b[1] - a[1]*b[0]]

    return c

Reference: dot product (inner product) and cross product (outer product, vector product)

2. NumPy cross product

2.1, cross product - mathematical explanation

Vector product, also known as outer product and cross product in mathematics, and vector product and cross product in physics, is a binary operation of vectors in vector space. Unlike the dot product, the result of the operation is a vector rather than a scalar. And the cross product of two vectors is perpendicular to the sum of these two vectors. Its applications are also very extensive, usually in physical optics and computer graphics.

Cross product is the simplest underlying logic of 3D design and game development, and computer practitioners should understand it.

The cross product in 2-dimensional space
insert image description here
looks like a scalar, but in fact the result of the cross product is a vector, oriented on the z-axis. Let's make a derivation. We can try to calculate the area of ​​the parallelogram enclosed by the two vectors. In fact, we can find the area of ​​the triangle enclosed by the vectors. We know that area is base*height, all derivations are as follows:

Assume:
insert image description here

2.2, cross product - program application

The three points P1(-1,0,1), P2(0,2,2) and P3(0,-1,2) can just enclose a triangle. What is the area of ​​this triangle?

import numpy as np

# 构建点
P1 = np.array([-1, 0, 1])
P2 = np.array([0, 2, 2])
P3 = np.array([0, -1, 2])

# A和B两个向量尾部相连
A = P3 - P1
B = P3 - P1
# 计算叉乘
A_B = np.cross(A, B)
# 计算叉乘的膜
AB_mo = np.linalg.norm(A_B)
# 计算面积
Area = AB_mo / 2
print("三角形的面积为:", Area)

The area of ​​the triangle is: 2.1213203435596424
insert image description here

import numpy as np

# 构建点
P1 = np.array([2, 0, 0])
P2 = np.array([1, 0, 2])
P3 = np.array([-1, 0, 0])
P4 = np.array([2, 5, 0])

# 构建向量,以P1为原点
A = P2 - P1
B = P4 - P1
C = P3 - P1

# 计算叉乘
A_B = np.cross(B, C)
# 计算点积
volume = np.inner(A, A_B)

print("四点围成的体积:", volume)

Volume enclosed by four points: 30

Reference: NumPy Cross Product
Reference:

Guess you like

Origin blog.csdn.net/juluwangriyue/article/details/123860024