Author Topic: .NET GEOMETRY Routines  (Read 63532 times)

0 Members and 2 Guests are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
.NET GEOMETRY Routines
« on: January 27, 2010, 03:34:28 AM »
LIBRARY THREAD for  AutoCAD GEOMETRY
 Members are encouraged to post any functions, methods, snips regarding
AutoCAD GEOMETRY in .NET : C# ,  VB , F# , Python , etc

Feel free to include comments, descriptive notes, limitations,  and images to document your post.

Please post questions in a regular thread.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #1 on: January 27, 2010, 05:28:26 PM »
Hi,

Some geometry extensions methods for Point2d, Point3d, CircularArc2d, Polyline and Polyline2d classes and two little (maybe usefull) classes: Triangle2d, Triangle3d.

All these classes work together and have to be compiled in a single DLL.

The extension methods requires to reference System.Core (.NET Framework 3.5)

EDIT: extended

Available on Github.

The GeomExt class, contains the following extension methods:

Matrix3d Editor.UCS2WCS()
Matrix3d Editor.WCS2UCS()
Matrix3d Editor.DCS2WCS()
Matrix3d Editor.WCS2DCS()
Matrix3d Editor.DCS2PSDCS()
Matrix3d Editor.PSDCS2DCS()

Matrix3d Viewport.DCS2WCS()
Matrix3d Viewport.WCS2DCS()
Matrix3d Viewport.PSDCS2DCS()
Matrix3d Viewport.DCS2PSDCS()

Point3d Point2d.Convert3d()
Point3d Point2d.Convert3d(Plane plane)
Point3d Point2d.Convert3d(Vector3d normal, double elevation)
Point2d Point2d.Flatten(Vector3d normal)
bool IsBetween(Point2d p1, Point2d p2)
bool IsBetween(Point2d p1, Point2d p2, Tolerance tol)
Point2d Point2d.Polar(double angle, double distance)

Point3d Point3d.Convert2d()
Point3d Point3d.Flatten()
bool IsBetween(Point3d p1, Point3d p2)
bool IsBetween(Point3d p1, Point3d p2, Tolerance tol)
Point3d Point3d.Polar(double angle, double distance)
Point3d Point3d.Trans(int from, int to)
Point3d Point3d.Trans(Editor ed, int from, int to)
Point3d Point3d.Trans(CoordSystem from, CoordSystem to)
Point3d Point3d.Trans(Editor ed, CoordSystem from, CoordSystem to)

Vector3d Vector3d .Flatten()

void Point2dCollection.RemoveDuplicate()
void Point2dCollection.RemoveDuplicate(Tolerance tol)
bool Point2dCollection.Contains(Point2d pt, Tolerance tol)

void Point3dCollection.RemoveDuplicate()
void Point3dCollection.RemoveDuplicate(Tolerance tol)
bool Point3dCollection.Contains(Point3d pt, Tolerance tol)

double CircularArc2d.AlgebricArea()
Point2d CircularArc2d.Centroid()

Point2d Polyline.Centroid2d()
Point3d Polyline.Centroid()
Polyline Polyline.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline.GetOrthoProjectedPolyline(Plane plane)

Point3d Polyline2d.Centroid()
CircularArc3d Polyline2d.GetArcSegmentAt(int index)
CircularArc2d Polyline2d.GetArcSegment2dAt(int index)
LineSegment3d Polyline2d.GetLineSegmentAt(int index)
LineSegment2d Polyline2d.GetLineSegment2dAt(int index)
Polyline Polyline2d.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline2d.GetOrthoProjectedPolyline(Plane plane)
List<Vertex2d> Polyline2d.GetVertices()

Polyline Polyline3d.GetProjectedPolyline(Plane plane, Vector3d direction)
Polyline Polyline3d.GetOrthoProjectedPolyline(Plane plane)

Point3d Region.Centroid()

Point3d Spline.Centroid()

