nicklockwood / Euclid

A Swift library for creating and manipulating 3D geometry

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assertion fail when applying transform on Mesh

naccib opened this issue · comments

I'm creating a mesh from a SCNNode's geometry. When trying to apply a transform, I get the following fatal error:

Euclid/Plane.swift:195: Assertion failed
2022-11-02 14:21:16.644227-0300 NKAR[77549:28776043] Euclid/Plane.swift:195: Assertion failed

This error is caused by a failed normalization assert on Plane.swift.

Minimally Reproducible Example:

extension SCNNode {
    func withTransform(scale: SCNMatrix4, registration: SCNMatrix4) -> SCNNode {
        guard let mesh = Mesh(self.geometry!) else {
            fatalError("Couldn't create Euclid Mesh from geometry")
        }
        
        let scaleTransform = Euclid.Transform(scale)
        let registrationTransform = Euclid.Transform(registration)
        
        let transformed = mesh
            //.smoothNormals(Angle(degrees: 20.0))
            .transformed(by: registrationTransform)
            .transformed(by: scaleTransform)
        
        let geometry = SCNGeometry(transformed)
        
        return SCNNode(geometry: geometry)
    }
}

I'm loading the file using ModelIO. Mesh file used: https://1drv.ms/u/s!ApWujpauQd0YgpBiWTsh1zxIN-pWsA?e=4VEFP2

@naccib thanks for reporting this. What are the values you are using for scale and registration in your example?

scale is a simple symmetric scaling matrix with a factor of 0.001 (so 0.001 on the $m_{11}$, $m_{22}$ and $m_{33}$). The registration matrix is a little bit more complex because it's different at each runtime, but it only contains a rotation and a translation.

I've found that simply deleting this assertion fixes the issue with no apparent difference from applying those two transforms with SceneKit.

I'm not able to reproduce the assertion with the parameters you've given, so I suspect the specific transform matrix might be relevant. I'd prefer not to delete the assertion until I can understand why it's firing.

That's a reasonable approach. This causes the assertion to fire:

Scale Matrix (scale):

SCNMatrix4(m11: 0.001, m12: 0.0, m13: 0.0, m14: 0.0, m21: 0.0, m22: 0.001, m23: 0.0, m24: 0.0, m31: 0.0, m32: 0.0, m33: 0.001, m34: 0.0, m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0)

Registration matrix (registration):

SCNMatrix4(m11: 0.9978629, m12: -0.06377151, m13: -0.014242808, m14: 23.246614, m21: 0.018626042, m22: 0.068675354, m23: 0.9974652, m24: -149.8618, m31: -0.062631726, m32: -0.99559873, m33: 0.0697164, m34: -112.913246, m41: 0.0, m42: 0.0, m43: 0.0, m44: 1.0)

@naccib this is now fixed in 0.6.1