How to determine a semi-sphere's point x-y-z coordinates? - computational-geometry

I'm having serious problems solving a problem illustrated on the pic below.
Let's say we have 3 points in 3D space (blue dots), and the some center of the triangle based on them (red dot - point P). We also have a normal to this triangle, so that we know which semi-space we talking about.
I need to determine, what is the position on a point (red ??? point) that depends on two angles, both in range of 0-180 degrees. Doesnt matter how the alfa=0 and betha=0 angle is "anchored", it is only important to be able to scan the whole semi-sphere (of radius r).
http://i.stack.imgur.com/a1h1B.png
If anybody could help me, I'd be really thankful.
Kind regards,
Rav

From the drawing it looks as if the position of the point on the sphere is given by a form of spherical coordinates. Let r be the radius of the sphere; let alpha be given relative to the x-axis; and let beta be the angle relative to the x-y-plane. The Cartesian coordinates of the point on the sphere are:
x = r * cos(beta) * cos(alpha)
y = r * cos(beta) * sin(alpha)
z = r * sin(beta)
Edit
But for a general coordinate frame with axes (L, M, N) centered at (X, Y, Z) the coordinates are (as in dmuir's answer):
(x, y, z) =
(X, Y, Z)
+ r * cos(beta) * cos(alpha) * L
+ r * cos(beta) * sin(alpha) * M
+ r * sin(beta) * N
The axes L and N must be orthogonal and M = cross(N, L). alpha is given relative to L, and beta is given relative to the L-M plane. If you don't know how L is related to points of the triangle, then the question can't be answered.

You need to find two unit length orthogonal vectors L, M say, in the plane of the triangle as well as the the unit normal N. The points on the sphere are
r*cos(beta)*cos(alpha) * L + r*cos(beta)*sin(alpha)*M + r*sin(beta)*N

Related

Find the center-coordinates of a rotated rectangle

I am having trouble with finding the X and Y-coordinates of the center of a rotated rectangle through mathematical calculations.
What I have is the width 'A' and height 'B' of a rectangle, as well as its rotation 'R' in degrees.
What would be the formula to calculate those points?
Here is a sketch to further explain what i am trying to do:
Any help is greatly appreciated! :)
if you are trying to do it in pure way, by your self
it think this answer would be great for you
https://math.stackexchange.com/questions/270194/how-to-find-the-vertices-angle-after-rotation
if you want to use a package for this, then I do recommend
flatten-js , geometric ,Ecluid.ts
I doubt that this is what you are after, but here is a solution to a possibly related question.
We assume that the leftmost corner lies on the Y axis, and the bottommost on the X axis. Thus, the coordinates of these corners are (0, B sin R) and (B cos R, 0) respectively. The midpoint of the bottom side is (B/2 cos R, B/2 sin R). To this we add a vector of length A/2 in the orthogonal direction and obtain the coordinates of the center:
(B/2 cos R - A/2 sin R, B/2 sin R + A/2 cos R)

How to Determine the Angle of Rotation for a Point

I have 2 points in an x, y plane. I want to rotate one point onto the other point by rotating it about the z-axis.
How can I find the angle to rotate one point onto the other?
Maybe the best thing is to get the angles from horizontal for the two points and then take the difference.
angle_1 = atan2( y_1, x_1 );
angle_2 = atan2( y_2, x_2 );
rotation_angle = angle_1-angle_2;
Well, the sin of this angle is [a, b] / (abs(a) * abs(b)) and its cos is (a, b) / (abs(a) * abs(b)), where [a, b] is a cross product of a and b, (a, b) is a scalar product and abs(x) is the length of the vector x. It is pretty easy to find an angle given its sin and cos.

Determine the coordinates of a segment

How can I use atan2 to determine the coordinates where a segment ends, knowing the coordinates where it starts (x, y), the angle of the segment and the length of the segment?
Can you tell me a solution it doesn't have to be only with atan2...
You have a right angled triangle and you can assume that the point A is at (x,y), this is much easier to explain with the use of some diagrams:
Now we know what the value for c is already because this is the length of the segment and we want to know the values for a and b because the final point is at (x+b, y+a)
From trigonometry we know what the relations between the angles and the ratios of the sides are
sin(theta) = opposite/hypotenuse and cos(theta) = adjacent/hypotenuse
Substituting in the values we know we get:
sin(theta) = opposite/c
c = opposite * sin(theta)
and
cos(theta) = adjacent/b
b = adjacent * cos(theta)
We know that the end point is at (x+b, y+a) which given the values for the edges we found is (x+adjacent * cos(theta), y+opposite * sin(theta))

