How to do radial collections querying in C#? - c#

I have lots of points (with .X and .Y coordinates) in a 2d space For example a KDTree or some other IEnumerable<AnyXYPointImpl> collection. I want to do query that is not square but Circle\Radius based:
What I want is simple: having a point P, direction D and half angle a we want to get all points in the collection area covered by a sector formed by them.
So how to do such radial collections querying in C# with LINQ?

Here is a solution:
Example usage:
var field = new List<Vector2>() {
new Vector2(-1,1), new Vector2(1,1),
new Vector2(-1,-1), new Vector2(1,-1)
};
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,2), 45.0)));
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,-2), 45.0)));
Console.WriteLine(string.Join("; ", field.IsInSector(new Vector2(0,0), new Vector2(2,0), 90.0)));
Main logic to check point is it in sector via IEnumerable extension:
public static class Extenctions {
public static IEnumerable<IXYPoint> IsInSector(this IEnumerable<IXYPoint> source, Vector2 pointToCheck, Vector2 direction, double halfAngle)
{
if(!source.Any() || pointToCheck == null || halfAngle <= 0)
return new IXYPoint[0];
var dirVector = new Vector2() {
X = direction.X - pointToCheck.X,
Y = direction.Y - pointToCheck.Y
};
var radius = Distance(direction, pointToCheck);
var result = new List<IXYPoint>();
foreach(var p in source)
{
if(Distance(p, pointToCheck) > radius){ // check is point in circle
continue;
}
if(IsPointInSector(dirVector, p, halfAngle)) // main check
result.Add(p);
}
return result;
}
private static double Distance(IXYPoint from, IXYPoint to)
{
return Math.Sqrt(Math.Pow(to.X - from.X,2) + Math.Pow(from.Y-to.Y,2));
}
private static bool IsPointInSector(IXYPoint directionVector, IXYPoint pointToCheck, double halfAngle)
{
var rq0 = Math.Pow(directionVector.X,2) + Math.Pow(directionVector.Y,2);
var rq = Math.Pow(pointToCheck.X,2) + Math.Pow(pointToCheck.Y,2);
return rq0 >= rq && (directionVector.X*pointToCheck.X + directionVector.Y*pointToCheck.Y)/Math.Sqrt(rq0*rq) >= Math.Cos(halfAngle/180.0*Math.PI);
}
}

Here's a non-LINQ solution.
var filteredPoints = new List<Point2D>();
foreach (var point in points)
if (point.IsInSector(origin, d, a))
filteredPoints.Add(point);

Related

