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>
36 #include <Aspect_ColorScale.hxx>
40 Application::Application()
44 QMenu * file = menuBar()->addMenu( "&File" );
48 a = new QAction("Box", this);
49 connect(a, SIGNAL(triggered()), this, SLOT(box()));
52 a = new QAction("Cylinder", this);
53 connect(a, SIGNAL(triggered()), this, SLOT(cylinder()));
56 a = new QAction("Torus", this);
57 connect(a, SIGNAL(triggered()), this, SLOT(torus()));
60 a = new QAction("Sphere", this);
61 connect(a, SIGNAL(triggered()), this, SLOT(sphere()));
64 a = new QAction("Load shape...", this);
65 a->setShortcut(tr("Ctrl+O"));
66 connect(a, SIGNAL(triggered()), this, SLOT(choose()));
72 a = new QAction("Open", this);
73 connect(a, SIGNAL(triggered()), this, SLOT(open()));
77 a = new QAction("Save", this);
78 connect(a, SIGNAL(triggered()), this, SLOT(save()));
84 a = new QAction("&Quit", this);
85 a->setShortcut(tr("Ctrl+Q"));
86 connect(a, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
89 menuBar()->addSeparator();
93 QMenu * test = menuBar()->addMenu( "Test" );
95 a = new QAction("Test boolean", this);
96 connect(a, SIGNAL(triggered()), this, SLOT(testBoolDS()));
99 a = new QAction("Test color", this);
100 connect(a, SIGNAL(triggered()), this, SLOT(testColorDS()));
103 a = new QAction("Test float", this);
104 connect(a, SIGNAL(triggered()), this, SLOT(testFloatDS()));
107 a = new QAction("Test boolean / 8", this);
108 connect(a, SIGNAL(triggered()), this, SLOT(testOctBoolDS()));
111 a = new QAction("Test boolean / 8 / 8..", this);
112 connect(a, SIGNAL(triggered()), this, SLOT(testROctBoolDS()));
115 test->addSeparator();
117 a = new QAction("Test fusion of booleans", this);
118 connect(a, SIGNAL(triggered()), this, SLOT(testFuseBoolDS()));
121 a = new QAction("Test fusion of colors", this);
122 connect(a, SIGNAL(triggered()), this, SLOT(testFuseColorDS()));
125 a = new QAction("Test fusion of floating-points", this);
126 connect(a, SIGNAL(triggered()), this, SLOT(testFuseFloatDS()));
129 a = new QAction("Test cutting of booleans", this);
130 connect(a, SIGNAL(triggered()), this, SLOT(testCutBoolDS()));
133 a = new QAction("Test cutting of booleans", this);
134 connect(a, SIGNAL(triggered()), this, SLOT(testCutColorDS()));
137 a = new QAction("Test cutting of floating-points", this);
138 connect(a, SIGNAL(triggered()), this, SLOT(testCutFloatDS()));
143 QMenu * converter = menuBar()->addMenu( "Converter" );
147 a = new QAction("Number of splits along X", this);
148 connect(a, SIGNAL(triggered()), this, SLOT(setNbX()));
149 converter->addAction(a);
151 a = new QAction("Number of splits along Y", this);
152 connect(a, SIGNAL(triggered()), this, SLOT(setNbY()));
153 converter->addAction(a);
155 a = new QAction("Number of splits along Z", this);
156 connect(a, SIGNAL(triggered()), this, SLOT(setNbZ()));
157 converter->addAction(a);
159 converter->addSeparator();
161 a = new QAction("Side of scanning", this);
162 connect(a, SIGNAL(triggered()), this, SLOT(setScanSide()));
163 converter->addAction(a);
165 converter->addSeparator();
167 a = new QAction("Volumic value of 1bit voxels", this);
168 connect(a, SIGNAL(triggered()), this, SLOT(setVolumicBoolValue()));
169 converter->addAction(a);
171 a = new QAction("Volumic value of 4bit voxels", this);
172 connect(a, SIGNAL(triggered()), this, SLOT(setVolumicColorValue()));
173 converter->addAction(a);
175 converter->addSeparator();
179 a = new QAction("Convert to 1bit voxels", this);
180 connect(a, SIGNAL(triggered()), this, SLOT(convert2bool()));
181 converter->addAction(a);
183 a = new QAction("Convert to 4bit voxels", this);
184 connect(a, SIGNAL(triggered()), this, SLOT(convert2color()));
185 converter->addAction(a);
187 QMenu * vis = menuBar()->addMenu( "&Visualization" );
189 a = new QAction("Points", this);
190 connect(a, SIGNAL(triggered()), this, SLOT(displayPoints()));
195 a = new QAction("Nearest points", this);
196 connect(a, SIGNAL(triggered()), this, SLOT(displayNearestPoints()));
201 a = new QAction("Boxes", this);
202 connect(a, SIGNAL(triggered()), this, SLOT(displayBoxes()));
207 a = new QAction("Nearest boxes", this);
208 connect(a, SIGNAL(triggered()), this, SLOT(displayNearestBoxes()));
215 a = new QAction("Point size", this);
216 connect(a, SIGNAL(triggered()), this, SLOT(setPointSize()));
219 a = new QAction("Quadrangle size (%)", this);
220 connect(a, SIGNAL(triggered()), this, SLOT(setQuadrangleSize()));
225 a = new QAction("Color min value", this);
226 connect(a, SIGNAL(triggered()), this, SLOT(setColorMinValue()));
229 a = new QAction("Color max value", this);
230 connect(a, SIGNAL(triggered()), this, SLOT(setColorMaxValue()));
237 a = new QAction("Use GL lists", this);
238 connect(a, SIGNAL(triggered()), this, SLOT(setUsageOfGLlists()));
245 a = new QAction("Displayed X min", this);
246 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedXMin()));
249 a = new QAction("Displayed X max", this);
250 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedXMax()));
253 a = new QAction("Displayed Y min", this);
254 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedYMin()));
257 a = new QAction("Displayed Y max", this);
258 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedYMax()));
261 a = new QAction("Displayed Z min", this);
262 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedZMin()));
265 a = new QAction("Displayed Z max", this);
266 connect(a, SIGNAL(triggered()), this, SLOT(setDisplayedZMax()));
270 QMenu * demo = menuBar()->addMenu( "Demo" );
272 a = new QAction("Waves", this);
273 connect(a, SIGNAL(triggered()), this, SLOT(displayWaves()));
276 a = new QAction("Cut", this);
277 connect(a, SIGNAL(triggered()), this, SLOT(displayCut()));
280 a = new QAction("Collisions", this);
281 connect(a, SIGNAL(triggered()), this, SLOT(displayCollisions()));
285 QMenu * help = menuBar()->addMenu( "Help" );
287 a = new QAction("About", this);
288 a->setShortcut(tr("F1"));
289 connect(a, SIGNAL(triggered()), this, SLOT(about()));
293 myViewer = new Viewer( this );
294 myViewer->setFocus();
295 setCentralWidget( myViewer );
296 statusBar()->showMessage( "Ready", 2000 );
304 myVolumicBoolValue = false;
305 myVolumicColorValue = 0;
307 myQuadrangleSize = 40;
310 myColorMaxValue = 15;
315 myDisplayedXMin = -DBL_MAX;
316 myDisplayedXMax = DBL_MAX;
317 myDisplayedYMin = -DBL_MAX;
318 myDisplayedYMax = DBL_MAX;
319 myDisplayedZMin = -DBL_MAX;
320 myDisplayedZMax = DBL_MAX;
322 VoxelClient_VisDrawer::Init();
327 Application::~Application()
332 delete myColorVoxels;
335 void Application::choose()
337 QString fn = QFileDialog::getOpenFileName( this, QString::null, QString::null, "*.brep");
341 statusBar()->showMessage( "Loading aborted", 2000 );
344 void Application::load( const QString &fileName )
347 if ( !f.open( QIODevice::ReadOnly ) )
353 if (!BRepTools::Read(S, (char*) fileName.constData(), B))
354 statusBar()->showMessage( "Loading failed", 2000 );
359 void Application::open()
361 QString fn = QFileDialog::getOpenFileName( this, QString::null, QString::null, "*.vx");
362 if ( fn.isEmpty() || !QFile::exists(fn) )
364 statusBar()->showMessage( "Open aborted", 2000 );
373 if (!reader.Read((char*)fn.constData()))
375 statusBar()->showMessage( "Open failed... sorry", 2000 );
382 // Release current voxels
390 delete myColorVoxels;
395 if (reader.IsBoolVoxels())
397 myBoolVoxels = (Voxel_BoolDS*) reader.GetBoolVoxels();
398 myViewer->getSelector().SetVoxels(*myBoolVoxels);
400 else if (reader.IsColorVoxels())
402 myColorVoxels = (Voxel_ColorDS*) reader.GetColorVoxels();
403 myViewer->getSelector().SetVoxels(*myColorVoxels);
406 // Display the voxels
407 myViewer->getIC()->EraseAll(false, false);
408 Voxel_DS* ds = myBoolVoxels;
413 myDisplayedXMin = ds->GetX() - 10.0 * Precision::Confusion();
414 myDisplayedXMax = ds->GetX() + ds->GetXLen() + 10.0 * Precision::Confusion();
415 myDisplayedYMin = ds->GetY() - 10.0 * Precision::Confusion();
416 myDisplayedYMax = ds->GetY() + ds->GetYLen() + 10.0 * Precision::Confusion();
417 myDisplayedZMin = ds->GetZ() - 10.0 * Precision::Confusion();
418 myDisplayedZMax = ds->GetZ() + ds->GetZLen() + 10.0 * Precision::Confusion();
424 // Set voxels and display
425 Handle(Poly_Triangulation) empty;
426 myVoxels->SetBoolVoxels(myBoolVoxels);
427 myVoxels->SetColorVoxels(myColorVoxels);
428 myVoxels->SetTriangulation(empty);
429 if (myViewer->getIC()->IsDisplayed(myVoxels))
430 myViewer->getIC()->Redisplay(myVoxels, false);
432 myViewer->getIC()->Display(myVoxels, false);
438 myViewer->getView()->ColorScaleErase();
440 myViewer->getView()->FitAll();
442 statusBar()->showMessage( "Ready.", 2000 );
445 void Application::save()
447 QString fn = QFileDialog::getSaveFileName( this, QString::null, QString::null, "*.vx");
450 statusBar()->showMessage( "Storage aborted", 2000 );
453 if (fn.indexOf(".vx", -1, Qt::CaseInsensitive) == -1)
461 writer.SetFormat(Voxel_VFF_BINARY);
463 writer.SetVoxels(*myBoolVoxels);
464 else if (myColorVoxels)
465 writer.SetVoxels(*myColorVoxels);
468 statusBar()->showMessage( "Nothing to store", 2000 );
471 if (!writer.Write((char*)fn.constData()))
473 statusBar()->showMessage( "Storage failed... sorry", 2000 );
480 statusBar()->showMessage( "Saved.", 2000 );
483 void Application::closeEvent( QCloseEvent* ce )
488 void Application::about()
490 QMessageBox::about( this, "Voxel demo-application",
491 "This example demonstrates simple usage of "
492 "voxel models of Open CASCADE.");
495 void Application::testBoolDS()
499 int nbx = 100, nby = 100, nbz = 100;
506 Voxel_BoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
508 for (ix = 0; ix < nbx; ix++)
510 for (iy = 0; iy < nby; iy++)
512 for (iz = 0; iz < nbz; iz++)
515 ds.Set(ix, iy, iz, false);
517 ds.Set(ix, iy, iz, true);
522 for (ix = 0; ix < nbx; ix++)
524 for (iy = 0; iy < nby; iy++)
526 for (iz = 0; iz < nbz; iz++)
528 bool value = ds.Get(ix, iy, iz) == Standard_True;
532 cout<<"Wrong value!"<<endl;
537 cout<<"Wrong value!"<<endl;
544 timer.Print("BoolDS");
547 void Application::testColorDS()
551 int nbx = 100, nby = 100, nbz = 100;
558 Voxel_ColorDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
560 for (ix = 0; ix < nbx; ix++)
562 for (iy = 0; iy < nby; iy++)
564 for (iz = 0; iz < nbz; iz++)
567 ds.Set(ix, iy, iz, 8);
569 ds.Set(ix, iy, iz, 7);
574 for (ix = 0; ix < nbx; ix++)
576 for (iy = 0; iy < nby; iy++)
578 for (iz = 0; iz < nbz; iz++)
580 unsigned char value = ds.Get(ix, iy, iz);
584 cout<<"Wrong value!"<<endl;
589 cout<<"Wrong value!"<<endl;
596 timer.Print("ColorDS");
599 void Application::testFloatDS()
603 int nbx = 100, nby = 100, nbz = 100;
610 Voxel_FloatDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
612 for (ix = 0; ix < nbx; ix++)
614 for (iy = 0; iy < nby; iy++)
616 for (iz = 0; iz < nbz; iz++)
619 ds.Set(ix, iy, iz, 8.8f);
621 ds.Set(ix, iy, iz, 7.7f);
626 for (ix = 0; ix < nbx; ix++)
628 for (iy = 0; iy < nby; iy++)
630 for (iz = 0; iz < nbz; iz++)
632 float value = ds.Get(ix, iy, iz);
636 cout<<"Wrong value!"<<endl;
641 cout<<"Wrong value!"<<endl;
648 timer.Print("FloatDS");
651 void Application::testOctBoolDS()
655 int nbx = 30, nby = 30, nbz = 30;
662 Voxel_OctBoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
664 for (ix = 0; ix < nbx; ix++)
666 for (iy = 0; iy < nby; iy++)
668 for (iz = 0; iz < nbz; iz++)
672 ds.Set(ix, iy, iz, true);
676 for (int i = 0; i < 8; i++)
679 ds.Set(ix, iy, iz, i, true);
681 ds.Set(ix, iy, iz, i, false);
688 for (ix = 0; ix < nbx; ix++)
690 for (iy = 0; iy < nby; iy++)
692 for (iz = 0; iz < nbz; iz++)
696 bool value = ds.Get(ix, iy, iz) == Standard_True;
698 cout<<"Wrong value!"<<endl;
702 for (int i = 0; i < 8; i++)
706 bool value = ds.Get(ix, iy, iz, i) == Standard_True;
708 cout<<"Wrong value!"<<endl;
712 bool value = ds.Get(ix, iy, iz, i) == Standard_True;
714 cout<<"Wrong value!"<<endl;
722 for (ix = 0; ix < nbx; ix++)
724 for (iy = 0; iy < nby; iy++)
726 for (iz = 0; iz < nbz; iz++)
730 for (int i = 0; i < 8; i++)
732 ds.Set(ix, iy, iz, i, true);
737 for (int i = 0; i < 8; i++)
739 ds.Set(ix, iy, iz, i, false);
749 timer.Print("OctBoolDS");
752 void Application::testROctBoolDS()
755 int ix, iy, iz, i, j;
756 int nbx = 30, nby = 30, nbz = 30;
762 Voxel_ROctBoolDS ds(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
764 for (ix = 0; ix < nbx; ix++)
766 for (iy = 0; iy < nby; iy++)
768 for (iz = 0; iz < nbz; iz++)
770 ds.Set(ix, iy, iz, true);
775 for (ix = 0; ix < nbx; ix++)
777 for (iy = 0; iy < nby; iy++)
779 for (iz = 0; iz < nbz; iz++)
781 for (i = 0; i < 8; i++)
783 for (j = 0; j < 8; j++)
785 ds.Set(ix, iy, iz, i, j, true);
794 for (ix = 0; ix < nbx; ix++)
796 for (iy = 0; iy < nby; iy++)
798 for (iz = 0; iz < nbz; iz++)
800 if (ds.Deepness(ix, iy, iz) == 0)
802 bool value = ds.Get(ix, iy, iz);
804 cout<<"Wrong value..."<<endl;
806 if (ds.Deepness(ix, iy, iz) == 1)
808 for (i = 0; i < 8; i++)
810 bool value = ds.Get(ix, iy, iz, i);
812 cout<<"Wrong value..."<<endl;
815 if (ds.Deepness(ix, iy, iz) == 2)
817 for (i = 0; i < 8; i++)
819 for (j = 0; j < 8; j++)
821 bool value = ds.Get(ix, iy, iz, i, j);
823 cout<<"Wrong value..."<<endl;
832 timer.Print("ROctBoolDS");
836 TopoDS_Shape S = BRepPrimAPI_MakeSphere(100.0);
841 Voxel_ROctBoolDS* ds2 = new Voxel_ROctBoolDS;
842 Voxel_FastConverter converter(S, *ds2, 0.1, myNbX, myNbY, myNbZ, 1);
843 converter.Convert(progress);
844 ds2->OptimizeMemory();
847 timer.Print("ROctBoolDS::converter");
851 myViewer->getIC()->EraseAll(false, false);
853 myVoxels->SetBoolVoxels(0);
854 myVoxels->SetColorVoxels(0);
855 Handle(Poly_Triangulation) empty;
856 myVoxels->SetTriangulation(empty);
857 myVoxels->SetROctBoolVoxels(ds2);
858 myViewer->getIC()->Display(myVoxels, false);
859 myViewer->getView()->ColorScaleErase();
860 myViewer->getView()->FitAll();
861 myViewer->getSelector().SetVoxels(*ds2);
864 void Application::testFuseBoolDS()
868 int nbx = 100, nby = 100, nbz = 100;
871 // 1. Set two BoolDS:
875 Voxel_BoolDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
876 Voxel_BoolDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
878 for (ix = 0; ix < nbx; ix++)
880 for (iy = 0; iy < nby; iy++)
882 for (iz = 0; iz < nbz; iz++)
885 ds2.Set(ix, iy, iz, false);
887 ds2.Set(ix, iy, iz, true);
894 Voxel_BooleanOperation fuser;
895 if (!fuser.Fuse(ds1, ds2))
896 cout<<"The operation failed..."<<endl;
900 for (ix = 0; ix < nbx; ix++)
902 for (iy = 0; iy < nby; iy++)
904 for (iz = 0; iz < nbz; iz++)
906 bool value = ds1.Get(ix, iy, iz) == Standard_True;
910 cout<<"Wrong value!"<<endl;
915 cout<<"Wrong value!"<<endl;
922 timer.Print("Fusion of BoolDS");
925 void Application::testFuseColorDS()
929 int nbx = 100, nby = 100, nbz = 100;
932 // 1. Set two ColorDS:
936 Voxel_ColorDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
937 Voxel_ColorDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
939 for (ix = 0; ix < nbx; ix++)
941 for (iy = 0; iy < nby; iy++)
943 for (iz = 0; iz < nbz; iz++)
945 ds1.Set(ix, iy, iz, 11);
949 for (ix = 0; ix < nbx; ix++)
951 for (iy = 0; iy < nby; iy++)
953 for (iz = 0; iz < nbz; iz++)
956 ds2.Set(ix, iy, iz, 3);
958 ds2.Set(ix, iy, iz, 5);
965 Voxel_BooleanOperation fuser;
966 if (!fuser.Fuse(ds1, ds2))
967 cout<<"The operation failed..."<<endl;
971 for (ix = 0; ix < nbx; ix++)
973 for (iy = 0; iy < nby; iy++)
975 for (iz = 0; iz < nbz; iz++)
977 unsigned char value = ds1.Get(ix, iy, iz);
981 cout<<"Wrong value!"<<endl;
986 cout<<"Wrong value!"<<endl;
993 timer.Print("Fusion of ColorDS");
996 void Application::testFuseFloatDS()
1000 int nbx = 100, nby = 100, nbz = 100;
1003 // 1. Set two FloatDS:
1007 Voxel_FloatDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1008 Voxel_FloatDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1010 for (ix = 0; ix < nbx; ix++)
1012 for (iy = 0; iy < nby; iy++)
1014 for (iz = 0; iz < nbz; iz++)
1016 ds1.Set(ix, iy, iz, 11.1f);
1020 for (ix = 0; ix < nbx; ix++)
1022 for (iy = 0; iy < nby; iy++)
1024 for (iz = 0; iz < nbz; iz++)
1027 ds2.Set(ix, iy, iz, 3.3f);
1029 ds2.Set(ix, iy, iz, 5.5f);
1036 Voxel_BooleanOperation fuser;
1037 if (!fuser.Fuse(ds1, ds2))
1038 cout<<"The operation failed..."<<endl;
1042 for (ix = 0; ix < nbx; ix++)
1044 for (iy = 0; iy < nby; iy++)
1046 for (iz = 0; iz < nbz; iz++)
1048 float value = ds1.Get(ix, iy, iz);
1051 if (fabs(value - 14.4f) > 0.001)
1052 cout<<"Wrong value!"<<endl;
1056 if (fabs(value - 16.6f) > 0.001)
1057 cout<<"Wrong value!"<<endl;
1064 timer.Print("Fusion of FloatDS");
1067 void Application::testCutBoolDS()
1071 int nbx = 100, nby = 100, nbz = 100;
1074 // 1. Set two BoolDS:
1078 Voxel_BoolDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1079 Voxel_BoolDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1081 for (ix = 0; ix < nbx; ix++)
1083 for (iy = 0; iy < nby; iy++)
1085 for (iz = 0; iz < nbz; iz++)
1087 ds1.Set(ix, iy, iz, true);
1091 for (ix = 0; ix < nbx; ix++)
1093 for (iy = 0; iy < nby; iy++)
1095 for (iz = 0; iz < nbz; iz++)
1098 ds2.Set(ix, iy, iz, false);
1100 ds2.Set(ix, iy, iz, true);
1107 Voxel_BooleanOperation cutter;
1108 if (!cutter.Cut(ds1, ds2))
1109 cout<<"The operation failed..."<<endl;
1113 for (ix = 0; ix < nbx; ix++)
1115 for (iy = 0; iy < nby; iy++)
1117 for (iz = 0; iz < nbz; iz++)
1119 bool value = ds1.Get(ix, iy, iz) == Standard_True;
1123 cout<<"Wrong value!"<<endl;
1128 cout<<"Wrong value!"<<endl;
1135 timer.Print("Cut of BoolDS");
1138 void Application::testCutColorDS()
1142 int nbx = 100, nby = 100, nbz = 100;
1145 // 1. Set two ColorDS:
1149 Voxel_ColorDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1150 Voxel_ColorDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1152 for (ix = 0; ix < nbx; ix++)
1154 for (iy = 0; iy < nby; iy++)
1156 for (iz = 0; iz < nbz; iz++)
1158 ds1.Set(ix, iy, iz, 11);
1162 for (ix = 0; ix < nbx; ix++)
1164 for (iy = 0; iy < nby; iy++)
1166 for (iz = 0; iz < nbz; iz++)
1169 ds2.Set(ix, iy, iz, 3);
1171 ds2.Set(ix, iy, iz, 5);
1178 Voxel_BooleanOperation cutter;
1179 if (!cutter.Cut(ds1, ds2))
1180 cout<<"The operation failed..."<<endl;
1184 for (ix = 0; ix < nbx; ix++)
1186 for (iy = 0; iy < nby; iy++)
1188 for (iz = 0; iz < nbz; iz++)
1190 unsigned char value = ds1.Get(ix, iy, iz);
1194 cout<<"Wrong value!"<<endl;
1199 cout<<"Wrong value!"<<endl;
1206 timer.Print("Cut of ColorDS");
1209 void Application::testCutFloatDS()
1213 int nbx = 100, nby = 100, nbz = 100;
1216 // 1. Set two FloatDS:
1220 Voxel_FloatDS ds1(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1221 Voxel_FloatDS ds2(0, 0, 0, 1, 1, 1, nbx, nby, nbz);
1223 for (ix = 0; ix < nbx; ix++)
1225 for (iy = 0; iy < nby; iy++)
1227 for (iz = 0; iz < nbz; iz++)
1229 ds1.Set(ix, iy, iz, 11.1f);
1233 for (ix = 0; ix < nbx; ix++)
1235 for (iy = 0; iy < nby; iy++)
1237 for (iz = 0; iz < nbz; iz++)
1240 ds2.Set(ix, iy, iz, 3.3f);
1242 ds2.Set(ix, iy, iz, 5.5f);
1249 Voxel_BooleanOperation cutter;
1250 if (!cutter.Cut(ds1, ds2))
1251 cout<<"The operation failed..."<<endl;
1255 for (ix = 0; ix < nbx; ix++)
1257 for (iy = 0; iy < nby; iy++)
1259 for (iz = 0; iz < nbz; iz++)
1261 float value = ds1.Get(ix, iy, iz);
1264 if (fabs(value - 7.8f) > 0.001)
1265 cout<<"Wrong value!"<<endl;
1269 if (fabs(value - 5.6f) > 0.001)
1270 cout<<"Wrong value!"<<endl;
1277 timer.Print("Cut of FloatDS");
1280 void Application::convert2bool()
1285 void Application::convert2color()
1290 void Application::convert(const int ivoxel)
1293 if (!myShape.IsNull())
1294 S = myShape->Shape();
1297 QMessageBox::warning( this, "Voxel demo-application", "No shape for conversion!");
1306 myBoolVoxels = new Voxel_BoolDS;
1309 delete myColorVoxels;
1317 myColorVoxels = new Voxel_ColorDS;
1320 delete myBoolVoxels;
1336 Voxel_Converter converter(S, *myBoolVoxels, myNbX, myNbY, myNbZ);
1337 if (!converter.Convert(progress, myVolumicBoolValue, myScanSide))
1339 QMessageBox::warning( this, "Voxel demo-application", "Conversion failed...");
1345 Voxel_Converter converter(S, *myBoolVoxels, myNbX, myNbY, myNbZ, 2);
1346 ConversionThread thread1, thread2;
1348 thread1.setConverter(&converter);
1349 thread2.setConverter(&converter);
1351 thread1.setVolumicValue(myVolumicBoolValue);
1352 thread2.setVolumicValue(myVolumicBoolValue);
1354 thread1.setScanSide(myScanSide);
1355 thread2.setScanSide(myScanSide);
1357 thread1.setThreadIndex(1);
1358 thread2.setThreadIndex(2);
1363 while (thread1.running() || thread2.running())
1371 Voxel_FastConverter converter(S, *myBoolVoxels, 0.1, myNbX, myNbY, myNbZ, 1);
1372 converter.Convert(progress, 1);
1373 //if (myVolumicBoolValue)
1374 // converter.FillInVolume(myVolumicBoolValue);
1377 Voxel_FastConverter converter(S, *myBoolVoxels, 0.1, myNbX, myNbY, myNbZ, 2);
1378 ConversionThread thread1, thread2;
1380 thread1.setConverter(&converter);
1381 thread2.setConverter(&converter);
1383 thread1.setThreadIndex(1);
1384 thread2.setThreadIndex(2);
1389 while (thread1.isRunning() || thread2.isRunning())
1394 timer.Print("Converter");
1396 myViewer->getSelector().SetVoxels(*myBoolVoxels);
1407 Voxel_Converter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ);
1408 if (!converter.Convert(progress, myVolumicColorValue, myScanSide))
1410 QMessageBox::warning( this, "Voxel demo-application", "Conversion failed...");
1416 Voxel_Converter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ, 2);
1417 ConversionThread thread1, thread2;
1419 thread1.setConverter(&converter);
1420 thread2.setConverter(&converter);
1422 thread1.setVolumicValue(myVolumicColorValue);
1423 thread2.setVolumicValue(myVolumicColorValue);
1425 thread1.setScanSide(myScanSide);
1426 thread2.setScanSide(myScanSide);
1428 thread1.setThreadIndex(1);
1429 thread2.setThreadIndex(2);
1434 while (thread1.running() || thread2.running())
1442 Voxel_FastConverter converter(S, *myColorVoxels, myNbX, myNbY, myNbZ, 1);
1443 converter.Convert(progress, 1);
1444 if (myVolumicColorValue)
1445 converter.FillInVolume(myVolumicColorValue);
1448 Voxel_FastConverter converter(S, *myColorVoxels, 0.1, myNbX, myNbY, myNbZ, 2);
1449 ConversionThread thread1, thread2;
1451 thread1.setConverter(&converter);
1452 thread2.setConverter(&converter);
1454 thread1.setThreadIndex(1);
1455 thread2.setThreadIndex(2);
1460 while (thread1.isRunning() || thread2.isRunning())
1465 timer.Print("Converter");
1468 // Set color for demonstration
1470 fabs(myColorVoxels->GetX()) > fabs(myColorVoxels->GetY()) ?
1471 fabs(myColorVoxels->GetX()) : fabs(myColorVoxels->GetY());
1472 maxd = maxd > fabs(myColorVoxels->GetZ()) ? maxd : fabs(myColorVoxels->GetZ());
1473 maxd = maxd > fabs(myColorVoxels->GetX() + myColorVoxels->GetXLen()) ?
1474 maxd : fabs(myColorVoxels->GetX() + myColorVoxels->GetXLen());
1475 maxd = maxd > fabs(myColorVoxels->GetY() + myColorVoxels->GetYLen()) ?
1476 maxd : fabs(myColorVoxels->GetY() + myColorVoxels->GetYLen());
1477 maxd = maxd > fabs(myColorVoxels->GetZ() + myColorVoxels->GetZLen()) ?
1478 maxd : fabs(myColorVoxels->GetZ() + myColorVoxels->GetZLen());
1479 for (int ix = 0; ix < myNbX; ix++)
1481 for (int iy = 0; iy < myNbY; iy++)
1483 for (int iz = 0; iz < myNbZ; iz++)
1485 unsigned char value = myColorVoxels->Get(ix, iy, iz);
1488 double xc, yc, zc, xd, yd, zd;
1489 myColorVoxels->GetCenter(ix, iy, iz, xc, yc, zc);
1493 double mind = xd < yd ? xd : yd;
1494 mind = zd < mind ? zd : mind;
1495 value = unsigned char(15.0 * (maxd - mind) / maxd);
1498 myColorVoxels->Set(ix, iy, iz, value);
1504 myViewer->getSelector().SetVoxels(*myColorVoxels);
1509 myViewer->getIC()->EraseAll(false, false);
1511 Voxel_DS* ds = myBoolVoxels;
1516 myDisplayedXMin = ds->GetX() - 10.0 * Precision::Confusion();
1517 myDisplayedXMax = ds->GetX() + ds->GetXLen() + 10.0 * Precision::Confusion();
1518 myDisplayedYMin = ds->GetY() - 10.0 * Precision::Confusion();
1519 myDisplayedYMax = ds->GetY() + ds->GetYLen() + 10.0 * Precision::Confusion();
1520 myDisplayedZMin = ds->GetZ() - 10.0 * Precision::Confusion();
1521 myDisplayedZMax = ds->GetZ() + ds->GetZLen() + 10.0 * Precision::Confusion();
1527 // Set voxels and display
1528 Handle(Poly_Triangulation) empty;
1529 myVoxels->SetBoolVoxels(myBoolVoxels);
1530 myVoxels->SetColorVoxels(myColorVoxels);
1531 myVoxels->SetTriangulation(empty);
1532 if (myViewer->getIC()->IsDisplayed(myVoxels))
1533 myViewer->getIC()->Redisplay(myVoxels, false);
1535 myViewer->getIC()->Display(myVoxels, false);
1539 displayColorScale();
1541 myViewer->getView()->ColorScaleErase();
1543 myViewer->getView()->FitAll();
1546 void Application::setNbX()
1550 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbX,
1554 void Application::setNbY()
1558 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbY,
1562 void Application::setNbZ()
1566 QInputDialog::getInteger(this, "Voxel demo-application", "Number of splits in X-direction:", myNbZ,
1570 void Application::setColorMinValue()
1574 QInputDialog::getInteger(this, "Voxel demo-application", "Minimum value for color [0 .. 15]:", myColorMinValue,
1576 if (!myVoxels.IsNull())
1577 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1580 void Application::setColorMaxValue()
1584 QInputDialog::getInteger(this, "Voxel demo-application", "Maximum value for color [0 .. 15]:", myColorMaxValue,
1586 if (!myVoxels.IsNull())
1587 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1590 void Application::setUsageOfGLlists()
1592 int res = QMessageBox::question( this, "Voxel demo-application", "Press Yes to use GL lists and No not to use them.", QMessageBox::Yes, QMessageBox::No);
1593 if (!myVoxels.IsNull())
1594 myVoxels->SetUsageOfGLlists(res == QMessageBox::Yes);
1597 void Application::setDisplayedXMin()
1599 myDisplayedXMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum X value:", myDisplayedXMin);
1600 if (!myVoxels.IsNull())
1602 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1603 myDisplayedYMin, myDisplayedYMax,
1604 myDisplayedZMin, myDisplayedZMax);
1608 void Application::setDisplayedXMax()
1610 myDisplayedXMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum X value:", myDisplayedXMax);
1611 if (!myVoxels.IsNull())
1613 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1614 myDisplayedYMin, myDisplayedYMax,
1615 myDisplayedZMin, myDisplayedZMax);
1619 void Application::setDisplayedYMin()
1621 myDisplayedYMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum Y value:", myDisplayedYMin);
1622 if (!myVoxels.IsNull())
1624 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1625 myDisplayedYMin, myDisplayedYMax,
1626 myDisplayedZMin, myDisplayedZMax);
1630 void Application::setDisplayedYMax()
1632 myDisplayedYMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum Y value:", myDisplayedYMax);
1633 if (!myVoxels.IsNull())
1635 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1636 myDisplayedYMin, myDisplayedYMax,
1637 myDisplayedZMin, myDisplayedZMax);
1641 void Application::setDisplayedZMin()
1643 myDisplayedZMin = QInputDialog::getDouble(this, "Voxel demo-application", "Minimum Z value:", myDisplayedZMin);
1644 if (!myVoxels.IsNull())
1646 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1647 myDisplayedYMin, myDisplayedYMax,
1648 myDisplayedZMin, myDisplayedZMax);
1652 void Application::setDisplayedZMax()
1654 myDisplayedZMax = QInputDialog::getDouble(this, "Voxel demo-application", "Maximum Z value:", myDisplayedZMax);
1655 if (!myVoxels.IsNull())
1657 myVoxels->SetSizeRange(myDisplayedXMin, myDisplayedXMax,
1658 myDisplayedYMin, myDisplayedYMax,
1659 myDisplayedZMin, myDisplayedZMax);
1663 void Application::setScanSide()
1666 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):",
1667 myScanSide, 1, 7, 1);
1670 void Application::setVolumicBoolValue()
1672 myVolumicBoolValue =
1673 QInputDialog::getInteger(this, "Voxel demo-application", "Volumic value on voxelization [0 .. 1]:",
1674 myVolumicBoolValue, 0, 1, 1);
1677 void Application::setVolumicColorValue()
1679 myVolumicColorValue =
1680 QInputDialog::getInteger(this, "Voxel demo-application", "Volumic value on voxelization [0 .. 15]:",
1681 myVolumicColorValue, 0, 15, 1);
1684 void Application::setQuadrangleSize()
1687 QInputDialog::getInteger(this, "Voxel demo-application", "Size of quadrangles (0% .. 100%):",
1688 myQuadrangleSize, 1, 100, 10);
1689 if (!myVoxels.IsNull())
1691 myVoxels->SetQuadrangleSize(myQuadrangleSize);
1695 void Application::setPointSize()
1698 QInputDialog::getInteger(this, "Voxel demo-application", "Size of points (1 .. 10):",
1699 myPointSize, 1, 10, 1);
1700 if (!myVoxels.IsNull())
1702 myVoxels->SetPointSize(myPointSize);
1706 void Application::display(Voxel_VoxelDisplayMode mode)
1708 if (myVoxels.IsNull() || !myViewer->getIC()->IsDisplayed(myVoxels))
1710 QMessageBox::warning( this, "Voxel demo-application", "Voxels are not displayed");
1714 myVoxels->SetDisplayMode(mode);
1717 displayColorScale();
1719 myViewer->getView()->ColorScaleErase();
1721 myViewer->getIC()->Redisplay(myVoxels, true);
1724 void Application::displayPoints()
1726 display(Voxel_VDM_POINTS);
1729 void Application::displayNearestPoints()
1731 display(Voxel_VDM_NEARESTPOINTS);
1734 void Application::displayBoxes()
1736 display(Voxel_VDM_BOXES);
1739 void Application::displayNearestBoxes()
1741 display(Voxel_VDM_NEARESTBOXES);
1744 void Application::displayColorScale()
1746 Handle(Aspect_ColorScale) color_scale = myViewer->getView()->ColorScale();
1747 if (!color_scale.IsNull())
1749 int nb_colors = 1<<4 /* 4 bits */;
1750 color_scale->SetRange(0, nb_colors - 1);
1751 color_scale->SetNumberOfIntervals(nb_colors);
1752 color_scale->SetPosition(0.01, 0.5 - 0.01);
1753 color_scale->SetSize(0.5 - 0.01, 0.5 - 0.01);
1755 myViewer->getView()->ColorScaleDisplay();
1758 void Application::displayWaves()
1760 myViewer->getIC()->EraseAll(false, false);
1765 delete myBoolVoxels;
1769 delete myColorVoxels;
1771 int nbx = 500, nby = 50, nbz = 50;
1772 double xlen = 100.0, ylen = 100.0, zlen = 20.0;
1773 double dx = xlen / (double) nbx, dy = ylen / (double) nby, dz = zlen / (double) nbz;
1774 myColorVoxels = new Voxel_ColorDS(0.0, 0.0, 0.0, xlen, ylen, zlen, nbx, nby, nbz);
1776 // Initial state - no colors
1778 for (ix = 0; ix < nbx; ix++)
1780 for (iy = 0; iy < nby; iy++)
1782 for (iz = 0; iz < nbz; iz++)
1784 myColorVoxels->Set(ix, iy, iz, 0);
1791 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
1792 myVoxels->SetUsageOfGLlists(false);
1793 myVoxels->SetBoolVoxels(myBoolVoxels);
1794 myVoxels->SetColorVoxels(myColorVoxels);
1795 if (myViewer->getIC()->IsDisplayed(myVoxels))
1796 myViewer->getIC()->Redisplay(myVoxels, false);
1798 myViewer->getIC()->Display(myVoxels, false);
1799 myViewer->getView()->FitAll();
1801 // Prepare arrays of values
1803 int i = 0, di = 5 /* nb waves */;
1804 int* zvalues = new int[nbx];
1805 unsigned char* xvalues = new unsigned char[nbx];
1806 for (ix = 0; ix < nbx; ix++, i += di)
1808 if (i > nbx || i < 0)
1813 double rad = -M_PI / 2.0 + double(i) / (double) nbx * M_PI;
1814 double c = cos(rad);
1815 xvalues[ix] = 15.0 * c;
1816 if (xvalues[ix] == 0)
1818 zvalues[ix] = (nbz - 2) * c;
1822 unsigned char value = 0;
1823 for (i = 0; i <= 100; i++)
1825 for (ix = 0; ix < nbx; ix++)
1830 for (iz = 0; iz < nbz; iz++)
1833 if (iz < zvalues[ixi])
1834 value = xvalues[ixi];
1835 for (iy = 0; iy < nby; iy++)
1837 myColorVoxels->Set(ix, iy, iz, value);
1841 myViewer->getIC()->Redisplay(myVoxels, true);
1842 qApp->processEvents();
1849 void Application::initPrs()
1851 if (myVoxels.IsNull())
1853 myVoxels = new Voxel_Prs;
1854 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
1855 myVoxels->SetColor(Quantity_NOC_WHITE);
1856 myVoxels->SetPointSize(1.0);
1857 myVoxels->SetSmoothPoints(false);
1858 myVoxels->SetQuadrangleSize(myQuadrangleSize);
1859 myVoxels->SetColorRange(myColorMinValue, myColorMaxValue);
1860 // Colors of ColorDS
1861 int nb_colors = 16 /* 4 bits */;
1862 Handle(Quantity_HArray1OfColor) colors = new Quantity_HArray1OfColor(0, nb_colors - 1);
1863 for (int icolor = 0; icolor < nb_colors; icolor++)
1865 Quantity_Color color;
1866 Aspect_ColorScale::FindColor(icolor, 0, nb_colors - 1, nb_colors, color);
1867 colors->SetValue(icolor, color);
1869 myVoxels->SetColors(colors);
1870 myViewer->setPrs(myVoxels);
1874 myViewer->getIC()->RecomputePrsOnly(myVoxels, false);
1878 void Application::box()
1880 gp_Ax2 axes(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1));
1881 TopoDS_Shape S = BRepPrimAPI_MakeBox(axes, 100, 100, 100);
1885 void Application::cylinder()
1887 TopoDS_Shape S = BRepPrimAPI_MakeCylinder(50, 100);
1891 void Application::torus()
1893 TopoDS_Shape S = BRepPrimAPI_MakeTorus(100, 20);
1897 void Application::sphere()
1899 TopoDS_Shape S = BRepPrimAPI_MakeSphere(100);
1903 void Application::load(const TopoDS_Shape& S)
1905 myViewer->getIC()->EraseAll(false, false);
1907 // Delete voxels of previous shape.
1910 delete myBoolVoxels;
1915 delete myColorVoxels;
1921 double xmin, ymin, zmin, xmax, ymax, zmax, length = 0;
1922 BRepBndLib::Add(S, box);
1923 box.Get(xmin, ymin, zmin, xmax, ymax, zmax);
1924 length = xmax - xmin > ymax - ymin ? xmax - xmin : ymax - ymin;
1925 length = length > zmax - zmin ? length : zmax - zmin;
1927 myViewer->getView()->SetSize(length);
1928 myViewer->getView()->SetZSize(length);
1931 if (myShape.IsNull())
1933 myShape = new AIS_Shape(S);
1934 myShape->SetDisplayMode(1);
1935 myShape->UnsetSelectionMode();
1940 myViewer->getIC()->RecomputePrsOnly(myShape, false);
1942 if (myViewer->getIC()->IsDisplayed(myShape))
1943 myViewer->getIC()->Redisplay(myShape, false);
1945 myViewer->getIC()->Display(myShape, false);
1946 myViewer->getView()->FitAll();
1949 void Application::displayCut()
1951 myViewer->getIC()->EraseAll(false, false);
1953 // Make a sphere with a lot of toruses,
1954 // cut the toruses from the sphere.
1955 TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(100.0);
1956 TopoDS_Shape torus1 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 80, 0, 20), gp::DZ()), 30, 10);
1957 TopoDS_Shape torus2 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 0, 80, 20), gp::DZ()), 30, 10);
1958 TopoDS_Shape torus3 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt(-80, 0, 20), gp::DZ()), 30, 10);
1959 TopoDS_Shape torus4 = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt( 0, -80, 20), gp::DZ()), 30, 10);
1961 // Compute bounding box of the shapes
1963 BRepBndLib::Add(sphere, box);
1964 BRepBndLib::Add(torus1, box);
1965 BRepBndLib::Add(torus2, box);
1966 BRepBndLib::Add(torus3, box);
1967 BRepBndLib::Add(torus4, box);
1972 delete myColorVoxels;
1977 delete myBoolVoxels;
1984 // Create a cube of voxels
1985 int nbx = 100, nby = 100, nbz = 100;
1986 double xmin, ymin, zmin, xmax, ymax, zmax;
1987 box.Get(xmin, ymin, zmin, xmax, ymax, zmax);
1988 myColorVoxels = new Voxel_ColorDS(xmin, ymin, zmin,
1989 xmax - xmin, ymax - ymin, zmax - zmin,
1991 Voxel_ColorDS vtorus(xmin, ymin, zmin,
1992 xmax - xmin, ymax - ymin, zmax - zmin,
1995 // Make a cube of voxels for the sphere.
1997 Voxel_FastConverter converter(sphere, *myColorVoxels, 0.1, nbx, nby, nbz);
1998 converter.Convert(progress);
1999 converter.FillInVolume(15);
2002 Voxel_FastConverter converter1(torus1, vtorus, 0.1, nbx, nby, nbz);
2003 converter1.Convert(progress);
2004 converter1.FillInVolume(3);
2007 Voxel_FastConverter converter2(torus2, vtorus, 0.1, nbx, nby, nbz);
2008 converter2.Convert(progress);
2009 converter2.FillInVolume(7);
2012 Voxel_FastConverter converter3(torus3, vtorus, 0.1, nbx, nby, nbz);
2013 converter3.Convert(progress);
2014 converter3.FillInVolume(10);
2017 Voxel_FastConverter converter4(torus4, vtorus, 0.1, nbx, nby, nbz);
2018 converter4.Convert(progress);
2019 converter4.FillInVolume(12);
2022 Voxel_BooleanOperation cutter;
2023 cutter.Cut(*myColorVoxels, vtorus);
2025 // Remove volumic voxels
2026 converter.FillInVolume(0);
2033 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
2034 myVoxels->SetUsageOfGLlists(true);
2035 myVoxels->SetBoolVoxels(myBoolVoxels);
2036 myVoxels->SetColorVoxels(myColorVoxels);
2037 if (myViewer->getIC()->IsDisplayed(myVoxels))
2038 myViewer->getIC()->Redisplay(myVoxels, false);
2040 myViewer->getIC()->Display(myVoxels, false);
2041 myViewer->getView()->FitAll();
2044 void Application::displayCollisions()
2046 myViewer->getIC()->EraseAll(false, false);
2048 // Make a big box with a lot of small spheres inside.
2049 double x = 0.0, y = 0.0, z = 0.0, xlen = 100.0, ylen = 100.0, zlen = 100.0, r = 10.0;
2050 gp_Pnt P1(x, y, z); // center point of moving sphere (S1).
2051 TopoDS_Shape B = BRepPrimAPI_MakeBox(gp_Pnt(x-r, y-r, z-r), gp_Pnt(xlen+r, ylen+r, zlen+r));
2052 TopoDS_Shape S1 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, z), gp::DZ()), r / 2.0);
2053 TopoDS_Shape S2 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, z), gp::DZ()), r);
2054 TopoDS_Shape S3 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, z), gp::DZ()), r);
2055 TopoDS_Shape S4 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., z), gp::DZ()), r);
2056 TopoDS_Shape S5 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., z), gp::DZ()), r);
2057 TopoDS_Shape S6 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., z), gp::DZ()), r);
2058 TopoDS_Shape S7 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, z), gp::DZ()), r);
2059 TopoDS_Shape S8 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, z), gp::DZ()), r);
2060 TopoDS_Shape S9 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, z), gp::DZ()), r);
2061 TopoDS_Shape S10 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, zlen/2.), gp::DZ()), r);
2062 TopoDS_Shape S11 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, zlen/2.), gp::DZ()), r);
2063 TopoDS_Shape S12 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, zlen/2.), gp::DZ()), r);
2064 TopoDS_Shape S13 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., zlen/2.), gp::DZ()), r);
2065 TopoDS_Shape S14 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., zlen/2.), gp::DZ()), r);
2066 TopoDS_Shape S15 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., zlen/2.), gp::DZ()), r);
2067 TopoDS_Shape S16 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, zlen/2.), gp::DZ()), r);
2068 TopoDS_Shape S17 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, zlen/2.), gp::DZ()), r);
2069 TopoDS_Shape S18 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, zlen/2.), gp::DZ()), r);
2070 TopoDS_Shape S19 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, y, zlen), gp::DZ()), r);
2071 TopoDS_Shape S20 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., y, zlen), gp::DZ()), r);
2072 TopoDS_Shape S21 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, y, zlen), gp::DZ()), r);
2073 TopoDS_Shape S22 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen/2., zlen), gp::DZ()), r);
2074 TopoDS_Shape S23 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen/2., zlen), gp::DZ()), r);
2075 TopoDS_Shape S24 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen/2., zlen), gp::DZ()), r);
2076 TopoDS_Shape S25 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(x, ylen, zlen), gp::DZ()), r);
2077 TopoDS_Shape S26 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen/2., ylen, zlen), gp::DZ()), r);
2078 TopoDS_Shape S27 = BRepPrimAPI_MakeSphere(gp_Ax2(gp_Pnt(xlen, ylen, zlen), gp::DZ()), r);
2080 // Planes of the big box
2081 gp_Ax2 xminusPlane(gp_Pnt(x, y, z), gp::DX());
2082 gp_Ax2 xplusPlane (gp_Pnt(xlen, y, z), gp::DX());
2083 gp_Ax2 yminusPlane(gp_Pnt(x, y, z), gp::DY());
2084 gp_Ax2 yplusPlane (gp_Pnt(x, ylen, z), gp::DY());
2085 gp_Ax2 zminusPlane(gp_Pnt(x, y, z), gp::DZ());
2086 gp_Ax2 zplusPlane (gp_Pnt(x, y, zlen), gp::DZ());
2091 delete myColorVoxels;
2096 delete myBoolVoxels;
2100 // Prepare visualization
2102 myVoxels->SetDisplayMode(Voxel_VDM_POINTS);
2103 myVoxels->SetColor(Quantity_NOC_RED);
2104 myVoxels->SetPointSize(4);
2105 myVoxels->SetSmoothPoints(false);
2106 myVoxels->SetUsageOfGLlists(false);
2107 myVoxels->SetColorVoxels(myColorVoxels);
2109 // Display all shapes
2110 double transparency = 0.9;
2111 Handle(AIS_Shape) aisB = new AIS_Shape(B);
2112 Handle(AIS_Shape) aisS1 = new AIS_Shape(S1);
2113 Handle(AIS_Shape) aisS2 = new AIS_Shape(S2);
2114 Handle(AIS_Shape) aisS3 = new AIS_Shape(S3);
2115 Handle(AIS_Shape) aisS4 = new AIS_Shape(S4);
2116 Handle(AIS_Shape) aisS5 = new AIS_Shape(S5);
2117 Handle(AIS_Shape) aisS6 = new AIS_Shape(S6);
2118 Handle(AIS_Shape) aisS7 = new AIS_Shape(S7);
2119 Handle(AIS_Shape) aisS8 = new AIS_Shape(S8);
2120 Handle(AIS_Shape) aisS9 = new AIS_Shape(S9);
2121 Handle(AIS_Shape) aisS10 = new AIS_Shape(S10);
2122 Handle(AIS_Shape) aisS11 = new AIS_Shape(S11);
2123 Handle(AIS_Shape) aisS12 = new AIS_Shape(S12);
2124 Handle(AIS_Shape) aisS13 = new AIS_Shape(S13);
2125 Handle(AIS_Shape) aisS14 = new AIS_Shape(S14);
2126 Handle(AIS_Shape) aisS15 = new AIS_Shape(S15);
2127 Handle(AIS_Shape) aisS16 = new AIS_Shape(S16);
2128 Handle(AIS_Shape) aisS17 = new AIS_Shape(S17);
2129 Handle(AIS_Shape) aisS18 = new AIS_Shape(S18);
2130 Handle(AIS_Shape) aisS19 = new AIS_Shape(S19);
2131 Handle(AIS_Shape) aisS20 = new AIS_Shape(S20);
2132 Handle(AIS_Shape) aisS21 = new AIS_Shape(S21);
2133 Handle(AIS_Shape) aisS22 = new AIS_Shape(S22);
2134 Handle(AIS_Shape) aisS23 = new AIS_Shape(S23);
2135 Handle(AIS_Shape) aisS24 = new AIS_Shape(S24);
2136 Handle(AIS_Shape) aisS25 = new AIS_Shape(S25);
2137 Handle(AIS_Shape) aisS26 = new AIS_Shape(S26);
2138 Handle(AIS_Shape) aisS27 = new AIS_Shape(S27);
2139 aisS1-> SetDisplayMode(1);
2140 aisS2-> SetDisplayMode(1);
2141 aisS3-> SetDisplayMode(1);
2142 aisS4-> SetDisplayMode(1);
2143 aisS5-> SetDisplayMode(1);
2144 aisS6-> SetDisplayMode(1);
2145 aisS7-> SetDisplayMode(1);
2146 aisS8-> SetDisplayMode(1);
2147 aisS9-> SetDisplayMode(1);
2148 aisS10->SetDisplayMode(1);
2149 aisS11->SetDisplayMode(1);
2150 aisS12->SetDisplayMode(1);
2151 aisS13->SetDisplayMode(1);
2152 aisS14->SetDisplayMode(1);
2153 aisS15->SetDisplayMode(1);
2154 aisS16->SetDisplayMode(1);
2155 aisS17->SetDisplayMode(1);
2156 aisS18->SetDisplayMode(1);
2157 aisS19->SetDisplayMode(1);
2158 aisS20->SetDisplayMode(1);
2159 aisS21->SetDisplayMode(1);
2160 aisS22->SetDisplayMode(1);
2161 aisS23->SetDisplayMode(1);
2162 aisS24->SetDisplayMode(1);
2163 aisS25->SetDisplayMode(1);
2164 aisS26->SetDisplayMode(1);
2165 aisS27->SetDisplayMode(1);
2166 aisB-> UnsetSelectionMode();
2167 aisS1-> UnsetSelectionMode();
2168 aisS2-> UnsetSelectionMode();
2169 aisS3-> UnsetSelectionMode();
2170 aisS4-> UnsetSelectionMode();
2171 aisS5-> UnsetSelectionMode();
2172 aisS6-> UnsetSelectionMode();
2173 aisS7-> UnsetSelectionMode();
2174 aisS8-> UnsetSelectionMode();
2175 aisS9-> UnsetSelectionMode();
2176 aisS10->UnsetSelectionMode();
2177 aisS11->UnsetSelectionMode();
2178 aisS12->UnsetSelectionMode();
2179 aisS13->UnsetSelectionMode();
2180 aisS14->UnsetSelectionMode();
2181 aisS15->UnsetSelectionMode();
2182 aisS16->UnsetSelectionMode();
2183 aisS17->UnsetSelectionMode();
2184 aisS18->UnsetSelectionMode();
2185 aisS19->UnsetSelectionMode();
2186 aisS20->UnsetSelectionMode();
2187 aisS21->UnsetSelectionMode();
2188 aisS22->UnsetSelectionMode();
2189 aisS23->UnsetSelectionMode();
2190 aisS24->UnsetSelectionMode();
2191 aisS25->UnsetSelectionMode();
2192 aisS26->UnsetSelectionMode();
2193 aisS27->UnsetSelectionMode();
2194 aisS1-> SetTransparency(2.0 * transparency / 3.0);
2195 aisS2-> SetTransparency(transparency);
2196 aisS3-> SetTransparency(transparency);
2197 aisS4-> SetTransparency(transparency);
2198 aisS5-> SetTransparency(transparency);
2199 aisS6-> SetTransparency(transparency);
2200 aisS7-> SetTransparency(transparency);
2201 aisS8-> SetTransparency(transparency);
2202 aisS9-> SetTransparency(transparency);
2203 aisS10->SetTransparency(transparency);
2204 aisS11->SetTransparency(transparency);
2205 aisS12->SetTransparency(transparency);
2206 aisS13->SetTransparency(transparency);
2207 aisS14->SetTransparency(transparency);
2208 aisS15->SetTransparency(transparency);
2209 aisS16->SetTransparency(transparency);
2210 aisS17->SetTransparency(transparency);
2211 aisS18->SetTransparency(transparency);
2212 aisS19->SetTransparency(transparency);
2213 aisS20->SetTransparency(transparency);
2214 aisS21->SetTransparency(transparency);
2215 aisS22->SetTransparency(transparency);
2216 aisS23->SetTransparency(transparency);
2217 aisS24->SetTransparency(transparency);
2218 aisS25->SetTransparency(transparency);
2219 aisS26->SetTransparency(transparency);
2220 aisS27->SetTransparency(transparency);
2221 myViewer->getIC()->Display(aisB, false);
2222 myViewer->getIC()->Display(aisS1, false);
2223 myViewer->getIC()->Display(aisS2, false);
2224 myViewer->getIC()->Display(aisS3, false);
2225 myViewer->getIC()->Display(aisS4, false);
2226 myViewer->getIC()->Display(aisS5, false);
2227 myViewer->getIC()->Display(aisS6, false);
2228 myViewer->getIC()->Display(aisS7, false);
2229 myViewer->getIC()->Display(aisS8, false);
2230 myViewer->getIC()->Display(aisS9, false);
2231 myViewer->getIC()->Display(aisS10, false);
2232 myViewer->getIC()->Display(aisS11, false);
2233 myViewer->getIC()->Display(aisS12, false);
2234 myViewer->getIC()->Display(aisS13, false);
2235 myViewer->getIC()->Display(aisS14, false);
2236 myViewer->getIC()->Display(aisS15, false);
2237 myViewer->getIC()->Display(aisS16, false);
2238 myViewer->getIC()->Display(aisS17, false);
2239 myViewer->getIC()->Display(aisS18, false);
2240 myViewer->getIC()->Display(aisS19, false);
2241 myViewer->getIC()->Display(aisS20, false);
2242 myViewer->getIC()->Display(aisS21, false);
2243 myViewer->getIC()->Display(aisS22, false);
2244 myViewer->getIC()->Display(aisS23, false);
2245 myViewer->getIC()->Display(aisS24, false);
2246 myViewer->getIC()->Display(aisS25, false);
2247 myViewer->getIC()->Display(aisS26, false);
2248 myViewer->getIC()->Display(aisS27, false);
2250 // Prepare computer of collisions
2251 double deflection = 0.1;
2252 int nbx = 100, nby = 100, nbz = 100;
2254 Voxel_CollisionDetection coldet(deflection, nbx, nby, nbz);
2255 coldet.SetUsageOfVolume(false);
2256 coldet.KeepCollisions(false);
2257 coldet.AddShape(S1);
2258 coldet.AddShape(S2);
2259 coldet.AddShape(S3);
2260 coldet.AddShape(S4);
2261 coldet.AddShape(S5);
2262 coldet.AddShape(S6);
2263 coldet.AddShape(S7);
2264 coldet.AddShape(S8);
2265 coldet.AddShape(S9);
2266 coldet.AddShape(S10);
2267 coldet.AddShape(S11);
2268 coldet.AddShape(S12);
2269 coldet.AddShape(S13);
2270 coldet.AddShape(S14);
2271 coldet.AddShape(S15);
2272 coldet.AddShape(S16);
2273 coldet.AddShape(S17);
2274 coldet.AddShape(S18);
2275 coldet.AddShape(S19);
2276 coldet.AddShape(S20);
2277 coldet.AddShape(S21);
2278 coldet.AddShape(S22);
2279 coldet.AddShape(S23);
2280 coldet.AddShape(S24);
2281 coldet.AddShape(S25);
2282 coldet.AddShape(S26);
2283 coldet.AddShape(S27);
2284 //coldet.AddShape(BRepPrimAPI_MakeBox(gp_Pnt(x, y, z), gp_Pnt(xlen, ylen, zlen)));
2287 BRepBndLib::Add(B, box);
2288 coldet.SetBoundaryBox(box);
2292 // Move one of the spheres inside the box
2293 // and compute collisions
2295 gp_Vec vmove(1, 0.5, 0.25);
2297 int imove = 0, nb_moves = 900;
2298 while (imove < nb_moves)
2301 trsf.SetTranslation(vmove);
2302 TopLoc_Location loc(trsf);
2304 P1.Translate(vmove);
2306 // Check whether S1 is inside the big box
2307 // Detect the plane S1 touches to.
2309 vmove.Mirror(xminusPlane);
2310 else if (P1.X() > xlen)
2311 vmove.Mirror(xplusPlane);
2312 else if (P1.Y() < y)
2313 vmove.Mirror(yminusPlane);
2314 else if (P1.Y() > ylen)
2315 vmove.Mirror(yplusPlane);
2316 else if (P1.Z() < z)
2317 vmove.Mirror(zminusPlane);
2318 else if (P1.Z() > zlen)
2319 vmove.Mirror(zplusPlane);
2321 // Compute collisions
2322 coldet.ReplaceShape(1, S1);
2323 coldet.Voxelize(1); // only the first sphere (S1)
2325 myBoolVoxels = &((Voxel_BoolDS&) coldet.GetCollisions());
2329 myViewer->getIC()->Redisplay(aisS1, false);
2331 // Display the collisions
2332 myVoxels->SetBoolVoxels(myBoolVoxels);
2333 if (myViewer->getIC()->IsDisplayed(myVoxels))
2334 myViewer->getIC()->Redisplay(myVoxels, true);
2337 myViewer->getIC()->Display(myVoxels, false);
2338 myViewer->getView()->FitAll();
2342 qApp->processEvents();
2345 // Copy the result of collision detection
2347 myBoolVoxels = new Voxel_BoolDS(coldet.GetCollisions().GetX(),
2348 coldet.GetCollisions().GetY(),
2349 coldet.GetCollisions().GetZ(),
2350 coldet.GetCollisions().GetXLen(),
2351 coldet.GetCollisions().GetYLen(),
2352 coldet.GetCollisions().GetZLen(),
2354 for (ix = 0; ix < nbx; ix++)
2356 for (iy = 0; iy < nby; iy++)
2358 for (iz = 0; iz < nbz; iz++)
2360 if (coldet.GetCollisions().Get(ix, iy, iz))
2361 myBoolVoxels->Set(ix, iy, iz, Standard_True);
2365 myVoxels->SetBoolVoxels(myBoolVoxels);