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(), Handle(Graphic3d_SequenceOfHClipPlane)());
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 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
165 TraverseSensitives();
168 //=======================================================================
170 // Purpose : Selection using a polyline
171 //=======================================================================
172 void StdSelect_ViewerSelector3d::Pick (const TColgp_Array1OfPnt2d& thePolyline,
173 const Handle(V3d_View)& theView)
175 updateZLayers (theView);
176 mySelectingVolumeMgr.SetCamera (theView->Camera());
177 mySelectingVolumeMgr.SetActiveSelectionType (SelectMgr_SelectingVolumeManager::Polyline);
178 Standard_Integer aWidth = 0, aHeight = 0;
179 theView->Window()->Size (aWidth, aHeight);
180 mySelectingVolumeMgr.SetWindowSize (aWidth, aHeight);
181 mySelectingVolumeMgr.BuildSelectingVolume (thePolyline);
183 mySelectingVolumeMgr.SetViewClipping (theView->ClipPlanes(), Handle(Graphic3d_SequenceOfHClipPlane)());
185 TraverseSensitives();
188 //=======================================================================
189 // Function: DisplaySensitive.
190 // Purpose : Display active primitives.
191 //=======================================================================
192 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(V3d_View)& theView)
194 for (SelectMgr_SelectableObjectSet::Iterator aSelectableIt (mySelectableObjects); aSelectableIt.More(); aSelectableIt.Next())
196 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
197 const Handle (SelectMgr_SelectableObject)& anObj = aSelectableIt.Value();
198 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
200 if (aSelIter.Value()->GetSelectionState() == SelectMgr_SOS_Activated)
202 computeSensitivePrs (aStruct, aSelIter.Value(), anObj->Transformation(), Handle(Graphic3d_TransformPers)());
206 myStructs.Append (aStruct);
209 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
211 Handle(Graphic3d_Structure)& aStruct = aStructIter.ChangeValue();
212 aStruct->SetDisplayPriority (10);
219 //=======================================================================
220 // Function: ClearSensitive
222 //=======================================================================
223 void StdSelect_ViewerSelector3d::ClearSensitive (const Handle(V3d_View)& theView)
225 for (Graphic3d_SequenceOfStructure::Iterator aStructIter (myStructs); aStructIter.More(); aStructIter.Next())
227 aStructIter.ChangeValue()->Remove();
231 if (!theView.IsNull())
237 //=======================================================================
238 //function : DisplaySenstive
240 //=======================================================================
241 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
242 const gp_Trsf& theTrsf,
243 const Handle(V3d_View)& theView,
244 const Standard_Boolean theToClearOthers)
246 if (theToClearOthers)
248 ClearSensitive (theView);
251 Handle(Graphic3d_Structure) aStruct = new Graphic3d_Structure (theView->Viewer()->StructureManager());
253 computeSensitivePrs (aStruct, theSel, theTrsf, Handle(Graphic3d_TransformPers)());
255 myStructs.Append (aStruct);
256 myStructs.Last()->SetDisplayPriority (10);
257 myStructs.Last()->Display();
262 //=======================================================================
263 //function : computeSensitivePrs
265 //=======================================================================
266 void StdSelect_ViewerSelector3d::computeSensitivePrs (const Handle(Graphic3d_Structure)& theStructure,
267 const Handle(SelectMgr_Selection)& theSel,
268 const gp_Trsf& theLoc,
269 const Handle(Graphic3d_TransformPers)& theTrsfPers)
271 theStructure->SetTransformPersistence (theTrsfPers);
273 Handle(Graphic3d_Group) aSensGroup = theStructure->NewGroup();
275 Quantity_Color aColor (Quantity_NOC_INDIANRED3);
276 Handle(Graphic3d_AspectMarker3d) aMarkerAspect =new Graphic3d_AspectMarker3d (Aspect_TOM_O_PLUS, aColor, 2.0);
278 aSensGroup->SetPrimitivesAspect (aMarkerAspect);
279 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
281 Handle(Graphic3d_Group) anAreaGroup = theStructure->NewGroup();
283 anAreaGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_AQUAMARINE1, Aspect_TOL_DASH, 1.0));
285 TColgp_SequenceOfPnt aSeqLines, aSeqFree;
286 TColStd_SequenceOfInteger aSeqBnds;
287 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
289 const Handle(Select3D_SensitiveEntity)& Ent = aSelEntIter.Value()->BaseSensitive();
290 const Standard_Boolean hasloc = theLoc.Form() != gp_Identity;
296 if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveBox))
298 const Bnd_Box& B = Handle(Select3D_SensitiveBox)::DownCast (Ent)->Box();
299 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
300 B.Get (xmin, ymin, zmin, xmax, ymax, zmax);
302 gp_Pnt theboxpoint[8] =
304 gp_Pnt(xmin,ymin,zmin),
305 gp_Pnt(xmax,ymin,zmin),
306 gp_Pnt(xmax,ymax,zmin),
307 gp_Pnt(xmin,ymax,zmin),
308 gp_Pnt(xmin,ymin,zmax),
309 gp_Pnt(xmax,ymin,zmax),
310 gp_Pnt(xmax,ymax,zmax),
311 gp_Pnt(xmin,ymax,zmax)
315 for (i = 0; i <= 7; i++)
316 theboxpoint[i].Transform (theLoc);
320 for (i = 0; i < 4; i++)
321 aSeqLines.Append(theboxpoint[i]);
322 aSeqLines.Append(theboxpoint[0]);
325 for (i = 4; i < 8; i++)
326 aSeqLines.Append(theboxpoint[i]);
327 aSeqLines.Append(theboxpoint[4]);
329 for (i = 0; i < 4; i++)
332 aSeqLines.Append(theboxpoint[i]);
333 aSeqLines.Append(theboxpoint[i+4]);
339 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveFace))
341 Handle(Select3D_SensitiveFace) aFace = Handle(Select3D_SensitiveFace)::DownCast(Ent);
342 Handle(TColgp_HArray1OfPnt) TheHPts;
343 aFace->GetPoints(TheHPts);
344 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
346 aSeqBnds.Append(ThePts.Length());
347 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
350 aSeqLines.Append(ThePts(I).Transformed (theLoc));
352 aSeqLines.Append(ThePts(I));
358 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
360 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(Ent);
361 Handle(TColgp_HArray1OfPnt) TheHPts;
362 aCurve->Points3D(TheHPts);
363 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
365 aSeqBnds.Append(ThePts.Length());
366 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
369 aSeqLines.Append(ThePts(I).Transformed (theLoc));
371 aSeqLines.Append(ThePts(I));
377 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveWire))
379 Handle(Select3D_SensitiveWire) aWire = Handle(Select3D_SensitiveWire)::DownCast(Ent);
380 const NCollection_Vector<Handle(Select3D_SensitiveEntity)>& anEntities = aWire->GetEdges();
382 for (int i = 0; i < anEntities.Length(); i++)
384 Handle(Select3D_SensitiveEntity) SubEnt = anEntities.Value(i);
387 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
389 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->StartPoint().XYZ());
390 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->EndPoint().XYZ());
393 P1.Transform(theLoc);
394 P2.Transform(theLoc);
397 aSeqLines.Append(P1);
398 aSeqLines.Append(P2);
402 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
404 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(SubEnt);
405 Standard_Integer aFrom, aTo;
406 aCircle->ArrayBounds (aFrom, aTo);
408 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
412 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
413 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
414 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
419 aPnts[0].Transform (theLoc);
420 aPnts[1].Transform (theLoc);
421 aPnts[2].Transform (theLoc);
425 aSeqLines.Append (aPnts[0]);
426 aSeqLines.Append (aPnts[1]);
427 aSeqLines.Append (aPnts[2]);
428 aSeqLines.Append (aPnts[0]);
433 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
435 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(SubEnt);
436 Handle(TColgp_HArray1OfPnt) TheHPts;
437 aCurve->Points3D (TheHPts);
438 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
440 aSeqBnds.Append(ThePts.Length());
441 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
444 aSeqLines.Append(ThePts(I).Transformed (theLoc));
446 aSeqLines.Append(ThePts(I));
454 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
456 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->StartPoint().XYZ());
457 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->EndPoint().XYZ());
460 P1.Transform (theLoc);
461 P2.Transform (theLoc);
464 aSeqLines.Append(P1);
465 aSeqLines.Append(P2);
470 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
472 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(Ent);
473 Standard_Integer aFrom, aTo;
474 aCircle->ArrayBounds (aFrom, aTo);
476 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
480 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
481 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
482 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
487 aPnts[0].Transform (theLoc);
488 aPnts[1].Transform (theLoc);
489 aPnts[2].Transform (theLoc);
493 aSeqLines.Append (aPnts[0]);
494 aSeqLines.Append (aPnts[1]);
495 aSeqLines.Append (aPnts[2]);
496 aSeqLines.Append (aPnts[0]);
502 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitivePoint))
505 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point() :
506 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point().Transformed (theLoc);
507 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
508 anArrayOfPoints->AddVertex (P.X(), P.Y(), P.Z());
509 aSensGroup->AddPrimitiveArray (anArrayOfPoints);
511 //============================================================
512 // Triangulation : On met un petit offset ves l'interieur...
513 //==========================================================
514 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangulation))
516 Handle(Poly_Triangulation) PT (Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->Triangulation());
518 const Poly_Array1OfTriangle& triangles = PT->Triangles();
519 const TColgp_Array1OfPnt& Nodes = PT->Nodes();
520 Standard_Integer n[3];
522 TopLoc_Location iloc, bidloc;
523 if (Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->HasInitLocation())
524 bidloc = Handle(Select3D_SensitiveTriangulation)::DownCast (Ent)->GetInitLocation();
526 if (bidloc.IsIdentity())
529 iloc = theLoc * bidloc;
532 for (i = 1; i <= PT->NbTriangles(); i++)
534 triangles (i).Get (n[0], n[1], n[2]);
535 gp_Pnt P1 (Nodes (n[0]).Transformed (iloc));
536 gp_Pnt P2 (Nodes (n[1]).Transformed (iloc));
537 gp_Pnt P3 (Nodes (n[2]).Transformed (iloc));
538 gp_XYZ V1 (P1.XYZ());
539 gp_XYZ V2 (P2.XYZ());
540 gp_XYZ V3 (P3.XYZ());
541 gp_XYZ CDG (P1.XYZ()); CDG += (P2.XYZ()); CDG += (P3.XYZ()); CDG /= 3.0;
542 V1 -= CDG; V2 -= CDG; V3 -= CDG;
543 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
544 V1 += CDG; V2 += CDG; V3 += CDG;
547 aSeqLines.Append(gp_Pnt(V1));
548 aSeqLines.Append(gp_Pnt(V2));
549 aSeqLines.Append(gp_Pnt(V3));
550 aSeqLines.Append(gp_Pnt(V1));
553 // recherche des bords libres...
555 Handle(TColStd_HArray1OfInteger) FreeEdges = new TColStd_HArray1OfInteger (1, 2 * StdSel_NumberOfFreeEdges (PT));
556 TColStd_Array1OfInteger& FreeE = FreeEdges->ChangeArray1();
557 Poly_Connect pc (PT);
558 Standard_Integer t[3];
560 Standard_Integer fr (1);
561 for (i = 1; i <= PT->NbTriangles(); i++)
563 pc.Triangles (i, t[0], t[1], t[2]);
564 triangles (i).Get (n[0], n[1], n[2]);
565 for (j = 0; j < 3; j++)
567 Standard_Integer k = (j + 1) % 3;
571 FreeE (fr + 1)= n[k];
576 for (Standard_Integer ifri = 1; ifri <= FreeE.Length(); ifri += 2)
578 gp_Pnt pe1 (Nodes (FreeE (ifri)).Transformed (iloc)), pe2 (Nodes (FreeE (ifri + 1)).Transformed (iloc));
579 aSeqFree.Append(pe1);
580 aSeqFree.Append(pe2);
583 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangle))
585 Handle(Select3D_SensitiveTriangle) Str = Handle(Select3D_SensitiveTriangle)::DownCast(Ent);
587 Str->Points3D (P1, P2, P3);
588 gp_Pnt CDG = Str->Center3D();
590 gp_XYZ V1 (P1.XYZ()); V1 -= (CDG.XYZ());
591 gp_XYZ V2 (P2.XYZ()); V2 -= (CDG.XYZ());
592 gp_XYZ V3 (P3.XYZ()); V3 -= (CDG.XYZ());
593 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
594 V1 += CDG.XYZ(); V2 += CDG.XYZ(); V3 += CDG.XYZ();
597 aSeqLines.Append(gp_Pnt(V1));
598 aSeqLines.Append(gp_Pnt(V2));
599 aSeqLines.Append(gp_Pnt(V3));
600 aSeqLines.Append(gp_Pnt(V1));
606 if (aSeqLines.Length())
608 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),aSeqBnds.Length());
609 for (i = 1; i <= aSeqLines.Length(); i++)
610 aPrims->AddVertex(aSeqLines(i));
611 for (i = 1; i <= aSeqBnds.Length(); i++)
612 aPrims->AddBound(aSeqBnds(i));
613 anAreaGroup->AddPrimitiveArray(aPrims);
616 if (aSeqFree.Length())
618 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GREEN, Aspect_TOL_SOLID, 2.0));
619 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqFree.Length(),aSeqFree.Length()/2);
620 for (i = 1; i <= aSeqFree.Length(); i++)
623 aPrims->AddVertex(aSeqLines(i++));
624 aPrims->AddVertex(aSeqLines(i));
626 aSensGroup->AddPrimitiveArray(aPrims);
627 aSensGroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
631 //=======================================================================
632 // Function: updateZLayers
634 //=======================================================================
635 void StdSelect_ViewerSelector3d::updateZLayers (const Handle(V3d_View)& theView)
637 myZLayerOrderMap.Clear();
638 TColStd_SequenceOfInteger aZLayers;
639 theView->Viewer()->GetAllZLayers (aZLayers);
640 Standard_Integer aPos = 0;
641 Standard_Boolean isPrevDepthWrite = true;
642 for (TColStd_SequenceOfInteger::Iterator aLayerIter (aZLayers); aLayerIter.More(); aLayerIter.Next())
644 Graphic3d_ZLayerSettings aSettings = theView->Viewer()->ZLayerSettings (aLayerIter.Value());
645 if (aSettings.ToClearDepth()
646 || isPrevDepthWrite != aSettings.ToEnableDepthWrite())
650 isPrevDepthWrite = aSettings.ToEnableDepthWrite();
651 myZLayerOrderMap.Bind (aLayerIter.Value(), aPos);
657 //! Abstract class for filling pixel with color.
658 class BaseFiller : public Standard_Transient
660 DEFINE_STANDARD_RTTI_INLINE(BaseFiller, Standard_Transient)
663 //! Main constructor.
664 BaseFiller (Image_PixMap& thePixMap,
665 StdSelect_ViewerSelector3d* theSelector)
666 : myImage (&thePixMap),
667 myMainSel(theSelector) {}
669 //! Fill pixel at specified position.
670 virtual void Fill (const Standard_Integer theCol,
671 const Standard_Integer theRow,
672 const Standard_Integer thePicked) = 0;
674 //! Flush results into final image.
675 virtual void Flush() {}
679 //! Find the new unique random color.
680 void randomPastelColor (Quantity_Color& theColor)
684 nextRandomPastelColor (theColor);
685 if (myUniqueColors.Add (theColor))
692 //! Fills the given color as random.
693 void nextRandomPastelColor (Quantity_Color& theColor)
695 theColor = Quantity_Color (Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
696 Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
697 Standard_Real(myBullardGenerator.NextInt() % 256) / 255.0,
702 Image_PixMap* myImage;
703 StdSelect_ViewerSelector3d* myMainSel;
704 math_BullardGenerator myBullardGenerator;
705 NCollection_Map<Quantity_Color, Quantity_ColorHasher> myUniqueColors;
708 //! Help class for filling pixel with random color.
709 class GeneratedEntityColorFiller : public BaseFiller
711 DEFINE_STANDARD_RTTI_INLINE(GeneratedEntityColorFiller, BaseFiller)
713 GeneratedEntityColorFiller (Image_PixMap& thePixMap,
714 StdSelect_ViewerSelector3d* theSelector,
715 const SelectMgr_SelectableObjectSet& theSelObjects)
716 : BaseFiller (thePixMap, theSelector)
718 // generate per-entity colors in the order as they have been activated
719 for (SelectMgr_SelectableObjectSet::Iterator anObjIter (theSelObjects); anObjIter.More(); anObjIter.Next())
721 const Handle(SelectMgr_SelectableObject)& anObj = anObjIter.Value();
722 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
724 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
725 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
727 const Handle(SelectMgr_SensitiveEntity)& aSens = aSelEntIter.Value();
728 if (!myMapEntityColors.IsBound (aSens->BaseSensitive()))
730 Quantity_Color aColor;
731 randomPastelColor (aColor);
732 myMapEntityColors.Bind (aSens->BaseSensitive(), aColor);
739 virtual void Fill (const Standard_Integer theCol,
740 const Standard_Integer theRow,
741 const Standard_Integer thePicked) Standard_OVERRIDE
744 || thePicked > myMainSel->NbPicked())
746 myImage->SetPixelColor (theCol, theRow, Quantity_Color(Quantity_NOC_BLACK));
750 const Handle(Select3D_SensitiveEntity)& aPickedEntity = myMainSel->PickedEntity (thePicked);
751 Quantity_Color aColor (Quantity_NOC_BLACK);
752 myMapEntityColors.Find (aPickedEntity, aColor);
753 myImage->SetPixelColor (theCol, theRow, aColor);
757 NCollection_DataMap<Handle(Select3D_SensitiveEntity), Quantity_Color> myMapEntityColors;
760 //! Help class for filling pixel with normalized depth of ray.
761 class NormalizedDepthFiller : public BaseFiller
763 DEFINE_STANDARD_RTTI_INLINE(NormalizedDepthFiller, BaseFiller)
765 NormalizedDepthFiller (Image_PixMap& thePixMap,
766 StdSelect_ViewerSelector3d* theSelector,
767 const Standard_Boolean theToInverse)
768 : BaseFiller (thePixMap, theSelector),
769 myDepthMin ( RealLast()),
770 myDepthMax (-RealLast()),
771 myToInverse(theToInverse)
773 myUnnormImage.InitZero (Image_Format_GrayF, thePixMap.SizeX(), thePixMap.SizeY());
776 //! Accumulate the data.
777 virtual void Fill (const Standard_Integer theCol,
778 const Standard_Integer theRow,
779 const Standard_Integer thePicked) Standard_OVERRIDE
781 if (myUnnormImage.IsEmpty())
787 || thePicked > myMainSel->NbPicked())
789 myUnnormImage.ChangeValue<float> (theRow, theCol) = ShortRealLast();
793 const SelectMgr_SortCriterion& aSortCriterion = myMainSel->PickedData (thePicked);
794 myUnnormImage.ChangeValue<float> (theRow, theCol) = float(aSortCriterion.Depth);
795 myDepthMin = Min (myDepthMin, aSortCriterion.Depth);
796 myDepthMax = Max (myDepthMax, aSortCriterion.Depth);
799 //! Normalize the depth values.
800 virtual void Flush() Standard_OVERRIDE
804 if (myDepthMin <= myDepthMax)
806 aFrom = float(myDepthMin);
807 aDelta = float(myDepthMax) - float(myDepthMin);
808 if (aDelta <= ShortRealEpsilon())
813 for (Standard_Size aRowIter = 0; aRowIter < myUnnormImage.SizeY(); ++aRowIter)
815 for (Standard_Size aColIter = 0; aColIter < myUnnormImage.SizeX(); ++aColIter)
817 float aDepth = myUnnormImage.Value<float> (aRowIter, aColIter);
818 if (aDepth <= -ShortRealLast()
819 || aDepth >= ShortRealLast())
821 myImage->SetPixelColor (Standard_Integer(aColIter), Standard_Integer(aRowIter),
822 Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 1.0f));
826 float aNormDepth = (aDepth - aFrom) / aDelta;
829 aNormDepth = 1.0f - aNormDepth;
831 myImage->SetPixelColor (Standard_Integer(aColIter), Standard_Integer(aRowIter),
832 Quantity_ColorRGBA (aNormDepth, aNormDepth, aNormDepth, 1.0f));
838 Image_PixMap myUnnormImage;
839 Standard_Real myDepthMin;
840 Standard_Real myDepthMax;
841 Standard_Boolean myToInverse;
844 //! Help class for filling pixel with unnormalized depth of ray.
845 class UnnormalizedDepthFiller : public BaseFiller
847 DEFINE_STANDARD_RTTI_INLINE(UnnormalizedDepthFiller, BaseFiller)
849 UnnormalizedDepthFiller (Image_PixMap& thePixMap,
850 StdSelect_ViewerSelector3d* theSelector)
851 : BaseFiller (thePixMap, theSelector) {}
853 virtual void Fill (const Standard_Integer theCol,
854 const Standard_Integer theRow,
855 const Standard_Integer thePicked) Standard_OVERRIDE
858 || thePicked > myMainSel->NbPicked())
860 myImage->SetPixelColor (theCol, theRow, Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 1.0f));
864 const SelectMgr_SortCriterion& aSortCriterion = myMainSel->PickedData (thePicked);
865 const float aDepth = float(aSortCriterion.Depth);
866 myImage->SetPixelColor (theCol, theRow, Quantity_ColorRGBA (Graphic3d_Vec4 (aDepth, aDepth, aDepth, 1.0f)));
870 //! Help class for filling pixel with color of detected object.
871 class GeneratedOwnerColorFiller : public BaseFiller
873 DEFINE_STANDARD_RTTI_INLINE(GeneratedOwnerColorFiller, BaseFiller)
875 GeneratedOwnerColorFiller (Image_PixMap& thePixMap,
876 StdSelect_ViewerSelector3d* theSelector,
877 const SelectMgr_SelectableObjectSet& theSelObjects)
878 : BaseFiller (thePixMap, theSelector)
880 // generate per-owner colors in the order as they have been activated
881 for (SelectMgr_SelectableObjectSet::Iterator anObjIter (theSelObjects); anObjIter.More(); anObjIter.Next())
883 const Handle(SelectMgr_SelectableObject)& anObj = anObjIter.Value();
884 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anObj->Selections()); aSelIter.More(); aSelIter.Next())
886 const Handle(SelectMgr_Selection)& aSel = aSelIter.Value();
887 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
889 const Handle(SelectMgr_SensitiveEntity)& aSens = aSelEntIter.Value();
890 const Handle(SelectBasics_EntityOwner)& anOwner = aSens->BaseSensitive()->OwnerId();
891 if (!myMapOwnerColors.IsBound (anOwner))
893 Quantity_Color aColor;
894 randomPastelColor (aColor);
895 myMapOwnerColors.Bind (anOwner, aColor);
902 virtual void Fill (const Standard_Integer theCol,
903 const Standard_Integer theRow,
904 const Standard_Integer thePicked) Standard_OVERRIDE
907 || thePicked > myMainSel->NbPicked())
909 myImage->SetPixelColor (theCol, theRow, Quantity_Color(Quantity_NOC_BLACK));
913 const Handle(SelectMgr_EntityOwner)& aPickedOwner = myMainSel->Picked (thePicked);
914 Quantity_Color aColor (Quantity_NOC_BLACK);
915 myMapOwnerColors.Find (aPickedOwner, aColor);
916 myImage->SetPixelColor (theCol, theRow, aColor);
920 NCollection_DataMap<Handle(SelectBasics_EntityOwner), Quantity_Color> myMapOwnerColors;
923 //! Help class for filling pixel with random color for each selection mode.
924 class GeneratedSelModeColorFiller : public BaseFiller
926 DEFINE_STANDARD_RTTI_INLINE(GeneratedSelModeColorFiller, BaseFiller)
928 GeneratedSelModeColorFiller (Image_PixMap& thePixMap,
929 StdSelect_ViewerSelector3d* theSelector)
930 : BaseFiller (thePixMap, theSelector)
932 // generate standard modes in proper order, consider custom objects would use similar scheme
933 myMapSelectionModeColors.Bind ( 0, Quantity_NOC_WHITE); // default (entire object selection)
934 myMapSelectionModeColors.Bind ( 1, Quantity_NOC_YELLOW); // TopAbs_VERTEX
935 myMapSelectionModeColors.Bind ( 2, Quantity_NOC_GREEN); // TopAbs_EDGE
936 myMapSelectionModeColors.Bind ( 3, Quantity_NOC_RED); // TopAbs_WIRE
937 myMapSelectionModeColors.Bind ( 4, Quantity_NOC_BLUE1); // TopAbs_FACE
938 myMapSelectionModeColors.Bind ( 5, Quantity_NOC_CYAN1); // TopAbs_SHELL
939 myMapSelectionModeColors.Bind ( 6, Quantity_NOC_PURPLE); // TopAbs_SOLID
940 myMapSelectionModeColors.Bind ( 7, Quantity_NOC_MAGENTA1); // TopAbs_COMPSOLID
941 myMapSelectionModeColors.Bind ( 8, Quantity_NOC_BROWN); // TopAbs_COMPOUND
942 myMapSelectionModeColors.Bind (0x0010, Quantity_NOC_PINK); // MeshVS_SMF_Volume
943 myMapSelectionModeColors.Bind (0x001E, Quantity_NOC_LIMEGREEN); // MeshVS_SMF_Element
944 myMapSelectionModeColors.Bind (0x001F, Quantity_NOC_DARKOLIVEGREEN); // MeshVS_SMF_All
945 myMapSelectionModeColors.Bind (0x0100, Quantity_NOC_GOLD); // MeshVS_SMF_Group
948 virtual void Fill (const Standard_Integer theCol,
949 const Standard_Integer theRow,
950 const Standard_Integer thePicked) Standard_OVERRIDE
953 || thePicked > myMainSel->NbPicked())
955 myImage->SetPixelColor (theCol, theRow, Quantity_Color (Quantity_NOC_BLACK));
959 Standard_Integer aSelectionMode = -1;
960 const Handle(SelectMgr_SelectableObject)& aSelectable = myMainSel->Picked (thePicked)->Selectable();
961 const Handle(Select3D_SensitiveEntity)& anEntity = myMainSel->PickedEntity (thePicked);
962 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (aSelectable->Selections()); aSelIter.More(); aSelIter.Next())
964 const Handle(SelectMgr_Selection)& aSelection = aSelIter.Value();
965 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
967 if (aSelEntIter.Value()->BaseSensitive() == anEntity)
969 aSelectionMode = aSelection->Mode();
974 if (aSelectionMode == -1)
976 myImage->SetPixelColor (theCol, theRow, Quantity_Color (Quantity_NOC_BLACK));
980 if (!myMapSelectionModeColors.IsBound (aSelectionMode))
982 Quantity_Color aColor;
983 randomPastelColor (aColor);
984 myMapSelectionModeColors.Bind (aSelectionMode, aColor);
987 const Quantity_Color& aColor = myMapSelectionModeColors.Find (aSelectionMode);
988 myImage->SetPixelColor (theCol, theRow, aColor);
992 NCollection_DataMap<Standard_Integer, Quantity_Color> myMapSelectionModeColors;
995 //! Help class for filling pixel with color of detected shape.
996 class DetectedObjectColorFiller : public BaseFiller
998 DEFINE_STANDARD_RTTI_INLINE(DetectedObjectColorFiller, BaseFiller)
1000 DetectedObjectColorFiller (Image_PixMap& thePixMap,
1001 StdSelect_ViewerSelector3d* theSelector)
1002 : BaseFiller (thePixMap, theSelector) {}
1004 virtual void Fill (const Standard_Integer theCol,
1005 const Standard_Integer theRow,
1006 const Standard_Integer thePicked) Standard_OVERRIDE
1008 Quantity_Color aColor (Quantity_NOC_BLACK);
1010 && thePicked <= myMainSel->NbPicked())
1012 const Handle(SelectMgr_SelectableObject)& aSelectable = myMainSel->Picked (thePicked)->Selectable();
1013 aColor = aSelectable->Attributes()->Color();
1015 myImage->SetPixelColor (theCol, theRow, aColor);
1021 //=======================================================================
1022 //function : ToPixMap
1024 //=======================================================================
1025 Standard_Boolean StdSelect_ViewerSelector3d::ToPixMap (Image_PixMap& theImage,
1026 const Handle(V3d_View)& theView,
1027 const StdSelect_TypeOfSelectionImage theType,
1028 const Standard_Integer thePickedIndex)
1030 if (theImage.IsEmpty())
1032 throw Standard_ProgramError("StdSelect_ViewerSelector3d::ToPixMap() has been called with empty image");
1035 Handle(BaseFiller) aFiller;
1038 case StdSelect_TypeOfSelectionImage_NormalizedDepth:
1039 case StdSelect_TypeOfSelectionImage_NormalizedDepthInverted:
1041 aFiller = new NormalizedDepthFiller (theImage, this,
1042 theType == StdSelect_TypeOfSelectionImage_NormalizedDepthInverted);
1045 case StdSelect_TypeOfSelectionImage_UnnormalizedDepth:
1047 aFiller = new UnnormalizedDepthFiller (theImage, this);
1050 case StdSelect_TypeOfSelectionImage_ColoredDetectedObject:
1052 aFiller = new DetectedObjectColorFiller (theImage, this);
1055 case StdSelect_TypeOfSelectionImage_ColoredEntity:
1057 aFiller = new GeneratedEntityColorFiller (theImage, this, mySelectableObjects);
1060 case StdSelect_TypeOfSelectionImage_ColoredOwner:
1062 aFiller = new GeneratedOwnerColorFiller (theImage, this, mySelectableObjects);
1065 case StdSelect_TypeOfSelectionImage_ColoredSelectionMode:
1067 aFiller = new GeneratedSelModeColorFiller (theImage, this);
1071 if (aFiller.IsNull())
1073 return Standard_False;
1076 const Standard_Integer aSizeX = static_cast<Standard_Integer> (theImage.SizeX());
1077 const Standard_Integer aSizeY = static_cast<Standard_Integer> (theImage.SizeY());
1078 for (Standard_Integer aRowIter = 0; aRowIter < aSizeY; ++aRowIter)
1080 for (Standard_Integer aColIter = 0; aColIter < aSizeX; ++aColIter)
1082 Pick (aColIter, aRowIter, theView);
1083 aFiller->Fill (aColIter, aRowIter, thePickedIndex);
1087 return Standard_True;