1 // Created on: 1995-03-15
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <SelectMgr_ViewerSelector3d.hxx>
19 #include <Graphic3d_SequenceOfHClipPlane.hxx>
20 #include <Graphic3d_Structure.hxx>
21 #include <math_BullardGenerator.hxx>
22 #include <Quantity_ColorHasher.hxx>
23 #include <TColgp_Array1OfPnt2d.hxx>
24 #include <SelectMgr.hxx>
25 #include <SelectMgr_EntityOwner.hxx>
26 #include <SelectMgr_SelectableObject.hxx>
27 #include <SelectMgr_Selection.hxx>
28 #include <SelectMgr_SelectionImageFiller.hxx>
29 #include <V3d_View.hxx>
30 #include <V3d_Viewer.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_ViewerSelector3d, SelectMgr_ViewerSelector)
34 //=======================================================================
35 // Function : Constructor
37 //=======================================================================
38 SelectMgr_ViewerSelector3d::SelectMgr_ViewerSelector3d()
43 //=======================================================================
46 //=======================================================================
47 void SelectMgr_ViewerSelector3d::Pick (const Standard_Integer theXPix,
48 const Standard_Integer theYPix,
49 const Handle(V3d_View)& theView)
51 updateZLayers (theView);
52 if(myToUpdateTolerance)
54 mySelectingVolumeMgr.SetPixelTolerance (myTolerances.Tolerance());
55 myToUpdateTolerance = Standard_False;
58 mySelectingVolumeMgr.SetCamera (theView->Camera());
59 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Point);
60 Standard_Integer aWidth = 0, aHeight = 0;
61 theView->Window()->Size (aWidth, aHeight);
62 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
63 gp_Pnt2d aMousePos (static_cast<Standard_Real> (theXPix),
64 static_cast<Standard_Real> (theYPix));
65 mySelectingVolumeMgr.BuildSelectingVolume (aMousePos);
66 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
71 //=======================================================================
74 //=======================================================================
75 void SelectMgr_ViewerSelector3d::Pick (const Standard_Integer theXPMin,
76 const Standard_Integer theYPMin,
77 const Standard_Integer theXPMax,
78 const Standard_Integer theYPMax,
79 const Handle(V3d_View)& theView)
81 updateZLayers (theView);
82 mySelectingVolumeMgr.SetCamera (theView->Camera());
83 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Box);
84 Standard_Integer aWidth = 0, aHeight = 0;
85 theView->Window()->Size (aWidth, aHeight);
86 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
87 gp_Pnt2d aMinMousePos (static_cast<Standard_Real> (theXPMin),
88 static_cast<Standard_Real> (theYPMin));
89 gp_Pnt2d aMaxMousePos (static_cast<Standard_Real> (theXPMax),
90 static_cast<Standard_Real> (theYPMax));
91 mySelectingVolumeMgr.BuildSelectingVolume (aMinMousePos,
94 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
99 //=======================================================================
101 // Purpose : Selection using a polyline
102 //=======================================================================
103 void SelectMgr_ViewerSelector3d::Pick (const TColgp_Array1OfPnt2d& thePolyline,
104 const Handle(V3d_View)& theView)
106 updateZLayers (theView);
107 mySelectingVolumeMgr.SetCamera (theView->Camera());
108 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
109 Standard_Integer aWidth = 0, aHeight = 0;
110 theView->Window()->Size (aWidth, aHeight);
111 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
112 mySelectingVolumeMgr.BuildSelectingVolume (thePolyline);
114 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
116 TraverseSensitives();
119 //=======================================================================
120 // Function: DisplaySensitive.
121 // Purpose : Display active primitives.
122 //=======================================================================
123 void SelectMgr_ViewerSelector3d::DisplaySensitive (const Handle(V3d_View)& theView)
125 for (SelectMgr_SelectableObjectSet::Iterator aSelectableIt (mySelectableObjects); aSelectableIt.More(); aSelectableIt.Next())
127 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
128 const Handle (SelectMgr_SelectableObject)& anObj = aSelectableIt.Value();
129 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
131 if (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated)
133 SelectMgr::ComputeSensitivePrs (aStruct, aSelIter.Value(), anObj->Transformation(), Handle(Graphic3d_TransformPers)());
137 myStructs.Append (aStruct);
140 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
142 Handle(Graphic3d_Structure)& aStruct = aStructIter.ChangeValue();
143 aStruct->SetDisplayPriority (10);
150 //=======================================================================
151 // Function: ClearSensitive
153 //=======================================================================
154 void SelectMgr_ViewerSelector3d::ClearSensitive (const Handle(V3d_View)& theView)
156 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
158 aStructIter.ChangeValue()->Remove();
162 if (!theView.IsNull())
168 //=======================================================================
169 //function : DisplaySenstive
171 //=======================================================================
172 void SelectMgr_ViewerSelector3d::DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
173 const gp_Trsf& theTrsf,
174 const Handle(V3d_View)& theView,
175 const Standard_Boolean theToClearOthers)
177 if (theToClearOthers)
179 ClearSensitive (theView);
182 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
184 SelectMgr::ComputeSensitivePrs (aStruct, theSel, theTrsf, Handle(Graphic3d_TransformPers)());
186 myStructs.Append (aStruct);
187 myStructs.Last()->SetDisplayPriority (10);
188 myStructs.Last()->Display();
193 //=======================================================================
194 // Function: updateZLayers
196 //=======================================================================
197 void SelectMgr_ViewerSelector3d::updateZLayers (const Handle(V3d_View)& theView)
199 myZLayerOrderMap.Clear();
200 TColStd_SequenceOfInteger aZLayers;
201 theView->Viewer()->GetAllZLayers (aZLayers);
202 Standard_Integer aPos = 0;
203 Standard_Boolean isPrevDepthWrite = true;
204 for (TColStd_SequenceOfInteger::Iterator aLayerIter (aZLayers); aLayerIter.More(); aLayerIter.Next())
206 Graphic3d_ZLayerSettings aSettings = theView->Viewer()->ZLayerSettings (aLayerIter.Value());
207 if (aSettings.ToClearDepth()
208 || isPrevDepthWrite != aSettings.ToEnableDepthWrite())
212 isPrevDepthWrite = aSettings.ToEnableDepthWrite();
213 myZLayerOrderMap.Bind (aLayerIter.Value(), aPos);
217 //=======================================================================
218 //function : ToPixMap
220 //=======================================================================
221 Standard_Boolean SelectMgr_ViewerSelector3d::ToPixMap (Image_PixMap& theImage,
222 const Handle(V3d_View)& theView,
223 const StdSelect_TypeOfSelectionImage theType,
224 const Standard_Integer thePickedIndex)
226 if (theImage.IsEmpty())
228 throw Standard_ProgramError("SelectMgr_ViewerSelector3d::ToPixMap() has been called with empty image");
231 Handle(SelectMgr_SelectionImageFiller) aFiller = SelectMgr_SelectionImageFiller::CreateFiller (theImage, this, theType);
232 if (aFiller.IsNull())
234 return Standard_False;
237 const Standard_Integer aSizeX = static_cast<Standard_Integer> (theImage.SizeX());
238 const Standard_Integer aSizeY = static_cast<Standard_Integer> (theImage.SizeY());
239 for (Standard_Integer aRowIter = 0; aRowIter < aSizeY; ++aRowIter)
241 for (Standard_Integer aColIter = 0; aColIter < aSizeX; ++aColIter)
243 Pick (aColIter, aRowIter, theView);
244 aFiller->Fill (aColIter, aRowIter, thePickedIndex);
248 return Standard_True;
251 //=======================================================================
252 //function : DumpJson
254 //=======================================================================
255 void SelectMgr_ViewerSelector3d::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
257 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
259 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myStructs.Length())
260 for (Graphic3d_SequenceOfStructure::Iterator aStructsIt (myStructs); aStructsIt.More(); aStructsIt.Next())
262 const Handle(Graphic3d_Structure)& aStructure = aStructsIt.Value();
263 OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, aStructure)