transform_util
Short description
@author: Moritz F P Becker
- pyrid.math.transform_util.axis_angle_parameters(n0, n1, a_n)[source]
Calculates the normalized rotation axis and angle (sin(phi), cos(phi)) between two normalized vectors. Accounts for the two special cases where n0=n1 and n0=-n1.
- Parameters
- n0float64[3]
Vector 1
- n1float64[3]
Vector 2
- a_nfloat64[3]
Empty vector to be filled with values of the normalized rotation axis vector
- Returns
- tuple(float64, float64)
sin(phi), cos(phi)
- pyrid.math.transform_util.axis_halfangle_parameters(n0, n1, a_n)[source]
Calculates the normalized rotation axis and half-angle (sin(phi/2), cos(phi/2)) between two normalized vectors.
- Parameters
- n0float64[3]
Vector 1
- n1float64[3]
Vector 2
- a_nfloat64[3]
Empty vector to be filled with values of the normalized rotation axis vector
- Returns
- tuple(float64, float64)
sin(phi), cos(phi)
- pyrid.math.transform_util.barycentric_coord(pos, p0, p1, p2, barycentric_params)[source]
Calculates the barycentric coordinates of a position vector with respect to the triangle represented by the vertices p0, p1, and p2. The algorithm from Ericson [6] is used.
- Parameters
- posfloat64[3]
Position vector
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- barycentric_paramsfloat64[4]
Precalculated parameters.
- Returns
- tuple(float64, float64)
Barycentric coordinates u, v.
- pyrid.math.transform_util.barycentric_coord_projection_method(pos, p0, p1, p2, normal)[source]
Calculates the barycentric coordinates of a position vector with respect to the triangle represented by the vertices p0, p1, and p2. The algorithm from Ericson [6] is used. The algorithm takes advantage of the fact that the barycentric coordinates remain invariant under projections. It projects the vertices to either of the planes xy, yz, xz and then calculates the barycentric coordinates from the sub-triangle areas.
- Parameters
- posfloat64[3]
Position vector
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- normalfloat64[3]
Unnormalized triangle normal vector.
- Returns
- tuple(float64, float64)
Barycentric coordinates u, v.
- pyrid.math.transform_util.barycentric_direction(dX, p0, p1, p2, barycentric_params)[source]
Calculates the barycentric coordinates of a direction vector with respect to the triangle represented by the vertices p0, p1, and p2. The algorithm from Ericson [6] is used.
- Parameters
- posfloat64[3]
Position vector
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- barycentric_paramsfloat64[4]
Precalculated parameters.
- Returns
- tuple(float64, float64)
Barycentric coordinates u, v.
- pyrid.math.transform_util.barycentric_params(p0, p1, p2)[source]
Precalculates the fixed parameters for the barycentric coordinates of a triangle represented by the vertices p0, p1, and p2. The algorithm is taken from Ericson [6].
- Parameters
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- pyrid.math.transform_util.cantor_pairing(k1, k2)[source]
The Cantor pairing function maps a natural number (unsigned integer) pair k1,k2 to a unique natural number n such that the mapping is bijective, i.e. the original pair can always be recovered from n. The cantor pairing is sensitive to the order, e.g. (0,1)->2.0 whereas (1,0)->1.0 .
- Parameters
- k1uint64
Natural number 1
- k3uint64
Natural number 2
- Returns
- uint64
Result of the Cantor pairing
Notes
This function is just needed when validating the reaction registry class against new implementations which may change the order in which reactions are evaluated. The Cantor pairing function enables to assign a unique id to each educt pair and thereby enables sorting the reactions by the educts! Educt order is not exchangeable!
- pyrid.math.transform_util.cartesian_coord(u, v, p0, p1, p2)[source]
Calculates the cartesian coordinates of a position vector given in barycentric coordinates.
- Parameters
- ufloat64
Barycentric u coordinate
- vfloat64
Barycentric v coordinate
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
- Returns
- float64[3]
Position vector in cartesian coordiantes.
- pyrid.math.transform_util.cartesian_direction(du, dv, p0, p1, p2)[source]
Calculates the cartesian coordinates of a direction vector given in barycentric coordinates.
- Parameters
- dufloat64
Barycentric u coordinate of the direction vector
- dvfloat64
Barycentric v coordinate of the direction vector
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
- Returns
- float64[3]
Direction vector in cartesian coordiantes.
- pyrid.math.transform_util.collision_response(v, normal, v_refl)[source]
Calculates the collision response of a ray colliding with a mesh triangle. The collision is resolved via reflection along the triangle plane.
- Parameters
- vfloat64[3]
Ray vector which to reflect
- normalfloat64[3]
Normal vector of the triangle plane
- v_reflfloat64[3]
Empty array to be filled with the values of the reflected vector
Notes
Reflection is calculated by
\[\vec{v}_{ref} = \vec{v} - 2 (\vec{v} \cdot \hat{n}) \hat{n}\]where \(\hat{n}\) is the normal vector of the plane at which \(\vec{v}\) is reflected.
- pyrid.math.transform_util.cross(v0, v1)[source]
Calculates the cross product between vectors v1 and v2.
- Parameters
- v1float64[3]
Vector 1
- v2float64[3]
Vector 2
- Returns
- float64[3]
Cross product between v1 and v2.s
- pyrid.math.transform_util.eij(p0, p1)[source]
Normalizes the edge represented by the vertices p0 and p1. This edge can be used as the first coordinate vector when constructing a triangle’s local coordinate frame.
- Parameters
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- Returns
- float64[3]
Normalized edge vector
- pyrid.math.transform_util.ek(p0, p2, ej)[source]
Given the triangle vertices p0 and p2 and the normalized triangle edge norm(p1-p0), a vector that is orthogonal to the edge but lies in teh triangle plane is returned. Incombination with the triangle normal vector these 3 vector make up the triangle’s local coordinate frame.
- Parameters
- p0float64[3]
Vertex 1
- p2float64[3]
Vertex 3
- ejfloat64[3]
Normalized edge vector p1-p0
- Returns
- float64[3]
Normalized vector orhogonal to ej that lies within the plane of triangle p0, p1, p2.
- pyrid.math.transform_util.half_angle(cos_phi)[source]
Calculates the half angle cosine and sine functions (cos(phi/2), sin(phi/2)) given the full angle cosine function cos(phi).
- Parameters
- cos_phifloat64
cos(phi).
- Returns
- tuple(float64, float64)
sin(phi/2), cos(phi/2)
- pyrid.math.transform_util.is_diagonal(M)[source]
Tests whether matrix x is diagonal.
- Parameters
- Mfloat[:,:]
Matrix
- pyrid.math.transform_util.isclose(a, b, rel_tol=1e-05, abs_tol=0.0)[source]
Returns a boolean array where two arrays are element-wise equal within a tolerance. Reduced copy of the numpy fucntion isclose(), which is, however, currently not supported by numba.
- Parameters
- a, barray_like
Input arrays to compare.
- rtolfloat
The relative tolerance parameter.
- atolfloat
The absolute tolerance parameter.
- pyrid.math.transform_util.local_coord(p0, p1, p2)[source]
Returns the local coordinate system of the triangle represented by the three vertices p0,p1 and p2.
- Parameters
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- Returns
- tuple(float64[3], float64[3], float64[3], float64[3])
Local coordinate system of the triangle (origin, ex,ey,ez).
- pyrid.math.transform_util.normal_vector(p0, p1, p2)[source]
Returns the normal vector of a triangle given its vertex vectors p0, p1 and p2.
- Parameters
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- .. note::
The direction of the triangle normal vector depends on the vertex order (clockwise or counter-clockwise)!
- Returns
- float64[3]
Normal vector of the triangle.
- pyrid.math.transform_util.normalize(v)[source]
Normalizes a vector v.
- Parameters
- vfloat64[:]
Vector
- Returns
- float64[:]
Normalized vector
- pyrid.math.transform_util.orthogonal_vector(v, v_perp)[source]
Calculates a vector orthoginal to v.
- pyrid.math.transform_util.quat_mult(q1, q2)[source]
Executes the quaternion multiplication between two quaternions q1 and q2.
- Parameters
- q1float64[4]
Quaternion 1
- q2float64[4]
Quaternion 2
- Returns
- float64[4]
Quaternion product q1*q2
- pyrid.math.transform_util.quaternion_plane_to_plane(quaternion, n0, n1, a_n)[source]
updates the quaternion corresponding to an orientation in plane n0 to the orientation in plane n1 and returns sin(phi) and cos(phi).
- Parameters
- quaternionfloat64[4]
Quaternion
- normalfloat64[3]
Normal vector 1
- normalfloat64[3]
Normal vector 2
- a_nfloat64[3]
Empty rotation axis vector
- Returns
- float64[4]
Rotation quaternion
- pyrid.math.transform_util.quaternion_random_axis_rot(quaternion, a_n)[source]
Updates an orientation quaternion by a random rotation around a given axis.
- pyrid.math.transform_util.quaternion_to_plane(normal, quaternion)[source]
Creates a rotation quaternion representing the rotation of a vector [0.0, 0.0, 1.0] to a given plane normal vector.
- Parameters
- normalfloat64[3]
Normal vector
- quaternionfloat64[4]
Quaternion
- Returns
- float64[4]
Rotation quaternion
- pyrid.math.transform_util.rodrigues_rot(v, a_n, cos_phi, sin_phi)[source]
Rotates vector v by an angle alpha around the axis vector a using the Rodriguez rotation formula.
- Parameters
- vfloat64[3]
Vector which to rotate
- a_nfloat64[3]
Normalized rotation axis vector.
- cos_phifloat64
Cosine of the angle phi by which to rotate vector v.
- sin_phifloat64
Sine of the angle phi by which to rotate vector v.
- Returns
- float64[3]
Rotated vector
- pyrid.math.transform_util.rot_quaternion(phi=0, a_n=None)[source]
Returns a quaternion, corresponding to a rotation about a given axis by a given angle. If no angle or axis is passed [1.0, 0.0, 0.0, 0.0] is returned, correpsonding to a rotation by zero degree.
- Parameters
- phifloat64
Angle, given in rad.
- a_nfloat64[3]
Rotation axis normal vector
Notes
A rotation around the axis vector \(\boldsymbol{u}\) is represented in quaternion form:
\[\boldsymbol{q_{rot}} = \cos(\phi/2) + (u_x \boldsymbol{i} + u_y \boldsymbol{i} + u_z \boldsymbol{i}) \sin(\phi/2),\]
- pyrid.math.transform_util.solid_angle(p0, p1, p2)[source]
Calculates the solid angle defined by the tetraeder that is represented by the triangle with vertices p0, p1, and p2 and the coordinate system origin. Is used by PyRID to determine whether a point sited inside or outside a mesh.
- Parameters
- p0float64[3]
Vertex 1
- p1float64[3]
Vertex 2
- p2float64[3]
Vertex 3
- Returns
- float64
Solid angle
See also
pyrid.geometry.mesh_util.point_inside_mesh_test
- pyrid.math.transform_util.sqrt_matrix(M)[source]
Calculates the square root of a matrix by doing an eigenvalue decomposition and then taking the square root of the eigenvalues before reconstructing the matric form. Only works for diagonalzable matrices!
- Parameters
- Mfloat[:,:]
Matrix
- Returns
- float64[:,:]
Square root of matric M
See also
- pyrid.math.transform_util.tri_area_2D(x1, y1, x2, y2, x3, y3)[source]
Calculates the area of a triangle in 2D.
- Parameters
- x1float64
X value of vertex 1
- x2float64
X value of vertex 2
- x3float64
X value of vertex 3
- y1float64
Y value of vertex 1
- y2float64
Y value of vertex 2
- y3float64
y value of vertex 3
- Returns
- float64
Triangle area
- pyrid.math.transform_util.unique_pairing(k1, k2)[source]
The unique pairing function maps a natural number (unsigned integer) pair k1,k2 to a unique natural number n such that the mapping is bijective, i.e. the original pair can always be recovered from n. In contrast to the cantor pairing method, this emtjod is insensitive to the order of the integer pair, e.g. (0,1)->1 just as (1,0)->1 .
- Parameters
- k1uint64
Natural number 1
- k3uint64
Natural number 2
- Returns
- uint64
Result of the unique pairing
Notes
This function is used for mapping an educt pair to a unique node index when creating reaction graphs. Also, it is needed when validating the reaction handling functions against new implementations which may change the order in which reactions are executed. The function enables to assign a unique id for each educt pair and thereby enables sorting the reactions by the educts! Educt order is exchangeable!
from: https://math.stackexchange.com/questions/882877/produce-unique-number-given-two-integers
- pyrid.math.transform_util.valid_mobility_tensor_test(Mu, Mu_sqrt)[source]
Tests whether a mobility tensor i a positive semidefinite matrix. For mobility tensors and diffusion tesnors to have proper physical meaning, they should be real positive semidefinite. .
- Parameters
- Mufloat64[3,3]
Mobility tensor
- Mu_sqrtfloat64[3,3]
Square root of mobility tensor
- .. note::
Experimentaly estimated diffusion tensors are, although non-physical, not always positive semidefinite (see e.g. :cite:t`Niethammer2006`). Also, in case of overlapping beads, the diffusion tensor calculated by PyRID is not necessarily positive semidefinite!
- Raises
- Warning(‘Diffusion tensor should be a positive semidefinite matrix!’)
Warning is raised if the diffusion tensor and the mobility tensor respectively are not positive semidefinite.
- ValueError(‘Error: complex square root of the mobility tensor detected. Square root must be real valued (Diffusion tensor should be a real positive semidefinite matrix).’)
Raised in case the square root of the mobility tensor contains complex values.
See also