Calculate points on surface of 3d model - math

I have a 3d model (from Blender) with vertices, vertices normals (normalized) and faces (triangles). I need to calculate additional vertices and their normals. Other words, I need algorithm to calculate center vertex for triangle from three vertices and three vertices normals.
For example, in picture we have A, B, C vertices. How to calculate D vertex and it's normal?
Or, even better, point E (center of one of the sides).
Could anybody help me?

If you want point D lie exactly on plane based on ABC then I suggest you to use barycentric coordinates. Point D is intersection of medians and it is (1/3, 1/3, 1/3) in barycentric coordinates, or D = 1/3A + 1/3B + 1/3C, E would be (0,1/2,1/2). The normal ND should be calculated in the same way as D, ND = 1/3NA + 1/3NB + 1/3NC.
You didn't state the reason why do you need to calculate D and E. I suppose you want to get more triangles in the mesh, thus better level of detail. In this case PN-triangles should be used

Related

Find all 4 possible normals to an ellipse

Given a point p exterior to an axially aligned, origin centered ellipse E, find the (upto) four unique normals to E passing through p.
This is not a Mathematica question. Direct computation is too slow; I am willing to sacrifice precision and accuracy for speed.
I have searched the web, but all I found involved overly complex calculations which if implemented directly appear to lack the performance I need. Is there a more "programmatical" way to do this, like using matrices or scaling the ellipse into a circle?
Let's assume the ellipse E is in "standard position", center at the origin and axes parallel to the coordinate axes:
(x/a)^2 + (y/b)^2 = 1 where a > b > 0
The boundary cases a=b are circles, where the normal lines are simply ones that pass through the center (origin) and are thus easy to find. So we omit discussion of these cases.
The slope of the tangent to the ellipse at any point (x,y) may be found by implicit differentiation:
dy/dx = -(b^2 x)/(a^2 y)
For the line passing through (x,y) and a specified point p = (u,v) not on the ellipse, that is normal to ellipse E when its slope is the negative reciprocal of dy/dx:
(y-v)/(x-u) * (-b^2 x)/(a^2 y) = -1 (N)
which simplifies to:
(x - (1+g)u) * (y + gv) = -g(1+g)uv where g = b^2/(a^2 - b^2)
In this form we recognize it is the equation for a right rectangular hyperbola. Depending on how many points of intersection there are between the ellipse and the hyperbola (2,3,4), we have that many normals to E passing through p.
By reflected symmetry, if p is assumed exterior to E, we may take p to be in the first quadrant:
(u/a)^2 + (v/b)^2 > 1 (exterior to E)
u,v > 0 (1'st quadrant)
We could have boundary cases where u=0 or v=0, i.e. point p lies on an axis of E, but these cases may be reduced to solving a quadratic, because two normals are the (coinciding) lines through the endpoints of that axis. We defer further discussion of these special cases for the moment.
Here's an illustration with a=u=5,b=v=3 in which only one branch of the hyperbola intersects E, and there will be only two normals:
If the system of two equations in two unknowns (x,y) is reduced to one equation in one unknown, the simplest root-finding method to code is a bisection method, but knowing something about the possible locations of roots/intersections will expedite our search. The intersection in the first quadrant is the nearest point of E to p, and likewise the intersection in the third quadrant is the farthest point of E from p. If the point p were a good bit closer to the upper endpoint of the minor axis, the branches of the hyperbola would shift together enough to create up to two more points of intersection in the fourth quadrant.
One approach would be to parameterize E by points of intersection with the x-axis. The lines from p normal to the ellipse must intersect the major axis which is a finite interval [-a,+a]. We can test both the upper and lower points of intersection q=(x,y) of a line passing through p=(u,v) and (z,0) as z sweeps from -a to +a, looking for places where the ellipse and hyperbola intersect.
In more detail:
1. Find the upper and lower points `q` of intersection of E with the
line through `p` and `(z,0)` (amounts to solving a quadratic)
3. Check the sign of a^2 y(x-u) - b^2 x(y-v) at `q=(x,y)`, because it
is zero if and only `q` is a point of normal intersection
Once a subinterval is detected (either for upper or lower portion) where the sign changes, it can be refined to get the desired accuracy. If only modest accuracy is needed, there may be no need to use faster root finding methods, but even if they are needed, having a short subinterval that isolates a root (or root pair in the fourth quadrant) will be useful.
** more to come comparing convergence of various methods **
I had to solve a problem similar to this, for GPS initialization. The question is: what is the latitude of a point interior to the Earth, especially near the center, and is it single-valued? There are lots of methods for converting ECEF cartesian coordinates to geodetic latitude, longitude and altitude (look up "ECEF to Geodetic"). We use a fast one with only one divide and sqrt per iteration, instead of several trig evaluations like most methods, but since I can't find it in the wild, I can't give it to you here. I would start with Lin and Wang's method, since it only uses divisions in its iterations. Here is a plot of the ellipsoid surface normals to points within 100 km of Earth's center (North is up in the diagram, which is really ECEF Z, not Y):
The star-shaped "caustic" in the figure center traces the center of curvature of the WGS-84 ellipsoid as latitude is varied from pole to equator. Note that the center of curvature at the poles is on the opposite side of the equator, due to polar flattening, and that the center of curvature at the equator is nearer to the surface than the axis of rotation.
Wherever lines cross, there is more than one latitude for that cartesian position. The green circle shows where our algorithm was struggling. If you consider that I cut off these normal vectors where they reach the axis, you would have even more normals for a given position for the problem considered in this SO thread. You would have 4 latitudes / normals inside the caustic, and 2 outside.
The problem can be expressed as the solution of a cubic equation which
gives 1, 2, or 3 real roots. For the derivation and closed form
solution see Appendix B of Geodesics on an ellipsoid of revolution. The boundary between 1 and 3 solutions is an astroid.

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.

How to solve a symbolic non-linear vector equation? (Matlab or other)

I'm trying to find a solution to this symbolic non-linear vector equation:
P = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) for a, b and t
where P, V0, V1, V2, P0, P1, P2 are known 3d vectors.
I attempted to do that in Matlab like this:
P = sym('P', [3,1])
P0 = sym('P0', [3,1])
P1 = sym('P1', [3,1])
P2 = sym('P2', [3,1])
V0 = sym('V0', [3,1])
V1 = sym('V1', [3,1])
V2 = sym('V2', [3,1])
syms a b t
F = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) - P
solve(F,a,b,t)
I get
Warning: Explicit solution could not be found.
I'm starting to run out of ideas how to solve it, this isn't the first math package I tried.
The interesting bit is that this equation has a simple geometrical interpretation. If you imagine that points P0-P2 are vertices of a triangle, V0-V2 are roughly vertex normals and point P lies above the triangle, then the equation is satisfied for a triangle containing point P with it's three vertices on the three rays (V*t+P), sharing the same parameter t value. a, b and (1-a-b) become the barycentric coordinates of the point P.
So if the case is not degenerate, there should be only one well defined solution for t.
As symbolic equation, this one has 3 variables, so there is no way to have a single solution.
Imagine you pick any values for say b and t. Then in almost all cases you can solve for a, so you get many different solutions.
If you want to think geometrically, imagine that V0 and V1 point in the upper half-space regarding the (P0,P1,P2) triangle, but V2 point in the lower. Also V0,V1 are perpendicular to the plane of the triangle and V0 and V1 are unit vectors.
Now if you have a plane pinned at the point P, which intersects the rays P0+t*V0 and P1+t*V1 at the same distance above the triangle, you can move the plane in such a way that it stays pinned at P and intersecting the two rays at the same distance. It's only a matter of having had picked V2 in such a way that the point of intersection with this plane moves at the same velocity, so it will correspond to the same t, thus giving you infinitely many solutions.
Another example would be if all V0-V2 were colinear with the triangle P0,P1,P2. Then you trivially get a solution for any t.
So you need more equations to solve this symbolically.

