Generate the faces of a 3D object given its points and edges - c++

I have a 3D wire-frame consisting of 3D points and edges. How do I go about identifying sets of vertices forming a face of the 3D object?
I am using QT and want to render a 3D object given its point set and edge set. The best I have been able to do is given three points forming a triangular face, I am able to render it in 3D. But how to do the same given more points and edges? Or, alternatively, how to break down the set in sets of 3 points forming a triangular face?

Just take your first edge and its vertices (V1 and V2).
Find all edges that use V2 - their second vertices are your potential V3's.
For each potential V3 check if you have edge V1-V3 - if so, then you've found a triangle V1-V2-V3. For most meshes you should have one or two such triangles. When you're adding a new triangle always check if wasn't found already.
Do the same for edges that use V1.
Take next edge and repeat.
Depending on your exact data, edge directions, etc. it may need slight modifications but you should get the point.

Related

Test if a 3D Point is with an extruded "2.5D" Polygon

I need to check if a point given as x,y,z coordinates is within an extruded 2.5D Polygon. I thought it should be possible to achieve using a 2D point-in-polygon check (e.g. [1]) with additional distance checks.
I'd like to avoid creating meshes for the polygon for performance reasons. Is it possible?
[1] How can I determine whether a 2D Point is within a Polygon?

Create Topographic 2D Curves from Polygonal Mesh

