//Render rectangles out to Stl using a list to select shapes from
        public Triangles RenderRectsAsStlMapping(RectList rects, TrianglesList trianglesList)
        {
            if (rects == null || trianglesList == null)
            {
                return(null);
            }

            var renderedTriangleSoup = new List <Triangle>();

            foreach (Rect rect in rects)
            {
                if (rect.Properties.ShapeId == 0 || rect.Properties.ShapeId >= trianglesList.Count)
                {
                    rect.Properties.ShapeId = 1;
                }
                if (rect.Properties.ShapeId < trianglesList.Count)
                {
                    Triangles triangles = trianglesList.GetTriangles(rect.Properties.ShapeId);

                    MapRectToTriangles(ref renderedTriangleSoup, triangles, rect);
                }
            }
            Triangles triangleSet = new Triangles();

            Triangle[] triangleArray = renderedTriangleSoup.ToArray();
            triangleSet.SetTriangles(triangleArray);

            //Center entire grid/set on origin, mirror, and rotate to correct
            triangleSet.Translate(-rects.SizeX / 2.0f, -rects.SizeY / 2.0f, -rects.SizeZ / 2.0f);

            return(triangleSet);
        }
    /// <summary>
    /// Reset
    /// </summary>
    private void Clear()
    {
        in_mesh_f = GetComponent <MeshFilter>();
        in_mesh_r = GetComponent <MeshRenderer>();
        in_       = gameObject;

        out_       = new GameObject();
        out_mesh_f = out_.GetComponent <MeshFilter>() ? out_.GetComponent <MeshFilter>() : out_.AddComponent <MeshFilter>();
        out_mesh_r = out_.GetComponent <MeshRenderer>() ? out_.GetComponent <MeshRenderer>() : out_.AddComponent <MeshRenderer>();
        out_.name  = "Reconstruted_" + in_mesh_f.name;
        out_.transform.position   = in_.transform.position + new Vector3(3, 0, 0);
        out_.transform.localScale = in_.transform.localScale;

        out_triangles_ = new TrianglesList();

        superVertices_ = new List <SuperVertice>();
        for (int i = 0; i < in_mesh_f.mesh.vertices.Length; i++)
        {
            superVertices_.Add(new SuperVertice(i, in_mesh_f.mesh.vertices[i]));
        }

        out_mesh_f.mesh.vertices = in_mesh_f.mesh.vertices;
        out_mesh_r.material      = in_mesh_r.material;

        in_triangles_ = new TrianglesList(in_mesh_f.mesh.triangles, ref superVertices_);
        out_triangles_.unaltered_triangles = in_triangles_.triangles;
        nbTriangle     = in_triangles_.triangles.Count;
        nbTriangle_dec = nbTriangle;
        v3Triangles    = in_triangles_.ToFloatHeightList();
        intervals_     = new List <int>();
        //computed = false;
    }
        private void InsertNewTriangle()
        {
            while (_io.DoesUserWantInputeTriangle())
            {
                TrianglesList trianglesList = null;

                if (_validator.CheckInsertedString(out trianglesList, _io.GetNewTriangleString()))
                {
                    for (int i = 0; i < trianglesList.Triangles.Count; i++)
                    {
                        if (_triangles.AddTriangles(trianglesList.Triangles[i]))
                        {
                            _io.ShowMessage(CREATION_SUCCESSFUL);
                            continue;
                        }
                        else
                        {
                            _io.ShowMessage(CREATION_FAIL);
                        }
                    }
                }
                else
                {
                    _io.ShowMessage(WRONG_FORMAT);
                }
            }
        }
        public bool CheckInsertedString(out TrianglesList list, params string[] args)
        {
            string s = "";

            string[] arr  = null;
            bool     isOk = false;

            list = null;

            if (args.Length > 1)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    s += args[i];
                }
            }
            else
            {
                s = args[0];
            }

            arr = GetTriangleFormatedArr(s);

            if (arr != null)
            {
                list = GetTrianglesList(arr);
                isOk = true;
            }

            return(isOk);
        }
