Determine if point is in frustum - c++

I'm trying to work out the best way to determine whether a point is inside a frustum. I have something working, but not sure whether it is too cumbersome, and perhaps there is a more elegant / efficient way I should be doing this.
Suppose I want to find out whether point 'x' is inside a frustrum:
Once I have the locations of the 8 points of the frustrum (4 near points, four far points), I am calculating the normal for each plane of the frustum based on a triangle made from three of the points. For example (as in the diagram above), for the right side, I am making two vectors from three of the points:
Vector U = FBR - NBR
Vector V = FTR - NBR
Then I am making the cross product between these two vectors, ensuring that the winding order is correct for the normal to be pointing inside the frustum, in this case V x U will give the correct normal.
Right_normal = V x U
Once I have the normal for each plane, I am then checking whether point x is in front of or behind the plane by drawing a vector from x to one of the plane's points:
Vector xNBR = x - NBR
Then I am doing the dot product between this vector and the normal and testing whether the answer is positive, confirming whether point x is the correct side of that plane of the frustrum:
if ( xNBR . Right_normal < 0 )
{
return false;
}
else continue testing x against other planes...
If x is positive for all planes, then it is inside the frustum.
So this seems to work, but I'm just wondering whether I'm doing this in a stupid way. I didn't even know what 'cross product' meant until yesterday, so it's all rather new to me and I might be doing something rather silly.

To adapt the approach you have taken, rather than change it totally, you can make use of the fact that 2 of the pairs of planes are parallel. Create only one normal for that pair of planes. You already have the test for the point being "in front" of one of the planes, but assuming you know the depth of the frustum, you can use the same distance to test the point against the other parallel face.
double distancePastFrontPlane = xNBR . Right_normal;
if (distancePastFrontPlane < 0 )
{
// point is in front of front plane
return false;
if(distancePastFrontPlane > depthFaceRtoFaceL)
{
// point is behind back plane
return false;
}
}
If you have multiple points to test against the same frustum you can benefit because you only calculate the frustum depth once (per pair of parallel planes).

Related

Point inside rectangle given sides and center

