Is there any way to found box triangle intersection section or its area? - algorithm

I am trying to develop algorithm to find out not only the fact of intersection but its section or maybe its area. I found at least 6 different cases which may occur and I suppose that there are many more. That is why I am looking for universal algorithm neither try to solve each case separately. I am working with 3D box.

As a triangle and a box are convex shapes, you can use the Sutherland-Hodgman algorithm. https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
It amounts to finding the intersection of a polygon with a half-plane, and repeat this with every side of one of the shapes, against the other. Then the area is found by the shoelace formula.
In the case of an axis-aligned box, the computation is simpler.

Note that it is worth considering only the following points:
Intersection of the box and triangle;
The vertices of the triangle inside the box;
The vertices of the box inside the triangle.
(There are famous algorithms to find these points)
These points are vertices of the polygon that is the desired Intersection section, but we need to sort these points in correct order. You can do this using Graham's algorithm for finding a convex hull.
So, we have found a polygon that is box triangle intersection section.
You also can find it's area using one of the famous algorithms.

Finally I came to solution bu myself. I just intersect my box with edges of triangle to get points inside box or its faces. So I have points of my convex in the wrong order. To solve with problem I use the fact that all the points belong to one plane that is why I can find point by arithmetic mean of convex points which lies exactly inside the convex and use polar coordinates to get angle of each point. All that remains just sort the angles and we will get sorted convex.

Related

calculate intersection area of two triangle

I have been trying to find an algorithm which computes the intersecting area of two triangles but I failed to find any. Can anybody give a clue how to write this algorithm?
I would like something like:
double getAreaOfIntersection(Vector2 p1,Vector2 p2, Vector2 p3,Vector2 p4,Vector2 p5,Vector2 p6 )
where pX represents the 2 triangles.
You could first compute the polygon which describes the intersection area by a clipping algorithm, e.g.:
Sutherland-Hodgman algorithm
Then you would compute the area of the resulting convex polygon, which is rather easy, see, e.g., here:
Area of a Convex Polygon
Determining wether or not a point lies within a given polygon is easy (and even easier for triangles since those are simple polygons). You can use the winding number algorithm (and the crossing number algorithm for simple polygons) which is implemented and well explained here.
Using this you can obtain all the vertices of your intersection polygon:
The vertices pX of a triangle that are contained in the other triangle as well
The points where the two triangles intersect (see intersection of line segments)
You will need to loop over your edges to find all the intersection points, so this should be quick enough as long as you only want to determine intersections of triangles but i would not suggest to try to find intersections of arbitrary polygons this way.

Method to detect intersection between a rectangle and a polygon?

What is the best method to detect whether the red rectangle overlaps the black polygon? Please refer to this image:
There are four cases.
Rect is outside of Poly
Rect intersects Poly
Rect is inside of Poly
Poly is inside of Rect
First: check an arbitrary point in your Rect against the Poly (see Point in Polygon). If it's inside you are done, because it's either case 3 or 2.
If it's outside case 3 is ruled out.
Second: check an arbitrary point of your Poly against the Rect to validate/rule out case 4.
Third: check the lines of your Rect against the Poly for intersection to validate/rule out case 2.
This should also work for Polygon vs. Polygon (convex and concave) but this way it's more readable.
If your polygon is not convex, you can use tessellation to subdivide it into convex subparts. Since you are looking for methods to detect a possible collision, I think you could have a look at the GJK algorithm too. Even if you do not need something that powerful (it provides information on the minimum distance between two convex shapes and the associated witness points), it could prove to be useful if you decide to handle more different convex shapes.
Christer Ericson made a nice Powerpoint presentation if you want to know more about this algorithm. You could also take a look at his book, Real-Time Collision Detection, which is both complete and accessible for anyone discovering collision detection algorithms.
If you know for a fact that the red rectangle is always axis-aligned and that the black region consists of several axis-aligned rectangles (I'm not sure if this is just a coincidence or if it's inherent to the problem), then you can use the rectangle-on-rectangle intersection algorithm to very efficiently compute whether the two shapes overlap and, if so, where they overlap.
If you use axis-aligned rectangles and polygons consist of rectangles only, templatetypedef's answer is what you need.
If you use arbitrary polygons, it's a much more complex problem.
First, you need to subdivide polygons into convex parts, then perform collision detection using, for example, the SAT algorithm
Simply to find whether there is an intersection, I think you may be able to combine two algorithms.
1) The ray casting algorithm. Using the vertices of each polygon, determine if one of the vertices is in the other. Assuming you aren't worried about the actual intersection region, but just the existence of it. http://en.wikipedia.org/wiki/Point_in_polygon
2) Line intersection. If step 1 produces nothing, check line intersection.
I'm not certain this is 100% correct or optimal.
If you actually need to determine the region of the intersection, that is more complex, see previous SO answer:
A simple algorithm for polygon intersection

