File: standardDatasets.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (1299 lines) | stat: -rw-r--r-- 37,571 bytes parent folder | download | duplicates (4)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/DATATYPE/standardDatasets.h>
#include <BALL/VIEW/WIDGETS/datasetControl.h>
#include <BALL/VIEW/WIDGETS/scene.h>
#include <BALL/VIEW/KERNEL/common.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/KERNEL/message.h>
#include <BALL/VIEW/PRIMITIVES/mesh.h>
#include <BALL/VIEW/PRIMITIVES/quadMesh.h>

#include <BALL/VIEW/DIALOGS/snapShotVisualisation.h>
#include <BALL/VIEW/DIALOGS/contourSurfaceDialog.h>
#include <BALL/VIEW/DIALOGS/fieldLinesDialog.h>
#include <BALL/VIEW/DIALOGS/gridVisualizationDialog.h>

#include <BALL/FORMAT/trajectoryFile.h>
#include <BALL/FORMAT/trajectoryFileFactory.h>
#include <BALL/FORMAT/DSN6File.h>
#include <BALL/FORMAT/CCP4File.h>
#include <BALL/FORMAT/amiraMeshFile.h>
#include <BALL/MOLMEC/COMMON/snapShotManager.h>
#include <BALL/DATATYPE/contourSurface.h>
#include <BALL/DOCKING/COMMON/dockResult.h>
#include <BALL/DATATYPE/regularData3D.h>

#include <QtWidgets/QFileDialog>

using namespace std;

namespace BALL
{
	/////////////////////////////////////////////
	// Specialization for the default float type of TRegularData3D
	// possibly not the best location

	template<>
	void TRegularData3D<float>::binaryWriteRaw(const String& filename) const
	{	
		String coreName = filename;
		String::size_type dotIndex = filename.find_last_of(".");
		if (dotIndex != string::npos)
		{
			coreName = filename.getSubstring(0, dotIndex);				
		}
		
		String headerFN = coreName + ".vrt.vh";
		Log.info() << "Creating grid header '" << headerFN << "'" << std::endl;
		
		std::ofstream header(headerFN.c_str());
		if(!header.is_open()) 
		{
			throw Exception::FileNotFound(__FILE__, __LINE__, filename+".header");
		}	

		// dimensions
		header << size_.x << " " << size_.y << " " << size_.z << " ";
		// bit depth, for float datasets used to determine TFF table resolution
		header << 8 << " ";
		// voxel sizes
		header << spacing_.x << " " << spacing_.y << " " << spacing_.z << " ";		

		// unused header information
		//header << data_.size() << " ";
		//header << origin_.x << " " << origin_.y << " " << origin_.z << " ";
		//header << dimension_.x << " " << dimension_.y << " " << dimension_.z << " ";		

		header.close();

		Log.info() << "Header written" << std::endl;

		String rawFileFN = coreName + ".vrt";
		Log.info() << "Creating raw file '" << rawFileFN << "'" << std::endl;

		std::ofstream rawFile(rawFileFN.c_str(), std::ios::out | std::ios::binary);
		if(!rawFile.is_open())
		{
			throw Exception::FileNotFound(__FILE__, __LINE__, filename+".raw");
		}
		
		for (unsigned int i = 0; i < data_.size(); i++)
		{
			rawFile.write((const char*)&data_[i], sizeof(float));
		}
		
		rawFile.close();

		Log.info() << "Raw data written" << std::endl;		
		Log.info() << "File saved succesfully" << std::endl;
	}	

	/////////////////////////////////////////////

	namespace VIEW
	{

