Exemple #1
0
 private void FindIntersection(Vector3 origin, Vector3 dir, ref float tmin, ref Trig triangle, ref Vector3 barycentric)
 {
     foreach (var trig in trigs)
     {
         if (Geometry.ThereIsIntersection(origin, dir, trig, out var t, out var bary))
         {
             if (t > 0.01f && tmin > t)
             {
                 tmin        = t;
                 triangle    = trig;
                 barycentric = bary;
             }
         }
     }
 }
Exemple #2
0
        public static int CompareByPosition(Trig t1, Trig t2, int axes)
        {
            var v1 = t1.Center.GetByIndex(axes);
            var v2 = t2.Center.GetByIndex(axes);

            if (v1 > v2)
            {
                return(1);
            }
            if (v1 < v2)
            {
                return(-1);
            }
            return(0);
        }
Exemple #3
0
        public void Traverse(Ray ray, ref float tmin, ref Trig triangle, ref Vector3 barycentric)
        {
            if (isLeaf)
            {
                FindIntersection(ray.Origin, ray.Dir, ref tmin, ref triangle, ref barycentric);
                return;
            }

            if (!GetIntersection(ray.Origin, ray.Dir, box))
            {
                return;
            }

            left?.Traverse(ray, ref tmin, ref triangle, ref barycentric);
            middle?.Traverse(ray, ref tmin, ref triangle, ref barycentric);
            right?.Traverse(ray, ref tmin, ref triangle, ref barycentric);
        }
Exemple #4
0
        public static bool ThereIsIntersection(Vector3 rayOrigin, Vector3 rayVector, Trig inTriangle, out float t, out Vector3 barycentric)
        {
            t           = int.MaxValue;
            barycentric = Vector3.Zero;

            var vertex0 = inTriangle.A;
            var vertex1 = inTriangle.B;
            var vertex2 = inTriangle.C;
            var edge1   = vertex1 - vertex0;
            var edge2   = vertex2 - vertex0;
            var h       = Vector3.Cross(rayVector, edge2);

            var a       = Vector3.Dot(edge1, h);
            var EPSILON = 1e-10f;

            if (a > -EPSILON && a < EPSILON)
            {
                return(false);
            }

            var f = 1 / a;
            var s = rayOrigin - vertex0;
            var u = f * Vector3.Dot(s, h);

            if (u < 0.0 || u > 1.0)
            {
                return(false);
            }

            var q = Vector3.Cross(s, edge1);
            var v = f * Vector3.Dot(rayVector, q);

            if (v < 0.0 || u + v > 1.0)
            {
                return(false);
            }

            var w = 1 - v - u;

            barycentric = new Vector3(u, v, w);

            // At this stage we can compute t to find out where the intersection point is on the line.
            t = f * Vector3.Dot(edge2, q);
            return(t > EPSILON);
        }
Exemple #5
0
        public void SetupTrigs()
        {
            Trigs = new Trig[Faces.Length];
            for (var i = 0; i < Faces.Length; i++)
            {
                var face = Faces[i];

                var aIndex = face[0].VertexIndex;
                var bIndex = face[1].VertexIndex;
                var cIndex = face[2].VertexIndex;

                var anIndex = face[0].NormalIndex;
                var bnIndex = face[1].NormalIndex;
                var cnIndex = face[2].NormalIndex;

                Trigs[i] = new Trig(Vertices[aIndex], Vertices[bIndex], Vertices[cIndex],
                                    Normals[anIndex], Normals[bnIndex], Normals[cnIndex]);
            }
        }
Exemple #6
0
 public BoundingBox(Trig t)
 {
     SetBounds(t);
     UpdateSize();
 }
Exemple #7
0
        private void UpdateTriangleBoundBox(Trig t)
        {
            var newBox = new BoundingBox(t);

            MergeBoundBox(newBox);
        }
Exemple #8
0
 private bool IsMiddle(Trig t, float center)
 {
     return(t.Mins.GetByIndex(dimSplit) <= center && t.Maxs.GetByIndex(dimSplit) >= center);
 }