Polyline Ellipse.ToPolyline()


Triangle<T> abstract class

Constructors
Triangle()
Triangle(T[] pts)
Triangle(T a, T b, T c)

Indexor
T Item

Methods
T Inverse()
void Set(T[] pts)
void Set(T a, T b, T c)
T[] ToArray()


Triangle2d : Triangle<Point2d> class

Constructors
Triangle2d() : base()
Triangle2d(Point2d[] pts) : base(pts)
Triangle2d(Point2d a, Point2d b, Point2d c) : base(a, b, c)
Triangle2d(Point2d org, Vector2d v1, Vector2d v2)

Properties
double AlgebricArea
Point2d Centroid
CircularArc2d CircumscribedCircle
CircularArc2d InscribedCircle
bool IsClockwise

Methods
Triangle3d Convert3d(Plane plane)
Triangle3d Convert3d(Vector3d normal, double elevation)
double GetAngleAt(int index)
LineSegment2d GetSegmentAt(int index)
List<Point2d> IntersectWith(LinearEntity2d le2d)
List<Point2d> IntersectWith(LinearEntity2d le2d, Tolerance tol)
bool IsEqualTo(Triangle2d t2d)
bool IsEqualTo(Triangle2d t2d, Tolerance tol)
bool IsPointInside(Point2d pt)
bool IsPointOn(Point2d pt)
void Set(Point2d org, Vector2d v1, Vector2d v2)
Triangle2d TransformBy(Matrix2d mat)


Triangle3d : Triangle<Point3d> class

Constructors
Triangle3d() : base()
Triangle3d(Point3d[] pts) : base(pts)
Triangle3d(Point3d a, Point3d b, Point3d c) : base(a, b, c)
Triangle3d(Point3d org, Vector3d v1, Vector3d v2)

Properties
double Area
Point3d Centroid
CircularArc3d CircumscribedCircle
double Elevation
Vector3d GreatestSlope
Vector3d Horizontal
CircularArc3d InscribedCircle
bool IsHorizontal
Vector3d Normal
double SlopePerCent
Matrix3d SlopeUCS

Methods
Triangle2d Convert2d()
Triangle2d Flatten()
double GetAngleAt(int index)
BoundedPlane GetBoundedPlane()
Plane GetPlane()
LineSegment3d GetSegmentAt(int index)
bool IsEqualTo(Triangle3d t3d)
bool IsEqualTo(Triangle3d t3d, Tolerance tol)
bool IsPointInside(Point3d pt)
bool IsPointOn(Point3d pt)
void Set(Point3d org, Vector3d v1, Vector3d v2)
Triangle3d Transformby(Matrix3d mat)


PolylineSegment class

Constructors

PolylineSegment(Point2d startPoint, Point2d endPoint)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge, double constantWidth)
PolylineSegment(Point2d startPoint, Point2d endPoint, double bulge, double startWidth, double endWidth)
PolylineSegment(Line2dSegment line)
PolylineSegment(CircularArc2d arc)

Properties

double Bulge
Point2d EndPoint
double EndWidth
bool IsLinear
Point2d StartPoint
double StartWidth

Methods

bool Equals(object obj)
PolylineSegment Clone()
int GetHashCode()
double GetParameterOf(Point3d pt)
void Inverse()
CircularArc2d ToCircularArc()
Curve2d ToCurve2d()
LineSegment2d ToLineSegment()
string ToString()


PolylineSegmentCollection : IList<PolylineSegment> class

Constructors

PolylineSegmentCollection()
PolylineSegmentCollection(Circle circle)
PolylineSegmentCollection(Ellipse ellipse)
PolylineSegmentCollection(Polyline pline)
PolylineSegmentCollection(Polyline2d pline)
PolylineSegmentCollection(IEnumerable<PolylineSegment> segments)

Methods

