Esempio n. 1
0
bool Mesh::getIntersection(const Ray & ray, float & t, Vec3<float> & normal) const{
	Plane plane;
	Vec3<float> normalOutput, vertices4;
	bool colision=false;
	float pointIntersection;
	t=inf;
	
	for(size_t i = 0; i < poligons.size(); i++) {	
		plane.setPlane(poligons[i].vertices1,poligons[i].vertices2,poligons[i].vertices3);
		if(plane.getIntersection(ray,pointIntersection,normal)) {
			vertices4 = ray.eval(pointIntersection);

            if(getIntersectionTriangle(poligons[i].vertices1, poligons[i].vertices2, poligons[i].vertices3, vertices4) && pointIntersection < t) {
                t=pointIntersection;
                normalOutput=normal;
                colision=true;
            }
        }
	}

	if(colision) {
		normal = normalOutput;
		return true;
	}

	return false;
}
Esempio n. 2
0
bool Box::intersect(const Ray & ray, Intersection & it)
{
	Vector3f normal0, normal1, normalVec;
	float t0 = 0, t1 = FLT_MAX;
	float _t0, _t1;
	for (int i = 0; i < 3; ++i) {
		if (slabs[i].normal.dot(ray.D)) {
			float NdotQ = slabs[i].normal.dot(ray.Q);
			float NdotD = slabs[i].normal.dot(ray.D);
			_t0 = -(slabs[i].d0 + NdotQ) / NdotD;
			_t1 = -(slabs[i].d1 + NdotQ) / NdotD;
			if (_t0 > _t1) {
				float temp = _t0;
				_t0 = _t1;
				_t1 = temp;
			}
			normalVec = slabs[i].normal;
		} else {
			float NdotQ = slabs[i].normal.dot(ray.Q);
			float s0 = NdotQ + slabs[i].d0;
			float s1 = NdotQ + slabs[i].d1;
			if (s0 * s1 < 0.0f) {
				_t0 = 0.0f;
				_t1 = FLT_MAX;
			} else return false;
		}

		if (t0 < _t0) {
			t0 = _t0;
			normal0 = normalVec;
		}
		if (t1 > _t1) {
			t1 = _t1;
			normal1 = normalVec;
		}
	}

	if (t0 > t1) return false;
	else if (t0 > epsilon) {
		it.t = t0; 
		it.normal = normal0;
	}
	else if (t1 > epsilon) {
		it.t = t1;
		it.normal = normal1;
	}
	else return false;

	it.pos = ray.eval(it.t);
	it.normal.normalize();
	Vector3f cornerToIt = it.pos - corner;
	if (cornerToIt[0] < epsilon || cornerToIt[1] < epsilon || cornerToIt[2] < epsilon) {
		if (it.normal.dot(diag) > 0.0f) it.normal *= -1;
	} else if (it.normal.dot(diag) < 0.0f) it.normal *= -1;
	it.pS = this;
	return true;
}
Esempio n. 3
0
bool Sphere::intersect(const Ray & ray, Intersection & it)
{
	Vector3f Qbar = ray.Q - center;
	float QdotD = Qbar.dot(ray.D);
	float fvar = sqrt(QdotD*QdotD - Qbar.dot(Qbar) + radius*radius);
	if (-QdotD - fvar > epsilon) it.t = -QdotD - fvar;
	else if (-QdotD + fvar > epsilon) it.t = -QdotD + fvar;
	else return false;

	it.pos = ray.eval(it.t);
	it.normal = (it.pos - center).normalized();
	it.pS = this;

	return true;
}
Esempio n. 4
0
/**
 * Calculates whether a ray intersects with a sphere. Returns true or false,
 * storing the collision point in result if true.
 */
