This repository was archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.cpp
154 lines (136 loc) · 4.01 KB
/
Shape.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
#include "Util.hpp"
#include <iostream>
#include <sstream>
#include <vector>
#include <GL/glew.h>
#include <GL/glut.h>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <memory>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <cfloat>
#include <set>
#include "Shape.hpp"
#include "State.hpp"
//Shape(const fv* points, const fv* colours, const fv* red, int id, v3 topCenter, Set<Id> canCollideWith,
// v3 scale=oneV, v3 motionLimiter=oneV, v3 translationMultiplier=oneV
// size, rotation, , translationMultiplier, yaw_pitch_roll_min, yaw_pitch_roll_max
Shape::Shape(const fv* points, const fv* colours, const fv* purple, const fv* green,
int id, v3 topCenter, std::set<Id> canCollideWith, v3 scale, v3 translationMultiplier, v3 rotationMultiplier) :
_cuboid(*points,topCenter,scale,translationMultiplier,rotationMultiplier), canCollideWith(canCollideWith), _colours(colours),
purple(purple), green(green), id(id), VBOs(2)
{
}
bool Shape::selected() {
return _selected;
}
void Shape::selected(bool b) {
_selected = b;
}
vv3 Shape::getEdges(const vv3& v) {
vv3 e;
const int size = v.size();
for (int i=0; i<size; i+=4) {
vv3 face(4);
face[0] = v[i+0];
face[1] = v[i+1];
face[2] = v[i+2];
face[3] = v[i+3];
const int faceSize = face.size();
for (int j=0; j<faceSize; ++j) {
e.push_back((face[j] - face[(j+1)%faceSize]));
}
}
return e;
}
// returns normalised axes
vv3 Shape::getAxes(vv3 v1, vv3 v2) {
vv3 axes;
const vv3 axes1 = v1;
const vv3 axes2 = v2;
vv3 axes3;
for (const auto& axis1: axes1) {
for (const auto& axis2: axes2) {
const auto t = glm::normalize(glm::cross(axis1,axis2));
if (!std::isnan(t.x) && !std::isnan(t.y) && !std::isnan(t.z)) {
axes3.push_back(t);
}
}
}
concat(axes, axes1);
concat(axes, axes2);
concat(axes, axes3);
return axes;
}
bool Shape::colliding(Shape& s1, Shape& s2) {
vv3 s1Verts = s1.cuboid().getUniqueEdges();
vv3 s2Verts = s2.cuboid().getUniqueEdges();
vv3 allAxes = getAxes(s1Verts, s2Verts);
auto overlap = [&] (const Projection& p1, const Projection& p2) -> bool {
return (p1.second >= p2.first) && (p1.first <= p2.second);
};
for (const auto& axis: allAxes) {
Projection projection1 = project(axis, s1.cuboid().uniqueVertices());
Projection projection2 = project(axis, s2.cuboid().uniqueVertices());
if (!overlap(projection1,projection2)) {
return false;
}
}
return true;
}
std::pair<float, float> Shape::project(const v3& axis_in, const vv3* verts_in) {
const auto& verts = *verts_in;
const v3 axis = glm::normalize(axis_in);
float min = glm::dot(axis,verts[0]);
float max = min;
for (int i = 1; i < verts.size(); i++) {
// NOTE: the axis must be normalized to get accurate projections
float p = glm::dot(axis,verts[i]);
if (p < min) {
min = p;
}
if (p > max) {
max = p;
}
}
Projection proj = std::make_pair(min, max);
return proj;
}
GLuint Shape::colourVBO() {
if (_colliding) {
return VBOs[1];
} else {
return VBOs[0];
}
}
const fv* Shape::colours() {
if (_selected) {
return green;
}
else if (_colliding) {
return purple;
} else {
return _colours;
}
}
Shape::~Shape() {
}
const fv* Shape::points() {
return _cuboid.points();
}
bool Shape::colliding(bool isColliding) {
_colliding = isColliding;
}
std::ostream& operator<<(std::ostream& stream, const Shape& s) {
//return stream << "Pos" << printVec(c.pos_) << ", ang:" << printVec(c.ang_) << ", size" << printVec(c.size_);
return stream << s.name << ": " << s._cuboid;
}
bool Shape::colliding() const {
return _colliding;
}
Cuboid& Shape::cuboid() {
return _cuboid;
}