Notifications
Clear all

[Closed] Convex Triangulation

Heyya everyone, I have recently begun to learn Maxscript and since I found heaps of useful info here, I figured it could not hurt to make a post about m problem here.

I’m working on a csg brush exporter to export simple geometry from 3ds max to q3map .map format, the brush format only allows for conve shapes so I have to triangulate all non-planar N-Gons in a way that only allows convex shapes. With some helpful functions from this thread I managed to write a simple function that sets the diagonals on quads to only allow convex shapes. My problem lies in extending this function to work on N-sided faces as well. This is my current script:

Spoiler
fn pointPlaneDist pA pB pC pD = (
    
    local nABC=normalize (cross (pB-pA) (pC-pA))
    length ((dot (pA-pD) nABC)*nABC)
)

fn PlaneNormal pA pB pC= ( normalize (cross (pB-pA) (pC-pA)))

fn getVectorsAngle n1 n2 = ( acos (dot (normalize n1) (normalize n2)) )

fn getVector A B = return( B-A )    
    
fn solvePointInPlane P N = ( P.x*N.x + P.y * N.y + P.z * N.z) as float

fn ForceConvexity obj =(    
    
    for f=1 to polyop.getNumFaces obj do (
        
        FaceVerts = polyop.getFaceVerts obj f
        format "
Face % Verts = %
" f FaceVerts
        
        for v=0 to mod FaceVerts.count 4 do (
            
            PA =  polyop.getVert obj FaceVerts[1+v]
            PB =  polyop.getVert obj FaceVerts[2+v]
            PC =  polyop.getVert obj FaceVerts[3+v]
            PD =  polyop.getVert obj FaceVerts[4+v]
            
            Dist = pointPlaneDist PA PB PC PD
            
            Norm = PlaneNormal PA PB PC
            
            NewPoint = PD + Norm * Dist
            
            PlaneConst = (solvePointInPlane PA Norm)
            
            NewPointPlane = (solvePointInPlane NewPoint Norm)
            
            format "PlaneConst == %  NewPointPlane == %
" PlaneConst NewPointPlane
            
            if (PlaneConst * 10^4) as integer == (NewPointPlane * 10^4) as integer then (
                    polyop.setDiagonal obj f (1+v) (3+v)
                    format "Set Diagonal to 1-3
"
                ) 
                else (
                    polyop.setDiagonal obj f (2+v) (4+v)
                    format "Set Diagonal to 2-4
"
                )
            
        )
    )
)

ForceConvexity $

I’d be grateful for any help with extending this to support N-Gons