summaryrefslogtreecommitdiff
path: root/modules/dep/g3dlite/source/Ray.cpp
blob: 0436ef0b3233eb93ddebf68ca555cdcb9ae830f3 (plain)
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
/**
 @file Ray.cpp 
 
 @maintainer Morgan McGuire, http://graphics.cs.williams.edu
 
 @created 2002-07-12
 @edited  2004-03-19
 */

#include "G3D/platform.h"
#include "G3D/Ray.h"
#include "G3D/Plane.h"
#include "G3D/Sphere.h"
#include "G3D/CollisionDetection.h"

namespace G3D {

void Ray::set(const Vector3& origin, const Vector3& direction) {
	m_origin = origin;
	m_direction = direction;
	debugAssert(direction.isUnit());

	m_invDirection = Vector3::one() / direction;

	// ray slope
	ibyj = m_direction.x * m_invDirection.y;
	jbyi = m_direction.y * m_invDirection.x;
	jbyk = m_direction.y * m_invDirection.z;
	kbyj = m_direction.z * m_invDirection.y;
	ibyk = m_direction.x * m_invDirection.z;
	kbyi = m_direction.z * m_invDirection.x;

	// precomputed terms
	c_xy = m_origin.y - jbyi * m_origin.x;
	c_xz = m_origin.z - kbyi * m_origin.x;
	c_yx = m_origin.x - ibyj * m_origin.y;
	c_yz = m_origin.z - kbyj * m_origin.y;
	c_zx = m_origin.x - ibyk * m_origin.z;
	c_zy = m_origin.y - jbyk * m_origin.z;	

	//ray slope classification
	if (m_direction.x < 0) {
		if (m_direction.y < 0) {
			if (m_direction.z < 0) {
				classification = MMM;
			} else if (m_direction.z > 0) {
				classification = MMP;
			} else { //(m_direction.z >= 0)
				classification = MMO;
			}
		} else { //(m_direction.y >= 0)
			if (m_direction.z < 0) {
				if (m_direction.y == 0) {
					classification = MOM;
				} else {
					classification = MPM;
				}
			} else { //(m_direction.z >= 0)
				if ((m_direction.y == 0) && (m_direction.z == 0)) {
					classification = MOO;
				} else if (m_direction.z == 0) {
					classification = MPO;
				} else if (m_direction.y == 0) {
					classification = MOP;
				} else {
					classification = MPP;
				}
			}
		}
	} else { //(m_direction.x >= 0)
		if (m_direction.y < 0) {
			if (m_direction.z < 0) {
				if (m_direction.x == 0) {
					classification = OMM;
				} else {
					classification = PMM;
				}
			} else { //(m_direction.z >= 0)
				if ((m_direction.x == 0) && (m_direction.z == 0)) {
					classification = OMO;
				} else if (m_direction.z == 0) {
					classification = PMO;
				} else if (m_direction.x == 0) {
					classification = OMP;
				} else {
					classification = PMP;
				}
			}
		} else { //(m_direction.y >= 0)
			if (m_direction.z < 0) {
				if ((m_direction.x == 0) && (m_direction.y == 0)) {
					classification = OOM;
				} else if (m_direction.x == 0) {
					classification = OPM;
				} else if (m_direction.y == 0) {
					classification = POM;
				} else {
					classification = PPM;
				}
			} else { //(m_direction.z > 0)
				if (m_direction.x == 0) {
					if (m_direction.y == 0) {
						classification = OOP;
					} else if (m_direction.z == 0) {
						classification = OPO;
					} else {
						classification = OPP;
					}
				} else {
					if ((m_direction.y == 0) && (m_direction.z == 0)) {
						classification = POO;
					} else if (m_direction.y == 0) {
						classification = POP;
					} else if (m_direction.z == 0) {
						classification = PPO;
					} else {
						classification = PPP;
					}
				}
			}			
		}
	}
}

Ray::Ray(class BinaryInput& b) {
	deserialize(b);
}


void Ray::serialize(class BinaryOutput& b) const {
	m_origin.serialize(b);
	m_direction.serialize(b);
}


void Ray::deserialize(class BinaryInput& b) {
	m_origin.deserialize(b);
	m_direction.deserialize(b);
	set(m_origin, m_direction);
}


Ray Ray::refract(
    const Vector3&  newOrigin,
    const Vector3&  normal,
    float           iInside,
    float           iOutside) const {

    Vector3 D = m_direction.refractionDirection(normal, iInside, iOutside);
    return Ray(newOrigin + (m_direction + normal * (float)sign(m_direction.dot(normal))) * 0.001f, D);
}


Ray Ray::reflect(
    const Vector3&  newOrigin,
    const Vector3&  normal) const {

    Vector3 D = m_direction.reflectionDirection(normal);
    return Ray(newOrigin + (D + normal) * 0.001f, D);
}


Vector3 Ray::intersection(const Plane& plane) const {
    float d;
    Vector3 normal = plane.normal();
    plane.getEquation(normal, d);
    float rate = m_direction.dot(normal);

    if (rate >= 0.0f) {
        return Vector3::inf();
    } else {
        float t = -(d + m_origin.dot(normal)) / rate;
        return m_origin + m_direction * t;
    }
}


float Ray::intersectionTime(const class Sphere& sphere, bool solid) const {
    Vector3 dummy;
    return CollisionDetection::collisionTimeForMovingPointFixedSphere(
            m_origin, m_direction, sphere, dummy, dummy, solid);
}


float Ray::intersectionTime(const class Plane& plane) const {
    Vector3 dummy;
    return CollisionDetection::collisionTimeForMovingPointFixedPlane(
            m_origin, m_direction, plane, dummy);
}


float Ray::intersectionTime(const class Box& box) const {
    Vector3 dummy;
    float time = CollisionDetection::collisionTimeForMovingPointFixedBox(
            m_origin, m_direction, box, dummy);

    if ((time == finf()) && (box.contains(m_origin))) {
        return 0.0f;
    } else {
        return time;
    }
}


float Ray::intersectionTime(const class AABox& box) const {
    Vector3 dummy;
    bool inside;
    float time = CollisionDetection::collisionTimeForMovingPointFixedAABox(
            m_origin, m_direction, box, dummy, inside);

    if ((time == finf()) && inside) {
        return 0.0f;
    } else {
        return time;
    }
}

}