Crash in Mesh.subtract
Closed this issue · 6 comments
Here's what I'm trying to do: Use Euclid in a ARKit app.
Here's what I did:
- Create a new “Augmented Reality” project in Xcode. (12.5.)
- You’ll need to select
SceneKit
in the “Content Technology” dropdown.
- You’ll need to select
- Add the Euclid as a Swift Package Manager dependency.
- Go to:
File > Swift Packages > Add Package Dependency
- Add
https://github.com/nicklockwood/ShapeScript.git
- Go to:
- Open
ViewController.swift
- add
import Euclid
at the top of the file. - Comment out (or delete) the line of code that creates a new scene from
ship.scn
- Added the following lines in its place (these are from the EuclidExample project):
- add
// Create a new scene
let scene = SCNScene()
// create some geometry using Euclid
let start = CFAbsoluteTimeGetCurrent()
let cube = Mesh.cube(size: 0.8, material: UIColor.red)
let sphere = Mesh.sphere(slices: 120, material: UIColor.blue)
let mesh = cube.subtract(sphere)
print("Time:", CFAbsoluteTimeGetCurrent() - start)
print("Polys:", mesh.polygons.count)
// create SCNNode
let geometry = SCNGeometry(mesh)
let node = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(node)
- Provisioning bs...
- Build and run on my device. (iPhone 12 Pro Max)
I get this crash:
Interestingly observations:
- I have had mixed results trying to build and run the project in the simulator. I have had it crashe with the same error as on device, but I have also had it run successfully. I have also at least once seen the following errors in the logs:
2021-06-17 11:53:03.035867-0500 EuclidSKAR[9123:636030] Metal GPU Frame Capture Enabled
2021-06-17 11:53:03.036055-0500 EuclidSKAR[9123:636030] Metal API Validation Enabled
Time: 12.664151072502136
Polys: 3574
2021-06-17 11:53:16.680852-0500 EuclidSKAR[9123:639525] fopen failed for data file: errno = 2 (No such file or directory)
2021-06-17 11:53:16.680957-0500 EuclidSKAR[9123:639525] Errors found! Invalidating cache...
2021-06-17 11:53:16.943133-0500 EuclidSKAR[9123:639525] fopen failed for data file: errno = 2 (No such file or directory)
2021-06-17 11:53:16.943270-0500 EuclidSKAR[9123:639525] Errors found! Invalidating cache...
- The example project does build and run just fine on my phone (after changing provisioning, of course).
- I get the same error when running on device even if I add all the code above in a background thread.
- I originally created this issue on the ShapeScript repo, sorry about that. I wasn't even trying to use ShapeScript.
@mgrider hi, just to let you know I was able to recreate the crash (only on device, not on the simulator). I've no idea yet what's causing it.
@mgrider reducing the number of slices down from 120 to 110 fixes the problem, so I strongly suspect it's a stack overflow.
@mgrider CSG operations are highly recursive, so they can cause the stack to overflow if you have very complex meshes. You can solve this one of two ways, either reduce the mesh complexity, or increase the stack size.
You can increase the stack size by creating a new Thread
and then setting the desired size, as follows:
// Create a new scene
let scene = SCNScene()
let thread = Thread {
// create some geometry using Euclid
let start = CFAbsoluteTimeGetCurrent()
let cube = Mesh.cube(size: 0.8, material: UIColor.red)
let sphere = Mesh.sphere(slices: 120, material: UIColor.blue)
let mesh = cube.subtract(sphere)
print("Time:", CFAbsoluteTimeGetCurrent() - start)
print("Polys:", mesh.polygons.count)
// create SCNNode
DispatchQueue.main.async {
let geometry = SCNGeometry(mesh)
let node = SCNNode(geometry: geometry)
scene.rootNode.addChildNode(node)
}
}
thread.stackSize = 4 * 1024 * 1024 * 1024 // set stack to 4GB
thread.start()
@mgrider I've pushed a new version of Euclid (0.4.5) that changes the CSG implementation so these stack overflows won't occur. That means you'll no longer need to increase the thread stack size.
That's awesome, thank you so much! This project is fantastic, btw. I don't really think I'm going to have time to build it, but my idea was an AR enabled 3D modeling program so you can position the geometry over the thing you are trying to model. Just adding and subtracting primitives would be pretty huge, I think. Another cool use case would be to clean up meshes created by 3D scanning with Lidar enabled devices.
@mgrider sounds like a cool idea, hopefully you get a chance to work on it 👍