Bounding rectangle for a triangle and normalized coordinates - vector

I'm working on some computer graphic stuff and can't figure out solution to one problem:
I have three points in 3D space, each defined with (x,y,z) tupple. Three points together form a triangle. Let point names be A, B and C, so triangle is ABC.
Now I'd like to form a bounding rectangle such that AC is one of the sides. and point B lies on the opposide side of the rectangle. Now I want to map an arbitrary point (p) within the rectangle to normalized UV coordinates, so:
U represents how far projection of the point p to AC vector is from A.
V represents the orthogonal part of the rectangle. A bit hard to explain, but see the picture.
Anyway, I think I'm good with finding U:
vec3 AC = vec3(C - A);
vec3 Ap = vec3(p - A);
float u = dot(Ap, AC)/dot(AC, AC);
But I can't figure out how to find V.
As I use GLSL, it's more than welcome if the answer will use matrix thingies like dot, cross or anything GLSL has.
Clarification picture

Find u for point B
uB = dot(AB, AC)/dot(AC, AC);
Projection of B onto AC:
B' = A + uB * AC / length(AC)
Now we have B'B vector and can make projections on it similar to projections on AC and calculate v

Related

How to get points coordinates on circle circumference with equal arcs length in unity

Let's highlight some points on this image.
N point coordinates (0, 0)
A point coordinates (3, 0)
The (ANB) angle = 30 degrees
AB distance = BC distance = CD distance
Now, I want to get the coordinates of B, C and D points.
I can get every point coordinates by trigonometry (Sin, Cos ...), But my question is
Is unity engine provide any method to get point coordinates which lies on circle if I know the start point and arc length to the point which i need to get its coordinates ?? i.e. if i know the length of AB arc -in upper image- and A point coordinates, Can unity gives me the B point coordinates !? (of course i meant without trigonometry).
This is exactly the kind of problem trigonometry was invented to solve; trying to do it without trigonometry is just silly. If you are starting at point (x,y) on a circle or radius r, and you want to rotate by an angle of a, then the new point is at
(cos(a + arccos(x/r)), sin(a + arcsin(y/r)))
And if you need to implement these trig functions yourself, read up on their Taylor series expansions.

Find the altitude of an irregular tetrahedron given all 4 vertex positions

I have an irregular tetrahedron using 4 vertices.
I need to find out the altitude given that one specific vertex is the top and the others are the base.
Basically the height would be the shortest distance from the top to its base creating a 90 degrees angle. It should be a simple math question but I cannot find anything on Google.
I am looking for an optimized function that looks like this :
float GetPyramidAltitude (Vector3 top, Vector3 baseA, Vector3 baseB, Vector3 baseC) {
...
}
Thank you for your help.
This is equivalent to finding the distance between a point and a plane. The plane is defined by the three points comprising the base. There is a detailed explanation on determining a plane given three points and finding the minimal distance between a point and a plane.
Disclaimer: I don't know Unity3D, so I'm kind of making up the syntax below. If something's not clear, let me know; otherwise you're going to have to translate into something that will compile using the Unity3D API.
The first step is to determine the equation of the plane given three points. The plane normal is given by:
n = cross(baseB-baseA, baseC-baseA);
n = n / norm(n);
Where cross returns the cross product of the two arguments, and norm returns the l2 norm (vector magnitude). The offset term in the plane equation is given by:
d = -n.x*baseA.x - n.y*baseA.y - n.z*baseA.z;
This will result in the plane equation:
n.x*X + n.y*Y + n.z*Z + d = 0
To find the distance between the top and the plane is then given by
D = dot(n, top) + d;
where dot is the dot product of the unit vector normal of the plane n and top and d is defined earlier. When D > 0 top is "above" the plane defined by the three base points where the normal points "up". When D < 0 the top is below the plane. So, in your case, you may want to take the absolute value of D to get the distance.
Thanks, however I found a solution from a method in Unity.
Basically we only need 3 parameters : the top vertex, one vertex from the base and the base's face normal which I already had.
Here's my solution :
float GetPyramidAltitude (Vector3 top, Vector3 baseA, Vector3 baseNormal) {
Vector3 topToBase = Vector3.Project(baseA - top, baseNormal);
return topToBase.magnitude;
}

Retrieve 2D co-ordinate from a 3D point on a 3D plane

