user3853511
user3853511

Reputation: 43

Calculate if a rectangle is intersected by a ray in c++ 3D

I have the following 3D data:

Now I am searching for a simple c++ function which calculates, if the rectangle is intersected by the ray. I don´t need the intersection coordinates, only a "yes/no" bool. I have googled a lot but unfortunately I can´t find any simple function which fits to my requirements. I hope that I can avoid writing my own function because vector calculation is dawn long ago :-( ! If anyone has an idea, I´m greatful for any help.

Thanks....

Edit:

Thanks for your help. This was exactly what I am searching for, but I have a problem with the vxl library. First I have downloaded and compiled the sources. Then, while testing the lib, I got the following error when I try to create a plane with three 3D Points.

"undefined reference to `vgl_plane_3d::vgl_plane_3d(vgl_point_3d const&, vgl_point_3d const&, vgl_point_3d const&)'|"

My code:

// -----------------------------------------------
#include <vgl/vgl_point_3d.h>
#include <vgl/vgl_plane_3d.h>
#include <vgl/vgl_intersection.h>

void createTestPlane(void);

using namespace std;

int main()
{
    createTestPlane();
    return 0;
}

void createTestPlane()
{
vgl_point_3d<double> PlaneP0(1.0,0.0,0.0);
vgl_point_3d<double> PlaneP1(1.0,0.0,1.0);
vgl_point_3d<double> PlaneP2(1.0,1.0,0.0);
vgl_plane_3d<double> testConstruction();
vgl_plane_3d<double> Plane(PlaneP0,PlaneP1,PlaneP2);
}

// -----------------------------------------------

I don´t know where the problem is, because the constructor with three 3D points is available in the "" header. The default constructor seems to work correctly.

Part of the header file:

// -----------------------------------------------

  // Default constructor: horizontal XY-plane (equation 1.z = 0)
  inline vgl_plane_3d () : a_(0), b_(0), c_(1), d_(0) {}

  //: Construct from three non-collinear points
  //  The plane will contain all three points \a p1, \a p2 and \a p3.
  vgl_plane_3d (vgl_point_3d<T> const& p1,
                vgl_point_3d<T> const& p2,
                vgl_point_3d<T> const& p3);

// -----------------------------------------------

Does anybody have an idea what I am doing wrong ?

Upvotes: 0

Views: 631

Answers (2)

jhy
jhy

Reputation: 233

I am not allowed to post the graph. Here is the graph description:

rectangle points: V0, V1, V2, V3

ray origin: O

ray direction: Dir

surface normal: N^

P: the hit point on the rectangular plane. ( note: it may be outside )

I. compute hit point P:

N^ = ((V1 - V0) X (V3 -V0)).nomralize(). X is cross product; N^ length is 1, the surface normal.

Q = O - V0

H = Q * N^. * is dot product; H is the shortest distance to the rectangular surface

Dproj = Dir * (-N^); -N^ means revert N

Scale = H/Dproj.

P = O + (Dir).nomralized() * Scale. Dir is a normalized vector.

II. test if P is inside the rectangle by comparing the rectangular area and the four triangle areas

Reference about computing triangle/rectangle area: http://en.wikipedia.org/wiki/Cross_product

a0 = ((V0 - P) X (V1 - P) ).length() * 0.5

a1 = ((V1 - P) X (V2 - P) ).length() * 0.5

a2 = ((V2 - P) X (V3 - P) ).length() * 0.5

a3 = ((V3 - P) X (V0 - P) ).length() * 0.5

Trec = ((V1 - V0) X (V3 - V0)).length()

if ( (a0+a1+a2+a3) > Trec ), P is outside, otherwise, it is inside.

Hope this explanation is useful for you.

Upvotes: 1

StAlphonzo
StAlphonzo

Reputation: 766

You might want to look into VXL ( http://public.kitware.com/vxl/doc/development/core/vgl/html/vgl__intersection_8h_source.html ), starting at line 00240 is the declaration of the function you likely want. It's open source so you can either use the library or just download and inspect the code/

Upvotes: 0

Related Questions