void Add(PolylineSegment segment)
void AddRange(IEnumerable<PolylineSegment> range)
void Clear()
bool Contains(PolylineSegment segment)
int FinIindex(Predicate<PolylineSegment> match)
int GetClosestSegmentTo(Point2d pt)
IEnumerator<PolylineSegment> GetEnumerator()
int IndexOf(PolylineSegment item)
void Insert(int index, PolylineSegment item)
void InsertRange(int index, IEnumerable<PolylineSegment> collection)
List<PolylineSegmentCollection> Join()
List<PolylineSegmentCollection> Join(Tolerance tol)
bool Remove(PolylineSegment item)
void RemoveAt(int index)
public void RemoveRange(int index, int count)
Polyline ToPolyline()

For more information, see the attached documentation.

GeomExtDoc.zip contains a .chm documentation file.
GeometryExtensions(1.6).zip contains the C# source code files and the DLLs (GeometryExtensions_18.dll for A2010-2012 and GeometryExtensions_19 for A2013+)

<EDIT: version 1.6> added CircularArc2d and CircularArc3d GetTangentsTo methods, reorganized: one class per extended type.
« Last Edit: February 07, 2022, 06:23:08 AM by gile »
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #2 on: February 11, 2010, 04:31:52 PM »
Added some extension methods for Polyline2d.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #3 on: February 16, 2010, 06:03:22 PM »
I added some more extensions and rewrite the Triangle2d and Triangle3d classes
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #4 on: April 17, 2010, 08:56:17 AM »
Hi,

I added some properties and methods to the Triangle2d and Triangle3d classes that I needed to deal with 3dfaces (ground modeling).

Triangle2d class:
- Inverse() method returns the triangle in inverse order (Triangle2d)

Triangle3d class :
- GreatestSlope property returns the single unit vector of the triangle plane greatest slope (Vector3d)
- Horizontal property returns the single unit vector of the triangle plane horizontal (Vector3d)
- IsHorizontal property returns true if the triangle plane is parallel to WCS XY plane (Boolean)
- SlopePerCent property returns the percent expression of the greatest slope (Double)
- SlopeUCS property returns a coordinates system matrix which origin is the triangle centroid, X axis is horizontal vector, Y axis the negated greatest slope vector (Matrix3d)
- Flatten() method returns the flatten triangle (Triangle2d)
- Inverse() method returns the triangle in inverse order (Triangle3d)


Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #5 on: August 22, 2010, 02:18:56 PM »
Hi,

Some more adds:

Point3d and Point2d extension methods:
- IsBetween returns true if the first point (first parameter) is strictly between the two others (with or without Tolerance).

Triangle2d methods:
- GetSegmentAt: returns the LinearSegment2d at specified index.
- IntersecWith: returns a List<Point2d> of the intersection points between the triangle and a LinearEntity2d (with or without Tolerance).
- IsPointInside: returns true if the point is strictly inside the triangle.
- IsPointOn: returns true if the point lies on an edge.

Triangle3d methods:
- GetBoundedPlane: returns a BoundedPlane object.
- IsPointInside: returns true if the point is strictly inside the triangle.
- IsPointOn: returns true if the point lies on an edge.
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #6 on: August 23, 2010, 03:49:33 AM »
 :oops:

There was a mistake in the Triangle2d.IsPointInside() method.
Attached file in the first post is updated.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: .NET GEOMETRY Routines
« Reply #7 on: August 23, 2010, 04:45:37 AM »

Thanks gile, that should be a handy library.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

fixo

  • Guest
Re: .NET GEOMETRY Routines
« Reply #8 on: August 23, 2010, 05:23:12 AM »
:oops:

There was a mistake in the Triangle2d.IsPointInside() method.
Attached file in the first post is updated.

Thanks Gilles, I've used it once, very useful
I'm pretty sure you working on Rectangle 2d/3d as well :)

Oleg

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #9 on: August 23, 2010, 05:48:04 AM »
Thanks both, you're welcome.

