mirror of
https://github.com/snowykami/mbcp.git
synced 2026-04-27 04:45:37 +00:00
🐛 fix ε accuracy
This commit is contained in:
@@ -10,7 +10,7 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
"""
|
||||
from typing import overload
|
||||
|
||||
from .const import PI
|
||||
from .const import PI # type: ignore
|
||||
|
||||
|
||||
class AnyAngle:
|
||||
@@ -27,7 +27,7 @@ class AnyAngle:
|
||||
self.radian = value * PI / 180
|
||||
|
||||
@property
|
||||
def complementary(self) -> "AnyAngle":
|
||||
def complementary(self) -> 'AnyAngle':
|
||||
"""
|
||||
余角:两角的和为90°。
|
||||
Returns:
|
||||
@@ -36,7 +36,7 @@ class AnyAngle:
|
||||
return AnyAngle(PI / 2 - self.minimum_positive.radian, is_radian=True)
|
||||
|
||||
@property
|
||||
def supplementary(self) -> "AnyAngle":
|
||||
def supplementary(self) -> 'AnyAngle':
|
||||
"""
|
||||
补角:两角的和为180°。
|
||||
Returns:
|
||||
@@ -54,7 +54,7 @@ class AnyAngle:
|
||||
return self.radian * 180 / PI
|
||||
|
||||
@property
|
||||
def minimum_positive(self) -> "AnyAngle":
|
||||
def minimum_positive(self) -> 'AnyAngle':
|
||||
"""
|
||||
最小正角。
|
||||
Returns:
|
||||
@@ -63,7 +63,7 @@ class AnyAngle:
|
||||
return AnyAngle(self.radian % (2 * PI))
|
||||
|
||||
@property
|
||||
def maximum_negative(self) -> "AnyAngle":
|
||||
def maximum_negative(self) -> 'AnyAngle':
|
||||
"""
|
||||
最大负角。
|
||||
Returns:
|
||||
@@ -71,21 +71,21 @@ class AnyAngle:
|
||||
"""
|
||||
return AnyAngle(-self.radian % (2 * PI), is_radian=True)
|
||||
|
||||
def __add__(self, other: "AnyAngle") -> "AnyAngle":
|
||||
def __add__(self, other: 'AnyAngle') -> 'AnyAngle':
|
||||
return AnyAngle(self.radian + other.radian, is_radian=True)
|
||||
|
||||
def __sub__(self, other: "AnyAngle") -> "AnyAngle":
|
||||
def __sub__(self, other: 'AnyAngle') -> 'AnyAngle':
|
||||
return AnyAngle(self.radian - other.radian, is_radian=True)
|
||||
|
||||
def __mul__(self, other: float) -> "AnyAngle":
|
||||
def __mul__(self, other: float) -> 'AnyAngle':
|
||||
return AnyAngle(self.radian * other, is_radian=True)
|
||||
|
||||
@overload
|
||||
def __truediv__(self, other: float) -> "AnyAngle":
|
||||
def __truediv__(self, other: float) -> 'AnyAngle':
|
||||
...
|
||||
|
||||
@overload
|
||||
def __truediv__(self, other: "AnyAngle") -> float:
|
||||
def __truediv__(self, other: 'AnyAngle') -> float:
|
||||
...
|
||||
|
||||
def __truediv__(self, other):
|
||||
|
||||
@@ -8,87 +8,55 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
@File : other.py
|
||||
@Software: PyCharm
|
||||
"""
|
||||
import math
|
||||
from typing import TYPE_CHECKING, overload
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .mp_math_typing import OneSingleVarFunc, RealNumber
|
||||
from .utils import sign_format
|
||||
from .vector import Vector3
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .angle import AnyAngle
|
||||
from .plane import Plane3
|
||||
from .point import Point3
|
||||
|
||||
|
||||
class Line3:
|
||||
def __init__(self, a: float, b: float, c: float, d: float):
|
||||
def __init__(self, point: 'Point3', direction: 'Vector3'):
|
||||
"""
|
||||
三维空间中的直线。
|
||||
三维空间中的直线。由一个点和一个方向向量确定。
|
||||
Args:
|
||||
a: 直线方程的系数a
|
||||
b: 直线方程的系数b
|
||||
c: 直线方程的系数c
|
||||
d: 直线方程的常数项d
|
||||
point: 直线上的一点
|
||||
direction: 直线的方向向量
|
||||
"""
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.c = c
|
||||
self.d = d
|
||||
self.point = point
|
||||
self.direction = direction
|
||||
|
||||
def cal_angle(self, other: "Line3") -> "AnyAngle":
|
||||
def cal_angle(self, other: 'Line3') -> 'AnyAngle':
|
||||
"""
|
||||
计算直线和直线或面之间的夹角。
|
||||
计算直线和直线之间的夹角。
|
||||
Args:
|
||||
other: 另一条直线或面
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
夹角弧度
|
||||
Raises:
|
||||
TypeError: 不支持的类型
|
||||
"""
|
||||
if isinstance(other, Line3):
|
||||
return self.direction.cal_angle(other.direction)
|
||||
elif isinstance(other, Plane3):
|
||||
return self.direction.cal_angle(other.normal).complementary # 方向向量和法向量的夹角的余角
|
||||
else:
|
||||
raise TypeError(f"Unsupported type: {type(other)}")
|
||||
return self.direction.cal_angle(other.direction)
|
||||
|
||||
@property
|
||||
def direction(self) -> "Vector3":
|
||||
"""
|
||||
直线的方向向量。
|
||||
Returns:
|
||||
方向向量
|
||||
"""
|
||||
return Vector3(self.a, self.b, self.c)
|
||||
|
||||
def cal_intersection(self, line: "Line3") -> "Point3":
|
||||
def cal_intersection(self, other: 'Line3') -> 'Point3':
|
||||
"""
|
||||
计算两条直线的交点。
|
||||
Args:
|
||||
line: 另一条直线
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
交点
|
||||
"""
|
||||
|
||||
if self.is_parallel(line):
|
||||
if self.is_parallel(other):
|
||||
raise ValueError("Lines are parallel and do not intersect.")
|
||||
|
||||
if self.is_collinear(line):
|
||||
raise ValueError("Lines are collinear and do not have a single intersection point.")
|
||||
|
||||
if not self.is_coplanar(line):
|
||||
if not self.is_coplanar(other):
|
||||
raise ValueError("Lines are not coplanar and do not intersect.")
|
||||
return self.point + self.direction.cross(other.direction)
|
||||
|
||||
a1, b1, c1, d1 = self.a, self.b, self.c, self.d
|
||||
a2, b2, c2, d2 = line.a, line.b, line.c, line.d
|
||||
|
||||
t = (b1 * (c2 * d1 - c1 * d2) - b2 * (c1 * d1 - c2 * d2)) / (b1 * c2 - b2 * c1)
|
||||
|
||||
x = self.a * t + self.b * (-d1 / self.b)
|
||||
y = -self.b * t + self.a * (d1 / self.a)
|
||||
z = 0
|
||||
|
||||
return Point3(x, y, z)
|
||||
|
||||
def cal_perpendicular(self, point: "Point3") -> "Line3":
|
||||
def cal_perpendicular(self, point: 'Point3') -> 'Line3':
|
||||
"""
|
||||
计算直线经过指定点p的垂线。
|
||||
Args:
|
||||
@@ -96,64 +64,78 @@ class Line3:
|
||||
Returns:
|
||||
垂线
|
||||
"""
|
||||
a = -self.b
|
||||
b = self.a
|
||||
c = 0
|
||||
d = -(a * point.x + b * point.y + self.c * point.z)
|
||||
return Line3(a, b, c, d)
|
||||
return Line3(point, self.direction.cross(point - self.point))
|
||||
|
||||
def is_parallel(self, line: "Line3") -> bool:
|
||||
def get_point(self, t: RealNumber) -> 'Point3':
|
||||
"""
|
||||
获取直线上的点。同一条直线,但起始点和方向向量不同,则同一个t对应的点不同。
|
||||
Args:
|
||||
t: 参数t
|
||||
Returns:
|
||||
点
|
||||
"""
|
||||
return self.point + t * self.direction
|
||||
|
||||
def get_parametric_equations(self) -> tuple[OneSingleVarFunc, OneSingleVarFunc, OneSingleVarFunc]:
|
||||
"""
|
||||
获取直线的参数方程。
|
||||
Returns:
|
||||
x(t), y(t), z(t)
|
||||
"""
|
||||
return (lambda t: self.point.x + self.direction.x * t,
|
||||
lambda t: self.point.y + self.direction.y * t,
|
||||
lambda t: self.point.z + self.direction.z * t)
|
||||
|
||||
def is_parallel(self, other: 'Line3') -> bool:
|
||||
"""
|
||||
判断两条直线是否平行。
|
||||
直线平行的条件是它们的法向量成比例
|
||||
Args:
|
||||
line: 另一条直线
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
是否平行
|
||||
"""
|
||||
return self.direction.is_parallel(line.direction)
|
||||
return self.direction.is_parallel(other.direction)
|
||||
|
||||
def is_collinear(self, line: "Line3") -> bool:
|
||||
def is_collinear(self, other: 'Line3') -> bool:
|
||||
"""
|
||||
判断两条直线是否共线。
|
||||
直线共线的条件是它们的法向量成比例且常数项也成比例
|
||||
Args:
|
||||
line: 另一条直线
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
是否共线
|
||||
"""
|
||||
return self.is_parallel(line) and (self.d * line.b - self.b * line.d) / (self.a * line.b - self.b * line.a) == 0
|
||||
return self.is_parallel(other) and (self.point - other.point).is_parallel(self.direction)
|
||||
|
||||
def is_coplanar(self, line: "Line3") -> bool:
|
||||
def is_coplanar(self, other: 'Line3') -> bool:
|
||||
"""
|
||||
判断两条直线是否共面。
|
||||
两条直线共面的条件是它们的方向向量和法向量的叉乘为零向量
|
||||
Args:
|
||||
line: 另一条直线
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
是否共面
|
||||
"""
|
||||
direction1 = (-self.c, 0, self.a)
|
||||
direction2 = (line.c, -line.b, 0)
|
||||
cross_product = direction1[0] * direction2[1] - direction1[1] * direction2[0]
|
||||
return cross_product == 0
|
||||
return self.direction.cross(other.direction).is_parallel(self.direction)
|
||||
|
||||
def simplify(self):
|
||||
"""
|
||||
简化直线方程,等价相等。
|
||||
自体简化,不返回值。
|
||||
|
||||
按照可行性一次对x y z 化 0 处理,并对向量单位化
|
||||
"""
|
||||
self.direction.normalize()
|
||||
# 平行与zy平面,x始终为0
|
||||
if self.direction.x == 0:
|
||||
self.point.x = 0
|
||||
# 平行与xz平面,y始终为0
|
||||
if self.direction.y == 0:
|
||||
self.point.y = 0
|
||||
# 平行与xy平面,z始终为0
|
||||
if self.direction.z == 0:
|
||||
self.point.z = 0
|
||||
|
||||
@classmethod
|
||||
def from_point_and_direction(cls, point: "Point3", direction: "Vector3") -> "Line3":
|
||||
"""
|
||||
工厂函数 由点和方向向量构造直线(点向式构造)。
|
||||
Args:
|
||||
point: 点
|
||||
direction: 方向向量
|
||||
Returns:
|
||||
直线
|
||||
"""
|
||||
a, b, c = direction.x, direction.y, direction.z
|
||||
d = -(a * point.x + b * point.y + c * point.z)
|
||||
return cls(a, b, c, d)
|
||||
|
||||
@classmethod
|
||||
def from_two_points(cls, p1: "Point3", p2: "Point3") -> "Line3":
|
||||
def from_two_points(cls, p1: 'Point3', p2: 'Point3') -> 'Line3':
|
||||
"""
|
||||
工厂函数 由两点构造直线。
|
||||
Args:
|
||||
@@ -163,21 +145,45 @@ class Line3:
|
||||
直线
|
||||
"""
|
||||
direction = p2 - p1
|
||||
return cls.from_point_and_direction(p1, direction)
|
||||
return cls(p1, direction)
|
||||
|
||||
def __and__(self, other: 'Line3') -> 'Point3':
|
||||
"""
|
||||
计算两条直线点集合的交集。交点
|
||||
Args:
|
||||
other: 另一条直线
|
||||
Returns:
|
||||
交点
|
||||
"""
|
||||
return self.cal_intersection(other)
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
"""
|
||||
判断两条直线是否等价。
|
||||
|
||||
v1 // v2 ∧ (p1 - p2) // v1
|
||||
Args:
|
||||
other:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return self.a / other.a == self.b / other.b == self.c / other.c == self.d / other.d
|
||||
|
||||
def __repr__(self):
|
||||
return f"Line3({self.a}, {self.b}, {self.c}, {self.d})"
|
||||
return self.direction.is_parallel(other.direction) and (self.point - other.point).is_parallel(self.direction)
|
||||
|
||||
def __str__(self):
|
||||
return f"Line3({self.a}, {self.b}, {self.c}, {self.d})"
|
||||
"""
|
||||
返回点向式(x-x0)
|
||||
Returns:
|
||||
|
||||
"""
|
||||
s = "Line3: "
|
||||
if self.direction.x != 0:
|
||||
s += f"(x{sign_format(-self.point.x)})/{self.direction.x}"
|
||||
if self.direction.y != 0:
|
||||
s += f" = (y{sign_format(-self.point.y)})/{self.direction.y}"
|
||||
if self.direction.z != 0:
|
||||
s += f" = (z{sign_format(-self.point.z)})/{self.direction.z}"
|
||||
return s
|
||||
|
||||
def __repr__(self):
|
||||
return f"Line3({self.point}, {self.direction})"
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
平面模块
|
||||
"""
|
||||
import math
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, overload
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .vector import Vector3
|
||||
from .vector import Vector3, zero_vector3
|
||||
from .line import Line3
|
||||
from .point import Point3
|
||||
from .utils import sign
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .angle import AnyAngle
|
||||
@@ -30,7 +31,7 @@ class Plane3:
|
||||
self.c = c
|
||||
self.d = d
|
||||
|
||||
def cal_angle(self, other: "Line3 | Plane3") -> "AnyAngle":
|
||||
def cal_angle(self, other: 'Line3 | Plane3') -> 'AnyAngle':
|
||||
"""
|
||||
计算平面与平面之间的夹角。
|
||||
Args:
|
||||
@@ -43,11 +44,11 @@ class Plane3:
|
||||
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)
|
||||
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)}")
|
||||
|
||||
def cal_distance(self, other: "Plane3 | Point3") -> float:
|
||||
def cal_distance(self, other: 'Plane3 | Point3') -> float:
|
||||
"""
|
||||
计算平面与平面或点之间的距离。
|
||||
Args:
|
||||
@@ -64,27 +65,58 @@ class Plane3:
|
||||
else:
|
||||
raise TypeError(f"Unsupported type: {type(other)}")
|
||||
|
||||
def cal_intersection_line3(self, other: "Plane3") -> "Line3":
|
||||
def cal_intersection_line3(self, other: 'Plane3') -> 'Line3':
|
||||
"""
|
||||
计算两平面的交线。该方法有问题,待修复。
|
||||
Args:
|
||||
other: 另一个平面
|
||||
Returns:
|
||||
交线
|
||||
Raises:
|
||||
"""
|
||||
# 计算两法向量的叉积作为交线的方向向量
|
||||
s = self.normal.cross(other.normal) # 交线的方向向量
|
||||
# 联立两平面方程求交线的一点
|
||||
# 两平面方程联立得到的方程组
|
||||
# | a1x + b1y + c1z = -d1
|
||||
# | a2x + b2y + c2z = -d2
|
||||
# 用numpy解方程组
|
||||
a = np.array([[self.a, self.b, self.c], [other.a, other.b, other.c]])
|
||||
b = np.array([-self.d, -other.d])
|
||||
p = np.linalg.lstsq(a, b, rcond=None)[0]
|
||||
return Line3.from_point_and_direction(Point3(*p), s)
|
||||
if self.normal.is_parallel(other.normal):
|
||||
raise ValueError("Planes are parallel and have no intersection.")
|
||||
direction = self.normal.cross(other.normal) # 法向量叉乘得到方向向量
|
||||
# 寻找直线上的一点,依次假设x=0, y=0, z=0,找到合适的点
|
||||
x, y, z = 0, 0, 0
|
||||
# 依次判断条件假设x=0, y=0, 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)
|
||||
|
||||
def cal_parallel_plane3(self, point: "Point3") -> "Plane3":
|
||||
return Line3(Point3(x, y, z), direction)
|
||||
|
||||
def cal_intersection_point3(self, other: 'Line3') -> 'Point3':
|
||||
"""
|
||||
计算平面与直线的交点。
|
||||
Args:
|
||||
other: 不与平面平行或在平面上的直线
|
||||
Returns:
|
||||
交点
|
||||
Raises:
|
||||
ValueError: 平面与直线平行或重合
|
||||
"""
|
||||
# 若平面的法向量与直线方向向量垂直,则直线与平面平行或重合
|
||||
if self.normal @ other.direction == 0:
|
||||
raise ValueError("The plane and the line are parallel or coincident.")
|
||||
# 获取直线的参数方程
|
||||
# 代入平面方程,解出t
|
||||
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))
|
||||
|
||||
def cal_parallel_plane3(self, point: 'Point3') -> 'Plane3':
|
||||
"""
|
||||
计算平行于该平面且过指定点的平面。
|
||||
Args:
|
||||
@@ -95,7 +127,7 @@ class Plane3:
|
||||
return Plane3.from_point_and_normal(point, self.normal)
|
||||
|
||||
@property
|
||||
def normal(self) -> "Vector3":
|
||||
def normal(self) -> 'Vector3':
|
||||
"""
|
||||
平面的法向量。
|
||||
Returns:
|
||||
@@ -104,7 +136,7 @@ class Plane3:
|
||||
return Vector3(self.a, self.b, self.c)
|
||||
|
||||
@classmethod
|
||||
def from_point_and_normal(cls, point: "Point3", normal: "Vector3") -> "Plane3":
|
||||
def from_point_and_normal(cls, point: 'Point3', normal: 'Vector3') -> 'Plane3':
|
||||
"""
|
||||
工厂函数 由点和法向量构造平面(点法式构造)。
|
||||
Args:
|
||||
@@ -117,8 +149,93 @@ class Plane3:
|
||||
d = -a * point.x - b * point.y - c * point.z # d = -ax - by - cz
|
||||
return cls(a, b, c, d)
|
||||
|
||||
@classmethod
|
||||
def from_three_points(cls, p1: 'Point3', p2: 'Point3', p3: 'Point3') -> 'Plane3':
|
||||
"""
|
||||
工厂函数 由三点构造平面。
|
||||
Args:
|
||||
p1: 点1
|
||||
p2: 点2
|
||||
p3: 点3
|
||||
Returns:
|
||||
平面
|
||||
"""
|
||||
# 两个向量
|
||||
v1 = p2 - p1
|
||||
v2 = p3 - p1
|
||||
# 法向量
|
||||
normal = v1.cross(v2)
|
||||
return cls.from_point_and_normal(p1, normal)
|
||||
|
||||
@classmethod
|
||||
def from_two_lines(cls, l1: 'Line3', l2: 'Line3') -> 'Plane3':
|
||||
"""
|
||||
工厂函数 由两直线构造平面。
|
||||
Args:
|
||||
l1: 直线1
|
||||
l2: 直线2
|
||||
Returns:
|
||||
平面
|
||||
"""
|
||||
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))
|
||||
|
||||
@classmethod
|
||||
def from_point_and_line(cls, point: 'Point3', line: 'Line3') -> 'Plane3':
|
||||
"""
|
||||
工厂函数 由点和直线构造平面。
|
||||
Args:
|
||||
point: 面上一点
|
||||
line: 面上直线,不包含点
|
||||
Returns:
|
||||
平面
|
||||
"""
|
||||
return cls.from_point_and_normal(point, line.direction)
|
||||
|
||||
def __repr__(self):
|
||||
return f"Plane3({self.a}, {self.b}, {self.c}, {self.d})"
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.a}x + {self.b}y + {self.c}z + {self.d} = 0"
|
||||
s = "Plane3: "
|
||||
if self.a != 0:
|
||||
s += f"{sign(self.a, only_neg=True)}{abs(self.a)}x"
|
||||
if self.b != 0:
|
||||
s += f" {sign(self.b)} {abs(self.b)}y"
|
||||
if self.c != 0:
|
||||
s += f" {sign(self.c)} {abs(self.c)}z"
|
||||
if self.d != 0:
|
||||
s += f" {sign(self.d)} {abs(self.d)}"
|
||||
return s + " = 0"
|
||||
|
||||
@overload
|
||||
def __and__(self, other: 'Line3') -> 'Point3 | None':
|
||||
...
|
||||
|
||||
@overload
|
||||
def __and__(self, other: 'Plane3') -> 'Line3 | None':
|
||||
...
|
||||
|
||||
def __and__(self, other):
|
||||
"""
|
||||
取两平面的交集(人话:交线)
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
不平行平面的交线,平面平行返回None
|
||||
"""
|
||||
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)}'")
|
||||
|
||||
def __rand__(self, other: 'Line3') -> 'Point3':
|
||||
return self.cal_intersection_point3(other)
|
||||
|
||||
@@ -8,9 +8,10 @@ class Point3:
|
||||
def __init__(self, x: float, y: float, z: float):
|
||||
"""
|
||||
笛卡尔坐标系中的点。
|
||||
:param x:
|
||||
:param y:
|
||||
:param z:
|
||||
Args:
|
||||
x: x 坐标
|
||||
y: y 坐标
|
||||
z: z 坐标
|
||||
"""
|
||||
self.x = x
|
||||
self.y = y
|
||||
@@ -31,26 +32,30 @@ class Point3:
|
||||
"""
|
||||
P + V -> P
|
||||
P + P -> P
|
||||
:param other:
|
||||
:return:
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
"""
|
||||
return Point3(self.x + other.x, self.y + other.y, self.z + other.z)
|
||||
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
判断两个点是否相等。
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
"""
|
||||
return self.x == other.x and self.y == other.y and self.z == other.z
|
||||
|
||||
def __sub__(self, other: "Point3") -> "Vector3":
|
||||
"""
|
||||
P - P -> V
|
||||
|
||||
P - V -> P 已在 :class:`Vector3` 中实现
|
||||
:param other:
|
||||
:return:
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
|
||||
"""
|
||||
from .vector import Vector3
|
||||
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
|
||||
|
||||
def __truediv__(self, other: float) -> "Point3":
|
||||
"""
|
||||
P / n -> P
|
||||
:param other:
|
||||
:return:
|
||||
"""
|
||||
return Point3(self.x / other, self.y / other, self.z / other)
|
||||
|
||||
@@ -56,3 +56,38 @@ def approx(x: float, y: float = 0.0, epsilon: float = 0.0001) -> bool:
|
||||
是否近似相等
|
||||
"""
|
||||
return abs(x - y) < epsilon
|
||||
|
||||
|
||||
def sign(x: float, only_neg: bool = False) -> str:
|
||||
"""获取数的符号。
|
||||
Args:
|
||||
x: 数
|
||||
only_neg: 是否只返回负数的符号
|
||||
Returns:
|
||||
符号 + - ""
|
||||
"""
|
||||
if x > 0:
|
||||
return "+" if not only_neg else ""
|
||||
elif x < 0:
|
||||
return "-"
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
def sign_format(x: float, only_neg: bool = False) -> str:
|
||||
"""格式化符号数
|
||||
-1 -> -1
|
||||
1 -> +1
|
||||
0 -> ""
|
||||
Args:
|
||||
x: 数
|
||||
only_neg: 是否只返回负数的符号
|
||||
Returns:
|
||||
符号 + - ""
|
||||
"""
|
||||
if x > 0:
|
||||
return f"+{x}" if not only_neg else f"{x}"
|
||||
elif x < 0:
|
||||
return f"-{abs(x)}"
|
||||
else:
|
||||
return ""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import math
|
||||
from typing import overload, TYPE_CHECKING
|
||||
|
||||
from .mp_math_typing import RealNumber
|
||||
from .point import Point3
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -10,10 +11,11 @@ if TYPE_CHECKING:
|
||||
class Vector3:
|
||||
def __init__(self, x: float, y: float, z: float):
|
||||
"""
|
||||
笛卡尔坐标系中的向量。
|
||||
:param x:
|
||||
:param y:
|
||||
:param z:
|
||||
3维向量
|
||||
Args:
|
||||
x: x轴分量
|
||||
y: y轴分量
|
||||
z: z轴分量
|
||||
"""
|
||||
self.x = x
|
||||
self.y = y
|
||||
@@ -27,7 +29,7 @@ class Vector3:
|
||||
Returns:
|
||||
夹角
|
||||
"""
|
||||
return AnyAngle(math.acos(self * other / (self.length * other.length)), is_radian=True)
|
||||
return AnyAngle(math.acos(self @ other / (self.length * other.length)), is_radian=True)
|
||||
|
||||
def is_parallel(self, other: 'Vector3') -> bool:
|
||||
"""
|
||||
@@ -37,20 +39,39 @@ class Vector3:
|
||||
Returns:
|
||||
是否平行
|
||||
"""
|
||||
return self @ other == Vector3(0, 0, 0)
|
||||
return self.cross(other) == Vector3(0, 0, 0)
|
||||
|
||||
def cross(self, other: 'Vector3') -> 'Vector3':
|
||||
"""
|
||||
向量积 叉乘:V1 @ V2 -> V3
|
||||
向量积 叉乘:v1 cross v2 -> v3
|
||||
返回如下行列式的结果:
|
||||
|
||||
``i j k``
|
||||
|
||||
``x1 y1 z1``
|
||||
|
||||
``x2 y2 z2``
|
||||
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
叉乘结果,为0向量则两向量平行,否则垂直于两向量
|
||||
行列式的结果
|
||||
"""
|
||||
return Vector3(self.y * other.z - self.z * other.y,
|
||||
self.z * other.x - self.x * other.z,
|
||||
self.x * other.y - self.y * other.x)
|
||||
|
||||
def normalize(self):
|
||||
"""
|
||||
将向量归一化。
|
||||
|
||||
自体归一化,不返回值。
|
||||
"""
|
||||
length = self.length
|
||||
self.x /= length
|
||||
self.y /= length
|
||||
self.z /= length
|
||||
|
||||
@property
|
||||
def length(self) -> float:
|
||||
"""
|
||||
@@ -117,7 +138,7 @@ class Vector3:
|
||||
...
|
||||
|
||||
@overload
|
||||
def __sub__(self, other: 'Point3') -> 'Point3':
|
||||
def __sub__(self, other: 'Point3') -> "Point3":
|
||||
...
|
||||
|
||||
def __sub__(self, other):
|
||||
@@ -133,9 +154,9 @@ class Vector3:
|
||||
elif isinstance(other, Point3):
|
||||
return Point3(self.x - other.x, self.y - other.y, self.z - other.z)
|
||||
else:
|
||||
raise TypeError(f"unsupported operand type(s) for -: 'Vector3' and '{type(other)}'")
|
||||
raise TypeError(f"unsupported operand type(s) for -: \"Vector3\" and \"{type(other)}\"")
|
||||
|
||||
def __rsub__(self, other: Point3):
|
||||
def __rsub__(self, other: 'Point3'):
|
||||
"""
|
||||
P - V -> P
|
||||
Args:
|
||||
@@ -150,54 +171,41 @@ class Vector3:
|
||||
raise TypeError(f"unsupported operand type(s) for -: '{type(other)}' and 'Vector3'")
|
||||
|
||||
@overload
|
||||
def __mul__(self, other: float) -> 'Vector3':
|
||||
def __mul__(self, other: RealNumber) -> 'Vector3':
|
||||
...
|
||||
|
||||
@overload
|
||||
def __mul__(self, other: 'Vector3') -> float:
|
||||
def __mul__(self, other: 'Vector3') -> 'Vector3':
|
||||
...
|
||||
|
||||
def __mul__(self, other):
|
||||
def __mul__(self, other: 'RealNumber | Vector3') -> 'Vector3':
|
||||
"""
|
||||
点乘法。包括点乘和数乘。
|
||||
V * V -> float\n
|
||||
数组运算 非点乘。点乘使用@,叉乘使用cross。
|
||||
Args:
|
||||
other:
|
||||
|
||||
Returns:
|
||||
float
|
||||
Raises:
|
||||
TypeError: 不支持的类型
|
||||
"""
|
||||
if isinstance(other, (int, float)):
|
||||
if isinstance(other, RealNumber):
|
||||
return Vector3(self.x * other, self.y * other, self.z * other)
|
||||
elif isinstance(other, Vector3):
|
||||
return self.x * other.x + self.y * other.y + self.z * other.z
|
||||
return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
|
||||
else:
|
||||
raise TypeError(f"unsupported operand type(s) for *: 'Vector3' and '{type(other)}'")
|
||||
|
||||
def __rmul__(self, other: float) -> 'Vector3':
|
||||
"""
|
||||
右乘。
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
乘积
|
||||
"""
|
||||
def __rmul__(self, other: RealNumber) -> 'Vector3':
|
||||
return Vector3(self.x * other, self.y * other, self.z * other)
|
||||
|
||||
def __matmul__(self, other: 'Vector3') -> 'Vector3':
|
||||
def __matmul__(self, other: 'Vector3') -> float:
|
||||
"""
|
||||
向量积 叉乘:V1 @ V2 -> V3
|
||||
点乘。
|
||||
Args:
|
||||
other:
|
||||
Returns:
|
||||
叉乘结果,为0向量则两向量平行,否则垂直于两向量
|
||||
"""
|
||||
return Vector3(self.y * other.z - self.z * other.y,
|
||||
self.z * other.x - self.x * other.z,
|
||||
self.x * other.y - self.y * other.x)
|
||||
return self.x * other.x + self.y * other.y + self.z * other.z
|
||||
|
||||
def __truediv__(self, other: float) -> 'Vector3':
|
||||
def __truediv__(self, other: RealNumber) -> 'Vector3':
|
||||
return Vector3(self.x / other, self.y / other, self.z / other)
|
||||
|
||||
def __neg__(self):
|
||||
@@ -208,3 +216,16 @@ class Vector3:
|
||||
|
||||
def __str__(self):
|
||||
return f"Vector3({self.x}, {self.y}, {self.z})"
|
||||
|
||||
|
||||
zero_vector3 = Vector3(0, 0, 0)
|
||||
"""零向量"""
|
||||
x_axis = Vector3(1, 0, 0)
|
||||
"""x轴单位向量"""
|
||||
y_axis = Vector3(0, 1, 0)
|
||||
"""y轴单位向量"""
|
||||
z_axis = Vector3(0, 0, 1)
|
||||
"""z轴单位向量"""
|
||||
|
||||
v1: Vector3 = Vector3(1, 2, 3) * 3.0
|
||||
v2: Vector3 = 3.0 * Vector3(1, 2, 3)
|
||||
|
||||
Reference in New Issue
Block a user