public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture) { var solid = new Solid(generator.GetNextObjectID()) { Colour = Colour.GetRandomBrushColour() }; // The lower Z plane will be base, the x planes will be triangles var c1 = new Coordinate(box.Start.X, box.Start.Y, box.Start.Z); var c2 = new Coordinate(box.End.X, box.Start.Y, box.Start.Z); var c3 = new Coordinate(box.End.X, box.End.Y, box.Start.Z); var c4 = new Coordinate(box.Start.X, box.End.Y, box.Start.Z); var c5 = new Coordinate(box.Center.X, box.Start.Y, box.End.Z); var c6 = new Coordinate(box.Center.X, box.End.Y, box.End.Z); var faces = new[] { new[] { c1, c2, c3, c4 }, new[] { c2, c1, c5 }, new[] { c5, c6, c3, c2 }, new[] { c4, c3, c6 }, new[] { c6, c5, c1, c4 } }; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; }
public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture, int roundDecimals) { var solid = new Solid(generator.GetNextObjectID()) { Colour = Colour.GetRandomBrushColour() }; // The higher Z plane will be triangle, with the lower X value getting the two corners var c1 = new Coordinate(box.Start.X, box.Start.Y, box.End.Z).Round(roundDecimals); var c2 = new Coordinate(box.End.X, box.Start.Y, box.End.Z).Round(roundDecimals); var c3 = new Coordinate(box.Center.X, box.End.Y, box.End.Z).Round(roundDecimals); var c4 = new Coordinate(box.Center.X, box.Center.Y, box.Start.Z).Round(roundDecimals); var faces = new[] { new[] { c3, c2, c1 }, new[] { c3, c1, c4 }, new[] { c2, c3, c4 }, new[] { c1, c2, c4 } }; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; }
public VMError(string message, Solid solid, IEnumerable<Face> faces = null, IEnumerable<Vertex> vertices = null) { Message = message; Solid = solid; Faces = (faces ?? new Face[0]).ToList(); Vertices = (vertices ?? new Vertex[0]).ToList(); }
public static Solid CreateFromIntersectingPlanes(IEnumerable<Plane> planes, IDGenerator generator) { var solid = new Solid(generator.GetNextObjectID()); var list = planes.ToList(); for (var i = 0; i < list.Count; i++) { // Split the polygon by all the other planes var poly = new Polygon(list[i]); for (var j = 0; j < list.Count; j++) { if (i != j) poly.Split(list[j]); } // The final polygon is the face var face = new Face(generator.GetNextFaceID()) { Plane = poly.Plane , Parent = solid }; face.Vertices.AddRange(poly.Vertices.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToWorld(); solid.Faces.Add(face); } // Ensure all the faces point outwards var origin = solid.GetOrigin(); foreach (var face in solid.Faces) { if (face.Plane.OnPlane(origin) >= 0) face.Flip(); } solid.UpdateBoundingBox(); return solid; }
public void Dispose() { _carver = null; _objects = null; _clipped = null; _results = null; }
public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture, int roundDecimals) { var numsides = (int) _numSides.GetValue(); if (numsides < 3) yield break; // Cylinders can be elliptical so use both major and minor rather than just the radius // NOTE: when a low number (< 10ish) of faces are selected this will cause the cylinder to not touch all the edges of the box. var width = box.Width; var length = box.Length; var height = box.Height; var major = width / 2; var minor = length / 2; var angle = 2 * DMath.PI / numsides; // Calculate the X and Y points for the ellipse var points = new Coordinate[numsides]; for (var i = 0; i < numsides; i++) { var a = i * angle; var xval = box.Center.X + major * DMath.Cos(a); var yval = box.Center.Y + minor * DMath.Sin(a); var zval = box.Start.Z; points[i] = new Coordinate(xval, yval, zval).Round(roundDecimals); } var faces = new List<Coordinate[]>(); // Add the vertical faces var z = new Coordinate(0, 0, height).Round(roundDecimals); for (var i = 0; i < numsides; i++) { var next = (i + 1) % numsides; faces.Add(new[] {points[i], points[i] + z, points[next] + z, points[next]}); } // Add the elliptical top and bottom faces faces.Add(points.ToArray()); faces.Add(points.Select(x => x + z).Reverse().ToArray()); // Nothing new here, move along var solid = new Solid(generator.GetNextObjectID()) { Colour = Colour.GetRandomBrushColour() }; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; }
public override MapObject Clone() { var e = new Solid(ID); foreach (var f in Faces.Select(x => x.Clone())) { f.Parent = e; e.Faces.Add(f); f.UpdateBoundingBox(); } CopyBase(e, null, true); return e; }
public override MapObject Copy(IDGenerator generator) { var e = new Solid(generator.GetNextObjectID()); foreach (var f in Faces.Select(x => x.Copy(generator))) { f.Parent = e; e.Faces.Add(f); f.UpdateBoundingBox(); } CopyBase(e, generator); return e; }
public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture) { var numsides = (int) _numSides.GetValue(); if (numsides < 3) yield break; // This is all very similar to the cylinder brush. var width = box.Width; var length = box.Length; var major = width / 2; var minor = length / 2; var angle = 2 * DMath.PI / numsides; var points = new Coordinate[numsides]; for (var i = 0; i < numsides; i++) { var a = i * angle; var xval = box.Center.X + major * DMath.Cos(a); var yval = box.Center.Y + minor * DMath.Sin(a); var zval = box.Start.Z; points[i] = new Coordinate(xval, yval, zval).Round(0); } var faces = new List<Coordinate[]>(); var point = new Coordinate(box.Center.X, box.Center.Y, box.End.Z); for (var i = 0; i < numsides; i++) { var next = (i + 1) % numsides; faces.Add(new[] {points[i], point, points[next]}); } faces.Add(points.ToArray()); var solid = new Solid(generator.GetNextObjectID()) { Colour = Colour.GetRandomBrushColour() }; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; }
public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture, int roundDecimals) { var solid = new Solid(generator.GetNextObjectID()) { Colour = Colour.GetRandomBrushColour() }; foreach (var arr in box.GetBoxFaces()) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x.Round(roundDecimals), face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; }
public void BenchmarkSolidConstruction() { var idg = new IDGenerator(); var box = new Box(Coordinate.One * -100, Coordinate.One * 100); var planes = new CylinderBrush().Create(idg, box, null, 2).OfType<Solid>().SelectMany(x => x.Faces).Select(x => x.Plane).ToList(); var stopwatch = new Stopwatch(); stopwatch.Start(); for (var b = 0; b < 1000; b++) { Solid.CreateFromIntersectingPlanes(planes, idg); } stopwatch.Stop(); Debug.WriteLine(stopwatch.Elapsed); stopwatch.Restart(); for (var b = 0; b < 1000; b++) { var polys = new List<Polygon>(); for (var i = 0; i < planes.Count; i++) { var poly = new Polygon(planes[i]); for (var j = 0; j < planes.Count; j++) { if (i != j) poly.Split(planes[j]); } polys.Add(poly); } var solid = new Solid(idg.GetNextObjectID()); foreach (var polygon in polys) { var face = new Face(idg.GetNextFaceID()) {Plane = polygon.Plane}; face.Vertices.AddRange(polygon.Vertices.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToWorld(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); } stopwatch.Stop(); Debug.WriteLine(stopwatch.Elapsed); }
private static Solid CreateFakeDecalSolid(Face face) { var s = new Solid(0) { Colour = face.Colour, IsVisgroupHidden = face.IsHidden, IsSelected = face.IsSelected }; s.Faces.Add(face); var p = face.BoundingBox.Center - face.Plane.Normal * 10; // create a new point underneath the face var p1 = face.Vertices[0].Location; var p2 = face.Vertices[1].Location; var p3 = face.Vertices[2].Location; var p4 = face.Vertices[3].Location; var faces = new[] { new[] { p2, p1, p}, new[] { p3, p2, p}, new[] { p4, p3, p}, new[] { p1, p4, p} }; foreach (var ff in faces) { var f = new Face(-1) { Colour = face.Colour, IsSelected = face.IsSelected, IsHidden = face.IsHidden, Plane = new Plane(ff[0], ff[1], ff[2]) }; f.Vertices.AddRange(ff.Select(x => new Vertex(x, f))); f.UpdateBoundingBox(); s.Faces.Add(f); } s.UpdateBoundingBox(); return s; }
private Solid MakeSolid(IDGenerator generator, IEnumerable<Coordinate[]> faces, ITexture texture, Color col) { var solid = new Solid(generator.GetNextObjectID()) { Colour = col }; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = { Texture = texture } }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToWorld(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); return solid; }
private static Solid ReadSolid(GenericStructure solid, IDGenerator generator) { var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor"); var faces = solid.GetChildren("side").Select(x => ReadFace(x, generator)).ToList(); Solid ret; if (faces.All(x => x.Vertices.Count >= 3)) { // Vertices were stored in the VMF ret = new Solid(GetObjectID(solid, generator)); ret.Faces.AddRange(faces); } else { // Need to grab the vertices using plane intersections var idg = new IDGenerator(); // No need to increment the id generator if it doesn't have to be ret = Solid.CreateFromIntersectingPlanes(faces.Select(x => x.Plane), idg); ret.ID = GetObjectID(solid, generator); for (var i = 0; i < ret.Faces.Count; i++) { var face = ret.Faces[i]; var f = faces.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(ret.Faces[i].Plane.Normal)); if (f == null) { // TODO: Report invalid solids Debug.WriteLine("Invalid solid! ID: " + solid["id"]); return null; } face.Texture = f.Texture; var disp = f as Displacement; if (disp == null) continue; disp.Plane = face.Plane; disp.Vertices = face.Vertices; disp.Texture = f.Texture; disp.AlignTextureToWorld(); disp.CalculatePoints(); ret.Faces[i] = disp; } } ret.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour()); ret.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse)); foreach (var face in ret.Faces) { face.Parent = ret; face.Colour = ret.Colour; face.UpdateBoundingBox(); } if (ret.Faces.Any(x => x is Displacement)) { ret.Faces.ForEach(x => x.IsHidden = !(x is Displacement)); } ret.UpdateBoundingBox(false); return ret; }
private static GenericStructure WriteSolid(Solid solid) { var ret = new GenericStructure("solid"); ret["id"] = solid.ID.ToString(CultureInfo.InvariantCulture); foreach (var face in solid.Faces.OrderBy(x => x.ID)) { ret.Children.Add(WriteFace(face)); } var editor = WriteEditor(solid); ret.Children.Add(editor); if (solid.IsVisgroupHidden) { var hidden = new GenericStructure("hidden"); hidden.Children.Add(ret); ret = hidden; } return ret; }
/// <summary> /// Splits this solid into two solids by intersecting against a plane. /// </summary> /// <param name="plane">The splitting plane</param> /// <param name="back">The back side of the solid</param> /// <param name="front">The front side of the solid</param> /// <param name="generator">The IDGenerator to use</param> /// <returns>True if the plane splits the solid, false if the plane doesn't intersect</returns> public bool Split(Plane plane, out Solid back, out Solid front, IDGenerator generator) { back = front = null; // Check that this solid actually spans the plane var classify = Faces.Select(x => x.ClassifyAgainstPlane(plane)).Distinct().ToList(); if (classify.All(x => x != PlaneClassification.Spanning)) { if (classify.Any(x => x == PlaneClassification.Back)) back = this; else if (classify.Any(x => x == PlaneClassification.Front)) front = this; return false; } var backPlanes = new List<Plane> { plane }; var frontPlanes = new List<Plane> { new Plane(-plane.Normal, -plane.DistanceFromOrigin) }; foreach (var face in Faces) { var classification = face.ClassifyAgainstPlane(plane); if (classification != PlaneClassification.Back) frontPlanes.Add(face.Plane); if (classification != PlaneClassification.Front) backPlanes.Add(face.Plane); } back = CreateFromIntersectingPlanes(backPlanes, generator); front = CreateFromIntersectingPlanes(frontPlanes, generator); CopyBase(back, generator); CopyBase(front, generator); front.Faces.Union(back.Faces).ToList().ForEach(x => { x.Texture = Faces[0].Texture.Clone(); x.AlignTextureToFace(); x.Colour = Colour; }); // Restore textures (match the planes up on each face) foreach (var orig in Faces) { foreach (var face in back.Faces) { var classification = face.ClassifyAgainstPlane(orig.Plane); if (classification != PlaneClassification.OnPlane) continue; face.Texture = orig.Texture.Clone(); break; } foreach (var face in front.Faces) { var classification = face.ClassifyAgainstPlane(orig.Plane); if (classification != PlaneClassification.OnPlane) continue; face.Texture = orig.Texture.Clone(); break; } } front.Faces.Union(back.Faces).ToList().ForEach(x => x.CalculateTextureCoordinates(true)); return true; }
public IEnumerable<Face> GetBoxFaces() { var faces = new List<Face>(); if (Children.Any()) return faces; var box = BoundingBox.GetBoxFaces(); var dummySolid = new Solid(-1) { IsCodeHidden = IsCodeHidden, IsRenderHidden2D = IsRenderHidden2D, IsSelected = IsSelected, IsRenderHidden3D = IsRenderHidden3D, IsVisgroupHidden = IsVisgroupHidden }; foreach (var ca in box) { var face = new Face(0) { Plane = new Plane(ca[0], ca[1], ca[2]), Colour = Colour, IsSelected = IsSelected, Parent = dummySolid }; face.Vertices.AddRange(ca.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); faces.Add(face); } return faces; }
private Solid SolidifyFace(DataStructures.MapObjects.Map map, Face face) { var solid = new Solid(map.IDGenerator.GetNextObjectID()); solid.Colour = Colour.GetRandomBrushColour(); solid.Faces.Add(face); face.Parent = solid; var center = face.Vertices.Aggregate(Coordinate.Zero, (sum, v) => sum + v.Location) / face.Vertices.Count; var offset = center - face.Plane.Normal * 5; for (var i = 0; i < face.Vertices.Count; i++) { var v1 = face.Vertices[i]; var v2 = face.Vertices[(i + 1) % face.Vertices.Count]; var f = new Face(map.IDGenerator.GetNextFaceID()); f.Parent = solid; f.Plane = new Plane(v1.Location, offset, v2.Location); f.Parent = solid; f.Vertices.Add(new Vertex(offset, f)); f.Vertices.Add(new Vertex(v2.Location, f)); f.Vertices.Add(new Vertex(v1.Location, f)); f.UpdateBoundingBox(); solid.Faces.Add(f); } return solid; }
private IEnumerable<Solid> CreateSolids(DataStructures.MapObjects.Map map, List<Coordinate> points, IEnumerable<ObjFace> objFaces) { var faces = objFaces.Select(x => CreateFace(map, points, x)).ToList(); // See if the solid is valid var solid = new Solid(map.IDGenerator.GetNextObjectID()); solid.Colour = Colour.GetRandomBrushColour(); solid.Faces.AddRange(faces); faces.ForEach(x => x.Parent = solid); if (solid.IsValid()) { // Do an additional check to ensure that all edges are shared var edges = solid.Faces.SelectMany(x => x.GetEdges()).ToList(); if (edges.All(x => edges.Count(y => x.EquivalentTo(y)) == 2)) { // Valid! let's get out of here! yield return solid; yield break; } } // Not a valid solid, decompose into tetrahedrons/etc foreach (var face in faces) { var polygon = new Polygon(face.Vertices.Select(x => x.Location)); if (!polygon.IsValid() || !polygon.IsConvex()) { // tetrahedrons foreach (var triangle in face.GetTriangles()) { var tf = new Face(map.IDGenerator.GetNextFaceID()); tf.Plane = new Plane(triangle[0].Location, triangle[1].Location, triangle[2].Location); tf.Vertices.AddRange(triangle.Select(x => new Vertex(x.Location, tf))); tf.UpdateBoundingBox(); yield return SolidifyFace(map, tf); } } else { // cone/pyramid/whatever yield return SolidifyFace(map, face); } } }
public IEnumerable<MapObject> Create(IDGenerator generator, Box box, ITexture texture, int roundDecimals) { var width = box.Width; var length = Math.Max(1, Math.Abs((int) box.Length)); var height = box.Height; var flatten = (float) _flattenFactor.Value; var text = _text.GetValue(); var family = _fontChooser.GetFontFamily(); var style = Enum.GetValues(typeof (FontStyle)).OfType<FontStyle>().FirstOrDefault(fs => family.IsStyleAvailable(fs)); if (!family.IsStyleAvailable(style)) family = FontFamily.GenericSansSerif; var set = new PolygonSet(); var sizes = new List<RectangleF>(); using (var bmp = new Bitmap(1,1)) { using (var g = System.Drawing.Graphics.FromImage(bmp)) { using (var font = new Font(family, length, style, GraphicsUnit.Pixel)) { for (var i = 0; i < text.Length; i += 32) { using (var sf = new StringFormat(StringFormat.GenericTypographic)) { var rem = Math.Min(text.Length, i + 32) - i; var range = Enumerable.Range(0, rem).Select(x => new CharacterRange(x, 1)).ToArray(); sf.SetMeasurableCharacterRanges(range); var reg = g.MeasureCharacterRanges(text.Substring(i, rem), font, new RectangleF(0, 0, float.MaxValue, float.MaxValue), sf); sizes.AddRange(reg.Select(x => x.GetBounds(g))); } } } } } var xOffset = box.Start.DX; var yOffset = box.End.DY; for (var ci = 0; ci < text.Length; ci++) { var c = text[ci]; var size = sizes[ci]; var gp = new GraphicsPath(); gp.AddString(c.ToString(CultureInfo.InvariantCulture), family, (int)style, length, new PointF(0, 0), StringFormat.GenericTypographic); gp.Flatten(new System.Drawing.Drawing2D.Matrix(), flatten); var polygons = new List<Polygon>(); var poly = new List<PolygonPoint>(); for (var i = 0; i < gp.PointCount; i++) { var type = gp.PathTypes[i]; var point = gp.PathPoints[i]; poly.Add(new PolygonPoint(point.X + xOffset, -point.Y + yOffset)); if ((type & 0x80) == 0x80) { polygons.Add(new Polygon(poly)); poly.Clear(); } } var tri = new List<Polygon>(); Polygon polygon = null; foreach (var p in polygons) { if (polygon == null) { polygon = p; tri.Add(p); } else if (p.CalculateWindingOrder() != polygon.CalculateWindingOrder()) { polygon.AddHole(p); } else { polygon = null; tri.Add(p); } } foreach (var pp in tri) { try { P2T.Triangulate(pp); set.Add(pp); } catch { // Ignore } } xOffset += size.Width; } var zOffset = box.Start.Z; foreach (var polygon in set.Polygons) { foreach (var t in polygon.Triangles) { var points = t.Points.Select(x => new Coordinate((decimal) x.X, (decimal) x.Y, zOffset).Round(roundDecimals)).ToList(); var faces = new List<Coordinate[]>(); // Add the vertical faces var z = new Coordinate(0, 0, height).Round(roundDecimals); for (var j = 0; j < points.Count; j++) { var next = (j + 1) % points.Count; faces.Add(new[] {points[j], points[j] + z, points[next] + z, points[next]}); } // Add the top and bottom faces faces.Add(points.ToArray()); faces.Add(points.Select(x => x + z).Reverse().ToArray()); // Nothing new here, move along var solid = new Solid(generator.GetNextObjectID()) {Colour = Colour.GetRandomBrushColour()}; foreach (var arr in faces) { var face = new Face(generator.GetNextFaceID()) { Parent = solid, Plane = new Plane(arr[0], arr[1], arr[2]), Colour = solid.Colour, Texture = {Texture = texture} }; face.Vertices.AddRange(arr.Select(x => new Vertex(x, face))); face.UpdateBoundingBox(); face.AlignTextureToFace(); solid.Faces.Add(face); } solid.UpdateBoundingBox(); yield return solid; } } }
private static void WriteMapSolid(BinaryWriter bw, Solid s) { bw.WriteCString("CMapSolid"); WriteMapBase(bw, s); bw.Write(s.Faces.Count); foreach (var face in s.Faces) { WriteFace(bw, face); } }
private static Solid ReadMapSolid(BinaryReader br, List<Visgroup> visgroups, IDGenerator generator) { var sol = new Solid(generator.GetNextObjectID()); ReadMapBase(br, sol, visgroups, generator); var numFaces = br.ReadInt32(); for (var i = 0; i < numFaces; i++) { var face = ReadFace(br, generator); face.Parent = sol; face.Colour = sol.Colour; sol.Faces.Add(face); } sol.UpdateBoundingBox(false); return sol; }
private void WriteSolid(StreamWriter sw, Solid solid) { sw.WriteLine("{"); solid.Faces.ForEach(x => WriteFace(sw, x)); sw.WriteLine("}"); }
private void CreateFace(Polygon polygon, Solid parent, Face original) { var verts = polygon.Vertices; var f = new Face(Document.Map.IDGenerator.GetNextFaceID()) { Parent = parent, Plane = new Plane(verts[0], verts[1], verts[2]), Colour = parent.Colour, Texture = original.Texture.Clone() }; f.Vertices.AddRange(verts.Select(x => new Vertex(x, f))); f.UpdateBoundingBox(); f.CalculateTextureCoordinates(true); parent.Faces.Add(f); }
public Carve(IEnumerable<Solid> objects, Solid carver) { _objects = objects.ToList(); _carver = carver; }