Module mbcp.mp_math.plane
本模块定义了三维空间中的平面类
class Plane3
method __init__(self, a: float, b: float, c: float, d: float)
Description: 平面方程:ax + by + cz + d = 0
Arguments:
- a (
float
): x系数- b (
float
): y系数- c (
float
): z系数- d (
float
): 常数项
Source code or View on GitHub
def __init__(self, a: float, b: float, c: float, d: float):
self.a = a
self.b = b
self.c = c
self.d = d
method approx(self, other: Plane3) -> bool
Description: 判断两个平面是否近似相等。
Arguments:
- other (
Plane3
): 另一个平面
Return: bool
: 是否近似相等
Source code or View on GitHub
def approx(self, other: 'Plane3') -> bool:
if self.a != 0:
k = other.a / self.a
return approx(other.b, self.b * k) and approx(other.c, self.c * k) and approx(other.d, self.d * k)
elif self.b != 0:
k = other.b / self.b
return approx(other.a, self.a * k) and approx(other.c, self.c * k) and approx(other.d, self.d * k)
elif self.c != 0:
k = other.c / self.c
return approx(other.a, self.a * k) and approx(other.b, self.b * k) and approx(other.d, self.d * k)
else:
return False
method cal_angle(self, other: Line3 | Plane3) -> AnyAngle
Description: 计算平面与平面之间的夹角。
TIP
平面间夹角计算公式:
其中
TIP
平面与直线夹角计算公式:
其中
Arguments:
Return: AnyAngle
: 夹角
Raises:
TypeError
不支持的类型
Source code or View on GitHub
def cal_angle(self, other: 'Line3 | Plane3') -> 'AnyAngle':
if isinstance(other, Line3):
return self.normal.cal_angle(other.direction).complementary
elif isinstance(other, Plane3):
return AnyAngle(math.acos(self.normal @ other.normal / (self.normal.length * other.normal.length)), is_radian=True)
else:
raise TypeError(f'Unsupported type: {type(other)}')
method cal_distance(self, other: Plane3 | Point3) -> float
Description: 计算平面与平面或点之间的距离。
TIP
平面和平面之间的距离计算公式: 暂未实现
- 平行 = 0
- 相交 = 0
- 不平行 =
其中, 和 分别为两个平面上的点, 为平面的法向量。
TIP
平面和点之间的距离计算公式:
其中,
Arguments:
Return: float
: 距离
Raises:
TypeError
不支持的类型
Source code or View on GitHub
def cal_distance(self, other: 'Plane3 | Point3') -> float:
if isinstance(other, Plane3):
raise NotImplementedError('Not implemented yet.')
elif isinstance(other, Point3):
return abs(self.a * other.x + self.b * other.y + self.c * other.z + self.d) / (self.a ** 2 + self.b ** 2 + self.c ** 2) ** 0.5
else:
raise TypeError(f'Unsupported type: {type(other)}')
method cal_intersection_line3(self, other: Plane3) -> Line3
Description: 计算两平面的交线。
TIP
计算两平面交线的一般步骤:
- 求两平面的法向量的叉乘得到方向向量
- 寻找直线上的一点,依次假设
, , ,并代入两平面方程求出合适的点 直线最终可用参数方程或点向式表示
或
Arguments:
- other (
Plane3
): 另一个平面
Return: Line3
: 交线
Raises:
ValueError
平面平行且无交线
Source code or View on GitHub
def cal_intersection_line3(self, other: 'Plane3') -> 'Line3':
if self.normal.is_parallel(other.normal):
raise ValueError('Planes are parallel and have no intersection.')
direction = self.normal.cross(other.normal)
x, y, z = (0, 0, 0)
if self.a != 0 and other.a != 0:
A = np.array([[self.b, self.c], [other.b, other.c]])
B = np.array([-self.d, -other.d])
y, z = np.linalg.solve(A, B)
elif self.b != 0 and other.b != 0:
A = np.array([[self.a, self.c], [other.a, other.c]])
B = np.array([-self.d, -other.d])
x, z = np.linalg.solve(A, B)
elif self.c != 0 and other.c != 0:
A = np.array([[self.a, self.b], [other.a, other.b]])
B = np.array([-self.d, -other.d])
x, y = np.linalg.solve(A, B)
return Line3(Point3(x, y, z), direction)
method cal_intersection_point3(self, other: Line3) -> Point3
Description: 计算平面与直线的交点。
TIP
计算平面与直线交点的一般步骤:
- 求直线的参数方程
- 代入平面方程,解出t
- 代入直线参数方程,求出交点
Arguments:
- other (
Line3
): 直线
Return: Point3
: 交点
Raises:
ValueError
平面与直线平行或重合
Source code or View on GitHub
def cal_intersection_point3(self, other: 'Line3') -> 'Point3':
if self.normal @ other.direction == 0:
raise ValueError('The plane and the line are parallel or coincident.')
x, y, z = other.get_parametric_equations()
t = -(self.a * other.point.x + self.b * other.point.y + self.c * other.point.z + self.d) / (self.a * other.direction.x + self.b * other.direction.y + self.c * other.direction.z)
return Point3(x(t), y(t), z(t))
method cal_parallel_plane3(self, point: Point3) -> Plane3
Description: 计算平行于该平面且过指定点的平面。
Arguments:
- point (
Point3
): 指定点
Return: Plane3
: 平面
Source code or View on GitHub
def cal_parallel_plane3(self, point: 'Point3') -> 'Plane3':
return Plane3.from_point_and_normal(point, self.normal)
method is_parallel(self, other: Plane3) -> bool
Description: 判断两个平面是否平行。
Arguments:
- other (
Plane3
): 另一个平面
Return: bool
: 是否平行
Source code or View on GitHub
def is_parallel(self, other: 'Plane3') -> bool:
return self.normal.is_parallel(other.normal)
method normal(self) -> Vector3
Description: 平面的法向量。
Return: Vector3
: 法向量
Source code or View on GitHub
@property
def normal(self) -> 'Vector3':
return Vector3(self.a, self.b, self.c)
method from_point_and_normal(cls, point: Point3, normal: Vector3) -> Plane3
Description: 工厂函数 由点和法向量构造平面(点法式构造)。
Arguments:
Return: Plane3
: 平面
Source code or View on GitHub
@classmethod
def from_point_and_normal(cls, point: 'Point3', normal: 'Vector3') -> 'Plane3':
a, b, c = (normal.x, normal.y, normal.z)
d = -a * point.x - b * point.y - c * point.z
return cls(a, b, c, d)
method from_three_points(cls, p1: Point3, p2: Point3, p3: Point3) -> Plane3
Description: 工厂函数 由三点构造平面。
Arguments:
- p1 (
Point3
): 点1- p2 (
Point3
): 点2- p3 (
Point3
): 点3
Return: 平面
Source code or View on GitHub
@classmethod
def from_three_points(cls, p1: 'Point3', p2: 'Point3', p3: 'Point3') -> 'Plane3':
v1 = p2 - p1
v2 = p3 - p1
normal = v1.cross(v2)
return cls.from_point_and_normal(p1, normal)
method from_two_lines(cls, l1: Line3, l2: Line3) -> Plane3
Description: 工厂函数 由两直线构造平面。
Arguments:
- l1 (
Line3
): 直线- l2 (
Line3
): 直线
Return: 平面
Source code or View on GitHub
@classmethod
def from_two_lines(cls, l1: 'Line3', l2: 'Line3') -> 'Plane3':
v1 = l1.direction
v2 = l2.point - l1.point
if v2 == zero_vector3:
v2 = l2.get_point(1) - l1.point
return cls.from_point_and_normal(l1.point, v1.cross(v2))
method from_point_and_line(cls, point: Point3, line: Line3) -> Plane3
Description: 工厂函数 由点和直线构造平面。
Arguments:
Return: 平面
Source code or View on GitHub
@classmethod
def from_point_and_line(cls, point: 'Point3', line: 'Line3') -> 'Plane3':
return cls.from_point_and_normal(point, line.direction)
@overload
method self & other: Line3 => Point3 | None
Source code or View on GitHub
@overload
def __and__(self, other: 'Line3') -> 'Point3 | None':
...
@overload
method self & other: Plane3 => Line3 | None
Source code or View on GitHub
@overload
def __and__(self, other: 'Plane3') -> 'Line3 | None':
...
method self & other
Description: 取两平面的交集(人话:交线)
Arguments:
Return: Line3
| Point3
| None
: 交集
Raises:
TypeError
不支持的类型
Source code or View on GitHub
def __and__(self, other):
if isinstance(other, Plane3):
if self.normal.is_parallel(other.normal):
return None
return self.cal_intersection_line3(other)
elif isinstance(other, Line3):
if self.normal @ other.direction == 0:
return None
return self.cal_intersection_point3(other)
else:
raise TypeError(f"unsupported operand type(s) for &: 'Plane3' and '{type(other)}'")
method self == other => bool
Description: 判断两个平面是否等价。
Arguments:
- other (
Plane3
): 另一个平面
Return: bool
: 是否等价
Source code or View on GitHub
def __eq__(self, other) -> bool:
return self.approx(other)
method self & other: Line3 => Point3
Source code or View on GitHub
def __rand__(self, other: 'Line3') -> 'Point3':
return self.cal_intersection_point3(other)