Calculate radius of curve/arc formed by line segments (approximate) - math

The calculation is for games so a approximation is better than a computational intense correct calculation. What i need to find is the radius of the given arc.
In this case the "curve" can be thought of a arc as this approximation is good enough. So the situation look like this:
I know:
the length the green lines (which are equal)
the length of the blue arc
the value of α in degrees
What i need to know:
the radius r
Background - Actually i need the radius for two things:
To calculate the length of a arc B with offset of x from the center arc. So that r of B would be r + x
To calculate the centrifugal force of a vehicle driving on that curve
What i tried:
I know how to calculate the radius if i have the circumference and the inner angle of the arc. But i am completely stuck with the given information though i am sure it should not be too complicated ..

If you think a triangle with right angle at the middle of the green line and another point at the center of the circle, then the angle at the intersection of the green segments in that triangle is α/2 and the cosine ratio for that angle is
cos(α/2)*r = g/2
g the length of a green segment.
The angle at the top of a pie slice is π-α so that for the length b of the blue curve segments you should get
b = (π-α)*r
The radius values you get from both formulas should not differ more than the measurement errors let expect.

Alternatively, you could use Catmull-Rom (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline), Hermite (https://en.wikipedia.org/wiki/Cubic_Hermite_spline), or natural (https://en.wikipedia.org/wiki/Spline_interpolation) cubic spline interpolation to calculate the path between the points.
This will give you cubic polynomials for the (x,y) coordinates, and it's easy to take their 2nd derivatives to get the direction and magnitude of acceleration.

Related

Distance to edge of ellipse given a vector

I've been trying to figure this out for a while and haven't found the answer.
Given:
Height of ellipse,
Width of ellipse,
Xposition of vector,
Ypostion of vector,
Direction of vector.
Find the distance to the edge of the circle
Here's a simple diagram:
Distance to the edge of a circle
I ran across this post: Calculate Point collision between a point of a given vector and the edge of a Circle But this for a circle, not an ellipse.
This is my first time posting on here. I would be very grateful for any help or pointers about this.
One fairly easy way of doing this is to represent both the ellipse and the vector in their cartesian forms
x^2/a^2 + y^2/b^2 = 1 where a & b are the lengths of the semi-major (half the width) and semi-minor (half the height) axes and the centre of the ellipse is assumed to be at (0,0)
and
y - ypos = m(x - xpos) where xpos and ypos are the position of your vector and m is the slope, the cosine of the angle (direction) it makes with the x axis.
Solve them together to get the intercept and use pythagoras to calculate the distance.
This assumes that the centre of the ellipse is at (0,0) and the axes are parallel to the x and y coordinate axes. If this is not the case then you would need a more general equation for the ellipse which is discussed in great detail here in Wikipedia.
As wierdan points out in his comment you can get 0,1 or 2 solutions.
0 if the vector starts outside the ellipse and misses it completely.
1 if the vector is a tangent to the ellipse.
2 if the vector either passes through the ellipse or it's start point is inside the ellipse.
In the case of 2 solutions 0,1 or 2 may be valid
If the vector direction points away from the ellipse then the solutions are for the reciprocal vector, the one pointing 180 degrees in the opposite direction. This may also apply to the tangent solution. So the solution(s) are not valid by your criteria.
If the start point is inside the ellipse then one solution will be for the result you want and the other for the reciprocal vector. So only one solution will be valid.
If the vector passes through the ellipse then both solutions are valid, your choice if you ignore the furthest one.

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.

How to quantize surface normals

I am trying to quantize surface normals into let's say 8 bins.
For example, when computing features like HOG to quantize 2D gradients [x,y] into 8 bins we just take the angle with the y plane i.e. arctan(y/x) which will give us an angle between 0-360.
My question is, given a 3D direction [x,y,z], a surface normal in this case, how can we histogram it in a similar way? Do we just project onto one plane and use that angle i.e. the dot product of [x,y,z] and [0,1,0] for example?
Thanks
EDIT
I also read a paper recently where they quantized surface normals by measuring angles between normal and precomputed vectors that which are arranged around a right circular cone shape. I have added a link to this paper in the question (section 3.3.2 last paragraph), is this an effective approach? And if so, how do we compute these vectors?
Quantizing a continuous topological space corresponds to partitioning it and assigning labels to each partition. The straightforward standard approach for this scenario (quantizing normals) is as follows.
Choose your favorite uniform polyhedron:
http://en.wikipedia.org/wiki/Tetrahedron (4 faces)
http://en.wikipedia.org/wiki/Cube (6 faces)
http://en.wikipedia.org/wiki/Octahedron (8 faces)
http://en.wikipedia.org/wiki/Dodecahedron (12 faces)
http://en.wikipedia.org/wiki/Icosahedron (20 faces)
In general: http://en.wikipedia.org/wiki/Schl%C3%A4fli_symbol
Develop a mapping function from a normal on the unit sphere to the face of your chosen polyhedron that the normal intersects.
I would advise doing an argmax across polyhedron faces, taking the dot product of your normal and each polyhedron face normal. The one that gives the highest dot product is the face your normal should be binned into.
Use the face normal for each polyhedron face as the label for that face.
Prefer this approach to the approach suggested by others of mapping to spherical coordinates and then binning those. That approach suffers from too much sensitivity near the poles of the sphere.
Edit
In the paper you added to your question, the same idea is being used. There, however, the normals are restricted to a hemisphere - the only surfaces directly visible in an image have surface normals no more than 90 degrees away from the vector from the surface to the viewpoint.
The paper wants to quantize these surface normals into 8 values, represented by 8-bit integers with exactly one bit set to 1 and the rest set to 0. The 8 precomputed normals are computed as:
ntx = cos(a)*cos(t)
nty = cos(a)*sin(t)
ntz = sin(a)
where a = pi/4 and t = 0, pi/4, 2*pi/4, 3*pi/4, ..., 7*pi/4.
Notice
[cos(a)*cos(t)]2 + [cos(a)*sin(t)]2 + [sin(a)]2 = cos2(a)[cos2(t) + sin2(t)] + sin2(a) = cos2(a) + sin2(a) = 1
given a 3D direction [x,y,z], a surface normal in this case, how can
we histogram it in a similar way?
In the first case you quantize the polar orientation theta of the gradients. Now you need to quantize the spherical orientations theta and phi in a 2D histogram.
Do we just project onto one plane and use that angle
The binning of the sphere determines how you summarize the information to build a compact yet descriptive histogram.
Projecting the normal is not a good idea, if theta is more important than phi, just use more bins for theta
EDIT
Timothy Shields points in his comment and his answer that a regular binning of theta and phi won't produce a regular binning over the sphere as the bins will be bunched toward the poles.
His answer gives a solution. Alternatively, the non-regular binning described here can be hacked as follows:
Phi is quantized regularly in [0,pi]. For theta rather than quantizing the range [0,pi], the range [-1,1] is quantized instead;
For each quantized value u in [-1,1], theta is computed as
theta = arcsin(sqrt(1 - u * u)) * sign(u)
sign(u) returns -1 if u is negative, 1 otherwise.
The computed theta along with phi produce a regular quantization over the sphere.
To have an idea of the equation given above look at this article. It describes the situation in the context of random sampling though.
EDIT
In the above hack Timothy Shields points out that only the area of the bins is considered. The valence of the vertices (point of intersection of neighboring bins) won't be regular because of the poles singularity.
A hack for the previous hack would be to remesh the bins into a regular quadrilateral mesh and keep the regular area.
A heuristic to optimize this problem with the global constraints of having the same valence and the area can be inspired from Integer-Grid Maps Quad Meshing.
With the two hacks, this answer is too hacky and a little out of context as opposed to Timothy Shields answer.
A 3-dimensional normal cannot be quantized into a 1-D array as easily as for a 2-D normal (e.g., using arctan). I would recommend histogramming it into a 2-d space with a polar angle and an azimuth angle. For example, use spherical coordinates where the r (radius) value is always 1.0 (since your surface normal is normalized, length 1.0). In this case, you can throw away the r-value and just use polar angle θ (theta), and azimuthal angle φ (phi) to quantize the 3D normal.

Translation coordinates for a circle under a certain angle

I have 2 circles that collide in a certain collision point and under a certain collision angle which I calculate using this formula :
C1(x1,y1) C2(x2,y2)
and the angle between the line uniting their centre and the x axis is
X = arctg (|y2 - y1| / |x2 - x1|)
and what I want is to translate the circle on top under the same angle that collided with the other circle. I mean with the angle X and I don't know what translation coordinates should I give for a proper and a straight translation!
For what I think you mean, here's how to do it cleanly.
Think in vectors.
Suppose the centre of the bottom circle has coordinates (x1,y1), and the centre of the top circle has coordinates (x2,y2). Then define two vectors
support = (x1,y1)
direction = (x2,y2) - (x1,y1)
now, the line between the two centres is fully described by the parametric representation
line = support + k*direction
with k any value in (-inf,+inf). At the initial time, substituting k=1 in the equation above indeed give the coordinates of the top circle. On some later time t, the value of k will have increased, and substituting that new value of k in the equation will give the new coordinates of the centre of the top circle.
How much k increases at value t is equal to the speed of the circle, and I leave that entirely up to you :)
Doing it this way, you never need to mess around with any angles and/or coordinate transformations etc. It even works in 3D (provided you add in z-coordinates everywhere).

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