tychoBCG3D  0.2
 All Data Structures Namespaces Files Functions Variables
mainwindow.cpp
Go to the documentation of this file.
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "glwidget.h"
4 #include "help.h"
5 
6 #include <math.h>
7 #include <stdio.h>
8 #include <QFileDialog>
9 #include <QVector>
10 #include <QDebug>
11 #include <QColorDialog>
12 #include <QMessageBox>
13 
27 MainWindow::MainWindow(QWidget *parent) :
28  QMainWindow(parent),
29  ui(new Ui::MainWindow)
30 {
31 
32  ui->setupUi(this);
33 
34  ui->progressBar->setHidden(true);
35 
36  counter = 0;
37  norm_faktor = 2.0;
38 
39  //set the default colors ui->glwidget->redAmbient = 0.92f;
40  ui->glwidget->greenAmbient = 0.58f; ui->glwidget->blueAmbient = 0.18f;
41 
42  ui->glwidget->redModel = 0.5;
43  ui->glwidget->greenModel = 0.5;
44  ui->glwidget->blueModel = 0.5;
45 
46  ui->glwidget->backroundColor.setAlpha(1.0);
47  ui->glwidget->backroundColor.setRedF(0.2);
48  ui->glwidget->backroundColor.setGreenF(0.2);
49  ui->glwidget->backroundColor.setBlueF(0.2);
50 
51  ui->glwidget->drawer = 1;
52 
53  ui->glwidget->shininess = 50;
54  ui->glwidget->point_size = 1;
55 
56  //menu
57  connect(ui->actionOpen_Point_Data, SIGNAL(triggered()), this, SLOT(openfile_point_data()));
58  connect(ui->actionOpen_STL_Data_2, SIGNAL(triggered()), this, SLOT(openfile_stl_data()));
59  connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(on_actionQuit_triggered()));
60  connect(ui->actionReset_Boundary, SIGNAL(triggered()), this, SLOT(on_actionReset_Bounding_Box_triggered()));
61  connect(ui->actionMake_Boundary, SIGNAL(triggered()), this, SLOT(on_actionMake_Boundary_Conditions_triggered()));
62  connect(ui->actionSave_Soundemitter, SIGNAL(triggered()), this, SLOT(on_actionMake_Soundemitter_triggered()));
63  connect(ui->actionSolid_Color, SIGNAL(triggered()), this, SLOT(modelColor()));
64  connect(ui->actionAmbient_Color, SIGNAL(triggered()), this, SLOT(ambientColor()));
65  connect(ui->actionBackground_Color, SIGNAL(triggered()), this, SLOT(setbackroundColor()));
66  connect(ui->actionCalcualte_Normal_Vectors, SIGNAL(triggered()), this, SLOT(calculateNormalVector()));
67  connect(ui->actionHelp_2, SIGNAL(triggered()), this, SLOT(showHelp()));
68 
69 
70  //menubar
71  connect(ui->actionOpen_File_point_data, SIGNAL(triggered()), this, SLOT(openfile_point_data()));
72  connect(ui->actionOpen_STL_Data, SIGNAL(triggered()), this, SLOT(openfile_stl_data()));
73  connect(ui->actionColor_ambient_light, SIGNAL(triggered()), this, SLOT(ambientColor()));
74  connect(ui->actionColor_model, SIGNAL(triggered()), this, SLOT(modelColor()));
75  connect(ui->actionCalculate_normal_vectors, SIGNAL(triggered()), this, SLOT(calculateNormalVector()));
76  connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(on_actionQuit_triggered()));
77  connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
78  connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
79 
80  //controls
81  connect(ui->edit_xmin, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
82  connect(ui->edit_ymin, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
83  connect(ui->edit_zmin, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
84  connect(ui->edit_xmax, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
85  connect(ui->edit_ymax, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
86  connect(ui->edit_zmax, SIGNAL(valueChanged(double)), this, SLOT(on_resize_bounding_box_text_lines()));
87  connect(ui->res_x,SIGNAL(editingFinished()),this, SLOT(alter_resolution_x()));
88  connect(ui->res_y,SIGNAL(editingFinished()),this, SLOT(alter_resolution_y()));
89  connect(ui->res_z,SIGNAL(editingFinished()),this, SLOT(alter_resolution_z()));
90  connect(ui->drawer_chooser, SIGNAL(currentIndexChanged(QString)), this, SLOT(set_drawer()));
91  connect(ui->dial, SIGNAL(sliderMoved(int)), this, SLOT(setShininess()));
92  connect(ui->dial_point_size, SIGNAL(sliderMoved(int)), this, SLOT(changePointSize()));
93 
94 }
95 
97 {
98  delete ui;
99 }
100 
107 {
108  FILE *fd;
109  char tmp_buffer[100];
110  float x, y, z, vx, vy, vz;
111  int res_x, res_y, res_z;
112  int counter = 0;
113  int triangular_facets = 0;
114 
115  ui->glwidget->stl_data[0].clear();
116  ui->glwidget->stl_data[1].clear();
117  ui->glwidget->stl_data[2].clear();
118 
119  ui->glwidget->coordiantes[0].clear();
120  ui->glwidget->coordiantes[1].clear();
121  ui->glwidget->coordiantes[2].clear();
122  ui->glwidget->coordiantes[3].clear();
123  ui->glwidget->coordiantes[4].clear();
124  ui->glwidget->coordiantes[5].clear();
125 
126  ui->glwidget->sampled_data[0].clear();
127  ui->glwidget->sampled_data[1].clear();
128  ui->glwidget->sampled_data[2].clear();
129 
130  ui->glwidget->x_min = ui->glwidget->x_max = ui->glwidget->y_min = ui->glwidget->y_max = ui->glwidget->z_min = ui->glwidget->z_max = 0.0;
132 
133  file = QFileDialog::getOpenFileName(this, tr("Open Point-Data File"),"",tr("FILE (*.txt)"));
134 
135  //Now we read the file
136  ui->glwidget->coordiantes[0].clear();
137  ui->glwidget->coordiantes[1].clear();
138  ui->glwidget->coordiantes[2].clear();
139 
140  if (file.length() != 0){
141 
142  fd = fopen(file.toUtf8().constData(),"r");
143 
144  ui->progressBar->setHidden(false);
145  ui->progressBar->setValue(0);
146 
147  fscanf(fd,"%f %f %f\n", &x_min_init, &y_min_init, &z_min_init);
148 
152 
153  fseek(fd,0,0);
154 
155  while(!feof(fd)){
156  fscanf(fd,"%s", &tmp_buffer);
157  if (strcmp(tmp_buffer,"\n") != 0) counter++;
158 
159  }
160 
161  triangular_facets = counter/6;
162 
163  counter = 0;
164 
165  fseek(fd,0,0);
166 
167  while(!feof(fd)){
168 
169  fscanf(fd,"%f %f %f %f %f %f\n", &x, &y, &z, &vx, &vy, &vz);
170 
171  ui->glwidget->coordiantes[0].append(x);
172  ui->glwidget->coordiantes[1].append(y);
173  ui->glwidget->coordiantes[2].append(z);
174 
175  ui->glwidget->coordiantes[3].append(vx);
176  ui->glwidget->coordiantes[4].append(vy);
177  ui->glwidget->coordiantes[5].append(vz);
178 
179  if (x < x_min_init) x_min_init = x;
180  if (x > x_max_init) x_max_init = x;
181  if (y < y_min_init) y_min_init = y;
182  if (y > y_max_init) y_max_init = y;
183  if (z < z_min_init) z_min_init = z;
184  if (z > z_max_init) z_max_init = z;
185 
186  counter++;
187 
188  ui->progressBar->setValue(100.0/triangular_facets*counter);
189 
190  }
191 
192  ui->progressBar->setHidden(true);
193 
194  fclose(fd);
195 
199 
200  for (int i = 0; i < ui->glwidget->coordiantes[0].size(); i++){
201 
202  ui->glwidget->coordiantes[0][i] = (ui->glwidget->coordiantes[0][i] - x_min_init - diameter_x/2.0) * (norm_faktor / diameter_x);
203  ui->glwidget->coordiantes[1][i] = (ui->glwidget->coordiantes[1][i] - y_min_init - diameter_y/2.0) * (norm_faktor / diameter_x);
204  ui->glwidget->coordiantes[2][i] = (ui->glwidget->coordiantes[2][i] - z_min_init - diameter_z/2.0) * (norm_faktor / diameter_x);
205  }
206 
207  ui->glwidget->x_min = x_min_init = (-diameter_x/2.0)*(norm_faktor / diameter_x);
208  ui->glwidget->y_min = y_min_init = (-diameter_y/2.0)*(norm_faktor / diameter_x);
209  ui->glwidget->z_min = z_min_init = (-diameter_z/2.0)*(norm_faktor / diameter_x);
210 
211  ui->glwidget->x_max = x_max_init = (diameter_x/2.0)*(norm_faktor / diameter_x);
212  ui->glwidget->y_max = y_max_init = (diameter_y/2.0)*(norm_faktor / diameter_x);
213  ui->glwidget->z_max = z_max_init = (diameter_z/2.0)*(norm_faktor / diameter_x);
214 
215  res_x = (int)(300.0);
216  res_y = (int)((300.0/diameter_x * diameter_y) + 0.5);
217  res_z = (int)((300.0/diameter_x * diameter_z) + 0.5);;
218 
219  ui->res_x->setValue(res_x);
220  ui->res_y->setValue(res_y);
221  ui->res_z->setValue(res_z);
222 
223  ui->edit_xmin->setValue(x_min_init);
224  ui->edit_ymin->setValue(y_min_init);
225  ui->edit_zmin->setValue(z_min_init);
226  ui->edit_xmax->setValue(x_max_init);
227  ui->edit_ymax->setValue(y_max_init);
228  ui->edit_zmax->setValue(z_max_init);
229 
230 
231  ui->glwidget->updateGL();
232  }
233 }
234 
240 {
241  FILE *fd;
242  char buffer[50];
243  char tmp_buffer[100];
244  int bin_or_string = 0;
245  float x, y, z;
246  float norm_x, norm_y, norm_z;
247  int res_x, res_y, res_z;
248  unsigned int counter = 0;
249  unsigned short attribute;
250  unsigned int triangular_facets;
251 
252  ui->glwidget->stl_data[0].clear();
253  ui->glwidget->stl_data[1].clear();
254  ui->glwidget->stl_data[2].clear();
255 
256  ui->glwidget->coordiantes[0].clear();
257  ui->glwidget->coordiantes[1].clear();
258  ui->glwidget->coordiantes[2].clear();
259  ui->glwidget->coordiantes[3].clear();
260  ui->glwidget->coordiantes[4].clear();
261  ui->glwidget->coordiantes[5].clear();
262 
263  ui->glwidget->sampled_data[0].clear();
264  ui->glwidget->sampled_data[1].clear();
265  ui->glwidget->sampled_data[2].clear();
266 
267  ui->glwidget->x_min = ui->glwidget->x_max = ui->glwidget->y_min = ui->glwidget->y_max = ui->glwidget->z_min = ui->glwidget->z_max = 0.0;
269 
270  x = y = z = 0.0;
271  //buffer .clear();
272 
273  file = QFileDialog::getOpenFileName(this, tr("Open STL-File"),"",tr("FILE (*.stl)"));
274 
275  //Now we read the file
276  ui->glwidget->stl_data[0].clear();
277  ui->glwidget->stl_data[1].clear();
278  ui->glwidget->stl_data[2].clear();
279 
280  if (file.length() != 0){
281 
282  ui->progressBar->setHidden(false);
283  ui->progressBar->setValue(0);
284 
285  fd = fopen(file.toUtf8().constData(),"r");
286 
287  //decide binary or text file
288  fread(&buffer, 6, sizeof(char), fd);
289  fseek(fd,0,0);
290 
291  QString tmp = "";
292 
293  for (int i = 0; i < 5; i++) tmp+=buffer[i];
294 
295  if (tmp == "solid") bin_or_string = 0;
296  if (tmp != "solid") bin_or_string = 1;
297 
298  fclose(fd);
299 
300  counter = 0;
301 
302  qDebug() << bin_or_string;
303 
304  //an ascii stl file
305  if (bin_or_string == 0){
306 
307  fd = fopen(file.toUtf8().constData(),"r");
308 
309  while(!feof(fd)){
310 
311  fscanf(fd,"%s", &tmp_buffer);
312  if (strcmp(tmp_buffer,"normal") == 0) counter++;
313  if (strcmp(tmp_buffer,"vertex") == 0) counter++;
314  }
315 
316  triangular_facets = counter;
317  counter = 0;
318 
319  fseek(fd,0,0);
320 
321  while(!feof(fd)){
322  fscanf(fd,"%s", &tmp_buffer);
323 
324  if (strcmp(tmp_buffer,"normal") == 0){
325 
326  fscanf(fd,"%f %f %f", &norm_x, &norm_y, &norm_z);
327 
328  ui->glwidget->stl_data[0].append(norm_x);
329  ui->glwidget->stl_data[1].append(norm_y);
330  ui->glwidget->stl_data[2].append(norm_z);
331 
332  counter++;
333  }
334 
335  if (strcmp(tmp_buffer,"vertex") == 0){
336 
337  fscanf(fd,"%f %f %f", &x, &y, &z);
338  counter++;
339 
340  if (counter == 1){
341 
342  x_max_init = x_min_init = x;
343  y_max_init = y_min_init = y;
344  z_max_init = z_min_init = z;
345  }
346 
347  ui->glwidget->stl_data[0].append(x);
348  ui->glwidget->stl_data[1].append(y);
349  ui->glwidget->stl_data[2].append(z);
350 
351  if (x < x_min_init) x_min_init = x;
352  if (x > x_max_init) x_max_init = x;
353  if (y < y_min_init) y_min_init = y;
354  if (y > y_max_init) y_max_init = y;
355  if (z < z_min_init) z_min_init = z;
356  if (z > z_max_init) z_max_init = z;
357 
358  counter++;
359 
360  }
361 
362  ui->progressBar->setValue(100.0/triangular_facets*counter);
363  }
364 
365  ui->progressBar->setHidden(true);
366 
367  fclose(fd);
368  }
369 
370  counter = 0;
371 
372  //a binary stl file
373  if (bin_or_string == 1){
374 
375  fd = fopen(file.toUtf8().constData(),"rb");
376 
377  //shift a couple of bits
378  fseek(fd,80,0);
379 
380  fread(&triangular_facets, 1, sizeof(unsigned int), fd);
381 
382  while(counter < triangular_facets){
383 
384  norm_x = norm_y = norm_z = 0.0;
385  x = y = z = 0.0;
386 
387  fread(&norm_x, 1, sizeof(float), fd);
388  fread(&norm_y, 1, sizeof(float), fd);
389  fread(&norm_z, 1, sizeof(float), fd);
390 
391  ui->glwidget->stl_data[0].append(norm_x);
392  ui->glwidget->stl_data[1].append(norm_y);
393  ui->glwidget->stl_data[2].append(norm_z);
394 
395  fread(&x, 1, sizeof(float), fd);
396  fread(&y, 1, sizeof(float), fd);
397  fread(&z, 1, sizeof(float), fd);
398 
399  ui->glwidget->stl_data[0].append(x);
400  ui->glwidget->stl_data[1].append(y);
401  ui->glwidget->stl_data[2].append(z);
402 
403  fread(&x, 1, sizeof(float), fd);
404  fread(&y, 1, sizeof(float), fd);
405  fread(&z, 1, sizeof(float), fd);
406 
407  ui->glwidget->stl_data[0].append(x);
408  ui->glwidget->stl_data[1].append(y);
409  ui->glwidget->stl_data[2].append(z);
410 
411  fread(&x, 1, sizeof(float), fd);
412  fread(&y, 1, sizeof(float), fd);
413  fread(&z, 1, sizeof(float), fd);
414 
415  ui->glwidget->stl_data[0].append(x);
416  ui->glwidget->stl_data[1].append(y);
417  ui->glwidget->stl_data[2].append(z);
418 
419  fread(&attribute, 1, sizeof(unsigned short), fd);
420 
421  counter++;
422 
423  ui->progressBar->setValue(100.0/triangular_facets*counter);
424 
425  if (counter == 1){
426 
427  x_max_init = x_min_init = x;
428  y_max_init = y_min_init = y;
429  z_max_init = z_min_init = z;
430  }
431 
432  if (x < x_min_init) x_min_init = x;
433  if (x > x_max_init) x_max_init = x;
434  if (y < y_min_init) y_min_init = y;
435  if (y > y_max_init) y_max_init = y;
436  if (z < z_min_init) z_min_init = z;
437  if (z > z_max_init) z_max_init = z;
438 
439  }
440 
441  ui->progressBar->setHidden(true);
442 
443  fclose(fd);
444  }
445 
449 
450  for (int i = 0; i < ui->glwidget->stl_data[0].size(); i++){
451 
452  if (i%4==0) continue;
453 
454  ui->glwidget->stl_data[0][i] = (ui->glwidget->stl_data[0][i] - x_min_init - diameter_x/2.0) * (norm_faktor / diameter_x);
455  ui->glwidget->stl_data[1][i] = (ui->glwidget->stl_data[1][i] - y_min_init - diameter_y/2.0) * (norm_faktor / diameter_x);
456  ui->glwidget->stl_data[2][i] = (ui->glwidget->stl_data[2][i] - z_min_init - diameter_z/2.0) * (norm_faktor / diameter_x);
457  }
458 
459  ui->glwidget->x_min = x_min_init = (-diameter_x/2.0)*(norm_faktor / diameter_x);
460  ui->glwidget->y_min = y_min_init = (-diameter_y/2.0)*(norm_faktor / diameter_x);
461  ui->glwidget->z_min = z_min_init = (-diameter_z/2.0)*(norm_faktor / diameter_x);
462 
463  ui->glwidget->x_max = x_max_init = (diameter_x/2.0)*(norm_faktor / diameter_x);
464  ui->glwidget->y_max = y_max_init = (diameter_y/2.0)*(norm_faktor / diameter_x);
465  ui->glwidget->z_max = z_max_init = (diameter_z/2.0)*(norm_faktor / diameter_x);
466 
467  res_x = (int)(300.0);
468  res_y = (int)((300.0/diameter_x * diameter_y) + 0.5);
469  res_z = (int)((300.0/diameter_x * diameter_z) + 0.5);;
470 
471  ui->res_x->setValue(res_x);
472  ui->res_y->setValue(res_y);
473  ui->res_z->setValue(res_z);
474 
475  ui->edit_xmin->setValue(x_min_init);
476  ui->edit_ymin->setValue(y_min_init);
477  ui->edit_zmin->setValue(z_min_init);
478  ui->edit_xmax->setValue(x_max_init);
479  ui->edit_ymax->setValue(y_max_init);
480  ui->edit_zmax->setValue(z_max_init);
481 
482  ui->glwidget->init();
483  ui->glwidget->updateGL();
484 
485  }
486 
487 }
488 
490 {
491  qApp->exit();
492 }
493 
502 
503  ui->glwidget->x_min = ui->edit_xmin->value();
504  ui->glwidget->y_min = ui->edit_ymin->value();
505  ui->glwidget->z_min = ui->edit_zmin->value();
506  ui->glwidget->x_max = ui->edit_xmax->value();
507  ui->glwidget->y_max = ui->edit_ymax->value();
508  ui->glwidget->z_max = ui->edit_zmax->value();
509 
513 
514  ui->glwidget->updateGL();
515 }
516 
522 {
523 
524  ui->glwidget->x_min = x_min_init;
525  ui->glwidget->y_min = y_min_init;
526  ui->glwidget->z_min = z_min_init;
527  ui->glwidget->x_max = x_max_init;
528  ui->glwidget->y_max = y_max_init;
529  ui->glwidget->z_max = z_max_init;
530 
531  ui->edit_xmin->setValue(x_min_init);
532  ui->edit_ymin->setValue(y_min_init);
533  ui->edit_zmin->setValue(z_min_init);
534  ui->edit_xmax->setValue(x_max_init);
535  ui->edit_ymax->setValue(y_max_init);
536  ui->edit_zmax->setValue(z_max_init);
537 
541 
542  ui->glwidget->updateGL();
543 
544 }
545 
552 {
553 
554  if ((ui->glwidget->stl_data[0].size()==0)&&(ui->glwidget->coordiantes[0].size()==0)){
555 
556  QMessageBox msgBox;
557  msgBox.setText("Please load first some data.");
558  msgBox.exec();
559  }
560 
561  if ((ui->glwidget->stl_data[0].size()!=0)||(ui->glwidget->coordiantes[0].size()!=0)){
562 
563  FILE *fd;
564  QString file_save;
565  file_save = QFileDialog::getSaveFileName(this, tr("TYCHO boundary-conditions"),"",tr("Tycho Boundary File (*.tyc)"));
566  QFileInfo file(file_save);
567 
568  if (!file_save.isEmpty()){
569 
570  if(file.suffix().isEmpty()) file_save += ".tyc";
571 
572  float sx, sy, sz;
573  int ZAX, ZAY, ZAZ;
574  int i, j, k, l;
575  int tmp;
576  int counter;
577 
578  float vertex1[3];
579  float vertex2[3];
580  float vertex3[3];
581 
582  //upper limits and values for running indices for the sample algol for stl data
583  int nu, nw;
584  int max_u, max_w, max_uw;
585  int max_ax, max_ay, max_az;
586  int max_bx, max_by, max_bz;
587 
588  //triangle distance-vectors
589  float ux, uy, uz;
590  float wx, wy, wz;
591 
592  //point on triangle
593  float px, py, pz;
594 
595  ZAX = ui->res_x->value();
596  ZAY = ui->res_y->value();
597  ZAZ = ui->res_z->value();
598 
599 
600  ui->glwidget->x_min = ui->edit_xmin->value();
601  ui->glwidget->y_min = ui->edit_ymin->value();
602  ui->glwidget->z_min = ui->edit_zmin->value();
603  ui->glwidget->x_max = ui->edit_xmax->value();
604  ui->glwidget->y_max = ui->edit_ymax->value();
605  ui->glwidget->z_max = ui->edit_zmax->value();
606 
607  diameter_x = ui->glwidget->x_max - ui->glwidget->x_min;
608  diameter_y = ui->glwidget->y_max - ui->glwidget->y_min;
609  diameter_z = ui->glwidget->z_max - ui->glwidget->z_min;
610 
611  sx = (diameter_x/(float)ZAX);
612  sy = (diameter_y/(float)ZAY);
613  sz = (diameter_z/(float)ZAZ);
614 
615  bounding = (int ***)calloc((size_t)ZAX, sizeof(int **));
616  for (k = 0; k < ZAX; k++) {
617  bounding[k] = (int **)calloc((size_t)ZAY, sizeof(int *));
618  if ( !bounding[k] ) {
619  fprintf(stderr, "Could not allocate memory.\n");
620  exit(1);
621  }
622  for (j = 0; j < ZAY; j++) {
623  bounding[k][j] = (int *)calloc((size_t)ZAZ, sizeof(int));
624  if ( !bounding[k][j] ) {
625  fprintf(stderr, "Could not allocate memory.\n");
626  exit(1);
627  }
628  }
629  }
630 
631  //Point Data sampled on a grid
632  if (ui->glwidget->coordiantes[0].size() > 0){
633 
634  ui->progressBar->setHidden(false);
635  ui->progressBar->setValue(0);
636 
637  counter = 0;
638 
639  for (l = 0; l < ui->glwidget->coordiantes[0].size(); l++) // Für jedes Partikel
640  {
641 
642  ui->glwidget->coordiantes[0][l] += -(ui->glwidget->x_min);
643  ui->glwidget->coordiantes[1][l] += -(ui->glwidget->y_min);
644  ui->glwidget->coordiantes[2][l] += -(ui->glwidget->z_min);
645 
646  i = (int)(floor(((ui->glwidget->coordiantes[0][l])/sx)));//Mit floor werden alle Partikel einer Zelle zugeordnet.Es werden also alle Partikel im selben sx der selben Zelle zugeordnet
647  j = (int)(floor(((ui->glwidget->coordiantes[1][l])/sy)));//Der nächste Gitterpunkt wird bestimmt. Mit -175 in x und y Koordinatentransformation, um Würfel ins Zentrum zu setzen.
648  k = (int)(floor(((ui->glwidget->coordiantes[2][l])/sz)));
649 
650  if ((i < 0)||(j < 0)||(k < 0)||(i > ZAX - 1)||(j > ZAY - 1)||(k > ZAZ - 1)) {
651 
652  ui->glwidget->coordiantes[0][l] += ui->glwidget->x_min;
653  ui->glwidget->coordiantes[1][l] += ui->glwidget->y_min;
654  ui->glwidget->coordiantes[2][l] += ui->glwidget->z_min;
655 
656  continue;
657  }
658 
659  bounding[i][j][k] += 1;
660 
661  ui->glwidget->coordiantes[0][l] += ui->glwidget->x_min;
662  ui->glwidget->coordiantes[1][l] += ui->glwidget->y_min;
663  ui->glwidget->coordiantes[2][l] += ui->glwidget->z_min;
664 
665  counter++;
666 
667  ui->progressBar->setValue(100.0/ui->glwidget->coordiantes[0].size()*counter);
668 
669  }
670 
671  ui->progressBar->setHidden(true);
672  }
673 
674 
675  //STL Data sampled on a grid
676  if (ui->glwidget->stl_data[0].size() > 0){
677 
678  ui->progressBar->setHidden(false);
679  ui->progressBar->setValue(0);
680 
681  counter = 0;
682 
683  // for each triangle (remember first the norm vector, then the 3 vertices
684  for (l = 0; l <= ui->glwidget->stl_data[0].size() - 4; l = l + 4)
685  {
686 
687  //just to be sure
688  vertex1[0] = ui->glwidget->stl_data[0].at(l+1)-(ui->glwidget->x_min);
689  vertex1[1] = ui->glwidget->stl_data[1].at(l+1)-(ui->glwidget->y_min);
690  vertex1[2] = ui->glwidget->stl_data[2].at(l+1)-(ui->glwidget->z_min);
691  vertex2[0] = ui->glwidget->stl_data[0].at(l+2)-(ui->glwidget->x_min);
692  vertex2[1] = ui->glwidget->stl_data[1].at(l+2)-(ui->glwidget->y_min);
693  vertex2[2] = ui->glwidget->stl_data[2].at(l+2)-(ui->glwidget->z_min);
694  vertex3[0] = ui->glwidget->stl_data[0].at(l+3)-(ui->glwidget->x_min);
695  vertex3[1] = ui->glwidget->stl_data[1].at(l+3)-(ui->glwidget->y_min);
696  vertex3[2] = ui->glwidget->stl_data[2].at(l+3)-(ui->glwidget->z_min);
697 
698  ux = vertex2[0] - vertex1[0];
699  uy = vertex2[1] - vertex1[1];
700  uz = vertex2[2] - vertex1[2];
701 
702  wx = vertex3[0] - vertex1[0];
703  wy = vertex3[1] - vertex1[1];
704  wz = vertex3[2] - vertex1[2];
705 
706  max_ax = ceil(fabs(ux/sx));
707  max_ay = ceil(fabs(uy/sy));
708  max_az = ceil(fabs(uz/sz));
709 
710  max_u = max_ax + max_ay + max_az;
711 
712  max_bx = ceil(fabs(wx/sx));
713  max_by = ceil(fabs(wy/sy));
714  max_bz = ceil(fabs(wz/sz));
715 
716  max_w = max_bx + max_by + max_bz;
717  max_uw = max_u + max_w;
718 
719  for (nu = 0; nu <= max_uw; nu++){
720  for (nw = 0; nw <= max_uw - nu; nw++){
721 
722  px = vertex1[0] + ((1.0 * nu)/max_uw) * ux + ((1.0 * nw)/max_uw) * wx;
723  py = vertex1[1] + ((1.0 * nu)/max_uw) * uy + ((1.0 * nw)/max_uw) * wy;
724  pz = vertex1[2] + ((1.0 * nu)/max_uw) * uz + ((1.0 * nw)/max_uw) * wz;
725 
726  i = (int)(floor((px/sx)));
727  j = (int)(floor((py/sy)));
728  k = (int)(floor((pz/sz)));
729 
730  if ((i < 0)||(j < 0)||(k < 0)||(i > ZAX - 1)||(j > ZAY - 1)||(k > ZAZ - 1)) continue;
731 
732  bounding[i][j][k] = 1;
733  }
734  }
735 
736  counter = counter + 4;
737 
738  ui->progressBar->setValue(100.0/ui->glwidget->stl_data[0].size()*counter);
739  }
740 
741  ui->progressBar->setHidden(true);
742  }
743 
744  //here the TYCHO boundary file is written
745  fd = fopen(file_save.toUtf8(), "w");
746  fprintf(fd,"This is a TYCHO boundary condition file.\n");
747  fprintf(fd,"X %i\n", ZAX);
748  fprintf(fd,"Y %i\n", ZAY);
749  fprintf(fd,"Z %i\n", ZAZ);
750  fseek(fd, 200, SEEK_SET);
751 
752  for (int i = 0; i < ZAX; i++) {
753  for (int j = 0; j < ZAY; j++) {
754  for (int k = 0; k < ZAZ; k++) {
755 
756  tmp = 0;
757  if (bounding[i][j][k] > 0) tmp = 1;
758  fwrite(&tmp, 1, sizeof (int), fd);
759  }
760  }
761  }
762 
763  draw_sampled_data(ZAX, ZAY, ZAZ, bounding);
764 
765  free(bounding);
766 
767  fclose(fd);
768 
769  }
770  }
771 }
772 
779 {
780 
781  if ((ui->glwidget->stl_data[0].size()==0)&&(ui->glwidget->coordiantes[0].size()==0)){
782 
783  QMessageBox msgBox;
784  msgBox.setText("Please load first some data.");
785  msgBox.exec();
786  }
787 
788  if ((ui->glwidget->stl_data[0].size()!=0)||(ui->glwidget->coordiantes[0].size()!=0)){
789 
790  FILE *fd;
791  QString file_save;
792  file_save = QFileDialog::getSaveFileName(this, tr("TYCHO soundemitter"),"",tr("Tycho Soundemitter File (*.tyc)"));
793  QFileInfo file(file_save);
794 
795  if (!file_save.isEmpty()){
796 
797  if(file.suffix().isEmpty()) file_save += ".tyc";
798 
799  float sx, sy, sz;
800  int ZAX, ZAY, ZAZ;
801  int i, j, k, l;
802  int tmp;
803  int counter;
804 
805  float vertex1[3];
806  float vertex2[3];
807  float vertex3[3];
808 
809  //upper limits and values for running indices for the sample algol for stl data
810  int nu, nw;
811  int max_u, max_w, max_uw;
812  int max_ax, max_ay, max_az;
813  int max_bx, max_by, max_bz;
814 
815  //triangle distance-vectors
816  float ux, uy, uz;
817  float wx, wy, wz;
818 
819  //point on triangle
820  float px, py, pz;
821 
822  ZAX = ui->res_x->value();
823  ZAY = ui->res_y->value();
824  ZAZ = ui->res_z->value();
825 
826 
827  ui->glwidget->x_min = ui->edit_xmin->value();
828  ui->glwidget->y_min = ui->edit_ymin->value();
829  ui->glwidget->z_min = ui->edit_zmin->value();
830  ui->glwidget->x_max = ui->edit_xmax->value();
831  ui->glwidget->y_max = ui->edit_ymax->value();
832  ui->glwidget->z_max = ui->edit_zmax->value();
833 
834  diameter_x = ui->glwidget->x_max - ui->glwidget->x_min;
835  diameter_y = ui->glwidget->y_max - ui->glwidget->y_min;
836  diameter_z = ui->glwidget->z_max - ui->glwidget->z_min;
837 
838  sx = (diameter_x/(float)ZAX);
839  sy = (diameter_y/(float)ZAY);
840  sz = (diameter_z/(float)ZAZ);
841 
842  bounding = (int ***)calloc((size_t)ZAX, sizeof(int **));
843  for (k = 0; k < ZAX; k++) {
844  bounding[k] = (int **)calloc((size_t)ZAY, sizeof(int *));
845  if ( !bounding[k] ) {
846  fprintf(stderr, "Could not allocate memory.\n");
847  exit(1);
848  }
849  for (j = 0; j < ZAY; j++) {
850  bounding[k][j] = (int *)calloc((size_t)ZAZ, sizeof(int));
851  if ( !bounding[k][j] ) {
852  fprintf(stderr, "Could not allocate memory.\n");
853  exit(1);
854  }
855  }
856  }
857 
858  //Point Data sampled on a grid
859  if (ui->glwidget->coordiantes[0].size() > 0){
860 
861  ui->progressBar->setHidden(false);
862  ui->progressBar->setValue(0);
863 
864  counter = 0;
865 
866  for (l = 0; l < ui->glwidget->coordiantes[0].size(); l++) // Für jedes Partikel
867  {
868 
869  ui->glwidget->coordiantes[0][l] += -(ui->glwidget->x_min);
870  ui->glwidget->coordiantes[1][l] += -(ui->glwidget->y_min);
871  ui->glwidget->coordiantes[2][l] += -(ui->glwidget->z_min);
872 
873  i = (int)(floor(((ui->glwidget->coordiantes[0][l])/sx)));//Mit floor werden alle Partikel einer Zelle zugeordnet.Es werden also alle Partikel im selben sx der selben Zelle zugeordnet
874  j = (int)(floor(((ui->glwidget->coordiantes[1][l])/sy)));//Der nächste Gitterpunkt wird bestimmt. Mit -175 in x und y Koordinatentransformation, um Würfel ins Zentrum zu setzen.
875  k = (int)(floor(((ui->glwidget->coordiantes[2][l])/sz)));
876 
877  if ((i < 0)||(j < 0)||(k < 0)||(i > ZAX - 1)||(j > ZAY - 1)||(k > ZAZ - 1)) {
878 
879  ui->glwidget->coordiantes[0][l] += ui->glwidget->x_min;
880  ui->glwidget->coordiantes[1][l] += ui->glwidget->y_min;
881  ui->glwidget->coordiantes[2][l] += ui->glwidget->z_min;
882 
883  continue;
884  }
885 
886  bounding[i][j][k] += 1;
887 
888  ui->glwidget->coordiantes[0][l] += ui->glwidget->x_min;
889  ui->glwidget->coordiantes[1][l] += ui->glwidget->y_min;
890  ui->glwidget->coordiantes[2][l] += ui->glwidget->z_min;
891 
892  counter++;
893 
894  ui->progressBar->setValue(100.0/ui->glwidget->coordiantes[0].size()*counter);
895 
896  }
897 
898  ui->progressBar->setHidden(true);
899  }
900 
901 
902  //STL Data sampled on a grid
903  if (ui->glwidget->stl_data[0].size() > 0){
904 
905  ui->progressBar->setHidden(false);
906  ui->progressBar->setValue(0);
907 
908  counter = 0;
909 
910  // for each triangle (remember first the norm vector, then the 3 vertices
911  for (l = 0; l <= ui->glwidget->stl_data[0].size() - 4; l = l + 4)
912  {
913 
914  //just to be sure
915  vertex1[0] = ui->glwidget->stl_data[0].at(l+1)-(ui->glwidget->x_min);
916  vertex1[1] = ui->glwidget->stl_data[1].at(l+1)-(ui->glwidget->y_min);
917  vertex1[2] = ui->glwidget->stl_data[2].at(l+1)-(ui->glwidget->z_min);
918  vertex2[0] = ui->glwidget->stl_data[0].at(l+2)-(ui->glwidget->x_min);
919  vertex2[1] = ui->glwidget->stl_data[1].at(l+2)-(ui->glwidget->y_min);
920  vertex2[2] = ui->glwidget->stl_data[2].at(l+2)-(ui->glwidget->z_min);
921  vertex3[0] = ui->glwidget->stl_data[0].at(l+3)-(ui->glwidget->x_min);
922  vertex3[1] = ui->glwidget->stl_data[1].at(l+3)-(ui->glwidget->y_min);
923  vertex3[2] = ui->glwidget->stl_data[2].at(l+3)-(ui->glwidget->z_min);
924 
925  ux = vertex2[0] - vertex1[0];
926  uy = vertex2[1] - vertex1[1];
927  uz = vertex2[2] - vertex1[2];
928 
929  wx = vertex3[0] - vertex1[0];
930  wy = vertex3[1] - vertex1[1];
931  wz = vertex3[2] - vertex1[2];
932 
933  max_ax = ceil(fabs(ux/sx));
934  max_ay = ceil(fabs(uy/sy));
935  max_az = ceil(fabs(uz/sz));
936 
937  max_u = max_ax + max_ay + max_az;
938 
939  max_bx = ceil(fabs(wx/sx));
940  max_by = ceil(fabs(wy/sy));
941  max_bz = ceil(fabs(wz/sz));
942 
943  max_w = max_bx + max_by + max_bz;
944  max_uw = max_u + max_w;
945 
946  for (nu = 0; nu <= max_uw; nu++){
947  for (nw = 0; nw <= max_uw - nu; nw++){
948 
949  px = vertex1[0] + ((1.0 * nu)/max_uw) * ux + ((1.0 * nw)/max_uw) * wx;
950  py = vertex1[1] + ((1.0 * nu)/max_uw) * uy + ((1.0 * nw)/max_uw) * wy;
951  pz = vertex1[2] + ((1.0 * nu)/max_uw) * uz + ((1.0 * nw)/max_uw) * wz;
952 
953  i = (int)(floor((px/sx)));
954  j = (int)(floor((py/sy)));
955  k = (int)(floor((pz/sz)));
956 
957  if ((i < 0)||(j < 0)||(k < 0)||(i > ZAX - 1)||(j > ZAY - 1)||(k > ZAZ - 1)) continue;
958 
959  bounding[i][j][k] = 1;
960  }
961  }
962 
963  counter = counter + 4;
964 
965  ui->progressBar->setValue(100.0/ui->glwidget->stl_data[0].size()*counter);
966  }
967 
968  ui->progressBar->setHidden(true);
969  }
970 
971  //here the TYCHO boundary file is written
972  fd = fopen(file_save.toUtf8(), "w");
973  fprintf(fd,"This is a TYCHO boundary condition file.\n");
974  fprintf(fd,"X %i\n", ZAX);
975  fprintf(fd,"Y %i\n", ZAY);
976  fprintf(fd,"Z %i\n", ZAZ);
977  fseek(fd, 200, SEEK_SET);
978 
979  for (int i = 0; i < ZAX; i++) {
980  for (int j = 0; j < ZAY; j++) {
981  for (int k = 0; k < ZAZ; k++) {
982 
983  tmp = 0;
984  if (bounding[i][j][k] > 0) tmp = 1;
985  fwrite(&tmp, 1, sizeof (int), fd);
986  }
987  }
988  }
989 
990  draw_sampled_data(ZAX, ZAY, ZAZ, bounding);
991 
992  free(bounding);
993 
994  fclose(fd);
995 
996  }
997  }
998 }
999 
1007 
1008  int res_x;
1009  int res_y;
1010  int res_z;
1011  double dia_x;
1012  double dia_y;
1013  double dia_z;
1014 
1015  dia_x = ui->edit_xmax->value()-ui->edit_xmin->value();
1016  dia_y = ui->edit_ymax->value()-ui->edit_ymin->value();
1017  dia_z = ui->edit_zmax->value()-ui->edit_zmin->value();
1018 
1019  res_x = ui->res_x->value();
1020 
1021  if (dia_x != 0.0){
1022  res_y = (int)(res_x * dia_y / dia_x);
1023  ui->res_y->setValue(res_y);
1024  }
1025  if (dia_x != 0.0){
1026  res_z = (int)(res_x * dia_z / dia_x);
1027  ui->res_z->setValue(res_z);
1028  }
1029 }
1030 
1038 
1039  int res_x;
1040  int res_y;
1041  int res_z;
1042  double dia_x;
1043  double dia_y;
1044  double dia_z;
1045 
1046  dia_x = ui->edit_xmax->value()-ui->edit_xmin->value();
1047  dia_y = ui->edit_ymax->value()-ui->edit_ymin->value();
1048  dia_z = ui->edit_zmax->value()-ui->edit_zmin->value();
1049 
1050  res_y = ui->res_y->value();
1051 
1052  if (dia_y != 0.0){
1053  res_x = (int)(res_y * dia_x / dia_y);
1054  ui->res_x->setValue(res_x);
1055  }
1056  if (dia_y != 0.0){
1057  res_z = (int)(res_y * dia_z / dia_y);
1058  ui->res_z->setValue(res_z);
1059  }
1060 }
1061 
1069 
1070  int res_x;
1071  int res_y;
1072  int res_z;
1073  double dia_x;
1074  double dia_y;
1075  double dia_z;
1076 
1077  dia_x = ui->edit_xmax->value()-ui->edit_xmin->value();
1078  dia_y = ui->edit_ymax->value()-ui->edit_ymin->value();
1079  dia_z = ui->edit_zmax->value()-ui->edit_zmin->value();
1080 
1081  res_z = ui->res_z->value();
1082 
1083  if (dia_z != 0.0){
1084  res_x = (int)(res_z * dia_x / dia_z);
1085  ui->res_x->setValue(res_x);
1086  }
1087  if (dia_z != 0.0){
1088  res_y = (int)(res_z * dia_y / dia_z);
1089  ui->res_y->setValue(res_y);
1090  }
1091 }
1092 
1099 {
1100 
1101  QColor color = QColorDialog::getColor(Qt::black, this, "Ambient Colot", QColorDialog::DontUseNativeDialog);
1102  if(color.isValid())
1103  {
1104 
1105  ui->glwidget->redAmbient = (float)color.red()/255.0;
1106  ui->glwidget->greenAmbient = (float)color.green()/255.0;
1107  ui->glwidget->blueAmbient = (float)color.blue()/255.0;
1108 
1109  ui->glwidget->init();
1110  ui->glwidget->updateGL();
1111  }
1112 }
1113 
1119 {
1120 
1121  QColor color = QColorDialog::getColor(Qt::black, this, "Model Color", QColorDialog::DontUseNativeDialog);
1122  if(color.isValid())
1123  {
1124 
1125  ui->glwidget->redModel = (float)color.red()/255.0;
1126  ui->glwidget->greenModel = (float)color.green()/255.0;
1127  ui->glwidget->blueModel = (float)color.blue()/255.0;
1128 
1129  ui->glwidget->init();
1130  ui->glwidget->updateGL();
1131  }
1132 }
1133 
1139 {
1140 
1141  QColor color = QColorDialog::getColor(Qt::black, this, "Baclground Color", QColorDialog::DontUseNativeDialog);
1142  if(color.isValid())
1143  {
1144 
1145  ui->glwidget->backroundColor = color;
1146 
1147  ui->glwidget->init();
1148  ui->glwidget->updateGL();
1149  }
1150 }
1151 
1158 
1159  int l;
1160 
1161  float vertex1[3];
1162  float vertex2[3];
1163  float vertex3[3];
1164 
1165  float ux;
1166  float uy;
1167  float uz;
1168 
1169  float wx;
1170  float wy;
1171  float wz;
1172 
1173  float xComp;
1174  float yComp;
1175  float zComp;
1176 
1177  if (ui->glwidget->stl_data[0].size()>0){
1178 
1179 
1180  for (l = 0; l <= ui->glwidget->stl_data[0].size() - 4; l = l + 4)
1181  {
1182 
1183  //just to be sure
1184  vertex1[0] = ui->glwidget->stl_data[0].at(l+1)-(ui->glwidget->x_min);
1185  vertex1[1] = ui->glwidget->stl_data[1].at(l+1)-(ui->glwidget->y_min);
1186  vertex1[2] = ui->glwidget->stl_data[2].at(l+1)-(ui->glwidget->z_min);
1187  vertex2[0] = ui->glwidget->stl_data[0].at(l+2)-(ui->glwidget->x_min);
1188  vertex2[1] = ui->glwidget->stl_data[1].at(l+2)-(ui->glwidget->y_min);
1189  vertex2[2] = ui->glwidget->stl_data[2].at(l+2)-(ui->glwidget->z_min);
1190  vertex3[0] = ui->glwidget->stl_data[0].at(l+3)-(ui->glwidget->x_min);
1191  vertex3[1] = ui->glwidget->stl_data[1].at(l+3)-(ui->glwidget->y_min);
1192  vertex3[2] = ui->glwidget->stl_data[2].at(l+3)-(ui->glwidget->z_min);
1193 
1194  ux = vertex2[0] - vertex1[0];
1195  uy = vertex2[1] - vertex1[1];
1196  uz = vertex2[2] - vertex1[2];
1197 
1198  wx = vertex3[0] - vertex1[0];
1199  wy = vertex3[1] - vertex1[1];
1200  wz = vertex3[2] - vertex1[2];
1201 
1202  xComp = uy*wz - uz*wy;
1203  yComp = uz*wx - ux*wz;
1204  zComp = ux*wy - uy*wx;
1205 
1206  ui->glwidget->stl_data[0][l] = xComp;
1207  ui->glwidget->stl_data[1][l] = yComp;
1208  ui->glwidget->stl_data[2][l] = zComp;
1209  }
1210  }
1211 
1212  ui->glwidget->updateGL();
1213 
1214 }
1215 
1226 void MainWindow::draw_sampled_data(int ZAX, int ZAY, int ZAZ, int ***bounding){
1227 
1228  ui->glwidget->x_min = ui->edit_xmin->value();
1229  ui->glwidget->y_min = ui->edit_ymin->value();
1230  ui->glwidget->z_min = ui->edit_zmin->value();
1231  ui->glwidget->x_max = ui->edit_xmax->value();
1232  ui->glwidget->y_max = ui->edit_ymax->value();
1233  ui->glwidget->z_max = ui->edit_zmax->value();
1234 
1235  ui->glwidget->sampled_data[0].clear();
1236  ui->glwidget->sampled_data[1].clear();
1237  ui->glwidget->sampled_data[2].clear();
1238 
1239  float spacing_x = (ui->glwidget->x_max - ui->glwidget->x_min)/(float)ZAX;
1240  float spacing_y = (ui->glwidget->y_max - ui->glwidget->y_min)/(float)ZAY;
1241  float spacing_z = (ui->glwidget->z_max - ui->glwidget->z_min)/(float)ZAZ;
1242 
1243  ui->progressBar->setHidden(false);
1244 
1245  for (int l = 0; l < ZAX; l++){
1246  for (int m = 0; m < ZAY; m++){
1247  for (int n = 0; n < ZAZ; n++){
1248 
1249  if (bounding[l][m][n] != 0){
1250 
1251  ui->glwidget->sampled_data[0].append(ui->glwidget->x_min + l*spacing_x);
1252  ui->glwidget->sampled_data[1].append(ui->glwidget->y_min + m*spacing_y);
1253  ui->glwidget->sampled_data[2].append(ui->glwidget->z_min + n*spacing_z);
1254 
1255  }
1256  }
1257  }
1258  ui->progressBar->setValue(100.0/ZAX*l);
1259  }
1260 
1261  ui->progressBar->setHidden(true);
1262 
1263 }
1264 
1270 
1271  if (ui->drawer_chooser->currentText().compare("Raw Data")==0) ui->glwidget->drawer = 1;
1272  if (ui->drawer_chooser->currentText().compare("Sampled Data")==0) ui->glwidget->drawer = 2;
1273 
1274  ui->glwidget->updateGL();
1275 }
1276 
1282 
1283  ui->glwidget->point_size = ui->dial_point_size->value();
1284  ui->glwidget->updateGL();
1285 
1286 }
1287 
1293 
1294  ui->glwidget->shininess = 128 - ui->dial->value();
1295  ui->glwidget->updateGL();
1296 }
1297 
1303 
1304  myhelp = new help;
1305  myhelp->setWindowModality(Qt::ApplicationModal);
1306  myhelp->setWindowFlags(Qt::WindowStaysOnTopHint);
1307  myhelp->show();
1308 
1309 }
1310 
1316 
1317  myAbout = new about;
1318  myAbout->setWindowModality(Qt::ApplicationModal);
1319  myAbout->setWindowFlags(Qt::WindowStaysOnTopHint);
1320  myAbout->show();
1321 
1322 }