How to calculate an orthogonal plane from a vector - linear-algebra

I have a position in space called X1. X1 has a velocity called V1. I need to construct an orthogonal plane perpendicular to the velocity vector. The origin of the plane is X1.
I need to turn the two edges from the plane into two vectors, E1 and E2. The edges connect at the origin. So the three vectors form an axis.
I'm using the GLM library for the vector mathematics.

One way to create a frame from a vector is to use Householder transformations. This may seem complicated but the code is in quite short, at least as efficient as using cross products, and less prone to rounding error. Moreover exactly the same idea works in any number of dimensions.
The ideas is, given a vector v, find a Householder transformation that maps v to a multiple of (1,0,0), and then apply the inverse of this to (0,1,0) and (0,0,1) to get the other frame vectors. Since a Householder transformation is it's own inverse, and since they are simple to apply, the resulting code is fairly efficient. Below is C code that I use:
static void make_frame( const double* v, double* f)
{
double lv = hypot( hypot( v[0], v[1]), v[2]); // length of v
double s = v[0] > 0.0 ? -1.0 : 1.0;
double h[3] = { v[0] - s*lv, v[1], v[2]}; // householder vector for Q
double a = 1.0/(lv*(lv + fabs( v[0]))); // == 2/(h'*h)
double b;
// first frame vector is v normalised
b = 1.0/lv;
f[3*0+0] = b*v[0]; f[3*0+1] = b*v[1]; f[3*0+2] = b*v[2];
// compute other frame vectors by applying Q to (0,1,0) and (0,0,1)
b = -v[1]*a;
f[3*1+0] = b*h[0]; f[3*1+1] = 1.0 + b*h[1]; f[3*1+2] = b*h[2];
b = -v[2]*a;
f[3*2+0] = h[0]*b; f[3*2+1] = b*h[1]; f[3*2+2] = 1.0 + b*h[2];
}

In general you can define a plane in 3D using four numbers, e.g., Ax+By+Cz=D. You can think of the triple of numbers (A,B,C) as a vector that sticks out perpendicularly to the plane (called the normal vector).
The normal vector n = (A,B,C) only defines the orientation of the plane, so depending on the choice of the constant D you get planes at different distance from the origin.
If I understand your question correctly, the plane you're looking for has normal vector (A,B,C) = V1 and the constant D is obtained using the dot product: D = (A,B,C) . X1, i.e., D = AX1.x + BX1.y + C*X1.z.
Note you can also obtain the same result using the geometric equation of a plane n . ((x,y,z) - p0) = 0, where p0 is some point on the plane, which in your case is V1 . ( (x,y,z) - X1) = 0.

Related

Finding closest vector to point

How can i sort vectors by distance from point?
For example i have three vectors: A, B, C and the point
Example image with point and vectors
And the sorted result must be something like this: (A, C, B)
Okay, this is more of a math question, but let me explain it here anyways. Take a look at this picture:
Let's define a line segment by vector A for the start point and a for the vector running through that line segment which end at the arrows end. Same is valid for the other segments B and C respectively. The point P as coordinates as also a vector.
Now let's make linear algebra our friend, yet be programatically efficient.
:-)
At the example of segment a you can do this and with the other respectively:
With the dot product of a and AP (vector from A to P) you get the projection projA on a where the where P is closest.
If you set A+ (projA)*na (na is the the normalized a vector) you get the closest Point in the vector a of P.
Let's set dA = A+projA*na - P and with its length you get the closest distance to compare.
Instead of saving the distances, try to store and compare squared distance of dA, dB and dC and compare those instead. It will save you to compute the square root which might become very expensive.
Here is some pseudocode:
vector3 AP = P-A;
vector3 projA = a.dot(AP);
vector3 nA = a.normalized();
dA = A + projA*na - P;
dA2 = dA.x*dA.x + dA.y*dA.y + dA.z*dA.z;
-> Compare and sort them by that value
Hope it helps a bit...

Check if 4 points in space are corner points of a rectangle

