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 <StdSelect_ViewerSelector3d.hxx>
18 #include <StdSelect.hxx>
19 #include <Select3D_SensitiveEntity.hxx>
20 #include <Graphic3d_AspectLine3d.hxx>
23 #include <gp_Pnt2d.hxx>
26 #include <gp_GTrsf.hxx>
28 #include <Select3D_SensitiveEntity.hxx>
29 #include <Graphic3d_ArrayOfPolylines.hxx>
30 #include <Graphic3d_Group.hxx>
31 #include <Graphic3d_SequenceOfHClipPlane.hxx>
32 #include <Graphic3d_Structure.hxx>
33 #include <SelectMgr_SelectableObject.hxx>
34 #include <TColgp_HArray1OfPnt.hxx>
35 #include <TColgp_Array1OfPnt.hxx>
36 #include <TColgp_Array1OfPnt2d.hxx>
37 #include <TColgp_HArray1OfPnt2d.hxx>
38 #include <Select3D_SensitiveCurve.hxx>
39 #include <Select3D_SensitiveSegment.hxx>
40 #include <Select3D_SensitiveFace.hxx>
41 #include <Select3D_SensitiveCircle.hxx>
42 #include <Select3D_SensitivePoint.hxx>
43 #include <Select3D_SensitiveTriangulation.hxx>
44 #include <Select3D_SensitiveTriangle.hxx>
45 #include <Select3D_SensitiveWire.hxx>
46 #include <Select3D_SensitiveBox.hxx>
47 #include <SelectMgr_Selection.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
50 #include <Aspect_Grid.hxx>
51 #include <Aspect_TypeOfMarker.hxx>
52 #include <Aspect_Window.hxx>
53 #include <Graphic3d_AspectMarker3d.hxx>
54 #include <Graphic3d_ArrayOfPoints.hxx>
55 #include <math_BullardGenerator.hxx>
56 #include <Message.hxx>
57 #include <Message_Messenger.hxx>
58 #include <Quantity_ColorHasher.hxx>
59 #include <Poly_Connect.hxx>
60 #include <TColStd_HArray1OfInteger.hxx>
62 #include <Poly_Array1OfTriangle.hxx>
63 #include <Poly_Triangulation.hxx>
64 #include <OSD_Environment.hxx>
66 #include <V3d_View.hxx>
67 #include <V3d_Viewer.hxx>
68 #include <TColgp_SequenceOfPnt.hxx>
70 #include <OSD_Timer.hxx>
73 IMPLEMENT_STANDARD_RTTIEXT(StdSelect_ViewerSelector3d,SelectMgr_ViewerSelector)
75 static Standard_Integer StdSel_NumberOfFreeEdges (const Handle(Poly_Triangulation)& Trg)
77 Standard_Integer nFree = 0;
79 Standard_Integer t[3];
80 Standard_Integer i, j;
81 for (i = 1; i <= Trg->NbTriangles(); i++)
83 pc.Triangles (i, t[0], t[1], t[2]);
84 for (j = 0; j < 3; j++)
85 if (t[j] == 0) nFree++;
90 //=======================================================================
91 // Function : Constructor
93 //=======================================================================
94 StdSelect_ViewerSelector3d::StdSelect_ViewerSelector3d() {}
96 //=======================================================================
97 // Function: SetPixelTolerance
99 //=======================================================================
100 void StdSelect_ViewerSelector3d::SetPixelTolerance (const Standard_Integer theTolerance)
102 if (myTolerances.Tolerance() != theTolerance)
104 if (theTolerance < 0)
105 myTolerances.ResetDefaults();
107 myTolerances.SetCustomTolerance (theTolerance);
108 myToUpdateTolerance = Standard_True;
112 //=======================================================================
115 //=======================================================================
116 void StdSelect_ViewerSelector3d::Pick (const Standard_Integer theXPix,
117 const Standard_Integer theYPix,
118 const Handle(V3d_View)& theView)
120 updateZLayers (theView);
121 if(myToUpdateTolerance)
123 mySelectingVolumeMgr.SetPixelTolerance (myTolerances.Tolerance());
124 myToUpdateTolerance = Standard_False;
127 mySelectingVolumeMgr.SetCamera (theView->Camera());
128 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Point);
129 Standard_Integer aWidth = 0, aHeight = 0;
130 theView->Window()->Size (aWidth, aHeight);
131 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
132 gp_Pnt2d aMousePos (static_cast<Standard_Real> (theXPix),
133 static_cast<Standard_Real> (theYPix));
134 mySelectingVolumeMgr.BuildSelectingVolume (aMousePos);
135 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes());
137 TraverseSensitives();
140 //=======================================================================
143 //=======================================================================
144 void StdSelect_ViewerSelector3d::Pick (const Standard_Integer theXPMin,
145 const Standard_Integer theYPMin,
146 const Standard_Integer theXPMax,
147 const Standard_Integer theYPMax,
148 const Handle(V3d_View)& theView)
150 updateZLayers (theView);
151 mySelectingVolumeMgr.SetCamera (theView->Camera());
152 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Box);
153 Standard_Integer aWidth = 0, aHeight = 0;
154 theView->Window()->Size (aWidth, aHeight);
155 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
156 gp_Pnt2d aMinMousePos (static_cast<Standard_Real> (theXPMin),
157 static_cast<Standard_Real> (theYPMin));
158 gp_Pnt2d aMaxMousePos (static_cast<Standard_Real> (theXPMax),
159 static_cast<Standard_Real> (theYPMax));
160 mySelectingVolumeMgr.BuildSelectingVolume (aMinMousePos,
163 TraverseSensitives();
166 //=======================================================================
168 // Purpose : Selection using a polyline
169 //=======================================================================
170 void StdSelect_ViewerSelector3d::Pick (const TColgp_Array1OfPnt2d& thePolyline,
171 const Handle(V3d_View)& theView)
173 updateZLayers (theView);
174 mySelectingVolumeMgr.SetCamera (theView->Camera());
175 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
176 Standard_Integer aWidth = 0, aHeight = 0;
177 theView->Window()->Size (aWidth, aHeight);
178 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
179 mySelectingVolumeMgr.BuildSelectingVolume (thePolyline);
181 TraverseSensitives();
184 //=======================================================================
185 // Function: DisplaySensitive.
186 // Purpose : Display active primitives.
187 //=======================================================================
188 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(V3d_View)& theView)
190 for (SelectMgr_SelectableObjectSet::Iterator aSelectableIt (mySelectableObjects); aSelectableIt.More(); aSelectableIt.Next())
192 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
193 const Handle (SelectMgr_SelectableObject)& anObj = aSelectableIt.Value();
194 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
196 if (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated)
198 computeSensitivePrs (aStruct, aSelIter.Value(), anObj->Transformation(), Handle(Graphic3d_TransformPers)());
202 myStructs.Append (aStruct);
205 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
207 Handle(Graphic3d_Structure)& aStruct = aStructIter.ChangeValue();
208 aStruct->SetDisplayPriority (10);
215 //=======================================================================
216 // Function: ClearSensitive
218 //=======================================================================
219 void StdSelect_ViewerSelector3d::ClearSensitive (const Handle(V3d_View)& theView)
221 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
223 aStructIter.ChangeValue()->Remove();
227 if (!theView.IsNull())
233 //=======================================================================
234 //function : DisplaySenstive
236 //=======================================================================
237 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
238 const gp_Trsf& theTrsf,
239 const Handle(V3d_View)& theView,
240 const Standard_Boolean theToClearOthers)
242 if (theToClearOthers)
244 ClearSensitive (theView);
247 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
249 computeSensitivePrs (aStruct, theSel, theTrsf, Handle(Graphic3d_TransformPers)());
251 myStructs.Append (aStruct);
252 myStructs.Last()->SetDisplayPriority (10);
253 myStructs.Last()->Display();
258 //=======================================================================
259 //function : computeSensitivePrs
261 //=======================================================================
262 void StdSelect_ViewerSelector3d::computeSensitivePrs (const Handle(Graphic3d_Structure)& theStructure,
263 const Handle(SelectMgr_Selection)& theSel,
264 const gp_Trsf& theLoc,
265 const Handle(Graphic3d_TransformPers)& theTrsfPers)
267 theStructure->SetTransformPersistence (theTrsfPers);
269 Handle(Graphic3d_Group) aSensGroup = theStructure->NewGroup();
271 Quantity_Color aColor (Quantity_NOC_INDIANRED3);
272 Handle(Graphic3d_AspectMarker3d) aMarkerAspect =new Graphic3d_AspectMarker3d (Aspect_TOM_O_PLUS, aColor, 2.0);
274 aSensGroup->SetPrimitivesAspect (aMarkerAspect);
275 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
277 Handle(Graphic3d_Group) anAreaGroup = theStructure->NewGroup();
279 anAreaGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_AQUAMARINE1, Aspect_TOL_DASH, 1.0));
281 TColgp_SequenceOfPnt aSeqLines, aSeqFree;
282 TColStd_SequenceOfInteger aSeqBnds;
283 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
285 const Handle(Select3D_SensitiveEntity)& Ent = aSelEntIter.Value()->BaseSensitive();
286 const Standard_Boolean hasloc = theLoc.Form() != gp_Identity;
292 if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveBox))
294 const Bnd_Box& B = Handle(Select3D_SensitiveBox)::DownCast (Ent)->Box();
295 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
296 B.Get (xmin, ymin, zmin, xmax, ymax, zmax);
298 gp_Pnt theboxpoint[8] =
300 gp_Pnt(xmin,ymin,zmin),
301 gp_Pnt(xmax,ymin,zmin),
302 gp_Pnt(xmax,ymax,zmin),
303 gp_Pnt(xmin,ymax,zmin),
304 gp_Pnt(xmin,ymin,zmax),
305 gp_Pnt(xmax,ymin,zmax),
306 gp_Pnt(xmax,ymax,zmax),
307 gp_Pnt(xmin,ymax,zmax)
311 for (i = 0; i <= 7; i++)
312 theboxpoint[i].Transform (theLoc);
316 for (i = 0; i < 4; i++)
317 aSeqLines.Append(theboxpoint[i]);
318 aSeqLines.Append(theboxpoint[0]);
321 for (i = 4; i < 8; i++)
322 aSeqLines.Append(theboxpoint[i]);
323 aSeqLines.Append(theboxpoint[4]);
325 for (i = 0; i < 4; i++)
328 aSeqLines.Append(theboxpoint[i]);
329 aSeqLines.Append(theboxpoint[i+4]);
335 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveFace))
337 Handle(Select3D_SensitiveFace) aFace = Handle(Select3D_SensitiveFace)::DownCast(Ent);
338 Handle(TColgp_HArray1OfPnt) TheHPts;
339 aFace->GetPoints(TheHPts);
340 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
342 aSeqBnds.Append(ThePts.Length());
343 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
346 aSeqLines.Append(ThePts(I).Transformed (theLoc));
348 aSeqLines.Append(ThePts(I));
354 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
356 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(Ent);
357 Handle(TColgp_HArray1OfPnt) TheHPts;
358 aCurve->Points3D(TheHPts);
359 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
361 aSeqBnds.Append(ThePts.Length());
362 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
365 aSeqLines.Append(ThePts(I).Transformed (theLoc));
367 aSeqLines.Append(ThePts(I));
373 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveWire))
375 Handle(Select3D_SensitiveWire) aWire = Handle(Select3D_SensitiveWire)::DownCast(Ent);
376 const NCollection_Vector<Handle(Select3D_SensitiveEntity)>& anEntities = aWire->GetEdges();
378 for (int i = 0; i < anEntities.Length(); i++)
380 Handle(Select3D_SensitiveEntity) SubEnt = anEntities.Value(i);
383 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
385 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->StartPoint().XYZ());
386 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->EndPoint().XYZ());
389 P1.Transform(theLoc);
390 P2.Transform(theLoc);
393 aSeqLines.Append(P1);
394 aSeqLines.Append(P2);
398 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
400 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(SubEnt);
401 Standard_Integer aFrom, aTo;
402 aCircle->ArrayBounds (aFrom, aTo);
404 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
408 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
409 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
410 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
415 aPnts[0].Transform (theLoc);
416 aPnts[1].Transform (theLoc);
417 aPnts[2].Transform (theLoc);
421 aSeqLines.Append (aPnts[0]);
422 aSeqLines.Append (aPnts[1]);
423 aSeqLines.Append (aPnts[2]);
424 aSeqLines.Append (aPnts[0]);
429 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
431 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(SubEnt);
432 Handle(TColgp_HArray1OfPnt) TheHPts;
433 aCurve->Points3D (TheHPts);
434 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
436 aSeqBnds.Append(ThePts.Length());
437 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
440 aSeqLines.Append(ThePts(I).Transformed (theLoc));
442 aSeqLines.Append(ThePts(I));
450 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
452 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->StartPoint().XYZ());
453 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->EndPoint().XYZ());
456 P1.Transform (theLoc);
457 P2.Transform (theLoc);
460 aSeqLines.Append(P1);
461 aSeqLines.Append(P2);
466 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
468 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(Ent);
469 Standard_Integer aFrom, aTo;
470 aCircle->ArrayBounds (aFrom, aTo);
472 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
476 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
477 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
478 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
483 aPnts[0].Transform (theLoc);
484 aPnts[1].Transform (theLoc);
485 aPnts[2].Transform (theLoc);
489 aSeqLines.Append (aPnts[0]);
490 aSeqLines.Append (aPnts[1]);
491 aSeqLines.Append (aPnts[2]);
492 aSeqLines.Append (aPnts[0]);
498 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitivePoint))
501 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point() :
502 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point().Transformed (theLoc);
503 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
504 anArrayOfPoints->AddVertex (P.X(), P.Y(), P.Z());
505 aSensGroup->AddPrimitiveArray (anArrayOfPoints);
507 //============================================================
508 // Triangulation : On met un petit offset ves l'interieur...
509 //==========================================================
510 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangulation))
512 Handle(Poly_Triangulation) PT (Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->Triangulation());
514 const Poly_Array1OfTriangle& triangles = PT->Triangles();
515 const TColgp_Array1OfPnt& Nodes = PT->Nodes();
516 Standard_Integer n[3];
518 TopLoc_Location iloc, bidloc;
519 if (Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->HasInitLocation())
520 bidloc = Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->GetInitLocation();
522 if (bidloc.IsIdentity())
525 iloc = theLoc * bidloc;
528 for (i = 1; i <= PT->NbTriangles(); i++)
530 triangles (i).Get (n[0], n[1], n[2]);
531 gp_Pnt P1 (Nodes (n[0]).Transformed (iloc));
532 gp_Pnt P2 (Nodes (n[1]).Transformed (iloc));
533 gp_Pnt P3 (Nodes (n[2]).Transformed (iloc));
534 gp_XYZ V1 (P1.XYZ());
535 gp_XYZ V2 (P2.XYZ());
536 gp_XYZ V3 (P3.XYZ());
537 gp_XYZ CDG (P1.XYZ()); CDG += (P2.XYZ()); CDG += (P3.XYZ()); CDG /= 3.0;
538 V1 -= CDG; V2 -= CDG; V3 -= CDG;
539 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
540 V1 += CDG; V2 += CDG; V3 += CDG;
543 aSeqLines.Append(gp_Pnt(V1));
544 aSeqLines.Append(gp_Pnt(V2));
545 aSeqLines.Append(gp_Pnt(V3));
546 aSeqLines.Append(gp_Pnt(V1));
549 // recherche des bords libres...
551 Handle(TColStd_HArray1OfInteger) FreeEdges = new TColStd_HArray1OfInteger (1, 2 * StdSel_NumberOfFreeEdges (PT));
552 TColStd_Array1OfInteger& FreeE = FreeEdges->ChangeArray1();
553 Poly_Connect pc (PT);
554 Standard_Integer t[3];
556 Standard_Integer fr (1);
557 for (i = 1; i <= PT->NbTriangles(); i++)
559 pc.Triangles (i, t[0], t[1], t[2]);
560 triangles (i).Get (n[0], n[1], n[2]);
561 for (j = 0; j < 3; j++)
563 Standard_Integer k = (j + 1) % 3;
567 FreeE (fr + 1)= n[k];
572 for (Standard_Integer ifri = 1; ifri <= FreeE.Length(); ifri += 2)
574 gp_Pnt pe1 (Nodes (FreeE (ifri)).Transformed (iloc)), pe2 (Nodes (FreeE (ifri + 1)).Transformed (iloc));
575 aSeqFree.Append(pe1);
576 aSeqFree.Append(pe2);
579 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangle))
581 Handle(Select3D_SensitiveTriangle) Str = Handle(Select3D_SensitiveTriangle)::DownCast(Ent);
583 Str->Points3D (P1, P2, P3);
584 gp_Pnt CDG = Str->Center3D();
586 gp_XYZ V1 (P1.XYZ()); V1 -= (CDG.XYZ());
587 gp_XYZ V2 (P2.XYZ()); V2 -= (CDG.XYZ());
588 gp_XYZ V3 (P3.XYZ()); V3 -= (CDG.XYZ());
589 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
590 V1 += CDG.XYZ(); V2 += CDG.XYZ(); V3 += CDG.XYZ();
593 aSeqLines.Append(gp_Pnt(V1));
594 aSeqLines.Append(gp_Pnt(V2));
595 aSeqLines.Append(gp_Pnt(V3));
596 aSeqLines.Append(gp_Pnt(V1));
602 if (aSeqLines.Length())
604 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),aSeqBnds.Length());
605 for (i = 1; i <= aSeqLines.Length(); i++)
606 aPrims->AddVertex(aSeqLines(i));
607 for (i = 1; i <= aSeqBnds.Length(); i++)
608 aPrims->AddBound(aSeqBnds(i));
609 anAreaGroup->AddPrimitiveArray(aPrims);
612 if (aSeqFree.Length())
614 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GREEN, Aspect_TOL_SOLID, 2.0));
615 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqFree.Length(),aSeqFree.Length()/2);
616 for (i = 1; i <= aSeqFree.Length(); i++)
619 aPrims->AddVertex(aSeqLines(i++));
620 aPrims->AddVertex(aSeqLines(i));
622 aSensGroup->AddPrimitiveArray(aPrims);
623 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
627 //=======================================================================
628 //function : HasDepthClipping
630 //=======================================================================
631 Standard_Boolean StdSelect_ViewerSelector3d::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& theOwner) const
633 if (!theOwner->HasSelectable())
635 return Standard_False;
638 const Handle(SelectMgr_SelectableObject)& aSelectable = theOwner->Selectable();
639 return !aSelectable->ClipPlanes().IsNull()
640 && !aSelectable->ClipPlanes()->IsEmpty();
643 //=======================================================================
644 // Function: updateZLayers
646 //=======================================================================
647 void StdSelect_ViewerSelector3d::updateZLayers (const Handle(V3d_View)& theView)
649 myZLayerOrderMap.Clear();
650 TColStd_SequenceOfInteger aZLayers;
651 theView->Viewer()->GetAllZLayers (aZLayers);
652 Standard_Integer aPos = 0;
653 Standard_Boolean isPrevDepthWrite = true;
654 for (TColStd_SequenceOfInteger::Iterator aLayerIter (aZLayers); aLayerIter.More(); aLayerIter.Next())
656 Graphic3d_ZLayerSettings aSettings = theView->Viewer()->ZLayerSettings (aLayerIter.Value());
657 if (aSettings.ToClearDepth()
658 || isPrevDepthWrite != aSettings.ToEnableDepthWrite())
662 isPrevDepthWrite = aSettings.ToEnableDepthWrite();
663 myZLayerOrderMap.Bind (aLayerIter.Value(), aPos);
669 //! Abstract class for filling pixel with color.
670 class BaseFiller : public Standard_Transient
672 DEFINE_STANDARD_RTTI_INLINE(BaseFiller, Standard_Transient)
675 //! Main constructor.
676 BaseFiller (Image_PixMap& thePixMap,
677 StdSelect_ViewerSelector3d* theSelector)
678 : myImage (&thePixMap),
679 myMainSel(theSelector) {}
681 //! Fill pixel at specified position.
682 virtual void Fill (const Standard_Integer theCol,
683 const Standard_Integer theRow,
684 const Standard_Integer thePicked) = 0;
686 //! Flush results into final image.
687 virtual void Flush() {}
691 //! Find the new unique random color.
692 void randomPastelColor (Quantity_Color& theColor)
696 nextRandomPastelColor (theColor);
697 if (myUniqueColors.Add (theColor))
704 //! Fills the given color as random.
705 void nextRandomPastelColor (Quantity_Color& theColor)
707 theColor = Quantity_Color (Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
708 Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
709 Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
714 Image_PixMap* myImage;
715 StdSelect_ViewerSelector3d* myMainSel;
716 math_BullardGenerator myBullardGenerator;
717 NCollection_Map<Quantity_Color, Quantity_ColorHasher> myUniqueColors;
720 //! Help class for filling pixel with random color.
721 class GeneratedEntityColorFiller : public BaseFiller
723 DEFINE_STANDARD_RTTI_INLINE(GeneratedEntityColorFiller, BaseFiller)
725 GeneratedEntityColorFiller (Image_PixMap& thePixMap,
726 StdSelect_ViewerSelector3d* theSelector,
727 const SelectMgr_SelectableObjectSet& theSelObjects)
728 : BaseFiller (thePixMap, theSelector)
730 // generate per-entity colors in the order as they have been activated
731 for (SelectMgr_SelectableObjectSet::Iterator anObjIter (theSelObjects); anObjIter.More(); anObjIter.Next())
733 const Handle(SelectMgr_SelectableObject)& anObj = anObjIter.Value();
734 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
736 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
737 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
739 const Handle(SelectMgr_SensitiveEntity)& aSens = aSelEntIter.Value();
740 if (!myMapEntityColors.IsBound (aSens->BaseSensitive()))
742 Quantity_Color aColor;
743 randomPastelColor (aColor);
744 myMapEntityColors.Bind (aSens->BaseSensitive(), aColor);
751 virtual void Fill (const Standard_Integer theCol,
752 const Standard_Integer theRow,
753 const Standard_Integer thePicked) Standard_OVERRIDE
756 || thePicked > myMainSel->NbPicked())
758 myImage->SetPixelColor (theCol, theRow, Quantity_Color(Quantity_NOC_BLACK));
762 const Handle(Select3D_SensitiveEntity)& aPickedEntity = myMainSel->PickedEntity (thePicked);
763 Quantity_Color aColor (Quantity_NOC_BLACK);
764 myMapEntityColors.Find (aPickedEntity, aColor);
765 myImage->SetPixelColor (theCol, theRow, aColor);
769 NCollection_DataMap<Handle(Select3D_SensitiveEntity), Quantity_Color> myMapEntityColors;
772 //! Help class for filling pixel with normalized depth of ray.
773 class NormalizedDepthFiller : public BaseFiller
775 DEFINE_STANDARD_RTTI_INLINE(NormalizedDepthFiller, BaseFiller)
777 NormalizedDepthFiller (Image_PixMap& thePixMap,
778 StdSelect_ViewerSelector3d* theSelector,
779 const Standard_Boolean theToInverse)
780 : BaseFiller (thePixMap, theSelector),
781 myDepthMin ( RealLast()),
782 myDepthMax (-RealLast()),
783 myToInverse(theToInverse)
785 myUnnormImage.InitZero (Image_Format_GrayF, thePixMap.SizeX(), thePixMap.SizeY());
788 //! Accumulate the data.
789 virtual void Fill (const Standard_Integer theCol,
790 const Standard_Integer theRow,
791 const Standard_Integer thePicked) Standard_OVERRIDE
793 if (myUnnormImage.IsEmpty())
799 || thePicked > myMainSel->NbPicked())
801 myUnnormImage.ChangeValue<float> (theRow, theCol) = ShortRealLast();
805 const SelectMgr_SortCriterion& aSortCriterion = myMainSel->PickedData (thePicked);
806 myUnnormImage.ChangeValue<float> (theRow, theCol) = float(aSortCriterion.Depth);
807 myDepthMin = Min (myDepthMin, aSortCriterion.Depth);
808 myDepthMax = Max (myDepthMax, aSortCriterion.Depth);
811 //! Normalize the depth values.
812 virtual void Flush() Standard_OVERRIDE
816 if (myDepthMin <= myDepthMax)
818 aFrom = float(myDepthMin);
819 aDelta = float(myDepthMax) - float(myDepthMin);
820 if (aDelta <= ShortRealEpsilon())
825 for (Standard_Size aRowIter = 0; aRowIter < myUnnormImage.SizeY(); ++aRowIter)
827 for (Standard_Size aColIter = 0; aColIter < myUnnormImage.SizeX(); ++aColIter)
829 float aDepth = myUnnormImage.Value<float> (aRowIter, aColIter);
830 if (aDepth <= -ShortRealLast()
831 || aDepth >= ShortRealLast())
833 myImage->SetPixelColor (Standard_Integer(aColIter), Standard_Integer(aRowIter),
834 Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 1.0f));
838 float aNormDepth = (aDepth - aFrom) / aDelta;
841 aNormDepth = 1.0f - aNormDepth;
843 myImage->SetPixelColor (Standard_Integer(aColIter), Standard_Integer(aRowIter),
844 Quantity_ColorRGBA (aNormDepth, aNormDepth, aNormDepth, 1.0f));
850 Image_PixMap myUnnormImage;
851 Standard_Real myDepthMin;
852 Standard_Real myDepthMax;
853 Standard_Boolean myToInverse;
856 //! Help class for filling pixel with unnormalized depth of ray.
857 class UnnormalizedDepthFiller : public BaseFiller
859 DEFINE_STANDARD_RTTI_INLINE(UnnormalizedDepthFiller, BaseFiller)
861 UnnormalizedDepthFiller (Image_PixMap& thePixMap,
862 StdSelect_ViewerSelector3d* theSelector)
863 : BaseFiller (thePixMap, theSelector) {}
865 virtual void Fill (const Standard_Integer theCol,
866 const Standard_Integer theRow,
867 const Standard_Integer thePicked) Standard_OVERRIDE
870 || thePicked > myMainSel->NbPicked())
872 myImage->SetPixelColor (theCol, theRow, Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 1.0f));
876 const SelectMgr_SortCriterion& aSortCriterion = myMainSel->PickedData (thePicked);
877 const float aDepth = float(aSortCriterion.Depth);
878 myImage->SetPixelColor (theCol, theRow, Quantity_ColorRGBA (Graphic3d_Vec4 (aDepth, aDepth, aDepth, 1.0f)));
882 //! Help class for filling pixel with color of detected object.
883 class GeneratedOwnerColorFiller : public BaseFiller
885 DEFINE_STANDARD_RTTI_INLINE(GeneratedOwnerColorFiller, BaseFiller)
887 GeneratedOwnerColorFiller (Image_PixMap& thePixMap,
888 StdSelect_ViewerSelector3d* theSelector,
889 const SelectMgr_SelectableObjectSet& theSelObjects)
890 : BaseFiller (thePixMap, theSelector)
892 // generate per-owner colors in the order as they have been activated
893 for (SelectMgr_SelectableObjectSet::Iterator anObjIter (theSelObjects); anObjIter.More(); anObjIter.Next())
895 const Handle(SelectMgr_SelectableObject)& anObj = anObjIter.Value();
896 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
898 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
899 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
901 const Handle(SelectMgr_SensitiveEntity)& aSens = aSelEntIter.Value();
902 const Handle(SelectBasics_EntityOwner)& anOwner = aSens->BaseSensitive()->OwnerId();
903 if (!myMapOwnerColors.IsBound (anOwner))
905 Quantity_Color aColor;
906 randomPastelColor (aColor);
907 myMapOwnerColors.Bind (anOwner, aColor);
914 virtual void Fill (const Standard_Integer theCol,
915 const Standard_Integer theRow,
916 const Standard_Integer thePicked) Standard_OVERRIDE
919 || thePicked > myMainSel->NbPicked())
921 myImage->SetPixelColor (theCol, theRow, Quantity_Color(Quantity_NOC_BLACK));
925 const Handle(SelectMgr_EntityOwner)& aPickedOwner = myMainSel->Picked (thePicked);
926 Quantity_Color aColor (Quantity_NOC_BLACK);
927 myMapOwnerColors.Find (aPickedOwner, aColor);
928 myImage->SetPixelColor (theCol, theRow, aColor);
932 NCollection_DataMap<Handle(SelectBasics_EntityOwner), Quantity_Color> myMapOwnerColors;
935 //! Help class for filling pixel with random color for each selection mode.
936 class GeneratedSelModeColorFiller : public BaseFiller
938 DEFINE_STANDARD_RTTI_INLINE(GeneratedSelModeColorFiller, BaseFiller)
940 GeneratedSelModeColorFiller (Image_PixMap& thePixMap,
941 StdSelect_ViewerSelector3d* theSelector)
942 : BaseFiller (thePixMap, theSelector)
944 // generate standard modes in proper order, consider custom objects would use similar scheme
945 myMapSelectionModeColors.Bind ( 0, Quantity_NOC_WHITE); // default (entire object selection)
946 myMapSelectionModeColors.Bind ( 1, Quantity_NOC_YELLOW); // TopAbs_VERTEX
947 myMapSelectionModeColors.Bind ( 2, Quantity_NOC_GREEN); // TopAbs_EDGE
948 myMapSelectionModeColors.Bind ( 3, Quantity_NOC_RED); // TopAbs_WIRE
949 myMapSelectionModeColors.Bind ( 4, Quantity_NOC_BLUE1); // TopAbs_FACE
950 myMapSelectionModeColors.Bind ( 5, Quantity_NOC_CYAN1); // TopAbs_SHELL
951 myMapSelectionModeColors.Bind ( 6, Quantity_NOC_PURPLE); // TopAbs_SOLID
952 myMapSelectionModeColors.Bind ( 7, Quantity_NOC_MAGENTA1); // TopAbs_COMPSOLID
953 myMapSelectionModeColors.Bind ( 8, Quantity_NOC_BROWN); // TopAbs_COMPOUND
954 myMapSelectionModeColors.Bind (0x0010, Quantity_NOC_PINK); // MeshVS_SMF_Volume
955 myMapSelectionModeColors.Bind (0x001E, Quantity_NOC_LIMEGREEN); // MeshVS_SMF_Element
956 myMapSelectionModeColors.Bind (0x001F, Quantity_NOC_DARKOLIVEGREEN); // MeshVS_SMF_All
957 myMapSelectionModeColors.Bind (0x0100, Quantity_NOC_GOLD); // MeshVS_SMF_Group
960 virtual void Fill (const Standard_Integer theCol,
961 const Standard_Integer theRow,
962 const Standard_Integer thePicked) Standard_OVERRIDE
965 || thePicked > myMainSel->NbPicked())
967 myImage->SetPixelColor (theCol, theRow, Quantity_Color (Quantity_NOC_BLACK));
971 Standard_Integer aSelectionMode = -1;
972 const Handle(SelectMgr_SelectableObject)& aSelectable = myMainSel->Picked (thePicked)->Selectable();
973 const Handle(Select3D_SensitiveEntity)& anEntity = myMainSel->PickedEntity (thePicked);
974 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (aSelectable->Selections()); aSelIter.More(); aSelIter.Next())
976 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
977 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
979 if (aSelEntIter.Value()->BaseSensitive() == anEntity)
981 aSelectionMode = aSelection->Mode();
986 if (aSelectionMode == -1)
988 myImage->SetPixelColor (theCol, theRow, Quantity_Color (Quantity_NOC_BLACK));
992 if (!myMapSelectionModeColors.IsBound (aSelectionMode))
994 Quantity_Color aColor;
995 randomPastelColor (aColor);
996 myMapSelectionModeColors.Bind (aSelectionMode, aColor);
999 const Quantity_Color& aColor = myMapSelectionModeColors.Find (aSelectionMode);
1000 myImage->SetPixelColor (theCol, theRow, aColor);
1004 NCollection_DataMap<Standard_Integer, Quantity_Color> myMapSelectionModeColors;
1007 //! Help class for filling pixel with color of detected shape.
1008 class DetectedObjectColorFiller : public BaseFiller
1010 DEFINE_STANDARD_RTTI_INLINE(DetectedObjectColorFiller, BaseFiller)
1012 DetectedObjectColorFiller (Image_PixMap& thePixMap,
1013 StdSelect_ViewerSelector3d* theSelector)
1014 : BaseFiller (thePixMap, theSelector) {}
1016 virtual void Fill (const Standard_Integer theCol,
1017 const Standard_Integer theRow,
1018 const Standard_Integer thePicked) Standard_OVERRIDE
1020 Quantity_Color aColor (Quantity_NOC_BLACK);
1022 && thePicked <= myMainSel->NbPicked())
1024 const Handle(SelectMgr_SelectableObject)& aSelectable = myMainSel->Picked (thePicked)->Selectable();
1025 aColor = aSelectable->Attributes()->Color();
1027 myImage->SetPixelColor (theCol, theRow, aColor);
1033 //=======================================================================
1034 //function : ToPixMap
1036 //=======================================================================
1037 Standard_Boolean StdSelect_ViewerSelector3d::ToPixMap (Image_PixMap& theImage,
1038 const Handle(V3d_View)& theView,
1039 const StdSelect_TypeOfSelectionImage theType,
1040 const Standard_Integer thePickedIndex)
1042 if (theImage.IsEmpty())
1044 throw Standard_ProgramError("StdSelect_ViewerSelector3d::ToPixMap() has been called with empty image");
1047 Handle(BaseFiller) aFiller;
1050 case StdSelect_TypeOfSelectionImage_NormalizedDepth:
1051 case StdSelect_TypeOfSelectionImage_NormalizedDepthInverted:
1053 aFiller = new NormalizedDepthFiller (theImage, this,
1054 theType == StdSelect_TypeOfSelectionImage_NormalizedDepthInverted);
1057 case StdSelect_TypeOfSelectionImage_UnnormalizedDepth:
1059 aFiller = new UnnormalizedDepthFiller (theImage, this);
1062 case StdSelect_TypeOfSelectionImage_ColoredDetectedObject:
1064 aFiller = new DetectedObjectColorFiller (theImage, this);
1067 case StdSelect_TypeOfSelectionImage_ColoredEntity:
1069 aFiller = new GeneratedEntityColorFiller (theImage, this, mySelectableObjects);
1072 case StdSelect_TypeOfSelectionImage_ColoredOwner:
1074 aFiller = new GeneratedOwnerColorFiller (theImage, this, mySelectableObjects);
1077 case StdSelect_TypeOfSelectionImage_ColoredSelectionMode:
1079 aFiller = new GeneratedSelModeColorFiller (theImage, this);
1083 if (aFiller.IsNull())
1085 return Standard_False;
1088 const Standard_Integer aSizeX = static_cast<Standard_Integer> (theImage.SizeX());
1089 const Standard_Integer aSizeY = static_cast<Standard_Integer> (theImage.SizeY());
1090 for (Standard_Integer aRowIter = 0; aRowIter < aSizeY; ++aRowIter)
1092 for (Standard_Integer aColIter = 0; aColIter < aSizeX; ++aColIter)
1094 Pick (aColIter, aRowIter, theView);
1095 aFiller->Fill (aColIter, aRowIter, thePickedIndex);
1099 return Standard_True;