Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Register
    • Register
    • Login

    How to detectflip normals of object inwardoutward?

    Cinema 4D SDK
    r23 python
    3
    4
    970
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • FSSF
      FSS
      last edited by

      Hi everyone, hi ferdinand,

      simple question, is there a way to unify a objects normals (inward facing, outward facing) in code and flip them?

      I know about the OLathe FlipNormals Boolean, but found that these is not a reliable indictor for inwards or outwards facing normals.

      Is there a way to get the outwardness and inwarndness of normals (points majority towards pivot or away from pivot)..

      And is there a modifier (sorry for the 3ds max term) that flips all normals for editables.

      Merry XMas
      FSS

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by m_adam

        Hi @FSS there is no such built-in function to detect this things, however there is the command MCOMMAND_ALIGNNORMALS that can be used to align the normals.

        settings = c4d.BaseContainer()  # Settings
        settings[c4d.MDATA_ALIGNNORMALS_USERNORMALS] =  True  # Align user normals.
        
        res = c4d.utils.SendModelingCommand(command=c4d.MCOMMAND_ALIGNNORMALS,
                                        list=[op],
                                        mode=c4d.MODELINGCOMMANDMODE_ALL,
                                        bc=settings,
                                        doc=doc)
        c4d.EventAdd()
        

        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.

        Side note, we will be away the 26th and 27th December, so don't be surprise by the delay if you have follow-up questions, for more information see No support on 26/12 and 27/12.

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        ferdinandF 1 Reply Last reply Reply Quote 0
        • ferdinandF
          ferdinand @m_adam
          last edited by ferdinand

          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

          MAXON SDK Specialist
          developers.maxon.net

          1 Reply Last reply Reply Quote 1
          • M
            m_adam
            last edited by

            Hello @FSS,

            without further questions or postings, we will consider this topic as solved by Friday 02/06/2023 and flag it accordingly.

            Thank you for your understanding,
            Maxime.

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            1 Reply Last reply Reply Quote 0
            • First post
              Last post