コード例 #1
0
ファイル: IntersectionTests.cs プロジェクト: ukitake/Stratum
        public static bool Test(LineSegment seg, Vector3 a, Vector3 b, Vector3 c, out float u, out float v, out float w)
        {
            Vector3 ab = b - a;
            Vector3 ac = c - a;
            Vector3 qp = seg.Point1 - seg.Point2;

            // Compute triangle normal. Can be precalculated or cached if
            // intersecting multiple segments against the same triangle
            Vector3 n = Vector3.Cross(ab, ac);

            // Compute denominator d. If d <= 0, segment is parallel to or points
            // away from triangle, so exit early
            float d = Vector3.Dot(qp, n);
            if (d <= 0.0f)
            {
                u = 0; v = 0; w = 0;
                return false;
            }
            // Compute intersection t value of pq with plane of triangle. A ray
            // intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
            // dividing by d until intersection has been found to pierce triangle
            Vector3 ap = seg.Point1 - a;
            float t = Vector3.Dot(ap, n);
            if (t < 0.0f)
            {
                u = 0; v = 0; w = 0;
                return false;
            }
            if (t > d)
            {
                u = 0; v = 0; w = 0;
                return false; // For segment; exclude this code line for a ray test
            }
            // Compute barycentric coordinate components and test if within bounds
            Vector3 e = Vector3.Cross(qp, ap);
            v = Vector3.Dot(ac, e);
            if (v < 0.0f || v > d)
            {
                u = 0; w = 0;
                return false;
            }
            w = -Vector3.Dot(ab, e);
            if (w < 0.0f || v + w > d)
            {
                u = 0;
                return false;
            }
            // Segment/ray intersects triangle. Perform delayed division and
            // compute the last barycentric coordinate component
            float ood = 1.0f / d;
            t *= ood;
            v *= ood;
            w *= ood;
            u = 1.0f - v - w;
            return true;
        }
コード例 #2
0
ファイル: Ray.cs プロジェクト: ukitake/Stratum
 public override bool Test(LineSegment other)
 {
     // TODO
     throw new NotImplementedException();
 }
コード例 #3
0
ファイル: IntersectionTests.cs プロジェクト: ukitake/Stratum
 public static bool Test(LineSegment seg, BoundingSphere sphere)
 {
     ContainmentType ct1 = sphere.Contains(seg.Point1);
     ContainmentType ct2 = sphere.Contains(seg.Point2);
     return ct1 != ct2;
 }
コード例 #4
0
ファイル: IntersectionTests.cs プロジェクト: ukitake/Stratum
        public static bool Test(LineSegment seg, Plane plane)
        {
            Vector3 q;
            float t;

            // Compute the t value for the directed line ab intersecting the plane
            Vector3 ab = seg.Point2 - seg.Point1;
            t = (plane.D - Vector3.Dot(plane.Normal, seg.Point1)) / Vector3.Dot(plane.Normal, ab);

            // If t in [0..1] compute and return intersection point
            if (t >= 0.0f && t <= 1.0f)
            {
                q = seg.Point1 + t * ab;
                return true;
            }
            // Else no intersection
            return false;
        }
コード例 #5
0
ファイル: IntersectionTests.cs プロジェクト: ukitake/Stratum
        public static bool Test(LineSegment seg, AxisAlignedBoundingBox bb)
        {
            Vector3 c = (bb.Min + bb.Max) * 0.5f;     // box center point
            Vector3 e = bb.Max - c;                      // box halflength extents
            Vector3 m = (seg.Point1 + seg.Point2) * 0.5f;           // segment midpoint
            Vector3 d = seg.Point1 - m;                         // segment halflength vector
            m = m - c;                                      // translate box and segment to origin

            // try world coordinate axes as seperating axes
            float adx = Math.Abs(d.X);
            if (Math.Abs(m.X) > e.X + adx)
                return false;
            float ady = Math.Abs(d.Y);
            if (Math.Abs(m.Y) > e.Y + ady)
                return false;
            float adz = Math.Abs(d.Z);
            if (Math.Abs(m.Z) > e.Z + adz)
                return false;

            // add in an epsilon term to counteract arithmetic errors when segment is (near) parallel to a coordinate axis
            adx += FPPrecisionHelper.EPSILON; ady += FPPrecisionHelper.EPSILON; adz += FPPrecisionHelper.EPSILON;

            // try cross products of segment direction vector with coordinate axes
            if (Math.Abs(m.Y * d.Z - m.Z * d.Y) > e.Y * adz + e.Z * ady)
                return false;
            if (Math.Abs(m.Z * d.X - m.X * d.Z) > e.X * adz + e.Z * adx)
                return false;
            if (Math.Abs(m.X * d.Y - m.Y * d.X) > e.X * ady + e.Y * adx)
                return false;

            // no separating axis found; segment must be overlapping AABB
            return true;
        }
コード例 #6
0
ファイル: IntersectionTests.cs プロジェクト: ukitake/Stratum
 public static bool Test(LineSegment seg, BoundingFrustum frus)
 {
     ContainmentType ct1 = frus.Contains(seg.Point1);
     ContainmentType ct2 = frus.Contains(seg.Point2);
     return ct1 != ct2;
 }
コード例 #7
0
ファイル: Primitive.cs プロジェクト: ukitake/Stratum
 public abstract bool Test(LineSegment other);
コード例 #8
0
ファイル: Primitive.cs プロジェクト: ukitake/Stratum
 public abstract bool Test(LineSegment other);
コード例 #9
0
ファイル: BoundingFrustum.cs プロジェクト: ukitake/Stratum
 public override bool Test(LineSegment other)
 {
     return IntersectionTests.Test(other, this);
 }
コード例 #10
0
ファイル: BoundingSphere.cs プロジェクト: ukitake/Stratum
 public override bool Test(LineSegment other)
 {
     return(IntersectionTests.Test(other, this));
 }
コード例 #11
0
 public override bool Test(LineSegment other)
 {
     // TODO
     throw new NotImplementedException();
 }