Salome HOME
#18963 Minimize compiler warnings
[modules/gui.git] / src / OCCViewer / OCCViewer_Utilities.cxx
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "OCCViewer_Utilities.h"
22 #include "OCCViewer_ViewFrame.h"
23 #include "OCCViewer_ViewModel.h"
24 #include "OCCViewer_ViewPort3d.h"
25
26 #include "SUIT_ViewManager.h"
27 #include "QtxActionToolMgr.h"
28 #include "QtxMultiAction.h"
29
30 // OCC includes
31 #include <V3d_View.hxx>
32 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
33
34 // QT includes
35 #include <QImage>
36 #include <QAction>
37 #include <QDialog>
38
39 Handle(Image_PixMap) OCCViewer_Utilities::imageToPixmap( const QImage& anImage )
40 {
41   Handle(Image_PixMap) aPixmap = new Image_PixMap();
42   if ( !anImage.isNull() ) {
43     aPixmap->InitTrash( Image_PixMap::ImgBGRA, anImage.width(), anImage.height() );
44     aPixmap->SetTopDown( Standard_True );
45
46     const uchar* aImageBytes = anImage.bits();
47       
48     for ( int aLine = anImage.height() - 1; aLine >= 0; --aLine ) {
49       // convert pixels from ARGB to renderer-compatible RGBA
50       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
51             Image_ColorBGRA& aPixmapBytes = aPixmap->ChangeValue<Image_ColorBGRA>(aLine, aByte);
52
53             aPixmapBytes.b() = (Standard_Byte) *aImageBytes++;
54             aPixmapBytes.g() = (Standard_Byte) *aImageBytes++;
55             aPixmapBytes.r() = (Standard_Byte) *aImageBytes++;
56             aPixmapBytes.a() = (Standard_Byte) *aImageBytes++;
57           }
58     }
59   }
60   return aPixmap;
61 }
62
63 OCCViewer_ViewWindow::Mode2dType OCCViewer_Utilities::setViewer2DMode
64                                          ( OCCViewer_Viewer* theViewer,
65                                            const OCCViewer_ViewWindow::Mode2dType& theMode )
66 {
67   OCCViewer_ViewWindow::Mode2dType anOldMode = OCCViewer_ViewWindow::No2dMode;
68   OCCViewer_ViewFrame* aFrame = dynamic_cast<OCCViewer_ViewFrame*>
69                                      ( theViewer->getViewManager()->getActiveView() );
70   OCCViewer_ViewWindow* aView = aFrame ? aFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW ) : 0;
71   if ( !aView )
72     return anOldMode;
73
74   // set a view mode
75   anOldMode = aView->get2dMode();
76   aView->set2dMode( theMode );
77   bool is2dMode = theMode != OCCViewer_ViewWindow::No2dMode;
78
79   // enable/disable view actions
80   QList<int> aNo2dActions;
81   aNo2dActions << OCCViewer_ViewWindow::ChangeRotationPointId
82                << OCCViewer_ViewWindow::RotationId
83                << OCCViewer_ViewWindow::FrontId
84                << OCCViewer_ViewWindow::BackId
85                //<< OCCViewer_ViewWindow::TopId
86                << OCCViewer_ViewWindow::BottomId
87                << OCCViewer_ViewWindow::LeftId
88                << OCCViewer_ViewWindow::RightId
89                << OCCViewer_ViewWindow::AntiClockWiseId
90                << OCCViewer_ViewWindow::ClockWiseId
91                << OCCViewer_ViewWindow::OrthographicId
92                << OCCViewer_ViewWindow::PerspectiveId
93                << OCCViewer_ViewWindow::ResetId;
94
95   QtxActionToolMgr* aToolMgr = aView->toolMgr();
96   QAction* anAction;
97   for ( int i = 0, aNb = aNo2dActions.size(); i < aNb; i++ ) {
98     anAction = aToolMgr->action( aNo2dActions[i] );
99     if ( anAction )
100     {
101       anAction->setEnabled( !is2dMode );
102       anAction->setVisible( !is2dMode );
103     }
104   }
105   QAction* aTop = aToolMgr->action( OCCViewer_ViewWindow::TopId );
106   QtxMultiAction* aMulti = dynamic_cast<QtxMultiAction*>( aTop->parent() );
107   aMulti->setActiveAction( aTop );
108
109   // change view position
110   Handle(V3d_View) aView3d = aView->getViewPort()->getView();
111   if ( !aView3d.IsNull() ) {
112     switch ( theMode ) {
113       case OCCViewer_ViewWindow::XYPlane:
114         aView3d->SetProj (V3d_Zpos);
115         break;
116       case OCCViewer_ViewWindow::XZPlane:
117         aView3d->SetProj (V3d_Yneg);
118         break;
119       case OCCViewer_ViewWindow::YZPlane:
120         aView3d->SetProj (V3d_Xpos);
121         break;
122       default:
123         break;
124     }
125   }
126
127   return anOldMode;
128 }
129
130 bool OCCViewer_Utilities::isDialogOpened( OCCViewer_ViewWindow* theView, const QString& theName )
131 {
132   bool isFound = false;
133   OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theView->parent()->parent() );
134   QList<QDialog*> allDialogs = aViewFrame->findChildren<QDialog*>();
135   foreach ( QDialog* d, allDialogs )
136     if ( d->objectName() == theName )
137       isFound = true;
138   return isFound;
139 }
140
141 bool OCCViewer_Utilities::computeVisibleBounds( const Handle(V3d_View) theView,
142                                                 double theBounds[6] )
143 {
144   bool isAny = false;
145
146   theBounds[0] = theBounds[2] = theBounds[4] = DBL_MAX;
147   theBounds[1] = theBounds[3] = theBounds[5] = -DBL_MAX;
148
149   Graphic3d_MapOfStructure aSetOfStructures;
150   theView->View()->DisplayedStructures( aSetOfStructures );
151   Graphic3d_MapIteratorOfMapOfStructure aStructureIt( aSetOfStructures );
152
153   for( ; aStructureIt.More(); aStructureIt.Next() ) {
154     const Handle(Graphic3d_Structure)& aStructure = aStructureIt.Key();
155     if ( aStructure->IsEmpty() || !aStructure->IsVisible() ||
156          aStructure->IsInfinite() || aStructure->CStructure()->IsForHighlight )
157       continue;
158     double aBounds[6];
159     Bnd_Box aBox = aStructure->MinMaxValues();
160     aBounds[0] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
161     aBounds[2] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
162     aBounds[4] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
163     aBounds[1] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().X();
164     aBounds[3] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().Y();
165     aBounds[5] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().Z();
166
167     if ( aBounds[0] > -DBL_MAX && aBounds[1] < DBL_MAX &&
168          aBounds[2] > -DBL_MAX && aBounds[3] < DBL_MAX &&
169          aBounds[4] > -DBL_MAX && aBounds[5] < DBL_MAX )
170     {
171       isAny = true;
172       for ( int i = 0; i < 5; i = i + 2 ) {
173         theBounds[i] = std::min( theBounds[i], aBounds[i] );
174         theBounds[i+1] = std::max( theBounds[i+1], aBounds[i+1] );
175       }
176     }
177   }
178   return isAny;
179 }
180
181 bool OCCViewer_Utilities::computeVisibleBBCenter( const Handle(V3d_View) theView,
182                                                   double& theX, double& theY, double& theZ )
183 {
184   double aBounds[6];
185   if ( !computeVisibleBounds( theView, aBounds ) )
186   {
187     // null bounding box => the center is (0,0,0)
188     theX = 0.0;
189     theY = 0.0;
190     theZ = 0.0;
191     return true;
192   }
193
194   static double aMinDistance = 1.0 / DBL_MAX;
195
196   double aLength = aBounds[1]-aBounds[0];
197   aLength = std::max( ( aBounds[3]-aBounds[2]), aLength );
198   aLength = std::max( ( aBounds[5]-aBounds[4]), aLength );
199
200   if ( aLength < aMinDistance )
201     return false;
202
203   double aWidth = sqrt( ( aBounds[1] - aBounds[0] ) * ( aBounds[1] - aBounds[0] ) +
204                         ( aBounds[3] - aBounds[2] ) * ( aBounds[3] - aBounds[2] ) +
205                         ( aBounds[5] - aBounds[4] ) * ( aBounds[5] - aBounds[4] ) );
206
207   if(aWidth < aMinDistance)
208     return false;
209
210   theX = (aBounds[0] + aBounds[1])/2.0;
211   theY = (aBounds[2] + aBounds[3])/2.0;
212   theZ = (aBounds[4] + aBounds[5])/2.0;
213
214   return true;
215 }
216
217 bool OCCViewer_Utilities::computeSceneBBCenter( const Handle(V3d_View) theView,
218                                                 double& theX, double& theY, double& theZ )
219 {
220   theX = 0, theY = 0, theZ = 0;
221   Bnd_Box aBox = theView->View()->MinMaxValues();
222   if (!aBox.IsVoid())
223   {
224     double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
225     aBox.Get (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
226     gp_Pnt aPnts[8] =
227     {
228       gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
229       gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
230       gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
231       gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
232     };
233
234     for (Standard_Integer i = 0; i < 8; i++)
235     {
236       const gp_Pnt& aCornPnt = aPnts[i];
237       theX += aCornPnt.X();
238       theY += aCornPnt.Y();
239       theZ += aCornPnt.Z();
240     }
241
242     theX /= 8;
243     theY /= 8;
244     theZ /= 8;
245     return true;
246   }
247   return false;
248 }