bool Sphere::intersection(Ray prim_ray, Vec3 &result) {
  double t0, t1;
  Vec3 obj_center(pos);

  Vec3 L = (*prim_ray.p1) - obj_center;

  double a = prim_ray.p2->dot(*prim_ray.p2);
  double b = 2 * prim_ray.p2->dot(L);
  double c = L.dot(L) - (radius * radius);

  if(!solve_quadratic(a, b, c, t0, t1)) {
    return false;
  } else {
    prim_ray.eval(t0, result);
    return true;
  }
}
Esempio n. 5
0
bool Triangle::intersect(const Ray & ray, Intersection & it)
{
	Vector3f e1 = v1 - v0;
	Vector3f e2 = v2 - v0;
	Vector3f p = ray.D.cross(e2);
	float d = p.dot(e1);
	if (abs(d) < epsilon) return false;
	Vector3f s = ray.Q - v0;
	float u = (p.dot(s)) / d;
	if (u < 0.0f || u >1) return false;
	Vector3f q = s.cross(e1);
	float v = ray.D.dot(q) / d;
	if (v < 0.0f || u + v > 1.0f) return false;
	float t = e2.dot(q) / d;
	if (t < 0.0f)return false;

	it.t = t;
	it.pos = ray.eval(t);
	it.normal = e1.cross(e2);
	it.normal.normalize();
	it.pS = this;
	return true;
}
Esempio n. 6
0
bool Cylinder::intersect(const Ray & ray, Intersection & it)
{
	Vector3f Q = q._transformVector(ray.Q - base);
	Vector3f D = q._transformVector(ray.D);
	float t0 = 0, t1 = FLT_MAX;
	double b0, b1;
	Vector3f normalVec;
	if (slab.normal.dot(D)) {
		double NdotQ = slab.normal.dot(Q);
		double NdotD = slab.normal.dot(D);
		b0 = -(slab.d0 + NdotQ) / NdotD;
		b1 = -(slab.d1 + NdotQ) / NdotD;
		if (b0 > b1) {
			double temp = b0;
			b0 = b1;
			b1 = temp;
		}
	} else {
		float NdotQ = slab.normal.dot(Q);
		float s0 = NdotQ + slab.d0;
		float s1 = NdotQ + slab.d1;
		if (s0 * s1 < 0.0f) {
			b0 = 0.0f;
			b1 = FLT_MAX;
		} else return false;
	}
	double a = D[0] * D[0] + D[1] * D[1];
	double b = 2 * (D[0] * Q[0] + D[1] * Q[1]);
	double c = Q[0] * Q[0] + Q[1] * Q[1] - radius * radius;
	double deter = b * b - 4.0f * a * c;
	if (deter < 0) return false;
	double c0 = (-b - sqrt(deter)) / 2.0f / a;
	double c1 = (-b + sqrt(deter)) / 2.0f / a;


	Vector3f normal0, normal1, M;
	if (b0 > c0) {
		t0 = b0;
		if (D[2] > epsilon) normal0 = Vector3f(0.0f, 0.0f, -1.0f);
		else normal0 = Vector3f(0.0f, 0.0f, 1.0f);
	} else {
		t0 = c0;
		M = Q + t0*D;
		normal0 = Vector3f(M[0], M[1], 0.0f);
	}

	if (b1 < c1) {
		t1 = b1;
		if (D[2] > epsilon) normal1 = Vector3f(0.0f, 0.0f, 1.0f);
		else normal1 = Vector3f(0.0f, 0.0f, -1.0f);
	} else {
		t1 = c1;
		M = Q + t1*D;
		normal1 = Vector3f(M[0], M[1], 0.0f);
	}

	if (t0 > t1) return false;
	else if (t0 > epsilon) {
		it.t = t0;
		it.normal = q.conjugate()._transformVector(normal0);
	} else if (t1 > epsilon) {
		it.t = t1;
		it.normal = q.conjugate()._transformVector(normal1);
	} else return false;

	it.pos = ray.eval(it.t);
	it.pS = this;
	it.normal.normalize();
	return true;
}
Esempio n. 7
0
bool Cube::getIntersection(const Ray & ray, float & t, Vec3<float> & normal) const {
	Plane add1(Vec3<float>(min.x, min.y, min.z), Vec3<float>(max.x, min.y, min.z), Vec3<float>(max.x, min.y, max.z));
    Plane dd1c1(Vec3<float>(max.x, min.y, min.z), Vec3<float>(max.x, min.y, max.z), Vec3<float>(max.x, max.y, max.z));
	Plane adc(Vec3<float>(min.x, min.y, min.z), Vec3<float>(max.x, min.y, min.z), Vec3<float>(max.x, max.y, min.z));
	Plane abb1(Vec3<float>(min.x, min.y, min.z), Vec3<float>(min.x, max.y, min.z), Vec3<float>(min.x, max.y, max.z));
	Plane bcc1(Vec3<float>(min.x, max.y, min.z), Vec3<float>(max.x, max.y, min.z), Vec3<float>(max.x, max.y, max.z));
	Plane a1b1c1(Vec3<float>(min.x, min.y, max.z), Vec3<float>(min.x, max.y, max.z), Vec3<float>(max.x, max.y, max.z));
	
	Vec3<float> pointIntersection, minValue, maxValue, normal1, normalP;
	float t1, tP;
	t1 = inf;
	
	if(add1.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = min.x;
		minValue.y = min.y;
		minValue.z = min.z;
		maxValue.x = max.x;
		maxValue.y = min.y;
		maxValue.z = max.z;		
		if ((pointIntersection >= minValue) && (pointIntersection <= maxValue)) {		
			if(tP < t1) {
				t1 = tP;
				normal1 = normalP;
			}
		}
	}

   if(dd1c1.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = max.x;
		minValue.y = min.y;
		minValue.z = min.z;
		maxValue.x = max.x;
		maxValue.y = max.y;
		maxValue.z = max.z;
		if (pointIntersection >= minValue && pointIntersection <= maxValue) {
			if(tP < t1) {
				t1 = tP;
				normal1 = normalP;
			}
		}
	}
	if(adc.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = min.x;
		minValue.y = min.y;
		minValue.z = min.z;
		maxValue.x = max.x;
		maxValue.y = max.y;
		maxValue.z = min.z;	
		if (pointIntersection >= minValue && pointIntersection <= maxValue) {
			if(tP < t1) {
				t1 = tP;
				normal1 = normalP;
			}
		}
	}
	if(abb1.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = min.x;
		minValue.y = min.y;
		minValue.z = min.z;
		maxValue.x = min.x;
		maxValue.y = max.y;
		maxValue.z = max.z;
		if (pointIntersection >= minValue && pointIntersection <= maxValue) {
			if(tP < t1) {
				t1 = t;
				normal1 = normalP;
			}
		}
	}
	if(bcc1.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = min.x;
		minValue.y = max.y;
		minValue.z = min.z;
		maxValue.x = max.x;
		maxValue.y = max.y;
		maxValue.z = max.z;
		if (pointIntersection >= minValue && pointIntersection <= maxValue) {
			if(tP < t1) {
				t1 = tP;
				normal1 = normalP;
			}
		}
	}
	if(a1b1c1.getIntersection(ray, tP, normalP)) {
		pointIntersection = ray.eval(tP);
		minValue.x = min.x;
		minValue.y = min.y;
		minValue.z = max.z;
		maxValue.x = max.x;
		maxValue.y = max.y;
		maxValue.z = max.z;
		if (pointIntersection >= minValue && pointIntersection <= maxValue) {
			if(tP < t1) {
				t1 = tP;
				normal1 = normalP;
			}
		}
	}
	
	if(t1 == Math::inf) {
		return false;
	}
	
	t = t1;
	normal = normal1;
	return true;
}