-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_manager.cpp
261 lines (225 loc) · 7.26 KB
/
scene_manager.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
261
#include "scene_manager.h"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
static void display_element (Element *e) { e->draw(); }
/* Main display function */
void Scene::display () {
std::for_each (elements.begin(), elements.end(), display_element);
}
void Scene::clearScene(){
elements.clear();
verts.clear();
}
/**********************************************************
* Vertex Functions *
**********************************************************/
/* Adds a new vertex to the scene, returns a pointer to that vertex */
Vertex *Scene::addVertex3f (GLfloat x, GLfloat y, GLfloat z, int r ) {
Vertex *vertex;
if(r == 1)
vertex = new Vertex(x, y, z, 0.f, 1);
else
vertex = new Vertex(x,y,z,0.f);
if (vertex) {
elements.push_back (vertex);
verts.push_back (vertex);
vertexes[vertex->id] = vertex;
}
count++;
return vertex;
}
Vertex *Scene::addVertex (glm::vec4 vector) {
return addVertex3f (vector.x, vector.y, vector.z, 0);
}
/* Gets the Vertex object associated with vert v and returns a pointer to it,
returns NULL if the vert is invalid */
list<Vertex*> &Scene::getVertexList () {
return verts;
}
/* Guesses at the best selection vertex based on the coordinates */
Vertex *Scene::selectVertex (int x, int y) {
return verts.front();
}
/* Removes the vertex at the index specified, returning true if successful.
Also destroys any dependent elements
Returns false when there is no entry at this location */
void Scene::removeVertex (Vertex *vertex) {
if (vertex) {
elements.remove (vertex);
verts.remove (vertex);
vertexes[vertex->id] = NULL;
delete vertex;
}
}
/**********************************************************
* Element Functions *
**********************************************************/
/* Create functions return NULL if the verts are not found, or memory allocation fails
otherwise a pointer to the element created is returned. These elements are self-
drawing once created. */
/* a simple line between two verts */
Line *Scene::createLine (Vertex *a, Vertex *b) {
Line *line = NULL;
if (a && b) {
line = new Line(a, b);
elements.push_back (line);
}
return line;
}
/* static helper for angle checks (must share one and only one vertex) */
static enum LM validAngle (Line &a, Line &b) {
enum LM elm = LM_NONE;
if (&a && &b) {
if (a.a == b.a)
elm = LM_AA;
if (a.b == b.b) {
if (elm)
return LM_NONE;
else
elm = LM_BB;
}
if (a.a == b.b) {
if (elm)
return LM_NONE;
else
elm = LM_AB;
}
if (a.b == b.a) {
if (elm)
return LM_NONE;
else
elm = LM_BA;
}
}
return elm;
}
/* an angle (number) is displayed for the lines defined. The lines must share a vertex, or the
angle will return NULL */
Angle *Scene::createAngle (Line *a, Line *b) {
Angle *angle = NULL;
enum LM elm = validAngle (*a, *b);
if (elm > LM_NONE) {
angle = new Angle (a, b, elm);
elements.push_back (angle);
}
return angle;
}
/* an ellipse is drawn at the foci calculated from the midpoints of the lines provided. */
Fano *Scene::createFano (Line *a, Line *b, Line *c) {
Fano *fano = NULL;
if (validAngle (*a,*b) && validAngle (*b,*c) && validAngle (*c,*a)) {
fano = new Fano (a, b, c);
elements.push_back (fano);
}
return fano;
}
/* Displays a specific element (used by the display function on each element) */
void Scene::displayElement (Element *e) {
display_element (e);
}
/* Removes lines, angles, circles, etcetera from the scene */
void Scene::removeElement (Element *e) {
elements.remove (e);
}
/**********************************************************************
* FILE PARSING *
**********************************************************************/
/* load the file, parse the text, add the elements */
bool Scene::loadFile (std::string filename) {
std::ifstream fs;
fs.open (filename);
if (!fs.is_open ()) {
fs.close ();
return false;
}
clearScene();
int vertNum = 0;
int lineNum = 0;
int angleNum = 0;
int fanoNum = 0;
Vertex * newVerts[1024];
Line * newLines[1024];
int vertIdx = 0;
int lineIdx = 0;
std::cout<< "Reading in file " << filename << std::endl;
std::string line;
while(std::getline(fs, line)){
//line that includes data
if(line.length() > 0 && (line.at(0) == 'V' || line.at(0) == 'v')){
char *copyLine = (char*)line.c_str();
char *token;
token = strtok(copyLine, " ,");
token = strtok(NULL, " ,");
float x, y, z;
x = atof(token);
token = strtok(NULL, " ,");
y = atof(token);
token = strtok(NULL, " ,");
z = atof(token);
if(vertIdx == 0)
newVerts[vertIdx++] = addVertex3f(x, y, z, 1);
else
newVerts[vertIdx++] = addVertex3f(x, y, z, 0);
}
else if(line.length() > 0 && (line.at(0) == 'L' || line.at(0) == 'l')){
char *copyLine = (char*)line.c_str();
char *token;
token = strtok(copyLine, " ,");
token = strtok(NULL, " ,");
int v1, v2;
v1 = atoi(token);
token = strtok(NULL, " ,");
v2 = atoi(token);
newLines[lineIdx++] = createLine(newVerts[v1], newVerts[v2]);
}
else if(line.length() > 0 && (line.at(0) == 'A' || line.at(0) == 'a')){
char *copyLine = (char*)line.c_str();
char *token;
token = strtok(copyLine, " ,");
token = strtok(NULL, " ,");
int l1, l2;
l1 = atoi(token);
token = strtok(NULL, " ,");
l2 = atoi(token);
createAngle(newLines[l1], newLines[l2]);
}
else if(line.length() > 0 && (line.at(0) == 'F' || line.at(0) == 'f')){
char *copyLine = (char*)line.c_str();
char *token;
token = strtok(copyLine, " ,");
token = strtok(NULL, " ,");
int a1, a2, a3;
a1 = atoi(token);
token = strtok(NULL, " ,");
a2 = atoi(token);
token = strtok(NULL, " ,");
a3 = atoi(token);
createFano(newLines[a1], newLines[a2], newLines[a3]);
}
else if(line.length()>0 && (line.at(0)=='C' || line.at(0)=='c')){
char *copyLine = (char*)line.c_str();
char *token;
token = strtok(copyLine, " ,");
token = strtok(NULL, " ,");
int l;
float r, g, b;
l = atoi(token);
token = strtok(NULL, " ,");
r = atof(token);
token = strtok(NULL, " ,");
g = atof(token);
token = strtok(NULL, " ,");
b = atof(token);
Line *a = newLines[l];
a->setColor3f(r,g,b);
}
}
//...
return true;
}