		// move this somewhere else ????
		Representation* createSphere(RegularData3D& grid)
		{
			int num_lines_theta = 50;
			int num_lines_phi   = 50;
			float theta_start   = (float) (2./9 * BALL::Constants::PI);

			Vector3 origin = grid.getOrigin();

			Vector3 vw(1,0,0); 
			Scene* scene = Scene::getInstance(0);
			if (scene) vw = -scene->getStage()->getCamera().getViewVector();

			// rotate the z-vector onto the look_at - vector
			Matrix4x4 rotation;
			vw.normalize();
			
			Vector3 axis = vw % Vector3(0,0,1);

			if (axis.getLength() > 1e-6)
			{
					rotation.setRotation(Angle(-acos(vw.z)), axis);
			}
			else
			{
					rotation.setIdentity();
			}

			Vector3 center = origin + grid.getDimension() * 0.5;
			RegularData3D::IndexType size = grid.getSize();
			Vector3 x = grid.getCoordinates(RegularData3D::IndexType(size.x - 1, 0, 0)) - origin;
			Vector3 y = grid.getCoordinates(RegularData3D::IndexType(0, size.y - 1, 0)) - origin;
			Vector3 z = grid.getCoordinates(RegularData3D::IndexType(0, 0, size.z - 1)) - origin;
			float min = std::min(std::min(x.getLength(), y.getLength()), z.getLength());
			min /= 2.;
			min -= 0.01;

			if (min < 0) return 0;

			float radius = min;
			float delta_phi   = (float) (2 * Constants::PI / (num_lines_phi-1));
			float delta_theta = (float) (Constants::PI / (num_lines_theta-1));

			int theta_start_index = (int)(theta_start / Constants::PI * num_lines_theta);
			vector<vector<Vector3> > points_on_sphere(num_lines_theta-theta_start_index);

			for (Index k = theta_start_index; k < num_lines_theta; k++)
			{
				for (Index i = 0; i < num_lines_phi; i++)
				{
					float phi   = i*delta_phi;
					float theta = k*delta_theta;

					Vector3 current_point;
					current_point.x = radius*cos(phi)*sin(theta);
					current_point.y = radius*sin(phi)*sin(theta);
					current_point.z = radius*cos(theta);
					
					Vector4 transformed_point(current_point.x, current_point.y, current_point.z, 1.);
					transformed_point = rotation*transformed_point;
					current_point.x = transformed_point.x;
					current_point.y = transformed_point.y;
					current_point.z = transformed_point.z;

					current_point += center;

					points_on_sphere[k-theta_start_index].push_back(current_point);        
				}
			}

			QuadMesh* qm = new QuadMesh();

			int num_theta = points_on_sphere.size();
			int num_phi   = points_on_sphere[0].size();

			for (int current_theta = 0; current_theta < num_theta; current_theta++)
			{
				for (int current_phi = 0; current_phi < num_phi; current_phi++)
				{
					Vector3& vc = points_on_sphere[current_theta][current_phi];
					qm->vertex.push_back(vc);
					Vector3 vx = -(vc - center);
					vx.normalize();
					qm->normal.push_back(vx);

					if (current_theta != num_theta-1)
					{
						Index next_theta = current_theta+1;
						Index next_phi = current_phi+1;
						if (current_phi == num_phi-1) next_phi = 0;

						qm->quad.push_back(QuadMesh::Quadruple(current_theta*num_phi + current_phi,
																										current_theta*num_phi + next_phi,
																										next_theta*num_phi    + next_phi,
																										next_theta*num_phi    + current_phi));
					}
				}
			}

			qm->colors.clear();
			qm->colors.push_back(ColorRGBA(0,0,1.));

			Representation* rep = new Representation();
			rep->insert(*qm);
			return rep;
		}


		// move this somewhere else ????
		VectorGrid* createVectorGrid(RegularData3D& potential)
		{
			RegularData3D::CoordinateType spacing = potential.getSpacing();
			RegularData3D::IndexType         size = potential.getSize();

			VectorGrid* grid_ptr = new VectorGrid(potential.getOrigin(), potential.getDimension(), spacing);
			VectorGrid& gradient_grid = *grid_ptr;

			TRegularData3D<Vector3>::IndexType index;

			for (index.x = 0; index.x < size.x; index.x++)
			{
				for (index.y = 0; index.y < size.y; index.y++)
				{
					for (index.z = 0; index.z < size.z; index.z++)
					{
						RegularData3D::IndexType next, last;

						next.x = index.x; next.y = index.y; next.z = index.z;
						last = next;

						float factor = 1;
						if (index.x == 0)
						{
							// onlx forward difference possible
							next.x++;
						} 
						else if (index.x == size.x-1) {
							// onlx backward difference possible
							last.x--;
						}	
						else {
							// mid point formula
							next.x++;
							last.x--;
							factor = 0.5;
						}

						gradient_grid[index].x = factor * spacing.x * (potential[next] - potential[last]);
						factor = 1; next.x = index.x; next.y = index.y; next.z = index.z; last = next;

						if (index.y == 0)
						{
							// only forward difference possible
							next.y++;
						} 
						else if (index.y == size.y-1) {
							// only backward difference possible
							last.y--;
						}	
						else {
							// mid point formula
							next.y++;
							last.y--;
							factor = 0.5;
						}

						gradient_grid[index].y = factor * spacing.y * (potential[next] - potential[last]);
						factor = 1; next.x = index.x; next.y = index.y; next.z = index.z; last = next;

						if (index.z == 0)
						{
							// only forward difference possible
							next.z++;
						} 
						else if (index.z == size.z-1) {
							// only backward difference possible
							last.z--;
						}	
						else {
							// mid point formula
							next.z++;
							last.z--;
							factor = 0.5;
						}

						gradient_grid[index].z = factor * spacing.z * (potential[next] - potential[last]);
					}
				}
			}

			return grid_ptr;
		}


		/////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////

		String RegularData3DController::type = "3D Grid";

		RegularData3DController::RegularData3DController()
			: DatasetController(),
				dialog_(0),
				grid_dialog_(0)
		{
			type_ = type;
			file_formats_.push_back("3dg");
			file_formats_.push_back("ccp4");
			file_formats_.push_back("omap");
			file_formats_.push_back("map");
			file_formats_.push_back("dsn6");
			file_formats_.push_back("am");
			setIdentifier("RegularData3DController");
			registerThis();
		}

		RegularData3DController::RegularData3DController(RegularData3DController& dc)
			: DatasetController(dc),
				dialog_(0)
		{
			setIdentifier("RegularData3DController");
			registerThis();
		}

		RegularData3DController::~RegularData3DController()
		{
		}