Beispiel #5
0
        public static void Control(ScratchControl ctl, RectList rects)
        {
            //Then render that to triangles
            Console.WriteLine("Creating triangle library");
            TrianglesList trianglesList = RasterLib.RasterApi.CreateTrianglesList();

            foreach (string filename in ctl.FileNamesInStlLibrary)
            {
                trianglesList.ImportAndReduceToUnit(filename);
            }

            //Render the rectangles out as shapes(Triangles) to a new set of triangles
            Triangles triangles = RasterLib.RasterApi.Renderer.RenderRectsAsStlMapping(rects, trianglesList);

            Console.WriteLine("Rendering triangles to grid");

            //Reduce scale to 1x1x1, making it 1mm x 1mm x 1mm
            triangles.CalcNormals();
            triangles.ReduceToUnit();
            triangles.Translate(0.5f, 0.5f, 0.5f);

            //Scale up to make an exactly sized models in inches then millimeters
            const float finalSizeInInches      = 2;
            const float finalSizeInMillimeters = finalSizeInInches * 25.4f; //Inches to millimeters

            triangles.Scale(finalSizeInMillimeters, finalSizeInMillimeters, finalSizeInMillimeters);

            //Save final result to STL file
            Console.WriteLine("Saving triangles to {0}", ctl.FileNameOutStl);
            RasterLib.RasterApi.SaveTrianglesToStlAscii(ctl.FileNameOutStl, triangles);

            //So.. as long as we are here.. let's make a preview

            //Since we can, normalize it now
            triangles.ReduceToUnit();

            //Save a rendering out to a PNG, why not, too.
            Console.WriteLine("Creating preview grid");
            Grid gridFromStl = RasterLib.RasterApi.CreateGrid(96, 96, 96, 4);

            Console.WriteLine("Rendering triangles to grid");
            RasterLib.RasterApi.Renderer.RenderTrianglesToGrid(triangles, gridFromStl);

            //Then render to a new grid
            Console.WriteLine("Rendering grid to oblique preview grid");
            Grid gridObliqueRendered = RasterLib.RasterApi.Renderer.RenderObliqueCells(gridFromStl);

            //Then save
            if (ctl.FileNameOutStlPreview != null)
            {
                Console.WriteLine("Saving file to {0}", ctl.FileNameOutStlPreview);
                GraphicsApi.SaveFlatPng(ctl.FileNameOutStlPreview, gridObliqueRendered);
            }
        }
        private TrianglesList GetTrianglesList(string[] arr)
        {
            TrianglesList trianglesList = new TrianglesList();

            if (arr == null)
            {
                return(trianglesList);
            }
            else
            {
                for (int i = 0; i < arr.Length; i++)
                {
                    string[] name = arr[i].Split(',');

                    trianglesList.CheckTriangleString(name[0], GetDbl(name[1]), GetDbl(name[2]), GetDbl(name[3]));
                }
            }

            return(trianglesList);
        }
        private void CheckInsertedArgs(string[] args)
        {
            TrianglesList tmp = null;

            if (args.Length == 0)
            {
                _io.ShowRules(RULES);
            }
            else
            {
                // _triangles = _validator.CheckInsertedString(args);

                if (_validator.CheckInsertedString(out tmp, args))
                {
                    _io.ShowTrianglesList(_triangles.GetTrinaglesList());
                }
                else
                {
                    _io.ShowMessage(CONVERTATION_FAIL);
                    _io.ShowRules(RULES);
                }
            }
        }
