Example #1
0
File: AABB.cpp Project: katik/naali
bool AABB::Intersects(const LineSegment &lineSegment, float *dNear, float *dFar) const
{
	float tNear, tFar;
	bool success = IntersectLineAABB(lineSegment.a, lineSegment.Dir(), *this, tNear, tFar);
	success = (tNear >= 0.f && tNear*tNear <= lineSegment.LengthSq());
	if (dNear)
		*dNear = tNear / lineSegment.Length();
	if (dFar)
		*dFar = tFar / lineSegment.Length();
	return success;
}
Example #2
0
	Intersection(const LineSegment<T, 2>& l1, const LineSegment<T, 2>& l2) {
		lineSegment1 = l1;
		lineSegment2 = l2;
		auto intersection = Intersect(l1.Line(), l2.Line());
		if (intersection.Intersecting()) {
			param1 = intersection.LineParameter1() / l1.Length();
			param2 = intersection.LineParameter2() / l2.Length();
		}
		else {
			param1 = param2 = std::numeric_limits<T>::infinity();
		}
	}
Example #3
0
bool Plane::Intersects(const LineSegment &lineSegment, float *d) const
{
    float t;
    bool success = IntersectLinePlane(PointOnPlane(), normal, lineSegment.a, lineSegment.Dir(), &t);
    const float lineSegmentLength = lineSegment.Length();
    if (d)
        *d = t / lineSegmentLength;
    return success && t >= 0.f && t <= lineSegmentLength;
}
Example #4
0
bool Plane::Intersects(const LineSegment &lineSegment, float *dist) const
{
	float t;
	bool success = IntersectLinePlane(normal, this->d, lineSegment.a, lineSegment.Dir(), t);
	const float lineSegmentLength = lineSegment.Length();
	if (dist)
		*dist = t / lineSegmentLength;
	return success && t >= 0.f && t <= lineSegmentLength;
}
Example #5
0
	Intersection(const LineSegment<T, 2>& line1, const Line<T, 2>& line2) {
		auto inter = Intersect(line1.Line(), line2);
		if (inter.Intersecting() && inter.LineParameter1() < line1.Length()) {
			param1 = inter.LineParameter1();
			param2 = inter.LineParameter2();
		}
		else {
			param1 = param2 = std::numeric_limits<T>::infinity();
		}
	}
Example #6
0
float3 Polygon::PointOnEdge(float normalizedDistance) const
{
	if (p.empty())
		return float3::nan;
	if (p.size() < 2)
		return p[0];
	normalizedDistance = Frac(normalizedDistance); // Take modulo 1 so we have the range [0,1[.
	float perimeter = Perimeter();
	float d = normalizedDistance * perimeter;
	for(int i = 0; i < NumVertices(); ++i)
	{
		LineSegment edge = Edge(i);
		float len = edge.Length();
		assume(len != 0.f && "Degenerate Polygon detected!");
		if (d <= len)
			return edge.GetPoint(d / len);
		d -= len;
	}
	mathassert(false && "Polygon::PointOnEdge reached end of loop which shouldn't!");
	return p[0];
}
Example #7
0
int Sphere::Intersects(const LineSegment &l, vec *intersectionPoint, vec *intersectionNormal, float *d, float *d2) const
{
	float t1, t2;
	int numIntersections = IntersectLine(l.a, l.Dir(), pos, r, t1, t2);

	if (numIntersections == 0)
		return 0;

	float lineLength = l.Length();
	if (t2 < 0.f || t1 > lineLength)
		return 0;
	vec hitPoint = l.GetPoint(t1 / lineLength);
	if (intersectionPoint)
		*intersectionPoint = hitPoint;
	if (intersectionNormal)
		*intersectionNormal = (hitPoint - pos).Normalized();
	if (d)
		*d = t1 / lineLength;
	if (d2)
		*d2 = t2 / lineLength;

	return true;
}