Circle to tangent mapping

Assume you're given a circle with the line AB containing its center O, such that A and B are on the circle (OA=OB=radius). A tangent t is drawn on the point A, and
I should calculate the mapping of certain points (a,b,c,d...) of the circle to the points on the tangent (at, bt, ct, dt, ...) such that the distance Aa (the distance along the circle) is the same as the distance Aat (the distance along the tangent) (and the same for the distances Ab, Ac, Ad). But, here, certain constraint should be considered: those points of the circle (among (a, b, c, d)) that are from one side of the circle from A to B should be placed on one side of the tangent (the nearer), and those from the other side of the circle form A to B should be placed on the other side. Basically, the circle should be split at B, and then mapped to the tangent. I hope this explanation is sufficient enough.
It should be noted that I have information about coordinates of A, B, O, a, b, c, d. I supposed to calculate (at, bt, ct, dt).
For solving this problem, I have two approaches, but I'm not sure how I could make sure they always work correctly.
1) I calculate the equation of the tangent at point A. Then for each point (a, b, c, d) I calculate the distance from A (along the circle), and use these distances for calculating (at, bt, ct, dt...) along the tangent. What I dont know here is how to calculate the distances
from A to (a, b, c, d). The problem is the 'proper side' determination, meaning how should I determine whether the point should be mapped on one side of the tangent or the other. What would be the way to determine this.
2) I calculate the equation of the tangent at point A. Then for each point (a, b, c, d) I calculate the distance from A (along the circle), and use these distances for calculating (at, bt, ct, dt...) along the tangent. To determine the 'proper side' of a given point, I might use the projection of that point to the tangent. But, even with this, how I know 'which side is which'? Perhaps there are much simpler ways to do this.
Any suggestion on how to do this is welcome. In case I was not precise enough, I'll elaborate.
A better suggestion would be to calculate a coordinate transformation that would map the circle into a unit circle with the centre at the origin, so that A will have coordinates (1, 0) (and B respectively (-1, 0)).
The transformation should be dilation with rotation.
Now, the distance on Aa is just the angle aOA measured in radians. So you can easily calculate at, it is (1, atan2(y, x)) where (x, y) are the coordinates of a.
Now, the only thing you need is to return to the original coordinate system, applying the inverse transformation.
To determine which "side" of the circle you're on, you basically need to determine which side of the line AB you're on. For the answer to that, see e.g. Determine which side of a line a point lies.