Quote
I'm pretty sure you working on Rectangle 2d/3d as well
I'm not so sure, IMO, in geometry (almost 3d geometry) rectangles are'nt as useful as triangles which are not deformable, define planes and so on...
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #10 on: February 03, 2011, 05:03:03 AM »
Hi,

Added some extension methods:

void Point2dCollection.RemoveDuplicate()
void Point2dCollection.RemoveDuplicate(Tolerance tol)
bool Point2dCollection.Contains(Point2d pt, Tolerance tol)

void Point3dCollection.RemoveDuplicate()
void Point3dCollection.RemoveDuplicate(Tolerance tol)
bool Point3dCollection.Contains(Point3d pt, Tolerance tol)

And edit the Triangle2d and Triangle3d classes:
both inherits from a 'generic' abstract class: Triangle<T> and have two new properties : CircumscribedCircle and InscribedCircle
« Last Edit: February 03, 2011, 05:11:58 AM by gile »
Speaking English as a French Frog

jgr

  • Guest
Re: .NET GEOMETRY Routines
« Reply #11 on: March 03, 2011, 10:37:26 AM »
« Last Edit: March 03, 2011, 12:47:02 PM by jgr »

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #12 on: April 14, 2011, 05:16:55 PM »
Hi

Here's a F# convex hull (Graham's scan)

EDIT: new code see here

EDIT: a tolerance in comparisons was needed in some cases with colinear points

Code - F#: [Select]
  1. module ConvexHull
  2.  
  3. open System
  4. open Autodesk.AutoCAD.ApplicationServices
  5. open Autodesk.AutoCAD.DatabaseServices
  6. open Autodesk.AutoCAD.EditorInput
  7. open Autodesk.AutoCAD.Geometry
  8. open Autodesk.AutoCAD.Runtime
  9.  
  10. let clockwise (p1:Point2d) (p2:Point2d) (p3:Point2d) =
  11.     (p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X) < 1e-8
  12.    
  13. let convexHull (pts : Point2d seq) =
  14.     let rec fill acc pt =
  15.         match acc with
  16.         | a :: b :: _ when clockwise b a pt -> fill acc.Tail pt
  17.         | _ -> pt :: acc
  18.     let p0 = pts
  19.              |> Seq.reduce (fun p1 p2 ->
  20.                 if p2.Y < p1.Y || (p1.Y = p2.Y && p2.X < p1.X) then p2 else p1)
  21.     pts
  22.     |> List.ofSeq
  23.     |> List.sortBy (fun p ->
  24.         let d = p0.GetDistanceTo(p)
  25.         (Math.Round((p0.X - p.X) / d, 8), d))
  26.     |> List.fold fill []
  27.     |> List.rev
  28.  
  29. [<CommandMethod("ch")>]
  30. let Test() =
  31.     let doc = Application.DocumentManager.MdiActiveDocument
  32.     let psr = doc.Editor.GetSelection(new SelectionFilter([| new TypedValue(0, "POINT") |]))
  33.     if psr.Status = PromptStatus.OK then
  34.         use tr = doc.TransactionManager.StartTransaction()
  35.         use pl = new Polyline()
  36.         psr.Value
  37.         |> Seq.cast
  38.         |> Seq.map (fun (so : SelectedObject) ->
  39.             let pt = tr.GetObject(so.ObjectId, OpenMode.ForRead) :?> DBPoint
  40.             new Point2d(pt.Position.X, pt.Position.Y))
  41.         |> convexHull
  42.         |> List.iteri(fun i p -> pl.AddVertexAt(i, p, 0.0, 0.0, 0.0))
  43.         pl.Closed <- true
  44.         let btr = tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) :?> BlockTableRecord
  45.         btr.AppendEntity(pl) |> ignore
  46.         tr.AddNewlyCreatedDBObject(pl, true)
  47.         tr.Commit()
