-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.cpp
260 lines (215 loc) · 4.68 KB
/
cube.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*****************************************************************************
Cube Class
NAME:cube.cpp
DATE:17/12/1996
AUTHOR: Z.A. Nolan
*****************************************************************************/
// include the plane header
#include "cube.h"
#include "zerotest.h"
#define BIGNUMBER 1000000
// Constructor
TCube::TCube ()
:TObject()
{
_TopCorner=TVector(0,0,0) ;
_BottomCorner=TVector(0,0,0) ;
}
// Constructor
TCube::TCube (TVector TopCorner,TVector BottomCorner,
const TSurfaceProperties SurfaceProperties)
:TObject(SurfaceProperties)
{
_TopCorner=TopCorner;
_BottomCorner=BottomCorner ;
}
// Find the intersection of a plane and a line
double TCube::Intersection (TVector &Point,TVector &Normal,
const TLine &Line) const
{
TVector LineDirection ;
TVector NewNormal ;
double t1,t2 ;
double TNear,TFar ;
int Hit ;
double temp ;
Hit=1 ;
LineDirection=(Line.EndPoint()-Line.StartPoint()).Unit() ;
TNear=-BIGNUMBER ;
TFar=BIGNUMBER ;
if (IsZero(LineDirection.X()))
{
if ((Line.StartPoint().X()<_BottomCorner.X())||
(Line.StartPoint().X()>_TopCorner.X()))
{
// ray missed the cube
Hit=0;
}
}
else
{
// check the x slab
t1=(_BottomCorner.X()-Line.StartPoint().X())/LineDirection.X() ;
t2=(_TopCorner.X()-Line.StartPoint().X())/LineDirection.X() ;
NewNormal=TVector(-1,0,0) ;
if (t1>t2)
{
temp=t2 ;
t2=t1 ;
t1=temp ;
NewNormal=-NewNormal ;
}
if (t1>TNear)
{
TNear=t1 ;
Normal=NewNormal ;
}
if (t2<TFar)
{
TFar=t2 ;
}
if (TNear>TFar)
{
Hit=0;
}
if (TFar<0)
{
Hit=0 ;
}
}
// check the Y slab
if (IsZero(LineDirection.Y()))
{
if ((Line.StartPoint().Y()<_BottomCorner.Y())||
(Line.StartPoint().Y()>_TopCorner.Y()))
{
// ray missed the cube
Hit=0;
}
}
else
{
// check the y slab
t1=(_BottomCorner.Y()-Line.StartPoint().Y())/LineDirection.Y() ;
t2=(_TopCorner.Y()-Line.StartPoint().Y())/LineDirection.Y() ;
NewNormal=TVector(0,-1,0) ;
if (t1>t2)
{
temp=t2 ;
t2=t1 ;
t1=temp ;
NewNormal=-NewNormal ;
}
if (t1>TNear)
{
TNear=t1 ;
Normal=NewNormal ;
}
if (t2<TFar)
{
TFar=t2 ;
}
if (TNear>TFar)
{
Hit=0;
}
if (TFar<0)
{
Hit=0 ;
}
}
// do the Z slab
if (IsZero(LineDirection.Z()))
{
if ((Line.StartPoint().Z()<_BottomCorner.Z())||
(Line.StartPoint().Z()>_TopCorner.Z()))
{
// ray missed the cube
Hit=0;
}
}
else
{
// check the Z slab
t1=(_BottomCorner.Z()-Line.StartPoint().Z())/LineDirection.Z() ;
t2=(_TopCorner.Z()-Line.StartPoint().Z())/LineDirection.Z() ;
NewNormal=TVector(0,0,-1) ;
if (t1>t2)
{
temp=t2 ;
t2=t1 ;
t1=temp ;
NewNormal=-NewNormal ;
}
if (t1>TNear)
{
TNear=t1 ;
Normal=NewNormal ;
}
if (t2<TFar)
{
TFar=t2 ;
}
if (TNear>TFar)
{
Hit=0;
}
if (TFar<0)
{
Hit=0 ;
}
}
// check to see if the cube has been missed
if (Hit==0)
{
TNear=-BIGNUMBER ;
}
// now we can work out the intersection
Point=Line.StartPoint()+(LineDirection*TNear) ;
// now return the nearest one
return TNear ;
}
// function to input a plane
istream &TCube::Read (istream &In)
{
char Comma ; // delimter for the items
char Bracket ; // delimiter for vector and colour parts
TVector TopCorner ;
TVector BottomCorner ;
TColour Colour;
double Diffuse ;
double Specular ;
double Shine ;
int Roughness ;
// read in the colour
In>>Bracket>>TopCorner>>Bracket>>Comma>>Bracket>>BottomCorner>>Bracket
>>Comma>>Bracket>>Colour>>Bracket>>Comma>>Diffuse>>Comma>>Specular>>
Comma>>Shine>>Comma>>Roughness ;
// copy to the object
(*this)=TCube(TopCorner,BottomCorner,
TSurfaceProperties(Colour,Diffuse,Specular,Shine,Roughness)) ;
// return the stream
return In ;
}
// function to output a colour
ostream &TCube::Write (ostream &Out) const
{
// write out the colour
Out<<'('<<_TopCorner<<')'<<','<<_BottomCorner<<','
<<'('<<(*this).Colour()<<','<<(*this).Diffuse()<<','
<<(*this).Specular()<<','<<(*this).Shine()<<','
<<(*this).Roughness()<<')' ;
// return the stream
return Out ;
}
// function to read in a plane
istream &operator>>(istream &In,TCube &Cube)
{
// input a plane
return Cube.Read(In) ;
}
ostream &operator<<(ostream &Out,const TCube &Cube)
{
// output the colour
return Cube.Write(Out) ;
}