How can I near clip vertices in homogenous clip space (-w < xyz < w)? - graphics

I currently have a system in place to near clip vertices, but it only seems to work in view space.
The system is: Given a triangle, the near plane and the near plane normal, I check how many points are behind the clipping plane
If its 1: I create 2 new vertices at the intersections of the near plane, and reform triangles
If its 2: I create 1 new vertice at the intersection of the near plane, and reform triangle
But the problem is, when I try to use this system in homogenous clip space it doesen't work. I can't find the intersections, I don't know what the near plane is in homogenous clip space and I don't even know how to tackle the problem of all the vertices having different w values.
So what algorithm would I use to actually near clip triangles in homogenous clip space?
So far I've tried looking at many many articles to find this answer, but I find they don't give me answers that I am able to understand, instead just spouting loads and loads of math formulas.
Honestly I think this whole problem just stems from my lack of knowledge on 3D maths.

Related

How to clip a line using circle and triangle clipping windows?

While using Cohen-Sutherland line clipping algorithm, the clipping window is a rectangle. Is it possible to clip a line using a triangular or circular windows using a similar technique?
The idea can be reused for clipping against a triangle, with seven regions instead of nine. You can easily see what pair of origin/destination regions result in no visibility or full visibility. For the remaining cases deeper analysis is required.
For circles the coding is of less use because two "outside" codes are not enough to decide. But clipping a segment against a circle is simple: write the parametric equation of the segment and find the values of the parameter giving a point inside the circle (this amounts to the resolution of a quadratic equation).

Polygon triangulation with collinear points?

I have a polygon with collinear points. I want to triangulate the polygon while retaining all the collinear points, since I require those vertices on the generated meshes. Currently I tried to use poly2tri, but it doesn't support collinear points. Is there a polygon triangulation algorithm which support collinear points?
Try moving the points slightly, so they are not colinear. Doing the meshing and then moving the points back.
I tried running the meshing algorithm, then perturbing all the points it missed, then running it again.
It can be quite slow but it does work.
A simple polygon with collinear vertices presents a serious problem for the "ear splitting" triangulation. The best example to see this problem - sketch yourself a 5-point star. Pick any of the points as the convex vertex for a candidate ear.
The diagonal for this ear lies completely within the polygon, and doesn't form "X" intersections with any of the other edges, so by the criteria for a "valid diagonal", it should work. But, notice that once you slice off the ear, the remaining polygon is no longer a simple polygon - because it contains two vertices where the edges meet at a straight (180 deg.) angle. That alone disqualifies the new sub-polygon as "simple", and it will crash any attempt to continue triangulation on it using ear-splitting. I will post a separate question on how best to triangulate these ill-conditioned polys.

Projecting a line segment onto a polygon mesh

I am working on a 3d application and am currently looking for a way to project a line segment defined by two points in screen-space onto a three-dimensional polygonal mesh (in my case a triangle mesh). The goal is to find the intersection points in world-space of the line segment with the edges of the mesh.
I can only think of two ways to do this, but neither is ideal. The first is to sample the line segment (in screen-space) at small intervals and ray trace at those intervals to find the world-space coordinates where the ray hits the mesh, but this does not easily give me the intersection points of the line segment with the mesh edges.
The other way I can think of is to somehow back-project the mesh into screen-space, find the intersections there (in 2d) and then project those intersection points back to 3d. The problem with this is that the screen-space coordinate system may change between the selection of the first and second endpoints of the line segment (due to moving the camera).
If any of that was confusing, then here is an image that approximately shows what I'm trying to do (the white dots indicate the points that I want to find). However, in my case the yellow curve is simply a line segment.
[Yunjin Lee, et al. "Mesh scissoring with minima rule and part salience." 2005]
Any help is very much appreciated.
Here's my suggestion:
Project the screen line into world space (getting a plane in world space).
Intersect the plane with the triangles in the mesh, getting a set of edges.
Add the edges to a data structure that keeps only the parts of the edges that are closest to the camera plane (see the diagram below, in which the red line segments and their endpoints are the ones we want to keep). This is like building up an image via a Z-buffer, except that because we know that this set is piecewise linear, we don't have to rasterize it, we can just maintain a sorted list of endpoints.

