1 #include "Application.h"
2 #include "ConversionThread.h"
6 #include <QToolButton.h>
7 #include <QWhatsThis.h>
10 #include <QStatusBar.h>
11 #include <QApplication.h>
12 #include <QFileDialog.h>
13 #include <QMessageBox.h>
14 #include <QInputDialog.h>
15 #include <QCloseEvent>
17 #include <Voxel_BoolDS.hxx>
18 #include <Voxel_ColorDS.hxx>
19 #include <Voxel_FloatDS.hxx>
20 #include <Voxel_OctBoolDS.hxx>
21 #include <Voxel_ROctBoolDS.hxx>
22 #include <Voxel_BooleanOperation.hxx>
23 #include <Voxel_CollisionDetection.hxx>
24 #include <Voxel_FastConverter.hxx>
25 #include <Voxel_Writer.hxx>
26 #include <Voxel_Reader.hxx>
27 #include <VoxelClient_VisDrawer.h>
29 #include <BRepTools.hxx>
30 #include <BRepBndLib.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepPrimAPI_MakeBox.hxx>
33 #include <BRepPrimAPI_MakeTorus.hxx>
34 #include <BRepPrimAPI_MakeSphere.hxx>
35 #include <BRepPrimAPI_MakeCylinder.hxx>
39 Application::Application()
43 QMenu * file = menuBar()->addMenu( "&File" );
47 a = new QAction("Box", this);
48 connect(a, SIGNAL(triggered()), this, SLOT(box()));
51 a = new QAction("Cylinder", this);
52 connect(a, SIGNAL(triggered()), this, SLOT(cylinder()));
55 a = new QAction("Torus", this);
56 connect(a, SIGNAL(triggered()), this, SLOT(torus()));
59 a = new QAction("Sphere", this);
60 connect(a, SIGNAL(triggered()), this, SLOT(sphere()));
63 a = new QAction("Load shape...", this);
64 a->setShortcut(tr("Ctrl+O"));
65 connect(a, SIGNAL(triggered()), this, SLOT(choose()));
71 a = new QAction("Open", this);
72 connect(a, SIGNAL(triggered()), this, SLOT(open()));
76 a = new QAction("Save", this);
77 connect(a, SIGNAL(triggered()), this, SLOT(save()));
83 a = new QAction("&Quit", this);
84 a->setShortcut(tr("Ctrl+Q"));
85 connect(a, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
88 menuBar()->addSeparator();
92 QMenu * test = menuBar()->addMenu( "Test" );
94 a = new QAction("Test boolean", this);
95 connect(a, SIGNAL(triggered()), this, SLOT(testBoolDS()));
98 a = new QAction("Test color", this);
99 connect(a, SIGNAL(triggered()), this, SLOT(testColorDS()));
102 a = new QAction("Test float", this);
103 connect(a, SIGNAL(triggered()), this, SLOT(testFloatDS()));
106 a = new QAction("Test boolean / 8", this);
107 connect(a, SIGNAL(triggered()), this, SLOT(testOctBoolDS()));
110 a = new QAction("Test boolean / 8 / 8..", this);
111 connect(a, SIGNAL(triggered()), this, SLOT(testROctBoolDS()));
114 test->addSeparator();
116 a = new QAction("Test fusion of booleans", this);
117 connect(a, SIGNAL(triggered()), this, SLOT(testFuseBoolDS()));
120 a = new QAction("Test fusion of colors", this);
121 connect(a, SIGNAL(triggered()), this, SLOT(testFuseColorDS()));
124 a = new QAction("Test fusion of floating-points", this);
125 connect(a, SIGNAL(triggered()), this, SLOT(testFuseFloatDS()));
128 a = new QAction("Test cutting of booleans", this);
129 connect(a, SIGNAL(triggered()), this, SLOT(testCutBoolDS()));
132 a = new QAction("Test cutting of booleans", this);
133 connect(a, SIGNAL(triggered()), this, SLOT(testCutColorDS()));
136 a = new QAction("Test cutting of floating-points", this);
137 connect(a, SIGNAL(triggered()), this, SLOT(testCutFloatDS()));
142 QMenu * converter = menuBar()->addMenu( "Converter" );
146 a = new QAction("Number of splits along X", this);
147 connect(a, SIGNAL(triggered()), this, SLOT(setNbX()));
148 converter->addAction(a);
150 a = new QAction("Number of splits along Y", this);
151 connect(a, SIGNAL(triggered()), this, SLOT(setNbY()));
152 converter->addAction(a);
154 a = new QAction("Number of splits along Z", this);
155 connect(a, SIGNAL(triggered()), this, SLOT(setNbZ()));
156 converter->addAction(a);
158 converter->addSeparator();
160 a = new QAction("Side of scanning", this);
161 connect(a, SIGNAL(triggered()), this, SLOT(setScanSide()));
162 converter->addAction(a);
164 converter->addSeparator();
166 a = new QAction("Volumic value of 1bit voxels", this);
167 connect(a, SIGNAL(triggered()), this, SLOT(setVolumicBoolValue()));
168 converter->addAction(a);
170 a = new QAction("Volumic value of 4bit voxels", this);
171 connect(a, SIGNAL(triggered()), this, SLOT(setVolumicColorValue()));
172 converter->addAction(a);
174 converter->addSeparator();
178 a = new QAction("Convert to 1bit voxels", this);
179 connect(a, SIGNAL(triggered()), this, SLOT(convert2bool()));
180 converter->addAction(a);
182 a = new QAction("Convert to 4bit voxels", this);
183 connect(a, SIGNAL(triggered()), this, SLOT(convert2color()));
184 converter->addAction(a);
186 QMenu * vis = menuBar()->addMenu( "&Visualization" );
188 a = new QAction("Points", this);
189 connect(a, SIGNAL(triggered()), this, SLOT(displayPoints()));
194 a = new QAction("Nearest points", this);
195 connect(a, SIGNAL(triggered()), this, SLOT(displayNearestPoints()));
200 a = new QAction("Boxes", this);
201 connect(a, SIGNAL(triggered()), this, SLOT(displayBoxes()));
206 a = new QAction("Nearest boxes", this);
207 connect(a, SIGNAL(triggered()), this, SLOT(displayNearestBoxes()));
214 a = new QAction("Point size", this);
215 connect(a, SIGNAL(triggered()), this, SLOT(setPointSize()));
218 a = new QAction("Quadrangle size (%)", this);
219 connect(a, SIGNAL(triggered()), this, SLOT(setQuadrangleSize()));
224 a = new QAction("Color min value", this);
225 connect(a, SIGNAL(triggered()), this, SLOT(setColorMinValue()));
228 a = new QAction("Color max value", this);
229 connect(a, SIGNAL(triggered()), this, SLOT(setColorMaxValue()));
236 a = new QAction("Use GL lists", this);
237 connect(a, SIGNAL(triggered()), this, SLOT(setUsageOfGLlists()));
244 a = new QAction("Displayed X min", this);
245 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedXMin()));
248 a = new QAction("Displayed X max", this);
249 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedXMax()));
252 a = new QAction("Displayed Y min", this);
253 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedYMin()));
256 a = new QAction("Displayed Y max", this);
257 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedYMax()));
260 a = new QAction("Displayed Z min", this);
261 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedZMin()));
264 a = new QAction("Displayed Z max", this);
265 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedZMax()));
269 QMenu * demo = menuBar()->addMenu( "Demo" );
271 a = new QAction("Waves", this);
272 connect(a, SIGNAL(triggered()), this, SLOT(displayWaves()));
275 a = new QAction("Cut", this);
276 connect(a, SIGNAL(triggered()), this, SLOT(displayCut()));
279 a = new QAction("Collisions", this);
280 connect(a, SIGNAL(triggered()), this, SLOT(displayCollisions()));
284 QMenu * help = menuBar()->addMenu( "Help" );
286 a = new QAction("About", this);
287 a->setShortcut(tr("F1"));
288 connect(a, SIGNAL(triggered()), this, SLOT(about()));
292 myViewer = new Viewer( this );
293 myViewer->setFocus();
294 setCentralWidget( myViewer );
295 statusBar()->showMessage( "Ready", 2000 );
303 myVolumicBoolValue = false;
304 myVolumicColorValue = 0;
306 myQuadrangleSize = 40;
309 myColorMaxValue = 15;
313 myColorScale = new AIS_ColorScale;
314 myColorScale->SetZLayer (Graphic3d_ZLayerId_TopOSD);
315 myColorScale->SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt (-1,-1,0));
318 myDisplayedXMin = -DBL_MAX;
319 myDisplayedXMax = DBL_MAX;
320 myDisplayedYMin = -DBL_MAX;
321 myDisplayedYMax = DBL_MAX;
322 myDisplayedZMin = -DBL_MAX;
323 myDisplayedZMax = DBL_MAX;
327 myViewer->getIC()->SetAutoActivateSelection (Standard_False);
330 Application::~Application()
335 delete myColorVoxels;
338 void Application::choose()
340 QString fn = QFileDialog::getOpenFileName( this, QString::null, QString::null, "*.brep");
344 statusBar()->showMessage( "Loading aborted", 2000 );
347 void Application::load( const QString &fileName )
350 if ( !f.open( QIODevice::ReadOnly ) )
356 if (!BRepTools::Read(S, (char*) fileName.constData(), B))
357 statusBar()->showMessage( "Loading failed", 2000 );
362 void Application::open()
364 QString fn = QFileDialog::getOpenFileName( this, QString::null, QString::null, "*.vx");
365 if ( fn.isEmpty() || !QFile::exists(fn) )
367 statusBar()->showMessage( "Open aborted", 2000 );
376 if (!reader.Read((char*)fn.constData()))
378 statusBar()->showMessage( "Open failed... sorry", 2000 );
385 // Release current voxels
393 delete myColorVoxels;
398 if (reader.IsBoolVoxels())
400 myBoolVoxels = (Voxel_BoolDS*) reader.GetBoolVoxels();
401 myViewer->getSelector().SetVoxels(*myBoolVoxels);
403 else if (reader.IsColorVoxels())
405 myColorVoxels = (Voxel_ColorDS*) reader.GetColorVoxels();
406 myViewer->getSelector().SetVoxels(*myColorVoxels);
409 // Display the voxels
410 myViewer->getIC()->EraseAll(false);
411 Voxel_DS* ds = myBoolVoxels;
416 myDisplayedXMin = ds->GetX() - 10.0 * Precision::Confusion();
417 myDisplayedXMax = ds->GetX() + ds->GetXLen() + 10.0 * Precision::Confusion();
418 myDisplayedYMin = ds->GetY() - 10.0 * Precision::Confusion();
419 myDisplayedYMax = ds->GetY() + ds->GetYLen() + 10.0 * Precision::Confusion();
420 myDisplayedZMin = ds->GetZ() - 10.0 * Precision::Confusion();
421 myDisplayedZMax = ds->GetZ() + ds->GetZLen() + 10.0 * Precision::Confusion();
427 // Set voxels and display
428 Handle(Poly_Triangulation) empty;
429 myVoxels->SetBoolVoxels(myBoolVoxels);
430 myVoxels->SetColorVoxels(myColorVoxels);
431 myVoxels->SetTriangulation(empty);
432 if (myViewer->getIC()->IsDisplayed(myVoxels))
433 myViewer->getIC()->Redisplay(myVoxels, false);
435 myViewer->getIC()->Display(myVoxels, false);
441 myViewer->getIC()->Erase(myColorScale);
443 myViewer->getView()->FitAll();
445 statusBar()->showMessage( "Ready.", 2000 );
448 void Application::save()
450 QString fn = QFileDialog::getSaveFileName( this, QString::null, QString::null, "*.vx");
453 statusBar()->showMessage( "Storage aborted", 2000 );
456 if (fn.indexOf(".vx", -1, Qt::CaseInsensitive) == -1)
464 writer.SetFormat(Voxel_VFF_BINARY);
466 writer.SetVoxels(*myBoolVoxels);
467 else if (myColorVoxels)
468 writer.SetVoxels(*myColorVoxels);
471 statusBar()->showMessage( "Nothing to store", 2000 );
474 if (!writer.Write((char*)fn.constData()))
476 statusBar()->showMessage( "Storage failed... sorry", 2000 );
483 statusBar()->showMessage( "Saved.", 2000 );
486 void Application::closeEvent( QCloseEvent* ce )
491 void Application::about()
493 QMessageBox::about( this, "Voxel demo-application",
494 "This example demonstrates simple usage of "
495 "voxel models of Open CASCADE.");
498 void Application::testBoolDS()
502 int nbx = 100, nby = 100, nbz = 100;
509 Voxel_BoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
511 for (ix = 0; ix < nbx; ix++)
513 for (iy = 0; iy < nby; iy++)
515 for (iz = 0; iz < nbz; iz++)
518 ds.Set(ix, iy, iz, false);
520 ds.Set(ix, iy, iz, true);
525 for (ix = 0; ix < nbx; ix++)
527 for (iy = 0; iy < nby; iy++)
529 for (iz = 0; iz < nbz; iz++)
531 bool value = ds.Get(ix, iy, iz) == Standard_True;
535 cout<<"Wrong value!"<<endl;
540 cout<<"Wrong value!"<<endl;
547 timer.Print("BoolDS");
550 void Application::testColorDS()
554 int nbx = 100, nby = 100, nbz = 100;
561 Voxel_ColorDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
563 for (ix = 0; ix < nbx; ix++)
565 for (iy = 0; iy < nby; iy++)
567 for (iz = 0; iz < nbz; iz++)
570 ds.Set(ix, iy, iz, 8);
572 ds.Set(ix, iy, iz, 7);
577 for (ix = 0; ix < nbx; ix++)
579 for (iy = 0; iy < nby; iy++)
581 for (iz = 0; iz < nbz; iz++)
583 unsigned char value = ds.Get(ix, iy, iz);
587 cout<<"Wrong value!"<<endl;
592 cout<<"Wrong value!"<<endl;
599 timer.Print("ColorDS");
602 void Application::testFloatDS()
606 int nbx = 100, nby = 100, nbz = 100;
613 Voxel_FloatDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
615 for (ix = 0; ix < nbx; ix++)
617 for (iy = 0; iy < nby; iy++)
619 for (iz = 0; iz < nbz; iz++)
622 ds.Set(ix, iy, iz, 8.8f);
624 ds.Set(ix, iy, iz, 7.7f);
629 for (ix = 0; ix < nbx; ix++)
631 for (iy = 0; iy < nby; iy++)
633 for (iz = 0; iz < nbz; iz++)
635 float value = ds.Get(ix, iy, iz);
639 cout<<"Wrong value!"<<endl;
644 cout<<"Wrong value!"<<endl;
651 timer.Print("FloatDS");
654 void Application::testOctBoolDS()
658 int nbx = 30, nby = 30, nbz = 30;
665 Voxel_OctBoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
667 for (ix = 0; ix < nbx; ix++)
669 for (iy = 0; iy < nby; iy++)
671 for (iz = 0; iz < nbz; iz++)
675 ds.Set(ix, iy, iz, true);
679 for (int i = 0; i < 8; i++)
682 ds.Set(ix, iy, iz, i, true);
684 ds.Set(ix, iy, iz, i, false);
691 for (ix = 0; ix < nbx; ix++)
693 for (iy = 0; iy < nby; iy++)
695 for (iz = 0; iz < nbz; iz++)
699 bool value = ds.Get(ix, iy, iz) == Standard_True;
701 cout<<"Wrong value!"<<endl;
705 for (int i = 0; i < 8; i++)
709 bool value = ds.Get(ix, iy, iz, i) == Standard_True;
711 cout<<"Wrong value!"<<endl;
715 bool value = ds.Get(ix, iy, iz, i) == Standard_True;
717 cout<<"Wrong value!"<<endl;
725 for (ix = 0; ix < nbx; ix++)
727 for (iy = 0; iy < nby; iy++)
729 for (iz = 0; iz < nbz; iz++)
733 for (int i = 0; i < 8; i++)
735 ds.Set(ix, iy, iz, i, true);
740 for (int i = 0; i < 8; i++)
742 ds.Set(ix, iy, iz, i, false);
752 timer.Print("OctBoolDS");
755 void Application::testROctBoolDS()
758 int ix, iy, iz, i, j;
759 int nbx = 30, nby = 30, nbz = 30;
765 Voxel_ROctBoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
767 for (ix = 0; ix < nbx; ix++)
769 for (iy = 0; iy < nby; iy++)
771 for (iz = 0; iz < nbz; iz++)
773 ds.Set(ix, iy, iz, true);
778 for (ix = 0; ix < nbx; ix++)
780 for (iy = 0; iy < nby; iy++)
782 for (iz = 0; iz < nbz; iz++)
784 for (i = 0; i < 8; i++)
786 for (j = 0; j < 8; j++)
788 ds.Set(ix, iy, iz, i, j, true);
797 for (ix = 0; ix < nbx; ix++)
799 for (iy = 0; iy < nby; iy++)
801 for (iz = 0; iz < nbz; iz++)
803 if (ds.Deepness(ix, iy, iz) == 0)
805 bool value = ds.Get(ix, iy, iz);
807 cout<<"Wrong value..."<<endl;
809 if (ds.Deepness(ix, iy, iz) == 1)
811 for (i = 0; i < 8; i++)
813 bool value = ds.Get(ix, iy, iz, i);
815 cout<<"Wrong value..."<<endl;
818 if (ds.Deepness(ix, iy, iz) == 2)
820 for (i = 0; i < 8; i++)
822 for (j = 0; j < 8; j++)
824 bool value = ds.Get(ix, iy, iz, i, j);
826 cout<<"Wrong value..."<<endl;
835 timer.Print("ROctBoolDS");
839 TopoDS_Shape S = BRepPrimAPI_MakeSphere(100.0);
844 Voxel_ROctBoolDS* ds2 = new Voxel_ROctBoolDS;
845 Voxel_FastConverter converter(S, *ds2, 0.1, myNbX, myNbY, myNbZ, 1);
846 converter.Convert(progress);
847 ds2->OptimizeMemory();
850 timer.Print("ROctBoolDS::converter");
854 myViewer->getIC()->EraseAll(false);
856 myVoxels->SetBoolVoxels(0);
857 myVoxels->SetColorVoxels(0);
858 Handle(Poly_Triangulation) empty;
859 myVoxels->SetTriangulation(empty);
860 myVoxels->SetROctBoolVoxels(ds2);
861 myViewer->getIC()->Display(myVoxels, false);
862 myViewer->getIC()->Erase(myColorScale);
863 myViewer->getView()->FitAll();
864 myViewer->getSelector().SetVoxels(*ds2);
867 void Application::testFuseBoolDS()
871 int nbx = 100, nby = 100, nbz = 100;
874 // 1. Set two BoolDS:
878 Voxel_BoolDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
879 Voxel_BoolDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
881 for (ix = 0; ix < nbx; ix++)
883 for (iy = 0; iy < nby; iy++)
885 for (iz = 0; iz < nbz; iz++)
888 ds2.Set(ix, iy, iz, false);
890 ds2.Set(ix, iy, iz, true);
897 Voxel_BooleanOperation fuser;
898 if (!fuser.Fuse(ds1, ds2))
899 cout<<"The operation failed..."<<endl;
903 for (ix = 0; ix < nbx; ix++)
905 for (iy = 0; iy < nby; iy++)
907 for (iz = 0; iz < nbz; iz++)
909 bool value = ds1.Get(ix, iy, iz) == Standard_True;
913 cout<<"Wrong value!"<<endl;
918 cout<<"Wrong value!"<<endl;
925 timer.Print("Fusion of BoolDS");
928 void Application::testFuseColorDS()
932 int nbx = 100, nby = 100, nbz = 100;
935 // 1. Set two ColorDS:
939 Voxel_ColorDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
940 Voxel_ColorDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
942 for (ix = 0; ix < nbx; ix++)
944 for (iy = 0; iy < nby; iy++)
946 for (iz = 0; iz < nbz; iz++)
948 ds1.Set(ix, iy, iz, 11);
952 for (ix = 0; ix < nbx; ix++)
954 for (iy = 0; iy < nby; iy++)
956 for (iz = 0; iz < nbz; iz++)
959 ds2.Set(ix, iy, iz, 3);
961 ds2.Set(ix, iy, iz, 5);
968 Voxel_BooleanOperation fuser;
969 if (!fuser.Fuse(ds1, ds2))
970 cout<<"The operation failed..."<<endl;
974 for (ix = 0; ix < nbx; ix++)
976 for (iy = 0; iy < nby; iy++)
978 for (iz = 0; iz < nbz; iz++)
980 unsigned char value = ds1.Get(ix, iy, iz);
984 cout<<"Wrong value!"<<endl;
989 cout<<"Wrong value!"<<endl;
996 timer.Print("Fusion of ColorDS");
999 void Application::testFuseFloatDS()
1003 int nbx = 100, nby = 100, nbz = 100;
1006 // 1. Set two FloatDS:
1010 Voxel_FloatDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1011 Voxel_FloatDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1013 for (ix = 0; ix < nbx; ix++)
1015 for (iy = 0; iy < nby; iy++)
1017 for (iz = 0; iz < nbz; iz++)
1019 ds1.Set(ix, iy, iz, 11.1f);
1023 for (ix = 0; ix < nbx; ix++)
1025 for (iy = 0; iy < nby; iy++)
1027 for (iz = 0; iz < nbz; iz++)
1030 ds2.Set(ix, iy, iz, 3.3f);
1032 ds2.Set(ix, iy, iz, 5.5f);
1039 Voxel_BooleanOperation fuser;
1040 if (!fuser.Fuse(ds1, ds2))
1041 cout<<"The operation failed..."<<endl;
1045 for (ix = 0; ix < nbx; ix++)
1047 for (iy = 0; iy < nby; iy++)
1049 for (iz = 0; iz < nbz; iz++)
1051 float value = ds1.Get(ix, iy, iz);
1054 if (fabs(value - 14.4f) > 0.001)
1055 cout<<"Wrong value!"<<endl;
1059 if (fabs(value - 16.6f) > 0.001)
1060 cout<<"Wrong value!"<<endl;
1067 timer.Print("Fusion of FloatDS");
1070 void Application::testCutBoolDS()
1074 int nbx = 100, nby = 100, nbz = 100;
1077 // 1. Set two BoolDS:
1081 Voxel_BoolDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1082 Voxel_BoolDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1084 for (ix = 0; ix < nbx; ix++)
1086 for (iy = 0; iy < nby; iy++)
1088 for (iz = 0; iz < nbz; iz++)
1090 ds1.Set(ix, iy, iz, true);
1094 for (ix = 0; ix < nbx; ix++)
1096 for (iy = 0; iy < nby; iy++)
1098 for (iz = 0; iz < nbz; iz++)
1101 ds2.Set(ix, iy, iz, false);
1103 ds2.Set(ix, iy, iz, true);
1110 Voxel_BooleanOperation cutter;
1111 if (!cutter.Cut(ds1, ds2))
1112 cout<<"The operation failed..."<<endl;
1116 for (ix = 0; ix < nbx; ix++)
1118 for (iy = 0; iy < nby; iy++)
1120 for (iz = 0; iz < nbz; iz++)
1122 bool value = ds1.Get(ix, iy, iz) == Standard_True;
1126 cout<<"Wrong value!"<<endl;
1131 cout<<"Wrong value!"<<endl;
1138 timer.Print("Cut of BoolDS");
1141 void Application::testCutColorDS()
1145 int nbx = 100, nby = 100, nbz = 100;
1148 // 1. Set two ColorDS:
1152 Voxel_ColorDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1153 Voxel_ColorDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1155 for (ix = 0; ix < nbx; ix++)
1157 for (iy = 0; iy < nby; iy++)
1159 for (iz = 0; iz < nbz; iz++)
1161 ds1.Set(ix, iy, iz, 11);
1165 for (ix = 0; ix < nbx; ix++)
1167 for (iy = 0; iy < nby; iy++)
1169 for (iz = 0; iz < nbz; iz++)
1172 ds2.Set(ix, iy, iz, 3);
1174 ds2.Set(ix, iy, iz, 5);
1181 Voxel_BooleanOperation cutter;
1182 if (!cutter.Cut(ds1, ds2))
1183 cout<<"The operation failed..."<<endl;
1187 for (ix = 0; ix < nbx; ix++)
1189 for (iy = 0; iy < nby; iy++)
1191 for (iz = 0; iz < nbz; iz++)
1193 unsigned char value = ds1.Get(ix, iy, iz);
1197 cout<<"Wrong value!"<<endl;
1202 cout<<"Wrong value!"<<endl;
1209 timer.Print("Cut of ColorDS");
1212 void Application::testCutFloatDS()
1216 int nbx = 100, nby = 100, nbz = 100;
1219 // 1. Set two FloatDS:
1223 Voxel_FloatDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1224 Voxel_FloatDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1226 for (ix = 0; ix < nbx; ix++)
1228 for (iy = 0; iy < nby; iy++)
1230 for (iz = 0; iz < nbz; iz++)
1232 ds1.Set(ix, iy, iz, 11.1f);
1236 for (ix = 0; ix < nbx; ix++)
1238 for (iy = 0; iy < nby; iy++)
1240 for (iz = 0; iz < nbz; iz++)
1243 ds2.Set(ix, iy, iz, 3.3f);
1245 ds2.Set(ix, iy, iz, 5.5f);
1252 Voxel_BooleanOperation cutter;
1253 if (!cutter.Cut(ds1, ds2))
1254 cout<<"The operation failed..."<<endl;
1258 for (ix = 0; ix < nbx; ix++)
1260 for (iy = 0; iy < nby; iy++)
1262 for (iz = 0; iz < nbz; iz++)
1264 float value = ds1.Get(ix, iy, iz);
1267 if (fabs(value - 7.8f) > 0.001)
1268 cout<<"Wrong value!"<<endl;
1272 if (fabs(value - 5.6f) > 0.001)
1273 cout<<"Wrong value!"<<endl;
1280 timer.Print("Cut of FloatDS");
1283 void Application::convert2bool()
1288 void Application::convert2color()
1293 void Application::convert(const int ivoxel)
1296 if (!myShape.IsNull())
1297 S = myShape->Shape();
1300 QMessageBox::warning( this, "Voxel demo-application", "No shape for conversion!");
1309 myBoolVoxels = new Voxel_BoolDS;
1312 delete myColorVoxels;
1320 myColorVoxels = new Voxel_ColorDS;
1323 delete myBoolVoxels;
1339 Voxel_Converter converter(S, *myBoolVoxels, myNbX, myNbY, myNbZ);
1340 if (!converter.Convert(progress, myVolumicBoolValue, myScanSide))
1342 QMessageBox::warning( this, "Voxel demo-application", "Conversion failed...");
1348 Voxel_Converter converter(S, *myBoolVoxels, myNbX, myNbY, myNbZ, 2);
1349 ConversionThread thread1, thread2;
1351 thread1.setConverter(&converter);
1352 thread2.setConverter(&converter);
1354 thread1.setVolumicValue(myVolumicBoolValue);
1355 thread2.setVolumicValue(myVolumicBoolValue);
1357 thread1.setScanSide(myScanSide);
1358 thread2.setScanSide(myScanSide);
1360 thread1.setThreadIndex(1);
1361 thread2.setThreadIndex(2);
1366 while (thread1.running() || thread2.running())
1374 Voxel_FastConverter converter(S, *myBoolVoxels, 0.1, myNbX, myNbY, myNbZ, 1);
1375 converter.Convert(progress, 1);
1376 //if (myVolumicBoolValue)
1377 // converter.FillInVolume(myVolumicBoolValue);
1380 Voxel_FastConverter converter(S, *myBoolVoxels, 0.1, myNbX, myNbY, myNbZ, 2);
1381 ConversionThread thread1, thread2;
1383 thread1.setConverter(&converter);
1384 thread2.setConverter(&converter);
1386 thread1.setThreadIndex(1);
1387 thread2.setThreadIndex(2);
1392 while (thread1.isRunning() || thread2.isRunning())
1397 timer.Print("Converter");
1399 myViewer->getSelector().SetVoxels(*myBoolVoxels);
1410 Voxel_Converter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ);
1411 if (!converter.Convert(progress, myVolumicColorValue, myScanSide))
1413 QMessageBox::warning( this, "Voxel demo-application", "Conversion failed...");
1419 Voxel_Converter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ, 2);
1420 ConversionThread thread1, thread2;
1422 thread1.setConverter(&converter);
1423 thread2.setConverter(&converter);
1425 thread1.setVolumicValue(myVolumicColorValue);
1426 thread2.setVolumicValue(myVolumicColorValue);
1428 thread1.setScanSide(myScanSide);
1429 thread2.setScanSide(myScanSide);
1431 thread1.setThreadIndex(1);
1432 thread2.setThreadIndex(2);
1437 while (thread1.running() || thread2.running())
1445 Voxel_FastConverter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ, 1);
1446 converter.Convert(progress, 1);
1447 if (myVolumicColorValue)
1448 converter.FillInVolume(myVolumicColorValue);
1451 Voxel_FastConverter converter(S, *myColorVoxels, 0.1, myNbX, myNbY, myNbZ, 2);
1452 ConversionThread thread1, thread2;
1454 thread1.setConverter(&converter);
1455 thread2.setConverter(&converter);
1457 thread1.setThreadIndex(1);
1458 thread2.setThreadIndex(2);
1463 while (thread1.isRunning() || thread2.isRunning())
1468 timer.Print("Converter");
1471 // Set color for demonstration
1473 fabs(myColorVoxels->GetX()) > fabs(myColorVoxels->GetY()) ?
1474 fabs(myColorVoxels->GetX()) : fabs(myColorVoxels->GetY());
1475 maxd = maxd > fabs(myColorVoxels->GetZ()) ? maxd : fabs(myColorVoxels->GetZ());
1476 maxd = maxd > fabs(myColorVoxels->GetX() + myColorVoxels->GetXLen()) ?
1477 maxd : fabs(myColorVoxels->GetX() + myColorVoxels->GetXLen());
1478 maxd = maxd > fabs(myColorVoxels->GetY() + myColorVoxels->GetYLen()) ?
1479 maxd : fabs(myColorVoxels->GetY() + myColorVoxels->GetYLen());
1480 maxd = maxd > fabs(myColorVoxels->GetZ() + myColorVoxels->GetZLen()) ?
1481 maxd : fabs(myColorVoxels->GetZ() + myColorVoxels->GetZLen());
1482 for (int ix = 0; ix < myNbX; ix++)
1484 for (int iy = 0; iy < myNbY; iy++)
1486 for (int iz = 0; iz < myNbZ; iz++)
1488 unsigned char value = myColorVoxels->Get(ix, iy, iz);
1491 double xc, yc, zc, xd, yd, zd;
1492 myColorVoxels->GetCenter(ix, iy, iz, xc, yc, zc);
1496 double mind = xd < yd ? xd : yd;
1497 mind = zd < mind ? zd : mind;
1498 value = unsigned char(15.0 * (maxd - mind) / maxd);
1501 myColorVoxels->Set(ix, iy, iz, value);
1507 myViewer->getSelector().SetVoxels(*myColorVoxels);
1512 myViewer->getIC()->EraseAll(false);
1514 Voxel_DS* ds = myBoolVoxels;
1519 myDisplayedXMin = ds->GetX() - 10.0 * Precision::Confusion();
1520 myDisplayedXMax = ds->GetX() + ds->GetXLen() + 10.0 * Precision::Confusion();
1521 myDisplayedYMin = ds->GetY() - 10.0 * Precision::Confusion();
1522 myDisplayedYMax = ds->GetY() + ds->GetYLen() + 10.0 * Precision::Confusion();
1523 myDisplayedZMin = ds->GetZ() - 10.0 * Precision::Confusion();
1524 myDisplayedZMax = ds->GetZ() + ds->GetZLen() + 10.0 * Precision::Confusion();
1530 // Set voxels and display
1531 Handle(Poly_Triangulation) empty;
1532 myVoxels->SetBoolVoxels(myBoolVoxels);
1533 myVoxels->SetColorVoxels(myColorVoxels);
1534 myVoxels->SetTriangulation(empty);
1535 if (myViewer->getIC()->IsDisplayed(myVoxels))
1536 myViewer->getIC()->Redisplay(myVoxels, false);
1538 myViewer->getIC()->Display(myVoxels, false);
1542 displayColorScale();
1544 myViewer->getIC()->Erase(myColorScale);
1546 myViewer->getView()->FitAll();
1549 void Application::setNbX()
1553 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbX,
1557 void Application::setNbY()
1561 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbY,
1565 void Application::setNbZ()
1569 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbZ,
1573 void Application::setColorMinValue()
1577 QInputDialog::getInteger(this, "Voxel demo-application", "Minimum value for color [0 .. 15]:", myColorMinValue,
1579 if (!myVoxels.IsNull())
1580 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1583 void Application::setColorMaxValue()
1587 QInputDialog::getInteger(this, "Voxel demo-application", "Maximum value for color [0 .. 15]:", myColorMaxValue,
1589 if (!myVoxels.IsNull())
1590 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1593 void Application::setUsageOfGLlists()
1595 int res = QMessageBox::question( this, "Voxel demo-application", "Press Yes to use GL lists and No not to use them.", QMessageBox::Yes, QMessageBox::No);
1596 if (!myVoxels.IsNull())
1597 myVoxels->SetUsageOfGLlists(res == QMessageBox::Yes);
1600 void Application::setDisplayedXMin()
1602 myDisplayedXMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum X value:", myDisplayedXMin);
1603 if (!myVoxels.IsNull())
1605 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1606 myDisplayedYMin, myDisplayedYMax,
1607 myDisplayedZMin, myDisplayedZMax);
1611 void Application::setDisplayedXMax()
1613 myDisplayedXMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum X value:", myDisplayedXMax);
1614 if (!myVoxels.IsNull())
1616 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1617 myDisplayedYMin, myDisplayedYMax,
1618 myDisplayedZMin, myDisplayedZMax);
1622 void Application::setDisplayedYMin()
1624 myDisplayedYMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum Y value:", myDisplayedYMin);
1625 if (!myVoxels.IsNull())
1627 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1628 myDisplayedYMin, myDisplayedYMax,
1629 myDisplayedZMin, myDisplayedZMax);
1633 void Application::setDisplayedYMax()
1635 myDisplayedYMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum Y value:", myDisplayedYMax);
1636 if (!myVoxels.IsNull())
1638 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1639 myDisplayedYMin, myDisplayedYMax,
1640 myDisplayedZMin, myDisplayedZMax);
1644 void Application::setDisplayedZMin()
1646 myDisplayedZMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum Z value:", myDisplayedZMin);
1647 if (!myVoxels.IsNull())
1649 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1650 myDisplayedYMin, myDisplayedYMax,
1651 myDisplayedZMin, myDisplayedZMax);
1655 void Application::setDisplayedZMax()
1657 myDisplayedZMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum Z value:", myDisplayedZMax);
1658 if (!myVoxels.IsNull())
1660 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1661 myDisplayedYMin, myDisplayedYMax,
1662 myDisplayedZMin, myDisplayedZMax);
1666 void Application::setScanSide()
1669 QInputDialog::getInteger(this, "Voxel demo-application", "Side of scanning (1: +X side, 2: +Y side, 3: +Z side, 4: +X & +Y sides, .. 7: +X, +Y,& +Z sides):",
1670 myScanSide, 1, 7, 1);
1673 void Application::setVolumicBoolValue()
1675 myVolumicBoolValue =
1676 QInputDialog::getInteger(this, "Voxel demo-application", "Volumic value on voxelization [0 .. 1]:",
1677 myVolumicBoolValue, 0, 1, 1);
1680 void Application::setVolumicColorValue()
1682 myVolumicColorValue =
1683 QInputDialog::getInteger(this, "Voxel demo-application", "Volumic value on voxelization [0 .. 15]:",
1684 myVolumicColorValue, 0, 15, 1);
1687 void Application::setQuadrangleSize()
1690 QInputDialog::getInteger(this, "Voxel demo-application", "Size of quadrangles (0% .. 100%):",
1691 myQuadrangleSize, 1, 100, 10);
1692 if (!myVoxels.IsNull())
1694 myVoxels->SetQuadrangleSize(myQuadrangleSize);
1698 void Application::setPointSize()
1701 QInputDialog::getInteger(this, "Voxel demo-application", "Size of points (1 .. 10):",
1702 myPointSize, 1, 10, 1);
1703 if (!myVoxels.IsNull())
1705 myVoxels->SetPointSize(myPointSize);
1709 void Application::display(Voxel_VoxelDisplayMode mode)
1711 if (myVoxels.IsNull() || !myViewer->getIC()->IsDisplayed(myVoxels))
1713 QMessageBox::warning( this, "Voxel demo-application", "Voxels are not displayed");
1717 myVoxels->SetDisplayMode(mode);
1720 displayColorScale();
1722 myViewer->getIC()->Erase(myColorScale);
1724 myViewer->getIC()->Redisplay(myVoxels, true);
1727 void Application::displayPoints()
1729 display(Voxel_VDM_POINTS);
1732 void Application::displayNearestPoints()
1734 display(Voxel_VDM_NEARESTPOINTS);
1737 void Application::displayBoxes()
1739 display(Voxel_VDM_BOXES);
1742 void Application::displayNearestBoxes()
1744 display(Voxel_VDM_NEARESTBOXES);
1747 void Application::displayColorScale()
1749 if (myColorScale.IsNull())
1751 myColorScale = new AIS_ColorScale;
1753 if (!myColorScale.IsNull())
1755 int nb_colors = 1<<4 /* 4 bits */;
1756 myColorScale->SetRange(0, nb_colors - 1);
1757 myColorScale->SetNumberOfIntervals(nb_colors);
1758 myColorScale->SetPosition(0.01, 0.5 - 0.01);
1759 myColorScale->SetSize(0.5 - 0.01, 0.5 - 0.01);
1761 myViewer->getIC()->Display(myColorScale);
1764 void Application::displayWaves()
1766 myViewer->getIC()->EraseAll(false);
1771 delete myBoolVoxels;
1775 delete myColorVoxels;
1777 int nbx = 500, nby = 50, nbz = 50;
1778 double xlen = 100.0, ylen = 100.0, zlen = 20.0;
1779 double dx = xlen / (double) nbx, dy = ylen / (double) nby, dz = zlen / (double) nbz;
1780 myColorVoxels = new Voxel_ColorDS(0.0, 0.0, 0.0, xlen, ylen, zlen, nbx, nby, nbz);
1782 // Initial state - no colors
1784 for (ix = 0; ix < nbx; ix++)
1786 for (iy = 0; iy < nby; iy++)
1788 for (iz = 0; iz < nbz; iz++)
1790 myColorVoxels->Set(ix, iy, iz, 0);
1797 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
1798 myVoxels->SetUsageOfGLlists(false);
1799 myVoxels->SetBoolVoxels(myBoolVoxels);
1800 myVoxels->SetColorVoxels(myColorVoxels);
1801 if (myViewer->getIC()->IsDisplayed(myVoxels))
1802 myViewer->getIC()->Redisplay(myVoxels, false);
1804 myViewer->getIC()->Display(myVoxels, false);
1805 myViewer->getView()->FitAll();
1807 // Prepare arrays of values
1809 int i = 0, di = 5 /* nb waves */;
1810 int* zvalues = new int[nbx];
1811 unsigned char* xvalues = new unsigned char[nbx];
1812 for (ix = 0; ix < nbx; ix++, i += di)
1814 if (i > nbx || i < 0)
1819 double rad = -M_PI / 2.0 + double(i) / (double) nbx * M_PI;
1820 double c = cos(rad);
1821 xvalues[ix] = 15.0 * c;
1822 if (xvalues[ix] == 0)
1824 zvalues[ix] = (nbz - 2) * c;
1828 unsigned char value = 0;
1829 for (i = 0; i <= 100; i++)
1831 for (ix = 0; ix < nbx; ix++)
1836 for (iz = 0; iz < nbz; iz++)
1839 if (iz < zvalues[ixi])
1840 value = xvalues[ixi];
1841 for (iy = 0; iy < nby; iy++)
1843 myColorVoxels->Set(ix, iy, iz, value);
1847 myViewer->getIC()->Redisplay(myVoxels, true);
1848 qApp->processEvents();
1855 void Application::initPrs()
1857 if (myVoxels.IsNull())
1859 myVoxels = new VoxelClient_PrsGl();
1860 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
1861 myVoxels->SetColor(Quantity_NOC_WHITE);
1862 myVoxels->SetPointSize(1.0);
1863 myVoxels->SetSmoothPoints(false);
1864 myVoxels->SetQuadrangleSize(myQuadrangleSize);
1865 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1866 // Colors of ColorDS
1867 int nb_colors = 16 /* 4 bits */;
1868 Handle(Quantity_HArray1OfColor) colors = new Quantity_HArray1OfColor(0, nb_colors - 1);
1869 for (int icolor = 0; icolor < nb_colors; icolor++)
1871 Quantity_Color color;
1872 AIS_ColorScale::FindColor(icolor, 0, nb_colors - 1, nb_colors, color);
1873 colors->SetValue(icolor, color);
1875 myVoxels->SetColors(colors);
1876 myViewer->setPrs(myVoxels);
1880 myViewer->getIC()->RecomputePrsOnly(myVoxels, false);
1884 void Application::box()
1886 gp_Ax2 axes(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
1887 TopoDS_Shape S = BRepPrimAPI_MakeBox(axes, 100, 100, 100);
1891 void Application::cylinder()
1893 TopoDS_Shape S = BRepPrimAPI_MakeCylinder(50, 100);
1897 void Application::torus()
1899 TopoDS_Shape S = BRepPrimAPI_MakeTorus(100, 20);
1903 void Application::sphere()
1905 TopoDS_Shape S = BRepPrimAPI_MakeSphere(100);
1909 void Application::load(const TopoDS_Shape& S)
1911 myViewer->getIC()->EraseAll(false);
1913 // Delete voxels of previous shape.
1916 delete myBoolVoxels;
1921 delete myColorVoxels;
1927 double xmin, ymin, zmin, xmax, ymax, zmax, length = 0;
1928 BRepBndLib::Add(S, box);
1929 box.Get(xmin, ymin, zmin, xmax, ymax, zmax);
1930 length = xmax - xmin > ymax - ymin ? xmax - xmin : ymax - ymin;
1931 length = length > zmax - zmin ? length : zmax - zmin;
1933 myViewer->getView()->SetSize(length);
1934 myViewer->getView()->SetZSize(length);
1937 if (myShape.IsNull())
1939 myShape = new AIS_Shape(S);
1940 myShape->SetDisplayMode(1);
1945 myViewer->getIC()->RecomputePrsOnly(myShape, false);
1947 if (myViewer->getIC()->IsDisplayed(myShape))
1948 myViewer->getIC()->Redisplay(myShape, false);
1950 myViewer->getIC()->Display(myShape, false);
1951 myViewer->getView()->FitAll();
1954 void Application::displayCut()
1956 myViewer->getIC()->EraseAll(false);
1958 // Make a sphere with a lot of toruses,
1959 // cut the toruses from the sphere.
1960 TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(100.0);
1961 TopoDS_Shape torus1 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 80, 0, 20), gp::DZ()), 30, 10);
1962 TopoDS_Shape torus2 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 0, 80, 20), gp::DZ()), 30, 10);
1963 TopoDS_Shape torus3 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt(-80, 0, 20), gp::DZ()), 30, 10);
1964 TopoDS_Shape torus4 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 0, -80, 20), gp::DZ()), 30, 10);
1966 // Compute bounding box of the shapes
1968 BRepBndLib::Add(sphere, box);
1969 BRepBndLib::Add(torus1, box);
1970 BRepBndLib::Add(torus2, box);
1971 BRepBndLib::Add(torus3, box);
1972 BRepBndLib::Add(torus4, box);
1977 delete myColorVoxels;
1982 delete myBoolVoxels;
1989 // Create a cube of voxels
1990 int nbx = 100, nby = 100, nbz = 100;
1991 double xmin, ymin, zmin, xmax, ymax, zmax;
1992 box.Get(xmin, ymin, zmin, xmax, ymax, zmax);
1993 myColorVoxels = new Voxel_ColorDS(xmin, ymin, zmin,
1994 xmax - xmin, ymax - ymin, zmax - zmin,
1996 Voxel_ColorDS vtorus(xmin, ymin, zmin,
1997 xmax - xmin, ymax - ymin, zmax - zmin,
2000 // Make a cube of voxels for the sphere.
2002 Voxel_FastConverter converter(sphere, *myColorVoxels, 0.1, nbx, nby, nbz);
2003 converter.Convert(progress);
2004 converter.FillInVolume(15);
2007 Voxel_FastConverter converter1(torus1, vtorus, 0.1, nbx, nby, nbz);
2008 converter1.Convert(progress);
2009 converter1.FillInVolume(3);
2012 Voxel_FastConverter converter2(torus2, vtorus, 0.1, nbx, nby, nbz);
2013 converter2.Convert(progress);
2014 converter2.FillInVolume(7);
2017 Voxel_FastConverter converter3(torus3, vtorus, 0.1, nbx, nby, nbz);
2018 converter3.Convert(progress);
2019 converter3.FillInVolume(10);
2022 Voxel_FastConverter converter4(torus4, vtorus, 0.1, nbx, nby, nbz);
2023 converter4.Convert(progress);
2024 converter4.FillInVolume(12);
2027 Voxel_BooleanOperation cutter;
2028 cutter.Cut(*myColorVoxels, vtorus);
2030 // Remove volumic voxels
2031 converter.FillInVolume(0);
2038 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
2039 myVoxels->SetUsageOfGLlists(true);
2040 myVoxels->SetBoolVoxels(myBoolVoxels);
2041 myVoxels->SetColorVoxels(myColorVoxels);
2042 if (myViewer->getIC()->IsDisplayed(myVoxels))
2043 myViewer->getIC()->Redisplay(myVoxels, false);
2045 myViewer->getIC()->Display(myVoxels, false);
2046 myViewer->getView()->FitAll();
2049 void Application::displayCollisions()
2051 myViewer->getIC()->EraseAll(false);
2053 // Make a big box with a lot of small spheres inside.
2054 double x = 0.0, y = 0.0, z = 0.0, xlen = 100.0, ylen = 100.0, zlen = 100.0, r = 10.0;
2055 gp_Pnt P1(x, y, z); // center point of moving sphere (S1).
2056 TopoDS_Shape B = BRepPrimAPI_MakeBox(gp_Pnt(x-r, y-r, z-r), gp_Pnt(xlen+r, ylen+r, zlen+r));
2057 TopoDS_Shape S1 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, z), gp::DZ()), r / 2.0);
2058 TopoDS_Shape S2 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, z), gp::DZ()), r);
2059 TopoDS_Shape S3 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, z), gp::DZ()), r);
2060 TopoDS_Shape S4 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., z), gp::DZ()), r);
2061 TopoDS_Shape S5 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., z), gp::DZ()), r);
2062 TopoDS_Shape S6 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., z), gp::DZ()), r);
2063 TopoDS_Shape S7 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, z), gp::DZ()), r);
2064 TopoDS_Shape S8 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, z), gp::DZ()), r);
2065 TopoDS_Shape S9 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, z), gp::DZ()), r);
2066 TopoDS_Shape S10 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, zlen/2.), gp::DZ()), r);
2067 TopoDS_Shape S11 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, zlen/2.), gp::DZ()), r);
2068 TopoDS_Shape S12 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, zlen/2.), gp::DZ()), r);
2069 TopoDS_Shape S13 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., zlen/2.), gp::DZ()), r);
2070 TopoDS_Shape S14 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., zlen/2.), gp::DZ()), r);
2071 TopoDS_Shape S15 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., zlen/2.), gp::DZ()), r);
2072 TopoDS_Shape S16 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, zlen/2.), gp::DZ()), r);
2073 TopoDS_Shape S17 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, zlen/2.), gp::DZ()), r);
2074 TopoDS_Shape S18 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, zlen/2.), gp::DZ()), r);
2075 TopoDS_Shape S19 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, zlen), gp::DZ()), r);
2076 TopoDS_Shape S20 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, zlen), gp::DZ()), r);
2077 TopoDS_Shape S21 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, zlen), gp::DZ()), r);
2078 TopoDS_Shape S22 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., zlen), gp::DZ()), r);
2079 TopoDS_Shape S23 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., zlen), gp::DZ()), r);
2080 TopoDS_Shape S24 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., zlen), gp::DZ()), r);
2081 TopoDS_Shape S25 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, zlen), gp::DZ()), r);
2082 TopoDS_Shape S26 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, zlen), gp::DZ()), r);
2083 TopoDS_Shape S27 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, zlen), gp::DZ()), r);
2085 // Planes of the big box
2086 gp_Ax2 xminusPlane(gp_Pnt(x, y, z), gp::DX());
2087 gp_Ax2 xplusPlane (gp_Pnt(xlen, y, z), gp::DX());
2088 gp_Ax2 yminusPlane(gp_Pnt(x, y, z), gp::DY());
2089 gp_Ax2 yplusPlane (gp_Pnt(x, ylen, z), gp::DY());
2090 gp_Ax2 zminusPlane(gp_Pnt(x, y, z), gp::DZ());
2091 gp_Ax2 zplusPlane (gp_Pnt(x, y, zlen), gp::DZ());
2096 delete myColorVoxels;
2101 delete myBoolVoxels;
2105 // Prepare visualization
2107 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
2108 myVoxels->SetColor(Quantity_NOC_RED);
2109 myVoxels->SetPointSize(4);
2110 myVoxels->SetSmoothPoints(false);
2111 myVoxels->SetUsageOfGLlists(false);
2112 myVoxels->SetColorVoxels(myColorVoxels);
2114 // Display all shapes
2115 double transparency = 0.9;
2116 Handle(AIS_Shape) aisB = new AIS_Shape(B);
2117 Handle(AIS_Shape) aisS1 = new AIS_Shape(S1);
2118 Handle(AIS_Shape) aisS2 = new AIS_Shape(S2);
2119 Handle(AIS_Shape) aisS3 = new AIS_Shape(S3);
2120 Handle(AIS_Shape) aisS4 = new AIS_Shape(S4);
2121 Handle(AIS_Shape) aisS5 = new AIS_Shape(S5);
2122 Handle(AIS_Shape) aisS6 = new AIS_Shape(S6);
2123 Handle(AIS_Shape) aisS7 = new AIS_Shape(S7);
2124 Handle(AIS_Shape) aisS8 = new AIS_Shape(S8);
2125 Handle(AIS_Shape) aisS9 = new AIS_Shape(S9);
2126 Handle(AIS_Shape) aisS10 = new AIS_Shape(S10);
2127 Handle(AIS_Shape) aisS11 = new AIS_Shape(S11);
2128 Handle(AIS_Shape) aisS12 = new AIS_Shape(S12);
2129 Handle(AIS_Shape) aisS13 = new AIS_Shape(S13);
2130 Handle(AIS_Shape) aisS14 = new AIS_Shape(S14);
2131 Handle(AIS_Shape) aisS15 = new AIS_Shape(S15);
2132 Handle(AIS_Shape) aisS16 = new AIS_Shape(S16);
2133 Handle(AIS_Shape) aisS17 = new AIS_Shape(S17);
2134 Handle(AIS_Shape) aisS18 = new AIS_Shape(S18);
2135 Handle(AIS_Shape) aisS19 = new AIS_Shape(S19);
2136 Handle(AIS_Shape) aisS20 = new AIS_Shape(S20);
2137 Handle(AIS_Shape) aisS21 = new AIS_Shape(S21);
2138 Handle(AIS_Shape) aisS22 = new AIS_Shape(S22);
2139 Handle(AIS_Shape) aisS23 = new AIS_Shape(S23);
2140 Handle(AIS_Shape) aisS24 = new AIS_Shape(S24);
2141 Handle(AIS_Shape) aisS25 = new AIS_Shape(S25);
2142 Handle(AIS_Shape) aisS26 = new AIS_Shape(S26);
2143 Handle(AIS_Shape) aisS27 = new AIS_Shape(S27);
2144 aisS1-> SetDisplayMode(1);
2145 aisS2-> SetDisplayMode(1);
2146 aisS3-> SetDisplayMode(1);
2147 aisS4-> SetDisplayMode(1);
2148 aisS5-> SetDisplayMode(1);
2149 aisS6-> SetDisplayMode(1);
2150 aisS7-> SetDisplayMode(1);
2151 aisS8-> SetDisplayMode(1);
2152 aisS9-> SetDisplayMode(1);
2153 aisS10->SetDisplayMode(1);
2154 aisS11->SetDisplayMode(1);
2155 aisS12->SetDisplayMode(1);
2156 aisS13->SetDisplayMode(1);
2157 aisS14->SetDisplayMode(1);
2158 aisS15->SetDisplayMode(1);
2159 aisS16->SetDisplayMode(1);
2160 aisS17->SetDisplayMode(1);
2161 aisS18->SetDisplayMode(1);
2162 aisS19->SetDisplayMode(1);
2163 aisS20->SetDisplayMode(1);
2164 aisS21->SetDisplayMode(1);
2165 aisS22->SetDisplayMode(1);
2166 aisS23->SetDisplayMode(1);
2167 aisS24->SetDisplayMode(1);
2168 aisS25->SetDisplayMode(1);
2169 aisS26->SetDisplayMode(1);
2170 aisS27->SetDisplayMode(1);
2171 aisS1-> SetTransparency(2.0 * transparency / 3.0);
2172 aisS2-> SetTransparency(transparency);
2173 aisS3-> SetTransparency(transparency);
2174 aisS4-> SetTransparency(transparency);
2175 aisS5-> SetTransparency(transparency);
2176 aisS6-> SetTransparency(transparency);
2177 aisS7-> SetTransparency(transparency);
2178 aisS8-> SetTransparency(transparency);
2179 aisS9-> SetTransparency(transparency);
2180 aisS10->SetTransparency(transparency);
2181 aisS11->SetTransparency(transparency);
2182 aisS12->SetTransparency(transparency);
2183 aisS13->SetTransparency(transparency);
2184 aisS14->SetTransparency(transparency);
2185 aisS15->SetTransparency(transparency);
2186 aisS16->SetTransparency(transparency);
2187 aisS17->SetTransparency(transparency);
2188 aisS18->SetTransparency(transparency);
2189 aisS19->SetTransparency(transparency);
2190 aisS20->SetTransparency(transparency);
2191 aisS21->SetTransparency(transparency);
2192 aisS22->SetTransparency(transparency);
2193 aisS23->SetTransparency(transparency);
2194 aisS24->SetTransparency(transparency);
2195 aisS25->SetTransparency(transparency);
2196 aisS26->SetTransparency(transparency);
2197 aisS27->SetTransparency(transparency);
2198 myViewer->getIC()->Display(aisB, false);
2199 myViewer->getIC()->Display(aisS1, false);
2200 myViewer->getIC()->Display(aisS2, false);
2201 myViewer->getIC()->Display(aisS3, false);
2202 myViewer->getIC()->Display(aisS4, false);
2203 myViewer->getIC()->Display(aisS5, false);
2204 myViewer->getIC()->Display(aisS6, false);
2205 myViewer->getIC()->Display(aisS7, false);
2206 myViewer->getIC()->Display(aisS8, false);
2207 myViewer->getIC()->Display(aisS9, false);
2208 myViewer->getIC()->Display(aisS10, false);
2209 myViewer->getIC()->Display(aisS11, false);
2210 myViewer->getIC()->Display(aisS12, false);
2211 myViewer->getIC()->Display(aisS13, false);
2212 myViewer->getIC()->Display(aisS14, false);
2213 myViewer->getIC()->Display(aisS15, false);
2214 myViewer->getIC()->Display(aisS16, false);
2215 myViewer->getIC()->Display(aisS17, false);
2216 myViewer->getIC()->Display(aisS18, false);
2217 myViewer->getIC()->Display(aisS19, false);
2218 myViewer->getIC()->Display(aisS20, false);
2219 myViewer->getIC()->Display(aisS21, false);
2220 myViewer->getIC()->Display(aisS22, false);
2221 myViewer->getIC()->Display(aisS23, false);
2222 myViewer->getIC()->Display(aisS24, false);
2223 myViewer->getIC()->Display(aisS25, false);
2224 myViewer->getIC()->Display(aisS26, false);
2225 myViewer->getIC()->Display(aisS27, false);
2227 // Prepare computer of collisions
2228 double deflection = 0.1;
2229 int nbx = 100, nby = 100, nbz = 100;
2231 Voxel_CollisionDetection coldet(deflection, nbx, nby, nbz);
2232 coldet.SetUsageOfVolume(false);
2233 coldet.KeepCollisions(false);
2234 coldet.AddShape(S1);
2235 coldet.AddShape(S2);
2236 coldet.AddShape(S3);
2237 coldet.AddShape(S4);
2238 coldet.AddShape(S5);
2239 coldet.AddShape(S6);
2240 coldet.AddShape(S7);
2241 coldet.AddShape(S8);
2242 coldet.AddShape(S9);
2243 coldet.AddShape(S10);
2244 coldet.AddShape(S11);
2245 coldet.AddShape(S12);
2246 coldet.AddShape(S13);
2247 coldet.AddShape(S14);
2248 coldet.AddShape(S15);
2249 coldet.AddShape(S16);
2250 coldet.AddShape(S17);
2251 coldet.AddShape(S18);
2252 coldet.AddShape(S19);
2253 coldet.AddShape(S20);
2254 coldet.AddShape(S21);
2255 coldet.AddShape(S22);
2256 coldet.AddShape(S23);
2257 coldet.AddShape(S24);
2258 coldet.AddShape(S25);
2259 coldet.AddShape(S26);
2260 coldet.AddShape(S27);
2261 //coldet.AddShape(BRepPrimAPI_MakeBox(gp_Pnt(x, y, z), gp_Pnt(xlen, ylen, zlen)));
2264 BRepBndLib::Add(B, box);
2265 coldet.SetBoundaryBox(box);
2269 // Move one of the spheres inside the box
2270 // and compute collisions
2272 gp_Vec vmove(1, 0.5, 0.25);
2274 int imove = 0, nb_moves = 900;
2275 while (imove < nb_moves)
2278 trsf.SetTranslation(vmove);
2279 TopLoc_Location loc(trsf);
2281 P1.Translate(vmove);
2283 // Check whether S1 is inside the big box
2284 // Detect the plane S1 touches to.
2286 vmove.Mirror(xminusPlane);
2287 else if (P1.X() > xlen)
2288 vmove.Mirror(xplusPlane);
2289 else if (P1.Y() < y)
2290 vmove.Mirror(yminusPlane);
2291 else if (P1.Y() > ylen)
2292 vmove.Mirror(yplusPlane);
2293 else if (P1.Z() < z)
2294 vmove.Mirror(zminusPlane);
2295 else if (P1.Z() > zlen)
2296 vmove.Mirror(zplusPlane);
2298 // Compute collisions
2299 coldet.ReplaceShape(1, S1);
2300 coldet.Voxelize(1); // only the first sphere (S1)
2302 myBoolVoxels = &((Voxel_BoolDS&) coldet.GetCollisions());
2306 myViewer->getIC()->Redisplay(aisS1, false);
2308 // Display the collisions
2309 myVoxels->SetBoolVoxels(myBoolVoxels);
2310 if (myViewer->getIC()->IsDisplayed(myVoxels))
2311 myViewer->getIC()->Redisplay(myVoxels, true);
2314 myViewer->getIC()->Display(myVoxels, false);
2315 myViewer->getView()->FitAll();
2319 qApp->processEvents();
2322 // Copy the result of collision detection
2324 myBoolVoxels = new Voxel_BoolDS(coldet.GetCollisions().GetX(),
2325 coldet.GetCollisions().GetY(),
2326 coldet.GetCollisions().GetZ(),
2327 coldet.GetCollisions().GetXLen(),
2328 coldet.GetCollisions().GetYLen(),
2329 coldet.GetCollisions().GetZLen(),
2331 for (ix = 0; ix < nbx; ix++)
2333 for (iy = 0; iy < nby; iy++)
2335 for (iz = 0; iz < nbz; iz++)
2337 if (coldet.GetCollisions().Get(ix, iy, iz))
2338 myBoolVoxels->Set(ix, iy, iz, Standard_True);
2342 myVoxels->SetBoolVoxels(myBoolVoxels);