I'm given rectangle by it's height(h) and width(w), and it's center O(x0,y0). I need to calculate if given point A(x,y) is inside that rectangle. It is parallel to x and y axis. All values are real.
I came up with following test but for some reason website on which I'm testing the code is not working for all examples. Could someone point me in the right direction.
#include <iostream>
#include <cmath>
using namespace std;
int main(){
long x,y,x0,y0,r,h,w;
scanf("%ld",&x);
scanf("%ld",&y);
scanf("%ld",&x0);
scanf("%ld",&y0);
scanf("%ld",&h);
scanf("%ld",&w);
if((x0+w/2.0>=x)&&(x0-w/2.0<=x)&&(y0+h/2.0>=y)&&(y0-h/2.0<=y))
printf("inside a rectangle");
else
printf("outside a rectangle");
}
Thanks in advance.
After OP's Edit:
The rectangle's side are parallel to x axis and y-axis. Then also it is possible to get the co-ordinates and apply the below mentioned algorithm.
Centre -- (x0,y0)
A -- (x0-w/2,y0-h/2)
B -- (x0-w/2.y0+h/2)
C -- (x0+w/2,y0+h/2)
D -- (x0+w/2,y0-h/2)
So all you have to do is, Apply the algorithms provided below.
More simply we can do this,
if( 2*x <= 2*x0+w && 2*x >= 2*x0-w && 2*y <= 2*y0+h && 2*y >= 2*y0-h)
// it's inside
Before OP's edit
Your logic is wrong. It may say a point inside rectangle to be outside of it.
(For any rectangle this is wrong - OP didn't mention the condition of being sides parallel to x-y axes)
There is a simple way and cleaner way to do this for rectangle. Find the Area of the rectangle.
Suppose it's A.
Now if the point P lies inside ABCD then
area of PAB+PBC+PCD+PDA = A
For better thing do this with ,
AB.Bc+BC.CD+CD.DA+DA.AB = 2*AB*BC
or even better make a square of both side
LHS^2 = 4*AB^2*BC^2
Now you will just multiply and check it. One drawback of this solution is for large values of side length you have a chance of overflow.
Another method would be to consider the projections.
If point is inside of the rectangle then the projection of the corner of rectangle to point, on two of it's side must be less than the corresponding sides. You can check the projection length using dot products.
For example if P is the point and ABCD is rectangle check,
if AP's projection on AB has greater than zero length but less than the length of AB. Check the same with BC and BP and check if length is greater than zero and less than BC or not.
This two condition makes sure that your point lies inside the rectangle.

OpenCV estimateRigidTransform()

After reading several posts about getting the 2D transformation of 2D points from one image to another, estimateRigidTransform() seems to be the recommendation. I'm trying to use it. I modified the source code (to change the RANSAC parameters, because its hardcoded, and the hardcoded parameters are not very good)(the source code for this function is in lkpyramid.cpp). I have read up on how RANSAC works, and am trying to understand the steps in estimateRigidTransform().
// choose random 3 non-complanar points from A & B
...
// additional check for non-complanar vectors
a[0] = pA[idx[0]];
a[1] = pA[idx[1]];
a[2] = pA[idx[2]];
b[0] = pB[idx[0]];
b[1] = pB[idx[1]];
b[2] = pB[idx[2]];
double dax1 = a[1].x - a[0].x, day1 = a[1].y - a[0].y;
double dax2 = a[2].x - a[0].x, day2 = a[2].y - a[0].y;
double dbx1 = b[1].x - b[0].x, dby1 = b[1].y - b[0].y;
double dbx2 = b[2].x - b[0].x, dby2 = b[2].y - b[0].y;
const double eps = 0.01;
if( fabs(dax1*day2 - day1*dax2) < eps*std::sqrt(dax1*dax1+day1*day1)*std::sqrt(dax2*dax2+day2*day2) ||
fabs(dbx1*dby2 - dby1*dbx2) < eps*std::sqrt(dbx1*dbx1+dby1*dby1)*std::sqrt(dbx2*dbx2+dby2*dby2) )
continue;
Is it a typo that it uses non-coplanar vectors? I mean the 2D points are all on the same plane right?
My second question is what is that if condition doing? I know that the left hand side (gives the area of triangle times 2) would be zero or near zero if the points are collinear, and the right hand side is the multiplication of the lengths of 2 sides of the triangle.
Collinearity is preserved in affine transformations (such as the one you are probably estimating), but this transformations also calculate also changes in rotations in point of view (as if you rotated the object in a 3d world). However, these points will be collinear as well, so for the algorithm it may have not a unique solution. Look at the pictures:
imagine selecting 3 center points of each black square in the first row in the first image. Then map it to the same centers in the next image. It may generate a mapping to that solution, but also a mapping to a zoom version of the first one. The same may happen with the third one, just that this time may map to a zoom out version of the first one (without any other change). However if the points are not collinear, for example, 3 corner squares centers, it will find a unique mapping.
I hope this helps you to clarify your doubts. If not, leave a comment

Find direction of given x,y,z cordinates

i'm new to opencv & it's developing. I have x,y,z coordinates ( 0.00949334694383068, -0.3999829847985352, 0.8449078780010854) by using the given coordinates how could i find the direction.
for an example
input one : x,y,z = 0.00949334694383068, -0.3999829847985352, 0.8449078780010854
input two : x,y,z = 0.01603883281370779, 0.6066595296580494, 0.5342810849038371
At the finally i want to compare input_one direction and input_two direction. Any help is appreciated
This is called vector math.
"Coordinates" are a special kind of vector, relative to some origin (in your case x=0,y=0,z=0). For that reason, the difference x1-x2, y1-y2, z1-z2 is a vector from point 2 to point 1. The inverse x2-x1, y2-y1, z2-z1 is a vector from point 1 to point 2.
The direction of a vector is usually defined by ignoring its length, or alternatively by setting its length to one. So we need to first define the length, which is L = √(x*x + y*y + z*z). We can define the vector x/L, y/L, z/L which points in the same direction as x,y,z but with length one.
Finally, to compare two directions we can calculate the inner product of those two directions: x1/L1 * x2/L2 + y1/L1 * y2/L2 + z1/L1 * z2/L2. If that's one, they point in the same direction. If it's 0, they're orthogonal. If it's -1, they point in opposite directions.
As you can see, the vector 0,0,0 has length 0 and no direction. That can complicate things a bit.
In OpenCV: class Vec. The length function is called norm(v) and the inner product is called v1.mul(v2)
What you're trying to do is called calculating the azimuth. If you're interested in doing this for navigational or geographic purposes and need a thorough understanding of this, you can start here:
http://mobile.codeguru.com/cpp/cpp/algorithms/article.php/c5115/Geographic-Distance-and-Azimuth-Calculations.htm
Otherwise you could look for a library for calculating the azimuth bases on 3d co-ordinates

vtk IntersectWithLine() function using a normal as the line

I have two isosurfaces (skull and skin). given point A on skull isosurface, i calculated the normal at point A using "double *pos = pickerCell->GetPickNormal()".
when i print pos, this is what i got: -6.2367, 1.98263, -0.9823
could someone explain to me what these 3 values mean?
I would like to find the intersection point of this normal of point A with the skin isosurface.
Could I use IntersectWithLine() function to do so? If yes, the line in my case would then be the normal? what is the start and end point of the normal?
Or is there a better way of doing ?
As you've found, you need to define the line to intersect with as two points. What is commonly done is to start at the point, P, that you picked (the same point where the normal, v, was computed) and compute two points, A = P + v delta and B = P - v delta where you have to set delta using context (if your model is in a unit cube, delta might be something like .01, where if your model has units of size 1000, delta may be 1, etc.).
Also, I'm not sure why the normal the cell picker returns is not normalized, but I'm assuming that if you normalize it it is the surface normal. I'd call it something other than 'pos' to avoid confusion (as it is a direction, not a position).

Tangent of a parametric discrete curve

I have a parametric curve, say two vectors of doubles where the parameter is the index, and I have to calculate the angle of the tangent to this curve at any given point (index).
Any suggestion or link about how to do that?
Thanks.
Here's a short formula, equivalent (I think) to pau.estalella's answer:
m[i] = (y[i+1] - y[i-1]) / (x[i+1] - x[i-1])
this approximates, reasonably well, the slope at the point (x[i], y[i]).
Your question mentions the "angle of the tangent". The tangent line, having slope m[i], makes angle arctangent(m[i]) with the positive x axis. If this is what you're after, you might use the two-argument arctangent, if it's available:
angle[i] = atan2(y[i+1] - y[i-1], x[i+1] - x[i-1])
this will work correctly, even when x[i+1] == x[i-1].
I suggest you check out the Wikipedia article on numerical differentiation for a start. Before you go much further than that, decide what purposes you want the tangent for and decide whether or not you need to try more complex schemes than the simple ones in the article.
The first problem you run into is to even define the tangent in one of the vertexes of the curve. Consider e.g. that you have the two arrays:
x = { 1.0, 2.0, 2.0 };
y = { 1.0, 1.0, 2.0 };
Then at the second vertex you have a 90-degree change of direction of the line. In that place the tangent isn't even defined mathematically.
Answer to gregseth's comment below
I guess in your example the "tangent" at the second point would be the line parallel to (P0,P2) passing through P1... which kind of give me the answer : for any point of index N the parallel to (N-1, N+1) passing through N. Would that be a not-too-bad approximation?
It depends on what you are using it for. Consider for example:
x = { 1.0, 2.0, 2.0 };
y = { 1.0, 1000000, 1000000 };
That is basically an L shape with a very high vertical line. In your suggestion it would give you an almost vertical tangent. Is that what you want, or do you rather want a 45-degree tangent in that case? It also depends on your input data how you sould define it.
One solution is get the two vectors connection to the vertex, normalize them and then use your algorithm. That way you would get a 45-degree tangent in the above example.
Compute the first derivative: dy/dx. That gives you the tangent.
The tangent to a smooth curve at a point P is the parametric straight line P + tV, where V is the derivative of the curve with respect to "the parameter". But here the parameter is just the index of an array, and numerical differentiation is a difficult problem, hence to approximate the tangent I would use (weighted) least squares approximation.
In other words, choose three or five points of the curve around your point of interest P (i.e. P[i-2], P[i-1], P[i], P[i+1], and P[i+2], if P==P[i]), and approximate them with a straight line, in the least squares sense. The more weight you assign to the middle point P, the more close the line will be to P; on the other hand, the more weight you assign to the extremal points, the more "tangent" the straight line will be, that is, the more nicely it will approximate you curve in the neighborhood of P.
For example, with respect to the following points:
x = [-1, 0, 1]
y = [ 0, 1, 0]
for which the tangent is not defined (as in Anders Abel's answer),
this approach should yield a horizontal straight line close to the point (0,1).
You can try to compute the tangent of an interpolating curve that passes through the given points (I'm thinking of a cubic spline, which is pretty easy to derive) or compute the tangent directly from the data points.
You can find a rough approximation of the derivative in the following manner
Let a curve C pass through points p1,p2 and p3. At point p2 you have two possible tangents: t1=p2-p1 and t2=p3-p2. You can combine them by simply computing their average: 0.5*(t1+t2)
or you can combine them according to their lengths (or their reciprocal 1/length)
Remember to normalize the resulting tangent.
In order to compute the angle between the tangent and the curve, remember that the dot product of two unit vectors gives the cosine of the angle between them. Take the resulting tangent t and the unit vector v2=|p3-p2|, and acos(dot(t,v2)) gives the angle you need.