I'm trying to convert a polygonal 3D mesh into a series of topographic curves that represent the part of the mesh at a specific height for every interval. So far, I've come up with the idea to intersect a horizontal plane with the mesh and get the intersection curve(s). So for this mesh:
I'd intersect a plane repeatedly at a set interval of precision:
and etc.
While this is straightforward to do visually and in a CAD application, I'm completely lost doing this programmatically. How could I achieve calculating this in a programming environment/ what algorithms can I look into to achieve this?
I'm programming in an STL C++ environment (with Boost), loading .obj meshes with this simple loader, and need simple cartesian 2D points to define the output curve.
An option is to process all the faces in turn and for every face determine the horizontal planes that traverses them. For a given plane and face, check all four vertexes in turn and find the changes of sign (of Zvertex - Zplane). There will be exactly two such changes, defining an edge that belongs to a level curve. (Exceptionally you can find four changes of sign, which occurs when the facet isn't planar - join the points in pairs.)
Every time you find an intersection point, you tag it with the (unique) index of the plane and the (unique) index of the edge that was intersected; you also tag it with the index of the other edge that was intersected in that face.
By sorting on the plane index, you can group the intersections per plane.
For a given plane, using a hash table, you can follow the chain of intersections, from edge to edge.
This gives you the desired set of curves.

Normal of point via its location on STL mesh model

Can someone tell me the best way to estimate the normal at a point on CAD STL geometry?
This is not exactly a question on code, but rather about efficiency and approach.
I have used an approach in which I compare the point whose normal needs to be estimated with all the triangles in the mesh and check to see if it lies inside the triangle using the barycentric coordinates test. (If the value of each barycentric coordinate lies between 0 and 1, the point lies inside.) This post explains it
https://math.stackexchange.com/questions/4322/check-whether-a-point-is-within-a-3d-triangle
Then I compute the normal of that triangle to get the point normal.
The problem with my approach is that, if I have some 1000 points, and if the mesh has say, 500 triangles, that would mean doing some 500X1000 checks. This takes a lot of time.
Is there an efficient data structure or approach I could use, to pinpoint the right triangle? Or a library that could get the work done?
A relatively easy solution is by using a grid: decompose the space in a 3D array of voxels, and for every voxel keep a list of the triangles that interfere with it.
By interfere, I mean that there is a nonempty intersection between the voxel and the bounding box of the triangle. (When you know the bounding box, it is straight forward to tell what voxels it covers.)
When you want to test a point, find the voxel it belongs to and compare to the list of triangles. You will achieve a speedup equal to N/M, where M is the average number of triangles per voxel.
The voxel size should be chosen carefully. Too small will result in a too big data structure; too large will make the method ineffective. If possible, adjust to "a few" triangles per voxel. (Use the average triangle size - square root of double area - as a starting value.)
For better efficiency, you can compute the exact intersections between the triangles and the voxels, using a 3D polygon clipping algorithm (rather than a mere bounding box test), but this is more complex to implement.

Determine if a point is interior or exterior to a 3D Alpha-shapes surface in CGAL

I am using CGAL to create the concave hull of a set of 3D points using ex_alpha_shapes_3 example. Next, I would like to find out whether a point query in space is located within the surface created by the triangular concave hull faces (the output of ex_alpha_shapes_3 code) or not. A "point in polygon" technique should be useful for this purpose. I would appreciate it if anyone could help me with this problem.
You can use the locate function and depending on the simplex the point fall on and the output of the function classify of the simplex you'll directly know if you're inside, outside or on the boundary.
whatever simplex type, EXTERIOR is exterior, INTERIOR is interior.
If the point falls on an edge, REGULAR is on the boundary and SINGULAR depends whether what an isolated edge should be in your setting
If the points fall on a vertex, REGULAR is on the boundary and SINGULAR is depends whether an isolated input point should be in your setting
I don't know about CGAL, but there are a few heuristics you can use given that your polyhedron is known to be convex. You can do a lot of the work in essentially 2D. You can use any axes, but let's assume we are working in the XY plane and momentarily ignoring the Z component. Since your shape is convex, there will generally only be two triangles whose XY coordinates surround the XY coordinates of your point. For any triangle, you can quickly determine if x[min] < x[point] < x[max] and likewise for y. If those tests fail, move on. If they succeed, a little further testing is needed to determine whether the point is actually inside the triangle. Once you have located 2 acceptable triangles, find the Z value for each triangle at the point's (X,Y). If the point's Z is between the Z values for the faces, then the point is inside the solid.

How to construct a voronoi diagram inside a polygon?

I need an algorithm that fills a 2D non-convex polygon that may have holes with points randomly, and then constructs a voronoi diagram on them. The diagram should be bounded with the polygon and the algorithm should run in O(n log n).
My idea was to fill the poly by testing random points inside the polys bounding box and taking only the points inside the poly, and than building voronoi on them, and than clipping the edges of the diagram that exit the polygon.
The problem is, testing random points and clipping the edges is O(n^2).
Can this be done in boost, or is there another small library, or anything else really?
I guess with "holes" you man self-intersections of a single, closed polygon.
Do a Delaunay triangulation of your polygon first:
Calculate section points between segments; add these points, split the segments and rearrange the input so that "inside" is always on the same side of the edge when traversing the polygon's points.
Trangulate all points in your polygon.
Delete the triangles that lie outside your polygon. These will be the concavities and holes created by self-intersections. You can identify them by walking along your polygon and deleting all triangles that lie outside an edge. You need the connectivity of the edges, but that's a byproduct of the triangulation.
You now have the starting point for further triangulation with the Bowyer-Watson algorithm, which triangulates by successively adding points to a parent triangle. So, to add a random point, we can pick a point and update the triangulation in one go:
Pick a random triangle, where the probability for each triangle to be picked is proportional to its area.
Chose a random location inside that riangle by picking barycentric coordinates s in [0, 1], t in[0, 1]and withs + t < 1`. Your new point is then:
{P} = s * ({N2} - {N1}) + t * ({N3} - {N1})
Add your point and retriangulate the parent triangle and other triangles whose circumcircle contains the new point.
The set of triangles to pick has now changed.
You now have a Delaunay triangulation, but you want a Voronoi diagram, which you can easily obtain by connecting the centres of all circumcircles of adjacent triangles. Again, the Delaunay triangulation provides you with the information on the circumcircles and on which triangles are adjacent.
You can use the Bowyer-Watson algorithm on your initial triangulation when you create a large dummy triangle that encloses all your points.
I'm not aware of any triangulation libraries for C++, but this question might get you started.