Beispiel #8
0
        /* Example:
         * Purpose:
         *
         * Example concepts:
         *  1) Create Code from code string
         *  2) Extracting codename from Code
         *  3) Loading library of Triangles
         *  4) Rendering Grid to Triangles
         *  5) Scaling for absolute real-world dimensions in inches
         *  6) Converting inches to millimeters
         *  7) Saving New STL to file
         */
        static void Main()
        {
            //Glyphics codeString string

            /*
             *                    const string code = @"PrintableNexus,Size3D4 64 64 64;Spawn 25 5 25;PenShape 1;
             *        PenColorD4 31 127 255 255;WallCube 1;
             *
             *        PenColorD4 255 255 255 255;PenSize 1 2 1;Rect 0 0 0 31 0 31;Rect 0 0 32 31 0 63;Rect 32 0 0 63 0 31;Rect 32 0 32 63 0 63;Rect 16 0 16 48 0 48;
             *        PenSize 1 1 1;PenColorD4 31 127 255 255;FillRect 17 0 17 47 0 47;FillRect 16 1 49 48 16 63;
             *        PenColorD4 0 0 0 0;
             *        FillRect 17 1 49 47 15 63;
             *        Rect 0 1 0 63 63 63;
             *        ImgEdgeX 255 255 255 255;ImgEdgeY 255 255 255 255;ImgEdgeZ 255 255 255 255;
             #Now draw the multicolor volumes
             *        PenShape 2;
             *        PenColorD3 127 255 127;FillRect 2 1 2 13 12 13;
             *        PenColorD3 255 127 127;FillRect 2 1 18 13 12 29;
             *        PenColorD3 127 127 255;FillRect 2 1 34 13 12 45;
             *        PenColorD3 255 255 127;FillRect 2 1 50 13 12 61;
             *        PenColorD3 255 127 255;FillRect 18 1 2 29 12 13;
             *
             # Shape on top
             #        PenShape 3;PenColorD3 255 255 255;FillRect 26 17 51 36 28 62;
             #
             #Finally create a mirror image to the other side
             #        ImgMirrorX
             #        ";
             */
            const string code = @"_Palette,
Size3D4 16 16 16
PenShape 1
PenColorD4 31 127 255 255
WallCube 1

PenShape 1
Plot 2 1 2
Rect 1 1 1 14 2 14
FillRect 4 1 4 11 10 11

#Cylinders
PenShape 2
Rect 1 3 1 2 12 2

#Cone
PenShape 3
Rect 1 13 1 2 14 2
FillRect 4 10 4 11 16 11

ImgMirrorX
ImgMirrorZ
";

            //Then render that to triangles
            string dir = "C:\\Github\\Glyphics2\\Stl Files\\";

            Console.WriteLine("Creating triangle library");

            TrianglesList trianglesList = RasterLib.RasterApi.CreateTrianglesList();

            trianglesList.ImportAndReduceToUnit(dir + "Box.stl");                //1
            trianglesList.ImportAndReduceToUnit(dir + "Cylinder.stl");           //2
            trianglesList.ImportAndReduceToUnit(dir + "Cone.stl");               //3
            trianglesList.ImportAndReduceToUnit(dir + "Wedge.stl");              //3
            trianglesList.ImportAndReduceToUnit(dir + "WedgeCorn1.stl");         //4
            trianglesList.ImportAndReduceToUnit(dir + "WedgeCorn2.stl");         //5
            trianglesList.ImportAndReduceToUnit(dir + "WedgeCurved.stl");        //6
            trianglesList.ImportAndReduceToUnit(dir + "WedgeCurvedCorner1.stl"); //7
            trianglesList.ImportAndReduceToUnit(dir + "WedgeCurvedCorner2.stl"); //8

            //            const string codeString = @"PrintableNexus,Size3D4 4 4 4;PenColorD4 255 255 255 255;FillRect 0 0 0 4 4 4;";
            Console.WriteLine("Code: {0}", code);

            //Glyphics codeString object
            Code rasterCode = RasterLib.RasterApi.CreateCode(code);

            //Extract codename from codeString object, to use for filename
            Codename codename = RasterLib.RasterApi.CodeToCodename(rasterCode);

            //Create filename
            string outputFilename = dir + codename.Name + ".STL";

            Console.WriteLine("\nOutput Filename: {0}", outputFilename);

            //Convert the codeString to actual grid
            Console.WriteLine("Code to grid");
            Grid grid = RasterLib.RasterApi.CodeToGrid(rasterCode);

            //Convert to rects
            Console.WriteLine("Grid to rects");
            RectList rects = RasterLib.RasterApi.GridToRects(grid);

//            Triangles tris = trianglesList.GetTriangles(2);
//          Console.WriteLine(tris);

            /*
             * const string filename1 = "C:\\Github\\Glyphics2\\Examples\\ExampleCodeToSTL\\cube_ascii.stl";
             * const string filename2 = "C:\\Github\\Glyphics2\\Examples\\ExampleCodeToSTL\\archquad.stl";
             * const string filename3 = "C:\\Github\\Glyphics2\\Examples\\ExampleCodeToSTL\\pipesphere.stl";
             *
             * //Import the models and make sure they are unit sized
             * trianglesList.ImportAndReduceToUnit(filename1);
             * trianglesList.ImportAndReduceToUnit(filename2);
             * trianglesList.ImportAndReduceToUnit(filename3);
             */
            //Render the rectangles out as shapes(Triangles) to a new set of triangles
            Triangles triangles = RasterLib.RasterApi.Renderer.RenderRectsAsStlMapping(rects, trianglesList);

            Console.WriteLine("Rendering triangles to grid");

            //Reduce scale to 1x1x1, making it 1mm x 1mm x 1mm
            triangles.CalcNormals();
            triangles.ReduceToUnit();
            triangles.Translate(0.5f, 0.5f, 0.5f);

            //Scale up to make an exactly sized models in inches then millimeters
            const float finalSizeInInches      = 2;
            const float finalSizeInMillimeters = finalSizeInInches * 25.4f; //Inches to millimeters

            triangles.Scale(finalSizeInMillimeters, finalSizeInMillimeters, finalSizeInMillimeters);

            //Save final result to STL file
            Console.WriteLine("Saving triangles to {0}", outputFilename);
            RasterLib.RasterApi.SaveTrianglesToStlAscii(outputFilename, triangles);

            //So.. as long as we are here.. let's make a preview

            //Since we can, normalize it now
            triangles.ReduceToUnit();

            //Save a rendering out to a PNG, why not, too.
            Console.WriteLine("Creating preview grid");
            Grid gridFromStl = RasterLib.RasterApi.CreateGrid(96, 96, 96, 4);

            Console.WriteLine("Rendering triangles to grid");
            RasterLib.RasterApi.Renderer.RenderTrianglesToGrid(triangles, gridFromStl);

            //Then render to a new grid
            Console.WriteLine("Rendering grid to oblique preview grid");
            Grid gridObliqueRendered = RasterLib.RasterApi.Renderer.RenderObliqueCells(gridFromStl);

            //Then save
            //const string filenamePreview = "..\\..\\preview.png";
            //Console.WriteLine("Saving file to {0}", filenamePreview);
            //GraphicsApi.SaveFlatPng(filenamePreview, gridObliqueRendered);

            //.. and write finish
            Console.WriteLine("\nDone.");
        }