I have a point a point (x, y, z) that is on a plane defined by ax+by+cz+d=0. I'm trying to figure out what the (x', y') relative to the plane, where it has a starting point of (x0, y0, z0) and the x'-axis is defined by (1,0) and the y'-axis is defined by (0,1).
My major goal is to have the mouse click on a surface, and know the 2D co-ordinates on a particular surface. I've managed to intersect the ray onto a plane quite trivially.
As a side-note, I'm using DirectX 9 - my familiarity with matrix/vector math is limited by the APIs provided to me through the D3DX libraries.
One thought I had was to use the angle of between one of the axis vectors and find the distance from origin, and figure out the x/y using simple trig. But I'm not sure if that's really an ideal solution or not - or if it can actually solve the issue at hand.
Since you have a 2D image on that plane, you apparently want to match its coordinate system. To do so, determine the unit vectors of the picture. That is, take the 3d coordinates B for the picture position (x,0) for any x>0, and subtract from that the 3d coordinates A for the origin (0,0) of the picture. The resulting vector B − A will describe the positive x direction of your image. Do the same for the y direction. Then normalize both these vectors. This means dividing them by their length, sqrt(x²+y²+z²), but D3Dx has a function D3DXVec3Normalize for this. Let's call the resulting 3d vectors X and Y. To compute the x and y coordinate of any 3D point p, simply subtract the origin A from p, i.e. compute the vector p − A. Then compute the dot product between the result and the unit vectors X and Y. This will give you two numbers: the desired coordinates. This is because the dot product can be used to compute an orthogonal projection.
Translating this into D3Dx, it should look somewhat like the following. As I have never used it, this might have mistakes.
D3DXVECTOR3 *p; // input point
D3DXVECTOR3 a, b, c, ab, ac, ap; // helper vectors
FLOAT x, y; // output coordinates
imagePosTo3D(&a, 0, 0); // a = origin of image
imagePosTo3D(&b, 1, 0); // b = anywhere on positive x axis, perhaps a corner
imagePosTo3D(&c, 0, 1); // c = anywhere on positive y axis, perhaps a corner
D3DXVec3Subtract(&ab, &b, &a); // ab = b - a
D3DXVec3Subtract(&ac, &c, &a); // ac = c - a
D3DXVec3Normalize(&ab, &ab); // ab = ab / |ab|
D3DXVec3Normalize(&ac, &ac); // ac = ac / |ac|
// the above has to be done once for the image, the code below for every p
D3DXVec3Subtract(&ap, p, &a); // ap = p - a
x = D3DXVec3Dot(&ab, &ap); // x = ab∙ap
y = D3DXVec3Dot(&ac, &ap); // y = ac∙ap

Exit angle and point of a segment that lies on a triangle in 3D

Knowing the vertices of a 3D triangle, and the x, y coordinates of the projection on the horizontal plane of a point E belonging to the triangle. Also the angle alpha is given, representing the angle respect to the edge AB of a segment that lies on the same plane of the triangle. I would like to find out 3 things:
for a given alpha, on which side is F
what is the angle created by the "exit" side with the segment EF (considering always the following vertex in a clockwise way)
the length of EF
The length of the segment BF if BC is the exit side (clockwise again)
This is though... and I want to see how it will perform.
Thank you.
grid http://www.keplero.com/upps/mesh.jpg
Find point E. Draw a line perpendicular to the horizontal plane, passing through E's projection. Point E is the intersection of that line and the plane the triangle lies on. (if the triangle's plane is perpendicular to the horizontal plane, you don't have enough information to find E.)
Perform a transformation on points A, B, C, E so that they lie on the horizontal plane. Use only rotations and translations so the angles and distances are preserved. With this step, the problem can be solved in only two dimensions, which simplifies things.
Draw a ray extending out from E, that has angle alpha with respect to AB.
For each of AB, BC, CA, determine whether the ray extending from E intersects it. Point F is the intersection of the ray and whichever line segment it intersects. (If the ray passes through a vertex of the triangle rather than an edge, then you may not be able to get meaningful answers your questions involving the "exit" side.)
Using the position of F, determine the answers of each of your bullet points.
Optionally, perform the reverse of the transformation done in step 2, to get the true position of F.

width of a frustum at a given distance from the near plane

I'm using CML to manage the 3D math in an OpenGL-based interface project I'm making for work. I need to know the width of the viewing frustum at a given distance from the eye point, which is kept as a part of a 4x4 matrix that represents the camera. My goal is to position gui objects along the apparent edge of the viewport, but at some distance into the screen from the near clipping plane.
CML has a function to extract the planes of the frustum, giving them back in Ax + By + Cz + D = 0 form. This frustum is perpendicular to the camera, which isn't necessarily aligned with the z axis of the perspective projection.
I'd like to extract x and z coordinates so as to pin graphical elements to the sides of the screen at different distances from the camera. What is the best way to go about doing it?
Thanks!
This seems to be a duplicate of Finding side length of a cross-section of a pyramid frustum/truncated pyramid, if you already have a cross-section of known width a known distance from the apex. If you don't have that and you want to derive the answer yourself you can follow these steps.
Take two adjacent planes and find
their line of intersection L1. You
can use the steps here. Really
what you need is the direction
vector of the line.
Take two more planes, one the same
as in the previous step, and find
their line of intersection L2.
Note that all planes of the form Ax + By + Cz + D = 0 go through the origin, so you know that L1 and L2
intersect.
Draw yourself a picture of the
direction vectors for L1 and L2,
tails at the origin. These form an
angle; call it theta. Find theta
using the formula for the angle
between two vectors, e.g. here.
Draw a bisector of that angle. Draw
a perpendicular to the bisector at
the distance d you want from the
origin (this creates an isosceles
triangle, bisected into two
congruent right triangles). The
length of the perpendicular is your
desired frustum width w. Note that w is
twice the length of one of the bases
of the right triangles.
Let r be the length of the
hypotenuses of the right triangles.
Then rcos(theta/2)=d and
rsin(theta/2)=w/2, so
tan(theta/2)=(w/2)/d which implies
w=2d*tan(theta/2). Since you know d
and theta, you are done.
Note that we have found the length of one side of a cross-section of a frustrum. This will work with any perpendicular cross-section of any frustum. This can be extended to adapt it to a non-perpendicular cross-section.

Resources