		bool RegularData3DController::write()
		{
			Dataset* set = getSelectedDataset();
			if (set == 0) return 0;

			QString file = QFileDialog::getSaveFileName(0,
			QString("Save as ") + type_.c_str(),
			getDatasetControl()->getWorkingDir().c_str(),
			QString("( *.3dg)"));

			if (file == QString::null) return false;

			vector<String> fields;
			String s(ascii(file));
			s.split(fields, ".");
			if (fields.size() == 0)
			{
				BALLVIEW_DEBUG
				return false;
			}

			return write(set, fields[fields.size() - 1], ascii(file));
		}

		bool RegularData3DController::write(Dataset* set, String /*filetype*/, String filename)
		{
			if (getData(set) == 0) return false;
			getData(set)->binaryWrite(filename);
			return true;
		}

		Dataset* RegularData3DController::open(String filetype, String filename)
		{
			RegularData3D* d3 = new RegularData3D;
			RegularData3DDataset* set = 0;
			bool read_success = false;
			String type = type_;
			String maptype = "3DG";
			if(filetype == "ccp4" || filetype == "map")
			{
				CCP4File* ccp4file = new CCP4File(filename);
				read_success = ccp4file->read(*d3);
				ccp4file->close();
				//type = "Electron Density";
				type = RegularData3DController::type;
				maptype = "CCP4";
			}
			else if(filetype == "omap" || filetype == "dsn6")
			{
				DSN6File* dsn6file = new DSN6File(filename);
				read_success = dsn6file->read(*d3);
				dsn6file->close();
				//type = "Electron Density";
				type = RegularData3DController::type;
				maptype = "DSN6";
			}
			else if(filetype == "am")
			{
				AmiraMeshFile* amirafile = new AmiraMeshFile(filename);
				read_success = amirafile->read(*d3);
				amirafile->close();
				type = RegularData3DController::type;
				maptype = "AMIRA";
			}
			else
			{
				read_success = true;
				(*d3).binaryRead(filename);
			}


			if (read_success)
			{
				set = new RegularData3DDataset;
				set->setData(d3);
				String mapname = getNameFromFileName_(filename);
				set->setName(mapname);
				set->setType(type);
				float dens_mean = d3->calculateMean();
				float dens_stddev = d3->calculateSD();
				setStatusbarText(tr("Successfully read ") + type.c_str() + tr(" from ") + maptype.c_str() + tr(" file: ") + filename.c_str(), true);
				Log.info() << mapname << " Map Density Mean: " << dens_mean << endl;
				Log.info() << mapname << " Map Density Sigma: " << dens_stddev << endl;
			}
			else
			{
				setStatusbarText("Error reading " + type + " from " + maptype + "file: " + filename, true);
			}
			return set;
		}

		RegularData3D* RegularData3DController::getData(Dataset* set)
		{
			RegularData3DDataset* dset = dynamic_cast<RegularData3DDataset*>(set);
			if (dset == 0) return 0;
			return dset->getData();
		}

		void RegularData3DController::deleteDataset_(Dataset* set)
		{
			RegularData3D* data = getData(set);
			if (data == 0) return;
			delete data;
		}

		QMenu* RegularData3DController::buildContextMenu(QTreeWidgetItem* item)
		{
			QMenu* menu = DatasetController::buildContextMenu(item);
			if (menu == 0) return 0;
			
			// In case the dataset is a non-orthogonal RegData3D, for now only allow calculation of contour surfaces
			bool is_ortho = true;
			RegularData3D* data = getData(getSelectedDataset());
			if (data != 0)
			{
				is_ortho = data->isOrthogonal();
			}
			menu->addAction("Render Grid", this, SLOT(visualizeGrid()))->setEnabled(is_ortho);
			menu->addAction("Create Sphere", this, SLOT(createSphere()))->setEnabled(is_ortho);
			menu->addAction("Render Contour Surface", this, SLOT(computeIsoContourSurface()));
			menu->addAction("Resize for Rendering", this, SLOT(resizeGrid()))->setEnabled(is_ortho);
			menu->addSeparator();
			menu->addAction("Create normalized Grid", this, SLOT(createHistogramGrid()))->setEnabled(is_ortho);
			menu->addAction("Create Gradient Grid", this, SLOT(createVectorGrid()))->setEnabled(is_ortho);
			
			// NOTE: raytraceable grids are deferred until 1.4/2.0
			// menu->addAction("Create raytraceable Grid", this, SLOT(createRaytraceableGrid()));

			return menu;
		}

		bool RegularData3DController::createMenuEntries()
		{
			if (!DatasetController::createMenuEntries()) return false;
			
			String description = "Shortcut|Tools|Grid|Calculate_normalized_grid";
			insertMenuEntry_(MainControl::TOOLS_GRID, tr("Calculate normalized Grid"), SLOT(createHistogramGrid()),
							description, QKeySequence(), tr("Create a new grid with a histogram equalization"),
							UIOperationMode::MODE_ADVANCED);

			description = "Shortcut|Tools|Grid|Resize_for_Rendering";
			insertMenuEntry_(MainControl::TOOLS_GRID, tr("Resize for Rendering"), SLOT(resizeGrid()),
							description, QKeySequence(), tr("Resize a grid for rendering"),
							UIOperationMode::MODE_ADVANCED);

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS_GRID, UIOperationMode::MODE_ADVANCED);

