Shapes2D Module

Methods for creating geometric shapes in 2D.

This module provides a collection of functions for generating and manipulating various 2D geometric shapes within NumPy arrays. The shapes include basic primitives such as circles, ellipses, rectangles, and triangles, as well as more complex structures like polygons, stars, and Voronoi diagrams.

Each function operates directly on a 2D NumPy array, modifying it by filling the specified shape with a given value. The methods utilize vectorized operations for efficiency and support both solid and hollow versions of the shapes.

These functions are useful for synthetic data generation, image processing, pattern recognition, and visualization tasks in scientific computing.

@author: Dr A. Vamvakeros

nDTomo.sim.shapes2D.create_circle(arr, center, radius, thickness=0, fill_value=1)[source]

Creates a solid or hollow circle (ring) in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the circle will be drawn.

  • center (tuple of int) – The (x, y) coordinates of the circle’s center.

  • radius (int) – The outer radius of the circle.

  • thickness (int, optional (default=0)) – Thickness of the ring. If thickness=0, the circle is solid. Otherwise, a hollow ring is created with an inner radius of radius - thickness.

  • fill_value (int or float, optional (default=1)) – The value to fill inside the circle or ring.

Returns:

The modified array with the drawn circle or ring.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_ellipse(arr, center, axes_radii, thickness=0, fill_value=1)[source]

Creates a solid or hollow ellipse in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the ellipse will be drawn.

  • center (tuple of float) – Coordinates (cx, cy) for the center of the ellipse (row, col).

  • axes_radii (tuple of float) – (a, b): The radii of the semi-major and semi-minor axes.

  • thickness (int, optional (default=0)) – Thickness of the hollow ring. If set to 0, the ellipse is solid.

  • fill_value (int or float, optional (default=1)) – The value used to fill the ellipse in the array.

Returns:

The modified array with the drawn ellipse.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_equilateral_triangle(arr, p1, side, fill_value=1, orientation='default')[source]

Creates and fills an equilateral triangle in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the triangle will be drawn.

  • p1 (tuple of int) – Coordinates (x, y) for the first vertex (base-left) of the triangle.

  • side (int or float) – The length of each side of the equilateral triangle.

  • fill_value (int or float, optional (default=1)) – The value used to fill the triangle in the array.

  • orientation (str, optional (default='default')) – Specifies the orientation of the triangle: - ‘default’: Base is horizontal, apex above the base. - ‘down’: Base is horizontal, apex below the base. - ‘random’: Base is at a random angle.

Returns:

The modified array with the drawn triangle.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_polygon(arr, vertices, fill_value=1)[source]

Creates and fills an arbitrary polygon in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the polygon will be drawn.

  • vertices (list of tuples) – List of (x, y) coordinates representing the polygon vertices.

  • fill_value (int or float, optional (default=1)) – The value used to fill the polygon in the array.

Returns:

The modified array with the drawn polygon.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_rectangle(arr, corner, width, height, thickness=0, fill_value=1)[source]

Creates a solid or hollow rectangle in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the rectangle will be drawn.

  • corner (tuple of int) – The (x, y) coordinates of the top-left corner of the rectangle.

  • width (int) – The width of the rectangle (along the y-axis).

  • height (int) – The height of the rectangle (along the x-axis).

  • thickness (int, optional (default=0)) – Thickness of the border. If thickness=0, the rectangle is solid. Otherwise, a hollow rectangle is created with an inner area left unfilled.

  • fill_value (int or float, optional (default=1)) – The value to fill inside the rectangle or its border.

Returns:

The modified array with the drawn rectangle.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_star(arr, center, n_points=5, r_outer=10, r_inner=5, fill_value=1, angle_offset=0.0)[source]

Creates a filled star shape in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the star will be drawn.

  • center (tuple of int) – Coordinates (cx, cy) for the center of the star (row, col).

  • n_points (int, optional (default=5)) – Number of points in the star.

  • r_outer (float, optional (default=10)) – Radius of the outer points of the star.

  • r_inner (float, optional (default=5)) – Radius of the inner points of the star.

  • fill_value (int or float, optional (default=1)) – The value used to fill the star in the array.

  • angle_offset (float, optional (default=0.0)) – Angle offset in radians to rotate the star.

Returns:

The modified array with the drawn star.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_triangle(arr, p1, p2, p3, fill_value=1)[source]

Creates a solid or hollow triangle in a 2D array.

Parameters:
  • arr (numpy.ndarray) – The 2D array where the triangle will be drawn.

  • p1 (tuple of int) – Coordinates (x, y) of the first vertex of the triangle.

  • p2 (tuple of int) – Coordinates (x, y) of the second vertex of the triangle.

  • p3 (tuple of int) – Coordinates (x, y) of the third vertex of the triangle.

  • fill_value (int or float, optional (default=1)) – The value to fill inside the triangle or its border.

Returns:

The modified array with the drawn triangle.

Return type:

numpy.ndarray

nDTomo.sim.shapes2D.create_voronoi(arr, seed_points, fill_values=None)[source]

Generates a Voronoi diagram in a 2D NumPy array based on given seed points.

Parameters:
  • arr (np.ndarray) – A 2D array where the Voronoi diagram will be drawn.

  • seed_points (list of tuples) – A list of coordinates (x, y) representing the Voronoi seed points.

  • fill_values (list or np.ndarray, optional) – An array or list of values assigned to each region. If None, assigns unique integer values to each Voronoi cell.

Returns:

The modified array with Voronoi regions assigned.

Return type:

np.ndarray