🐛 fix line.cal_intersection

This commit is contained in:
2024-08-28 01:26:59 +08:00
parent 04d7c82783
commit 5f4adbb75f
12 changed files with 374 additions and 106 deletions

View File

@@ -8,6 +8,7 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@File : angle.py
@Software: PyCharm
"""
import math
from typing import overload
from .const import PI # type: ignore
@@ -71,6 +72,60 @@ class AnyAngle:
"""
return AnyAngle(-self.radian % (2 * PI), is_radian=True)
@property
def sin(self) -> float:
"""
正弦值。
Returns:
正弦值
"""
return math.sin(self.radian)
@property
def cos(self) -> float:
"""
余弦值。
Returns:
余弦值
"""
return math.cos(self.radian)
@property
def tan(self) -> float:
"""
正切值。
Returns:
正切值
"""
return math.tan(self.radian)
@property
def cot(self) -> float:
"""
余切值。
Returns:
余切值
"""
return 1 / math.tan(self.radian)
@property
def sec(self) -> float:
"""
正割值。
Returns:
正割值
"""
return 1 / math.cos(self.radian)
@property
def csc(self) -> float:
"""
余割值。
Returns:
余割值
"""
return 1 / math.sin(self.radian)
def __add__(self, other: 'AnyAngle') -> 'AnyAngle':
return AnyAngle(self.radian + other.radian, is_radian=True)

View File

@@ -16,4 +16,6 @@ E = math.e
GOLDEN_RATIO = (1 + math.sqrt(5)) / 2
GAMMA = 0.57721566490153286060651209008240243104215933593992
EPSILON = 0.0001
"""ε"""
APPROX = 0.001
"""约等于误差"""

View File

@@ -8,8 +8,10 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@File : other.py
@Software: PyCharm
"""
import math
from typing import TYPE_CHECKING
from .const import APPROX
from .mp_math_typing import OneSingleVarFunc, RealNumber
from .utils import sign_format
from .vector import Vector3
@@ -30,6 +32,17 @@ class Line3:
self.point = point
self.direction = direction
def approx(self, other: 'Line3', epsilon: float = APPROX) -> bool:
"""
判断两条直线是否近似相等。
Args:
other: 另一条直线
epsilon: 误差
Returns:
是否近似相等
"""
return self.is_approx_parallel(other, epsilon) and (self.point - other.point).is_approx_parallel(self.direction, epsilon)
def cal_angle(self, other: 'Line3') -> 'AnyAngle':
"""
计算直线和直线之间的夹角。
@@ -42,6 +55,28 @@ class Line3:
"""
return self.direction.cal_angle(other.direction)
def cal_distance(self, other: 'Line3 | Point3') -> float:
"""
计算直线和直线或点之间的距离。
Args:
other: 平行直线或点
Returns:
距离
Raises:
ValueError: 直线不平行
TypeError: 不支持的类型
"""
if isinstance(other, Line3):
if self.is_parallel(other):
return (self.point - other.point).cross(self.direction).length / self.direction.length
else:
raise ValueError("Lines are not parallel.")
elif isinstance(other, Point3):
return (other - self.point).cross(self.direction).length / self.direction.length
else:
raise TypeError("Unsupported type.")
def cal_intersection(self, other: 'Line3') -> 'Point3':
"""
计算两条直线的交点。
@@ -49,12 +84,16 @@ class Line3:
other: 另一条直线
Returns:
交点
Raises:
ValueError: 直线平行
ValueError: 直线不共面
"""
if self.is_parallel(other):
raise ValueError("Lines are parallel and do not intersect.")
if not self.is_coplanar(other):
raise ValueError("Lines are not coplanar and do not intersect.")
return self.point + self.direction.cross(other.direction)
return (self.point + (self.direction.cross(other.direction) @ other.direction.cross(self.point - other.point)) /
self.direction.cross(other.direction).length ** 2 * self.direction)
def cal_perpendicular(self, point: 'Point3') -> 'Line3':
"""
@@ -86,6 +125,17 @@ class Line3:
lambda t: self.point.y + self.direction.y * t,
lambda t: self.point.z + self.direction.z * t)
def is_approx_parallel(self, other: 'Line3', epsilon: float = 1e-6) -> bool:
"""
判断两条直线是否近似平行。
Args:
other: 另一条直线
epsilon: 误差
Returns:
是否近似平行
"""
return self.direction.is_approx_parallel(other.direction, epsilon)
def is_parallel(self, other: 'Line3') -> bool:
"""
判断两条直线是否平行。
@@ -106,15 +156,26 @@ class Line3:
"""
return self.is_parallel(other) and (self.point - other.point).is_parallel(self.direction)
def is_point_on(self, point: 'Point3') -> bool:
"""
判断点是否在直线上。
Args:
point: 点
Returns:
是否在直线上
"""
return (point - self.point).is_parallel(self.direction)
def is_coplanar(self, other: 'Line3') -> bool:
"""
判断两条直线是否共面。
充要条件两直线方向向量的叉乘与两直线上任意一点的向量的点积为0。
Args:
other: 另一条直线
Returns:
是否共面
"""
return self.direction.cross(other.direction).is_parallel(self.direction)
return self.direction.cross(other.direction) @ (self.point - other.point) == 0
def simplify(self):
"""
@@ -147,15 +208,20 @@ class Line3:
direction = p2 - p1
return cls(p1, direction)
def __and__(self, other: 'Line3') -> 'Point3':
def __and__(self, other: 'Line3') -> 'Line3 | Point3 | None':
"""
计算两条直线点集合的交集。交点
计算两条直线点集合的交集。重合线返回自身平行线返回None交线返回交点
Args:
other: 另一条直线
Returns:
交点
"""
return self.cal_intersection(other)
if self.is_collinear(other):
return self
elif self.is_parallel(other) or not self.is_coplanar(other):
return None
else:
return self.cal_intersection(other)
def __eq__(self, other) -> bool:
"""

