Estimate torsion for a discrete curve using four points - geometry

The curvature of a discrete space curve can be calculated using 3 successive points can be calculated using the Menger curvature (see https://en.wikipedia.org/wiki/Menger_curvature and Calculate curvature for 3 Points (x,y)).
My question is: is there a similar explicit formula for the torsion (https://en.wikipedia.org/wiki/Torsion_of_a_curve or ) using four 4 successive points?
If not an explicit formula, does someone know of an algorithm/package for calculating it? I work in python, but anything will do.
I can imagine the basic steps. Two successive vectors define a plane, and thus 3 successive vectors define two planes. The change in angle between the plane normals is proportional to the torsion. But I need an exact formula, with the calculated torsion having the proper dimension of 1/length^2.

Having some parametrization of curve r(t) (for example, by length of polyline chain) you can calculate three derivatives using 4 points: r', r'', r'''.
Then torsion is:
v = r' x r'' //(vector product)
torsion = (r''' .dot. v) / (v.dot.v) //.dot. is scalar product

Related

Calculate distance between colors in HSV space

I intend to find a distance metric between two colours in HSV space.
Suppose that each colour element has 3 components: hue, saturation, and value. Hue is ranged between 0 to 360, saturation is ranged between 0 to 1, and value is ranged between 0 to 255.
Also hue has a circular property, for example, 359 in hue is closer to 0 in hue value than 10 in hue.
Can anyone provide a good metric to calculate the distance between 2 colour element in HSV space here?
First a short warning: Computing the distance of colors does not make sense (in most cases). Without considering the results of 50 years of research in Colorimetry, things like the CIECAM02 Color Space or perceptual linearity of distance measures, the result of such a distance measure will be counterintuitive. Colors that are "similar" according to your distance measure will appear "very different" to a viewer, and other colors, that have a large "distance" will be undistinguishable by viewers. However...
The actual question seems to aim mainly at the "Hue" part, which is a value between 0 and 360. And in fact, the values of 0 and 360 are the same - they both represent "red", as shown in this image:
Now, computing the difference of two of these values boils down to computing the distance of two points on a circle with a circumference of 360. You already know that the values are strictly in the range [0,360). If you did not know that, you would have to use the Floating-Point Modulo Operation to bring them into this range.
Then, you can compute the distance between these hue values, h0 and h1, as
hueDistance = min(abs(h1-h0), 360-abs(h1-h0));
Imagine this as painting both points on a circle, and picking the smaller "piece of the cake" that they describe - that is, the distance between them either in clockwise or in counterclockwise order.
EDIT Extended for the comment:
The "Hue" elements are in the range [0,360]. With the above formula, you can compute a distance between two hues. This distance is in the range [0,180]. Dividing the distance by 180.0 will result in a value in [0,1]
The "Saturation" elements are in the range [0,1]. The (absolute) difference between two saturations will also be in the range [0,1].
The "Value" elements are in the range [0,255]. The absolute difference between two values will thus be in the range [0,255] as well. Dividing this difference by 255.0 will result in a value in [0,1].
So imagine you have two HSV tuples. Call them (h0,s0,v0) and (h1,s1,v1). Then you can compute the distances as follows:
dh = min(abs(h1-h0), 360-abs(h1-h0)) / 180.0
ds = abs(s1-s0)
dv = abs(v1-v0) / 255.0
Each of these values will be in the range [0,1]. You can compute the length of this tuple:
distance = sqrt(dh*dh+ds*ds+dv*dv)
and this distance will be a metric for the HSV space.
Given hsv values, normalized to be in the ranges [0, 2pi), [0, 1], [0, 1], this formula will project the colors into the HSV cone and give you the squared (cartesian) distance in that cone:
( sin(h1)*s1*v1 - sin(h2)*s2*v2 )^2
+ ( cos(h1)*s1*v1 - cos(h2)*s2*v2 )^2
+ ( v1 - v2 )^2
In case you are looking for checking just the hue, Marco's answer will do it. However, for a more accurate comparison considering hue, saturation and value, Sean's answer is the right one.
You can't simply check the distance from hue, saturation and value equally, because hue is a circle, not a normal vector. It's not like RGB where red, green and blue are vectors
PS: I know I am not giving any new solutions with this post, but Sean's answer really saved me and I wanted to acknowledge it besides upvoting since it is not the top answer here.
Lets start with:
c0 = HSV( h0, s0, v0 )
c1 = HSV( h1, s1, v1 )
Here are two more solutions:
(Helix Length)Find the length of curve in euclidean space:
x = ( s0+t*(s1-s0) ) * cos( h0+t*( h1-h0 ) )
y = ( s0+t*(s1-s0) ) * sin( h0+t*( h1-h0 ) )
z = ( v0+t*(v1-v0) )
t goes from 0 to 1.
Note: h1-h0 is not just subtraction it is modulus subtraction.
This can be optimized by rotation and then use: h0=0, and h1 = min(abs(h1-h0), 360-abs(h1-h0))
(Helix Length over RGB)Same as above but convert above curve in to RGB instead to euclidean space then calculate arc length.
And again convex combination by coordinate of HSV colors, each point on HSV-line convert to RGB.
Calculate the length of that line in RGB space with euclidean norm.
helix_rgb( t ) = RGB( HSV( h0+t*( h1-h0 ), s0+t*(s1-s0), v0+t*(v1-v0) ) )
t goes from 0 to 1.
Note: h1-h0 is not just subtraction it is (more than) modulus subtraction e.g.
D(HSV(300,50,50),HSV(10,50,50)) = D(HSV(300,50,50),HSV( 0,50,50)) + D(HSV( 0,50,50), HSV(10,50,50))
Comparison of metrics:
RGB(0,1,0) is referent point and calculate distance to color in right-down corner image.
Color image is generated by rule HSL([0-360], 100, [1-100] ).
EM is short from Euclid with Modulo as Marco13 propose with Sean Gerrish's scale.
Comparison of solutions over HSI, HSL and HSV, there are also distance in RGB and CIE76(LAB).
Comparing EM to other solutions like Helix len, RGB2RGB, CIE76 appears that EM give acceptable result at very low cost.
In https://github.com/dmilos/color.git it is implemented EM with arbitrary scaling.
Example:
typedef ::color::hsv<double> color_t; // or HSI, HSL
color_t A = ::color::constant::orange_t{}; \
color_t B = ::color::constant::lime_t{}; \
auto distance = ::color::operation::distance<::color::constant::distance::hue_euclid_entity>( A, B, 3.1415926/* pi is default */ );

Formula for calculating Euclidian direction in Excel

I am calculating Euclidian distance between points in an Excel application, and also need to be able to specify the direction of the difference in two-dimensional location for each pair of points.
Does anyone know how to implement this in Excel?
Below is a simplified illustration of my current Euclidian distance calculation. I have two points, and calculate how far apart Point1 is from Point2. But I would also like to find the direction (in degrees preferably) between Point1 and Point2.
For direction, you could use the angle that the vector from point one to point two makes with respect to the positive x axis:
=DEGREES(ATAN2(B3-B2,C3-C2))
this will return a number between -180 and +180 degrees. The ATAN2 function is given by ATAN2(x,y) = arctan(y/x) with the refinement that it returns pi/2 rather than a division by 0 error if x = 0 and also gives an answer in the appropriate quadrant.

finding vector normal - back face algorithm

for back face culling algorithm I need to find the normal vector for each polygon.
given 3 points, I want to find the normal of a plane.
so I know how to do it :
find 2 vectors on the plane
find their cross vector - which will give me the normal vector (a,b,c)
my question is, does it matter what is the order of the points when I find 2 vectors?
for ex: given 3 points: p1(0,0,0), p2(5,0,0), p3(10,10,10)
does it matter if I choose vector
V1=(p2-p1)=(5, 0, 0)-(0, 0, 0)=(5, 0, 0)
V2=(p3-p1)=(10,10,10)-(0, 0, 0)=(10, 10, 10)
or
v1=(p1-p2)
v2=(p1-p3)
your polygon has vertexes a, b, c.
you calculate the vectors:
v1 = a-c
v2 = b-c
this refers a and b to c. It would be the same if you decided to refer, say, b and c to a.
calculate the cross product v1*v2 (this gives a vector perpendicular to v1 and v2) and normalize it.
If, you did calculate (a-b)(a-c) instead of (b-a)(c-a), the resulting vector will be mirrored (ie, pointing to the wrong direction).
OT: normalize with/see http://en.wikipedia.org/wiki/Fast_inverse_square_root that was developed exactly to calculate face normals

Calculating Coords of a point Perpendicular to a Line, Calculating Coords of Third Point in Angle Given Two Points and Angle

I actually have two questions, I found the answer to the second and didn't update the diagram. I'm not actually sure if these are possible, they really stumped me.
Question 1:
Given point A and e, the angle of the line A is on relative to the x-axis where 0<=e<360 degrees, how do you calculate the coordinates of B? BA is perpendicular to A's line and 1 unit long.
SOLVED: I start by taking the unit vector from a parallel to the x-axis and then I rotate it 90 + e degrees.
Question 2:
I'm using this approach. If anyone has any better suggestions, please let me know.
SOLVED: I find the dot product of the vector from step 1 and the normalized vector AC.
Question 3:
This one should be pretty self-explanatory from the diagram. I need to find the coordinates of C given A, B, the angle of BAC and the distance between A and C.
SOLVED: I rotate BA e degrees and then change the magnitude to d.
If anyone spots problems with my solutions, please comment.
Easy if you understand vectors. Learn about how 2D vectors work and you'll have it. Is that the course you're taking?
Take the unit vector from e to A, knowing that a unit vector has length 1. Assume l1 = xi + yj. The perpendicular vector has components that are the reverse of l1 with one sign changed. In this case, l2 = -yi + xj.
Take the vector l2 that you got from the first problem and transform it as follows:
cx = -cos(t)y - sin(t)x
cy = +sin(t)y + cos(t)x
where t is the rotation angle in radians.
I'll leave the third one for you. Read about 2D vectors and transformations.

How can I find the 3D coordinates of a projected rectangle?

I have the following problem which is mainly algorithmic.
Let ABCD be a rectangle with known dimensions d1, d2 lying somewhere in space.
The rectangle ABCD is projected on a plane P (forming in the general case a trapezium KLMN). I know the projection matrix H.
I can also find the 2D coordinates of the trapezium edge points K,L,M,N.
The Question is the following :
Given the Projection Matrix H, The coordinates of the edges on the trapezium and the knowledge that our object is a rectangle with specified geometry (dimensions d1, d2), could we calculate the 3D coordinates of the points A, B, C, D ?
I am grabbing images of simple rectangles with a single camera and i want to reconstruct the rectangles on space. I could grab more than one image and use triangulation but this is not desired.
The projection Matrix alone isn't enough since a ray is projected to the same point. The fact that the object has known dimensions, makes me believe that the problem is solvable and there are finite solutions.
If I figure out how this reconstruction can be made I know how to program it. So I am asking for an algorithmic/math answer.
Any ideas are welcome
Thanks
You need to calculate the inverse of your projection matrix. (your matrix cannot be singular)
I'm going to give a fairly brief answer here, but I think you'll get my general drift. I'm assuming you have a 3x4 projection matrix (P), so you should be able to get the camera centre by finding the right null vector of P: call it C.
Once you have C, you'll be able to compute rays with the same direction as vectors CK,CL,CM and CN (i.e. the cross product of C and K,L,M or N, e.g. CxK)
Now all you have to do is compute 3 points (u1,u2,u3) which satisfies the following 6 constraints (arbitrarily assuming KL and KN are adjacent and ||KL|| >= ||KN|| if d1 >= d2):
u1 lies on CK, i.e. u1.CK = 0
u2 lies on CL
u3 lies on CN
||u1-u2|| = d1
||u1-u3|| = d2
(u1xu2).(u1xu3) = 0 (orthogonality)
where, A.B = dot product of vectors A and B
||A|| = euclidean norm of A
AxB = cross product of A and B
I think this problem will generate a set of possible solutions, at least in 2D it does. For the 2D case:
|
-----------+-----------
/|\
/ | \
/ | \
/---+---\VP
/ | \
/ | \
/ | \
/ | \
/ | -- \
/ | | \
/ | | \
In the above diagram, the vertical segment and the horizontal segment would project to the same line on the view plane (VP). If you drew this out to scale you'd see that there are two rays from the eye passing through each end point of the unprojected line. This line can be in many positions and rotations - imagine dropping a stick into a cone, it can get stuck in any number of positions.
So, in 2D space there are an infinite number of solutions within a well defined set.
Does this apply to 3D?
The algorithm would be along the lines of:
Invert the projection matrix
Calculate the four rays that pass through the vertices of the rectangle, effectively creating a skewed pyramid
Try and fit your rectangle into the pyramid. This is the tricky bit and I'm trying to mentally visualise rectangles in pyramids to see if they can fit in more than one way.
EDIT: If you knew the distance to the object it would become trivial.
EDIT V2:
OK, let Rn be the four rays in world space, i.e. transformed via the inverse matrix, expressed in terms of m.Rn, where |Rn| is one. The four points of the rectange are therefore:
P1 = aR1
P2 = bR2
P3 = cR3
P4 = dR4
where P1..P4 are the points around the circumference of the rectangle. From this, using a bit of vector maths, we can derive four equations:
|aR1 - bR2| = d1
|cR3 - dR4| = d1
|aR1 - cR3| = d2
|bR2 - dR4| = d2
where d1 and d2 are the lengths of the sides of the rectangle and a, b, c and d are the unknowns.
Now, there may be no solution to the above in which case you'd need to swap d1 with d2. You can expand each line to:
(a.R1x - b.R2x)2 + (a.R1y - b.R2y)2 + (a.R1z - b.R2z)2 = d12
where R1? and R2? are the x/y/z components of rays 1 and 2. Note that you're solving for a and b in the above, not x,y,z.
m_oLogin is right. If I understand your goal, the image the camera snaps is the plane P, right? If so, you're measuring K,L,M,N off the 2D image. You need the inverse of the projection matrix to reconstruct A,B,C, and D.
Now I've never done this before, but it ocurrs to me that you might run into the same problem GPS does with only 3 satellite fixes - there are two possible solutions, one 'behind' P and one 'in front' of it, right?
The projection matrix encapsulates both the perspective and scale, so the inverse will give you the solution you are after. I think you are assuming that it only encapsulates the perspective, and you need something else to choose the correct scale.

Resources