# How to implement Constructive Solid Geometry in ray tracing with implicit surfaces (spheres)?

What I'm trying to do is to simulate refraction through biconcave lens described by spheres A, B, C where C is a sphere in between A and B. So far I've gathered that a good approach would be to use CSG when it comes to modeling such object. I'm failing to understand how to implement this though. There are mentions of using Interval Arithmetics but I don't understand how exactly are they supposed to help me.

• Could you tell also where you found the "mentions of using Interval Arithmetics"? – rych Apr 29 '16 at 12:11
• @rych In this paper. – niksbenik Apr 29 '16 at 16:55

I'm not sure what exactly you are asking but, IIRC, to do CSG with ray tracing, you maintain not just the closest intersection with an object, but a list of ordered pairs of [Inpoint, OutPoint] 'interval' distances. With a single sphere test (or any solid convex primitive for that matter), this list would have at most one [Inpoint, OutPoint] entry or is empty if there is a miss.

To implement CSG you then apply the binary operator to a pair of lists, which in turn means applying them to the entries in the lists in a manner akin to merge-sorting two lists.

For example, for intersection, if you had [a,b] in one list and [c,d] in the other, compute e=Max(a,c) f=Min(b,d). If e > f there's no intersection else return the intersection, [e,f].

Does that help?

• Thank you for the answer, also sorry for being so vague in my question. The goal is to create diverging lens using the CSG difference operator \ in the following manner: (C \ A) \ B. You've made it much more clearer to me on the general approach, but I'm still struggling to implement \. Can you please give an example for this operator as well? – niksbenik May 1 '16 at 19:19
• (Hoping I've got this right) Would it be easier for if you constructed the difference operator from Intersection and Not i.e. A \ B = A ^ ~B. To generate ~B you just need the "gaps". For example, if the intersection along a ray for B is { [b1, b2] }, then for ~B it'd be { [-infinity, b1] , [b2, +inf] }. – Simon F May 3 '16 at 10:50