-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRay.cpp
81 lines (60 loc) · 1.7 KB
/
Ray.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
//
// Created by Tizian Rettig on 09.12.2020.
//
#include "Ray.h"
#include <exception>
#include <sstream>
Ray::Ray() {
m_line = CLine();
}
Ray::Ray(const Vec &origin, const Vec &direction) {
m_line = CLine(origin, direction);
m_end = m_line.pointAt(MAX_CASTING_DISTANCE);
m_length = length(m_end - getOrigin());
}
std::string Ray::toString() const {
std::stringstream ss;
// ss << getOrigin().toString() << "->" << m_end.toString() << " m_length: " << m_length << std::endl;
return ss.str();
}
const Vec &Ray::getOrigin() const {
return m_line.getP1();
}
const Vec* Ray::getEnd() const {
return &m_end;
}
const float &Ray::getLength() const {
return m_length;
}
const Vec &Ray::getDirection() const {
return m_line.getDirection();
}
const CLine &Ray::getLine() const {
return m_line;
}
const int & Ray::getIntersectionIdx() const {
return m_intersection_idx;
}
bool Ray::intersectWorld(Geometry** world, const int& size) {
if (!m_hasIntersected) {
m_hasIntersected = true;
float distance;
Vec intersection_point;
for ( int i = 0; i < size; i++ ) {
if (world[i]->isIntersectingAt(m_line, &intersection_point)) {
//intersection_point = world[i]->firstIntersectionPoint(m_line);
distance = length(intersection_point -getOrigin());
if (distance < m_length) {
m_end = intersection_point;
m_length = distance;
m_intersection_idx = i;
}
}
}
return true;
}
return false;
}
bool Ray::hasIntersections() const {
return m_hasIntersected && m_intersection_idx >= 0;
}