Geometry Module

This module contains classes for representing 3D shapes.

class Vector3D(x: float, y: float, z: float)[source]

Represents a vector in 3D space.

Initialize a new vector.

Parameters:
  • x (float) – The x-coordinate of the vector.

  • y (float) – The y-coordinate of the vector.

  • z (float) – The z-coordinate of the vector.

classmethod create_random() Vector3D[source]

Create a random vector.

Returns:

A random vector.

Return type:

Vector3D

length() float[source]

Calculate the length of this vector.

Returns:

The length of this vector.

Return type:

float

normalize() Vector3D[source]

Normalize this vector.

Returns:

A new vector with the same direction as this vector but with unit length.

Return type:

Vector3D

dot(other: Vector3D) float[source]

Calculate the dot product of this vector with another vector.

Parameters:

other (Vector3D) – The vector to dot with this vector.

Returns:

The dot product of this vector with another vector.

Return type:

float

cross(other: Vector3D) Vector3D[source]

Calculate the cross product of this vector with another vector.

Parameters:

other (Vector3D) – The vector to cross with this vector.

Returns:

The cross product of this vector with another vector.

Return type:

Vector3D

subtract(other: Vector3D) Vector3D[source]

Subtracts another vector from this vector.

Parameters:

other (Vector3D) – The vector to subtract from this vector.

Returns:

A new vector with the coordinates of the difference.

Return type:

Vector3D

multiply(scalar: float) Vector3D[source]

Multiplies this vector by a scalar.

Parameters:

scalar (float) – The scalar to multiply this vector by.

Returns:

A new vector with the coordinates of the product.

Return type:

Vector3D

class Point2D(x: float, y: float)[source]

Represents a point in 2D space.

Initialize a new point.

Parameters:
  • x (float) – The x-coordinate of the point.

  • y (float) – The y-coordinate of the point.

subtract_point(other: Point2D) Point2D[source]

Subtracts the coordinates of another point from this point.

Parameters:

other (Point2D) – The point to subtract from this point.

Returns:

A new point with the coordinates of the difference.

Return type:

Point2D

cross(other: Point2D) float[source]

Calculate the cross product of this point with another point.

Parameters:

other (Point2D) – The point to cross with this point.

Returns:

The cross product of this point with another point.

Return type:

float

class Point3D(x: float, y: float, z: float)[source]

Represents a point in 3D space.

Initialize a new point.

Parameters:
  • x (float) – The x-coordinate of the point.

  • y (float) – The y-coordinate of the point.

  • z (float) – The z-coordinate of the point.

create_vector(other: Point3D) Vector3D[source]

Create a vector from this point to another point.

Parameters:

other (Point3D) – The other point.

Returns:

A vector from this point to another point.

Return type:

Vector3D

calculate_distance(other: Point3D) float[source]

Calculate the distance between this point and another point.

Parameters:

other (Point3D) – The other point.

Returns:

The distance between this point and another point.

Return type:

float

midpoint(other: Point3D) Point3D[source]

Calculate the midpoint between this point and another point.

Parameters:

other (Point3D) – The other point.

Returns:

The midpoint between this point and another point.

Return type:

Point3D

translate(vector: Vector3D) Point3D[source]

Add a vector to this point.

Parameters:

vector (Vector3D) – The vector to add to this point.

Returns:

A new point with the coordinates of the sum.

Return type:

Point3D

rotate(x: float = 0.0, y: float = 0.0, z: float = 0.0) Point3D[source]

Rotate this point around the origin.

Parameters:
  • x (float) – The clockwise rotation around the x-axis.

  • y (float) – The clockwise rotation around the y-axis.

  • z (float) – The clockwise rotation around the z-axis.

Returns:

A new point with the coordinates of the rotated point.

Return type:

Point3D

Note x, y, z and in radians.

sign(x: float) int[source]

Return the sign of a number.

Parameters:

x (float) – The number.

Returns:

The sign of the number.

Return type:

int

gram_schmidt(n: Vector3D) Tuple[Vector3D, Vector3D][source]

Generate two orthogonal vectors for a given vector using the Gram-Schmidt process.

Parameters:

n (Vector3D) – The vector to generate orthogonal vectors for.

Returns:

Two orthogonal vectors.

Return type:

ty.Tuple[Vector3D, Vector3D]

class Line3D(start: Point3D, end: Point3D)[source]

A line in 3D.

Parameters:
  • start (Point3D) – The start point of the line.

  • end (Point3D) – The end point of the line.

class Plane3D(center: Point3D, normal: Vector3D)[source]

A plane in 3D.

Parameters:
  • center (Point3D) – The center of the plane.

  • normal (Vector3D) – The normal vector of the plane.

class Circle3D(center: Point3D, radius: float, normal: Vector3D)[source]

A circle in 3D.

Parameters:
  • center (Point3D) – The center of the circle.

  • radius (float) – The radius of the circle.

  • normal (Vector3D) – The normal vector of the circle.

class Sphere(center: Point3D, radius: float)[source]

A sphere.

Parameters:
  • center (Point3D) – The center of the sphere.

  • radius (float) – The radius of the sphere.

class CylinderCapType(value)[source]

The type of a cap.

Variables:
  • NO_CAP – No cap.

  • FLAT – Flat cap.

  • ROUND – Round cap.

class Cylinder(start: Point3D, end: Point3D, radius: float, cap_type: CylinderCapType)[source]

A cylinder.

Parameters:
  • start (Point3D) – The start point of the cylinder.

  • end (Point3D) – The end point of the cylinder.

  • radius (float) – The radius of the cylinder.

  • cap_type (CylinderCapType) – The type of the cap.