« Last Edit: June 10, 2017, 11:16:23 AM by gile »
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2505
  • Marseille, France
Re: .NET GEOMETRY Routines
« Reply #13 on: April 15, 2011, 06:09:33 AM »
And the new C# implementation:

EDIT: I repalced the lambda function with a delegate (ComparePoints) so that it can be used with NET Framework 2.0 (default for A2007 -> A2009)

Code - C#: [Select]
  1. using System.Collections.Generic;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.Runtime;
  7. using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
  8.  
  9. namespace ConvexHull
  10. {
  11.     public class Commands
  12.     {
  13.         private Point2d _p0;
  14.  
  15.         private bool Clockwise(Point2d p1, Point2d p2, Point2d p3)
  16.         {
  17.             return ((p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X)) < 1e-8;
  18.         }
  19.  
  20.         private int ComparePoints(Point2d p1, Point2d p2)
  21.         {
  22.             if (p1.IsEqualTo(p2)) return 0;
  23.             double d1 = _p0.GetDistanceTo(p1);
  24.             double d2 = _p0.GetDistanceTo(p2);
  25.             if (d1 == 0.0) return -1;
  26.             if (d2 == 0.0) return 1;
  27.             double cos = (p2.X - _p0.X) / d2 - (p1.X - _p0.X) / d1;
  28.             if (cos < -1e-8) return -1;
  29.             if (cos > 1e-8) return 1;
  30.             return d1.CompareTo(d2);
  31.         }
  32.  
  33.         private List<Point2d> ConvexHull(List<Point2d> pts)
  34.         {
  35.             _p0 = pts[0];
  36.             for (int i = 1; i < pts.Count; i++)
  37.             {
  38.                 Point2d pt = pts[i];
  39.                 if (pt.Y < _p0.Y || (pt.Y == _p0.Y && pt.X < _p0.X))
  40.                     _p0 = pt;
  41.             }
  42.             pts.Sort(ComparePoints);
  43.             for (int i = 1; i < pts.Count - 1; i++)
  44.             {
  45.                 while (i > 0 && Clockwise(pts[i - 1], pts[i], pts[i + 1]))
  46.                 {
  47.                     pts.RemoveAt(i);
  48.                     i--;
  49.                 }
  50.             }
  51.             return pts;
  52.         }
  53.  
  54.         [CommandMethod("ch")]
  55.         public void testCh()
  56.         {
  57.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  58.             Database db = doc.Database;
  59.             Editor ed = doc.Editor;
  60.             TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "POINT") };
  61.             PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
  62.             if (psr.Status != PromptStatus.OK) return;
  63.             using (Transaction tr = db.TransactionManager.StartTransaction())
  64.             using (Polyline pline = new Polyline())
  65.             {
  66.                 List<Point2d> pts = new List<Point2d>();
  67.                 foreach (SelectedObject so in psr.Value)
  68.                 {
  69.                     DBPoint dbPt = (DBPoint)tr.GetObject(so.ObjectId, OpenMode.ForRead);
  70.                     pts.Add(new Point2d(dbPt.Position.X, dbPt.Position.Y));
  71.                 }
  72.                 for (int i = 0; i < ConvexHull(pts).Count; i++)
  73.                 {
  74.                     pline.AddVertexAt(i, pts[i], 0.0, 0.0, 0.0);
  75.                 }
  76.                 pline.Closed = true;
  77.                 pline.SetDatabaseDefaults();
  78.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  79.                 btr.AppendEntity(pline);
  80.                 tr.AddNewlyCreatedDBObject(pline, true);
  81.                 tr.Commit();
  82.             }
  83.         }
  84.     }
  85. }
« Last Edit: June 10, 2017, 11:16:52 AM by gile »
Speaking English as a French Frog

fixo

  • Guest
Re: .NET GEOMETRY Routines
« Reply #14 on: April 18, 2011, 04:18:27 PM »
Nice as always, thanks :)
Regards,

Oleg