Intersection points between 2 discs in 3d

i am trying to find a algorithm or a way to find the intersection between two circle on a sphere (in 3d). For example if i have two circles center at two pointsA(latitude1,longitude1) and B(latidude2,longitude2)
assuming that they intersect, how can i find the intersection between those two circles? is there an algorithm to do that?
Thank you
Convert from latitude/longitude to 3D Cartesian
coordinates.
For each circle, find the equation nx x + ny y + nz z = d of the
plane whose intersection with the sphere is the circle. Assuming
that the sphere is centered at the origin, the normal vector
(nx, ny, nz) is the circle center (cx, cy, cz) (projected or
not) after normalization.
(cx, cy, cz)
(nx, ny, nz) = -----------------
||(cx, cy, cz)||
2
The distance d is computed using Pythagoras. Let r be the radius
of the circle and R be the radius of the sphere.
2 2 2
R = d + r
_______ _______________
| 2 2 |
d = \|R - r = \|(R + r) (R - r)
The second expression is preferred for numerical stability.
If we know only the length r' on the surface of the sphere from
the projected center of the circle to the circle, then compute
d = R cos(r'/R)
r = R sin(r'/R).
We actually don’t need r in this case.
Find the intersection of the two planes, a
line.
Find the intersection of the line and the sphere, between zero and two
points.
Convert the points to
latitude/longitude.

Testing whether a line segment intersects a sphere

I am trying to determine whether a line segment (i.e. between two points) intersects a sphere. I am not interested in the position of the intersection, just whether or not the segment intersects the sphere surface. Does anyone have any suggestions as to what the most efficient algorithm for this would be? (I'm wondering if there are any algorithms that are simpler than the usual ray-sphere intersection algorithms, since I'm not interested in the intersection position)
If you are only interested if knowing if it intersects or not then your basic algorithm will look like this...
Consider you have the vector of your ray line, A -> B.
You know that the shortest distance between this vector and the centre of the sphere occurs at the intersection of your ray vector and a vector which is at 90 degrees to this which passes through the centre of the sphere.
You hence have two vectors, the equations of which fully completely defined. You can work out the intersection point of the vectors using linear algebra, and hence the length of the line (or more efficiently the square of the length of the line) and test if this is less than the radius (or the square of the radius) of your sphere.
I don't know what the standard way of doing it is, but if you only want to know IF it intersects, here is what I would do.
General rule ... avoid doing sqrt() or other costly operations. When possible, deal with the square of the radius.
Determine if the starting point is inside the radius of the sphere. If you know that this is never the case, then skip this step. If you are inside, your ray will intersect the sphere.
From here on, your starting point is outside the sphere.
Now, imagine the small box that will fit sphere. If you are outside that box, check the x-direction, y-direction and z-direction of the ray to see if it will intersect the side of the box that your ray starts at. This should be a simple sign check, or comparison against zero. If you are outside the and moving away from it, you will never intersect it.
From here on, you are in the more complicated phase. Your starting point is between the imaginary box and the sphere. You can get a simplified expression using calculus and geometry.
The gist of what you want to do is determine if the shortest distance between your ray and the sphere is less than radius of the sphere.
Let your ray be represented by (x0 + it, y0 + jt, z0 + kt), and the centre of your sphere be at (xS, yS, zS). So, we want to find t such that it would give the shortest of (xS - x0 - it, yS - y0 - jt, zS - z0 - kt).
Let x = xS - x0, y = yX - y0, z = zS - z0, D = magnitude of the vector squared
D = x^2 -2*xit + (i*t)^2 + y^2 - 2*yjt + (j*t)^2 + z^2 - 2*zkt + (k*t)^2
D = (i^2 + j^2 + k^2)t^2 - (xi + yj + zk)*2*t + (x^2 + y^2 + z^2)
dD/dt = 0 = 2*t*(i^2 + j^2 + k^2) - 2*(xi + yj + z*k)
t = (xi + yj + z*k) / (i^2 + j^2 + k^2)
Plug t back into the equation for D = .... If the result is less than or equal the square of the sphere's radius, you have an intersection. If it is greater, then there is no intersection.
This page has an exact solution for this problem. Essentially, you are substituting the equation for the line into the equation for the sphere, then computes the discriminant of the resulting quadratic. The values of the discriminant indicate intersection.
Are you still looking for an answer 13 years later? Here is a complete and simple solution
Assume the following:
the line segment is defined by endpoints as 3D vectors v1 and v2
the sphere is centered at vc with radius r
Ne define the three side lengths of a triangle ABC as:
A = v1-vc
B = v2-vc
C = v1-v2
If |A| < r or |B| < r, then we're done; the line segment intersects the sphere
After doing the check above, if the angle between A and B is acute, then we're done; the line segment does not intersect the sphere.
If neither of these conditions are met, then the line segment may or may not intersect the sphere. To find out, we just need to find H, which is the height of the triangle ABC taking C as the base. First we need φ, the angle between A and C:
φ = arccos( dot(A,C) / (|A||C|) )
and then solve for H:
sin(φ) = H/|A|
===> H = |A|sin(φ) = |A| sqrt(1 - (dot(A,C) / (|A||C|))^2)
and we are done. The result is
if H < r, then the line segment intersects the sphere
if H = r, then the line segment is tangent to the sphere
if H > r, then the line segment does not intersect the sphere
Here that is in Python:
import numpy as np
def unit_projection(v1, v2):
'''takes the dot product between v1, v2 after normalization'''
u1 = v1 / np.linalg.norm(v1)
u2 = v2 / np.linalg.norm(v2)
return np.dot(u1, u2)
def angle_between(v1, v2):
'''computes the angle between vectors v1 and v2'''
return np.arccos(np.clip(unit_projection(v1, v2), -1, 1))
def check_intersects_sphere(xa, ya, za, xb, yb, zb, xc, yc, zc, radius):
'''checks if a line segment intersects a sphere'''
v1 = np.array([xa, ya, za])
v2 = np.array([xb, yb, zb])
vc = np.array([xc, yc, zc])
A = v1 - vc
B = v2 - vc
C = v1 - v2
if(np.linalg.norm(A) < radius or np.linalg.norm(B) < radius):
return True
if(angle_between(A, B) < np.pi/2):
return False
H = np.linalg.norm(A) * np.sqrt(1 - unit_projection(A, C)**2)
if(H < radius):
return True
if(H >= radius):
return False
Note that I have written this so that it returns False when either endpoint is on the surface of the sphere, or when the line segment is tangent to the sphere, because it serves my purposes better.
This might be essentially what user Cruachan suggested. A comment there suggests that other answers are "too elaborate". There might be a more elegant way to implement this that uses more compact linear algebra operations and identities, but I suspect that the amount of actual compute required boils down to something like this. If someone sees somewhere to save some effort please do let us know.
Here is a test of the code. The figure below shows several trial line segments originating from a position (-1, 1, 1) , with a unit sphere at (1,1,1). Blue line segments have intersected, red have not.
And here is another figure which verifies that line segments that stop just short of the sphere's surface do not intersect, even if the infinite ray that they belong to does:
Here is the code that generates the image:
import matplotlib.pyplot as plt
radius = 1
xc, yc, zc = 1, 1, 1
xa, ya, za = xc-2, yc, zc
nx, ny, nz = 4, 4, 4
xx = np.linspace(xc-2, xc+2, nx)
yy = np.linspace(yc-2, yc+2, ny)
zz = np.linspace(zc-2, zc+2, nz)
n = nx * ny * nz
XX, YY, ZZ = np.meshgrid(xx, yy, zz)
xb, yb, zb = np.ravel(XX), np.ravel(YY), np.ravel(ZZ)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i in range(n):
if(xb[i] == xa): continue
intersects = check_intersects_sphere(xa, ya, za, xb[i], yb[i], zb[i], xc, yc, zc, radius)
color = ['r', 'b'][int(intersects)]
s = [0.3, 0.7][int(intersects)]
ax.plot([xa, xb[i]], [ya, yb[i]], [za, zb[i]], '-o', color=color, ms=s, lw=s, alpha=s/0.7)
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = np.outer(np.cos(u), np.sin(v)) + xc
y = np.outer(np.sin(u), np.sin(v)) + yc
z = np.outer(np.ones(np.size(u)), np.cos(v)) + zc
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='k', linewidth=0, alpha=0.25, zorder=0)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.tight_layout()
plt.show()
you sorta have to work that the position anyway if you want accuracy. The only way to improve speed algorithmically is to switch from ray-sphere intersection to ray-bounding-box intersection.
Or you could go deeper and try and improve sqrt and other inner function calls
http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection

Resources