コード例 #1
0
        static void ExecuteCodeToPng(ScratchControl ctl, string code)
        {
            //Glyphics codeString object
            Code rasterCode = RasterLib.RasterApi.CreateCode(code);

            //Save final result to PNG file
            string filenameOrthogonal = "..\\..\\" + RasterLib.RasterApi.CodeToCodename(rasterCode).Name + "-Ortho.PNG";
            string filenameIsometric  = "..\\..\\" + RasterLib.RasterApi.CodeToCodename(rasterCode).Name + "-Isometric.PNG";

            //Execute, render, and save to png
            Grid grid = RasterLib.RasterApi.CodeToGrid(rasterCode);

            //Save png's to each
            if (ctl.FileNameOutOrthogonalAnimated)
            {
                GraphicsApi.SaveFlatPng(filenameOrthogonal, RasterLib.RasterApi.Renderer.RenderObliqueCells(grid));
            }

            if (ctl.FileNameOutIsometricAnimated)
            {
                GraphicsApi.SaveFlatPng(filenameIsometric, RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(grid,
                                                                                                                   ctl.IsometricBgRgba[0],
                                                                                                                   ctl.IsometricBgRgba[1],
                                                                                                                   ctl.IsometricBgRgba[2],
                                                                                                                   ctl.IsometricBgRgba[3],
                                                                                                                   ctl.IsometricCellWidth, ctl.IsometricCellHeight));
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        public static void DoAnimation(ScratchControl ctl, string rawCode, string prefix)
        {
            if (rawCode.Contains(","))
            {
                rawCode = rawCode.Split(',')[1].TrimStart();
            }
            string    codeString = prefix + "," + rawCode;
            Code      code       = RasterLib.RasterApi.CreateCode(codeString);
            TokenList tokens     = RasterLib.RasterApi.CodeToTokens(code);

            int tokenId     = 0;
            int actualCount = 1;

            while (tokenId < tokens.Count)
            {
                Console.WriteLine(tokenId + " / " + tokens.Count);

                //Skip pen changes, they are boring
                string str = tokens.GetToken(tokenId).ToString();

                if (str.StartsWith("Pen") == false)
                {
                    //Get clean triple-# padded numbering for filenames
                    string prependNumber = "";
                    if (actualCount < 100)
                    {
                        prependNumber += "0";
                    }
                    if (actualCount < 10)
                    {
                        prependNumber += "0";
                    }

                    string curCode = prefix + "-" + prependNumber + actualCount + ",";
                    for (int i = 0; i <= tokenId; i++)
                    {
                        curCode += tokens.GetToken(i) + ";";
                    }
                    if (tokenId > 1)
                    {
                        //Console.WriteLine("Token " + curCode);
                        ExecuteCodeToPng(ctl, curCode);
                        actualCount++;
                    }
                }
                tokenId++;
            }
        }
コード例 #4
0
        public static void SuperDebug(ScratchControl ctl)
        {
            Grid grid = null;

            if (ctl.FileNameInCode == null && ctl.FileNameInImage == null && ctl.FileNameInStl == null && ctl.FileNameInSvg == null)
            {
                Console.WriteLine("Must have FileNameIn type.");
                return;
            }

            if (ctl.FileNameInSvg != null)
            {
                Console.WriteLine("SVG Input filename: {0}", ctl.FileNameInSvg);
                string codeString = Svg2Gly.ConvertSvg2Gly(ctl.FileNameInSvg);
                Console.WriteLine("SVG:\n" + codeString);

                Code code = RasterLib.RasterApi.CreateCode(codeString);
                if (ctl.Resize != null)
                {
                    code = RasterLib.RasterApi.CodeToRescaledCode(code, ctl.Resize[0], ctl.Resize[1], ctl.Resize[2]);
                }

                Console.WriteLine("Code: {0}\n", codeString);
                grid = RasterLib.RasterApi.CodeToGrid(code);
            }
            else if (ctl.FileNameInStl != null)
            {
                grid = RasterLib.RasterApi.CreateGrid(64, 64, 64, 4);

                Console.WriteLine("STL Input filename: {0}", ctl.FileNameInStl);

                //Load the triangles from the STL file and reduce to a unit 1x1x1 size
                Triangles triangles = RasterLib.RasterApi.StlToTriangles(ctl.FileNameInStl);
                triangles.ReduceToUnit();
                Console.WriteLine("Triangle count: {0}", triangles.Count);

                //Render the triangles to the grid, will autosize to grid size
                RasterLib.RasterApi.Renderer.RenderTrianglesToGrid(triangles, grid);
            }
            else if (ctl.FileNameInImage != null)
            {
                Grid        gridImg = GraphicsApi.FileToGrid(ctl.FileNameInImage);
                GridContext gc      = new GridContext(gridImg);
                RasterLib.RasterApi.Painter.FlipY(gc);
                int div = 63;// 31;// 15;
                grid = RasterLib.RasterApi.CreateGrid(gridImg.SizeX, 255 / div + 1, gridImg.SizeY, 4);

                Console.WriteLine("Extruding");
                Extrusion(grid, gridImg, div);
                Console.WriteLine("Rendering");
                Grid gridIsometricScaledQuick = RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(grid, 0, 0, 0, 0, 4, 4);
                Console.WriteLine("Saving");
                GraphicsApi.SaveFlatPng(ctl.FileNameOutIsometric, gridIsometricScaledQuick);
                Console.WriteLine("Saved");
            }
            else if (ctl.FileNameInCode != null)
            {
                string codeString = RasterLib.RasterApi.ReadGlyc(ctl.FileNameInCode).Replace(';', '\n');

                Code code = RasterLib.RasterApi.CreateCode(codeString);
                Console.WriteLine("Code: {0}\n", codeString);

                Codename codename = RasterLib.RasterApi.CodeToCodename(code);
                Console.WriteLine("Codename: {0}\n", codename);

                if (ctl.OutputTokens)
                {
                    TokenList glyphTokens = RasterLib.RasterApi.CodeToTokens(code);
                    string    tokenDesc   = "Tokens:\n" + glyphTokens + "\n";
                    Console.WriteLine(tokenDesc);
                }

                if (ctl.Resize != null)
                {
                    code = RasterLib.RasterApi.CodeToRescaledCode(code, ctl.Resize[0], ctl.Resize[1], ctl.Resize[2]);
                }

                grid = RasterLib.RasterApi.CodeToGrid(code);
                Console.WriteLine("Grid: {0}\n", grid);

                if (ctl.FileNameOutOrthogonalAnimated || ctl.FileNameOutIsometricAnimated)
                {
                    ControlAnimator.DoAnimation(ctl, code.codeString, codename.ToString());
                }
            }

            //DownSolver ds = new GeneralLibrary.DownSolver(codeString);

            if (ctl.OutputBytes && grid != null)
            {
                string bytesDesc = RasterLib.RasterApi.BytesToString(grid.CloneData());
                Console.WriteLine("GridBytes:\n{0}\n", bytesDesc);
            }

            RectList rects = RasterLib.RasterApi.GridToRects(grid);

            if (ctl.OutputRectangles)//rects
            {
                Console.WriteLine("Rects: {0}\n{1}", rects.Count, rects);
                RasterLib.RasterApi.BuildCircuit(rects, true);

                string serialized = RasterLib.RasterApi.RectsToSerializedRectsLimit255(rects).SerializedData;
                Console.WriteLine("Serialized Rects: (len={0})\n{1}\n", serialized.Length, serialized);

                RasterLib.RasterApi.SaveFlatText("..\\..\\foo.txt", serialized);
                //Clipboard.SetText(serialized);
                SerializedRects serializedRects = new SerializedRects(serialized);
                RectList        rectsDecoded    = RectConverter.SerializedRectsToRects(serializedRects);

                int count = 0;
                Console.WriteLine("# of Decoded Rects = {0}", rectsDecoded.Count);
                foreach (Rect rect in rectsDecoded)
                {
                    Console.WriteLine(count + " : " + rect);
                    count++;
                }
            }

            if (ctl.OutputRenderedAscii)
            {
                Console.WriteLine("2d view:\n{0}", RasterLib.RasterApi.Renderer.GridToHexDescription(grid));
                Console.WriteLine("3d view:\n{0}", RasterLib.RasterApi.Renderer.GridTo3DDescription(grid, 0, 0, 0));
            }

            if (ctl.OutputSceneGraph)
            {
                Scene     scene     = RasterLib.RasterApi.RectsToScene(rects); Console.WriteLine("Scene: {0}", scene);
                QuadList  quads     = RasterLib.RasterApi.RectsToQuads(rects); Console.WriteLine("Quads: {0}", quads);
                Triangles triangles = RasterLib.RasterApi.QuadsToTriangles(quads); Console.WriteLine("Triangles: {0}", triangles);
            }

            if (ctl.FileNameOutStl != null)
            {
                ControlStl.Control(ctl, rects);
            }

            if (ctl.FileNameOutOrthogonal != null)
            {
                Console.WriteLine("Orthogonal Rendering..");
                Grid gridOrthogonal = RasterLib.RasterApi.Renderer.RenderObliqueCells(grid);
                GraphicsApi.SaveFlatPng(ctl.FileNameOutOrthogonal, gridOrthogonal);
            }

            if (ctl.FileNameOutIsometric != null)
            {
                Console.WriteLine("Isometric Rendering..");
                if (ctl.IsometricCellWidth < 1)
                {
                    ctl.IsometricCellWidth = 1;
                }
                if (ctl.IsometricCellHeight < 1)
                {
                    ctl.IsometricCellHeight = 1;
                }
                Grid gridIsometricScaled = RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(grid,
                                                                                                   ctl.IsometricBgRgba[0],
                                                                                                   ctl.IsometricBgRgba[1],
                                                                                                   ctl.IsometricBgRgba[2],
                                                                                                   ctl.IsometricBgRgba[3],
                                                                                                   ctl.IsometricCellWidth, ctl.IsometricCellHeight);
                GraphicsApi.SaveFlatPng(ctl.FileNameOutIsometric, gridIsometricScaled);
            }

            Console.WriteLine();
        }