How to get common part of two flat meshes? - c#

I am working on cutting meshes. What am I exactly trying to achieve is that:
Input - two meshes(procedurally generated of course)
Output - three meshes(or more, depends on the given meshes)
A - which is a meshA substract mesh B,
B - which is a common part of meshes A and B,
C - which is a mesh B substract mesh A.
I am using the Triangulator that for the given set of vertices creates triangles for me. All I have to do is to give him those vertices, and here goes the question. Is there any algorithm that would help me to do this? Maybe your brilliant idea that came into your mind at the time you saw this picture? I am working in Unity(C#) so anything related to Unity Tools is helpful aswell. Thanks !
EDIT:
I am using this clipping library: http://sourceforge.net/projects/polyclipping/ and everything works fine untill this case:
I am trying to get the difference A-B and it should look like the one from Picture 2), unfortunately the output is mesh A and mesh B as in the picture 1).
What i do:
Clipper c = new Clipper();
c.AddPath(here goes the vertices of mesh A, polyType.Subject, true);
c.AddPath(here goes the vertices of mesh B, polyType.Clip, true);
c.Execute(ClipType.ctDIfference, a list of lists for my output, PolyFillType.NonZero, PolyFillType.NonZero);
I've already tried to change PolyFillTypes but it changed nothing. And here I am, asking for your advices :)

It is enough to work with mesh boundaries. With that you have two polygons and problem of finding there intersection. Some external library can be used for it, like GPC.
After intersection is found just perform triangulation of these three polygons.

Related

Is there a built in way of efficiently finding if a vector is in a vector array and if so what # variable it is

