andreasala98 / NM4PIG

Numerical Methods for Photorealistic Image Generation
MIT License
2 stars 1 forks source link

CSG bug #16

Closed andreasala98 closed 3 years ago

andreasala98 commented 3 years ago

Hi, I was trying to implement this shape and I found a bug in CSGIntersection (it could also be in CSGDifference). The error log is quite long, it says something like

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Trace.CSGIntersection.rayIntersection(Ray ray) in /Users/andreasala/Desktop/cngif/NM4PIG/Trace/CSG.cs:line 296

so it's probably related to the RayIntersectionList method.

We will need to spend some time on this, soon or later!

PietroKlausner commented 3 years ago

@andreasala98, could you show us the build that you used for that complicated shape? Also, did you have problems when you made a CSG shape out of CSG Shapes, or was it already bugged when you tried a CSG with "primitive" Shapes?

andreasala98 commented 3 years ago

Hey @PietroKlausner, below is the code I used to (try to) generate that shape. As you can see from this the cylinder part works fine, because it is made of CSG Unions only. On the other hand, the CSG intersection between the Cube and the Sphere fails when the sphere exceeds the faces of the cube. It works fine when the sphere is fully inside the cube (i.e. the result of the intersection is just the blue sphere)

                    Shape C1 = new Cylinder(Tsf.Scaling(0.5f, 0.5f, 1.5f), cylMat);
                    Shape C2 = new Cylinder(Tsf.RotationY(Utility.DegToRad(45)) * 
                                            Tsf.RotationX(CC.PI / 2f) * 
                                            Tsf.Scaling(0.5f, 0.5f, 1.5f), cylMat);
                    Shape C3 = new Cylinder(Tsf.RotationX(Utility.DegToRad(-45)) * 
                                            Tsf.RotationY(CC.PI / 2f) * 
                                            Tsf.Scaling(0.5f, 0.5f, 1.5f), cylMat);

                    Shape S1 = new Sphere(Tsf.Scaling(1.5f), BrightBlueMat);
                    Shape B1 = new Box(material: BrightRedMat);

                    Shape left = S1 * B1; // intersection
                    Shape right = (C1 + C2) + C3; //union

                    Shape tot = left - right; //difference

                    world.addShape(tot);
PietroKlausner commented 3 years ago

This bug should be' fixed now. Can I close the issue?

andreasala98 commented 3 years ago

Ok for me!