Salome HOME
Copyright update 2022
[modules/gui.git] / src / OCCViewer / OCCViewer_Utilities.cxx
1 // Copyright (C) 2014-2022  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 #include <Basics_OCCTVersion.hxx>
31
32 // OCC includes
33 #include <V3d_View.hxx>
34 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
35
36 // QT includes
37 #include <QImage>
38 #include <QAction>
39 #include <QDialog>
40
41 Handle(Image_PixMap) OCCViewer_Utilities::imageToPixmap( const QImage& anImage )
42 {
43   Handle(Image_PixMap) aPixmap = new Image_PixMap();
44   if ( !anImage.isNull() ) {
45     aPixmap->InitTrash( Image_PixMap::ImgBGRA, anImage.width(), anImage.height() );
46 #if OCC_VERSION_LARGE < 0x07050000
47     aPixmap->SetTopDown( Standard_True );
48 #endif
49
50     const uchar* aImageBytes = anImage.bits();
51
52 #if OCC_VERSION_LARGE < 0x07050000
53     for ( int aLine = anImage.height() - 1; aLine >= 0; --aLine ) {
54 #else
55     for ( int aLine = 0; aLine < anImage.height(); ++aLine ) {
56 #endif
57       // convert pixels from ARGB to renderer-compatible RGBA
58       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
59             Image_ColorBGRA& aPixmapBytes = aPixmap->ChangeValue<Image_ColorBGRA>(aLine, aByte);
60
61             aPixmapBytes.b() = (Standard_Byte) *aImageBytes++;
62             aPixmapBytes.g() = (Standard_Byte) *aImageBytes++;
63             aPixmapBytes.r() = (Standard_Byte) *aImageBytes++;
64             aPixmapBytes.a() = (Standard_Byte) *aImageBytes++;
65           }
66     }
67   }
68   return aPixmap;
69 }
70
71 OCCViewer_ViewWindow::Mode2dType OCCViewer_Utilities::setViewer2DMode
72                                          ( OCCViewer_Viewer* theViewer,
73                                            const OCCViewer_ViewWindow::Mode2dType& theMode )
74 {
75   OCCViewer_ViewWindow::Mode2dType anOldMode = OCCViewer_ViewWindow::No2dMode;
76   OCCViewer_ViewFrame* aFrame = dynamic_cast<OCCViewer_ViewFrame*>
77                                      ( theViewer->getViewManager()->getActiveView() );
78   OCCViewer_ViewWindow* aView = aFrame ? aFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW ) : 0;
79   if ( !aView )
80     return anOldMode;
81
82   // set a view mode
83   anOldMode = aView->get2dMode();
84   aView->set2dMode( theMode );
85   bool is2dMode = theMode != OCCViewer_ViewWindow::No2dMode;
86
87   // enable/disable view actions
88   QList<int> aNo2dActions;
89   aNo2dActions << OCCViewer_ViewWindow::ChangeRotationPointId
90                << OCCViewer_ViewWindow::RotationId
91                << OCCViewer_ViewWindow::FrontId
92                << OCCViewer_ViewWindow::BackId
93                //<< OCCViewer_ViewWindow::TopId
94                << OCCViewer_ViewWindow::BottomId
95                << OCCViewer_ViewWindow::LeftId
96                << OCCViewer_ViewWindow::RightId
97                << OCCViewer_ViewWindow::AntiClockWiseId
98                << OCCViewer_ViewWindow::ClockWiseId
99                << OCCViewer_ViewWindow::OrthographicId
100                << OCCViewer_ViewWindow::PerspectiveId
101                << OCCViewer_ViewWindow::ResetId;
102
103   QtxActionToolMgr* aToolMgr = aView->toolMgr();
104   QAction* anAction;
105   for ( int i = 0, aNb = aNo2dActions.size(); i < aNb; i++ ) {
106     anAction = aToolMgr->action( aNo2dActions[i] );
107     if ( anAction )
108     {
109       anAction->setEnabled( !is2dMode );
110       anAction->setVisible( !is2dMode );
111     }
112   }
113   QAction* aTop = aToolMgr->action( OCCViewer_ViewWindow::TopId );
114   QtxMultiAction* aMulti = dynamic_cast<QtxMultiAction*>( aTop->parent() );
115   aMulti->setActiveAction( aTop );
116
117   // change view position
118   Handle(V3d_View) aView3d = aView->getViewPort()->getView();
119   if ( !aView3d.IsNull() ) {
120     switch ( theMode ) {
121       case OCCViewer_ViewWindow::XYPlane:
122         aView3d->SetProj (V3d_Zpos);
123         break;
124       case OCCViewer_ViewWindow::XZPlane:
125         aView3d->SetProj (V3d_Yneg);
126         break;
127       case OCCViewer_ViewWindow::YZPlane:
128         aView3d->SetProj (V3d_Xpos);
129         break;
130       default:
131         break;
132     }
133   }
134
135   return anOldMode;
136 }
137
138 bool OCCViewer_Utilities::isDialogOpened( OCCViewer_ViewWindow* theView, const QString& theName )
139 {
140   bool isFound = false;
141   OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theView->parent()->parent() );
142   QList<QDialog*> allDialogs = aViewFrame->findChildren<QDialog*>();
143   foreach ( QDialog* d, allDialogs )
144     if ( d->objectName() == theName )
145       isFound = true;
146   return isFound;
147 }
148
149 bool OCCViewer_Utilities::computeVisibleBounds( const Handle(V3d_View) theView,
150                                                 double theBounds[6] )
151 {
152   bool isAny = false;
153
154   theBounds[0] = theBounds[2] = theBounds[4] = DBL_MAX;
155   theBounds[1] = theBounds[3] = theBounds[5] = -DBL_MAX;
156
157   Graphic3d_MapOfStructure aSetOfStructures;
158   theView->View()->DisplayedStructures( aSetOfStructures );
159   Graphic3d_MapIteratorOfMapOfStructure aStructureIt( aSetOfStructures );
160
161   for( ; aStructureIt.More(); aStructureIt.Next() ) {
162     const Handle(Graphic3d_Structure)& aStructure = aStructureIt.Key();
163     if ( aStructure->IsEmpty() || !aStructure->IsVisible() ||
164          aStructure->IsInfinite() || aStructure->CStructure()->IsForHighlight )
165       continue;
166     double aBounds[6];
167     Bnd_Box aBox = aStructure->MinMaxValues();
168     aBounds[0] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().X();
169     aBounds[2] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Y();
170     aBounds[4] = aBox.IsVoid() ? RealFirst() : aBox.CornerMin().Z();
171     aBounds[1] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().X();
172     aBounds[3] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().Y();
173     aBounds[5] = aBox.IsVoid() ? RealLast()  : aBox.CornerMax().Z();
174
175     if ( aBounds[0] > -DBL_MAX && aBounds[1] < DBL_MAX &&
176          aBounds[2] > -DBL_MAX && aBounds[3] < DBL_MAX &&
177          aBounds[4] > -DBL_MAX && aBounds[5] < DBL_MAX )
178     {
179       isAny = true;
180       for ( int i = 0; i < 5; i = i + 2 ) {
181         theBounds[i] = std::min( theBounds[i], aBounds[i] );
182         theBounds[i+1] = std::max( theBounds[i+1], aBounds[i+1] );
183       }
184     }
185   }
186   return isAny;
187 }
188
189 bool OCCViewer_Utilities::computeVisibleBBCenter( const Handle(V3d_View) theView,
190                                                   double& theX, double& theY, double& theZ )
191 {
192   double aBounds[6];
193   if ( !computeVisibleBounds( theView, aBounds ) )
194   {
195     // null bounding box => the center is (0,0,0)
196     theX = 0.0;
197     theY = 0.0;
198     theZ = 0.0;
199     return true;
200   }
201
202   static double aMinDistance = 1.0 / DBL_MAX;
203
204   double aLength = aBounds[1]-aBounds[0];
205   aLength = std::max( ( aBounds[3]-aBounds[2]), aLength );
206   aLength = std::max( ( aBounds[5]-aBounds[4]), aLength );
207
208   if ( aLength < aMinDistance )
209     return false;
210
211   double aWidth = sqrt( ( aBounds[1] - aBounds[0] ) * ( aBounds[1] - aBounds[0] ) +
212                         ( aBounds[3] - aBounds[2] ) * ( aBounds[3] - aBounds[2] ) +
213                         ( aBounds[5] - aBounds[4] ) * ( aBounds[5] - aBounds[4] ) );
214
215   if(aWidth < aMinDistance)
216     return false;
217
218   theX = (aBounds[0] + aBounds[1])/2.0;
219   theY = (aBounds[2] + aBounds[3])/2.0;
220   theZ = (aBounds[4] + aBounds[5])/2.0;
221
222   return true;
223 }
224
225 bool OCCViewer_Utilities::computeSceneBBCenter( const Handle(V3d_View) theView,
226                                                 double& theX, double& theY, double& theZ )
227 {
228   theX = 0, theY = 0, theZ = 0;
229   Bnd_Box aBox = theView->View()->MinMaxValues();
230   if (!aBox.IsVoid())
231   {
232     double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
233     aBox.Get (Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
234     gp_Pnt aPnts[8] =
235     {
236       gp_Pnt (Xmin, Ymin, Zmin), gp_Pnt (Xmin, Ymin, Zmax),
237       gp_Pnt (Xmin, Ymax, Zmin), gp_Pnt (Xmin, Ymax, Zmax),
238       gp_Pnt (Xmax, Ymin, Zmin), gp_Pnt (Xmax, Ymin, Zmax),
239       gp_Pnt (Xmax, Ymax, Zmin), gp_Pnt (Xmax, Ymax, Zmax)
240     };
241
242     for (Standard_Integer i = 0; i < 8; i++)
243     {
244       const gp_Pnt& aCornPnt = aPnts[i];
245       theX += aCornPnt.X();
246       theY += aCornPnt.Y();
247       theZ += aCornPnt.Z();
248     }
249
250     theX /= 8;
251     theY /= 8;
252     theZ /= 8;
253     return true;
254   }
255   return false;
256 }