Hello I am creating a procedurally generated cave generation script and I have gotten down all the perlin noise garbage out of the way and am trying to transform the vertices into a mesh . I understand that I need to declare the faces for it and need some form of marching cubes algorithm. For me to know which direction to render the face in I need my script to be aware of all the vertices around it by searching through the vertices. Is there any way my script can efficiently search through a vector3 array to find if a vector3 is in there and if so what place in the array is the Vector3 in?
If you're using a triangulation lookup table based implementation of marching cubes, you could store a normal vector alongside the face in the same table entry. A video by Sebastian Lague mentions using such a table. I'm not exactly sure where he downloaded it from, but he includes it in his repo which is MIT licensed. Video, Table (EDIT: The order of a triangle's vertices alone may be sufficient to define its direction, and you may not need an explicit normal vector)
Also heads up: Old Perlin noise tends to be visibly grid aligned. Most times I see it used, appear to be because a library provided it or because it was mentioned in a tutorial, and not because it was actually the best choice for the application. Simplex-type noises generally produce less grid-aligned results. It's straightforward to import external noise into Unity. It looks like you might need to anyway, if your implementation depends on 3D noise. Here are the noises from my repo that use an open implementation for 3D, a tailored gradient table, and rotated evaluators that are good for terrain. There are a lot of other options out there too, though they may not have these aspects in particular. Note that the range is [-1,1] not [0,1] so if your current noise is [0, 1], you might need to do noise(...) * 0.5 + 0.5 to correct that. Choose the 2F version if you have a lot of octaves, or the 2S version if you have one octave or are doing ridged noise.

Wall generator script

I have some trouble.
I need generate walls by path.
For example. I have vectors A, B, C. And I need to find vectors D, E, F for creating volumetric walls around ABC.
Firstly, I going on easiest way - scaling ABC (with finding a centroid).
But it's not a correct solution, b/c I need to set correct width of walls, and I need to find some additional points like this (point J and point K with JK perpendicular AB)
I will be gratefull for any help!
You will need to find out normals to the outer edge of the polygon.
There could be two approaches.
One is using edge normals, another is vertex normals.
On edge normals you will need to find out what direction is outside (depends on closed / open)
If you use vertex normals, you will only need to translate the vertex to the new position. Finding out a good vertex normal is the main problem.
This is known as offsetting. More details can be found below.
An algorithm for inflating/deflating (offsetting, buffering) polygons

Quadrangle intersection algorithm

I'm currently writing a game (2D with OpenTK) in which there is a lot of rotation, and with it comes that I sometimes need to get the intersection between these lines/shapes:
Two quadrangles http://files.myopera.com/antonijn/albums/12693002/TwoQuadrangles.png
I know the rotation (in degrees) of both of them, and therefore I know the position of all the vertices in both shapes.
The algorithm needs to give me a bool on whether they intersect, or better yet, the coordinates of the intersections.
I have written my own algorithm, which scrolls through the sides of the first box, gets the formula for each side and compares them to the formulas of the lines of the second box. Now, this doesn't work when the lines are upright (slope of float.Infinity or float.NegativeInfinity), is a pain to debug and is far from fast, so I need a better one!
Any suggestions?
I ended up using the SAT method, as suggested by Nickon, thanks a bunch mate!

Collision detecting custom sketched shape, represented as list of points

I have a set of points, drawn by the user. They will be drawing around some objects.
I need to somehow turn this set of points into a shape, so I can find the area to detect collisions.
An image will clarify:
Set of points represented as shape http://www.imagechicken.com/uploads/1277188630025178800.jpg
.
The best idea I have had so far involves iterating over every pixel determining if it is 'inside' or 'outside' the shape, but that would be horribly slow, and I'm not even sure how to do the determining 'inside'/'outside' bit...
Any hints? I am using .NET (C# and XNA) if that helps you help me!
You can think of your shape as an union of several shapes each of which is a simple closed polygon.
the check for every object if it is inside any of the polygons in the following manner:
All dots connected by lines - each line has an equation defining it.
For every object - build an equation for a line passing through this object.
now - for each object equation you need to check how many lines (those between the dots) intersects this object equation - but count only the intersection points that are in the rage between the two dots (and not in the rest of the line outside the two dots) and only the intersection points that are in one side of the object (pick a side - doesn't matter).
If the count is even - the object is outside the shape - otherwise it is inside.
Just a precursor to anything I will say, I have no experience in this field, this is just how I would go about the problem.
A tactic a lot of games use for this is known as Hit Boxes. It is much easier to detect if a point is inside a square than any other figure. But this doesn't give you an exact collision, it could be right outside your desired object.
I've seen Collision 'Bubbles' used before. Here is a link I found for you. This explains the use of Collision Bubbles in the console game Super Smash Brothers.
Given a point, the distance formula, and a radius, you can easily implement collision bubbles.
To take it even one step forward, I did a little bit of research, I saw a nifty little algorithm (more advanced that the top two suggestions), the "Gilbert-Johnson-Keerthi Collision detection algorithm for convex objects." Here is a link for ya. The implementation provided is written in D. If your working in C# it shouldn't be too hard to translate (I would highly suggest digesting the algorithm too).
Hope this gives you some direction.
Well I got it working thanks to some help on another forum.
I used the GraphicsPath class to do all the hard work for me.
This is what my method ended up looking like:
public bool IsColliding(Vector2 point)
{
GraphicsPath gp = new GraphicsPath();
Vector2 prevPoint = points[0];
for (int i = 1; i < points.Count; i++)
{
Vector2 currentPoint = points[i];
gp.AddLine(prevPoint.X, prevPoint.Y, currentPoint.X, currentPoint.Y);
prevPoint = currentPoint;
}
gp.CloseFigure(); //closing line segment
return gp.IsVisible(point.X, point.Y);
}
Thanks for your suggestions both of you

Can DirectX 3D Meshes be merged, or joined together

C# programmer, beginner DirectX.
Have created 2 meshes using Mesh.Cylinder but need to combine them into a single mesh. Is that possible?
Yeah thats doable. You have a transform matrix for both meshes presumably?
Lock both meshes and then take the 1st mesh (I will assume we add it to the second) and transform its vertices one by one by the matrix transformation transfoming from cylinder 1s local space to clyinder 2's local space (ie [cylinder 1 world transform] * [inverse cylinder 2 world transform]). Define up the correct indices and you have now added mesh 1 to Mesh 2.
It will get more compilcated if you want both meshes to intersect properly. If you want to do that I suggest you look into Constructive Solid Geometry (CSG). There are plenty of links to be found on google on the subject.

Categories