//Serialize a set of rectangles(RectList) to SerializedRects public static SerializedRects RectsToSerializedRects(RectList rectSet) { GridConverter.BuildCircuit(rectSet, false); var sb = new StringBuilder(); bool mode2D = false;// RectsAre2D(rectSet); //Sort the rectlist into sets of rectlists where rects have same properties IEnumerable <List <Rect> > superRects = SuperSortByProperties(rectSet); //Iterate through entire set of rectlists foreach (List <Rect> rectList in superRects) { string rectPropertiesSerialized = SerializeRectProperties(rectList[0].Properties, mode2D); sb.Append(rectPropertiesSerialized); //Iterate through each rect in the set (will all have same properties) //Each will inherit properties values when read later from last properties write foreach (Rect rect in rectList) { string rectSerialized = Transcode64.SerializeRectPointsToString(rect, 1, mode2D); rectSerialized += SerializeCircuitProperties(rectList[0].Properties); sb.Append(rectSerialized); } } //Then return new serialized rect with that string return(new SerializedRects(sb.ToString())); }
//Load a glyphics file, precompute rectangles and append them to codeString as speedy cache public static void PreSerializeGlyphicsFile(string filename) { CodeList codes = GlyToCodes(filename); foreach (Code t in codes) { string code = t.codeString; if (code.Contains("*")) { code = code.Split('*')[0]; } Grid grid = CodeConverter.TokensToGrid(CodeConverter.CodeToTokens(new Code(code))); if (grid != null) { RectList rectSet = GridConverter.GridToRects(grid); string serializedRects = RectConverter.RectsToSerializedRects(rectSet).SerializedData; code = code + serializedRects; t.codeString = code; } } CodesToGly(filename, codes); }
public static bool HasPathConstrained(RectList rects, bool[,] graph, int i, int j, int depth) { if (depth > 6) { return(false); } Rect rect1 = rects.GetRect(i); Rect rect2 = rects.GetRect(j); if (!DirectLinkingType((PhysicsType)rect1.Properties.PhysicsId, (PhysicsType)rect2.Properties.PhysicsId)) { return(false); } if (graph[i, j]) //direct path { return(true); } //If didn't find, then recurse for (int q = 0; q < graph.GetLength(0); q++) { if (graph[i, q]) { if (HasPathConstrained(rects, graph, q, j, depth + 1)) { return(true); } } } return(false); }
public static void AssignCircuitIdsToNonGates(RectList rects, List <List <int> > touchGraph, List <List <int> > circuitIds) { int circuitId = 0; var listid = 0; foreach (List <int> list in touchGraph) { circuitIds.Add(new List <int>()); if (list.Count > 0) { if (rects.GetRect(list[0]).Properties.PhysicsId >= (byte)PhysicsType.GateNot) { } else { //Make sure that we don't assign circuitid 0 if circuit type if (rects.GetRect(list[0]).Properties.PhysicsId > 0 && circuitId == 0) { circuitId = 1; } circuitIds[listid].Add(circuitId); circuitId++; } } listid++; } }
//Limits it to 255 strings between carriage returns public static string SerializeLimit255(RectList rectSet) { IEnumerable <List <Rect> > superRects = SuperSortByProperties(rectSet); string str = ""; string runStr = ""; bool mode2D = false;// RectsAre2D(rectSet); foreach (List <Rect> rectList in superRects) { runStr += SerializeRectProperties(rectList[0].Properties, mode2D); foreach (Rect t in rectList) { runStr += Transcode64.SerializeRectPointsToString(t, 1, mode2D); runStr += SerializeCircuitProperties(rectList[0].Properties); if (runStr.Length > 200)//236) { str += runStr + CharLimit255; runStr = SerializeRectProperties(rectList[0].Properties, mode2D); } } } str += runStr + CharLimit255; str = str.TrimEnd('\n') + "\n";//ensure only one \n at end return(str); }
public static void Vox2Glyc(string dirName, string name) { Console.Write("Vox2Glyc converting: " + name + " ... "); VoxFile_VoxelSet voxels = null; voxels = ReadVox(dirName + name + ".vox"); Grid grid = RasterLib.RasterApi.CreateGrid(64, 64, 64, 4); for (int i = 0; i < voxels.voxels.Length; i++) { VoxFile_Voxel voxel = voxels.voxels[i]; CellProperties cp = new CellProperties(); byte r = (byte)voxel.I; byte g = (byte)voxel.I; byte b = (byte)voxel.I; byte a = (byte)voxel.I; r = (byte)((voxels.palette[voxel.I] >> 0) & 255); g = (byte)((voxels.palette[voxel.I] >> 8) & 255); b = (byte)((voxels.palette[voxel.I] >> 16) & 255); a = (byte)((voxels.palette[voxel.I] >> 24) & 255); cp.Rgba = RasterLib.RasterApi.Rgba2Ulong(r, g, b, a); grid.Plot(voxel.X, voxel.Y, voxel.Z, cp); } RectList rects = RasterLib.RasterApi.GridToRects(grid); RasterLib.Language.Code code = RasterLib.RasterApi.RectsToCode(rects); //Console.WriteLine(code.codeString); RasterLib.RasterApi.CodeToGlyC(dirName + name + ".glyc", name + ",\n" + code.codeString); }
public static Rect RectsToBoundaries(RectList rectSet) { if (rectSet == null) { return(null); } return(rectSet.Boundaries); }
public static Grid RectsToGrid(RectList rectSet) { Rect rect = RasterLib.RasterApi.RectsToBoundaries(rectSet); Grid grid = new Grid((int)rect.Pt2[0], (int)rect.Pt2[1], (int)rect.Pt2[2], 4); RasterLib.RasterApi.Renderer.RenderRectsToGrid(rectSet, grid); return(grid); }
//Rect(s)-To public static Code RectsToCode(RectList rectSet) { if (rectSet == null) { return(null); } return(RectConverter.RectsToCode(rectSet)); }
public static void WriteMesh(string filename, RectList rects) { List <ulong> palette = FindRgbaPalette(rects); _nodeid = 1; var scene = new ColladaScene { Id = _nodeid++ }; var collada = new Collada(); collada.Scenes.Add(scene); foreach (ulong rgba in palette) { var effect = new ColladaEffect(); Converter.Ulong2Rgba(rgba, out effect.R, out effect.G, out effect.B, out effect.A); var material = new ColladaMaterial { Id = _nodeid++ }; effect.Id = _nodeid++; material.Url = effect.Id; effect.Rgba = rgba; collada.Materials.Add(material); collada.Effects.Add(effect); } foreach (Rect rect in rects) { var geom = new ColladaGeometry { Id = _nodeid++, PositionId = _nodeid++, NormalId = _nodeid++, PositionId2 = _nodeid++, NormalId2 = _nodeid++, VertexId = _nodeid++ }; int material = collada.FindMaterial(rect.Properties.Rgba); geom.Materialid = material; geom.X1 = rect.Pt1[0]; geom.Y1 = rect.Pt1[1]; geom.Z1 = rect.Pt1[2]; geom.X2 = rect.Pt2[0]; geom.Y2 = rect.Pt2[1]; geom.Z2 = rect.Pt2[2]; collada.Geometries.Add(geom); scene.Geometry.Add(geom.Id); } collada.WriteMesh(filename); }
public static void AddPathsConstrained(RectList rects, bool[,] graph, List <int> list, int id) { for (int j = 0; j < graph.GetLength(0); j++) { if (HasPathConstrained(rects, graph, id, j, 0) && !list.Contains(j)) { list.Add(j); } } }
//Convert an Scene to an RectList public static RectList SceneToRects(Scene scene) { RectList rects = new RectList(); foreach (Element element in scene) { Rect rect = ElementToRect(element); rects.AddRect(rect); } return(rects); }
public static bool RectsAre2D(RectList rects) { foreach (Rect rect in rects) { if (rect.Pt2[1] > 1) { return(false); } } return(true); }
//Convert an RectList to an Scene public static Scene RectsToScene(RectList rects) { Scene scene = new Scene(); foreach (Rect rect in rects) { Element element = RectToElement(rect); scene.AddElement(element); } return(scene); }
public static List <ulong> FindRgbaPalette(RectList rects) { var palette = new List <ulong>(); foreach (Rect rect in rects) { if (palette.Contains(rect.Properties.Rgba) == false) { palette.Add(rect.Properties.Rgba); } } return(palette); }
public void FromRects(RectList inRects) { Rects = inRects; SerializedRects = RasterLib.RasterApi.RectsToSerializedRects(Rects); SerializedRectsLimit255 = RasterLib.RasterApi.RectsToSerializedRectsLimit255(Rects); JSON = SceneToJson.Convert(RasterLib.RasterApi.RectsToScene(Rects)); if ((enableFlags & enables.DoScene) == enables.DoScene) { FromSerializedRects(SerializedRects); } }
//Input: Glyphics code //Output: Rectangles //Side-Effects: May use deserialized cache at end of codeString instead public static RectList CodeToRects(Code rasterCode) { string code = rasterCode.codeString; if (code.Contains("" + CharRgba)) { string start = code.Substring(code.IndexOf('*')); return(SerializedRectsToRects(new SerializedRects(start))); } RectList rectSet = GridConverter.GridToRects(RasterApi.TokensToGrid(RasterApi.CodeToTokens(rasterCode))); return(rectSet); }
public static void CalculateTouchGraphPaths(bool[,] graph, RectList rects, List <List <int> > touchGraph) { for (int i = 0; i < graph.GetLength(0); i++) { if (!FindInList(touchGraph, i)) { List <int> newList = new List <int> { i }; AddPathsConstrained(rects, graph, newList, i); touchGraph.Add(newList); } } }
//Convert Grid into minimal/optimal List of rectangles public static RectList GridToRects(Grid grid) { IEnumerable <Rect> rects = GridToAabb(grid); RectList rectList = new RectList(); foreach (Rect rect in rects) { rectList.AddRect(rect); } rectList.SizeX = grid.SizeX; rectList.SizeY = grid.SizeY; rectList.SizeZ = grid.SizeZ; return(rectList); }
public void FromSerializedRects(SerializedRects inSerializedRects) { SerializedRects = inSerializedRects; Rects = RasterLib.RasterApi.SerializedRectsToRects(SerializedRects); if ((enableFlags & enables.DoQuads) == enables.DoQuads) { Quads = RasterLib.RasterApi.RectsToQuads(Rects); } if ((enableFlags & enables.DoTriangles) == enables.DoTriangles) { FromQuads(Quads); } }
public static List <List <int> > CreateTouchGraphFromRects(RectList rects) { var touchGraph = new List <List <int> > { new List <int>() }; for (int i = 0; i < rects.Count; i++) { if (rects.GetRect(i).Properties.PhysicsId == 0) { touchGraph[0].Add(i); } } return(touchGraph); }
public static void DisplayAdjacencyGraph(RectList rects, bool[,] graph) { Console.WriteLine("Adjacency graph:"); for (int i = 0; i < rects.Count; i++) { Console.Write(i + "=" + rects.GetRect(i).Properties.PhysicsId + "\t: "); for (int j = 0; j < rects.Count; j++) { if (graph[i, j]) { Console.Write(j + "=" + rects.GetRect(j).Properties.PhysicsId + " "); } } Console.WriteLine(); } }
//Converts a set of rectangles back into Glyphics codeString that can be modified/executed public static Code RectsToCode(RectList rects) { IEnumerable <List <Rect> > superRects = SuperSortByProperties(rects); //bool mode2D = false;// RectsAre2D(rectSet); ulong rgba = 0x12345678;//Set to unlikely initial color, then detect changes var sb = new StringBuilder(); string sizeStr = "Size3D4 " + (rects.Boundaries.Width + 1) + " " + (rects.Boundaries.Height + 1) + " " + (rects.Boundaries.Depth + 1); sb.Append(sizeStr); foreach (List <Rect> rectList in superRects) { Rect rectfirst = rectList[0]; rgba = rectfirst.Properties.Rgba; byte r, g, b, a; Converter.Ulong2Rgba(rectfirst.Properties.Rgba, out r, out g, out b, out a); string rgbaStr = ";PenColorD4 " + r + " " + g + " " + b + " " + a; sb.Append(rgbaStr); foreach (Rect rect in rectList) { sb.Append(";FillRect " + rect.Pt1[0] + " " + rect.Pt1[1] + " " + rect.Pt1[2] + " " + (rect.Pt2[0] - 1) + " " + (rect.Pt2[1] - 1) + " " + (rect.Pt2[2] - 1)); } } /* * * foreach (Rect rect in rects) * { * if (rect.Properties.Rgba != rgba) //Only emit PenColor when it changes * { * rgba = rect.Properties.Rgba; * byte r, g, b, a; * Converter.Ulong2Rgba(rect.Properties.Rgba, out r, out g, out b, out a); * string rgbaStr = ";\nPenColorD4 " + r + " " + g + " " + b + " " + a; * sb.Append(rgbaStr); * } * sb.Append(";FillRect " + rect.Pt1[0] + " " + rect.Pt1[1] + " " + rect.Pt1[2] + " " + rect.Pt2[0] + " " + rect.Pt2[1] + " " + rect.Pt2[2]); * } */ string codeStr = sb.ToString(); return(new Code(codeStr)); }
//Convert rectangles to their 6 quads public static QuadList RectsToQuads(RectList rectSet) { QuadList quadsMacro = new QuadList(); foreach (Rect rect in rectSet) { QuadList quads = RectToQuads(rect); foreach (Quad quad in quads) { quadsMacro.AddQuad(quad); } } //Remove redundant ones automatically RemoveRedundantQuads(quadsMacro); return(quadsMacro); }
//True if same public bool IsEqualTo(RectList rects) { if (rects == null || rects.Count != Count) { return(false); } for (int i = 0; i < Count; i++) { Rect rect1 = rects.GetRect(i); Rect rect2 = Rects[i]; if (rect1.IsEqualTo(rect2) == false) { return(false); } } return(true); }
public static void DisplayRectCircuits(RectList rects) { Console.WriteLine("\nTouch Graph : Optimized list results (multiple circuit):"); int count = 0; foreach (Rect rect in rects) { Console.Write(count + " : " + rect + " "); if (rect.Properties.CircuitIds != null) { foreach (int id in rect.Properties.CircuitIds) { Console.Write(id + " "); } } Console.WriteLine(); count++; } Console.WriteLine(); }
public static void AssignCircuitIdsToRects(RectList rects, List <List <int> > touchGraph, List <List <int> > circuitIds) { int listid = 0; foreach (List <int> list in touchGraph) { foreach (int id in list) { if (circuitIds[listid][0] > 0) { rects.GetRect(id).Properties.CircuitIds = new List <int>(); foreach (int cid in circuitIds[listid]) { rects.GetRect(id).Properties.CircuitIds.Add(cid); } } } listid++; } }
//This should be deprecated as it creates it's own vertices instead of using STL public static Triangles RectsToTrianglesCube(RectList rectSet) { var triangles = new List <Triangle>(); //Iterate through reach rectangle(volume/cube) creating triangles foreach (Rect rect in rectSet) { QuadList quads = RectToQuads(rect); //RectToTrianglesCube(ref triangles, rect); foreach (Quad quad in quads) { Triangles twoTriangles = QuadToTwoTriangles(quad); triangles.Add(twoTriangles.GetTriangleArray()[0]); triangles.Add(twoTriangles.GetTriangleArray()[1]); } } return(new Triangles(triangles.ToArray())); }
public void FromGrid(Grid inGrid) { Grid = inGrid; int scale; if (Grid.SizeX > 128) { scale = 1; } else { //scale = (int)((256) / (float)(Grid.SizeX) * 1.3) & 254; scale = (int)((256) / (float)(Grid.SizeX) * 1.8) & 254; } //scale = 12; if ((enableFlags & enables.RenderIsometricThumb) == enables.RenderIsometricThumb) { GridIsometricThumb = RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(Grid, 0, 0, 0, 0, 1, 1, "Render Isometric (Thumbnail) "); } if ((enableFlags & enables.RenderIsometricRegular) == enables.RenderIsometricRegular) { GridIsometric = RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(Grid, 31, 31, 31, 0, scale, scale, "Render Isometric (Regular) "); } if ((enableFlags & enables.RenderIsometricLarge) == enables.RenderIsometricLarge) { GridIsometricLarge = RasterLib.RasterApi.Renderer.RenderIsometricCellsScaled(Grid, 0, 0, 0, 0, 8, 8, "Render Isometric (Large) "); } if ((enableFlags & enables.RenderOrthogonal) == enables.RenderOrthogonal) { GridOrthogonal = RasterLib.RasterApi.Renderer.RenderObliqueCells(Grid); } if ((enableFlags & enables.DoRects) == enables.DoRects) { Rects = GridConverter.GridToRects(Grid); FromRects(Rects); } }
public void FromCode(Code inCode) { code = inCode; codename = RasterLib.RasterApi.CodeToCodename(code); try { Tokens = RasterLib.RasterApi.CodeToTokens(code); FromTokens(Tokens); } catch (Exception ex) { Console.WriteLine("Exception " + ex); } if ((enableFlags & enables.DoRectsMipMap) == enables.DoRectsMipMap) { Code codeSmaller = RasterLib.RasterApi.CodeToRescaledCode(inCode, 16, 16, 16); Grid gridSmall = RasterLib.RasterApi.CodeToGrid(codeSmaller); RectList rectsSmall = RasterLib.RasterApi.GridToRects(gridSmall); SerializedRectsMipMap = RasterLib.RasterApi.RectsToSerializedRects(rectsSmall); } }