Hey everyone,
I just wanted to give a bit context to @m_adam great answer, as I will probably otherwise will forget when I am back.
To know if a face is inwards or outwards you can cast a ray with GeRayCollider having origin in that vertex and direction of vertex normal. Find how many times ray intersects the current polygon object. If it intersects it even number of times ( including 0 ) - normal is facing outwards.
The algorithm Maxime is referring to here to detect if one is in- or outside of a geometry volume is called crossing-number or Point in Polygon. It only works for manifold geometry. But more importantly, you should be aware that GeRayCollider.GetIntersectionCount
is not necessarily equal to the number of unique CPolygon
instances you have hit due to double hits of the triangles within a quadrangle.
To determine how many polygons you have hit, you must iterate over all intersections with GeRayCollider.GetIntersection
and count the unique 'face_id'
you have hit. 'backface'
in this context will also tell you from which direction you are hitting the polygon. I would also be careful with casting from the mean of all vertices of a polygon or one of its vertices. When you lie directly in the plane the polygon is lying in, you can run into false-positive or false-negative intersection tests due to floating-point precision. It is better to move a normal length before or behind the polygon to be sure to always hit it or not.
Last but not least, I would consider important to point out that the normal of a polygon is stored implicitly by its point order.
Flipping the normal of the polygon with the vertices (0, 1, 2, 3)
means simply constructing a polygon with the vertices (3, 2, 1, 0)
. For manifold meshes (holes are allowed in this case), this also means that when you have two polygons P
and Q
which both share an edge E
which has the vertices 0
and 1
, that P and Q have flipped orientation when their edge E indexes its points in the same order. For the normals of P and Q to be aligned, P must express its edge E as (0, 1)
and Q must express E as (1, 0)
(or vice versa).
This makes it a very cheap operation to detect non-aligned nomals as you only have to look at the point order of polygons and check if you can find an edge in the exact same point order again. But unless you must handle flipping polygons for some reason by yourself, you should use SendModelingCommand
as lined out by @m_adam.
Cheers,
Ferdinand