Beispiel #1
0
        public void RunCodeSceneTest(string filename, string classname)
        {
            string fullpath = @"C:\Users\chris\mmbot\convexcad\convexcad\Scenes\" + filename;

            CSGScene res = (CSGScene)SceneRunner.ExecuteCode(fullpath, "convexcad", classname, "Run", false);

            RunDefaultSceneTest(res, filename + "." + classname + ".dat");
        }
Beispiel #2
0
            public void Run()
            {
                foreach (Node n in Children)
                {
                    n.Run();
                }

                if (CSGScene.NextStage("NodeCreate"))
                {
                    Create();
                    CSGScene.LastNode = this;
                }
            }
Beispiel #3
0
            public static CSGScene Load(string file_name)
            {
                CSGScene res = null;

                if (File.Exists(file_name))
                {
                    Stream          strm         = File.OpenRead(file_name);
                    BinaryFormatter deserializer = new BinaryFormatter();
                    deserializer.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
                    res = (CSGScene)deserializer.Deserialize(strm);
                    strm.Close();
                }
                return(res);
            }
Beispiel #4
0
        public void RunDefaultSceneTest(CSGScene scene, string filename)
        {
            string fullpath = @"C:\Users\chris\mmbot\convexcad\convexcad\Scenes\" + filename;

            if (File.Exists(fullpath))
            {
                CSGSceneTestFIle tst = CSGScene.LoadTestFile(fullpath);
                if (scene.Root == null)
                {
                    scene.Run();
                }
                Vertex[] vertices = null;
                Edge[]   edges    = null;
                scene.Root.GetWeldedGeometry(out vertices, out edges);
                CompareGeometry(tst, vertices, edges);
            }
            else
            {
                scene.SaveTestFile(fullpath);
            }
        }
Beispiel #5
0
            public static bool CalculateClippedConvexes2d(Convex a, Convex b, List <Convex> a_only, ref Convex overlap)
            {
                if (!DoConvexesOverlap2d(a, b))
                {
                    a_only.Add(a);
                    overlap = null;
                    return(false);
                }

                overlap = a.Copy();

                Point3D hp0, hp1;
                int     hedge0, hedge1;
                double  ht0, ht1;

                hp0    = new Point3D();
                hp1    = new Point3D();
                hedge0 = hedge1 = -1;
                ht0    = ht1 = 0;

                bool any_split = false;

                foreach (Edge bedge in b.Edges)
                {
                    Point3D  raystart = b.Vertices[bedge.VertIndices[0]].Pos;
                    Vector3D raydir   = b.Vertices[bedge.VertIndices[1]].Pos - raystart;

                    if (!CSGScene.NextStage("Test convex"))
                    {
                        break;
                    }

                    if (CSGScene.IsCurrentStage())
                    {
                        if (CSGScene.DebugLines != null)
                        {
                            overlap.DebugDraw();
                            CSGScene.DebugLines.AddLine(raystart - raydir * 10, raystart + raydir * 10);
                        }
                    }

                    if (overlap.RayIntersect2d(raystart, raydir, ref hp0, ref hedge0, ref ht0, ref hp1, ref hedge1, ref ht1))
                    {
                        if (!CSGScene.NextStage("Splitting convex"))
                        {
                            break;
                        }

                        if (CSGScene.IsCurrentStage())
                        {
                            if (CSGScene.DebugLines != null)
                            {
                                CSGScene.DebugLines.AddLine(raystart - raydir * 10, raystart + raydir * 10);
                            }
                        }

                        //CSGScene.DebugLines.AddCross(hp0, 1);
                        //CSGScene.DebugLines.AddCross(hp1, 1);
                        //CSGScene.DebugLines.AddLine(hp0, hp1);
                        Convex newa, newb;
                        newa = newb = null;
                        Split2d(overlap, hp0, hedge0, hp1, hedge1, ref newa, ref newb);

                        Vector3D rayorth = new Vector3D(-raydir.Y, raydir.X, 0);
                        if (Vector3D.DotProduct(rayorth, newa.GetCentre() - raystart) < 0)
                        {
                            a_only.Add(newb);
                            overlap = newa;
                        }
                        else if (Vector3D.DotProduct(rayorth, newb.GetCentre() - raystart) < 0)
                        {
                            a_only.Add(newa);
                            overlap = newb;
                        }
                        else
                        {
                            throw new System.ApplicationException("Double degerate or something!");
                        }
                        any_split = true;

                        if (CSGScene.IsCurrentStage())
                        {
                            if (CSGScene.DebugLines != null)
                            {
                                foreach (Convex newaonly in a_only)
                                {
                                    newaonly.DebugDraw();
                                }
                                overlap.DebugDraw();
                            }
                        }
                    }
                }

                if (!any_split)
                {
                    a_only.Add(overlap);
                    overlap = null;
                }
                return(any_split);
            }
        public void RunDefaultSceneTest(CSGScene scene, string filename)
        {
            string fullpath = @"C:\Users\chris\mmbot\convexcad\convexcad\Scenes\" + filename;

            if (File.Exists(fullpath))
            {
                CSGSceneTestFIle tst = CSGScene.LoadTestFile(fullpath);
                if(scene.Root == null)
                    scene.Run();
                Vertex[] vertices = null;
                Edge[] edges = null;
                scene.Root.GetWeldedGeometry(out vertices, out edges);
                CompareGeometry(tst, vertices, edges);
            }
            else
            {
                scene.SaveTestFile(fullpath);
            }

        }
Beispiel #7
0
            public override void Create()
            {
                if (Is3d)
                {
                }
                else
                {
                    //this'll be the clever algorithm. it takes advantage of the fact that we always maintain:
                    //- for any given node, none of it's child convexes overlap
                    //therefore:
                    //- no node should test it's own convexes against each other



                    //this is the simplest algorithm - we assume every convex could potentially overlap every other convex
                    //and keep iterating until no more splits occur. it works, but involves a lot of unnecessary overlap tests
                    Convexes = new List <Convex>();
                    foreach (Node n in Children)
                    {
                        Convexes.AddRange(n.Convexes.Select(a => a.Copy()));
                    }

                    //draw all initial convexes if this is the current stage
                    if (!CSGScene.NextStage("Begin union"))
                    {
                        foreach (Convex c in Convexes)
                        {
                            c.DebugDraw();
                        }
                        return;
                    }

                    //now do the iterative splitting
                    //loop until no splits done
                    bool done_split = true;
                    while (done_split)
                    {
                        //spin over every convex
                        done_split = false;
                        for (int i = 0; i < Convexes.Count; i++)
                        {
                            //go over every other convex
                            for (int j = i + 1; j < Convexes.Count; j++)
                            {
                                //get the 2 convexes to compare
                                Convex acvx = Convexes[i];
                                Convex bcvx = Convexes[j];

                                //do a clip test
                                List <Convex> otherconvexsplit = new List <Convex>();
                                Convex        overlap          = null;
                                if (Convex.CalculateClippedConvexes2d(acvx, bcvx, otherconvexsplit, ref overlap))
                                {
                                    //got a split, so remove the convex that was split (cvx a), and re-add the sections
                                    //that didn't overlap
                                    Convexes.RemoveAt(i);
                                    Convexes.AddRange(otherconvexsplit);
                                    done_split = true;

                                    //if last stage, draw the convex we were splitting and then bail
                                    if (!CSGScene.NextStage("Done a split"))
                                    {
                                        return;
                                    }
                                    break;
                                }
                            }

                            //break out (so we iterate round again) if a split happened
                            if (done_split)
                            {
                                break;
                            }
                        }
                    }
                }
            }