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:
-
xiuminglib.geometry.normal.
transform_space
(normal_map, rotmat)[source]¶ Transforms the normal vectors from one space to another.
Parameters: - normal_map (numpy.ndarray) – H-by-W-by-3 array of normal vectors.
- rotmat (numpy.ndarray or mathutils.Matrix) – 3-by-3 rotation matrix, which is left-multiplied to the vectors.
Returns: Transformed normal map.
Return type:
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:
-
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.rot module¶
-
xiuminglib.geometry.rot.
axis_angle_to_rot_mat
(axis, theta)[source]¶ Gets rotation matrix that rotates points around an arbitrary axis by any angle.
Rotating around the \(x\)/\(y\)/\(z\) axis are special cases of this, where you simply specify the axis to be one of those axes.
Parameters: - axis (array_like) – 3-vector that specifies the end point of the rotation axis (start point is the origin). This will be normalized to be unit-length.
- theta (float) – Angle in radians, prescribed by the right-hand rule, so a negative value means flipping the rotation axis.
Returns: \(3\times 3\) rotation matrix, to be pre-multiplied with the vector to rotate.
Return type:
-
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:
-
xiuminglib.geometry.rot.
rot_mat_to_euler_angles
(rot_mat, tol=1e-06)[source]¶ Converts a rotation matrix into Euler angles (rotation angles around the \(x\), \(y\), and \(z\) axes).
Parameters: - rot_mat (numpy.ndarray) – \(3\times 3\) rotation matrix.
- tol (float, optional) – Tolerance for checking singularity.
Returns: Euler angles in radians.
Return type:
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:
-
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:
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:
-
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: