-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.cpp
More file actions
162 lines (148 loc) · 2.77 KB
/
Copy pathboolean.cpp
File metadata and controls
162 lines (148 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "boolean.h"
// Determine if the ray intersects with the boolean of A and B.
bool Boolean::Intersection(const Ray& ray, std::vector<Hit>& hits) const
{
// TODO
if(type == type_intersection)
{
std::vector<Hit> hits1;
std::vector<Hit> hits2;
if(A->Intersection(ray,hits1)&& B->Intersection(ray,hits2))
{
float A1 = hits1[0].t;
float A2 = hits1[1].t;
float B1 = hits2[0].t;
float B2 = hits2[1].t;
if( A2 < B1 )
{
return false;
}
else if( B2 < A1 )
{
return false;
}
else if( A1 <= B1 && B2 <= A2 )
{
hits = hits2;
return true;
}
else if( B1 <= A1 && A2 <= B2 )
{
hits = hits1;
return true;
}
else if( A1 <= B1 && A2 <= B2 )
{
hits.push_back(hits2[0]);
hits.push_back(hits1[1]);
return true;
}
else if( B1 <= A1 && B2 <= A2 )
{
hits.push_back(hits1[0]);
hits.push_back(hits2[1]);
return true;
}
}
return false;
}
else if(type == type_difference)
{
std::vector<Hit> hits1;
std::vector<Hit> hits2;
if(A->Intersection(ray,hits1))
{
if( B->Intersection(ray,hits2) )
{
float A1 = hits1[0].t;
float A2 = hits1[1].t;
float B1 = hits2[0].t;
float B2 = hits2[1].t;
if( A2 < B1 )
{
//std::cerr << "IN1 ";
hits = hits1;
//*B = *this;
return true;
}
else if( B2 < A1 )
{
//std::cerr << "IN2 ";
hits = hits1;
return true;
}
else if( A1 < B1 && B1 < A2 && A2 < B2 )
{
//std::cerr << "IN3 ";
hits.push_back( hits1[0] );
hits.push_back( hits2[0] );
//*B = *this;
return true;
}
else if( B1 < A1 && B2 < A2 && A1 < B2 )
{
//std::cerr << "IN4 ";
hits.push_back( hits2[1] );
hits.push_back( hits1[1] );
//*B = *this;
return true;
}
else if( B1 < A1 && A2 < B2 )
{
return false;
}
}
hits = hits1;
return true;
}
return false;
}
else if(type == type_union)
{
std::vector<Hit> hits1;
std::vector<Hit> hits2;
bool objA = A->Intersection(ray,hits1);
bool objB = B->Intersection(ray,hits2);
//if ray intersects both in union
if(objA && objB)
{
if(hits1[0].t < hits2[0].t)
{
hits.push_back(hits1[0]);
}
else
{
hits.push_back(hits2[0]);
}
if(hits1[1].t > hits2[1].t)
{
hits.push_back(hits1[1]);
}
else
{
hits.push_back(hits2[1]);
}
return true;
}
//if ray intersects only A
else if(objA)
{
hits = hits1;
return true;
}
//if ray intersects only B
else if(objB)
{
hits = hits2;
return true;
}
return false;
}
return false;
}
// This should never be called.
vec3 Boolean::Normal(const vec3& point, int part) const
{
assert(false);
return vec3();
}