Esempio n. 1
0
        // convenience function to construct a IntrRay3Triangle3 object for a mesh triangle
        public static IntrRay3Triangle3 TriangleIntersection(NGonsCore.geometry3Sharp.mesh.DMesh3 mesh, int ti, Ray3d ray)
        {
            if (!mesh.IsTriangle(ti))
            {
                return(null);
            }
            Triangle3d tri = new Triangle3d();

            mesh.GetTriVertices(ti, ref tri.V0, ref tri.V1, ref tri.V2);
            IntrRay3Triangle3 q = new IntrRay3Triangle3(ray, tri);

            q.Find();
            return(q);
        }
Esempio n. 2
0
        public static int FindHitTriangle_LinearSearch(NGonsCore.geometry3Sharp.mesh.DMesh3 mesh, Ray3d ray)
        {
            int        tNearestID = NGonsCore.geometry3Sharp.mesh.DMesh3.InvalidID;
            double     fNearestT  = double.MaxValue;
            Triangle3d tri        = new Triangle3d();

            foreach (int ti in mesh.TriangleIndices())
            {
                // [TODO] optimize this
                mesh.GetTriVertices(ti, ref tri.V0, ref tri.V1, ref tri.V2);
                IntrRay3Triangle3 ray_tri_hit = new IntrRay3Triangle3(ray, tri);
                if (ray_tri_hit.Find())
                {
                    if (ray_tri_hit.RayParameter < fNearestT)
                    {
                        fNearestT  = ray_tri_hit.RayParameter;
                        tNearestID = ti;
                    }
                }
            }

            return(tNearestID);
        }
Esempio n. 3
0
        IntrRay3Triangle3 find_added_hit(ref Ray3d ray, out int hit_tid)
        {
            hit_tid = DMesh3.InvalidID;
            IntrRay3Triangle3 nearest = null;
            double            dNearT  = double.MaxValue;

            Triangle3d tri = new Triangle3d();

            foreach (int tid in AddedT)
            {
                Index3i tv = EditMesh.GetTriangle(tid);
                tri.V0 = EditMesh.GetVertex(tv.a);
                tri.V1 = EditMesh.GetVertex(tv.b);
                tri.V2 = EditMesh.GetVertex(tv.c);
                IntrRay3Triangle3 intr = new IntrRay3Triangle3(ray, tri);
                if (intr.Find() && intr.RayParameter < dNearT)
                {
                    dNearT  = intr.RayParameter;
                    hit_tid = tid;
                    nearest = intr;
                }
            }
            return(nearest);
        }
Esempio n. 4
0
        // [RMS] this only tests some basic cases...
        public static void test_RayBoxIntersect()
        {
            Random rand = new Random(316136327);

            // check that box hit works
            for (int ii = 0; ii < 1000; ++ii)
            {
                // generate random triangle
                Triangle3d       t      = new Triangle3d(rand.PointInRange(10), rand.PointInRange(10), rand.PointInRange(10));
                AxisAlignedBox3d bounds = new AxisAlignedBox3d(t.V0);
                bounds.Contain(t.V1);
                bounds.Contain(t.V2);
                Vector3d c = (t.V0 + t.V1 + t.V2) / 3.0;
                for (int jj = 0; jj < 1000; ++jj)
                {
                    Vector3d d   = rand.Direction();
                    Ray3d    ray = new Ray3d(c - 100 * d, d);
                    IntrRay3AxisAlignedBox3 bhit = new IntrRay3AxisAlignedBox3(ray, bounds);
                    Debug.Assert(bhit.Find());
                    IntrRay3Triangle3 thit = new IntrRay3Triangle3(ray, t);
                    Debug.Assert(thit.Find());
                    Debug.Assert(bhit.RayParam0 < thit.RayParameter);
                }
            }

            int N = 100;

            for (int ii = 0; ii < N; ++ii)
            {
                // generate random boxes
                Vector3d         c     = rand.PointInRange(10);
                Vector3d         e     = rand.PositivePoint();
                AxisAlignedBox3d aabox = new AxisAlignedBox3d(c - e, c + e);
                Box3d            obox  = new Box3d(c, Vector3d.AxisX, Vector3d.AxisY, Vector3d.AxisZ, e);
                double           r     = aabox.DiagonalLength;

                // center-out tests
                for (int jj = 0; jj < N; ++jj)
                {
                    Ray3d ray = new Ray3d(c, rand.Direction());
                    assert_same_hit(aabox, obox, ray, true);
                }

                // outside-in tests
                for (int jj = 0; jj < N; ++jj)
                {
                    Vector3d p   = c + 2 * r * rand.Direction();
                    Ray3d    ray = new Ray3d(p, (c - p).Normalized);
                    assert_same_hit(aabox, obox, ray, true);
                }
            }



            // random rays
            int hits   = 0;
            int InnerN = 1000;

            for (int ii = 0; ii < N; ++ii)
            {
                // generate random boxe
                Vector3d c = rand.PointInRange(10);
                Vector3d e = rand.PositivePoint();

                // every tenth box, set an axis to degenerate
                if (ii % 10 == 0)
                {
                    e[rand.Next() % 3] = 0;
                }


                AxisAlignedBox3d aabox = new AxisAlignedBox3d(c - e, c + e);
                Box3d            obox  = new Box3d(c, Vector3d.AxisX, Vector3d.AxisY, Vector3d.AxisZ, e);
                double           r     = aabox.DiagonalLength;


                TrivialBox3Generator boxgen = new TrivialBox3Generator()
                {
                    Box = obox
                };
                boxgen.Generate();
                DMesh3 mesh = new DMesh3();
                boxgen.MakeMesh(mesh);

                for (int i = 0; i < InnerN; ++i)
                {
                    Vector3d target = c + rand.PointInRange(r);
                    Vector3d o      = c + rand.PointInRange(10 * r);
                    Ray3d    ray    = new Ray3d(o, (target - o).Normalized);
                    assert_same_hit(aabox, obox, ray, false);

                    int  hitT     = MeshQueries.FindHitTriangle_LinearSearch(mesh, ray);
                    bool bMeshHit = (hitT != DMesh3.InvalidID);
                    if (bMeshHit)
                    {
                        ++hits;
                    }
                    IntrRay3AxisAlignedBox3 aabbhit = new IntrRay3AxisAlignedBox3(ray, aabox);
                    Debug.Assert(aabbhit.Find() == bMeshHit);
                    Debug.Assert(aabbhit.Test() == bMeshHit);
                }
            }

            System.Console.WriteLine("hit {0} of {1} rays", hits, N * InnerN);
        }