117

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

2
  • If you are fine with only 2D then you can check out this.
    – Pithikos
    Commented Mar 9, 2015 at 11:56
  • There is a modern open-source library MeshLib having Python interface with major focus on operations with meshes but also able to work with point clouds, lines and voxels among other.
    – Fedor
    Commented Oct 2, 2022 at 15:03

8 Answers 8

39

Perhaps take a look at SymPy.

6
  • 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) Commented Jul 14, 2009 at 22:02
  • 3
    2d only sadly. Not really a constructive geometry engine.
    – meawoppl
    Commented Jan 24, 2013 at 4: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. Commented Nov 12, 2015 at 22:39
  • 3
    Update to SymPy now contains 3D libraries as well
    – control_fd
    Commented Jun 9, 2016 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
    Commented Oct 11, 2016 at 16:57
37

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

7
  • 9
    Seems to be very focused on GIS data handling, rather than pure "mathematical geometry" (so to speak) Commented Jul 3, 2009 at 0: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. Commented Sep 14, 2009 at 20:47
  • 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
    Commented Feb 17, 2012 at 22:46
  • 5
    The above comment took me about a half hour of coding to find out. Fairly agitating really.
    – meawoppl
    Commented Jan 24, 2013 at 4:18
  • 2
    Why only one downvote when the question specifically asks for 3D?
    – Jamie Bull
    Commented Apr 6, 2016 at 17:40
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.

11

CGAL has Python bindings too.

2
  • 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? Commented Jun 23, 2011 at 9: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), I'm using the cffi module and some awful hacks to use CGAL directly from python, using inline C++ snippets. Commented Nov 13, 2012 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.

6
  • 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. Commented Jun 23, 2011 at 15:53
  • @Stefano Borini: sure! I'd be happy to help however I can, but I'm nowhere near your level of qualification. Commented Jun 26, 2011 at 18:23
  • I'm similarly disappointed with what's available. For simple stuff, I've done my own code (e.g. in 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
    Commented Sep 1, 2011 at 1:09
  • does this project came to birth?
    – Mermoz
    Commented Jan 29, 2013 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 github.com/stefanoborini/python-geometry and github.com/bengolder/python-geometry Commented Jan 29, 2013 at 18:42
6

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

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.