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