Intersecting a minkowski difference from the origin in a direction, how do i find the face im intersecting?

Basically i have a set of vertices on the hull of a minkowski difference of two polyhedra. I want to find the distance from the origin to the hull in some arbitrary predetermined direction. Heres a quick 2D sketch:
So the issue is finding what triangular face/plane the ray is going to intersect. Once i have that plane i simply do a line/plane intersection test. My issue is finding the correct face/plane. Any ideas? Is there some set of dot product/cross product/triple product tests i can do to determine it? Or is it more complicated then that?
If your wondering what this is for im using a GJK algorithm to determine whether two objects are intersecting (which i've got working). If there is a collision i would like to find the penetration depth in a particular direction (which will be the direction of motion of the object).
Project the polyhedron in the direction of the ray, and your problem reduces to 2D, and finding which triangle encloses the origin. To test a single triangle, consider whether a given directed line segment (AB) is going clockwise or counterclockwise with respect to the origin. This is easy to determine with a simple cross-product test: it's counterclockwise iff A x (B-A) > 0.
If all three sides of a triangle have the same sense (clockwise or counterclockwise) then the triangle encloses the origin and that's the face you want.
EDIT:
Since your polyhedron is a hull it is convex, And since it is convex you can search the surface in an efficient way. You can traverse the edges in a very simple "walk uphill/downhill" way to find the two vertices farthest along the the ray in either direction. Then after you project the poyhedron you can start from these two points and do a similar climb toward the origin. This will be O(sqrt(n)).

Sorting of Points in 2D space

Suppose random points P1 to P20 scattered in a plane.
Then is there any way to sort those points in either clock-wise or anti-clock wise.
Here we can’t use degree because you can see from the image many points can have same degree.
E.g, here P4,P5 and P13 acquire the same degree.
If your picture has realistic distance between the points, you might get by with just choosing a point at random, say P1, and then always picking the nearest unvisited neighbour as your next point. Traveling Salesman, kind of.
Are you saying you want an ordered result P1, P2, ... P13?
If that's the case, you need to find the convex hull of the points. Walking around the circumference of the hull will then give you the order of the points that you need.
In a practical sense, have a look at OpenCV's documentation -- calling convexHull with clockwise=true gives you a vector of points in the order that you want. The link is for C++, but there are C and Python APIs there as well. Other packages like Matlab should have a similar function, as this is a common geometrical problem to solve.
EDIT
Once you get your convex hull, you could iteratively collapse it from the outside to get the remaining points. Your iterations would stop when there are no more pixels left inside the hull. You would have to set up your collapse function such that closer points are included first, i.e. such that you get:
and not:
In both diagrams, green is the original convex hull, the other colors are collapsed areas.
Find the right-most of those points (in O(n)) and sort by the angle relative to that point (O(nlog(n))).
It's the first step of graham's convex-hull algorithm, so it's a very common procedure.
Edit: Actually, it's just not possible, since the polygonal representation (i.e. the output-order) of your points is ambiguous. The algorithm above will only work for convex polygons, but it can be extended to work for star-shaped polygons too (you need to pick a different "reference-point").
You need to define the order you actually want more precisely.

Good algorithm for determination of non convex 2D figures collision

could you please provide me with some information (or suggest an article) on good collision detection algorithm for 2D non convex figures?
Thanks!
Try this:
http://www.cs.man.ac.uk/~toby/alan/software/
Note that it isn't free for commercial use.
For more details you can continue to this similar question:
A simple algorithm for polygon intersection
To determine if two simple polygons intersect :
If two simple polygons have a non-void intersection then one of the following will happen:
A) One of them has a corner inside the interior of the other one.
B) One of them has a whole edge inside the interior of the other one (the corners of that edge may not necessarily be in the interior). This means the middle of that edge will be inside the interior.
C) The polygons are identical.
D) There are two edges that intersect at an angle. The intersection point not being a corner to any of the polygons.
What you need to do is check if the polygons are identical (have the same corners), or one of the corners or one of the middle of the edges lies inside the interior of the other polygon or if there are two edges that intersect somewhere else than in a corner.
Determining if a point lies on the interior of a polygon.
I always found the wikipedia pages to be quite useful for my needs:
Sutherland Hodgman
Liang Barsky
Weiler Atherton
As well as this paper on the Weiler Atherton algorithm.

Resources