Sphere and nonuniform object intersection

I have two objects: A sphere and an object. Its an object that I created using surface reconstruction - so we do not know the equation of the object. I want to know the intersecting points on the sphere when the object and the sphere intersect. If we had a sphere and a cylinder, we could solve for the equation and figure out the area and all that but the problem here is that the object is not uniform.
Is there a way to find out the intersecting points or area on the sphere?
I'd start by finding the intersection of triangles with the sphere. First find the intersection of each triangle's plane and the sphere, which gives a circle. Then find the circle's intersection/s with the triangle edges in 2D using line/circle tests. The result will be many arcs which I guess you could approximate with lines. I'm not really sure where to go from here without knowing the end goal.
If it's surface area you're after, maybe a numerical approach would be better. I'd cover the sphere in points and count the number inside the non-uniform object. To find if a point is inside, maybe trace outwards and count the intersections with the surface (if it's odd, the point is inside). You could use the stencil buffer for this if you wanted (similar to stencil shadows).
If you want the volume of intersection a quick google search gives "carve", a mesh based CSG library.
Starting with triangles versus the sphere will give you the points of intersection.
You can take the arcs of intersection with each surface and combine them to make fences around the sphere. Ideally your reconstructed object will be in winged-edge format so you could just step from one fence segment to the next, but with reconstructed surfaces I guess you might need to apply some slightly fuzzy logic.
You can determine which side of each fence is inside the reconstructed object and which side is out by factoring in the surface normals along the fence.
You can then cut the sphere along the fences and add the internal bits to the display.
For the other side of things you could remove any triangle completely inside the sphere and cut those that intersect.

Minimize Polygon Vertices

What is a good algorithm for reducing the number of vertices in a polygon without changing the way it looks very much?
Input: A polygon, represented as a list of points, with way too many verticies: raw input from the mouse, for example.
Output: A polygon with much fewer verticies that still looks a lot like the original: something usable for collision detection, for example (not necessarily convex).
Edit: The solution to this would be similar to finding a multi-segmented line of best fit on a graph. It's called Segmented Least Squares in my algorithms book.
Edit2: The Douglas Peucker Algorithm is what I really want.
Edit: Oh look, Simplifying Polygons
You mentioned collision detection. You could go really simple and calculate a bounding convex hull around it.
If you cared about the concave areas, you can calculate a concave hull by taking the centroid of your polygon, and choosing a point to start. From the starting point rotate around the centroid, finding each vertex you want to keep, and assigning that as the next vertex in the bounding hull. The complexity of the algorithm would come in how you determined which vertices to keep, but I'm sure you thought of that already. You can throw all your vertices into buckets based on their location relative to the centroid. When a bucket gets more than an arbitrary number of vertices full, you can split it. Then take the mean of the vertices in that bucket as the vertex to use in your bounding hull. Or, forget the buckets, and when you're moving around the centroid, only choose a point if its more than a given distance from the last point.
Actually, you could probably just use all the vertices in your polygon as "cloud of points" and calculate the concave hull around that. I'll look for an algorithm link. Worst case on this would be a completely convex polygon.
Another alternative is to start with a bounding rectangle. For each vertex on the rectangle, find the distance from the point to the polygon. For the farthest vertex, split it into two more vertices and move them in some. Repeat until some proportion of either vertices or area is met. I'd have to think about the details of this one a little more.
If you care about the polygon actually looking similar, even in the case of a self-intersecting polygon, then another approach would be required, but it doesn't sound like thats necessary since you asked about collision detection.
This post has some details about the convex hull part.
There's a lot of material out there. Just google for things like "mesh reduction", "mesh simplification", "mesh optimization", etc.

Resources