achuthpnr
October 15th, 2014, 02:40 PM
Hi,
I am trying to figure out the intersection point of a given line segment and a cubic box of edge L. First I check each face of the cube for intersection and if it occurs, calculate the intersection point. I accept and print the point when its on the line segment. I have the commented sample code below:
My questions are:
1. Is there any way to optimize this code for speed or altenate method to achieve the same?
2. With the current example code, I am getting an intersection point, which is actually outside the box(-4.440892e-16). I understand that this is due to the error in double precision calcualtion. What should be done to correct/compensate this? eg: reassign to 0. ? and similarly with L<xc..?
3. when I test whether the final point is on the line (its commented out for the moment), shouldn´t the test be dot>0 since the vectors are in same direction? then why am I getting a negative value in this case? where am I going wrong?
//finding the intersection point between a line segment and a plane.
//plane is represented by a point in the plane and a normal vector to the plane. Here the unit vectors i,j,k
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define dist2(x,xd,y,yd,z,zd) ((x-xd)*(x-xd)+(y-yd)*(y-yd)+(z-zd)*(z-zd))
#define scalar(x,x1,y,y1,z,z1) (x*x1+y*y1+z*z1)
int main()
{
unsigned long int L;
double xc,yc,zc;
double x,y,z,xd,yd,zd;
double d2,d,dv1,dv2,costheta,dot;
x=5.; //the initial point of the line segment
y=5.;
z=5.;
xd=-03.; //the final point of the line segment
yd=02.;
zd=01.;
L=15; //Size of the cubic box, assuming the other corner is origin
d2=dist2(x,xd,y,yd,z,zd);
d=sqrt(d2);
dv1=scalar(1.,(x-0.),0.,(y-0.),0.,(z-0.)); //YZ plane left
dv2=scalar(1.,(xd-0.),0.,(yd-0.),0.,(zd-0.));
printf("left %le\n",dv1*dv2);
if(dv1*dv2<=0 )
{
costheta=scalar(1.,(xd-x)/d,0.,(yd-y)/d,0.,(zd-z)/d);
xc=xd-(xd-x)/d*dv2/costheta;
yc=yd-(yd-y)/d*dv2/costheta;
zc=zd-(zd-z)/d*dv2/costheta;
dot=scalar(xd-x,xc-x,yd-y,yc-y,zd-z,zc-z);
//if(dot>0)
printf("%le\t%le\t%le\t%le\n",xc,yc,zc,dot);
}
// similarly for the other 5faces
return(0);
}
I am trying to figure out the intersection point of a given line segment and a cubic box of edge L. First I check each face of the cube for intersection and if it occurs, calculate the intersection point. I accept and print the point when its on the line segment. I have the commented sample code below:
My questions are:
1. Is there any way to optimize this code for speed or altenate method to achieve the same?
2. With the current example code, I am getting an intersection point, which is actually outside the box(-4.440892e-16). I understand that this is due to the error in double precision calcualtion. What should be done to correct/compensate this? eg: reassign to 0. ? and similarly with L<xc..?
3. when I test whether the final point is on the line (its commented out for the moment), shouldn´t the test be dot>0 since the vectors are in same direction? then why am I getting a negative value in this case? where am I going wrong?
//finding the intersection point between a line segment and a plane.
//plane is represented by a point in the plane and a normal vector to the plane. Here the unit vectors i,j,k
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define dist2(x,xd,y,yd,z,zd) ((x-xd)*(x-xd)+(y-yd)*(y-yd)+(z-zd)*(z-zd))
#define scalar(x,x1,y,y1,z,z1) (x*x1+y*y1+z*z1)
int main()
{
unsigned long int L;
double xc,yc,zc;
double x,y,z,xd,yd,zd;
double d2,d,dv1,dv2,costheta,dot;
x=5.; //the initial point of the line segment
y=5.;
z=5.;
xd=-03.; //the final point of the line segment
yd=02.;
zd=01.;
L=15; //Size of the cubic box, assuming the other corner is origin
d2=dist2(x,xd,y,yd,z,zd);
d=sqrt(d2);
dv1=scalar(1.,(x-0.),0.,(y-0.),0.,(z-0.)); //YZ plane left
dv2=scalar(1.,(xd-0.),0.,(yd-0.),0.,(zd-0.));
printf("left %le\n",dv1*dv2);
if(dv1*dv2<=0 )
{
costheta=scalar(1.,(xd-x)/d,0.,(yd-y)/d,0.,(zd-z)/d);
xc=xd-(xd-x)/d*dv2/costheta;
yc=yd-(yd-y)/d*dv2/costheta;
zc=zd-(zd-z)/d*dv2/costheta;
dot=scalar(xd-x,xc-x,yd-y,yc-y,zd-z,zc-z);
//if(dot>0)
printf("%le\t%le\t%le\t%le\n",xc,yc,zc,dot);
}
// similarly for the other 5faces
return(0);
}