View File

@@ -7,10 +7,11 @@ from typing import TYPE_CHECKING, overload
import numpy as np
from .const import APPROX
from .vector import Vector3, zero_vector3
from .line import Line3
from .point import Point3
from .utils import sign
from .utils import approx, sign
if TYPE_CHECKING:
from .angle import AnyAngle
@@ -31,6 +32,28 @@ class Plane3:
self.c = c
self.d = d
def approx(self, other: 'Plane3', epsilon: float = APPROX) -> bool:
"""
判断两个平面是否近似相等。
Args:
other:
epsilon:
Returns:
是否近似相等
"""
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
def cal_angle(self, other: 'Line3 | Plane3') -> 'AnyAngle':
"""
计算平面与平面之间的夹角。
@@ -126,6 +149,16 @@ class Plane3:
"""
return Plane3.from_point_and_normal(point, self.normal)
def is_parallel(self, other: 'Plane3') -> bool:
"""
判断两个平面是否平行。
Args:
other: 另一个平面
Returns:
是否平行
"""
return self.normal.is_parallel(other.normal)
@property
def normal(self) -> 'Vector3':
"""
@@ -237,5 +270,10 @@ class Plane3:
else:
raise TypeError(f"unsupported operand type(s) for &: 'Plane3' and '{type(other)}'")
def __eq__(self, other) -> bool:
return self.approx(other)
def __rand__(self, other: 'Line3') -> 'Point3':
return self.cal_intersection_point3(other)

View File

@@ -1,5 +1,8 @@
from typing import TYPE_CHECKING, overload
from .const import APPROX
from .utils import approx
if TYPE_CHECKING:
from .vector import Vector3 # type: ignore
@@ -17,6 +20,18 @@ class Point3:
self.y = y
self.z = z
def approx(self, other: "Point3", epsilon: float = APPROX) -> bool:
"""
判断两个点是否近似相等。
Args:
other:
epsilon:
Returns:
是否近似相等
"""
return all([abs(self.x - other.x) < epsilon, abs(self.y - other.y) < epsilon, abs(self.z - other.z) < epsilon])
def __str__(self):
return f"Point3({self.x}, {self.y}, {self.z})"
@@ -45,7 +60,7 @@ class Point3:
other:
Returns:
"""
return self.x == other.x and self.y == other.y and self.z == other.z
return approx(self.x, other.x) and approx(self.y, other.y) and approx(self.z, other.z)
def __sub__(self, other: "Point3") -> "Vector3":
"""

View File

@@ -23,68 +23,18 @@ class Segment3:
:param p1:
:param p2:
"""
self._start = p1
self._end = p2
self.p1 = p1
self.p2 = p2
"""方向向量"""
self._direction = self._end - self._start
self.direction = self.p2 - self.p1
"""长度"""
self._length = self._direction.length
self.length = self.direction.length
"""中心点"""
self._midpoint = (self._start + self._end) / 2
self.midpoint = Point3((self.p1.x + self.p2.x) / 2, (self.p1.y + self.p2.y) / 2, (self.p1.z + self.p2.z) / 2)
def __repr__(self):
return f"Segment3({self.p1}, {self.p2})"
def __str__(self):
return f"Segment3({self._start}, {self._end})"
def _unset_properties(self):
self._length = None
self._direction = None
self._midpoint = None
@property
def start(self) -> "Point3":
return self._start
@start.setter
def start(self, value: "Point3"):
self._start = value
self._unset_properties()
@property
def end(self) -> "Point3":
return self._end
@end.setter
def end(self, value: "Point3"):
self._end = value
self._unset_properties()
@property
def length(self) -> float:
"""
线段的长度。
:return:
"""
if self._length is None:
self._length = (self._end - self._start).length
return self._length
@property
def direction(self) -> "Vector3":
"""
线段的方向向量。
:return:
"""
if self._direction is None:
self._direction = self._end - self._start
return self._direction
@property
def midpoint(self) -> "Point3":
"""
线段的中点。
:return:
"""
if self._midpoint is None:
self._midpoint = (self._start + self._end) / 2
return self._midpoint
return f"Segment3({self.p1} -> {self.p2})"

