<p>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 <em>or</em> is empty if there is a miss.</p>

<p>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. </p>

<p>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].</p>

<p>Does that help?</p>

CC BY-SA 3.0
2016-05-01T19:19:19.040Z
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
CC BY-SA 3.0
2016-05-03T10:50:38.390Z
(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
CC BY-SA 3.0
2016-04-29T13:54:57.470Z
2016-04-29T13:54:57.470Z
Simon F
<p>What I'm trying to do is to simulate refraction through biconcave lens described by spheres <code>A</code>, <code>B</code>, <code>C</code> where <code>C</code> is a sphere in between <code>A</code> and <code>B</code>. 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.</p>

CC BY-SA 3.0
2016-04-29T12:11:46.873Z
Could you tell also where you found the "mentions of using Interval Arithmetics"?
rych
CC BY-SA 3.0
2016-04-29T16:55:01.940Z
@rych [In this paper.](http://www.cs.utah.edu/~knolla/dagstuhlHijazi.pdf)
niksbenik
<raytracing>
How to implement Constructive Solid Geometry in ray tracing with implicit surfaces (spheres)?
niksbenik
# How to implement Constructive Solid Geometry in ray tracing with implicit surfaces (spheres)?

(Asked by: username_0 on 2016-04-28)

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.

**Tags:** raytracing

> **Comments:**
> **username_1**: Could you tell also where you found the "mentions of using Interval Arithmetics"?  
> **username_0**: @username_1 [In this paper.](http://www.cs.utah.edu/~knolla/dagstuhlHijazi.pdf)  

---

## 1 answers

### Answer by username_2 on 2016-04-29. Score: 6 | Accepted answer

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?

> **Comments:**
> **username_0**: 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?  
> **username_2**: (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] }.  

---