			// visualizations:
			description = "Shortcut|Tools|Grid|Visualize_3D_Grid";
			insertMenuEntry_(MainControl::TOOLS_GRID, tr("Visualize 3D Grid"), SLOT(visualizeGrid()),
							description, QKeySequence(), tr("Visualize a grid"),
							UIOperationMode::MODE_ADVANCED);

			description = "Shortcut|Tools|Grid|Render_Contour_Surface";
			insertMenuEntry_(MainControl::TOOLS_GRID, "Render Contour S&urface", SLOT(computeIsoContourSurface()),
							description, QKeySequence("Ctrl+U"),
							tr("Calculate an isocontour surface from a 3D grid. The grid has to be loaded in the DatasetControl."),
							UIOperationMode::MODE_ADVANCED);

			description = "Shortcut|Tools|Grid|Create_a_Sphere";
			insertMenuEntry_(MainControl::TOOLS_GRID, "Create a sphere", SLOT(createSphere()),
							description, QKeySequence(), tr("Create a sphere for the grid, e.g. for colorizing"),
							UIOperationMode::MODE_ADVANCED);

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS_GRID, UIOperationMode::MODE_ADVANCED);

			return true;
		}


		bool RegularData3DController::createVectorGrid()
		{
			Dataset* old_set = getSelectedDataset();
			RegularData3D* grid3d = getData(old_set);
			if (grid3d == 0) 
			{
				BALLVIEW_DEBUG
				return false;
			}

			VectorGrid* vgrid = VIEW::createVectorGrid(*grid3d);

			VectorGridDataset* new_set = new VectorGridDataset();
			new_set->setComposite(old_set->getComposite());
			new_set->setName(old_set->getName());
			new_set->setType(VectorGridController::type);
			new_set->setData(vgrid);
			DatasetMessage* msg = new DatasetMessage(new_set, DatasetMessage::ADD);
			getDatasetControl()->getMainControl()->sendMessage(*msg);
			return true;
		}

		bool RegularData3DController::createRaytraceableGrid()
		{
			Dataset* old_set = getSelectedDataset();
			RegularData3D* grid3d = getData(old_set);

			RaytraceableGrid* new_grid = new RaytraceableGrid(grid3d);
			RaytraceableGridDataset* new_set = new RaytraceableGridDataset();
			new_set->setComposite(old_set->getComposite());
			new_set->setType(RaytraceableGridController::type);
			new_set->setName(old_set->getName());
			new_set->setData(new_grid);
			DatasetMessage* msg = new DatasetMessage(new_set, DatasetMessage::ADD);
			getDatasetControl()->getMainControl()->sendMessage(*msg);
			return true;
		}

		void RegularData3DController::visualizeGrid()
		{
			Dataset* data = (Dataset*)getSelectedDataset();
			if (getData(data) == 0)
			{
				BALLVIEW_DEBUG
				return;
			}

			if (grid_dialog_ == 0) 
			{
				grid_dialog_ = new GridVisualizationDialog(getDatasetControl());
			}
			grid_dialog_->setController(this);
			grid_dialog_->setGrid(getData(data));
			grid_dialog_->exec();
		}

		
		void RegularData3DController::createSphere()
		{
			Dataset* data = (Dataset*)getSelectedDataset();
			if (getData(data) == 0)
			{
				BALLVIEW_DEBUG
				return;
			}

			RegularData3D* grid = getData(data);
			Representation* rep = VIEW::createSphere(*grid);
			getDatasetControl()->getMainControl()->insert(*rep);
			getDatasetControl()->getMainControl()->update(*rep);
		}

		void RegularData3DController::computeIsoContourSurface()
		{
			// execute the surface dialog and abort if cancel is clicked
			if (dialog_ == 0) 
			{
				dialog_ = new ContourSurfaceDialog(getDatasetControl(), "ContourSurfaceDialog");
				dialog_->setController(this);
			}
			dialog_->setGrid(getSelectedDataset());
			if (!dialog_->exec()) return;

			Dataset* data = dialog_->getGrid();
			if (data == 0) return;

			if (!computeIsoContourSurface(
				*data, dialog_->getColor(), dialog_->getThreshold()))
			{
				dialog_->exec();
			}
		}

		bool RegularData3DController::computeIsoContourSurface(Dataset& data, 
																													const ColorRGBA& color, float value) 
		{
			RegularData3D* grid = getData(&data);
			if (grid == 0) return false;
			// Create a new contour surface.
			ContourSurface cs(*grid, value);

			if (cs.vertex.size() == 0)
			{
				setStatusbarText(tr("Could not calculate ContourSurface, no grid points found for threshold!"), true);
				return false;
			}

			Mesh* mesh = new Mesh;
			
			// reorient the vertices for OpenGL
			for (Position t = 0; t < cs.triangle.size(); t++)
			{
				const Mesh::Triangle& org = cs.triangle[t];
				Mesh::Triangle tri;
				tri.v1 = org.v1;
				tri.v2 = org.v3;
				tri.v3 = org.v2;
				mesh->triangle.push_back(tri);
			}

			mesh->vertex = cs.vertex;
			mesh->normal = cs.normal;

			mesh->colors.clear(); 
			mesh->colors.push_back(color);

			//////////////////////////////////////////////
			// Create a new representation containing the contour surface.
			Representation* rep = new Representation();
			rep->insert(*mesh);
			rep->setModelType(MODEL_CONTOUR_SURFACE); 

			list<const Composite*> composites;
			if (data.getComposite() != 0) composites.push_back(data.getComposite());
			rep->setComposites(composites);

			// Make sure BALLView knows about the new representation.
			getMainControl()->insert(*rep);
			getMainControl()->getRepresentationManager().focusRepresentation(*rep);
			getMainControl()->update(*rep);

			return true;
		}


		Size RegularData3DController::getNextPowerOfTwo_(Size in) const
		{
			Size test = 2;
			while (test < in)
			{
				test = test * 2;
			}

			return test;
		}

		bool RegularData3DController::isGridSizePowerOfTwo(const RegularData3D& grid) const
		{
			RegularData3D::IndexType size = grid.getSize();
			RegularData3D::IndexType new_size;
			new_size.x = getNextPowerOfTwo_(size.x);
			new_size.y = getNextPowerOfTwo_(size.y);
			new_size.z = getNextPowerOfTwo_(size.z);
			return (new_size.x == size.x &&
							new_size.y == size.y &&
							new_size.z == size.z);
		}

		RegularData3D* RegularData3DController::resizeGrid(const RegularData3D& grid) 
		{
			RegularData3D::IndexType size = grid.getSize();
			RegularData3D::IndexType new_size;
			new_size.x = getNextPowerOfTwo_(size.x);
			new_size.y = getNextPowerOfTwo_(size.y);
			new_size.z = getNextPowerOfTwo_(size.z);
			if (new_size.x == size.x &&
					new_size.y == size.y &&
					new_size.z == size.z)
			{
				setStatusbarText(tr("Grid does not need to be resized!"), true);
				return 0;
			}

			// make sure new grid is a little bit smaller to prevent problems
			// with getInterpolatedValue
			Vector3 origin = grid.getOrigin();
			Vector3 dim = grid.getDimension();
			Vector3 epsilon = dim;
			epsilon.normalize();
			epsilon *= 0.0001;
			origin += epsilon;
			dim -= epsilon * 2.0;

			RegularData3D* grid_ptr = 0;
			try
			{
				grid_ptr = new RegularData3D(new_size, origin, dim);
			}
			catch(...)
			{
				setStatusbarText(tr("Not enough memory to resize grid!"), true);
				return 0;
			}

			RegularData3D& new_grid = *grid_ptr;

			bool problem = false;
			Size s = new_grid.getData().size();
			for (Position i = 0; i < s; i++)
			{
				const Vector3& v = new_grid.getCoordinates(i);
				try
				{
					new_grid[i] = grid.getInterpolatedValue(v);
				}
				catch(...)
				{
					problem = true;
				}
			}
			
			// should not happen:
			if (problem) setStatusbarText(tr("Grid may be inaccurate!"), true);

			return grid_ptr;
		}

		RegularData3D* RegularData3DController::createHistogramGrid(const RegularData3D& grid) 
		{
			RegularData3D* new_grid = new RegularData3D(grid);
			vector<float>& normalized =  *(vector<float>*)&new_grid->getData();
			calculateHistogramEqualization(grid.getData(), normalized, false);
			return new_grid;
		}

		void RegularData3DController::createHistogramGrid()
		{
			RegularData3DDataset* data = dynamic_cast<RegularData3DDataset*>(getSelectedDataset());
			if (data == 0 ||
			    getData(data) == 0)
			{
				BALLVIEW_DEBUG
				return;
			}

			RegularData3D* new_data = createHistogramGrid(*getData(data));

			RegularData3DDataset* new_set = new RegularData3DDataset(*data);
			new_set->setName(String("normalized ") + data->getName());
			new_set->setData(new_data);
			insertDataset(new_set);
		}

		void RegularData3DController::resizeGrid()
		{
			RegularData3DDataset* data = dynamic_cast<RegularData3DDataset*>(getSelectedDataset());
			if (data == 0 ||
			    getData(data) == 0)
			{
				BALLVIEW_DEBUG
				return;
			}

			RegularData3D* new_data = resizeGrid(*getData(data));

			RegularData3DDataset* new_set = new RegularData3DDataset(*data);
			new_set->setName(String("resized ") + data->getName());
			new_set->setData(new_data);
			insertDataset(new_set);
		}

		///////////////////////////////////////////////////////////////////////////

		String TrajectoryController::type = "Trajectory";
		
		TrajectoryController::TrajectoryController()
			: DatasetController()
		{
			type_ = type;
			file_formats_.push_back("dcd");
			file_formats_.push_back("trr");
			setIdentifier("TrajectoryController");
			registerThis();
		}

		TrajectoryController::TrajectoryController(TrajectoryController& dc)
			: DatasetController(dc)
		{
			setIdentifier("TrajectoryController");
			registerThis();
		}

		TrajectoryController::~TrajectoryController()
		{
		}

		SnapShotManager* TrajectoryController::getData(Dataset* set)
		{
			SnapShotManagerDataset* dset = dynamic_cast<SnapShotManagerDataset*>(set);
			if (dset == 0) return 0;
			return dset->getData();
		}

		void TrajectoryController::deleteDataset_(Dataset* set)
		{
			SnapShotManager* data = getData(set);
			if (data == 0) return;
			delete data;

			if (dialogs_per_ssm_.has(data))
				dialogs_per_ssm_.erase(dialogs_per_ssm_.find(data));
		}


		bool TrajectoryController::write(Dataset* set, String /*filetype*/, String filename)
		{
			if (getData(set) == 0) return false;
			getData(set)->getTrajectoryFile()->copyTo(filename);
			return true;
		}

		Dataset* TrajectoryController::open(String /*filetype*/, String filename)
		{
			System* system = getMainControl()->getSelectedSystem();
			if (system == 0) return 0;

			TrajectoryFile* trajectory_file = TrajectoryFileFactory::open(filename, std::ios::in);

			if (trajectory_file->getNumberOfAtoms() != system->countAtoms())
			{
				setStatusbarText(tr("Number of atoms do not match. Aborting..."));
				delete trajectory_file;
				return 0;
			}

			SnapShotManager* manager = new SnapShotManager(system, 0, trajectory_file);

			String name = getNameFromFileName_(filename);

			SnapShotManagerDataset* set = new SnapShotManagerDataset;
			set->setData(manager);
			set->setName(name);
			set->setType(type_);
			set->setComposite(system);

			return set;
		}


		QMenu* TrajectoryController::buildContextMenu(QTreeWidgetItem* item)
		{
			QMenu* menu = DatasetController::buildContextMenu(item);
			if (menu == 0) return 0;
			menu->addSeparator();
			menu->addAction("Buffer Trajectory", this, SLOT(bufferTrajectory()));
			menu->addAction("Visualize Trajectory", this, SLOT(visualizeTrajectory()));
			return menu;
		}


		bool TrajectoryController::createMenuEntries()
		{
			if (!DatasetController::createMenuEntries()) return false;

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS, UIOperationMode::MODE_ADVANCED);

			String description = "Shortcut|TrajectoryController|Buffer_Trajectory";
			insertMenuEntry_(MainControl::TOOLS, "Buffer Trajectory", SLOT(bufferTrajectory()),
							description, QKeySequence(), tr("Buffer Trajectory in RAM for faster access"),
							UIOperationMode::MODE_ADVANCED);

			description = "Shortcut|TrajectoryController|Visualize_Trajectory";
			insertMenuEntry_(MainControl::TOOLS, "Visualize Trajectory", SLOT(visualizeTrajectory()),
							description, QKeySequence(), tr(""), UIOperationMode::MODE_ADVANCED);

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS, UIOperationMode::MODE_ADVANCED);

			return true;
		}

		bool TrajectoryController::visualizeTrajectory()
		{
			SnapShotManager* ssm = getData(getSelectedDataset());
			if (ssm == 0) 
			{
				BALLVIEW_DEBUG
				return false;
			}

			if (!dialogs_per_ssm_.has(ssm))
			{
				boost::shared_ptr<SnapshotVisualisationDialog> dialog(new SnapshotVisualisationDialog(getDatasetControl()));

				dialog->setSnapShotManager(ssm);
				dialog->show();

				dialogs_per_ssm_[ssm] = dialog;
			}
			else
			{
				dialogs_per_ssm_[ssm]->show();
			}

			return true;
		}

		bool TrajectoryController::bufferTrajectory()
		{
			Dataset* data = getSelectedDataset();
			if (getData(data) == 0) return false;

			SnapShotManager* ssm = getData(data);
			if (ssm->getNumberOfSnapShotsInBuffer() == 0)
			{
				if (!ssm->readFromFile())
				{
					ssm->clearBuffer();
					setStatusbarText(tr("Could not read trajectories into buffer! Out of memory?"));
				}
			}
			return true;
		}

		///////////////////////////////////////////////////////////////////////////

		String DockResultController::type = "DockResult";
		
		DockResultController::DockResultController()
			: DatasetController()
		{
			type_ = type;
			file_formats_.push_back("bdr");
			setIdentifier("DockResultController");
			registerThis();
		}

		DockResultController::DockResultController(DockResultController& dc)
			: DatasetController(dc)
		{
			setIdentifier("DockResultController");
			registerThis();
		}

		DockResultController::~DockResultController()
		{
		}

		DockResult* DockResultController::getData(Dataset* set)
		{
			DockResultDataset* dset = dynamic_cast<DockResultDataset*>(set);
			if (dset == 0) return 0;
			return dset->getData();
		}

		void DockResultController::deleteDataset_(Dataset* set)
		{
			DockResult* data = getData(set);
			if (data == 0) return;
			delete data;
		}

		bool DockResultController::write(Dataset* set, String /*filetype*/, 
																												String filename)
		{
			if (getData(set) == 0) return false;
			getData(set)->writeDockResult(filename);
			return true;
		}

		Dataset* DockResultController::open(String /*filetype*/, String filename)
		{
			DockResult* dr = new DockResult();
			if (!dr->readDockResult(filename))
			{
				setStatusbarText(tr("Could not read DockResult file!"), true);
				return 0;
			}
		
			String name = getNameFromFileName_(filename);
			DockResultDataset* set = new DockResultDataset;
			set->setData(dr);
			set->setName(name);
			set->setType(type_);

			// get docked system, set its name to name of file and add it to BALLView structures
			// apply the first SnapShot to the system to get the positions of the docked structures
			SnapShot ss = (*(dr->getConformationSet()))[0];
			System* docked_system = new System(dr->getConformationSet()->getSystem());
			docked_system->setName(name);
			ss.applySnapShot(*docked_system);
			getMainControl()->insert(*docked_system);

			return set;
		}


		QMenu* DockResultController::buildContextMenu(QTreeWidgetItem* item)
		{
			QMenu* menu = DatasetController::buildContextMenu(item);
			if (menu == 0) return 0;
			menu->addSeparator();
			menu->addAction("Visualize DockResult", this, SLOT(showDockResult()));
			return menu;
		}


		bool DockResultController::createMenuEntries()
		{
			if (!DatasetController::createMenuEntries()) return false;

			insertMenuEntry_(MainControl::TOOLS, "Visualize DockResult", SLOT(showDockResult()), 
			                 "Shortcut|Tools|Visualize_DockResult", QKeySequence(), tr(""), UIOperationMode::MODE_ADVANCED);

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS, UIOperationMode::MODE_ADVANCED);

			return true;
		}

		bool DockResultController::saveDockTrajectory()
		{
			Dataset* data = getSelectedDataset();
			if (getData(data) == 0) return false;

			QString s = QFileDialog::getSaveFileName(0,
										"Choose a Trajectory file to save",
										getDatasetControl()->getWorkingDir().c_str(),
										"DCD files(*.dcd)");

			if (s == QString::null) return false;
			String filename = ascii(s);
			getDatasetControl()->setWorkingDirFromFilename_(filename);

			DockResult* res = getData(data);
			if (!res->getConformationSet()->writeDCDFile(filename))
			{
				setStatusbarText(tr("Could not write DCDFile."), true);
				return false;
			}

			setStatusbarText(tr("Written DCDFile."), true);
			return true;
		}


		void DockResultController::showDockResult()
		{	
			Dataset* set = getSelectedDataset();
			if (getData(set) == 0) return;

			DatasetMessage* msg = new DatasetMessage(set, DatasetMessage::VISUALIZE);
			getMainControl()->sendMessage(*msg);
		}


		/////////////////////////////////////////////////////////////////////
		// VectorGrid Controller:

		String VectorGridController::type = "Vector Grid";

		VectorGridController::VectorGridController()
			: DatasetController()
		{
			type_ = type;
			file_formats_.push_back("bvg");
			setIdentifier("VectorGridController");
			registerThis();
		}

		VectorGridController::VectorGridController(VectorGridController& dc)
			: DatasetController(dc)
		{
			setIdentifier("VectorGridController");
			registerThis();
		}

		VectorGridController::~VectorGridController()
		{
		}

		VectorGrid* VectorGridController::getData(Dataset* set)
		{
			VectorGridDataset* dset = dynamic_cast<VectorGridDataset*>(set);
			if (dset == 0) return 0;
			return dset->getData();
		}

		void VectorGridController::deleteDataset_(Dataset* set)
		{
			VectorGrid* data = getData(set);
			if (data == 0) return;
			delete data;
		}


		bool VectorGridController::write(Dataset* set, String /*filetype*/, 
																												String filename)
		{
			if (getData(set) == 0) return false;
			VectorGrid* dat = getData(set);
			(*dat).binaryWrite(filename);

			return true;
		}

		Dataset* VectorGridController::open(String /*filetype*/, String filename)
		{
			VectorGridDataset* set = new VectorGridDataset;
			VectorGrid* dat = new VectorGrid;
			(*dat).binaryRead(filename);
			set->setData(dat);
			set->setName(getNameFromFileName_(filename));
			set->setType(type_);

			return set;
		}


		QMenu* VectorGridController::buildContextMenu(QTreeWidgetItem* item)
		{
			QMenu* menu = DatasetController::buildContextMenu(item);
			if (menu == 0) return 0;
			menu->addSeparator();
			menu->addAction("Visualize VectorGrid", this, SLOT(visualizeFieldLines()));
			return menu;
		}


		bool VectorGridController::createMenuEntries()
		{
			if (!DatasetController::createMenuEntries()) return false;

			insertMenuEntry_(MainControl::TOOLS_GRID, "Visualize Field Lines", SLOT(visualizeFieldLines()), 
			                 "Shortcut|Tools|Grid|Visualize_Field_Lines", QKeySequence(),
											 tr("Visualise a gradient grid per field lines"), UIOperationMode::MODE_ADVANCED);

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS_GRID, UIOperationMode::MODE_ADVANCED);

			return true;
		}


		bool VectorGridController::visualizeFieldLines()
		{
			Dataset* data = getSelectedDataset();
			if (getData(data) == 0) return false;

			FieldLinesDialog dialog;

			dialog.vector_grid_ = getData(data);
			dialog.potential_grid_ = 0;

			vector<Dataset*> sets3d = getDatasetControl()->getDatasets(RegularData3DController::type);
			if (sets3d.size() > 0)
			{
				RegularData3DDataset* rset = dynamic_cast<RegularData3DDataset*>(sets3d[0]);
				if (rset == 0) return false;
 				dialog.potential_grid_ = rset->getData();
			}

			dialog.ac_ = dynamic_cast<AtomContainer*>(data->getComposite());
			if (dialog.ac_ == 0)
			{
				const CompositeManager& cm = getMainControl()->getCompositeManager();
				if (+cm.begin()) dialog.ac_ = (AtomContainer*) *cm.begin();
			}
			
			if (!dialog.exec()) return false;

			Representation* rep = dialog.createLines();
			if (rep == 0) 
			{
				setStatusbarText(tr("Creation of field line failed, see logs for further details..."), true);
				return false;
			}

			getMainControl()->insert(*rep);
			getMainControl()->update(*rep);
			setStatusbarText(String("Finished field line calculations. ") + 
											String(dialog.field_line_errors_) + " errorous positions tried.", true);
			return true;
		}

		/////////////////////////////////////////////////////////////////////
		// RaytraceableGrid Controller:

		String RaytraceableGridController::type = "Raytraceable Grid";

		RaytraceableGridController::RaytraceableGridController()
			: DatasetController(),
				rt_contour_surface_dialog_(0)
		{
			type_ = type;
			setIdentifier("RaytraceableGridController");
			registerThis();
			file_formats_.push_back("vrt");
		}

		RaytraceableGridController::RaytraceableGridController(RaytraceableGridController& rc)
			: DatasetController(rc),
				rt_contour_surface_dialog_(rc.rt_contour_surface_dialog_)
		{
			setIdentifier("RaytraceableGridController");
			registerThis();
			file_formats_.assign(rc.file_formats_.begin(), rc.file_formats_.end());
		}

		RaytraceableGridController::~RaytraceableGridController()
		{
			if (rt_contour_surface_dialog_)
				delete rt_contour_surface_dialog_;
		}

		bool RaytraceableGridController::write(Dataset* set, String /*filetype*/, String filename)
		{
			RaytraceableGrid* grid = getData(set);
			if (grid == NULL)
			{
				return false;
			}
			
			grid->binaryWrite(filename);			

			return true;
		}

		RaytraceableGrid* RaytraceableGridController::getData(Dataset* set)
		{
			RaytraceableGridDataset* dset = dynamic_cast<RaytraceableGridDataset*>(set);
			if (dset == 0) return 0;
			return dset->getData();
		}
			
		void RaytraceableGridController::deleteDataset_(Dataset* set)
		{
			RaytraceableGrid* data = getData(set);
			if (data == 0) return;
			delete data;
		}

		QMenu* RaytraceableGridController::buildContextMenu(QTreeWidgetItem* item)
		{
			QMenu* menu = DatasetController::buildContextMenu(item);
			if (menu == 0) return 0;
			menu->addSeparator();
			menu->addAction("Create Contour Surface", this, SLOT(visualizeRaytraceableContourSurface()));
			menu->addSeparator();
			return menu;
		}

		bool RaytraceableGridController::createMenuEntries()
		{
			if (!DatasetController::createMenuEntries()) return false;

			getDatasetControl()->getMainControl()->insertPopupMenuSeparator(MainControl::TOOLS_GRID, UIOperationMode::MODE_ADVANCED);

			return true;
		}

		void  RaytraceableGridController::visualizeRaytraceableContourSurface()
		{
			Dataset* data = (Dataset*)getSelectedDataset();
			if (getData(data) == 0)
			{
				BALLVIEW_DEBUG
				return;
			}

			RaytraceableGrid* grid = getData(data);
			RaytraceableContourSurfaceDialog rcsd(grid, getDatasetControl());
			rcsd.exec();
		
/*			if (rt_contour_surface_dialog_ == 0) 
			{
				rt_contour_surface_dialog_ = new RaytraceableContourSurfaceDialog(getDatasetControl());
			}

			//rt_contour_surface_dialog_->setController(this);
			//rt_contour_surface_dialog_->setGrid(getData(data));
			//since this is no modal window no exec but:
			///rt_contour_surface_dialog_->show();
			rt_contour_surface_dialog_->raise();
			rt_contour_surface_dialog_->activateWindow();
			//rt_contour_surface_dialog_->exec();

  TODO: something like
  	Representation* rep = new Representation();
			rep->insert(*mesh);
			rep->setModelType(MODEL_CONTOUR_SURFACE); 

			List<const Composite*> composites;
			if (data.getComposite() != 0) composites.push_back(data.getComposite());
			rep->setComposites(composites);

			// Make sure BALLView knows about the new representation.
			getMainControl()->insert(*rep);
			getMainControl()->update(*rep);

  */

		}

	} // namespace VIEW
} // namespace BALL