private static double getAngle(AreaVertex v0, AreaVertex v1, AreaVertex v2) { var angle1 = getAbsoluteAngle(v0, v1); var angle2 = getAbsoluteAngle(v1, v2); var deltaAngle = angle2 - angle1; if (deltaAngle < -Math.PI) { deltaAngle += 2 * Math.PI; } if (deltaAngle > Math.PI) { deltaAngle -= 2 * Math.PI; } return(deltaAngle); }
public List <AreaVertex> getOutline() { var list = new List <AreaVertex>(); foreach (var marker in markers) { var p = Geometry.convertPoint(marker.getPosition()); var av = new AreaVertex(); av.X = (float)p.X; av.Y = (float)p.Y; av.Z = (float)Geometry.groundLevel; av.W = (float)Geometry.groundLevel; list.Add(av); } return(list); }
public static bool isInside(AreaVertex v1, List <AreaVertex> outline) { var v2 = new AreaVertex(); var crossings = 0; for (int i = 0; i < outline.Count; i++) { var b1 = outline.ElementAt(i); var k = i + 1; if (i == outline.Count - 1) { k = 0; } var b2 = outline.ElementAt(k); if (v1.Equals(b1) || v1.Equals(b2) || v2.Equals(b1) || v2.Equals(b2)) { continue; } if (isRightHanded(new List <AreaVertex>() { v1, b1, b2 }) == isRightHanded(new List <AreaVertex>() { v2, b1, b2 })) { continue; } else if (isRightHanded(new List <AreaVertex>() { v1, v2, b1 }) == isRightHanded(new List <AreaVertex>() { v1, v2, b2 })) { continue; } else { crossings++; } } return(crossings % 2 == 1); }
private static bool lineIntersect(AreaVertex v1, AreaVertex v2, List <AreaVertex> outline) { bool intersects = false; for (int i = 0; i < outline.Count; i++) { var b1 = outline.ElementAt(i); var k = i + 1; if (i == outline.Count - 1) { k = 0; } var b2 = outline.ElementAt(k); if (v1.Equals(b1) || v1.Equals(b2) || v2.Equals(b1) || v2.Equals(b2)) { continue; } if (isRightHanded(new List <AreaVertex>() { v1, b1, b2 }) == isRightHanded(new List <AreaVertex>() { v2, b1, b2 })) { continue; } else if (isRightHanded(new List <AreaVertex>() { v1, v2, b1 }) == isRightHanded(new List <AreaVertex>() { v1, v2, b2 })) { continue; } else { return(true); } } return(false); }
private static List <AreaVertex> getInnerRing(List <AreaVertex> outline) { var innerRing = outline.Select(x => { var y = new AreaVertex(); y.X = x.X; y.Y = x.Y; y.Z = x.Z; y.W = x.W; y.Center = x.Center; return(y); }).ToList(); for (int i = 0; i < outline.Count; i++) { var im = i - 1; if (im < 0) { im = outline.Count - 1; } var ip = i + 1; if (ip == outline.Count) { ip = 0; } var localAngle = (getAngle(outline.ElementAt(im), outline.ElementAt(i), outline.ElementAt(ip)) + Math.PI) / 2; var angle = localAngle + getAbsoluteAngle(outline.ElementAt(im), outline.ElementAt(i)); float offset = 5 * scaleFactor / (float)Math.Abs(Math.Sin(localAngle)); var tmp = innerRing[i]; tmp.X += offset * (float)Math.Cos(angle); tmp.Y += offset * (float)Math.Sin(angle); innerRing[i] = tmp; } return(innerRing); }
protected Area ReadArea(Stream ms) { var currentZone = new Area { Content = new AreaContent() }; var buffer = new byte[4]; ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); int zoneDataVersion = BitConverter.ToInt32(buffer, 0); if (zoneDataVersion != 2) { throw new InvalidDataException("Zone data version != 2 not supported!"); } ms.Read(buffer, 0, buffer.Length); currentZone.Id = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); int idStrLen = BitConverter.ToInt32(buffer, 0); var idStrBuffer = new byte[Utils.RoundToNextDivBy4(idStrLen)]; ms.Read(idStrBuffer, 0, idStrBuffer.Length); currentZone.Name = Encoding.UTF8.GetString(idStrBuffer).TrimEnd('\0'); ms.AssertAreaMagic(); var attachmentPt = new Point3D(); ms.Read(buffer, 0, buffer.Length); attachmentPt.X = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); attachmentPt.Y = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); attachmentPt.Z = BitConverter.ToSingle(buffer, 0); currentZone.AttachmentPoint = attachmentPt; ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); var subParts = BitConverter.ToInt32(buffer, 0); for (int sp = 0; sp < subParts; sp++) { var aced = new AreaClipped(); ms.Read(buffer, 0, buffer.Length); aced.StartTriangle = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); aced.TriangleCount = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); aced.StartVertex = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); aced.VertexCount = BitConverter.ToInt32(buffer, 0); currentZone.Content.ClippedAreas.Add(aced); } ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderTriangle.StartTriangle = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderTriangle.TriangleCount = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderTriangle.StartVertex = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderTriangle.VertexCount = BitConverter.ToInt32(buffer, 0); ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderVertex.StartVertex = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentZone.Content.BorderVertex.VertexCount = BitConverter.ToInt32(buffer, 0); ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); int vertexCount = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); int trianglesCount = BitConverter.ToInt32(buffer, 0); for (int v = 0; v < vertexCount; v++) { var curVertex = new AreaVertex(); ms.Read(buffer, 0, buffer.Length); curVertex.X = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); curVertex.Y = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); curVertex.Z = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); curVertex.W = BitConverter.ToSingle(buffer, 0); ms.Read(buffer, 0, buffer.Length); curVertex.Center = BitConverter.ToSingle(buffer, 0); currentZone.Content.Vertices.Add(curVertex); } ms.AssertAreaMagic(); for (int f = 0; f < trianglesCount; f++) { var currentTriangle = new MeshTriangularFace(); ms.Read(buffer, 0, buffer.Length); currentTriangle.Point1 = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentTriangle.Point2 = BitConverter.ToInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); currentTriangle.Point3 = BitConverter.ToInt32(buffer, 0); currentZone.Content.Triangles.Add(currentTriangle); } ms.AssertAreaMagic(); ms.Seek(4, SeekOrigin.Current); ms.AssertAreaMagic(); ms.Read(buffer, 0, buffer.Length); if (BitConverter.ToUInt32(buffer, 0) != 809782853) { throw new InvalidDataException("END0 expected!"); } return(currentZone); }
private void doZoneProperties() { var list = data.Classes.First().Instances.First().PropertyValues.First().Value as NdfCollection; foreach (var item in list) { var reference = item.Value as NdfObjectReference; if (reference.Instance == null) { continue; } var designItem = reference.Instance; var position = designItem.PropertyValues.First(x => x.Property.Name.Equals("Position")).Value as NdfVector; var rotation = designItem.PropertyValues.First(x => x.Property.Name.Equals("Rotation")).Value as NdfSingle; var addonReference = designItem.PropertyValues.First(x => x.Property.Name.Equals("AddOn")).Value as NdfObjectReference; var scale = designItem.PropertyValues.FirstOrDefault(x => x.Property.Name.Equals("Scale")); float s = 1; if (scale != null) { var sc = scale.Value as NdfVector; if (sc != null) { s = (float)((Point3D)sc.Value).X; } } var addon = addonReference.Instance; var q = (Point3D)position.Value; var p = new AreaVertex(); p.X = (float)q.X; p.Y = (float)q.Y; Zone zone; zone = zones.FirstOrDefault(x => Geometry.isInside(p, x.getRawOutline()) ); if (addon.Class.Name.Equals("TGameDesignAddOn_CommandPoints") && zone != null) { var pos = addon.PropertyValues.First(x => x.Property.Name.Equals("Points")).Value as NdfInt32; if (pos == null) { zone.value = 0; } else { zone.value = (int)pos.Value; } } if (addon.Class.Name.Equals("TGameDesignAddOn_StartingPoint") && zone != null) { var pos = addon.PropertyValues.First(x => x.Property.Name.Equals("AllianceNum")).Value as NdfInt32; if (pos == null) { zone.possession = (Possession)0; } else { zone.possession = (Possession)pos.Value; } } if (addon.Class.Name.Equals("TGameDesignAddOn_ReinforcementLocation") && zone != null) { var spawn = new Spawn(Geometry.convertPoint(q), (float)rotation.Value, s, spawnNumber++, SpawnType.Land); editor.addScenarioItem(spawn); scenarioItems.Add(spawn); } if (addon.Class.Name.Equals("TGameDesignAddOn_MaritimeCorridor") && zone != null) { var spawn = new Spawn(Geometry.convertPoint(q), (float)rotation.Value, s, spawnNumber++, SpawnType.Sea); editor.addScenarioItem(spawn); scenarioItems.Add(spawn); } if (addon.Class.Name.Equals("TGameDesignAddOn_AerialCorridor") && zone != null) { var spawn = new Spawn(Geometry.convertPoint(q), (float)rotation.Value, s, spawnNumber++, SpawnType.Air); editor.addScenarioItem(spawn); scenarioItems.Add(spawn); } if (addon.Class.Name.Equals("TGameDesignAddOn_StartingCommandUnit") && zone != null) { var prop = addon.PropertyValues.First(x => x.Property.Name.Equals("AllocationPriority")); int prio = 0; if (!(prop.Value is NdfNull)) { prio = (int)((NdfInt32)prop.Value).Value; } var startPos = new Icon(Geometry.convertPoint(q), startPosNumber++, IconType.CV, prio); editor.addScenarioItem(startPos); scenarioItems.Add(startPos); } if (addon.Class.Name.Equals("TGameDesignAddOn_StartingFOB") && zone != null) { var prop = addon.PropertyValues.First(x => x.Property.Name.Equals("AllocationPriority")); int prio = 0; if (!(prop.Value is NdfNull)) { prio = (int)((NdfInt32)prop.Value).Value; } var startPos = new Icon(Geometry.convertPoint(q), startPosNumber++, IconType.FOB, prio); editor.addScenarioItem(startPos); scenarioItems.Add(startPos); } //Console.WriteLine(rotation); } }
private static double getAbsoluteAngle(AreaVertex v1, AreaVertex v2) { var angle = Math.Atan2(v2.Y - v1.Y, v2.X - v1.X); return(angle); }
public static AreaContent getFromOutline(List <AreaVertex> outline) { var content = new AreaContent(); if (!isRightHanded(outline)) { outline.Reverse(); } outline = outline.Select(x => { x.Center = 1; return(x); }).ToList(); content.Vertices.AddRange(outline); //var bottomRing = new List<AreaVertex>(outline); var bottomRing = outline.Select(x => { var y = new AreaVertex(); y.X = x.X; y.Y = x.Y; y.Z = x.Z; y.W = x.W; y.Center = 0; return(y); }).ToList(); content.Vertices.AddRange(bottomRing); var innerRing = getInnerRing(outline); content.Vertices.AddRange(innerRing); var triangles = getTriangles(outline, 0, outline.Count - 1); content.Triangles.AddRange(triangles); var borderTriangles = getBorderTriangles(outline.Count); content.Triangles.AddRange(borderTriangles); AreaClipped ac = new AreaClipped(); ac.StartTriangle = 0; ac.StartVertex = 0; ac.TriangleCount = triangles.Count; ac.VertexCount = outline.Count; //ac.TriangleCount = content.Triangles.Count; //ac.VertexCount = content.Vertices.Count; content.ClippedAreas.Add(ac); AreaClipped ac2 = new AreaClipped(); content.ClippedAreas.Add(ac2); AreaClipped bt = new AreaClipped(); bt.StartTriangle = triangles.Count; bt.StartVertex = outline.Count; bt.TriangleCount = borderTriangles.Count; bt.VertexCount = 2 * outline.Count; content.BorderTriangle = bt; AreaClipped bv = new AreaClipped(); bv.StartTriangle = 0; bv.StartVertex = 2 * outline.Count; bv.TriangleCount = 0; bv.VertexCount = outline.Count; content.BorderVertex = bv; //Console.Write("vertices=["); //var scen = content; /*foreach (var v in scen.Vertices) * { * Console.WriteLine("{0:G},{1:G},{2:G};", (int)v.X, (int)v.Y, (int)v.Center); * } * Console.WriteLine("]"); * * Console.Write("tri=["); * foreach (var v in scen.Triangles) * { * Console.WriteLine("{0},{1},{2};", (int)v.Point1, (int)v.Point2, (int)v.Point3); * } * Console.WriteLine("]"); * * * Console.WriteLine("bt=[{0:G},{1:G},{2:G},{3:G}]", scen.BorderTriangle.StartTriangle, scen.BorderTriangle.StartVertex, scen.BorderTriangle.TriangleCount, scen.BorderTriangle.VertexCount); * Console.WriteLine("bv=[{0:G},{1:G},{2:G},{3:G}]", scen.BorderVertex.StartTriangle, scen.BorderVertex.StartVertex, scen.BorderVertex.TriangleCount, scen.BorderVertex.VertexCount); * var k = 0; * foreach (var a in scen.ClippedAreas) * { * Console.WriteLine("t{4}=[{0:G},{1:G},{2:G},{3:G}]", a.StartTriangle, a.StartVertex, a.TriangleCount, a.VertexCount, k++); * } */ return(content); }