0029993: Visualization - AIS_TextLabel computes selection primitive only for attachme...
[occt.git] / src / SelectMgr / SelectMgr_ViewerSelector3d.cxx
CommitLineData
7f24b768 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
5//
6// This file is part of Open CASCADE Technology software library.
7//
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.
13//
14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
16
17#include <SelectMgr_ViewerSelector3d.hxx>
18
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>
31
32IMPLEMENT_STANDARD_RTTIEXT(SelectMgr_ViewerSelector3d, SelectMgr_ViewerSelector)
33
34//=======================================================================
35// Function : Constructor
36// Purpose :
37//=======================================================================
38SelectMgr_ViewerSelector3d::SelectMgr_ViewerSelector3d()
39{
40 //
41}
42
43//=======================================================================
44// Function: Pick
45// Purpose :
46//=======================================================================
47void SelectMgr_ViewerSelector3d::Pick (const Standard_Integer theXPix,
48 const Standard_Integer theYPix,
49 const Handle(V3d_View)& theView)
50{
51 updateZLayers (theView);
52 if(myToUpdateTolerance)
53 {
54 mySelectingVolumeMgr.SetPixelTolerance (myTolerances.Tolerance());
55 myToUpdateTolerance = Standard_False;
56 }
57
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)());
67
68 TraverseSensitives();
69}
70
71//=======================================================================
72// Function: Pick
73// Purpose :
74//=======================================================================
75void 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)
80{
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,
92 aMaxMousePos);
93
94 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
95
96 TraverseSensitives();
97}
98
99//=======================================================================
100// Function: Pick
101// Purpose : Selection using a polyline
102//=======================================================================
103void SelectMgr_ViewerSelector3d::Pick (const TColgp_Array1OfPnt2d& thePolyline,
104 const Handle(V3d_View)& theView)
105{
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);
113
114 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
115
116 TraverseSensitives();
117}
118
119//=======================================================================
120// Function: DisplaySensitive.
121// Purpose : Display active primitives.
122//=======================================================================
123void SelectMgr_ViewerSelector3d::DisplaySensitive (const Handle(V3d_View)& theView)
124{
125 for (SelectMgr_SelectableObjectSet::Iterator aSelectableIt (mySelectableObjects); aSelectableIt.More(); aSelectableIt.Next())
126 {
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())
130 {
131 if (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated)
132 {
3e9c1d1e 133 SelectMgr::ComputeSensitivePrs (aStruct, aSelIter.Value(), anObj->Transformation(), anObj->TransformPersistence());
7f24b768 134 }
135 }
136
137 myStructs.Append (aStruct);
138 }
139
140 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
141 {
142 Handle(Graphic3d_Structure)& aStruct = aStructIter.ChangeValue();
143 aStruct->SetDisplayPriority (10);
144 aStruct->Display();
145 }
146
147 theView->Update();
148}
149
150//=======================================================================
151// Function: ClearSensitive
152// Purpose :
153//=======================================================================
154void SelectMgr_ViewerSelector3d::ClearSensitive (const Handle(V3d_View)& theView)
155{
156 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
157 {
158 aStructIter.ChangeValue()->Remove();
159 }
160 myStructs.Clear();
161
162 if (!theView.IsNull())
163 {
164 theView->Update();
165 }
166}
167
168//=======================================================================
169//function : DisplaySenstive
170//purpose :
171//=======================================================================
172void SelectMgr_ViewerSelector3d::DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
173 const gp_Trsf& theTrsf,
174 const Handle(V3d_View)& theView,
175 const Standard_Boolean theToClearOthers)
176{
177 if (theToClearOthers)
178 {
179 ClearSensitive (theView);
180 }
181
182 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
183
184 SelectMgr::ComputeSensitivePrs (aStruct, theSel, theTrsf, Handle(Graphic3d_TransformPers)());
185
186 myStructs.Append (aStruct);
187 myStructs.Last()->SetDisplayPriority (10);
188 myStructs.Last()->Display();
189
190 theView->Update();
191}
192
193//=======================================================================
194// Function: updateZLayers
195// Purpose :
196//=======================================================================
197void SelectMgr_ViewerSelector3d::updateZLayers (const Handle(V3d_View)& theView)
198{
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())
205 {
206 Graphic3d_ZLayerSettings aSettings = theView->Viewer()->ZLayerSettings (aLayerIter.Value());
207 if (aSettings.ToClearDepth()
208 || isPrevDepthWrite != aSettings.ToEnableDepthWrite())
209 {
210 ++aPos;
211 }
212 isPrevDepthWrite = aSettings.ToEnableDepthWrite();
213 myZLayerOrderMap.Bind (aLayerIter.Value(), aPos);
214 }
215}
216
217//=======================================================================
218//function : ToPixMap
219//purpose :
220//=======================================================================
221Standard_Boolean SelectMgr_ViewerSelector3d::ToPixMap (Image_PixMap& theImage,
222 const Handle(V3d_View)& theView,
223 const StdSelect_TypeOfSelectionImage theType,
224 const Standard_Integer thePickedIndex)
225{
226 if (theImage.IsEmpty())
227 {
228 throw Standard_ProgramError("SelectMgr_ViewerSelector3d::ToPixMap() has been called with empty image");
229 }
230
231 Handle(SelectMgr_SelectionImageFiller) aFiller = SelectMgr_SelectionImageFiller::CreateFiller (theImage, this, theType);
232 if (aFiller.IsNull())
233 {
234 return Standard_False;
235 }
236
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)
240 {
241 for (Standard_Integer aColIter = 0; aColIter < aSizeX; ++aColIter)
242 {
243 Pick (aColIter, aRowIter, theView);
244 aFiller->Fill (aColIter, aRowIter, thePickedIndex);
245 }
246 }
247 aFiller->Flush();
248 return Standard_True;
249}
250
251//=======================================================================
252//function : DumpJson
253//purpose :
254//=======================================================================
255void SelectMgr_ViewerSelector3d::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
256{
257 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
258
259 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myStructs.Length())
260 for (Graphic3d_SequenceOfStructure::Iterator aStructsIt (myStructs); aStructsIt.More(); aStructsIt.Next())
261 {
262 const Handle(Graphic3d_Structure)& aStructure = aStructsIt.Value();
263 OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, aStructure)
264 }
265}