Finding intersection points between 3 spheres

I'm looking for an algorithm to find the common intersection points between 3 spheres.
Baring a complete algorithm, a thorough/detailed description of the math would be greatly helpful.
This is the only helpful resource I have found so far:
http://mathforum.org/library/drmath/view/63138.html
But neither method described there is detailed enough for me to write an algorithm on.
I would prefer the purely algebraic method described in the second post, but what ever works.
Here is an answer in Python I just ported from the Wikipedia article. There is no need for an algorithm; there is a closed form solution.
import numpy
from numpy import sqrt, dot, cross
from numpy.linalg import norm
# Find the intersection of three spheres
# P1,P2,P3 are the centers, r1,r2,r3 are the radii
# Implementaton based on Wikipedia Trilateration article.
def trilaterate(P1,P2,P3,r1,r2,r3):
temp1 = P2-P1
e_x = temp1/norm(temp1)
temp2 = P3-P1
i = dot(e_x,temp2)
temp3 = temp2 - i*e_x
e_y = temp3/norm(temp3)
e_z = cross(e_x,e_y)
d = norm(P2-P1)
j = dot(e_y,temp2)
x = (r1*r1 - r2*r2 + d*d) / (2*d)
y = (r1*r1 - r3*r3 -2*i*x + i*i + j*j) / (2*j)
temp4 = r1*r1 - x*x - y*y
if temp4<0:
raise Exception("The three spheres do not intersect!");
z = sqrt(temp4)
p_12_a = P1 + x*e_x + y*e_y + z*e_z
p_12_b = P1 + x*e_x + y*e_y - z*e_z
return p_12_a,p_12_b
Probably easier than constructing 3D circles, because working mainly on lines and planes:
For each pair of spheres, get the equation of the plane containing their intersection circle, by subtracting the spheres equations (each of the form X^2+Y^2+Z^2+aX+bY+c*Z+d=0). Then you will have three planes P12 P23 P31.
These planes have a common line L, perpendicular to the plane Q by the three centers of the spheres. The two points you are looking for are on this line. The middle of the points is the intersection H between L and Q.
To implement this:
compute the equations of P12 P23 P32 (difference of sphere equations)
compute the equation of Q (solve a linear system, or compute a cross product)
compute the coordinates of point H intersection of these four planes. (solve a linear system)
get the normal vector U to Q from its equation (normalize a vector)
compute the distance t between H and a solution X: t^2=R1^2-HC1^2, (C1,R1) are center and radius of the first sphere.
solutions are H+tU and H-tU
A Cabri 3D construction showing the various planes and line L
UPDATE
An implementation of this answer in python complete with an example of usage can be found at this github repo.
It turns out the analytic solution is actually quite nice using this method and can tell you when a solution exists and when it doesn't (it is also possible to have exactly one solution.) There is no reason to use Newton's method.
IMHO, this is far easier to understand and simpler than trilateration given below. However, both techniques give correct answers in my testing.
ORIGINAL ANSWER
Consider the intersection of two spheres. To visualize it, consider the 3D line segment N connecting the two centers of the spheres. Consider this cross section
(source: googlepages.com)
where the red-line is the cross section of the plane with normal N. By symmetry, you can rotate this cross-section from any angle, and the red line segments length can not change. This means that the resulting curve of the intersection of two spheres is a circle, and must lie in a plane with normal N.
That being said, lets get onto finding the intersection. First, we want to describe the resulting circle of the intersection of two spheres. You can not do this with 1 equation, a circle in 3D is essentially a curve in 3D and you cannot describe curves in 3D by 1 eq.
Consider the picture
(source: googlepages.com)
let P be the point of intersection of the blue and red line. Let h be the length of the line segment along the red line from point P upwards. Let the distance between the two centers be denoted by d. Let x be the distance from the small circle center to P. Then we must have
x^2 +h^2 = r1^2
(d-x)^2 +h^2 = r2^2
==> h = sqrt(r1^2 - 1/d^2*(r1^2-r2^2+d^2)^2)
i.e. you can solve for h, which is the radius of the circle of intersection. You can find the center point C of the circle from x, along the line N that joins the 2 circle centers.
Then you can fully describe the circle as (X,C,U,V are all vector)
X = C + (h * cos t) U + (h * sin t) V for t in [0,2*PI)
where U and V are perpendicular vectors that lie in a plane with normal N.
The last part is the easiest. It remains only to find the intersection of this circle with the final sphere. This is simply a plug and chug of the equations (plug in for x,y,z in the last equation the parametric forms of x,y,z for the circle in terms of t and solve for t.)
edit ---
The equation that you will get is actually quite ugly, you will have a whole bunch of sine's and cosine's equal to something. To solve this you can do it 2 ways:
write the cosine's and sine's in terms of exponentials using the equality
e^(it) = cos t + i sin t
then group all the e^(it) terms and you should get a quadratic equations of e^(it)'s
that you can solve for using the quadratic formula, then solve for t. This will give you the exact solution. This method will actually tell you exactly if a solution exists, two exist or one exist depending on how many of the points from the quadratic method are real.
use newton's method to solve for t, this method is not exact but its computationally much easier to understand, and it will work very well for this case.
Basically you need to do this in 3 steps. Let's say you've got three spheres, S1, S2, and S3.
C12 is the circle created by the intersection of S1 and S2.
C23 is the circle created by the intersection of S2 and S3.
P1, P2, are the intersection points of C12 and C13.
The only really hard part in here is the sphere intersection, and thankfully Mathworld has that solved pretty well. In fact, Mathworld also has the solution to the circle intersections.
From this information you should be able to create an algorithm.
after searching the web this is one of the first hits, so i am posting the most clean and easy solution i found after some hours of research here: Trilateration
This wiki site contains a full description of a fast and easy to understand vector approach, so one can code it with little effort.
Here is another interpretation of the picture which Eric posted above:
Let H be the plane spanned by the centers of the three spheres. Let C1,C2,C3 be the intersections of the spheres with H, then C1,C2,C3 are circles. Let Lij be the line connecting the two intersection points of Ci and Cj, then the three lines L12,L23,L13 intersect at one point P. Let M be the line orthogonal to H through P, then your two points of intersection lie on the line M; hence you just need to intersect M with either of the spheres.

Resources