Exemplo n.º 1
0
bool Functions:: rayplane(float nx,float ny,float nz,float xs,float ys,float zs,float xd,float yd,float zd,vector3d p1,vector3d p2,vector3d p3,vector3d p4,float* dist, vector3d* point)
{
        float a=xd*nx+yd*ny+zd*nz;
        if(a==0)
                return false; // check if ray is parrellel to plane return false they will never touch
        float t=((p1.x*nx+p1.y*ny+p1.z*nz-nx*xs-ny*ys-nz*zs)/a);
        if(t<0)
                return false; // return false if collision happens behind the place
        float x=xs+t*xd;
        float y=ys+t*yd;
        float z=zs+t*zd;
        vector3d cp(x,y,z);
        if(abs(trianglearea(p1,p3,p4)-trianglearea(p1,p4,cp)-trianglearea(p1,p3,cp)-trianglearea(p3,p4,cp))<0.000001 || abs(trianglearea(p1,p2,p3)-trianglearea(p1,p2,cp)-trianglearea(p2,p3,cp)-trianglearea(p1,p3,cp))<0.000001)
	 {
                if(dist!=NULL)
                {
                        (*dist)=t;
                        if(point!=NULL)
                        {
                                point->x=x;
                                point->y=y;
                                point->z=z;
                        }
                }
                return true;
        }
        return false;
};
Exemplo n.º 2
0
bool collision::rayplane(const float& nx, float ny, float nz, float x0, float y0, float z0, float xs, float ys, float zs, float xd, float yd, float zd, vector3d p1, vector3d p2, vector3d p3, vector3d p4, float* dis, vector3d* point)
{
	if ((xd*nx + yd*ny + zd*nz) == 0)	//if the two vector dot product is 0, then there is no intersection (we don't like to divide by 0)
	{
		return false;
	}
	float t = ((x0*nx + y0*ny + z0*nz - nx*xs - ny*ys - nz*zs) / (xd*nx + yd*ny + zd*nz));
	if (t<0)	//if t<0, the intersction point is in the opposite direction
	{
		return false;
	}
	float x = xs + t*xd;	//calculate the 3 point vector3d
	float y = ys + t*yd;
	float z = zs + t*zd;
	vector3d i(x, y, z);

	if ((std::abs(trianglearea(p1, p2, p3) - (trianglearea(p1, p2, i) + trianglearea(p2, p3, i) + trianglearea(p1, p3, i)))<0.3) || std::abs(trianglearea(p1, p3, p4) - (trianglearea(p1, p3, i) + trianglearea(p3, p4, i) + trianglearea(p1, p4, i)))<0.3)	//we divide the quad to 2 triangle, we divide one triangle to 3 (one point is the
																																																															//intersection point), and if the area of the 3 triangle is equal to the main triangle, then the point is inside the triangle. We do the same with
																																																															//the other triangle, and if one is true, then the point is in the quad
	{
		if (dis != NULL)
		{
			(*dis) = t;
			if (point != NULL)
			{
				point->x = x;
				point->y = y;
				point->z = z;
			}
		}
		return true;
	}

	return false;	//else not
}
Exemplo n.º 3
0
void dotriangle(vert *basevert)
{
/* we should now have a triangle completely contained within a source pixel */
  float area;
  int   i, j;

  area = trianglearea(basevert);
  getsourceij(basevert, &i, &j);
  addarea(i, j, area);
/* freepolygon(basevert);*/
}
Exemplo n.º 4
0
bool rayplane(float nx,float ny,float nz,float xs,float ys,float zs,float xd,float yd,float zd,coordinate p1,coordinate p2,coordinate p3,coordinate p4,float* dist,coordinate* point) {
	float a=xd*nx+yd*ny+zd*nz;
	if(a==0)
		return false;
	float t=((p1.x*nx+p1.y*ny+p1.z*nz-nx*xs-ny*ys-nz*zs)/a);
	if(t<0)
		return false;
	float x=xs+t*xd;
	float y=ys+t*yd;
	float z=zs+t*zd;
	coordinate cp(x,y,z);
	if(abs(trianglearea(p1,p3,p4)-trianglearea(p1,p4,cp)-trianglearea(p1,p3,cp)-trianglearea(p3,p4,cp))<0.000001 || abs(trianglearea(p1,p2,p3)-trianglearea(p1,p2,cp)-trianglearea(p2,p3,cp)-trianglearea(p1,p3,cp))<0.000001) {
		if(dist!=NULL) {
			(*dist)=t;
			if(point!=NULL) {
				point->x=x;
				point->y=y;
				point->z=z;
			}
		}
		return true;
	}
	return false;
}
Exemplo n.º 5
0
/*
 * map() maps a M2 x M1 source image fsource[i][j] onto an N2 x N1 target
 * image ftarget[i][j] with the mapping
 *		ftarget(r) += fsource(r + d)
 * where the deflection d = (di, dj) is supplied by function deflection()
 *
 * we use the slightly confusing notation (i,j) -> (y, x)
 */
void map(float **ftarget, int N1, int N2, float **fsource, int M1, int M2,
         int (*deflection)(float ri, float rj, float *di, float *dj))
{
  int   i, j, index, goodtriangle;
  float di, dj;
  vert *point[3], *basevert;

  /* set source image globals */
  gfsource = fsource;
  gftarget = ftarget;
  gM1      = M1;
  gM2      = M2;

  for (i = 0; i < N2; i++) {
    gtargeti = i;
    for (j = 0; j < N1; j++) {
      gtargetj = j;
      gfsum    = gareasum = 0.0;
      /* do the upper triangle */
      point[0]     = makevertex(j, i + 1);
      point[1]     = makevertex(j + 1, i + 1);
      point[2]     = makevertex(j + 1, i);
      goodtriangle = 1;
      for (index = 0; index < 3; index++) {
        goodtriangle    *= deflection(point[index]->y, 
                                      point[index]->x, &di, &dj);
        point[index]->y += di;
        point[index]->x += dj;
      }
      if (goodtriangle) {
        makering(&basevert, point, 3);
        if (globalmapmode == INVERSEMAPMODE) {
          garea = trianglearea(basevert);
        }
        decompose(basevert);
      }
      /* do the lower triangle */
      point[0]     = makevertex(j, i + 1);
      point[1]     = makevertex(j, i);
      point[2]     = makevertex(j + 1, i);
      goodtriangle = 1;
      for (index = 0; index < 3; index++) {
        goodtriangle    *= deflection(point[index]->y, 
                                      point[index]->x, &di, &dj);
        point[index]->y += di;
        point[index]->x += dj;
      }
      if (goodtriangle) {
        makering(&basevert, point, 3);
        if (globalmapmode == INVERSEMAPMODE) {
          garea = trianglearea(basevert);
        }
        decompose(basevert);
      }
      /* now add the pixel value */
      if ((gareasum > 0.0) && (globalmapmode == FORWARDMAPMODE)) {
        ftarget[i][j] = gfsum / gareasum;
      }
      freeverts();
    }
  }
}