I am testing to determine if two polygons overlap. I have developed a first version which does a simple point in polygon test (Fig 1). However I am looking to revamp that method to deal with situations where no vertices of polygon A are in polygon B but their line segments overlap (Fig B).
Any help getting started would be greatly appreciated.
Here is an example with using Region:
GraphicsPath grp = new GraphicsPath();
// Create an open figure
grp.AddLine(10, 10, 10, 50); // a of polygon
grp.AddLine(10, 50, 50, 50); // b of polygon
grp.CloseFigure(); // close polygon
// Create a Region regarding to grp
Region reg = new Region(grp);
Now you can use the Method Region.IsVisible to determine whether the region is in an Rectangle or Point.
The solution:
I modified some code found here.
private Region FindIntersections(List<PolyRegion> regions)
{
if (regions.Count < 1) return null;
Region region = new Region();
for (int i = 0; i < regions.Count; i++)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddPath(regions[i].Path, false);
region.Intersect(path);
}
}
return region;
}
The result:
Related
I have worked OpenCV with python before. But I am getting hard to using OpenCV with unity.
I training data for the specific points on the face. I can find landmark point and I can show that points on the unity webcamTexture but I want to draw contours on landmarks point that is determined by me. Firstly I need to convert landmark points to convex hull points for draw contour among existing points. How can I convert?
I tried
List<List<Vector2>> landmarkPoints = new List<List<Vector2>>();
OpenCVForUnityUtils.ConvertVector2ListToArray(landmarkPoints) ;
But landmark points doesn't convert. I need to convert landmark points to Hull Points.
Imgproc.drawContours (rgbaMat, hullPoints, -1, new Scalar (0, 255, 0), 2);
Could you help me, please?
I found this solution, finally . If you have match same problem that answer could help you.
// detect face landmark points.
OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);
for (int i = 0; i < trackedRects.Count; i++)
{
List<Vector2> points = faceLandmarkDetector.DetectLandmark(rect);
List<Vector2> pointss = new List<Vector2>();
//Draw Contours
List<Point> pointsss = OpenCVForUnityUtils.ConvertVector2ListToPointList(pointss);
MatOfPoint hullPointMat = new MatOfPoint();
hullPointMat.fromList(pointsss);
List<MatOfPoint> hullPoints = new List<MatOfPoint>();
hullPoints.Add(hullPointMat);
Imgproc.drawContours(rgbaMat, hullPoints, -1, new Scalar(150, 100, 5,255), -1);
}
i'm trying to create navigation mesh on autodesk naviswork using Eyeshot.
convert vertices and IndexTriangle to vertice triangles, after create solid using Solid.FromTriangles().
var solidList = new List();
var Solid = Solid.FromTriangles(item.vertices, item.triangles);
but it doesn't work to boolean operators at i thought.
so i want extract region for using boolean operators.
how can i extract region to mesh or solid (or vertices triangles)?
It is very easy to do. You have to make sure your region vertese are sorted otherwise you might have some issues with it down the line but it's a simple parameter. If the shape isn't hollow here is an example :
// the verteses has to be in order and direction doesn't matter here
// i simply assume it's drawn on X/Y for the purpose of the example
public static Region CreateRegion(List<Point3D> verteses)
{
// create a curve list representing
var curves = new List<ICurve>();
// for each vertex we add them to the list
for (int i = 1; i < verteses.Count; i++)
{
curves.Add(new Line(verteses[i - 1], verteses[i]));
}
// close the region
curves.Add(new Line(verteses.Last(), verteses[0]));
return new Region(new CompositeCurve(curves, true), Plane.XY, true);
}
// this extrude in Z the region
public static Solid CreateSolidFromRegion(Region region, double extrudedHeight)
{
// extrude toward Z by the amount
return region.ExtrudeAsSolid(new Vector3D(0, 0, 1), extrudedHeight);
}
a simple example of creating a cube of 10 by 10 by 10 from vertese (there are much easier method to make a cube but for sake of simplicity i'll make a cube)
// create the 4 verteses
var verteses = new List<Point3D>()
{
new Point3D(0, 0, 0),
new Point3D(10, 0, 0),
new Point3D(10, 10, 0),
new Point3D(0, 10, 0)
}
// create the region on the XY plane using the static method
var region = CreateRegion(verteses);
// extrude the region in Z by 10 units
var solid = CreateSolidFromRegion(region, 10d);
I have a canvas and draw curve with this code:
using (Graphics g = Graphics.FromImage(canvas.BackgroundImage))
{
g.DrawCurve(pen, points);
points is array that I fill that by mouse location points.
In the result I see some jagged lines that I didn't draw.
You can see them here(in red rectangles):
What can i do about this?
What you is see is the somewhat unlucky combination of the default for Linejoin, which is Miter and the default for MiterLimit, which is 10.
Instead you have a choice of either picking one of the other LineJoin options or reducing the MiterLimit to say less than half the Pen.Width..
using (Pen myPen = new Pen(Color.Blue, 24f))
{
// either another LineJoine;
myPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
// or a reduced MiterLimit:
myPen.MiterLimit = 1+ myPen.Width / 5f;
}
i am writing the venn diagram and i have a problem in intersection.
i draw circles but i can't fill the intersection of 3 or more circles.
i fill intersection of two circles with this code
Graphics g = this.CreateGraphics();
GraphicsPath path = new GraphicsPath();
Region region1 = new Region();
Brush blue = new SolidBrush(Color.Blue);
Brush white=new SolidBrush(Color.White);
Rectangle circle1 = new Rectangle(455, 200, 150, 150);
Rectangle circle2 = new Rectangle(455, 275, 150, 150);
g.FillEllipse(blue, circle1);
g.FillEllipse(blue, circle2);
path.AddEllipse(circle1);
path.AddEllipse(circle2);
region1.Intersect(path);
g.FillRegion(white, region1);
i mean something like this
Right now you're trying to intersect an infinite region with a single GraphicsPath object containing both of your circles. Since the region is infinite to begin with, the Intersect method will just return the region occupied by the GraphicsPath object you specific.
To fix this, create your region by passing a GraphicsPath representing the 1st circle to the constructor. Then call the Intersect function using a different GraphicsPath containing the 2nd circle.
I have some images like this where I need to find the central rectangle
Im using a variation of the EmguCV examples to find rectangles and came with this
using (MemStorage storage = new MemStorage())
{ //allocate storage for contour approximation
//Contour<Point> contours = gray.FindContours()
Contour<Point> contours = gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST,
storage);
for (; contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
//Seq<Point> currentContour = contours.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE);
if (contours.Area > MinRectangleArea) //only consider contours with area greater than 20000
{
if (currentContour.Total == 4) //The contour has 4 vertices.
{
bool isRectangle = true;
Point[] pts = currentContour.ToArray();
LineSegment2D[] edges = PointCollection.PolyLine(pts, true);
for (int i = 0; i < edges.Length; i++)
{
double angle = Math.Abs(edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
if (angle < 90 - RectangleAngleMargin || angle > RectangleAngleMargin + 90)
{
isRectangle = false;
break;
}
}
if (isRectangle)
{
boxList.Add(currentContour.GetMinAreaRect());
}
}
}
}
}
And the result of executing that over those images sometimes finds this two rectangles:
The orange rectangle is ok, thats what I need. But I dont want the blue. Sometimes the four vertex are in the border of the image, usually one of them is out.
Changing the RETR_TYPE of the FindContours function to CV_RETR_EXTERNAL, I only get the blue rectangle, so I wonder if there is an option of NOT getting the contours with external points.
The real image actually can have smaller rectangles inside the orange (or a line appears splitting the rectangle), so after that I´m selecting the bigger rectangle to be the one I want, but cant do it that way with that blue one.
Taking a look at your sample image I would choose another approach.
Instead of classical contour detection, If you perform Hough line detection and then peform intersections of line found, you will find exactly the four vertices of the rectangle you are searching for...
If you need some help in coding let me know and I will edit my answer.