forgi.threedee.utilities.vector module¶
-
forgi.threedee.utilities.vector.
GetPointsEquiAngularlyDistancedOnSphere
(numberOfPoints=45)[source]¶ each point you get will be of form ‘x, y, z’; in cartesian coordinates eg. the ‘l2 distance’ from the origion [0., 0., 0.] for each point will be 1.0 ———— converted from: http://web.archive.org/web/20120421191837/http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere )
-
forgi.threedee.utilities.vector.
change_basis
(coords, new_basis, old_basis)[source]¶ Change the basis of coordinates to a new basis. In a regular structure we have the coordinates in the regular cartesian coordinate system. For helix-helix orientations, however, we want to express the coordinates in a coordinate system defined by the first helix.
The new basis will consist of the axis of the first helix, one of its twist elements (the one closest to the second vector) and a third vector orthogonal to the previous two.
# http://tutorial.math.lamar.edu/Classes/LinAlg/ChangeOfBasis.aspx
Parameters: - coords – The coordinates to transform (array of n elements).
- new_basis – The new basis vectors (n x n matrix)
- old_basis – The old basis for the coordinates(n x n matrix)
Returns: The new coordinates according to the new basis
-
forgi.threedee.utilities.vector.
change_basis_vectorized
(coords, new_basis, old_basis)[source]¶ Change an array of vectors (coords) from old_basis to new_basis.
Parameters: coords – A array of coordinates to transform.
-
forgi.threedee.utilities.vector.
closest_point_on_seg
(seg_a, seg_b, circ_pos)[source]¶ Closest point between a line segment and a point.
Lifted from:
http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html
-
forgi.threedee.utilities.vector.
create_orthonormal_basis
(vec1, vec2=None, vec3=None)[source]¶ Create an orthonormal basis using the provided vectors.
If more than one is provided, it must be orthogonal to the others.
-
forgi.threedee.utilities.vector.
cylinder_line_intersection
(cyl, line, r)[source]¶ Get the points of intersection between a line and a cylinder.
If they do not intersect, return an empty list. If the line touches the cylinder, then return a 2 point list with two identical points. If the line crosses the cylinder, then return a list of 2 points.
-
forgi.threedee.utilities.vector.
elements_closer_than
(s1_p0, s1_p1, s2_p0, s2_p1, distance)[source]¶ Code copied from line_segment_distance, but with optimizations for fast comparison to distance.
Code shamelessly translated from: http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment
Parameters: - s1_p0 – The start of the first segment
- s1_p1 – The end of the first segment
- s2_p0 – The start of the second segment
- s2_p1 – The end of the second segment
Returns: True or False
-
forgi.threedee.utilities.vector.
get_alignment_matrix
(vec1, vec2)[source]¶ Return a rotation matrix that will align vec1 along vec2.
Parameters: - vec1 – The target vector.
- vec2 – The vector to be aligned.
-
forgi.threedee.utilities.vector.
get_double_alignment_matrix
(vp1, vp2)[source]¶ Align two sets of two vectors onto each other.
Parameters: - vp1 – A pair of two vectors.
- vp2 – Another pair of two vectors.
-
forgi.threedee.utilities.vector.
get_inter_distances
(vecs)[source]¶ Calculate all of the distances between the points of vecs.
Parameters: vecs – a list of vectors (=points) Returns: a list containing all distances between any two vectors in vecs.
-
forgi.threedee.utilities.vector.
get_non_colinear_unit_vector
(vec)[source]¶ Get a unit vector that does not lie on the line defined by vec.
This is done by creating a vector along the least represented axis in vec.
Parameters: vec – The vector under consideration. Returns: A vector along an axis.
-
forgi.threedee.utilities.vector.
get_orthogonal_unit_vector
(vec)[source]¶ Return a vector orthogonal to vec.
Note
To create a basis, use create_orthonormal_basis instead!
-
forgi.threedee.utilities.vector.
get_random_vector
(mult=1.0)[source]¶ Returns a random vector.
Parameters: mult – Stretch the random vector by this value. This is the longest value allowed for the total length. Returns: A random vector
-
forgi.threedee.utilities.vector.
get_standard_basis
(dim)[source]¶ Get a standard basis for the given dimension.
For 2D, this equals [[1.,0.],[0.,1.]]
Parameters: dim – The dimension of the vector space. Returns: A vector of vectors that constitute the standard basis.
-
forgi.threedee.utilities.vector.
get_vector_centroid
(crds1)[source]¶ Find the centroid of a set of vectors.
Parameters: crds – A matrix containing all of the vectors. Returns: The centroid of the rows of the matrix crds.
-
forgi.threedee.utilities.vector.
is_almost_parallel
(vec1, vec2)[source]¶ Are vec1 and vec2 almost parallel? Nothing is parallel to the zero vector!
Returns: 1 if vec1 and vec2 are parallel, -1 if they are antiparallel, 0 if they are neither.
-
forgi.threedee.utilities.vector.
line_segment_distance
(s1_p0, s1_p1, s2_p0, s2_p1)[source]¶ Calculate the two points on each of the segments that are closest to each other. The first segment is defined as p1->p2 and the second as p3->p4.
Code shamelessly translated from: http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm#dist3D_Segment_to_Segment
Parameters: - s1_p0 – The start of the first segment
- s1_p1 – The end of the first segment
- s2_p0 – The start of the second segment
- s2_p1 – The end of the second segment
Returns: A tuple of points (i1,i2) containing the point i1 on s1 closest to the point i2 on segment s2.
-
forgi.threedee.utilities.vector.
line_segments_collinearity
(segment1, segment2)[source]¶ Quantifies, how collinear (according to some measure) two line segments are.
Parameters: segment2 (segment1,) – Each a tuple of vectors (start, end)
-
forgi.threedee.utilities.vector.
magnitude
(vec)[source]¶ Return the magnitude of a vector
(|V|)
.This is guaranteed for arbitrary dimensions of vec.
Parameters: vec – The vector in question. Returns: The magnitude of the vector.
-
forgi.threedee.utilities.vector.
middlepoint
(vec1, vec2)[source]¶ The point in the middle between vec1 and vec2.
-
forgi.threedee.utilities.vector.
normalize
(vec)[source]¶ Normalize a vector so that its magnitude becomes 1.0 while its direction remains the same.
Parameters: vec – The vector in question. Returns: A normalized version of the vector.
-
forgi.threedee.utilities.vector.
pin_fits_two_cyl
(cyl1, cyl2, cyl_width)[source]¶ If we create a cone that starts at one end of cylinder1, does it enclose cylinder2?
This function approximates an answer by projecting a circle on the plane normal to the axis of cylinder1.
Parameters: - cyl1 – The coordinates of cylinder1
- cyl2 – The coordinates of cylinder2
- cyl_width – The widths of the two cylinders
Returns: True or False
-
forgi.threedee.utilities.vector.
point_in_cylinder
(pt1, pt2, r, testpt)[source]¶ Determine if testpt is within a cylinder of radius r.
Translated from:
Parameters: - pt1 – The start of the cylinder axis.
- pt2 – The end of the cylinder axis.
- r – The radius of the cylinder.
- testpt – The point we are testing.
Returns: True if the point is within the cylinder, False otherwise.
-
forgi.threedee.utilities.vector.
point_line_distance
(point, line_start, line_dir)[source]¶ Calculate the distance between the point and the line through line_point with direction line_dir.
Parameters: point – A point(an array with shape (3,)) or multiple points (shape n,3)
-
forgi.threedee.utilities.vector.
rotation_matrix
(axis, theta)[source]¶ Calculate the rotation matrix for a CLOCKWISE rotation of theta around axis. This is in the opposite direction that is usually used.
Thanks to unutbu on StackOverflow
http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
Parameters: - axis – The axis around which to rotate. A np-array with length 3 or one of “x”, “y” and “z”, where x:=standard_basis[0] etc.
- theta – The angle of rotation (in rad)
Returns: A matrix which can be used to perform the given rotation. The coordinates need only be multiplied by the matrix. (np.dot(matrix, vec))
-
forgi.threedee.utilities.vector.
seg_intersect
(line1, line2)[source]¶ Intersection of 2 line segments in 2D space (as lists or numpy array-like). :param line1: a tuple/list (a1, a2): The first line segment, from a1 to a2 :param line2: a tuple/list (b1, b2):The 2nd line segment, from b1 to b2
-
forgi.threedee.utilities.vector.
sortAlongLine
(start, end, points)[source]¶ Sort all points in points along the line from start to end.
Parameters: - start – A point
- end – A point
- points – A list of points
Returns: A list containing start, end and all elements of points, sorted by the distance from start
-
forgi.threedee.utilities.vector.
spherical_cartesian_to_polar
(vec)[source]¶ Return a parameterization of a vector of 3 coordinates:
x = r sin u cos v y = r sin u sin v z = r cos u
0 <= u <= pi -pi <= v <= pi
Where u is the polar angle and v is the azimuth angle.
Parameters: - vec – A vector of 3 cartesian coordinates.
- fast – Do not assert correctnes of result.
Returns: (r, u, v)
-
forgi.threedee.utilities.vector.
spherical_polar_to_cartesian
(vec)[source]¶ Convert spherical polar coordinates to cartesian coordinates:
See the definition of spherical_cartesian_to_polar.
Parameters: vec – A vector of the 3 polar coordinates (r, u, v) Returns: (x, y, z)
-
forgi.threedee.utilities.vector.
vec_angle
(vec1, vec2)[source]¶ Get the angle between two vectors using the identity:
A * B = |A||B| cos t
Where A and B are two vectors and t is the angle between themath.
Parameters: - vec1 – The first vector (A)
- vec2 – The second vector (B)
Returns: The angle between the two vectors.
-
forgi.threedee.utilities.vector.
vec_distance
(vec1, vec2)[source]¶ The (euclidean) distance between two points vec1 and vec2.
This is guaranteed to work for arbitrary but equal dimensions of vec1 and vec2.
Parameters: vec2 (vec1,) – A list or np.array of floats Returns: A float
-
forgi.threedee.utilities.vector.
vec_dot
(a, b)[source]¶ Vector dot product for vectors of length 3.
For small vectors of length 3 that are represented as lists and not as np.arary, this naive python implementation might be faster than the corresponding numpy implementation.
If a and b are already numpy arrays, the numpy implementation seems to be faster (depending an how numpy was compiled)
-
forgi.threedee.utilities.vector.
vector_rejection
(a, b)[source]¶ Return the vector rejection of a from b. In other words, return the orthogonal projection of a onto the plane orthogonal to b.
Parameters: - a – The vector to be projected.
- b – The vector defining the normal of the plane.
Returns: The rejection of the vector a from b. (a - (np.dot(a, b) / np.dot(b, b)) * b)