public Brush CopyList() { Brush brush = new Brush(); brush.max = max; brush.min = min; brush.Polys = Polys.CopyList(); if (!IsLast) { brush.SetNext(Next.CopyList()); } return brush; }
//----------------------- public void line_to_buffer(ref Polys apolys, Point p1, Point p2) { int ymin = 0; int ymax = 0; int xmin = 0; int xmax = 0; if (p1.Y > p2.Y) { ymin = p2.Y; ymax = p1.Y; xmin = p2.X; xmax = p1.X; } else { ymin = p1.Y; ymax = p2.Y; xmin = p1.X; xmax = p2.X; } if (ymax - ymin > 0) { for (int i = 0; i <= ymax - ymin; i++) { if (ymin + i >= 0 && ymin + i <= apolys.Height - 1) { int value = xmin + (xmax - xmin) * i / (ymax - ymin); if (value < apolys.minBuffer[ymin + i]) { if (value < 0) { value = 0; } apolys.minBuffer[ymin + i] = value; } if (value > apolys.maxBuffer[ymin + i]) { if (value > apolys.Width - 1) { value = apolys.Width - 1; } apolys.maxBuffer[ymin + i] = value; } } } } }
public void Load(string onvfile) { Name = Path.GetFileNameWithoutExtension(onvfile).ToLowerInvariant(); var lines = File.ReadAllLines(onvfile); bool inverts = false; bool ininds = false; bool inedges = false; bool inpolys = false; bool insectors = false; bool insectordata = false; bool insectorpolyinds = false; bool insectorbounds = false; bool inportals = false; int depth = 0; int cdepth = 0; OnvSector csector = SectorTree; var spacedelim = new[] { ' ' }; var cult = CultureInfo.InvariantCulture; foreach (var line in lines) { string tline = line.Trim(); if (string.IsNullOrEmpty(tline)) { continue; //blank line } //if (tline.StartsWith("#")) continue; //commented out string[] parts = tline.Split(spacedelim, StringSplitOptions.RemoveEmptyEntries); if (tline.StartsWith("{")) { depth++; continue; } if (tline.StartsWith("}")) { depth--; } //need to handle the closing cases if (inverts) { if (depth <= 0) { inverts = false; } else if (parts.Length == 3) { NavMeshVertex v = new NavMeshVertex(); v.X = ushort.Parse(parts[0].Trim(), cult); v.Y = ushort.Parse(parts[1].Trim(), cult); v.Z = ushort.Parse(parts[2].Trim(), cult); Vertices.Add(v); } } else if (ininds) { if (depth <= 0) { ininds = false; } else { for (int i = 0; i < parts.Length; i++) { int ind = int.Parse(parts[i]); Indices.Add(ind); } } } else if (inedges) { if (depth <= 0) { inedges = false; } else { OnvEdge edge = new OnvEdge(parts); Edges.Add(edge); } } else if (inpolys) { if (depth <= 0) { inpolys = false; } else { OnvPoly poly = new OnvPoly(parts); Polys.Add(poly); } } else if (insectors) { if (depth <= 0) { insectors = false; insectordata = false; insectorpolyinds = false; insectorbounds = false; } else if (insectordata) { if (depth <= cdepth) { insectordata = false; insectorpolyinds = false; insectorbounds = false; } else if (insectorpolyinds) { if (depth <= (cdepth + 1)) { insectorpolyinds = false; } else { for (int i = 0; i < parts.Length; i++) { int ind = int.Parse(parts[i]); csector.SectorData.PolyIndices.Add(ind); } } } else if (insectorbounds) { if (depth <= (cdepth + 1)) { insectorbounds = false; } else { OnvBounds bounds = new OnvBounds(parts); csector.SectorData.Bounds.Add(bounds); } } else { string idstr = parts[0].Trim(); if (idstr == "PolyIndices") { csector.SectorData.PolyIndicesCount = int.Parse(parts[1].Trim(), cult); insectorpolyinds = csector.SectorData.PolyIndicesCount > 0; if (insectorpolyinds) { csector.SectorData.PolyIndices = new List <int>(); } } else if (idstr == "Bounds") { csector.SectorData.BoundsCount = int.Parse(parts[1].Trim(), cult); insectorbounds = csector.SectorData.BoundsCount > 0; if (insectorbounds) { csector.SectorData.Bounds = new List <OnvBounds>(); } } } } else { if (depth < cdepth) { csector = csector.Parent; } cdepth = depth; string idstr = parts[0].Trim(); if (idstr == "AABBMin") { csector.AABBMin = Util.GetVector3(parts, 1); } else if (idstr == "AABBMax") { csector.AABBMax = Util.GetVector3(parts, 1); } else if ((parts.Length < 2) || (parts[1].Trim() != "null")) { if (idstr == "SectorData") { csector.SectorData = new OnvSectorData(); insectordata = true; } else if (idstr == "SubTree0") { csector.SubTree0 = new OnvSector(); csector.SubTree0.Parent = csector; csector = csector.SubTree0; } else if (idstr == "SubTree1") { csector.SubTree1 = new OnvSector(); csector.SubTree1.Parent = csector; csector = csector.SubTree1; } else if (idstr == "SubTree2") { csector.SubTree2 = new OnvSector(); csector.SubTree2.Parent = csector; csector = csector.SubTree2; } else if (idstr == "SubTree3") { csector.SubTree3 = new OnvSector(); csector.SubTree3.Parent = csector; csector = csector.SubTree3; } } } } else if (inportals) { if (depth <= 0) { inportals = false; } else { OnvPortal portal = new OnvPortal(parts); Portals.Add(portal); } } else { //at root level, look for identifier depth = 0; //reset just in case string idstr = parts[0].Trim(); if (idstr == "Version") { VersionMaj = int.Parse(parts[1].Trim(), cult); VersionMin = int.Parse(parts[2].Trim(), cult); } else if (idstr == "Sizes") { Sizes = Util.GetVector3(parts, 1); } else if (idstr == "Flags") { Flags = int.Parse(parts[1].Trim(), cult); } else if (idstr == "Vertices") { VerticesCount = int.Parse(parts[1].Trim(), cult); inverts = VerticesCount > 0; } else if (idstr == "Indices") { IndicesCount = int.Parse(parts[1].Trim(), cult); ininds = IndicesCount > 0; } else if (idstr == "Edges") { EdgesCount = int.Parse(parts[1].Trim(), cult); inedges = EdgesCount > 0; } else if (idstr == "Polys") { PolysCount = int.Parse(parts[1].Trim(), cult); inpolys = PolysCount > 0; } else if (idstr == "SectorTree") { insectors = true; } else if (idstr == "Portals") { PortalsCount = int.Parse(parts[1].Trim(), cult); inportals = PortalsCount > 0; } else if (idstr == "SectorID") { SectorID = int.Parse(parts[1].Trim(), cult); } } } }
//----------------------- public int Dopoly(Polys apolys, List <Point> aPointsLst, Boolean draw, byte[] img = null) { double errf = 0; int ymax = 0; int ymin = 3999; Point pnt1 = aPointsLst[p1]; Point pnt2 = aPointsLst[p2]; Point pnt3 = aPointsLst[p3]; ymax = max(pnt1.Y, pnt2.Y, pnt3.Y); ymin = min(pnt1.Y, pnt2.Y, pnt3.Y); if (ymin < 0) { ymin = 0; } ; if (ymax > apolys.Height - 1) { ymax = apolys.Height - 1; } ; for (int i = ymin; i <= ymax; i++) { apolys.minBuffer[i] = 99999; apolys.maxBuffer[i] = 0; } line_to_buffer(ref apolys, pnt1, pnt2); line_to_buffer(ref apolys, pnt2, pnt3); line_to_buffer(ref apolys, pnt3, pnt1); int totr = 0; int totg = 0; int totb = 0; int cnt = 0; for (int i = ymin; i < ymax; i++) { int tmp = i * apolys.Width * 3; for (int j = apolys.minBuffer[i]; j < apolys.maxBuffer[i]; j++) { totr = totr + apolys.src[j * 3 + tmp]; totg = totg + apolys.src[j * 3 + 1 + tmp]; totb = totb + apolys.src[j * 3 + 2 + tmp]; cnt = cnt + 1; } } errf = 0; byte r = 0; byte g = 0; byte b = 0; int srcr = 0; int srcg = 0; int srcb = 0; if (cnt != 0) { r = System.Convert.ToByte(totr / cnt); g = System.Convert.ToByte(totg / cnt); b = System.Convert.ToByte(totb / cnt); } rgb = Color.FromArgb(r, g, b); for (int i = ymin; i <= ymax; i++) { int pos = apolys.minBuffer[i] * 3 + i * apolys.Width * 3; byte[] bb = apolys.src; if (draw == true) { int mmin = apolys.minBuffer[i]; int mmax = apolys.maxBuffer[i]; for (int j = mmin; j <= mmax; j++) { img[pos] = b; img[pos + 1] = g; img[pos + 2] = r; pos = pos + 3; } } else { int mmin = apolys.minBuffer[i]; int mmax = apolys.maxBuffer[i]; for (int j = mmin; j <= mmax; j++) { srcr = bb[pos] - r; srcg = bb[pos + 1] - g; srcb = bb[pos + 2] - b; errf = errf + (srcr * srcr + srcg * srcg + srcb * srcb); pos = pos + 3; } } } return((int)errf); }