Our friend thehans published a library called functional.scad, which can be used to find what you are seeking. Indeed your code is an almost perfect use case for it. But don't expect too much, as the lib does not support any boolean operations.
To show what I mean, I translated your code into "functional OpenSCAD". You will have to pick the information you are interested in from the console output.