Final
Final
Final
Solid Geometry
by
Otmane Sabir
Date, Signature
Acknowledgements
The journey towards this thesis has been circuitous. Its completion is thanks to the special
people who challenged, supported, directed, and drove me along the way.
I am tremendously fortunate for my supervisor, Professor Dr. Sergey Kosov, whose ex-
pertise was invaluable in the progress of this research. Your theoretical and practical
experience remains a central part in understanding the research, architecting the imple-
mentation, and analyzing the results. Your stellar feedback pushed me to sharpen my
thinking and opened up different perspectives concerning several matters. I am grateful
for the inspiration you have been able to instill in all steps of this study.
Additionally, I am deeply indebted to my parents, my sister, and my grandmother for
their wise guidance, thoughtful words, and tender care. You are always there for me.
Finally, I could not have completed this dissertation without the support of my friends,
who continuously taught me how to embrace change and strive for the best.
iii
Abstract
This thesis report presents constructive solid geometry using ray tracing as a way of cre-
ating complex geometries for solid modeling. Solid objects are modeled using different
implicit and explicit geometries (i.e., spheres, tori, boxes) with boolean set operators. By
virtue of its simplicity, ray tracing constructive solid geometry is reliable and expandable.
The most challenging issue is finding the visible geometry intersections in the fastest and
most efficient way. So issues of adequacy and efficiency are addressed here, and I pro-
pose solutions providing significantly faster rendering of CSG. I validate the performance
by comparing various implementations of the algorithm. The paper has two major parts.
In the first, I present the generic method of describing constructively generated geometry.
The second is devoted to a study of acceleration of ray tracing constructive solid geome-
try. I expose algorithms that are both comprehensive and efficient, and the results of the
performance are shared.
iv
Contents
1 Introduction 1
1.1 Rendering Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Rasterization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 Ray Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Geometric Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 Boundary Representation . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 Constructive Solid Geometry . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Related Work 4
4 Optimization 15
4.1 Minimal hit CSG classification . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1.1 Union Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1.2 Intersection Classification . . . . . . . . . . . . . . . . . . . . . . . . 16
4.1.3 Difference Classification . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2 Bounding Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.3 Binary Space Partitioning Trees . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3.1 Building BSP trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3.2 Traversing BSP trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.4 Optimized CSG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
v
1 Introduction
Constructive Solid Geometry (CSG) is a method used in computer graphics, computer-
aided design, generic modeling languages, and numerous other applications to con-
struct complex geometries from simple primitives or polyhedral solids through the use
of boolean operators, namely union (∪), intersection (∩), and difference (−). Figure 1
respectively shows union, intersection, and difference operations. The approach grows
especially appealing when implemented in a ray tracing system as the core intricacy ren-
ders performing arithmetic logic on a pair of uni-dimensional rays. Nonetheless, most
current ray tracing systems generally suffer from the detriment of the expensive object
space intersection computation, and the generic CSG algorithms suffer immensely from
their computational complexity, making it very difficult to integrate into operating render-
ing engines. Therefore, this research concentrates on constructive solid geometry and
possible means of acceleration.
1.1.1 Rasterization
Rasterization has very quickly become the predominant approach for interactive applica-
tions because of its initially low computational requirements, its massive adoption in most
hardware solutions, and later by the ever-increasing performance of dedicated graphics
hardware. The use of local, per-triangle computation makes it well suited for a feed-
forward pipeline. However, the rasterization algorithm has many trade-offs. To name a
1
few: handling of global effects such as reflections and realistic shading, and limitations to
scenes with meshed geometries [22].
Ray tracing simulates the photographic process in reverse. For each pixel on the screen,
a ray is shot and objects that intersect the ray are identified. A ray-tracing algorithm
utilizes four essential components: the camera, the geometry, the light sources, and the
shaders. These components can have different varieties, to state a few, orthographic and
perspective cameras, unidirectional and area light sources, and Phong and Blinn shaders.
Hence, it allows achieving several outcomes depending on the necessities. The main
downside has been computational time and the constraints of using such an algorithm in
interactive applications. However, ray tracing parallelizes efficiently and trivially. Thus it
takes advantage of the continuously rising computational power of the hardware. Many
applications have successfully produced real-time ray tracing algorithms and allow for
highly photorealistic results in interactive applications [1, 2].
Boundary representations are indirect definitions of solids in space using their boundary
or limit. This representation is usually a hierarchical composition of different dimensionally
complex parts. On the very top, we have definitions of two-dimensional faces, which
build on uni-dimensional edges that are subsequently built on dimensionless vertices
(Figure 2). A BREP with non-curvilinear edges and planar faces is called a polygon mesh.
A triangle is the simplest polygon and has the excellent property of always being co-
planar. Polygons of any complexity are representable by a set of triangles. These qualities
make triangular meshes a fundamental component in BREPs. The representations built
on triangles are also highly optimized for fast operations. Therefore, I will mainly deal with
triangular meshes, though similar logic applies to descriptions for tetragon (quadrilateral)
meshes.
Constructive solid geometry takes basis on the fundamental premise that any complex
physical object is obtainable from primitive geometries and boolean operations. CSG is
radically different from BREPs as it does not collect any topological information but in-
stead evaluates the geometries as needed by the case scenario. In other words, there is
no explicit description of the boundary of the solid. Contrary to BREPs, CSG represen-
tations are quickly modified and manipulated since incremental changes do not trigger
2
Figure 2: Sample BREP of a 3D hyper-rectangle [30]
1
Polygon meshes are usually not considered in CSG algorithms; however, the implementation discussed
here allows such flexibility.
3
1.3 Overview
I present this CSG implementation in six sections. 1. Introduction; 2. Related Works; 3.
Constructive Solid Geometry; 4. Optimization; 5. Evaluation of the results; 6. Conclu-
sions & Future Works.
The first section was the previous introduction laying a foundation for a few topics I will be
addressing.
The second section presents works already done, the limitations of the proposed imple-
mentations, and solutions to problems related to CSG.
Section 3 defines the algorithm that performs the logic in the ray-tracing framework. I
first introduce the ideas behind ray intersection. I then lay a mathematical foundation for
boolean algebra and membership classification. Additionally, I dive into the detail of ray
classification for constructively generated geometries.
Section 4 discusses efficiency and optimization. The visible surface problem in ray tracing
requires a lot of CPU time, and without any optimization, the CSG algorithm significantly
increases the payload. Therefore, improvement is much needed to make this method
usable and suitable for real-life applications. The speed is a function of the screen reso-
lution and the geometry complexity (the number of primitives (e.g., triangles) in the solid,
and the number of nested geometries).
Section 5 describes the different implementations of the CSG algorithm with the various
optimization techniques. The first is the naive implementation which I refer to as N aiCSG.
The second uses a binary space partitioning tree to solve the visible surface problem but
still naively finds intersections inside the combinatorial geometry, which I will refer to as
BinCSG. Lastly, I will introduce our optimized algorithm, which uses a binary space
partition tree on the outside (solving the visible surface problem) and also inside each
composite geometry to direct the rays towards the correct geometries, which I will refer to
as OptimCSG. I conduct three types of tests. The primary one is a function of time and
complexity of the geometry, as I monitor the rendering time following gradual increases
in the detail level of two sphere meshes. The second computes the time taken to render
a scene after covering different amounts of the viewport. Additionally, I conduct a test to
check the number of ray tests conducted per pixel per variant. The final test computes the
time variations after increasing the number of nested geometries present in the composite
solid while crucially maintaining a consistent viewport fill rate.
2 Related Work
I discuss below the techniques most related to ours. However, there is a tremendous body
of work in this area, and I cannot possibly provide a full overview. The goal is instead to
outline similarities and differences with some of the widely adopted approaches for CSG
modeling.
Constructive solid geometry has been a subject of study since the late 1970s. It was
initially introduced in [31] as a digital solution to help in the design and production activities
in the discrete goods industry, this marked the basis for formalizing the method.
A rigorous mathematical foundation of constructive solid geometry was later laid out
in [19]. The membership classification function, a generalization of the ray clipping
4
method, is also thoroughly discussed, and various formal properties are introduced.
A few years later, it was revisited in [21] where Roth et al. (1982) introduced ray casting
as a basis for CAD solid modeling systems. Challenges of adequacy and efficiency of ray
casting are addressed, and fast picture methods for interactive modeling are introduced
to meet the challenges.
The focus then turned towards different optimizations of CSG algorithms in the setting
of ray tracing. A simplistic single hit intersection algorithm is introduced in [10]. This
suggested mechanism reduces memory load and the number of computations performed
for ray classification. Though limitations have to be respected since sub-objects must
closed, non-self-intersecting, and have consistently oriented normals. However, this later
proved to be a solution that does not gracefully handle edge cases, especially for the
difference and intersection operations [33].
A "slicing" approach is also proposed in [14]. Similar to our proposed solution com-
binations of meshes and analytical primitives through CSG operations are permissible.
Nevertheless, this approach requires one boolean per primitive and a complete evalua-
tion of the CSG expression in each step; therefore, making it simple but limited, and much
better approaches are imaginable.
Bound definitions are also a popular way of significantly reducing the time required by
CSG algorithms. If the ray and the geometric entities are bound, I first perform a test to
see if the ray and the bounding volume around a geometric entity overlap. Only when the
boxes overlap does one continue to test whether the ray and the entity do so as well. A
submitted S-bounds algorithm is brought forth in [5] as a means of acceleration in solid
modeling and CSG.
Techniques that optimize various CSG rendering algorithms, namely the Goldfeather and
the layered Goldfeather algorithm, and the Sequenced-Convex- Subtraction (SCS) al-
gorithm are advanced in [11]. Although the work represents a significant improvement
towards real-time image-based CSG rendering for complex models, the main focus is on
hardware acceleration.
5
~o = the origin of the ray (e.g., camera position).
~
d = the direction of the ray (e.g., direction from camera origin to pixel in raster).
t = the distance to the intersection.
prim = a pointer holding surface information of the intersected primitive.
One can distinguish two types of ray intersection tests [22]. First, ray-primitive intersection
tests on convex geometries such as blocks, cylinders, cones, and spheres. Because the
primitives are analytically defined, the solution is solving the analytic intersection equa-
tion. Consequently, this means that the intersection solution is primitive-specific. Many
resources providing the analytical solutions are available [18]. Second, we encounter
the more generic solid-ray intersection. As I have previously defined in the introduction,
a solid is often a boundary representation composed of several triangles. Hence, the
main intricacy in ray-solid intersection renders iterating over all primitives and reducing
the problem to n ray-primitive intersection tests with n being the number of primitives
(e.g., triangles) in the solid. We can consider the ray-solid intersection as a more gen-
eral form of ray-primitive intersection since a primitive is always representable as a solid
bearing a single surface. The interesting consequence of such an abstraction is that if a
ray is shot in the scene, the computation for determining ray-geometry intersection can
be generalized to:
6
(a) Miss intersection. (b) Tangent intersection.
set theory have been intensively discussed previously in [19], [29],[13], and many other
resources. Hence, I will be mainly focusing on definitions and properties that interest
us. Formal proofs of the introduced properties are also available in the before-mentioned
resources.
Definition 3.1 (Set Operations). Assume that X and Y are subsets of a universe W . We
can use the following standard notations:
X ∪Y (1)
X ∩Y (2)
X−Y (3)
Where (1), (2), and (3) respectively denote the union, intersection, and difference of the
subsets X and Y .
7
Property 3.1. Union and intersection operations are commutative [15].
X ∪Y =Y ∪X
X ∩Y =Y ∩X
Property 3.2. Union and intersection operations are distributive over themselves and
each other [15].
X ∪ (Y ∩ Z) = (X ∪ Y ) ∩ (X ∪ Z)
X ∩ (Y ∪ Z) = (X ∩ Y ) ∪ (X ∩ Z)
Property 3.3. The empty set ∅ and the universe W are identity elements for the union
and intersection operators [15].
X ∪∅=X
X ∩W =X
X ∪ cX = W
X ∩ cX = ∅
Definition 3.2 (Boolean Algebra). Conducting the three operations ∪, ∩, and − on a set
of elements from the universe W while satisfying the properties (3.1) to (3.4) is called
boolean algebra [19].
Topological spaces are a generalization of metric spaces in which the notion of "near-
ness" is introduced but not in any quantifiable way that requires a direct distance defini-
tion [15].
Definition 3.3. A topological space is a pair (W, T ) where W is a set and T is a class of
subsets of W called the open sets and satisfying the three properties 3.5, 3.6, and 3.7.
(Figure 5)
Property 3.5. The empty set ∅ and the universe W are open [15].
Property 3.6. The intersection of a finite number of open sets is an open set [15].
Property 3.7. The union of any collection of open sets is an open set [15].
8
3.2.3 Closed Sets
3.2.4 Neighborhood
3.2.5 Interior
3.2.6 Boundary
9
boundary points of X [19]. (Figure 8)
3.2.7 Closure
Definition 3.8. The closure of a subset X, denoted kX, is the union of X with the set of
all its limit points. A point is a limit point of a subset X of a topological space (W, T ) if
each neighborhood of y contains at least a point of X different from y [19]. (Figure 9)
3.2.8 Regularity
Definition 3.9 (Regularity). The regularity of a subset X of W , denoted rX, is the set of
rX = kiX with k the closure and i the interior [15].
Definition 3.10 (Regular Set). A set X is regular if X = rX, i.e. if X = kiX [15].
(Figure 10)
Definition 3.11 (Regularized Set Operators). The regularized union, intersection, differ-
ence and complement are defined per:
X∪∗ Y = r(X ∪ Y )
X∩∗ Y = r(X ∩ Y )
X−∗ Y = r(X−Y )
c∗ X = rcX
The membership classification function allows to segment a candidate set into three sub-
sets which are the "inside", "outside", and "on the" of the reference set [29]. Here, I will
10
(b) Typical intersection with
(a) Initial polygons. dangling edge. (c) Regularized intersection.
Table 1: Notation
En Euclidean n-space
∅ Empty Set
W Reference Set Universe
W0 Candidate Set Universe
∪, ∩, −, c Set Operators
∪∗ , ∩∗ , −∗ , c∗ Regularized Set Operators in W
0 0 0 0
∪∗ , ∩∗ , −∗ , c∗ Regularized Set Operators in W 0
i, b, k, r Interior, boundary, closure, and regularity in W
i0 , b0 , k 0 , r0 Interior, boundary, closure, and regularity in W 0
Primed symbols will be used in order to denote operations on the subspace W 0 while
normal symbols will be used to denote the subspace W (Table 1).
Definition 3.12. The membership classification function, M is defined as follows:
where
0
XinS = X ∩∗ iS
0
XonS = X ∩∗ bS
0
XoutS = X ∩∗ cS
11
The results obtained from this classification (XinS, XonS, XoutS) are the regular por-
tions of the candidate set, X, in the interior, boundary, and the exterior of the reference
set W (Figure 11). The produced results are a quasi-disjoint decomposition of the candi-
date; therefore:
X = XinS ∪ XonS ∪ XoutS (5)
and for "almost" all points in the subset:
XinS ∩ XonS = ∅
XonS ∩ XoutS = ∅
XinS ∩ XoutS = ∅
I say almost since the subsets are generally not disjoint in the conventional sense. (e.g.
in Figure 11, XinS and XonS share a boundary point) [15].
Constructive geometry representations are binary trees whose nonterminal nodes desig-
nate regularized set operators and whose terminal nodes designate primitives. We refer
to the specific case of constructive geometry in E 3 where regularized compositions are
constructed of solid primitives as constructive solid geometry. Regular sets are closed
under the regularized set operators. Thus, a class of regular sets can be represented
constructively as a combination of other more simple (regular) sets [19].
For example, as illustrated in Figure 12, if the universe W is in E 2 and I select the class
of closed half-planes as our primitives, I could construct any regular set in E 2 given that
it is bounded by a finite number of straight line segments.
We choose to define the constructively represented regular sets using the divide-and-
conquer paradigm as it is a natural approach to compute the value of such a function.
Therefore, when a regular set S is not a primitive, a nonterminal node, we convert the
problem of evaluating the function f (S) into two instances of f followed by a combine, g,
step. When S is a primitive, terminal node, the problem can no longer be divided, and an
evaluator, ef , is used. We can now consider the general function for evaluation M when
the reference set S is represented constructively.
(
eM (X, S), if S ⊂ A
M [X, S] = (6)
g(M [X, l-subtree(S)], M [X, r-subtree(S)], root(S)), otherwise
12
Figure 12: A constructive representation of a polygon P using half-planes.
The tree on the right is the constructive geometry representation.
where
To customize this general definition to be used in a specific domain, one must design the
classification procedure, eM , and the combine procedure. The next section discusses
both these procedures.
13
Figure 13: Example of combining ray classifications.
∩ in in in
in out out
out in out
out out out
− in in out
in out in
out in out
out out out
14
4 Optimization
In this section, I will introduce the state-of-the-art CSG algorithm that is implemented in
the OpenRT. Here I expose all the adjustments and changes I have made to the algorithm
in order to maximize its performance and results. I will discuss a minimal hit classification
algorithm, box enclosures, and how simple techniques such as "early-outs" can increase
performance. Additionally, I discuss the binary space partitioning indexing structure for
faster traversal of complex scenes and geometry. Finally, I will put it all together in our
version of the CSG algorithm.
Table 3: Sub-procedures
Consider the case of the spheres shown in Figure 14. The union of these two solids is
the boundary of each of the spheres without their interior. Therefore, to find the correct
classification results I must find the closest intersection from our ray origin such that it
does not belong to the interior of the sphere.
15
(a) Ray goes through both (b) Ray misses one of the (c) Ray is inside one of the
spheres. spheres. spheres.
For the case where the ray enters both spheres (Figure 14a), the procedure would first
get the closest intersections with A and B. Both these intersections would be classified
as enter states; therefore, it must only find M IN (A, B) to conclude which one of the
boundaries of the sphere is closest.
Let us now examine the case where no intersection is found with one or all of the solids
as shown in Figure 14b. If both A and B’s states are a miss, return a miss. Otherwise, if
only one of them is a miss, return the other regardless if it’s an enter or exit.
The last set of cases arise when the ray is shot from the interior of the spheres (Figure
Figure 14c). This is more intricate since our procedure must learn to neglect the inner
sides and only get the outer sides. If the first evaluation returns enter for B and exit for
A, then one must check which one of them is closer. If A < B, then return A. Otherwise,
move our origin to B and start the procedure again. If it is the opposite, then perform the
previous logic but permute A and B. The final case is when the ray exits both A and B.
Here, return M AX(A, B). Algorithm 2 shows the pseudocode for the union logic.
I will stick to the same general example; however, I will be performing the intersection of
two spheres. (Figure 15)
(a) Ray goes through both (b) Ray misses one of the (c) Ray is inside one or both
spheres. spheres. spheres.
The intersection of two spheres is their interior without the boundaries. I will apply the
same previously defined notations shown in Table 3. First, I will begin with the obvious
case where A or B classify as misses (Figure 15a). By definition, the intersection is the
shared area; therefore, if the ray misses one of the solids, one can already evaluate this
as a miss.
16
Algorithm 2: Minimal hit classification for union.
Result: Intersection Point
while true do
minA = intersectMin(A);
minB = intersectMin(B);
stateA = classify(minA );
stateB = classify(minB );
if stateA == miss && stateB == miss then
return miss;
if stateA == miss then
return minB ;
if stateB == miss then
return minA ;
if stateA == stateB then
if stateA == enter then
ReturnClosest(minA , minB );
if stateA == exit then
ReturnFurthest(minA , minB );
if stateA == enter && stateB == exit then
IfXCloserReturn(minB );
AdvanceToXLoop(minA );
if stateA == exit && stateB == enter then
IfXCloserReturn(minA );
AdvanceToXLoop(minB );
end
The second case is when they both have the same classification. If both return an exit
state, then take the closest of both. However, if they both return an enter, either advance
to A or B depending on which one is closest (Figure 15b).
The final case is when the states are not a miss and also different from each other. If A
is an enter state while B is an exit state, then return A if it’s closer or move the ray origin
to B and advance. Perform the opposite if A is exit and B is entered. Algorithm 3 shows
the pseudocode for the intersection logic.
The difference operation is not commutative; therefore, the direction of the ray renders
completely different results. I will stick to the same example as the previous two cases
and with similar notations (Figure 16).
17
Algorithm 3: Minimal hit classification for the intersection.
Result: Intersection Point
while true do
minA = intersectMin(A);
minB = intersectMin(B);
stateA = classify(minA );
stateB = classify(minB );
if stateA == miss || stateB == miss then
return miss;
if stateA == stateB then
if stateA == enter then
AdvanceToClosestLoop(minA , minB );
if stateA == exit then
ReturnClosest(minA , minB );
if stateA == enter && stateB == exit then
IfXCloserReturn(minA );
AdvanceToXLoop(minB );
if stateA == exit && stateB == enter then
IfXCloserReturn(minB );
AdvanceToXLoop(minA );
end
(a) Ray misses one of the (b) Ray goes through both (c) Ray is inside one or both
spheres. spheres. spheres.
I will first consider the case where a ray misses one of the two spheres, as shown in
Figure 16a. If the ray only misses A or both, then consider this a miss. If the ray only
misses B, return A regardless of entering or exit.
The second case is when they both have the same classification. If both return an exit
state then return B if closer and flip it’s normal, otherwise, advance the ray origin to B.
However, if they both return an entry then return A if it’s closer or advances to the hit point
of B.
The last case is when the classifications are different from each other. If the ray enters
A and exits B, return B if it’s closer or advances to A. However, if the classifications
are the opposite, advance to whichever is closer and continue. Algorithm 4 shows the
pseudocode for the different logic.
18
Algorithm 4: Minimal hit classification for the difference.
Result: Intersection Point
while true do
minA = intersectMin(A);
minB = intersectMin(B);
stateA = classify(minA );
stateB = classify(minB );
if stateA == miss then
return miss;
if stateB == miss then
return minA ;
if stateA == enter && stateB == enter then
IfXCloserReturn(minA );
AdvanceToXLoop(minB );
if stateA == exit && stateB == exit then
IfXCloserReturnFlip(minB );
AdvanceToXLoop(minA );
if stateA == enter && stateB == exit then
AdvanceToClosestLoop(minA , minB );
if stateA == exit && stateB == enter then
IfXCloserReturnFlip(minA );
return minB
end
19
boxes is straightforward [21]. Therefore, in the case of operations with less voluminous
geometry, parts of the initial primitives piercing outside of the composite’s bounds are
also neglected.
A bounding box is a rectangular parallelepiped defined by exactly two points (Figure 17).
Each primitive, solid, and composite must be able to define its bounding box. For primitive
cases, the bounding box is case-specific. For example, the bounding box of a primitive
sphere of radius r = 1 located at center point ~o = (0, 0, 0) has a bounding box whose
maximum and minimum points are (r, r, r) and (−r, −r, −r). Solids are more complicated
as they are composed of many primitives. Hence, one has to create a collapsed bounding
box (a bounding box whose min and max coordinates are respectively + inf and − inf)
and gradually start inflating using the primitive’s predefined boxes. The inflation step is
as simple as checking if the value of a coordinate of the current bounding box is smaller
or bigger than that of the primitive’s bounding box and either picking the smallest or
the greatest value depending on the point being checked. For instance, if our current
bounding box has min1 (0, 0, 0) and max1 (1, 1, 3) and the current primitives bounding box
has min2 (−1, −1, 1) and max2 (2, 2, 2) then the current values of the points of the inflated
bounding box become min(−1, −1, 0) and max(2, 2, 3).
Combining the boxes on the composite level is also very important to realize. One
can achieve this trivially with the usual rules of algebra defined in the previous section.
Though that doesn’t hold for the difference operation as its results are not easily fore-
seeable, and the cost of analyzing the entire composition is counter-productive in this
case [21]. When dealing with the union operation, select the smallest value from both
boxes per coordinate for the minimum and vice-versa. For the intersection operation,
pick the highest value from both boxes per coordinate for the minimum - opposite to the
union. The dual for the maximum. For the difference, I have previously mentioned that
it’s not possible to generalize using boolean algebra; therefore, keep the minimum and
maximum of the left box as one can be sure that the result of the subtraction opera-
tion will never be bigger than the left geometry - if A and B are closed sets in E n then
A−B ≤ A. Figure 18 shows the different operations on rectangles. The same logic holds
for the three-dimensional solids as only a check for an additional coordinate is needed.
Algorithm 5 defines the procedure for composite boxes [21].
20
(a) Initial bounding boxes. (b) Union of bounding boxes.
21
more "restricted" type of BSP trees in which only axis-aligned splitting planes are allowed.
These trees conform much better with computational advantages and memory needs but
do not adapt very well to scene complexities. It is relatively easy to generate an inef-
ficient binary tree with non-axis-aligned geometry (e.g., a long skinny cylinder oriented
diagonally) [7]. All variations of the algorithms are generally composed of two fundamen-
tal parts, building and traversing the tree. How one chooses these two core procedures
tremendously affects the amount of acceleration achievable. Because the main focus of
the work doesn’t align with the improvement of building or traversal procedures, the BSP
algorithm remains as is. However, many algorithms such as surface area heuristic, local
greedy SAH, automatic termination criteria, and many more have proved to optimize KD
trees [32, 25].
The tree is constructed recursively in a top-down manner, making a local greedy deci-
sion about the splitting planes. OpenRT uses axis-aligned bounding boxes to wrap the
nodes. It chooses the split dimension using the current largest dimension (i.e., if the box
is biggest in its x axis then it will pick that as our splitting plane). It then positions the
plane at the spatial median of the dimension. The subdivision is performed until either
the number of primitives in a single node falls below a predefined threshold or the tree
depth exceeds a maximum value. The user provides these stopping criteria. To better
illustrate the algorithm, I will utilize the simple two-dimensional KD-tree and the triangles
in Figure 19. Each node in the tree represents a triangle, and each internal node rep-
resents an axis-aligned rectangular region with an axis-aligned plane that separates the
regions of its two children.
Figure 19: Simple scene with a few triangles and a corresponding tree.
Leaves are boxes and inner nodes are circles.
A ray traverses a BSP tree by intersecting the ray with the split plane; therefore, giving
a ray distance to the plane, allowing us to divide it into segments. The ray segment is
analyzed by clipping the current ray with the bounding box of the current node. OpenRT
traverses a node if the ray segment overlays the node. Since the two-child nodes do not
overlap, it can trivially classify which node is closer to the ray direction and traverse that
node first. For the traversal algorithm, the children should be labeled as near and far
22
child nodes, giving us three possible cases of traversal:
1. Ray goes through near child only. (Figure 20a)
2. Ray goes through far child only. (Figure 20b)
3. Ray goes through the near child first followed by the far child. (Figure 20c)
The near and far classification uses the direction of the ray and the position of the splitting
plane. Therefore, it classifies the left node as near and the right node as far if the sign of
the ray direction in the splitting axis is positive and vice versa if negative. Once it reaches
the terminal nodes, it can then search for the intersection of all the primitives in the node,
if any.
(a) Ray goes through near (b) Ray goes through far child (c) Ray goes through both
child only. only. children.
23
5 Evaluation of the results
There are three variants of the CSG method implemented in OpenRT. The first is the
naive implementation which I refer to as N aiCSG. The second uses a binary space
partition tree to solve the visible surface problem but still naively finds intersections inside
the combinatorial geometry, which I will refer to as BinCSG. Finally, I will introduce
our optimized algorithm, which uses a binary space partition tree on the outside (solving
the visible surface problem) and also inside each composite geometry to direct the rays
towards the correct geometries, which I will refer to as OptimCSG. All these variants are
tested using the minimal hit CSG algorithm. I consider each algorithm for all operations
and a low, medium, and high viewport fill rate. I conduct three main tests. First, I assess
how the rendering time develops to the complexity of the geometry. In this case, the
complexity of the geometry is the number of polygons in the sphere meshes. The second
test demonstrates how the spatial distribution of the scene affects the times for each of
the algorithms. Therefore, helping us grasp how significant the viewport fill percentage
changes each of these algorithms individually. I also run tests to count the number of
intersection tests performed by each variant at each pixel. The last test is based on
the effect of a different number of nested geometries on the various algorithms while
maintaining a relevantly similar viewport fill rate. These tests are all run on the following
configuration with CPU parallel processing:
• Model: 2020 Macbook Pro A2289
• Graphics Card: Integrated Intel Iris Plus Graphics 645 1536 MB
• RAM: 16 GB 2133 MHz LPDDR3
• CPU: 1,7 GHz Quad-Core Intel Core i7
24
Figure 21: Range of view port rates on which the tests are conducted as the
complexity of the geomtery increases. The area around the curve signifies
the error by which the rate fluctuates.
25
Figure 23: N aiCSG rendering time of different operations with respect to
gradual increases in geometry complexity in a scene filling a medium rate of
the view port.
26
Figure 25: The N aiCSG mean rendering time of the operations for the differ-
ent view port fill rates.
Second, I compare the rendering time of the BinCSG variant. Figure 26 to 28 show the
performance of each operation in relation to the other. In Figure 26, one notices the same
discrepancy between the union operation and the two others. A simple check proves that
the factor by which the time increases per operation is indeed a constant. Figure 33
demonstrates how the computational time of BinCSG worsens depending on the spatial
distribution in the scene as the time gained from the ray-box tests performed in BinCSG
become less useful.
27
Figure 27: BinCSG rendering time of different operations with respect to
gradual increases in geometry complexity in a scene filling a medium rate of
the view port.
28
Figure 29: The BinCSG mean rendering time of the operations for the differ-
ent view port fill rates.
I must also analyze the performance of OptimCSG with the different operations. As seen
in Figures 30 to 32, OptimCSG shows different performance in terms of the operations.
The general curve is also not linear but follows a rather logarithmic trend. This can be
closely tied to the cost of traversing a binary tree. The procedure also increases in time
after an increase in the fill rate.
29
Figure 31: OptimCSG rendering time of different operations with respect to
gradual increases in geometry complexity in a scene filling a medium rate of
the view port.
30
Figure 33: The OptimCSG mean rendering time of the operations for the
different view port fill rates.
Lastly, I compare the performance of these variants to each other. Figures 34 to 36 show
the comparison of the different implementations with low, mid, and high viewport fills.
OptimCSG outperforms both variants in all cases. BinCSG does outperform N aiCSG
in smaller scenes; however, it scales to the same computational time in more complex
ones.
Figure 34: Rendering time of different operations with respect to gradual in-
creases in geometry complexity in a scene filling a small rate of the view port.
31
Figure 35: Rendering time of different operations with respect to gradual in-
creases in geometry complexity in a scene filling a medium rate of the view
port.
Figure 36: Rendering time of different operations with respect to gradual in-
creases in geometry complexity in a scene filling a high rate of the view port.
I can explain these variations by the number of ray-primitive intersection tests performed
by each variant. In the naive implementation, I check for all primitives for all the rays in
the scene, which explains why it is not so case-dependent. In BinCSG, if the ray-box
test is positive, it naively makes the ray-primitive intersections. OptimCSG has a more
directed approach as both the left and right geometries in the composite are also split
up into smaller boxes and traversed efficiently. Figures 38 to 40 represent the number
of intersections performed in a 1000 × 1000 pixel image with two spheres in the scene
(Figure 37). In Figure 38 one can see that the procedure continually makes the same
number of ray-primitive tests for each pixel. However, Figure 39 reveals how BinCSG is
capable of avoiding intersections toward the areas where the bounding box is not defined
32
but still performs all tests in pixels overlapping it. Ultimately, OptimCSG (Figure 40)
exhibits a much more efficient approach where the number of ray primitive intersections
solely grows in areas dense with primitives or overlapping bounding boxes.
Figure 38: Surface plot showing the number of ray-primitive tests on each
pixel on a 1000 × 1000px render with N aiCSG.
33
Figure 39: Surface plot showing the number of ray-primitive tests on each
pixel on a 1000 × 1000px render with BinCSG.
Figure 40: Surface plot showing the number of ray-primitive tests on each
pixel on a 1000 × 1000px render with OptimCSG.
Figure 41 demonstrates the achieved speedup between all three variants in the various
viewport fill rates with 6048 triangles. OptimCSG can achieve up to 49x faster times in a
small scene and up to 26x faster on a dense scene. Additionally, since both N aiCSG and
BinCSG grow linearly and OptimCSG grows logarithmically, then the speedup should
increase as the spatial distributions gets more complex.
34
(a) LVP speedup. (b) MVP speedup.
Figure 42: Range of view port rates on which the tests are conducted as the
number of nests increases.
Figure 43 portrays the different performances of each variant. As expected, the naive
time grows linearly as more geometries are used. BinCSG is also somewhat linear but
is expected to scale to the same time as the naive if the viewport fill rate is increased. On
the other hand, OptimCSG still follows a logarithmic trend when nesting.
35
Figure 43: Performance of the different variants in comparison to each other
when nesting more geometries together.
Similar to the geomtric complexity tests, OptimCSG provides a much higher speedup.
(Figure 44)
36
The first encountered issue is artifacts created by the numeric stability issues in classi-
fication. When classifying using the surface normals, I ran into the issue where the dot
product of the ray direction and the normal is near zero (vectors are orthogonal). Hence,
leading to certain artifacts emerging near the boundaries of the geometries. The de-
scribed issue can be solved using a sampler which would increase the number of rays
shot in the neighborhood of a pixel and estimate a better shading result. This solution is
already plausible in OpenRT.
The second limitation is that the geometries must have consistently oriented normals
to understand intersections. While all primitives and solids constructed inside OpenRT
guarantee this property, meshes imported from the outside could potentially lead to is-
sues. However, one can solve this by implementing an extra scan when constructing
solids or passing them to a composite to verify and modify the surface normals when
needed. Algorithms that allow for fast checks of consistent normal orientation are readily
available [4].
Many improvements are also possible in alignment with the work established in this pa-
per. The first is extending the binary space partitioning tree algorithm to be more efficient
in building and traversing. Such a change can bring drastic improvements to the perfor-
mance and handle much more complex scenes. Automated stopping criteria are a way
to let each solid deterministically choose which stopping criteria work best, particularly in
BREPs.
Second, this research heavily focused on acceleration with CPU-based ray tracing. Many
solutions to extend the system to a GPU exist, and such benefits could make the algo-
rithm gain from the ever-increasing performance of the graphics hardware.
Conversion algorithms from constructively defined solids to BREPs are detrimental as
well. Conversion allows for faster computations and ease of use. If we can translate a
constructively generated geometry to a BREP, we can increase performance, and final
geometries would no longer rely on a recursive evaluation. One can then also use the
optimized triangle operations for faster computations and rendering. The solution is es-
pecially appealing since we can divide complex geometries into smaller models to unify
later.
Applying textures to constructive solid geometry is also an area of interest. One could
achieve the latter with a few different flavors. Automatic texture mapping is one of them.
The goal is of automatic texture mapping is to produce texture coordinates for geometries
that don’t possess any. One could use a sphere, cube, or any other map to generate
these texture coordinates. Therefore, enabling the use of different textures on each of
the constructively generated geometries. The texture coordinate generation function can
also be specified separately for each geometry from the user.
Topics such as the reconstruction of constructive solid geometry trees using unsuper-
vised neural networks are also interesting. Kania, Zieba,
˛ and Kajdanowicz produced
models that allow for fast and accurate reconstruction of CSG trees from images in [8].
Such an advancement would make the possibility of combining a multitude of geomet-
rical resources simpler. Additionally, combining this with the conversion to a boundary
representation makes such a possibility more appealing.
37
References
[1] Timo Aila and Samuli Laine. “Understanding the Efficiency of Ray Traversal on
GPUs”. In: Proceedings of the Conference on High Performance Graphics 2009.
HPG ’09. New Orleans, Louisiana: Association for Computing Machinery, 2009,
145–149. ISBN: 9781605586038. DOI: 10 . 1145 / 1572769 . 1572792. URL: https :
//doi.org/10.1145/1572769.1572792.
[2] Tomas Akenine-Mller, Eric Haines, and Naty Hoffman. Real-Time Rendering, Fourth
Edition. 4th. USA: A. K. Peters, Ltd., 2018. ISBN: 0134997832.
[3] Arthur Appel. “Some Techniques for Shading Machine Renderings of Solids”. In:
Proceedings of the April 30–May 2, 1968, Spring Joint Computer Conference.
AFIPS ’68 (Spring). Atlantic City, New Jersey: Association for Computing Machin-
ery, 1968, 37–45. ISBN: 9781450378970. DOI: 10 . 1145 / 1468075 . 1468082. URL:
https://doi.org/10.1145/1468075.1468082.
[4] P. Borodin, Gabriel Zachmann, and Reinhard Klein. “Consistent normal orientation
for polygonal meshes”. In: July 2004, pp. 18 –25. ISBN: 0-7695-2171-1. DOI: 10.
1109/CGI.2004.1309188.
[5] S. Cameron. “Efficient Bounds in Constructive Solid Geometry”. In: IEEE Computer
Graphics and Applications 11.03 (May 1991), pp. 68–74. ISSN: 1558-1756. DOI:
10.1109/38.79455.
[6] Kuang-Hua Chang. Chapter 3 - Solid Modeling. Ed. by Kuang-Hua Chang. Boston,
2015. DOI: https : / / doi . org / 10 . 1016 / B978 - 0 - 12 - 382038 - 9 . 00003 - X. URL:
https://www.sciencedirect.com/science/article/pii/B978012382038900003X.
[7] Thiago Ize, Ingo Wald, and Steven Parker. “Ray tracing with the BSP tree”. In: Sept.
2008, pp. 159–166. ISBN: 978-1-4244-2741-3. DOI: 10.1109/RT.2008.4634637.
[8] Kacper Kania, Maciej Zieba,
˛ and Tomasz Kajdanowicz. UCSG-Net – Unsuper-
vised Discovering of Constructive Solid Geometry Tree. 2020. arXiv: 2006.09102
[cs.CV].
[9] Peter Keenan. Geographic Information Systems. Ed. by Hossein Bidgoli. New York,
2003. DOI: https://doi.org/10.1016/B0- 12- 227240- 4/00077- 0. URL: https:
//www.sciencedirect.com/science/article/pii/B0122272404000770.
[10] Andrew Kensler. Ray Tracing CSG Objects Using Single Hit Intersections. English.
Oct. 2006. URL: http://xrt.wdfiles.com/local--files/doc\%3Acsg/CSG.pdf.
[11] Florian Kirsch and Jürgen Döllner. “Rendering Techniques for Hardware-Accelerated
Image-Based CSG.” In: Jan. 2004, pp. 221–228.
[12] Reinhard Klette and Azriel Rosenfeld. CHAPTER 7 - Curves and Surfaces: Topol-
ogy. Ed. by Reinhard Klette and Azriel Rosenfeld. San Francisco, 2004. DOI: https:
//doi.org/10.1016/B978-155860861-0/50009-2. URL: https://www.sciencedirect.
com/science/article/pii/B9781558608610500092.
[13] Alistair H. Lachlan, Marian Srebrny, and Andrzej Zarach. Set theory and hierarchy
theory V: Bierutowice, Poland, 1976. Springer-Verlag, 1977.
[14] Sylvain Lefebvre. “IceSL: A GPU Accelerated CSG Modeler and Slicer”. In: AEFA’13,
18th European Forum on Additive Manufacturing. Paris, France, June 2013. URL:
https://hal.inria.fr/hal-00926861.
[15] Maynard J. Mansfield. Introduction to topology. R.E. Krieger, 1987.
38
[16] M. E. Newell, R. G. Newell, and T. L. Sancha. “A Solution to the Hidden Sur-
face Problem”. In: Proceedings of the ACM Annual Conference - Volume 1. ACM
’72. Boston, Massachusetts, USA: Association for Computing Machinery, 1972,
443–450. ISBN: 9781450374910. DOI: 10.1145/800193.569954. URL: https://
doi.org/10.1145/800193.569954.
[17] Stephen M. Pizer et al. 6 - Object shape representation via skeletal models (s-
reps) and statistical analysis. Ed. by Xavier Pennec, Stefan Sommer, and Tom
Fletcher. 2020. DOI: https : / / doi . org / 10 . 1016 / B978 - 0 - 12 - 814725 - 2 .
00014 - 5. URL: https : / / www . sciencedirect . com / science / article / pii /
B9780128147252000145.
[18] Ray tracing primitives. URL: https : / / www . cl . cam . ac . uk / teaching / 1999 /
AGraphHCI/SMAG/node2.html (visited on 04/05/2021).
[19] A. Requicha and R. Tilove. “Mathematical Foundations of Constructive Solid Ge-
ometry: General Topology of Closed Regular Sets”. In: 1978.
[20] Aristides G. Requicha. “Representations for Rigid Solids: Theory, Methods, and
Systems”. In: ACM Comput. Surv. 12.4 (Dec. 1980), 437–464. ISSN: 0360-0300.
DOI : 10.1145/356827.356833. URL : https://doi.org/10.1145/356827.356833.
[21] Scott D Roth. “Ray casting for modeling solids”. In: Computer Graphics and Image
Processing 18.2 (1982), pp. 109–144. ISSN: 0146-664X. DOI: https://doi.org/
10 . 1016 / 0146 - 664X(82 ) 90169 - 1. URL: https : / / www . sciencedirect . com /
science/article/pii/0146664X82901691.
[22] Scratchapixel. Rasterization: a Practical Implementation. Jan. 2015. URL: https:
/ / www . scratchapixel . com / lessons / 3d - basic - rendering / rasterization -
practical-implementation.
[23] Sergey Kosov. OpenRT. May 8, 2021. URL: https://github.com/Project- 10/
OpenRT.
[24] Bin Sheng et al. “Efficient non-incremental constructive solid geometry evaluation
for triangular meshes”. In: Graphical Models 97 (2018), pp. 1–16. ISSN: 1524-0703.
DOI : https : / / doi . org / 10 . 1016 / j . gmod . 2018 . 03 . 001. URL: https : / / www .
sciencedirect.com/science/article/pii/S1524070318300067.
[25] Maxim Shevtsov, Alexei Soupikov, and Alexander Kapustin. “Highly Parallel Fast
KD-tree Construction for Interactive Ray Tracing of Dynamic Scenes”. In: Computer
Graphics Forum 26.3 (2007), pp. 395–404. DOI: https://doi.org/10.1111/j.
1467-8659.2007.01062.x. eprint: https://onlinelibrary.wiley.com/doi/pdf/
10.1111/j.1467-8659.2007.01062.x. URL: https://onlinelibrary.wiley.com/
doi/abs/10.1111/j.1467-8659.2007.01062.x.
[26] Allan D. Spence and Yusuf Altintas. Modeling Techniques and Control Architectures
for Machining Intelligence. Ed. by C.T. Leondes. 1995. DOI: https://doi.org/10.
1016/S0090-5267(06)80031-9. URL: https://www.sciencedirect.com/science/
article/pii/S0090526706800319.
[27] Sebastian Steuer. Methods for Polygonalization of a Constructive Solid Geometry
Description in Web-based Rendering Environments. Dec. 2012. URL: https : / /
www.en.pms.ifi.lmu.de/publications/diplomarbeiten/Sebastian.Steuer/DA_
Sebastian.Steuer.pdf.
39
[28] Kelvin Sung and Peter Shirley. “VI.1 - RAY TRACING WITH THE BSP TREE”.
In: Graphics Gems III (IBM Version). Ed. by DAVID KIRK. San Francisco: Morgan
Kaufmann, 1992, pp. 271–274. ISBN: 978-0-12-409673-8. DOI: https://doi.org/
10.1016/B978-0-08-050755-2.50061-0. URL: https://www.sciencedirect.com/
science/article/pii/B9780080507552500610.
[29] R.B. Tilove. “A study of GEOMETRIC SET-MEMBERSHIP CLASSIFICATION”. en.
(Master’s thesis, University of Rochester, 1977.
[30] Visualization of a polygon mesh. Jan. 2021. URL: https://en.wikipedia.org/
wiki/Polygon_mesh.
[31] H. B. Voelcker and A. A. G. Requicha. “Geometric Modeling of Mechanical Parts
and Processes”. In: Computer 10.12 (1977), pp. 48–57. DOI: 10.1109/C-M.1977.
217601.
[32] Ingo Wald and Vlastimil Havran. “On Building Fast kd-trees for Ray Tracing, and on
Doing that in O(N log N)”. In: Symposium on Interactive Ray Tracing 0 (Sept. 2006),
pp. 61–69. DOI: 10.1109/RT.2006.280216.
[33] XRT Renderer. URL: http://xrt.wikidot.com/doc:csg.
40