Visualisierung2
Visualisation.cpp
Go to the documentation of this file.
1 #include "Visualisation.h"
2 
7 Visualisation::Visualisation(int w, int h, myList* order,std::vector<unsigned int> eList, int n, int maxId)
8 {
9  //set up the variables of the visualisation
10  lindenmeyer = new L_System(w, h,"hilbert");
11  edgeList = eList;
12  visuorder = 1;
13  maximum = maxId;
14  vertexCount = n;
15  graphOrder = order;
16 
17  width = w; height = h;
18  initPoints(order, n, maxId); //initialize the points
19 
20  //initialize the buffers for drawing
21  glGenBuffers(1, &positionBuffer);
22  glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
23  glBufferData(GL_ARRAY_BUFFER, positioncount * sizeof(float), &points[0], GL_STATIC_DRAW);
24  glBindBuffer(GL_ARRAY_BUFFER, 0);
25 
26  glGenBuffers(1, &indexBuffer);
27  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
28  glBufferData(GL_ELEMENT_ARRAY_BUFFER, edgeList.size() * sizeof(unsigned int), &edgeList[0], GL_STATIC_DRAW);
29  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
30 
31  glGenVertexArrays(1, &vao);
32  glBindVertexArray(vao);
33 
34  glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
35  glEnableVertexAttribArray(0);
36  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
37 
38  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
39 
40  glBindVertexArray(0);
41  glBindBuffer(GL_ARRAY_BUFFER, 0);
42  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
43 
45 }
46 
47 
49 {
50  glDeleteBuffers(1, &positionBuffer);
51  glDeleteBuffers(1, &indexBuffer);
52  glDeleteVertexArrays(1, &vao);
53  delete lindenmeyer; lindenmeyer = nullptr;
54 }
59 void Visualisation::initPoints(myList* order, int n, int max)
60 {
61  //initialize the pointlist with zeros
62  for(int i = 0; i<max; i++)
63  {
64  glm::vec3 tmp = glm::vec3(0.0,0.0,0.0);
65  points.push_back(tmp);
66  }
67 
68  //set up the startinpositions
69  int start_x = 0;
70  int start_y = -1*height;
71  //order the vertices equidistant along a line
72  myList* tmp = order;
73  for(int i = 0; i<n; i++)
74  {
75  double x = start_x + i*(1.0/(n-1));
76  points[tmp->index-2] = glm::vec3(x, start_y, 0.0);
77  tmp = tmp->next;
78  }
80 }
86 {
87  if(visuorder > 1)
88  {
89  visuorder--; //decrease the order of the visualisation
90  calcPoints(visuorder); //calculate the mapping along the curve with this order
91  lindenmeyer->setDrawingIteration(visuorder); //set the drawing order of the curve of the order of the visualisation
92  }
93 }
98 {
99  visuorder++; //increase the order of the visualisation
100  calcPoints(visuorder); //calculate the mapping along the curve with this order
101  lindenmeyer->setDrawingIteration(visuorder);//set the drawing order of the curve of the order of the visualisation
102 }
103 
107 void Visualisation::changeType(std::string type)
108 {
109  //change the type of the curve for the visualisation
110  if(strcmp(lindenmeyer->getType().c_str(),"hilbert")==0)
111  {
112  delete lindenmeyer; //delete current lindenmeyer system
113  lindenmeyer = new L_System(height,width,type); //make it new with the given type
114 
115  calcPoints(visuorder); //calculate the points along the new curvetype
116  }
117  if(strcmp(lindenmeyer->getType().c_str(),"gosper")==0)
118  {
119  delete lindenmeyer;
120  lindenmeyer = new L_System(height,width,type);
121 
123  }
124 }
125 
129 void Visualisation::calcPoints(int iteration)
130 {
131  visupoints.clear(); //delete the current points
132 
133  for(int i = 0; i<maximum;i++) //for each Vertex, calculate the new position
134  {
135  visupoints.push_back(mapping(points[i].x, iteration));
136  }
137  //set up the buffers to the new positions
138  glGenBuffers(1, &positionBuffer);
139  glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
140  glBufferData(GL_ARRAY_BUFFER, positioncount* sizeof(float), &visupoints[0], GL_STATIC_DRAW);
141  glBindBuffer(GL_ARRAY_BUFFER, 0);
142 
143  glGenBuffers(1, &indexBuffer);
144  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
145  glBufferData(GL_ELEMENT_ARRAY_BUFFER, edgeList.size()* sizeof(unsigned int), &edgeList[0], GL_STATIC_DRAW);
146  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
147 
148  glGenVertexArrays(1, &vao);
149  glBindVertexArray(vao);
150 
151  glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
152  glEnableVertexAttribArray(0);
153  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
154 
155  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
156 
157  glBindVertexArray(0);
158  glBindBuffer(GL_ARRAY_BUFFER, 0);
159  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
160 
161 }
162 
163 /*algo from the paper
164 SFCMap (value, order) -> (x, y) : {
165 segment = floor(value * num_segments)
166 if(order == 0){
167 select the segment of the basis curve
168 calculate (x, y) along this segment
169 } else {
170 subvalue = value * num_segments - segment
171 (x’, y’) = SFCMap(subvalue, order - 1)
172 transform (x’, y’) to (x, y) in right segment
173 }
174 return (x,y)
175 }
176 */
183 glm::vec3 Visualisation::mapping(double value, int order)
184 {
185  //calculate the segmentcount of the curve
186  int maxSegmentcount = 0;
187  if(strcmp(lindenmeyer->getType().c_str(),"hilbert")==0)
188  {
189  maxSegmentcount = (int)pow(4,(order))-1;
190  }
191  if(strcmp(lindenmeyer->getType().c_str(),"gosper")==0)
192  {
193  maxSegmentcount = (int)pow(7,(order));
194  }
195 
196  //on which segment should the point be
197  int segment = (int)glm::floor(value * maxSegmentcount);
198  if(order == 0)
199  {
200  return glm::vec3(value,value,0);
201  }else {
202  double subvalue = value * maxSegmentcount - segment; //calculate the value on the curve with order = oredr -1
203  glm::vec3 pos_prime = mapping(subvalue, order-1); //recursion
204  std::vector<glm::vec3> current = lindenmeyer->getPoints(order); //get points from lindenmeyer for the interpolation
205  if(segment == maxSegmentcount)
206  {
207  return current[segment];
208  }
209 
210  glm::vec3 tmp;
211  if(order == 1)
212  {
213  tmp.x = pos_prime.x * current[segment + 1].x + (1-pos_prime.x) * current[segment].x;
214  tmp.y = pos_prime.y * current[segment + 1].y + (1-pos_prime.y) * current[segment].y;
215  //tmp.x = subvalue * current[segment + 1].x + (1-subvalue) * current[segment].x;
216  //tmp.y = subvalue * current[segment + 1].y + (1-subvalue) * current[segment].y;
217  tmp.z = 0.0;
218 
219  return tmp;
220  }
221  //float weight_x = (pos_prime.x + 0.9f)/(1.8f * 1.0f);//normalize [-0.9,0.9] to [0,1]
222  //float weight_y = (pos_prime.y + 0.9f)/(1.8f * 1.0f);
223  float weight_x = subvalue;
224  float weight_y = subvalue;
225 
226  tmp.x = weight_x * current[segment + 1].x + (1-weight_x) * current[segment].x;
227  tmp.y = weight_y * current[segment + 1].y + (1-weight_y) * current[segment].y;
228  tmp.z = 0.0;
229 
230  return tmp;
231  }
232 }
233 
240 {
241  auto baseColor_location = glGetUniformLocation(shader, "color");
242  if(positioncount < 1000)
243  {
244  glUniform4f(baseColor_location, 0.5f, 0.0f, 0.0f, 0.3f);
245  }else{
246  glUniform4f(baseColor_location, 0.5f, 0.0f, 0.0f, 0.1f);
247  }
248 
249  glBindVertexArray(vao);
250  glEnable(GL_BLEND);
251  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
252  glDrawElements(GL_LINES, edgeList.size(), GL_UNSIGNED_INT, 0);
253  glDisable(GL_BLEND);
254  if(drawPoints)
255  {
256  glUniform4f(baseColor_location, 0.0f, 0.0f, 0.5f, 1.0f);
257  glPointSize(3.0);
258  glDrawElements(GL_POINTS, edgeList.size(), GL_UNSIGNED_INT, 0);
259  glPointSize(1.0);
260  }
261  glBindVertexArray(0);
262  if(drawCurve)
263  {
264  lindenmeyer->draw(shader);
265  }
266 }
GLuint positionBuffer
Buffer fuer die Positionen der Knoten.
Definition: Visualisation.h:30
int index
Listenindex.
Definition: myList.h:7
std::vector< glm::vec3 > getPoints(int order)
Gibt die Kurveneckpunkte in der Iterationstiefe order zurueck. Sollte order groesser als die aktuell ...
Definition: L_System.cpp:219
void changeType(std::string type)
Aendert den Typ der Visualisierungskurve auf den uebergeben Typ ("hilbert" oder "gosper").
std::string getType()
Gibt den Typ der Kurve zurueck ("hilbert" oder "gosper").
Definition: L_System.cpp:190
int width
Fensterbreite.
Definition: Visualisation.h:37
void initPoints(myList *order, int n, int max)
initialisiert die Punkte fuer die Visualisierung mit der Iterationsstufe order, der Knotenanzahl n un...
int vertexCount
Anzahl der Knoten im Graph.
Definition: Visualisation.h:41
int positioncount
Anzahl der Knotenpositionen.
Definition: Visualisation.h:36
L_System * lindenmeyer
Das L-System zum errechnen der Kurvenpunkte.
Definition: Visualisation.h:45
Definition: myList.h:4
myList * graphOrder
Die Reihenfolge der Knoten nach der Tiefensuche im Clustering.
Definition: Visualisation.h:43
void calcPoints(int iteration)
bool drawCurve
Speichert Benutzereingabe ob die Kurve mit gezeichnet werden soll.
Definition: main.cpp:33
void draw(GLuint shader)
Zeichnet die aktuelle Kurve.
Definition: L_System.cpp:136
std::vector< glm::vec3 > visupoints
Koordinatenliste der Knoten auf der Kurve.
Definition: Visualisation.h:34
void setDrawingIteration(int order)
Setzt die zu zeichnende Iterationsstufe auf order falls order kleiner als die maximale Iterationsstuf...
Definition: L_System.cpp:180
bool drawPoints
Speichert Benutzereingabe ob die Knotenpunkte mit gezeichnet werden sollen.
Definition: main.cpp:34
GLuint vao
Vertex Attribute Object zum Zeichnen der Visualisierung.
Definition: Visualisation.h:29
void decreaseOrder()
Verringert die Kurvebordnung um 1.
int visuorder
Iterationsstufe die gerade gezeichnet werden soll.
Definition: Visualisation.h:39
glm::vec3 mapping(double value, int order)
Berechnet die Knotenpositionen entlang der Kurve. Value ist die Position des Knotens auf der Geraden ...
std::vector< glm::vec3 > points
Koordinatenliste der Knoten auf der Geraden.
Definition: Visualisation.h:33
GLuint shader
Enthaellt den Zeichenshader.
Definition: main.cpp:26
int height
Fensterhoehe.
Definition: Visualisation.h:38
int maximum
Maximaler Index der Graphknoten.
Definition: Visualisation.h:40
void increaseOrder()
Erhoeht die Kurvenordnung um 1.
GLuint indexBuffer
Buffer fuer die Reihenfolge der Knoten.
Definition: Visualisation.h:31
void draw(GLuint shader, bool drawPoints, bool drawCurve)
Zeichnet den Graphen mit den uebergeben Shader. Die 2 anderen Parameter bestimmen ob zusaetzlich die ...
std::vector< unsigned int > edgeList
Kantenliste aus dem Graphen.
Definition: Visualisation.h:35
myList * next
Pointer zum naechsten Listenelement.
Definition: myList.h:8
Visualisation(int w, int h, myList *points, std::vector< unsigned int > eList, int n, int maxId)
den Konstruktor uebergeben wird: Laenge und Breite des Fensters, eine Liste mit den Geclusterten Grap...