public static IEnumerable <TestCaseData> GetTestHulls() { List <PlanePoint> p1 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), }; List <PlanePoint> p1Shuffle = new List <PlanePoint> { new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), }; List <PlanePoint> p3 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 1, 1 }), new PlanePoint(new double[] { 2, 2 }), new PlanePoint(new double[] { 0, 2 }), }; ConvexHull2d ch1 = new ConvexHull2d(p1); ConvexHull2d ch2 = new ConvexHull2d(p1Shuffle); ConvexHull2d ch3 = new ConvexHull2d(p1); ConvexHull2d ch4 = new ConvexHull2d(p3); yield return(new TestCaseData(ch1, ch2).SetName("{m}_When same point but different order.").Returns(true)); yield return(new TestCaseData(ch1, ch3).SetName("{m}_Equals_When same point and same order.").Returns(true)); yield return(new TestCaseData(ch1, ch4).SetName("{m}_Equals_When different point.").Returns(false)); }
public void FindConvexHull2D_Points2d_ReturnConvexHull2D() { PlanePoint[] points = new PlanePoint[] { new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 0.5, 0.5 }), new PlanePoint(new double[] { 1, 1 }), }; List <PlanePoint> expectPoint = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), }; ConvexHull2d expect = expectPoint.ToConvexHull2d(); GiftWrappingAlgorithm giftWrapping = new GiftWrappingAlgorithm(points, Tools.Eps); GrahamScan2d per = new GrahamScan2d(); ConvexHull2d actual = (ConvexHull2d)giftWrapping.Create(); actual.Should().Be(expect); }
public static IEnumerable <TestCaseData> GetTestFaces() { List <PlanePoint> p1 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), }; List <PlanePoint> p1Shuffle = new List <PlanePoint> { new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), }; List <PlanePoint> p3 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 1, 1 }), new PlanePoint(new double[] { 2, 2 }), new PlanePoint(new double[] { 0, 2 }), }; ConvexHull2d ch1 = new ConvexHull2d(p1); ConvexHull2d ch2 = new ConvexHull2d(p1Shuffle); ConvexHull2d ch3 = new ConvexHull2d(p1); ConvexHull2d ch4 = new ConvexHull2d(p3); Face f1 = new Face(3) { ConvexHull = ch1 }; Face f2 = new Face(3) { ConvexHull = ch2 }; yield return(new TestCaseData(f1, f2).SetName("{m}_When same point but different order.").Returns(true)); Face f3 = new Face(3) { ConvexHull = ch1 }; Face f4 = new Face(3) { ConvexHull = ch3 }; yield return(new TestCaseData(f3, f4).SetName("{m}_Equals_When same point and same order.").Returns(true)); Face f5 = new Face(3) { ConvexHull = ch4 }; Face f6 = new Face(3) { ConvexHull = ch1 }; yield return(new TestCaseData(f5, f6).SetName("{m}_Equals_When different point.").Returns(false)); }
public ConvexHull2d GiftWrapping2d(IList <PlanePoint> points) { GiftWrapping2d algorithm = new GiftWrapping2d(); ConvexHull2d result = (ConvexHull2d)algorithm.FindConvexHull(points); return(result); }
public IConvexHull FindConvexHull(IList <PlanePoint> points) { if (points.Count == 3) { return(new ConvexHull2d(points)); } List <PlanePoint> hullPoints = new List <PlanePoint>(); int first = points.IndexOf(points.Min()); Vector currentVector = new Vector(new double[] { 0, -1 }); int currentPlanePoint = first; bool[] processedPoint = new bool[points.Count]; do { hullPoints.Add(points[currentPlanePoint]); double maxCos = double.MinValue; double maxLen = double.MinValue; int next = 0; Vector maxVector = currentVector; for (int i = 0; i < points.Count; i++) { if (currentPlanePoint == i || processedPoint[i]) { continue; } Vector newVector = Point.ToVector(points[currentPlanePoint], points[i]); double newCos = currentVector * newVector; newCos /= newVector.Length * currentVector.Length; if (Tools.GT(newCos, maxCos)) { maxCos = newCos; next = i; maxLen = Point.Length(points[currentPlanePoint], points[next]); maxVector = newVector; } else if (Tools.EQ(maxCos, newCos)) { double dist = Point.Length(points[currentPlanePoint], points[i]); if (Tools.LT(maxLen, dist)) { next = i; maxVector = newVector; maxLen = dist; } } } processedPoint[next] = true; currentPlanePoint = next; currentVector = maxVector; } while (first != currentPlanePoint); ConvexHull2d hull = new ConvexHull2d(hullPoints); return(hull); }
public void Equals_EqualObjects_ReturnTrue() { List <PlanePoint> p1 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), }; List <PlanePoint> p2 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), }; List <PlanePoint> p3 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), }; List <PlanePoint> p4 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), }; ConvexHull2d c1 = p1.ToConvexHull2d(); ConvexHull2d c2 = p2.ToConvexHull2d(); ConvexHull2d c3 = p3.ToConvexHull2d(); ConvexHull2d c4 = p4.ToConvexHull2d(); Face f1 = new Face(hyperplane); f1.ConvexHull = c1; Face f2 = new Face(hyperplane); f2.ConvexHull = c2; Face f3 = new Face(hyperplane); f3.ConvexHull = c3; Face f4 = new Face(hyperplane); f4.ConvexHull = c4; ConvexHull first = new ConvexHull(3); first.Cells.Add(f1); first.Cells.Add(f2); ConvexHull second = new ConvexHull(3); second.Cells.Add(f3); second.Cells.Add(f4); first.Should().Equals(second); }
public void GetPoints_ContainsInsertedPoint() { List <PlanePoint> p1 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), }; ConvexHull2d ch = new ConvexHull2d(p1); List <PlanePoint> points = ch.GetPoints().ToList(); points.Should().Equal(p1); }
public void GetPoints_When3dConvexHull_ReturnTrue() { List <PlanePoint> p1 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), }; List <PlanePoint> p2 = new List <PlanePoint> { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 4, 4 }), }; ConvexHull2d c1 = p1.ToConvexHull2d(); Face f1 = new Face(hyperplane); f1.ConvexHull = c1; ConvexHull2d c2 = p2.ToConvexHull2d(); Face f2 = new Face(hyperplane); f2.ConvexHull = c2; ConvexHull ch1 = new ConvexHull(3); ch1.Cells.Add(f1); ch1.Cells.Add(f2); PlanePoint[] expect = new PlanePoint[] { new PlanePoint(new double[] { 0, 0 }), new PlanePoint(new double[] { 4, 0 }), new PlanePoint(new double[] { 0, 4 }), new PlanePoint(new double[] { 4, 4 }), }; Array.Sort(expect); Point[] actual = ch1.GetPoints().ToArray(); Array.Sort(actual); Assert.AreEqual(expect, actual); }
internal Class22(ConvexHull2d convexHull2d_1) { //System.ComponentModel.LicenseManager.Validate(typeof(Class22)); //base..ctor(); this.convexHull2d_0 = convexHull2d_1; }
internal void method_1(ConvexHull2d convexHull2d_1) { this.convexHull2d_0 = convexHull2d_1; }
public bool Equals_WhenCalls(ConvexHull2d ch1, ConvexHull2d ch2) { return(ch1.Equals(ch2)); }
public static void smethod_0(ObjectId[] objectId_0, CoordinateSystem coordinateSystem_0) { Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; Database workingDatabase = HostApplicationServices.WorkingDatabase; ProgressMeter progressMeter = new ProgressMeter(); MessageFilter value = new MessageFilter(); System.Windows.Forms.Application.AddMessageFilter(value); try { using (Transaction transaction = workingDatabase.TransactionManager.StartTransaction()) { progressMeter.SetLimit(objectId_0.Length); if ((double)objectId_0.Length > 10000.0) { progressMeter.Start("Computing min. perimeter rectangle..."); } CoordinateTransformator coordinateTransformator = new CoordinateTransformator(CoordinateSystem.Global(), coordinateSystem_0); CoordinateTransformator inverseTransformation = coordinateTransformator.GetInverseTransformation(); BlockTable blockTable = (BlockTable)transaction.GetObject(workingDatabase.BlockTableId, (OpenMode)1); BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], (OpenMode)1); ObjectId layerId = DBManager.CurrentLayerId(); DBManager.CurrentLayerName(); PointSet pointSet = new PointSet(); for (int i = 0; i < objectId_0.Length; i++) { progressMeter.MeterProgress(); DBPoint dBPoint = (DBPoint)transaction.GetObject(objectId_0[i], (OpenMode)0, true); Point point = new Point(dBPoint.Position.X, dBPoint.Position.Y, dBPoint.Position.Z); coordinateTransformator.Transform(point); pointSet.Add(point); } ConvexHull2d convexHull2d = new ConvexHull2d(); convexHull2d.InitialPoints = pointSet; convexHull2d.ComputeHull(); PointSet vertices = convexHull2d.Vertices; double z = vertices[0].Z; for (int j = 0; j < vertices.Count; j++) { vertices[j].Z = z; } BoundingRectangle boundingRectangle = convexHull2d.MinimumPerimeterEnclosingRectangle(); PointSet vertices2 = boundingRectangle.GetVertices(); for (int k = 0; k < vertices2.Count; k++) { inverseTransformation.Transform(vertices2[k]); } Point3dCollection point3dCollection = Conversions.ToPoint3dCollection(vertices2.ToList()); Polyline3d polyline3d = new Polyline3d(0, point3dCollection, true); polyline3d.LayerId = (layerId); blockTableRecord.AppendEntity(polyline3d); transaction.AddNewlyCreatedDBObject(polyline3d, true); editor.WriteMessage("\nMinimum perimeter enclosing rectangle properties:"); editor.WriteMessage("\n-------------------------------------------------"); editor.WriteMessage("\nWidth : " + boundingRectangle.Width.ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nHeight : " + boundingRectangle.Length.ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nArea : " + (boundingRectangle.Length * boundingRectangle.Width).ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nPerimeter length: " + (2.0 * boundingRectangle.Length + 2.0 * boundingRectangle.Width).ToString(DBManager.GetFormatFromLUPREC())); transaction.Commit(); } progressMeter.Stop(); } catch (System.Exception ex) { progressMeter.Stop(); throw; } }
public static IEnumerable <TestCaseData> Get2dPoints() { List <PlanePoint> points1 = new List <PlanePoint> { new PlanePoint(new double[] { 1, 5 }), new PlanePoint(new double[] { 1, 3 }), new PlanePoint(new double[] { 2, 1 }), new PlanePoint(new double[] { 4, 1.1 }), new PlanePoint(new double[] { 4, 0.5 }), new PlanePoint(new double[] { 5, 3 }), }; PlanePoint[] expectPoint1 = new PlanePoint[] { new PlanePoint(new double[] { 1, 3 }), new PlanePoint(new double[] { 2, 1 }), new PlanePoint(new double[] { 4, 0.5 }), new PlanePoint(new double[] { 5, 3 }), new PlanePoint(new double[] { 1, 5 }), }; ConvexHull2d expect1 = expectPoint1.ToConvexHull2d(); yield return(new TestCaseData(points1).SetName("{m}_When2dPoints").Returns(expect1)); List <PlanePoint> points2 = new List <PlanePoint> { new PlanePoint(new double[] { 1, 1 }), new PlanePoint(new double[] { 1, 5 }), new PlanePoint(new double[] { 5, 1 }), new PlanePoint(new double[] { 7, 1 }), new PlanePoint(new double[] { 10, 1.1 }), new PlanePoint(new double[] { 10, 5 }), new PlanePoint(new double[] { 10, 8 }), new PlanePoint(new double[] { 10, 10 }), }; PlanePoint[] expectPoint2 = new PlanePoint[] { new PlanePoint(new double[] { 1, 1 }), new PlanePoint(new double[] { 7, 1 }), new PlanePoint(new double[] { 10, 1.1 }), new PlanePoint(new double[] { 10, 10 }), new PlanePoint(new double[] { 1, 5 }), }; ConvexHull2d expect2 = expectPoint2.ToConvexHull2d(); yield return(new TestCaseData(points2).SetName("{m}_WhenMultiplePointsOnLine").Returns(expect2)); IEnumerable <PlanePoint> p1 = new PlanePoint[100]; p1 = p1.Select(((_, i) => new PlanePoint(new double[] { 1, i + 1 }))); IEnumerable <PlanePoint> p2 = new PlanePoint[100]; p2 = p2.Select(((_, i) => new PlanePoint(new double[] { i + 1, 102 }))); IEnumerable <PlanePoint> p3 = new PlanePoint[100]; p3 = p3.Select(((_, i) => new PlanePoint(new double[] { 100, 101 - i }))); IEnumerable <PlanePoint> p4 = new PlanePoint[100]; p4 = p4.Select(((_, i) => new PlanePoint(new double[] { 100 - i, 0.5 }))); List <PlanePoint> points3 = new List <PlanePoint>(); points3.AddRange(p1); points3.AddRange(p2); points3.AddRange(p3); points3.AddRange(p4); PlanePoint[] expectPoint3 = new PlanePoint[] { new PlanePoint(new double[] { 1, 0.5 }), new PlanePoint(new double[] { 100, 0.5 }), new PlanePoint(new double[] { 100, 102 }), new PlanePoint(new double[] { 1, 102 }), }; ConvexHull2d expect3 = expectPoint3.ToConvexHull2d(); yield return(new TestCaseData(points3).SetName("{m}_WhenMultiplePointsOnLine2").Returns(expect3)); //SetName("FindConvexHull2D_WhenMultiplePointsOnLine") }
public static void ComputeMinEnclosingCircle(ObjectId[] idArray, CoordinateSystem actualCS) { Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; Database workingDatabase = HostApplicationServices.WorkingDatabase; ProgressMeter progressMeter = new ProgressMeter(); MessageFilter value = new MessageFilter(); System.Windows.Forms.Application.AddMessageFilter(value); try { using (Transaction transaction = workingDatabase.TransactionManager.StartTransaction()) { progressMeter.SetLimit(idArray.Length); if ((double)idArray.Length > 10000.0) { progressMeter.Start("Computing min. enclosing circle..."); } CoordinateTransformator coordinateTransformator = new CoordinateTransformator(CoordinateSystem.Global(), actualCS); CoordinateTransformator inverseTransformation = coordinateTransformator.GetInverseTransformation(); BlockTable blockTable = (BlockTable)transaction.GetObject(workingDatabase.BlockTableId, (OpenMode)1); BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], (OpenMode)1); ObjectId layerId = DBManager.CurrentLayerId(); DBManager.CurrentLayerName(); PointSet pointSet = new PointSet(); for (int i = 0; i < idArray.Length; i++) { progressMeter.MeterProgress(); DBPoint dBPoint = (DBPoint)transaction.GetObject(idArray[i], (OpenMode)0, true); Point point = new Point(dBPoint.Position.X, dBPoint.Position.Y, dBPoint.Position.Z); coordinateTransformator.Transform(point); pointSet.Add(point); } ConvexHull2d convexHull2d = new ConvexHull2d(); convexHull2d.InitialPoints = pointSet; convexHull2d.ComputeHull(); PointSet vertices = convexHull2d.Vertices; double z = vertices[0].Z; for (int j = 0; j < vertices.Count; j++) { vertices[j].Z = z; } SmallestEnclosingCircle smallestEnclosingCircle = new SmallestEnclosingCircle(vertices); ngeometry.VectorGeometry.Circle circle = smallestEnclosingCircle.ComputeCircle(); Point center = circle.Center; Point point2 = circle.NormalVector.ToPoint(); inverseTransformation.Transform(center); inverseTransformation.Transform(point2); Autodesk.AutoCAD.DatabaseServices.Circle circle2 = new Autodesk.AutoCAD.DatabaseServices.Circle(new Autodesk.AutoCAD.Geometry.Point3d(center.X, center.Y, center.Z), new Autodesk.AutoCAD.Geometry.Vector3d(point2.X, point2.Y, point2.Z), circle.Radius); circle2.LayerId = (layerId); blockTableRecord.AppendEntity(circle2); transaction.AddNewlyCreatedDBObject(circle2, true); editor.WriteMessage("\nMinimum enclosing circle properties:"); editor.WriteMessage("\n------------------------------------"); editor.WriteMessage("\nRadius : " + circle.Radius.ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nArea : " + circle.Area.ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nPerimeter length: " + circle.Circumference.ToString(DBManager.GetFormatFromLUPREC())); transaction.Commit(); } progressMeter.Stop(); } catch (System.Exception ex) { progressMeter.Stop(); throw; } }
public static void Compute2dHull(ObjectId[] idArray, CoordinateSystem actualCS) { Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; Database workingDatabase = HostApplicationServices.WorkingDatabase; ProgressMeter progressMeter = new ProgressMeter(); MessageFilter value = new MessageFilter(); System.Windows.Forms.Application.AddMessageFilter(value); try { using (Transaction transaction = workingDatabase.TransactionManager.StartTransaction()) { progressMeter.SetLimit(idArray.Length); if ((double)idArray.Length > 10000.0) { progressMeter.Start("Computing 2d hull..."); } CoordinateTransformator coordinateTransformator = new CoordinateTransformator(CoordinateSystem.Global(), actualCS); CoordinateTransformator inverseTransformation = coordinateTransformator.GetInverseTransformation(); BlockTable blockTable = (BlockTable)transaction.GetObject(workingDatabase.BlockTableId, (OpenMode)1); BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], (OpenMode)1); ObjectId layerId = DBManager.CurrentLayerId(); DBManager.CurrentLayerName(); PointSet pointSet = new PointSet(); for (int i = 0; i < idArray.Length; i++) { DBPoint dBPoint = (DBPoint)transaction.GetObject(idArray[i], (OpenMode)0, true); Point point = new Point(dBPoint.Position.X, dBPoint.Position.Y, dBPoint.Position.Z); coordinateTransformator.Transform(point); pointSet.Add(point); } ConvexHull2d convexHull2d = new ConvexHull2d(); convexHull2d.InitialPoints = pointSet; convexHull2d.ComputeHull(); PointSet vertices = convexHull2d.Vertices; double z = vertices[0].Z; for (int j = 0; j < vertices.Count; j++) { vertices[j].Z = z; } double areaXY = convexHull2d.AreaXY; for (int k = 0; k < vertices.Count; k++) { inverseTransformation.Transform(vertices[k]); } Point3dCollection point3dCollection = Conversions.ToPoint3dCollection(vertices.ToList()); Polyline3d polyline3d = new Polyline3d(0, point3dCollection, true); polyline3d.LayerId = (layerId); blockTableRecord.AppendEntity(polyline3d); transaction.AddNewlyCreatedDBObject(polyline3d, true); editor.WriteMessage("\n2d convex hull properties:"); editor.WriteMessage("\nNumber of vertices on hull : " + convexHull2d.Vertices.Count); editor.WriteMessage("\nHull perimeter length : " + convexHull2d.PerimeterLength.ToString(DBManager.GetFormatFromLUPREC())); editor.WriteMessage("\nEnclosed area in hull plane: " + areaXY.ToString(DBManager.GetFormatFromLUPREC())); transaction.Commit(); } progressMeter.Stop(); } catch (System.Exception ex) { progressMeter.Stop(); throw; } }