Example #1
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();

            //var shapeToRay = ray.Origin - new RtPoint(0, 0, 0);
            var shapeToRay = ray.Origin.DistanceFromZero();

            var a = ray.Direction.Dot(ray.Direction);
            var b = 2 * ray.Direction.Dot(shapeToRay);
            var c = shapeToRay.Dot(shapeToRay) - 1;

            var discriminant = Math.Pow(b, 2) - 4 * a * c;

            if (discriminant < 0)
            {
                return(intersections);
            }

            var t1 = (-b - Math.Sqrt(discriminant)) / (2 * a);
            var t2 = (-b + Math.Sqrt(discriminant)) / (2 * a);

            intersections.Add(new Intersection(t1, this));
            intersections.Add(new Intersection(t2, this));

            return(intersections);
        }
Example #2
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();
            var a             = Math.Pow(ray.Direction.X, 2) - Math.Pow(ray.Direction.Y, 2) + Math.Pow(ray.Direction.Z, 2);
            var b             = 2 * ray.Origin.X * ray.Direction.X - 2 * ray.Origin.Y * ray.Direction.Y + 2 * ray.Origin.Z * ray.Direction.Z;

            if (a.ApproximateEquals(0) && b.ApproximateEquals(0))
            {
                IntersectCaps(ray, intersections);
                return(intersections);
            }

            var c = Math.Pow(ray.Origin.X, 2) - Math.Pow(ray.Origin.Y, 2) + Math.Pow(ray.Origin.Z, 2);

            if (a == 0 && b != 0)
            {
                var time = -c / (2 * b);
                intersections.Add(time, this);
                IntersectCaps(ray, intersections);
                return(intersections);
            }

            var discriminant = Math.Pow(b, 2) - 4 * a * c;
            var t0           = (-b - Math.Sqrt(discriminant)) / (2 * a);
            var t1           = (-b + Math.Sqrt(discriminant)) / (2 * a);

            if (t0 > t1)
            {
                var tt = t0;
                t0 = t1;
                t1 = tt;
            }

            var y0 = ray.Origin.Y + t0 * ray.Direction.Y;

            if (Minimum < y0 && y0 < Maximum)
            {
                intersections.Add(t0, this);
            }

            var y1 = ray.Origin.Y + t1 * ray.Direction.Y;

            if (Minimum < y1 && y1 < Maximum)
            {
                intersections.Add(t1, this);
            }

            IntersectCaps(ray, intersections);

            return(intersections);
        }
Example #3
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();

            if (!CheckAxis(ray, out double tmin, out double tmax))
            {
                return(intersections);
            }

            intersections.Add(tmin, this);
            intersections.Add(tmax, this);

            return(intersections);
        }
Example #4
0
        private void IntersectCaps(Ray ray, IntersectionList intersections)
        {
            if (!IsClosed || ray.Direction.Y < 0)
            {
                return;
            }

            var time = (Minimum - ray.Origin.Y) / ray.Direction.Y;

            if (CheckCap(ray, Minimum, time))
            {
                intersections.Add(time, this);
            }

            time = (Maximum - ray.Origin.Y) / ray.Direction.Y;
            if (CheckCap(ray, Maximum, time))
            {
                intersections.Add(time, this);
            }
        }
Example #5
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersecitons = new IntersectionList();

            if (Math.Abs(ray.Direction.Y) < DoubleExtensions.EPSILON)
            {
                return(intersecitons);
            }

            var time = -ray.Origin.Y / ray.Direction.Y;

            intersecitons.Add(new Intersection(time, this));
            return(intersecitons);
        }
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();

            var directionCrossEdge2 = ray.Direction.Cross(Edge2);
            var determinant         = Edge1.Dot(directionCrossEdge2);

            if (Math.Abs(determinant) < DoubleExtensions.EPSILON)
            {
                return(intersections);
            }

            var f = 1.0 / determinant;
            var point1ToOrigin = ray.Origin - Point1;
            var u = f * point1ToOrigin.Dot(directionCrossEdge2);

            if (u < 0 || u > 1)
            {
                return(intersections);
            }

            var originCrossEdge1 = point1ToOrigin.Cross(Edge1);
            var v = f * ray.Direction.Dot(originCrossEdge1);

            if (v < 0 || (u + v) > 1)
            {
                return(intersections);
            }

            var t            = f * Edge2.Dot(originCrossEdge1);
            var intersection = new Intersection
            {
                Shape = this,
                Time  = t,
                U     = u,
                V     = v
            };

            intersections.Add(intersection);

            return(intersections);
        }
        private void IntersectTrianle(TriangleGeometry triangleGeometry, IntersectionList intersections, Ray ray)
        {
            var vertex1 = _vertices[triangleGeometry.Vertex1 - 1];
            var vertex2 = _vertices[triangleGeometry.Vertex2 - 1];
            var vertex3 = _vertices[triangleGeometry.Vertex3 - 1];

            var edge1 = vertex2 - vertex1;
            var edge2 = vertex3 - vertex1;

            var directionCrossEdge2 = ray.Direction.Cross(edge2);
            var determinant         = edge1.Dot(directionCrossEdge2);

            if (Math.Abs(determinant) < DoubleExtensions.EPSILON)
            {
                return;
            }

            var f = 1.0 / determinant;
            var point1ToOrigin = ray.Origin - vertex1;
            var u = f * point1ToOrigin.Dot(directionCrossEdge2);

            if (u < 0 || u > 1)
            {
                return;
            }

            var originCrossEdge1 = point1ToOrigin.Cross(edge1);
            var v = f * ray.Direction.Dot(originCrossEdge1);

            if (v < 0 || (u + v) > 1)
            {
                return;
            }


            var t = f * edge2.Dot(originCrossEdge1);

            Triangle triangle;

            if (_normals.Count > 0)
            {
                var normal1 = _normals[triangleGeometry.Normal1 - 1];
                var normal2 = _normals[triangleGeometry.Normal2 - 1];
                var normal3 = _normals[triangleGeometry.Normal3 - 1];

                triangle = new Triangle(vertex1, vertex2, vertex3, normal1, normal2, normal3);
            }
            else
            {
                triangle = new Triangle(vertex1, vertex2, vertex3);
            }

            triangle.Parent          = this;
            triangle.InheritMaterial = true;

            var intersection = new Intersection
            {
                Shape = triangle,
                Time  = t,
                U     = u,
                V     = v
            };

            intersections.Add(intersection);
        }