113

I am looking for a good and well developed library for geometrical manipulations and evaluations in python, like:

  • evaluate the intersection between two lines in 2D and 3D (if present)
  • evaluate the point of intersection between a plane and a line, or the line of intersection between two planes
  • evaluate the minimum distance between a line and a point
  • find the orthonormal to a plane passing through a point
  • rotate, translate, mirror a set of points
  • find the dihedral angle defined by four points

I have a compendium book for all these operations, and I could implement it but unfortunately I have no time, so I would enjoy a library that does it. Most operations are useful for gaming purposes, so I am sure that some of these functionalities can be found in gaming libraries, but I would prefer not to include functionalities (such as graphics) I don't need.

Any suggestions ? Thanks

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • If you are fine with only 2D then you can check out [this](https://github.com/Pithikos/python-rectangles). – Pithikos Mar 09 '15 at 11:56
  • There is a modern open-source library [MeshLib](https://github.com/MeshInspector/MeshLib#readme) having Python interface with major focus on operations with meshes but also able to work with point clouds, lines and voxels among other. – Fedor Oct 02 '22 at 15:03

8 Answers8

38

Perhaps take a look at SymPy.

BoshWash
  • 5,320
  • 4
  • 30
  • 50
John Y
  • 14,123
  • 2
  • 48
  • 72
  • 2
    It does not really satisfy my requirements, but it's indeed a cool project, so I accept your answer as an interesting suggestion also for the future (as SymPy has a geometry module, and a huge amount of developers) – Stefano Borini Jul 14 '09 at 22:02
  • 1
    2d only sadly. Not really a constructive geometry engine. – meawoppl Jan 24 '13 at 04:18
  • 7
    SymPy's geometry module can also be very, very slow. Since it is designed for doing symbolic math, it prefers to use precise expressions over approximate floating point values. Given the amount of square roots that are usually involved in geometric computations, you can imagine how those expressions get really large and very slow. – dusktreader Nov 12 '15 at 22:39
  • 3
    Update to [SymPy](http://docs.sympy.org/latest/modules/geometry/line3d.html) now contains 3D libraries as well – control_fd Jun 09 '16 at 17:12
  • Thanks for this answer. I'm trying out SymPy right now. First experiment seemed to be quite slow to execute - even for a simple 2D object. Any idea if it is good for processing geometry in real time? Thanks. – Bill Oct 11 '16 at 16:57
  • I did a quick test to confirm what @dusktreader was saying above about the speed of SymPy and it is a lot slower. I wrote two functions, one using SymPy and one using pyeuclid. Both create two points/vectors and then return the angle of the line between them and and its length and then timed them both in iPython. The `test_euclid()` was 5.68 µs per loop and the `test_sympy_geometry()` was 4.12 ms per loop. But SymPy's symbolic results are pretty unique, and I assume, really useful in some situations: `(acos(3/5), sqrt(17)/2)` – Bill Oct 12 '16 at 17:33
36

Shapely is a nice python wrapper around the popular GEOS library.

BoshWash
  • 5,320
  • 4
  • 30
  • 50
A. Coady
  • 54,452
  • 8
  • 34
  • 40
  • 9
    Seems to be very focused on GIS data handling, rather than pure "mathematical geometry" (so to speak) – Stefano Borini Jul 03 '09 at 00:16
  • 2
    It certainly has been created for use by the GIS community, however it is deliberately ignorant of geographic projections, which makes it fine for use as a general-purpose euclidean geometry library. I use it heavily as such in a nascent computer game project, and it works fine. – Jonathan Hartley Sep 14 '09 at 20:47
  • Ah, caveat: I'm using Shapely in 2D, I cannot vouch for its abilities in 3D. – Jonathan Hartley Sep 14 '09 at 20:49
  • 18
    what it says about 3D geometries : "A third z coordinate value may be used when constructing instances, but has no effect on geometric analysis. All operations are performed in the x-y plane." – sebpiq Feb 17 '12 at 22:46
  • 5
    The above comment took me about a half hour of coding to find out. Fairly agitating really. – meawoppl Jan 24 '13 at 04:18
  • 2
    Why only one downvote when the question specifically asks for 3D? – Jamie Bull Apr 06 '16 at 17:40
  • Shapely is included in the [Anaconda package list](https://docs.continuum.io/anaconda/pkg-docs). (As is SymPy). – Bill Oct 12 '16 at 17:38
16

I found pyeuclid to be a great simple general purpose euclidean math package. Though the library may not contain exactly the problems that you mentioned, its infrastructure is good enough to make it easy to write these on your own.

Dov Grobgeld
  • 4,783
  • 1
  • 25
  • 36
11

CGAL has Python bindings too.

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • 3
    The last release was 2009, and they appear to be incomplete. Can anyone say that they've used them recently, and that they can do the things asked in the question using the bindings? – BenjaminGolder Jun 23 '11 at 09:04
  • Yes, all of the bindings I've found are incomplete, including some of the real goodies like constructive solid geometry. Also, the highly templated nature of CGAL makes it hard to wrap well. In a project of my own ([Demakein](http://pypi.python.org/pypi/demakein)), I'm using the cffi module and some awful hacks to use CGAL directly from python, using inline C++ snippets. – Paul Harrison Nov 13 '12 at 22:36
7

I really want a good answer to this question, and the ones above left me dissatisfied. However, I just came across pythonocc which looks great, apart from lacking good docs and still having some trouble with installation (not yet pypi compatible). The last update was 4 days ago (June 19th, 2011). It wraps OpenCascade which has a ton of geometry and modeling functionality. From the pythonocc website:

pythonOCC is a 3D CAD/CAE/PLM development framework for the Python programming language. It provides features such as advanced topological and geometrical operations, data exchange (STEP, IGES, STL import/export), 2D and 3D meshing, rigid body simulation, parametric modeling.

[EDIT: I've now downloaded pythonocc and began working through some of the examples]

I believe it can perform all of the tasks mentioned, but I found it to be unintuitive to use. It is created almost entirely from SWIG wrappers, and as a result, introspection of the commands becomes difficult.

BenjaminGolder
  • 1,573
  • 5
  • 19
  • 37
  • In my opinion, it makes too much.... should we start one ? I will need many geometry operations in my raytracer soon, and it probably makes sense to create a separate github for it. I already have some code, we dont' start from scratch, but it is going to be easy to redesign if we find out it's not good enough. – Stefano Borini Jun 23 '11 at 15:53
  • @Stefano Borini: sure! I'd be happy to help however I can, but I'm nowhere near your level of qualification. – BenjaminGolder Jun 26 '11 at 18:23
  • I'm similarly disappointed with what's available. For simple stuff, I've done my own code (e.g. in [Adolphus](https://github.com/ezod/adolphus)), but tonight, for example, I need polyhedron-polyhedron intersection and it's just too many steps from what I have to be sane. In any case, if this goes forward, count me in. – ezod Sep 01 '11 at 01:09
  • does this project came to birth? – Mermoz Jan 29 '13 at 14:54
  • 1
    @Mermoz yes, but very slowly. Stefano has already made the beginning to the library, and I've been writing my own additions, but we have not gone very far. see https://github.com/stefanoborini/python-geometry and https://github.com/bengolder/python-geometry – BenjaminGolder Jan 29 '13 at 18:42
  • As an alternative to pythonocc, take a look at [OCP](https://github.com/CadQuery/OCP): "Python wrapper for OCCT7.4 generated using pywrap." Since these bindings are generated as automatically as possible, updates to new OpenCascade versions are less of an issue. It's developed and used by [CadQuery](https://github.com/CadQuery/cadquery) for their own purposes. – tanius Jan 31 '21 at 15:15
6

geometry-simple has classes Point Line Plane Movement in ~ 300 lines, using only numpy; take a look.

denis
  • 21,378
  • 10
  • 65
  • 88
2

Python Wild Magic is another SWIG wrapped code. It is however a gaming library, but you could manipulate the SWIG library file to exclude any undesired graphics stuff from the Python API.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
2

You may be interested in Python module SpaceFuncs from OpenOpt project, http://openopt.org

SpaceFuncs is tool for 2D, 3D, N-dimensional geometric modeling with possibilities of parametrized calculations, numerical optimization and solving systems of geometrical equations

user871016
  • 41
  • 1
  • 2