View File

@@ -8,9 +8,16 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@File : utils.py
@Software: PyCharm
"""
from typing import overload
from typing import overload, TYPE_CHECKING
from mbcp.mp_math.mp_math_typing import RealNumber
from .mp_math_typing import RealNumber
from .const import APPROX
if TYPE_CHECKING:
from .vector import Vector3
from .point import Point3
from .plane import Plane3
from .line import Line3
def clamp(x: float, min_: float, max_: float) -> float:
@@ -27,24 +34,36 @@ def clamp(x: float, min_: float, max_: float) -> float:
return max(min(x, max_), min_)
class Approx(float):
class Approx:
"""
用于近似比较浮点数的类。
"""
epsilon = 0.001
"""全局近似值。"""
用于近似比较对象
def __new__(cls, x: RealNumber):
return super().__new__(cls, x)
已实现对象 实数 Vector3 Point3 Plane3 Line3
"""
def __init__(self, value: RealNumber):
self.value = value
def __eq__(self, other):
return abs(self - other) < Approx.epsilon
if isinstance(self.value, (float, int)):
if isinstance(other, (float, int)):
return abs(self.value - other) < APPROX
else:
self.raise_type_error(other)
elif isinstance(self.value, Vector3):
if isinstance(other, (Vector3, Point3, Plane3, Line3)):
return all([approx(self.value.x, other.x), approx(self.value.y, other.y), approx(self.value.z, other.z)])
else:
self.raise_type_error(other)
def raise_type_error(self, other):
raise TypeError(f"Unsupported type: {type(self.value)} and {type(other)}")
def __ne__(self, other):
return not self.__eq__(other)
def approx(x: float, y: float = 0.0, epsilon: float = 0.0001) -> bool:
def approx(x: float, y: float = 0.0, epsilon: float = APPROX) -> bool:
"""
判断两个数是否近似相等。或包装一个实数用于判断是否近似于0。
Args:

View File

@@ -1,8 +1,12 @@
import math
from typing import overload, TYPE_CHECKING
import numpy as np
from .const import APPROX
from .mp_math_typing import RealNumber
from .point import Point3
from .utils import approx
if TYPE_CHECKING:
from .angle import AnyAngle
@@ -21,6 +25,18 @@ class Vector3:
self.y = y
self.z = z
def approx(self, other: 'Vector3', epsilon: float = APPROX) -> bool:
"""
判断两个向量是否近似相等。
Args:
other:
epsilon:
Returns:
是否近似相等
"""
return all([abs(self.x - other.x) < epsilon, abs(self.y - other.y) < epsilon, abs(self.z - other.z) < epsilon])
def cal_angle(self, other: 'Vector3') -> 'AnyAngle':
"""
计算两个向量之间的夹角。
@@ -31,16 +47,6 @@ class Vector3:
"""
return AnyAngle(math.acos(self @ other / (self.length * other.length)), is_radian=True)
def is_parallel(self, other: 'Vector3') -> bool:
"""
判断两个向量是否平行。
Args:
other: 另一个向量
Returns:
是否平行
"""
return self.cross(other) == Vector3(0, 0, 0)
def cross(self, other: 'Vector3') -> 'Vector3':
"""
向量积 叉乘v1 cross v2 -> v3
@@ -61,6 +67,27 @@ class Vector3:
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
def is_approx_parallel(self, other: 'Vector3', epsilon: float = APPROX) -> bool:
"""
判断两个向量是否近似平行。
Args:
other: 另一个向量
epsilon: 允许的误差
Returns:
是否近似平行
"""
return self.cross(other).length < epsilon
def is_parallel(self, other: 'Vector3') -> bool:
"""
判断两个向量是否平行。
Args:
other: 另一个向量
Returns:
是否平行
"""
return self.cross(other).approx(zero_vector3)
def normalize(self):
"""
将向量归一化。
@@ -72,6 +99,15 @@ class Vector3:
self.y /= length
self.z /= length
@property
def np_array(self) -> 'np.ndarray':
"""
返回numpy数组
Returns:
"""
return np.array([self.x, self.y, self.z])
@property
def length(self) -> float:
"""
@@ -90,6 +126,9 @@ class Vector3:
"""
return self / self.length
def __abs__(self):
return self.length
@overload
def __add__(self, other: 'Vector3') -> 'Vector3':
...
@@ -122,7 +161,7 @@ class Vector3:
Returns:
是否相等
"""
return self.x == other.x and self.y == other.y and self.z == other.z
return approx(self.x, other.x) and approx(self.y, other.y) and approx(self.z, other.z)
def __radd__(self, other: 'Point3') -> 'Point3':
"""
@@ -197,7 +236,7 @@ class Vector3:
def __rmul__(self, other: 'RealNumber') -> 'Vector3':
return self.__mul__(other)
def __matmul__(self, other: 'Vector3') -> float:
def __matmul__(self, other: 'Vector3') -> 'RealNumber':
"""
点乘。
Args: