ddg.math.grids module
This module provides functions to create quad and triangle grids.
- ddg.math.grids.quad_grid(shape)[source]
Create a rectangular grid of the input shape.
Returns the quad faces and points of a box grid in Z^2 or Z^3.
- Parameters:
- shapetuple of length 2 or 3
Shape of the grid. First entry is the number of vertices in the x-direction, second the number of vertices in the y-direction and third (if given) the number of vertices in z-direction.
- Returns:
- facesnp.ndarray of shape (k, 4)
The quad faces of the grid, where k is the number of faces.
- coordinatesnp.ndarray
Default coordinates which start at (0,0) (or (0,0,0)) and expand to the specified values of shape.
The shape of the array is (n, 2) if the input shape has length 2, otherwise it is (n, 3).
- Raises:
- ValueError
If the given shape does not have .ndim 2 or 3.
Examples
>>> from ddg.math.grids import quad_grid >>> faces, coords = quad_grid((2, 3)) >>> faces array([[0, 1, 3, 2], [2, 3, 5, 4]]) >>> coords array([[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2]])
In the 3D case we get
>>> from ddg.math.grids import quad_grid >>> faces, coords = quad_grid((2, 2, 2)) >>> faces array([[0, 1, 3, 2], [4, 5, 7, 6], [0, 1, 5, 4], [2, 3, 7, 6], [0, 2, 6, 4], [1, 3, 7, 5]]) >>> coords array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]])
- ddg.math.grids.triangle_faces(quads)[source]
Subdivide quads into lower left and upper right triangles.
- Parameters:
- quadsnp.ndarray of shape (n, 4)
The quad faces of the grid, where n is the number of faces.
- Returns:
- np.ndarray of shape (n*2, 3)
The triangle faces of the given quad faces, where n*2 is the number of triangle faces.
Examples
>>> import numpy as np >>> from ddg.math.grids import triangle_faces >>> quad_faces = np.array([ ... [0, 1, 4, 3], ... [1, 2, 5, 4]]) >>> triangle_faces(quad_faces) array([[0, 1, 4], [1, 2, 5], [0, 4, 3], [1, 5, 4]])
- ddg.math.grids.triangle_grid(shape)[source]
Create a triangle grid of the input shape.
The input shape defines the amount of vertices in each direction. Both 2D and 3D are supported. Returns the faces and default coordinates.
- Parameters:
- shapetuple of length 2 or 3
Shape of the grid. First entry is the number of vertices in the x-direction, second the number of vertices in the y-direction and third (if given) the number of vertices in z-direction.
- Returns:
- facesnp.ndarray of shape (k, 3)
The faces of the triangle grid, where k is the number of faces.
- coordinatesnp.ndarray
The default coordinates which start at (0,0) (or (0,0,0)) and expand to the specified values of shape.
The shape of the array is (n, 2) if the input shape has length 2, otherwise it is (n, 3).
Examples
>>> from ddg.math.grids import triangle_grid >>> faces, coords = triangle_grid((2, 3)) >>> faces array([[0, 1, 3], [2, 3, 5], [0, 3, 2], [2, 5, 4]]) >>> coords array([[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2]])
In the 3D case we get
>>> faces, coords = triangle_grid((2, 2, 2)) >>> faces array([[0, 1, 3], [4, 5, 7], [0, 1, 5], [2, 3, 7], [0, 2, 6], [1, 3, 7], [0, 3, 2], [4, 7, 6], [0, 5, 4], [2, 7, 6], [0, 6, 4], [1, 7, 5]]) >>> coords array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]])