How to get the max value of a function in a list of objects (C#) ? -Beginner- [duplicate]

This question already has answers here:
How to use LINQ to select object with minimum or maximum property value
(20 answers)
How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]
(9 answers)
Closed 1 year ago.
first post here so I hope this is how I should ask questions here,
So, I have a function used in different classes (override), and I need to display the MaxValue of this function across different data. (don't know if I said it correcty).
Tried this first but it kept giving me the same answer even though it wasn't the max value:
class Program
{
static void Main(string[] args)
{
Trapezoid[] Shape = new Trapezoid[5];
Shape[0] = new Trapezoid(4,3.5,2);
Shape[1] = new Rectangle(9,2);
Shape[2] = new Square(5);
Shape[3] = new Trapezoid(2,6.8,10);
Shape[4] = new Square(8);
Trapezoid maximumArea = null;
foreach (Trapezoid trapezoid1 in Shape)
{
double area1 = trapezoid1.Area();
foreach (Trapezoid trapezoid2 in Shape)
{
double area2 = trapezoid1.Area();
if (area1 >= area2)
{
maximumArea = trapezoid1;
}
else
{
maximumArea = trapezoid2;
}
}
Console.WriteLine(maximumArea.ToString());
Console.ReadLine();
}
}
the second time I tried this but couldn't make it work:
class Program
{
static void Main(string[] args)
{
Trapezoid[] Shape = new Trapezoid[5];
Shape[0] = new Trapezoid(4,3.5,2);
Shape[1] = new Rectangle(9,2);
Shape[2] = new Square(5);
Shape[3] = new Trapezoid(2,6.8,10);
Shape[4] = new Square(8);
Trapezoid maximumArea = null;
foreach (Trapezoid trapezoid1 in Shape)
{
double area1 = trapezoid1.Area();
foreach (Trapezoid trapezoid2 in Shape)
{
double area2 = trapezoid2.Area();
maximumArea = Math.Max (area1 , area2);
}
}
Console.WriteLine(maximumArea.ToString());
Console.ReadLine();
}
}
the function is Area, if you need to see it:
class Trapezoid
{
protected double a,b,h;
public Trapezoid(double a, double b, double h)
{
this.a = a;
this.b = b;
this.h = h;
}
public virtual double Area()
{
return ((a + b) * h) / 2;
}
public override string ToString()
{
return "The area of this Trapezoid is " + Area() + ".";
}
}
did the same thing in rectangle and square classes by overriding (inherited classes).
I hope you can help me, thank you. :)
You need to use maximumArea to store the max of the list, not the max of the current pair.
Trapezoid maximumArea = new Square(0); //Smallest possible trapezoid
foreach (Trapezoid t in Shape)
{
if (t.Area() > maximumArea.Area()) maximumArea = t;
}
Console.WriteLine(maximumArea);
You can also shorten this considerably using LINQ:
var maximumArea = shape.OrderByDescending( x => x.Area() ).First();
And if there's a chance that some trapezoids will tie, maybe you should get a list:
var max = shape.Max( x => x.Area() );
var maximumAreas = shape.Where( x => x.Area() == max).ToList();
Use the power of Linq:
using System.Linq;
then
var trapezoidWithTheLargestArea = Shape.OrderByDescending(s => s.Area()).First()

Check if an stl file may contain two models

An stl file may contain 2 3D models. Is there any way I can detect if there are 2 or more models stored in one stl file?
In my current code, it can detect that there are 2 models in the example, but there are instances that it detects a lot of model even though it only has one.
The Triangle class structure has Vertices that contains 3 points (x, y, z)..
Sample STL File:
EDIT: Using #Gebb's answer this is how I implemented it:
private int GetNumberOfModels(List<TopoVertex> vertices)
{
Vertex[][] triangles = new Vertex[vertices.Count() / 3][];
int vertIdx = 0;
for(int i = 0; i < vertices.Count() / 3; i++)
{
Vertex v1 = new Vertex(vertices[vertIdx].pos.x, vertices[vertIdx].pos.y, vertices[vertIdx].pos.z);
Vertex v2 = new Vertex(vertices[vertIdx + 1].pos.x, vertices[vertIdx + 1].pos.y, vertices[vertIdx + 1].pos.z);
Vertex v3 = new Vertex(vertices[vertIdx + 2].pos.x, vertices[vertIdx + 2].pos.y, vertices[vertIdx + 2].pos.z);
triangles[i] = new Vertex[] { v1, v2, v3 };
vertIdx += 3;
}
var uniqueVertices = new HashSet<Vertex>(triangles.SelectMany(t => t));
int vertexCount = uniqueVertices.Count;
// The DisjointUnionSets class works with integers, so we need a map from vertex
// to integer (its id).
Dictionary<Vertex, int> indexedVertices = uniqueVertices
.Zip(
Enumerable.Range(0, vertexCount),
(v, i) => new { v, i })
.ToDictionary(vi => vi.v, vi => vi.i);
int[][] indexedTriangles =
triangles
.Select(t => t.Select(v => indexedVertices[v]).ToArray())
.ToArray();
var du = new XYZ.view.wpf.DisjointUnionSets(vertexCount);
// Iterate over the "triangles" consisting of vertex ids.
foreach (int[] triangle in indexedTriangles)
{
int vertex0 = triangle[0];
// Mark 0-th vertexes connected component as connected to those of all other vertices.
foreach (int v in triangle.Skip(1))
{
du.Union(vertex0, v);
}
}
var connectedComponents =
new HashSet<int>(Enumerable.Range(0, vertexCount).Select(x => du.Find(x)));
return connectedComponents.Count;
}
In some cases, it produces the correct output, but for the example image above, it outputs 3 instead of 2. I am now trying to optimize the snippet #Gebb gave to use float values since I believe that the floating points are necessary to the comparisons. Does anyone have a way to do that as well? Maybe I need another perspective.
You could do this by representing vertices and connections between them as a graph and finding the number of connected components of the graph with the help of the Disjoint-set data structure.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Vertex = System.ValueTuple<double,double,double>;
namespace UnionFindSample
{
internal class DisjointUnionSets
{
private readonly int _n;
private readonly int[] _rank;
private readonly int[] _parent;
public DisjointUnionSets(int n)
{
_rank = new int[n];
_parent = new int[n];
_n = n;
MakeSet();
}
// Creates n sets with single item in each
public void MakeSet()
{
for (var i = 0; i < _n; i++)
// Initially, all elements are in
// their own set.
_parent[i] = i;
}
// Finds the representative of the set
// that x is an element of.
public int Find(int x)
{
if (_parent[x] != x)
{
// if x is not the parent of itself, then x is not the representative of
// his set.
// We do the path compression by moving x’s node directly under the representative
// of this set.
_parent[x] = Find(_parent[x]);
}
return _parent[x];
}
// Unites the set that includes x and
// the set that includes x
public void Union(int x, int y)
{
// Find representatives of two sets.
int xRoot = Find(x), yRoot = Find(y);
// Elements are in the same set, no need to unite anything.
if (xRoot == yRoot)
{
return;
}
if (_rank[xRoot] < _rank[yRoot])
{
// Then move x under y so that depth of tree remains equal to _rank[yRoot].
_parent[xRoot] = yRoot;
}
else if (_rank[yRoot] < _rank[xRoot])
{
// Then move y under x so that depth of tree remains equal to _rank[xRoot].
_parent[yRoot] = xRoot;
}
else
{
// if ranks are the same
// then move y under x (doesn't matter which one goes where).
_parent[yRoot] = xRoot;
// And increment the result tree's
// rank by 1
_rank[xRoot] = _rank[xRoot] + 1;
}
}
}
internal class Program
{
private static void Main(string[] args)
{
string file = args[0];
Vertex[][] triangles = ParseStl(file);
var uniqueVertices = new HashSet<Vertex>(triangles.SelectMany(t => t));
int vertexCount = uniqueVertices.Count;
// The DisjointUnionSets class works with integers, so we need a map from vertex
// to integer (its id).
Dictionary<Vertex, int> indexedVertices = uniqueVertices
.Zip(
Enumerable.Range(0, vertexCount),
(v, i) => new {v, i})
.ToDictionary(vi => vi.v, vi => vi.i);
int[][] indexedTriangles =
triangles
.Select(t => t.Select(v => indexedVertices[v]).ToArray())
.ToArray();
var du = new DisjointUnionSets(vertexCount);
// Iterate over the "triangles" consisting of vertex ids.
foreach (int[] triangle in indexedTriangles)
{
int vertex0 = triangle[0];
// Mark 0-th vertexes connected component as connected to those of all other vertices.
foreach (int v in triangle.Skip(1))
{
du.Union(vertex0, v);
}
}
var connectedComponents =
new HashSet<int>(Enumerable.Range(0, vertexCount).Select(x => du.Find(x)));
int count = connectedComponents.Count;
Console.WriteLine($"Number of connected components: {count}.");
var groups = triangles.GroupBy(t => du.Find(indexedVertices[t[0]]));
foreach (IGrouping<int, Vertex[]> g in groups)
{
Console.WriteLine($"Group id={g.Key}:");
foreach (Vertex[] triangle in g)
{
string tr = string.Join(' ', triangle);
Console.WriteLine($"\t{tr}");
}
}
}
private static Regex _triangleStart = new Regex(#"^\s+outer loop");
private static Regex _triangleEnd = new Regex(#"^\s+endloop");
private static Regex _vertex = new Regex(#"^\s+vertex\s+(\S+)\s+(\S+)\s+(\S+)");
private static Vertex[][] ParseStl(string file)
{
double ParseCoordinate(GroupCollection gs, int i) =>
double.Parse(gs[i].Captures[0].Value, CultureInfo.InvariantCulture);
var triangles = new List<Vertex[]>();
bool isInsideTriangle = false;
List<Vertex> triangle = new List<Vertex>();
foreach (string line in File.ReadAllLines(file))
{
if (isInsideTriangle)
{
if (_triangleEnd.IsMatch(line))
{
isInsideTriangle = false;
triangles.Add(triangle.ToArray());
triangle = new List<Vertex>();
continue;
}
Match vMatch = _vertex.Match(line);
if (vMatch.Success)
{
double x1 = ParseCoordinate(vMatch.Groups, 1);
double x2 = ParseCoordinate(vMatch.Groups, 2);
double x3 = ParseCoordinate(vMatch.Groups, 3);
triangle.Add((x1, x2, x3));
}
}
else
{
if (_triangleStart.IsMatch(line))
{
isInsideTriangle = true;
}
}
}
return triangles.ToArray();
}
}
}
I'm also using the fact that System.ValueTuple implements Equals and GetHashCode in an appropriate way, so we can easily compare vertices (this is used implicitly by HashSet) and use them as keys in a dictionary.

C# custom sort by distance from a point

I am trying to sort a list using IComparer there are about 20,000 items in my list. The first approximately 100 and last 100 items are sorted but the center is not. I am not sure what I am doing wrong. Attached is the parts of the code that does the sort.
class myclass:me
{
private void mysort()
{
// Sort the data
DistComparer dc = new DistComparer();
st.Sort(dc);
}
}
class DistComparer : IComparer<Tower>
{
public int Compare(st x, st y)
{
double dist1 = getDistance(new PointF(45.0f, -80f), new PointF(x.Lat, x.Long));
double dist2 = getDistance(new PointF(45.0f, -80f), new PointF(y.Lat, y.Long));
if (dist1 > dist2)
return 1;
else if (dist1 == dist2)
return 0;
else
return -1;
}
private static double getDistance(PointF pt1, PointF pt2)
{
return Math.Sqrt(Math.Pow((pt1.X - pt2.X), 2) + Math.Pow((pt1.Y - pt2.Y), 2));
}
}
}

Implementing a fast DBSCAN in C#

I tried to implement a DBSCAN in C# using kd-trees. I followed the implementation from:
http://www.yzuzun.com/2015/07/dbscan-clustering-algorithm-and-c-implementation/
public class DBSCANAlgorithm
{
private readonly Func<PointD, PointD, double> _metricFunc;
public DBSCANAlgorithm(Func<PointD, PointD, double> metricFunc)
{
_metricFunc = metricFunc;
}
public void ComputeClusterDbscan(ScanPoint[] allPoints, double epsilon, int minPts, out HashSet<ScanPoint[]> clusters)
{
clusters = null;
var allPointsDbscan = allPoints.Select(x => new DbscanPoint(x)).ToArray();
var tree = new KDTree.KDTree<DbscanPoint>(2);
for (var i = 0; i < allPointsDbscan.Length; ++i)
{
tree.AddPoint(new double[] { allPointsDbscan[i].ClusterPoint.point.X, allPointsDbscan[i].ClusterPoint.point.Y }, allPointsDbscan[i]);
}
var C = 0;
for (int i = 0; i < allPointsDbscan.Length; i++)
{
var p = allPointsDbscan[i];
if (p.IsVisited)
continue;
p.IsVisited = true;
DbscanPoint[] neighborPts = null;
RegionQuery(tree, p.ClusterPoint.point, epsilon, out neighborPts);
if (neighborPts.Length < minPts)
p.ClusterId = (int)ClusterIds.NOISE;
else
{
C++;
ExpandCluster(tree, p, neighborPts, C, epsilon, minPts);
}
}
clusters = new HashSet<ScanPoint[]>(
allPointsDbscan
.Where(x => x.ClusterId > 0)
.GroupBy(x => x.ClusterId)
.Select(x => x.Select(y => y.ClusterPoint).ToArray())
);
return;
}
private void ExpandCluster(KDTree.KDTree<DbscanPoint> tree, DbscanPoint p, DbscanPoint[] neighborPts, int c, double epsilon, int minPts)
{
p.ClusterId = c;
for (int i = 0; i < neighborPts.Length; i++)
{
var pn = neighborPts[i];
if (!pn.IsVisited)
{
pn.IsVisited = true;
DbscanPoint[] neighborPts2 = null;
RegionQuery(tree, pn.ClusterPoint.point, epsilon, out neighborPts2);
if (neighborPts2.Length >= minPts)
{
neighborPts = neighborPts.Union(neighborPts2).ToArray();
}
}
if (pn.ClusterId == (int)ClusterIds.UNCLASSIFIED)
pn.ClusterId = c;
}
}
private void RegionQuery(KDTree.KDTree<DbscanPoint> tree, PointD p, double epsilon, out DbscanPoint[] neighborPts)
{
int totalCount = 0;
var pIter = tree.NearestNeighbors(new double[] { p.X, p.Y }, 10, epsilon);
while (pIter.MoveNext())
{
totalCount++;
}
neighborPts = new DbscanPoint[totalCount];
int currCount = 0;
pIter.Reset();
while (pIter.MoveNext())
{
neighborPts[currCount] = pIter.Current;
currCount++;
}
return;
}
}
//Dbscan clustering identifiers
public enum ClusterIds
{
UNCLASSIFIED = 0,
NOISE = -1
}
//Point container for Dbscan clustering
public class DbscanPoint
{
public bool IsVisited;
public ScanPoint ClusterPoint;
public int ClusterId;
public DbscanPoint(ScanPoint point)
{
ClusterPoint = point;
IsVisited = false;
ClusterId = (int)ClusterIds.UNCLASSIFIED;
}
}
, and modifying the regionQuery(P, eps) to invoke the nearest neighbour function of a kd-tree. To do so, I used the kd-sharp library for C#, which is one of the fastest kd-tree implementations out there.
However, when given a dataset of about 20000 2d points, its performance is in the region of 40s, as compared to the scikit-learn python implementation of DBSCAN, which given the same parameters, takes about 2s.
Since this algorithm is for a C# program that I am writing, I am stuck using C#. As such, I would like to find out what am I still missing out in terms of optimization of the algorithm?

How to speed up conversion of lat-long to a SqlGeometry?

I have this code, it takes the geometry of countries and also a set of points, and then it only returns the points within those countries :
public static IEnumerable<Point> RemovePointsOutsideBorders(IEnumerable<Point> points, IEnumerable<Country> countries)
{
var cc = new List<Point>();
var SPAT_REF_ID = 4326;
foreach (var p in points)
{
var validText = new SqlChars(new SqlString(string.Format("POINT({0} {1})", p.Longitude, p.Latitude)));
var geoPoint = SqlGeometry.STPointFromText(validText, SPAT_REF_ID);
foreach (var c in countries)
{
if(c.Polygon.STIntersects(geoPoint))
{
cc.Add(p);
break;
}
}
}
return cc;
}
Current its quite slow, there are about 4000 points, with double lat/long values, the conversion from that into a SqlGeometry is slow (takes about 25 seconds -- I need this to be perhaps down to a second or two):
var s = new SqlChars(new SqlString(string.Format("POINT({0} {1})", p.Longitude, p.Latitude)));
var pGeo = SqlGeometry.STPointFromText(s, SPAT_REF_ID);
This is only done because the SqlGeometry.Point takes x,y instead of lat,long ... any tips on how can I speed this up?
I already know the SqlGeometry (c.Polygon) could be reduced to speed up things, however I am unable to control that. What I am after is a way to speed up the conversion from lat/long to SqlGeometry point.
This is the solution I came up with in the end, it does the whole thing in half a second :
public static IEnumerable<Point> RemovePointsOutsideBorders(IEnumerable<Point> points, IEnumerable<Country> countries)
{
// join all the country polygons into one (make a stamp)
var allCountryPolygon = countries.Select(x => x.Polygon).UnionAll();
// make a single geometry shape from our evenly spaced extent points (cookie dough)
var pointsGeo = PointsToGeometry(points);
// do an intersect (stamp country shape over extent based geometry)
var cookieOfPoints = pointsGeo.STIntersection(allCountryPolygon);
// how many points left inside? pick them all back out
for (int n = 1; n <= cookieOfPoints.STNumPoints(); n++)
{
var insidePoint = cookieOfPoints.STPointN(n);
yield return new Point
{
Longitude = insidePoint.STX.Value,
Latitude = insidePoint.STY.Value
};
}
}
public static SqlGeometry PointsToGeometry(IEnumerable<Point> points)
{
var bld = new SqlGeometryBuilder();
bld.SetSrid(4326);
bld.BeginGeometry(OpenGisGeometryType.MultiPoint);
foreach (var p in points)
{
bld.BeginGeometry(OpenGisGeometryType.Point);
bld.BeginFigure(p.Longitude, p.Latitude);
bld.EndFigure();
bld.EndGeometry();
}
bld.EndGeometry();
return bld.ConstructedGeometry;
}
public static class ExtensionMethods
{
/// <summary>
/// Joins many geometries into one
/// </summary>
/// <param name="geometries">geometries to join</param>
/// <returns>composite geometry</returns>
public static SqlGeometry UnionAll(this IEnumerable<SqlGeometry> geometries)
{
var compositeGeometry = geometries.First();
foreach (var g in geometries.Skip(1))
{
compositeGeometry = compositeGeometry.STUnion(g);
}
return compositeGeometry;
}
}

Categories

Resources