基于射影几何和 numpy 的 Python 几何包。
项目描述
几何学
Geometer 是 Python 的几何库,它使用射影几何和 numpy 进行快速几何计算。在射影几何中,2D 中的每个点都由一个 3D 向量表示,3D 中的每个点都由一个 4 维向量表示。这具有以下优点:
- 无穷远处的点可以像普通点一样处理。
- 投影变换由矩阵描述,但它们也可以表示平移和一般仿射变换。
- 如果两条线位于同一平面上,则它们具有唯一的交点。平行线在无穷远处有一个交点。
- 通过给定点的交点、平面或线可以使用简单的叉积或张量图来计算。
- 无穷大和交叉比的特殊复点可用于计算角度和构建垂直几何结构。
- 点和线的集合可以用张量表示。它们的连接线和交点可以使用快速矩阵乘法计算。
库中的大部分计算都是通过张量图完成的(使用 numpy.einsum)。
Geometer 最初是作为一种学习练习而构建的,它基于慕尼黑工业大学教授的两门研究生课程。在项目上投入了大量时间后,它现在经过了相当良好的测试,API 应该是稳定的。
包的源代码可以在GitHub上找到 ,文档可以在Read the Docs上找到。
安装
您可以直接从 PyPI 安装包:
pip install geometer
用法
from geometer import *
import numpy as np
# Meet and Join operations
p = Point(2, 4)
q = Point(3, 5)
l = Line(p, q)
m = Line(0, 1, 0)
l.meet(m)
# Point(-2, 0)
# Parallel and perpendicular lines
m = l.parallel(through=Point(1, 1))
n = l.perpendicular(through=Point(1, 1))
is_perpendicular(m, n)
# True
# Angles and distances (euclidean)
a = angle(l, Point(1, 0))
p + 2*dist(p, q)*Point(np.cos(a), np.sin(a))
# Point(4, 6)
# Transformations
t1 = translation(0, -1)
t2 = rotation(-np.pi)
t1*t2*p
# Point(-2, -5)
# Collections of points and lines
coordinates = np.random.randint(100, size=(1000, 2))
points = PointCollection([Point(x, y) for x, y in coordinates])
lines = points.join(-points)
zero = PointCollection(np.zeros((1000, 2)), homogenize=True)
lines.meet(rotation(np.pi/2)*lines) == zero
# True
# Ellipses/Quadratic forms
a = Point(-1, 0)
b = Point(0, 3)
c = Point(1, 2)
d = Point(2, 1)
e = Point(0, -1)
conic = Conic.from_points(a, b, c, d, e)
ellipse = Conic.from_foci(c, d, bound=b)
# Geometric shapes
o = Point(0, 0)
x, y = Point(1, 0), Point(0, 1)
r = Rectangle(o, x, x+y, y)
r.area
# 1
# 3-dimensional objects
p1 = Point(1, 1, 0)
p2 = Point(2, 1, 0)
p3 = Point(3, 4, 0)
l = p1.join(p2)
A = join(l, p3)
A.project(Point(3, 4, 5))
# Point(3, 4, 0)
l = Line(Point(1, 2, 3), Point(3, 4, 5))
A.meet(l)
# Point(-2, -1, 0)
p3 = Point(1, 2, 0)
p4 = Point(1, 1, 1)
c = Cuboid(p1, p2, p3, p4)
c.area
# 6
# Cross ratios
t = rotation(np.pi/16)
crossratio(q, t*q, t**2 * q, t**3 * q, p)
# 1.4408954235712448
# Higher dimensions
p1 = Point(1, 1, 4, 0)
p2 = Point(2, 1, 5, 0)
p3 = Point(3, 4, 6, 0)
p4 = Point(0, 2, 7, 0)
E = Plane(p1, p2, p3, p4)
l = Line(Point(0, 0, 0, 0), Point(1, 2, 3, 4))
E.meet(l)
# Point(0, 0, 0, 0)
参考
包中实现的许多算法和公式取自以下书籍和论文:
- Jürgen Richter-Gebert,关于投影几何的观点
- Jürgen Richter-Gebert 和 Thorsten Orendt,Geometriekalküle
- Olivier Faugeras,三维计算机视觉
- Jim Blinn,空间中的线条:4D 交叉产品
- Jim Blinn,空间中的线条:线条公式
- Jim Blinn,空间中的线条:两个矩阵
- Jim Blinn,空间中的线条:回到图表
- Jim Blinn,《太空中的线条:两条线的故事》
- Jim Blinn,空间中的线条:我们的朋友双曲抛物面
- Jim Blinn,空间中的线条:Tinkertoys 的代数
- Jim Blinn,空间中的线条:通过四行的线条
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
geometer-0.3.4.tar.gz
(44.0 kB
查看哈希)
内置分布
geometer-0.3.4-py3-none-any.whl
(46.3 kB
查看哈希)