xiuminglib.geometry package

Submodules

xiuminglib.geometry.depth module

xiuminglib.geometry.normal module

xiuminglib.geometry.normal.gen_world2local(normal)[source]

Generates rotation matrices that transform world normals to local \(+z\), world tangents to local \(+x\), and world binormals to local \(+y\).

Parameters:normal (numpy.ndarray) – any size-by-3 array of normal vectors.
Returns:Any size-by-3-by-3 world-to-local rotation matrices, which should be left-multiplied to world coordinates.
Return type:numpy.ndarray
xiuminglib.geometry.normal.normalize(normal_map, norm_thres=0.5)[source]

Normalizes the normal vector at each pixel of the normal map.

Parameters:
  • normal_map (numpy.ndarray) – H-by-W-by-3 array of normal vectors.
  • norm_thres (float, optional) – Normalize only vectors with a norm greater than this; helpful to avoid errors at the boundary or in the background.
Returns:

Normalized normal map.

Return type:

numpy.ndarray

xiuminglib.geometry.normal.transform_space(normal_map, rotmat)[source]

Transforms the normal vectors from one space to another.

Parameters:
Returns:

Transformed normal map.

Return type:

numpy.ndarray

xiuminglib.geometry.proj module

xiuminglib.geometry.proj.from_homo(pts, axis=None)[source]

Converts from homogeneous to non-homogeneous coordinates.

Parameters:
  • pts (numpy.ndarray or mathutils.Vector) – NumPy array of N-D point(s), or Blender vector of a single N-D point.
  • axis (int, optional) – The last slice of which dimension holds the \(w\) values. Optional for 1D inputs.
Returns:

Non-homogeneous coordinates of the input point(s).

Return type:

numpy.ndarray or mathutils.Vector

xiuminglib.geometry.proj.to_homo(pts)[source]

Pads 2/3D points to homogeneous, by guessing which dimension to pad.

Parameters:pts (array_like) – Input array of 2D or 3D points.
Returns:Homogeneous coordinates of the input points.
Return type:numpy.ndarray

xiuminglib.geometry.pt module

xiuminglib.geometry.pt.ptcld2tdf(pts, res=128, center=False)[source]

Converts point cloud to truncated distance function (TDF).

Maximum distance is capped at 1 / res.

Parameters:
  • pts (array_like) – Cartesian coordinates in object space. Of shape N-by-3.
  • res (int, optional) – Resolution of the TDF.
  • center (bool, optional) – Whether to center these points around the object space origin.
Returns:

Output TDF.

Return type:

numpy.ndarray

xiuminglib.geometry.rot module

xiuminglib.geometry.rot.deg2rad(x)[source]
xiuminglib.geometry.rot.is_rot_mat(mat, tol=1e-06)[source]

Checks if a matrix is a valid rotation matrix.

Parameters:
  • mat (numpy.ndarray) – A \(3\times 3\) matrix.
  • tol (float, optional) – Tolerance for checking if all close.
Returns:

Whether this is a valid rotation matrix.

Return type:

bool

xiuminglib.geometry.rot.rad2deg(x)[source]

xiuminglib.geometry.sph module

xiuminglib.geometry.sph.cart2sph(pts_cart, convention='lat-lng')[source]

Converts 3D Cartesian coordinates to spherical coordinates.

Parameters:
  • pts_cart (array_like) – Cartesian \(x\), \(y\) and \(z\). Of shape N-by-3 or length 3 if just one point.
  • convention (str, optional) –

    Convention for spherical coordinates: 'lat-lng' or 'theta-phi':

    lat-lng
                             ^ z (lat = 90)
                             |
                             |
        (lng = -90) ---------+---------> y (lng = 90)
                           ,'|
                         ,'  |
    (lat = 0, lng = 0) x     | (lat = -90)
    
    theta-phi
                                ^ z (theta = 0)
                                |
                                |
           (phi = 270) ---------+---------> y (phi = 90)
                              ,'|
                            ,'  |
    (theta = 90, phi = 0) x     | (theta = 180)
    
Returns:

Spherical coordinates \((r, \theta_1, \theta_2)\) in radians.

Return type:

numpy.ndarray

xiuminglib.geometry.sph.main(func_name)[source]

Unit tests that can also serve as example usage.

xiuminglib.geometry.sph.sph2cart(pts_sph, convention='lat-lng')[source]

Inverse of cart2sph().

See cart2sph().

xiuminglib.geometry.sph.uniform_sample_sph(n, r=1, convention='lat-lng')[source]

Uniformly samples points on the sphere [source].

Parameters:
  • n (int) – Total number of points to sample. Must be a square number.
  • r (float, optional) – Radius of the sphere. Defaults to \(1\).
  • convention (str, optional) – Convention for spherical coordinates. See cart2sph() for conventions.
Returns:

Spherical coordinates \((r, \theta_1, \theta_2)\) in radians. The points are ordered such that all azimuths are looped through first at each elevation.

Return type:

numpy.ndarray

xiuminglib.geometry.tri module

xiuminglib.geometry.tri.barycentric(pts, tvs)[source]

Computes barycentric coordinates of 3D point(s) w.r.t. a triangle.

Parameters:
  • pts (array_like) – 3-array for one point; N-by-3 array for multiple points.
  • tvs (array_like) – 3-by-3 array with rows being the triangle’s vertices.
Returns:

Barycentric coordinates of the same shape as pts. If any array element \(\notin [0, 1]\), the input point doesn’t fall on the triangle.

Return type:

numpy.ndarray

xiuminglib.geometry.tri.moeller_trumbore(ray_orig, ray_dir, tri_v0, tri_v1, tri_v2)[source]

Decides if a ray intersects with a triangle using the Moeller-Trumbore algorithm.

\(O + D = (1-u-v)V_0 + uV_1 + vV_2\).

Parameters:
  • ray_orig (array_like) – 3D coordinates of the ray origin \(O\).
  • ray_dir (array_like) – Ray direction \(D\) (not necessarily normalized).
  • tri_v0 (array_like) – Triangle vertex \(V_0\).
  • tri_v1 (array_like) – Triangle vertex \(V_1\).
  • tri_v2 (array_like) – Triangle vertex \(V_2\).
Returns:

  • u (float) – The \(u\) component of the Barycentric coordinates of the intersection. Intersection is in-triangle (including on an edge or at a vertex), if \(u\geq 0\), \(v\geq 0\), and \(u+v\leq 1\).
  • v (float) – The \(v\) component.
  • t (float) – Distance coefficient from \(O\) to the intersection along \(D\). Intersection is between \(O\) and \(O+D\), if \(0 < t < 1\).

Return type:

tuple