ThreeCSG icon indicating copy to clipboard operation
ThreeCSG copied to clipboard

Call Stack problems

Open Yenza opened this issue 9 years ago • 10 comments

Using three.min.js rev 73

Method used: var roomBSP = new ThreeBSP(_RoomShape); // HUGE SHAPE

Info gathered from a TryCatch: "number : -2146828260" "Error: Out of stack space at ThreeBSP.Vertex.prototype.dot (/ThreeCSG.js) at ThreeBSP.Polygon.prototype.classifyVertex (/ThreeCSG.js) at ThreeBSP.Polygon.prototype.classifySide (/ThreeCSG.js) at ThreeBSP.Polygon.prototype.splitPolygon (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js) at ThreeBSP.Node (/ThreeCSG.js)"

Using the OUTDATED csg-wrapper from kraag22/csg-wrapper it doesn't happen but his wrapper is way outdated and needs an update.

Might this occur when using high values/numbers?

Another example: var intersect = shape1.intersect(shape2); // Small shapes

Info gathered from a TryCatch: "number : -2146828260" -:-

Yenza avatar Jan 25 '16 12:01 Yenza

I have an updated fork here

Wilt avatar Jan 27 '16 12:01 Wilt

Your fork is exactly the same except missing the face.vertexNormal fix

Yenza avatar Jan 27 '16 12:01 Yenza

Owh, sorry. I thought that there were more fixes in my fork...

Wilt avatar Jan 27 '16 13:01 Wilt

I provided the vertex normal fix... But I accidently pointed to the master, and that fix was in this patch1 branch.

Wilt avatar Jan 27 '16 13:01 Wilt

Your branch is fully up-to-date with Chandlers and you both provide exact same ThreeCSG.js files. I've just compared with a SVN program. No changes.

Yenza avatar Jan 27 '16 13:01 Yenza

@Yenza Yes, you are right. He recently merged my patch1...

Wilt avatar Jan 27 '16 14:01 Wilt

Maximum call stack happens with complex geometry for two reasons:

  1. ThreeCSG doesn't use any heuristic for picking a good divider
  2. Even if a great divider is selected it is still possible to reach maximum recursion. The proper fix would be to change the recursion into one function that keeps a local stack queue and while() loops over it.

Solving either of these would gain large speed improvements, especially for complex geometries. There are many white papers / thesis on CSG which provide ideas for selecting a good divider. Unfortunately I won't have time for a while to actively look into implementing.

chandlerprall avatar Jan 27 '16 16:01 chandlerprall

@Wilt Do you think you could try to fix this issue?

@chandlerprall But it isn't that very complex! It's two round shapes! It's two identical "almost" circle-wise shapes on the same height. The thing is that if I move any of them like 1 unit in any direction.. then it works! I have an image of the red area when using the intersect method that causes an error: http://i.imgur.com/NW0AOUU.png

Also this problem doesn't seem to be occurring when you use the old csg wrapper but that might just be because it's heavily outdated.

Yenza avatar Jan 28 '16 07:01 Yenza

So this is what I did to fix this for now... I dunno why it works but it does. The "var test = new ThreeBSP(shape)" doesn't though x.x

var intersection;
try {
    // If it doesn't work the first time...
    intersection= shapeBSP.intersect(shapeBSP_2);
}
catch (e) {
    // Then try again..
    var newShape = shapeBSP_2.toMesh(_Material);
    newShape.position.x += 1;
    newShape = new ThreeBSP(newShape );
    intersection= shapeBSP.intersect(newShape);
    // console.warn("Works!");
}

Yenza avatar Jan 28 '16 10:01 Yenza

FYI this new CSG repo using Octree data structure overcomes a lot of the performance issues with ThreeCSG and other CSG tools. See this SO thread for up to date information.

jgphilpott avatar Sep 01 '23 07:09 jgphilpott