I have 4 points in space A(x,y,z), B(x,y,z), C(x,y,z) and D(x,y,z). How can I check if these points are the corner points of a rectangle?
You must first determine whether or not the points are all coplanar, since a rectangle is a 2D geometric object, but your points are in 3-space. You can determine they are coplanar by comparing cross products as in:
V1 = (B-A)×(B-C)
V2 = (C-A)×(C-D)
This will give you two vectors which, if A, B, C, and D are coplanar are linearly dependent. By considering what Wolfram has to say on vector dependence, we can test the vectors for linear dependence by using
C = (V1∙V1)(V2∙V2) - (V1∙V2)(V2∙V1)
If C is 0 then the vectors V1 and V2 are linearly dependent and all the points are coplanar.
Next compute the distances between each pair of points. There should be a total of 6 such distances.
D1 = |A-B|
D2 = |A-C|
D3 = |A-D|
D4 = |B-C|
D5 = |B-D|
D6 = |C-D|
Assuming none of these distances are 0, these points form a rectangle if and only if the vertices are coplanar (already verified) and these lengths can be grouped into three pairs where elements of each pair have the same length. If the figure is a square, two sets of the pairs will have be the same length and will be shorter than the remaining pair.
Update: Reading this again, I realize the the above could define a parallelogram, so an additional check is required to check that the square of the longest distance is equal to the sum of the squares of the two shorter distances. Only then will the parallelogram also be a rectangle.
Keep in mind all of this is assuming infinite precision and within a strictly mathematical construct. If you're planning to code this up, you will need to account for rounding and accept a degree of imprecision that's not really a player when speaking in purely mathematical terms.
Check if V1=B-A and V2=D-A are orthogonal using the dot product. Then check if
C-A == V1+V2
within numerical tolerances. If both are true, the points are coplanar and form a rectangle.
Here a function is defined to check whether the 4 points represents the rectangle or not .
from math import sqrt
def Verify(A, B, C, D, epsilon=0.0001):
# Verify A-B = D-C
zero = sqrt( (A[0]-B[0]+C[0]-D[0])**2 + (A[1]-B[1]+C[1]-D[1])**2 + (A[2]-B[2]+C[2]-D[2])**2 )
if zero > epsilon:
raise ValueError("Points do not form a parallelogram; C is at %g distance from where it should be" % zero)
# Verify (D-A).(B-A) = 0
zero = (D[0]-A[0])*(B[0]-A[0]) + (D[1]-A[1])*(B[1]-A[1]) + (D[2]-A[2])*(B[2]-A[2])
if abs(zero) > epsilon:
raise ValueError("Corner A is not a right angle; edge vector dot product is %g" % zero)
else:
print('rectangle')
A = [x1,y1,z1]
print(A)
B = [x2,y2,z2]
C = [x3,y3,z3]
D = [x4,y4,z4]
Verify(A, B, C, D, epsilon=0.0001)

Translate vertices from space to another

I have 3D-vertices that are read from a model which up-direction is defined by unit-vector u. I need to translate all those vertex points to another space which up-direction is defined by unit-vector v.
I know that if original up direction is (0,1,0) and target is (1,0,0), I can use rotation matrix somewhat like this for all the points:
x' = x*cos(-90) - y*sin(-90) = y
y' = x*sin(-90) + y*cos(-90) = -x
z' = z
where -90 is the angle from (0,1,0) to (1,0,0).
...but so far my tryouts to generalize this approach for the case mentioned in the begining have proven futile.
Any suggestions how to solve this?
To generalize this, you can use an angle/axis rotation specification where the rotation angle is given by θ = cos-1(u∙v / |u||v|) and the axis is given by r = (u/|u|)×(v/|v|) where ∙ is the dot product, × is the cross product and | | is the Euclidean (L2) norm. You can either use a quaternion formulation or a rotation matrix to actually generate the linear transformation.
Why this works: The dot product of two unit length vectors is the cosine of the angle between those two vectors. The cross product of two unit length 3D vectors is one of two unit length 3D vectors orthogonal (perpendicular) to the other two. The two vectors differ only in their sign such that u×v = -v×u, so the formulation above may have a sign issue if I got the order wrong in the cross product.
if your up vector is [ Ux, Uy, 0 ]:
x' = Ux*x + Uy*y
y' = Uy*x - Ux*y
z' = z
You could generalize this better and cover all arbitrary angles if you used Matrices. For example:
[ Ux, Uy, 0,
Uy,-Ux, 0,
0, 0, 1 ]
and simply multiply the vector by the matrix.
Or use three vectors for your orientation Up Right and Back then your matrix is:
[ Rx, Ry, Rz,
Ux, Uy, Uz,
Bx, By, Bz ]
and multiplying the vector by the matrix will be like this:
x' = x*Rx + y*Ry + z*Rz
y' = x*Ux + y*Uy + z*Uz
z' = x*Bx + y*By + z*Bz
In your particular case you have R=[1,0,0], U=[0,1,0] and B=[0,0,1] which you then reoriented to R=[0,-1,0], U=[1,0,0] and B=[0,0,1]

Bisector of two vectors in 2D (may be collinear)