same_side_of_plane(plane: Plane3D, p1: Point3D, p2: Point3D) bool[source]

Check if two points are on the same side of a plane.

Parameters:
Returns:

True if the points are on the same side of the plane, False otherwise.

Return type:

bool

distance_to_line(line: Line3D, point: Point3D) float[source]

Compute the distance from a point to the line.

Parameters:
  • line (Line3D) – The line to compute the distance to.

  • point (Point3D) – The point to compute the distance to.

Returns:

The distance from the point to the line.

Return type:

float

get_perpendicular_lines(line: Line3D, width: float, num_lines: int) List[Line3D][source]

Split current line into multiple perpendicular lines.

Parameters:
  • line (Line3D) – The line to split.

  • width (float) – The spacing between the lines.

  • num_lines (int) – The number of lines to split the line into.

Returns:

The perpendicular lines.

Return type:

ty.List[Line3D]

Raises:
  • ValueError – If the number of lines is less than 1.

  • ValueError – If the width is less than or equal to 0.

get_points_on_line_3d(line: Line3D, num_points: int) List[Point3D][source]

Generate num_points + 1 points along a line.

Parameters:
  • line (Line3D) – The line.

  • num_points (int) – The number of points to generate.

Returns:

The points along the line.

Return type:

ty.List[Point3D]

get_points_on_circumference_circle_3d(circle: Circle3D, num_points: int) List[Point3D][source]

Generate points on the circumference of the circle.

Parameters:
  • circle (Circle3D) – The circle.

  • num_points (int) – The number of points to generate.

Returns:

The points on the circumference of the circle.

Return type:

ty.List[Point3D]

get_points_on_surface_circle_3d(circle: Circle3D, num_radii: int, num_points: int) List[Point3D][source]

Generate points on the surface of the circle.

Parameters:
  • circle (Circle3D) – The circle.

  • num_radii (int) – The number of radii to generate points for.

  • num_points (int) – The number of points to generate per radius.

Returns:

The points on the surface of the circle.

Return type:

ty.List[Point3D]

get_points_on_surface_sphere(sphere: Sphere, num_phi: int, num_theta: int, filter_for_pov: bool = True) List[Point3D][source]

Generate points on the surface of a sphere.

Parameters:
  • sphere (Sphere) – The sphere.

  • num_phi (int) – The resolution of the sphere in the phi direction.

  • num_theta (int) – The resolution of the sphere in the theta direction.

  • filter_for_pov (bool) – If True, only return points that are on the surface of the sphere we can see from POV positive z-axis towards origin.

Returns:

The points on the surface of the sphere.

Return type:

ty.List[Point3D]

get_points_on_surface_cap(cap_type: CylinderCapType, center_cap: Point3D, radius_cap: float, normal_cap: Vector3D, center_cylinder: Point3D, resolution: int, filter_for_pov: bool = True) List[Point3D][source]

Generate points on the surface of the cap.

Parameters:
  • cap_type (CylinderCapType) – The type of the cap.

  • center_cap (Point3D) – The center of the cap.

  • radius_cap (float) – The radius of the cap.

  • normal_cap (Vector3D) – The normal vector of the cap.

  • center_cylinder (Point3D) – The center of the cylinder.

  • resolution (int) – The resolution of the cap.

  • filter_for_pov (bool) – If True, only return points that are on the visible surface of the cap (i.e. the surface of the cap we can see from POV positive z-axis towards origin).

Returns:

The points on the surface of the cap.

Return type:

ty.List[Point3D]

Raises:

ValueError – If the cap type is unknown.

get_points_on_surface_cylinder(cylinder: Cylinder, resolution: int) List[Point3D][source]

Generate points on the surface of the cylinder.

Parameters:
  • cylinder (Cylinder) – The cylinder.

  • resolution (int) – The resolution of the cylinder.

Returns:

The points on the surface of the cylinder.

Return type:

ty.List[Point3D]

point_is_inside_sphere(sphere: Sphere, point: Point3D) bool[source]

Check if a point is inside the sphere.

Parameters:
  • sphere (Sphere) – The sphere to check.

  • point (Point3D) – The point to check.

Returns:

True if the point is inside the sphere, False otherwise.

Return type:

bool

point_is_inside_cylinder(cylinder: Cylinder, point: Point3D) bool[source]

Check if a point is inside the cylinder.

Parameters:
  • cylinder (Cylinder) – The cylinder to check.

  • point (Point3D) – The point to check.

Returns:

True if the point is inside the cylinder, False otherwise.

Return type:

bool

Raises:

ValueError – If the cap type is unknown.

sphere_intersects_with_sphere(sphere1: Sphere, sphere2: Sphere) bool[source]

Check if two spheres intersect.

Parameters:
  • sphere1 (Sphere) – The first sphere.

  • sphere2 (Sphere) – The second sphere.

Returns:

True if the spheres intersect, False otherwise.

Return type:

bool

sphere_intersects_with_cylinder(sphere: Sphere, cylinder: Cylinder) bool[source]

Check if a sphere intersects with a cylinder.

Parameters:
  • sphere (Sphere) – The sphere.

  • cylinder (Cylinder) – The cylinder.

Returns:

True if the sphere intersects with the cylinder, False otherwise.

Return type:

bool

cylinder_intersects_with_cylinder(cylinder1: Cylinder, cylinder2: Cylinder) bool[source]

Check if two cylinders intersect.

Parameters:
  • cylinder1 (Cylinder) – The first cylinder.

  • cylinder2 (Cylinder) – The second cylinder.

Returns:

True if the cylinders intersect, False otherwise.

Return type:

bool