NAV Navbar
Logo

Introduction

pycg3d - a pure Python package for Computational Geometry in 3D.

User Guide

pycg3d provides classes for modeling Point, Vector, Line, Plane and Transformation in 3D space, and all functions are provided via the instances of these classes.

Vector

Create a vector and access its components:

    from pycg3d.cg3d_vector import CG3dVector

    v1 = CG3dVector(1.0, 0.0, 0.0)
    print v1[0], v1[1], v1[2]  # 1.0 0.0 0.0
    v1[1]=2.0
    print v1  # [1.0, 2.0, 0.0]

Use operators ’+’/’-’ for component-wise addition/subtraction of vectors:

    from pycg3d.cg3d_vector import CG3dVector

    v1 = CG3dVector(1.0, 2.0, 3.0)
    v2 = CG3dVector(1.0, 0.0, 2.0)
    v3 = v1 + v2  # v3 == CG3dVector(2.0, 2.0, 5.0)
    v4 = v1 - v2  # v4 == CG3dVector(0.0, 2.0, 1.0)

Use operator ’*’ for dot product of vectors:

    from pycg3d.cg3d_vector import CG3dVector

    va = CG3dVector(1.0, 0.0, 0.0)
    vb = CG3dVector(2.0, 2.0, 0.0)
    dp = va*vb  # 2.0

Use operator ’^’ for cross product of vectors

    from pycg3d.cg3d_vector import CG3dVector

    vx = CG3dVector(1.0, 0.0, 0.0)
    vy = CG3dVector(0.0, 1.0, 0.0)
    vz = vx^vy  # vz == CG3dVector(0.0, 0.0, 1.0)

Class CG3dVector defines a vector in 3D space.

Point

Class CG3dPoint is actually an alias of class CG3dVector.

    from pycg3d.cg3d_vector import CG3dVector
    from pycg3d.cg3d_point import CG3dPoint

    p1 = CG3dPoint(1.0, 0.0, 0.0)
    print isinstance(p1, CG3dVector) # True

Class CG3dPoint defines a point in 3D space, and it is actually an alias of class CG3dVector.

Line

Class CG3dLine2P defines a line with two points:

    from pycg3d.cg3d_point import CG3dPoint
    from pycg3d.cg3d_line import CG3dLine2P

    p1 = CG3dPoint(1.0, 0.0, 0.0)
    p2 = CG3dPoint(0.0, 1.0, 0.0)
    line = CG3dLine2P(p1, p2)

Plane

Define a plane by a point and normal vector:

   from pycg3d.cg3d_point import CG3dPoint
   from pycg3d.cg3d_point import CG3dVector
   from pycg3d.cg3d_plane import CG3dPlanePN

   yz_plane = CG3dPlanePN(CG3dPoint(0.0, 0.0, 0.0), CG3dVector(1.0, 0.0, 0.0))

Define a plane by three points:

   from pycg3d.cg3d_point import CG3dPoint
   from pycg3d.cg3d_plane import CG3dPlane3P

   xy_plane = CG3dPlane3P(
       CG3dPoint(0.0, 0.0, 0.0), 
       CG3dPoint(1.0, 0.0, 0.0), 
       CG3dPoint(0.0, 1.0, 0.0)
   )

Transformation

Translation

Translation:

      from pycg3d.cg3d_point import CG3dPoint
      from pycg3d.transform.cg3d_translate import CG3dXtranslateTF, CG3dYtranslateTF

      p1 = CG3dPoint(0.0, 0.0, 0.0)
      tf1 = CG3dXtranslateTF(1.0)  # translate along X-axis by 1.0
      p2 = p1.transform(tf1)  #  p2 == CG3dPoint(1.0, 0.0, 0.0)
      tf2 = CG3dYtranslateTF(2.0)  # translate along Y-axis by 1.0
      p3 = p2.transform(tf2)  # p3 == CG3dPoint(1.0, 2.0, 0.0)
      p4 = p1.transform(tf1).transform(tf2)  # chained transformations, p4 == CG3dPoint(1.0, 2.0, 0.0)

Rotation

Reflection

Mirror point(-1.0, 0.0, 0.0) to Y-Z Plane:

   from pycg3d.cg3d_point import CG3dPoint
   from pycg3d.cg3d_point import CG3dVector
   from pycg3d.cg3d_plane import CG3dPlanePN
   from pycg3d.transform.cg3d_reflect import CG3dPlaneMirrorTF

   p1 = CG3dPoint(-1.0, 0.0, 0.0)
   yz_plane = CG3dPlanePN(CG3dPoint(0.0, 0.0, 0.0), CG3dVector(1.0, 0.0, 0.0))
   mirror = CG3dPlaneMirrorTF(yz_plane)
   p2 = p1.transform(mirror) # p2 == CG3dPoint(1.0, 0.0, 0.0)

Utility functions

Use utility functions:

from pycg3d import utils
from pycg3d.cg3d_point import CG3dPoint

utils.distance([0.0, 0.0, 0.0], [1.0, 0.0, 0.0])  # 1.0
utils.distance(CG3dPoint(0.0, 0.0, 0.0), CG3dPoint(1.0, 0.0, 0.0))  # 1.0

Changelog