How to find a bisecor b = (bx, by) of two vectors in general (we consider two non–zero vectors u = (ux, uy), v = (vx, vy), that may be collinear ).
For non-collinear vector we can write:
bx = ux/|u| + vx / |v|
by = uy/|u| + vy / |v|
But for collinear vectors
bx = by = 0.
Example:
u = (0 , 1)
v = (0, -1)
b = (0, 0)
A general and uniform approach is to get the angle of both vectors
theta_u = math.atan2(ux, uy)
theta_v = math.atan2(vx, vy)
and to create a new vector with the average angle:
middle_theta = (theta_u+theta_v)/2
(bx, by) = (cos(middle_theta), sin(middle_theta))
This way, you avoid the pitfall that you observed with opposite vectors.
PS: Note that there is an ambiguity in what the "bisector" vector is: there are generally two bisector vectors (typically one for the smaller angle and one for the larger angle). If you want the bisector vector inside the smaller angle, then your original formula is quite good; you may handle separately the special case that you observed for instance by taking a vector orthogonal to any of the two input vectors (-uy/|u|, ux/|u|) if your formula yields the null vector.
To find the unit bisection vectors of u and v.
if u/|u|+v/|v| !=0
first calculate the unit vector of u and v
then use the parallelogram rule to get the bisection (just add them)
since they both have unit of 1, their sum is the bisector vector
then calculate the unit vector of the calculated vector.
else (if u/|u|+v/|v| ==0):
(if you use the method above, it's like a indintermination: 0*infinity=?)
if you want the bisector of (u0v) if u/|u| = (cos(t),sin(t))
take b=(cost(t+Pi/2),sin(t+Pi/2)) = (-sin(t),cos(t) )as the bisector
therefore if u/|u|=(a1,a2) chose b=(-a2,a1)
Example:
u=(0,1)
v=(0,-1)
the bisector of (u0v):
b=(-1,0)

Convert a Unit Vector to a Quaternion

So I'm very new to quaternions, but I understand the basics of how to manipulate stuff with them. What I'm currently trying to do is compare a known quaternion to two absolute points in space. I'm hoping what I can do is simply convert the points into a second quaternion, giving me an easy way to compare the two.
What I've done so far is to turn the two points into a unit vector. From there I was hoping I could directly plug in the i j k into the imaginary portion of the quaternion with a scalar of zero. From there I could multiply one quaternion by the other's conjugate, resulting in a third quaternion. This third quaternion could be converted into an axis angle, giving me the degree by which the original two quaternions differ by.
Is this thought process correct? So it should just be [ 0 i j k ]. I may need to normalize the quaternion afterwards, but I'm not sure about that.
I have a bad feeling that it's not a direct mapping from a vector to a quaternion. I tried looking at converting the unit vector to an axis angle, but I'm not sure this would work, since I don't know what angle to give as an input.
Notation
Quaternions are defined in a four-space with bases {1, i, j, k}. Hamilton famously carved the fundamental relationship into the stone of the Brougham Bridge in Dublin:
i2 = j2 = k2 = i j k = -1.
There are many equivalent quaternion parameterizations, but here I'll use a {scalar, vector} form.
1.) A = {a0, a} and B = {b0, b}, where A and B are quaternions, a0 and b0 are scalars, and a and b are three-vectors.
2.) X = { 0, x } is a vector quaternion.
3.) The (non-commutative) quaternion product derives directly from the properties of i, j and k above, A⊗B = {a0 b0 - a.b, a0 b + b0 a + a x b}
4.) The quaternion conjugate is A* = {a0, - a}
5.) The conjugate of a quaternion product is the product of the conjugates in reverse order.
(A⊗B)* = B*⊗A*
6.) The conjugate of a vector quaternion is its negative. X* = {0, -x } = -X
7.) The quaternion norm is |A| = √(A⊗A*) = √( a0² + a.a )
8.) A unit quaternion is one that has a norm of 1.
9.) A unit three-vector x = {x1, x2, x3} with x . x = 1 is expressible as a unit vector quaternion X = { 0, x }, |X| = 1.
10.) The spherical rotation of a quaternion vector X by an angle θ about a unit vector axis n is Q⊗X⊗Q*,
where Q is the quaternion {cos(θ/2), sin(θ/2) n }. Note that |Q| = 1.
Notice the form of the quaternion vector product. Given vector quaternions X1 = { 0, x1 ) and X2 = { 0, x2 }, the quaternion product is X2⊗X1* = { x1.x2, x1 × x2 }. The quaternion reunites the dot product as the scalar part and cross product as the vector part, divorced over a hundred years ago. Neither of these products is invertible, but the quaternion is in the way described below.
Inversion
Find the spherical transform quaternion Q12 to rotate vector X1 to align with vector X2.
From above
X2 = Q12⊗X1⊗Q12*
Multiplying both sides by X1*,
X2⊗X1* = Q12⊗X1⊗(Q12*⊗X1*)
Remember that the rotation axis n derives from the cross product x1×x2, so n . x1 = 0. and Q*⊗X* = (X⊗Q)* = X*⊗Q, leaving
X2⊗X1* = Q12⊗X1⊗X1*⊗Q12 = Q12⊗Q12
So the quaternion transform can be solved directly as
Q12 = √(X2⊗X1*)
You're on your own for the quaternion square root. There are lots of ways to do it, and the best will depend on your application, considering speed and stability.
--hth,
Fred Klingener

Resources