Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();

            foreach (var triangle in _triangles)
            {
                IntersectTrianle(triangle, intersections, ray);
            }

            return(intersections);
        }
Ejemplo n.º 3
0
        public override IntersectionList LocalIntersect(Ray ray)
        {
            var intersections = new IntersectionList();

            foreach (var shape in Shapes)
            {
                intersections.AddRange(shape.Intersect(ray));
            }

            return(intersections);
        }
Ejemplo n.º 4
0
        public IntersectionList Intersect(Ray ray)
        {
            var intersections = new IntersectionList();

            foreach (var shape in Shapes)
            {
                var shapsesIntersections = shape.Intersect(ray);
                intersections.AddRange(shapsesIntersections);
            }

            return(intersections);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
0
        public Computations PrepareComputations(Ray ray, IntersectionList intersections)
        {
            var computations = new Computations()
            {
                Time = Time,
                Shape = Shape,
                Position = ray.Position(Time),
                EyeVector = ray.Direction.Negate(),
            };

            computations.NormalVector = Shape.NormalAt(computations.Position, this);

            if(computations.NormalVector.Dot(computations.EyeVector) < 0)
            {
                computations.Inside = true;
                computations.NormalVector = computations.NormalVector.Negate();
            }
            else
            {
                computations.Inside = false;
            }

            computations.ReflectVector = ray.Direction.Reflect(computations.NormalVector);

            List<Shape> container = new List<Shape>();

            if(intersections != null && intersections.Count > 0)
            {
                foreach (var intersection in intersections.Items)
                {
                    if (intersection == this)
                    {
                        if (container.Count == 0)
                        {
                            computations.n1 = 1.0;
                        }
                        else
                        {
                            computations.n1 = container.Last().Material.RefractiveIndex;
                        }
                    }

                    if (container.Contains(intersection.Shape))
                    {
                        container.Remove(intersection.Shape);
                    }
                    else
                    {
                        container.Add(intersection.Shape);
                    }

                    if (intersection == this)
                    {
                        if (container.Count == 0)
                        {
                            computations.n2 = 1.0;
                        }
                        else
                        {
                            computations.n2 = container.Last().Material.RefractiveIndex;
                        }

                        break;
                    }
                }
            }

            return computations;
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
 public void AddRange(IntersectionList intersectionList)
 {
     _intersections.AddRange(intersectionList._intersections);
     _sorted = false;
 }