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.ixx>
18 #include <StdSelect.hxx>
19 #include <SelectBasics_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_SequenceOfHClipPlane.hxx>
31 #include <SelectMgr_SelectableObject.hxx>
32 #include <SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive.hxx>
33 #include <SelectBasics_ListOfBox2d.hxx>
34 #include <TColgp_HArray1OfPnt.hxx>
35 #include <TColgp_Array1OfPnt.hxx>
36 #include <TColgp_HArray1OfPnt2d.hxx>
37 #include <Select3D_SensitiveCurve.hxx>
38 #include <Select3D_SensitiveSegment.hxx>
39 #include <Select3D_SensitiveFace.hxx>
40 #include <Select3D_SensitiveCircle.hxx>
41 #include <Select3D_SensitivePoint.hxx>
42 #include <Select3D_SensitiveTriangulation.hxx>
43 #include <Select3D_SensitiveTriangle.hxx>
44 #include <Select3D_SensitiveWire.hxx>
45 #include <Select3D_SensitiveEntitySequence.hxx>
46 #include <Select3D_ListOfSensitiveTriangle.hxx>
47 #include <Select3D_SensitiveBox.hxx>
48 #include <Select3D_ListIteratorOfListOfSensitiveTriangle.hxx>
50 #include <SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation.hxx>
51 #include <Aspect_Grid.hxx>
52 #include <Aspect_TypeOfMarker.hxx>
53 #include <Graphic3d_AspectMarker3d.hxx>
54 #include <Graphic3d_ArrayOfPoints.hxx>
55 #include <SelectBasics_ListIteratorOfListOfBox2d.hxx>
56 #include <Poly_Connect.hxx>
57 #include <TColStd_HArray1OfInteger.hxx>
59 #include <Poly_Array1OfTriangle.hxx>
60 #include <Poly_Triangulation.hxx>
61 #include <OSD_Environment.hxx>
63 #include <V3d_View.hxx>
64 #include <V3d_Viewer.hxx>
65 #include <TColgp_SequenceOfPnt.hxx>
67 static Standard_Integer StdSel_NumberOfFreeEdges (const Handle(Poly_Triangulation)& Trg)
69 Standard_Integer nFree = 0;
71 Standard_Integer t[3];
72 Standard_Integer i, j;
73 for (i = 1; i <= Trg->NbTriangles(); i++)
75 pc.Triangles (i, t[0], t[1], t[2]);
76 for (j = 0; j < 3; j++)
77 if (t[j] == 0) nFree++;
82 //=======================================================================
83 // Function : Constructor
85 //=======================================================================
86 StdSelect_ViewerSelector3d::StdSelect_ViewerSelector3d()
87 : myProjector (new Select3D_Projector()),
90 myPrevOrthographic (Standard_True),
91 mySensMode (StdSelect_SM_WINDOW),
93 myToUpdateTolerance (Standard_True)
104 myPrevAxialScale[0] = 0.0;
105 myPrevAxialScale[1] = 0.0;
106 myPrevAxialScale[2] = 0.0;
109 //=======================================================================
110 // Function : Constructor
112 //=======================================================================
113 StdSelect_ViewerSelector3d::StdSelect_ViewerSelector3d (const Handle(Select3D_Projector)& theProj)
114 : myProjector (theProj),
117 myPrevOrthographic (Standard_True),
118 mySensMode (StdSelect_SM_WINDOW),
119 myPixelTolerance (2),
120 myToUpdateTolerance (Standard_True)
131 myPrevAxialScale[0] = 0.0;
132 myPrevAxialScale[1] = 0.0;
133 myPrevAxialScale[2] = 0.0;
136 //=======================================================================
139 //=======================================================================
140 void StdSelect_ViewerSelector3d::Convert (const Handle(SelectMgr_Selection)& theSel)
142 for (theSel->Init(); theSel->More(); theSel->Next())
144 if (theSel->Sensitive()->NeedsConversion())
146 Handle(Select3D_SensitiveEntity) aSE = *((Handle(Select3D_SensitiveEntity)*) &(theSel->Sensitive()));
147 aSE->Project (myProjector);
150 tosort = Standard_True;
156 //=======================================================================
159 //=======================================================================
160 void StdSelect_ViewerSelector3d::Set (const Handle(Select3D_Projector)& theProj)
162 myProjector = theProj;
163 toupdate = Standard_True;
166 //=======================================================================
167 // Function: SetSensitivityMode
169 //=======================================================================
170 void StdSelect_ViewerSelector3d::SetSensitivityMode (const StdSelect_SensitivityMode theMode)
172 mySensMode = theMode;
173 toupdate = Standard_True;
176 //=======================================================================
177 // Function: SetPixelTolerance
179 //=======================================================================
180 void StdSelect_ViewerSelector3d::SetPixelTolerance (const Standard_Integer theTolerance)
182 if (myPixelTolerance != theTolerance)
184 myPixelTolerance = theTolerance;
185 myToUpdateTolerance = Standard_True;
189 //=======================================================================
192 //=======================================================================
193 void StdSelect_ViewerSelector3d::Pick (const Standard_Integer theXPix,
194 const Standard_Integer theYPix,
195 const Handle(V3d_View)& theView)
197 SetClipping (theView->GetClipPlanes());
198 UpdateProj (theView);
199 Standard_Real aPnt3d[3];
200 theView->Convert (theXPix, theYPix,
201 aPnt3d[0], aPnt3d[1], aPnt3d[2]);
204 myProjector->Project (gp_Pnt (aPnt3d[0], aPnt3d[1], aPnt3d[2]), aPnt2d);
206 InitSelect (aPnt2d.X(), aPnt2d.Y());
209 //=======================================================================
212 //=======================================================================
213 void StdSelect_ViewerSelector3d::Pick (const Standard_Integer theXPMin,
214 const Standard_Integer theYPMin,
215 const Standard_Integer theXPMax,
216 const Standard_Integer theYPMax,
217 const Handle(V3d_View)& theView)
219 if (myToUpdateTolerance && SensitivityMode() == StdSelect_SM_WINDOW)
221 SetSensitivity (theView->Convert (myPixelTolerance));
222 myToUpdateTolerance = Standard_False;
225 UpdateProj (theView);
227 Standard_Real aX1 = 0.0;
228 Standard_Real aY1 = 0.0;
229 Standard_Real aZ1 = 0.0;
230 Standard_Real aX2 = 0.0;
231 Standard_Real aY2 = 0.0;
232 Standard_Real aZ2 = 0.0;
236 theView->Convert (theXPMin, theYPMin, aX1, aY1, aZ1);
237 theView->Convert (theXPMax, theYPMax, aX2, aY2, aZ2);
238 myProjector->Project (gp_Pnt (aX1, aY1, aZ1), aP2d1);
239 myProjector->Project (gp_Pnt (aX2, aY2, aZ2), aP2d2);
241 InitSelect (Min (aP2d1.X(), aP2d2.X()),
242 Min (aP2d1.Y(), aP2d2.Y()),
243 Max (aP2d1.X(), aP2d2.X()),
244 Max (aP2d1.Y(), aP2d2.Y()));
247 //=======================================================================
249 // Purpose : Selection using a polyline
250 //=======================================================================
251 void StdSelect_ViewerSelector3d::Pick (const TColgp_Array1OfPnt2d& thePolyline,
252 const Handle(V3d_View)& theView)
254 if (myToUpdateTolerance && SensitivityMode() == StdSelect_SM_WINDOW)
256 SetSensitivity (theView->Convert (myPixelTolerance));
257 myToUpdateTolerance = Standard_False;
260 UpdateProj (theView);
262 Standard_Integer aNbPix = thePolyline.Length();
265 Handle(TColgp_HArray1OfPnt2d) aP2d = new TColgp_HArray1OfPnt2d (1, aNbPix);
267 for (Standard_Integer aPntIt = 1; aPntIt <= aNbPix; ++aPntIt)
269 Standard_Integer aXP = (Standard_Integer)(thePolyline (aPntIt).X());
270 Standard_Integer aYP = (Standard_Integer)(thePolyline (aPntIt).Y());
272 Standard_Real aX = 0.0;
273 Standard_Real aY = 0.0;
274 Standard_Real aZ = 0.0;
277 theView->Convert (aXP, aYP, aX, aY, aZ);
278 myProjector->Project (gp_Pnt (aX, aY, aZ), aPnt2d);
280 aP2d->SetValue (aPntIt, aPnt2d);
283 const TColgp_Array1OfPnt2d& aPolyConvert = aP2d->Array1();
285 InitSelect (aPolyConvert);
288 //=======================================================================
289 // Function: DisplayAreas
290 // Purpose : display the activated areas...
291 //=======================================================================
292 void StdSelect_ViewerSelector3d::DisplayAreas (const Handle(V3d_View)& theView)
294 if (myToUpdateTolerance && SensitivityMode() == StdSelect_SM_WINDOW)
296 SetSensitivity (theView->Convert (myPixelTolerance));
297 myToUpdateTolerance = Standard_False;
300 UpdateProj (theView);
301 UpdateSort(); // Updates the activated areas
303 if (mystruct.IsNull())
305 mystruct = new Graphic3d_Structure (theView->Viewer()->Viewer());
308 if (myareagroup.IsNull())
310 myareagroup = mystruct->NewGroup();
313 SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive anIt (myentities);
314 Handle(Select3D_Projector) aProjector = StdSelect::GetProjector (theView);
315 aProjector->SetView (theView);
317 Standard_Real aXmin = 0.0;
318 Standard_Real aYmin = 0.0;
319 Standard_Real aXmax = 0.0;
320 Standard_Real aYmax = 0.0;
322 SelectBasics_ListOfBox2d aBoxList;
324 TColgp_SequenceOfPnt aSeqLines;
325 for (; anIt.More(); anIt.Next())
327 anIt.Value()->Areas (aBoxList);
329 for (SelectBasics_ListIteratorOfListOfBox2d aBoxIt (aBoxList); aBoxIt.More(); aBoxIt.Next())
331 aBoxIt.Value().Get (aXmin, aYmin, aXmax, aYmax);
333 aPbid.SetCoord (aXmin - mytolerance, aYmin - mytolerance, 0.0);
334 aProjector->Transform (aPbid, aProjector->InvertedTransformation());
335 aSeqLines.Append (aPbid);
337 aPbid.SetCoord (aXmax + mytolerance, aYmin - mytolerance, 0.0);
338 aProjector->Transform (aPbid, aProjector->InvertedTransformation());
339 aSeqLines.Append (aPbid);
341 aPbid.SetCoord (aXmax + mytolerance, aYmax + mytolerance, 0.0);
342 aProjector->Transform (aPbid, aProjector->InvertedTransformation());
343 aSeqLines.Append (aPbid);
345 aPbid.SetCoord (aXmin - mytolerance, aYmax + mytolerance, 0.0);
346 aProjector->Transform (aPbid, aProjector->InvertedTransformation());
347 aSeqLines.Append (aPbid);
351 if (aSeqLines.Length())
353 Standard_Integer aN = 0;
354 Standard_Integer aNp = 0;
355 const Standard_Integer aNbl = aSeqLines.Length() / 4;
357 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines (5 * aNbl, aNbl);
358 for (aNp = 1, aN = 0; aN < aNbl; aN++)
360 aPrims->AddBound (5);
361 const gp_Pnt &aPnt1 = aSeqLines (aNp++);
362 aPrims->AddVertex (aPnt1);
363 aPrims->AddVertex (aSeqLines (aNp++));
364 aPrims->AddVertex (aSeqLines (aNp++));
365 aPrims->AddVertex (aSeqLines (aNp++));
366 aPrims->AddVertex (aPnt1);
368 myareagroup->AddPrimitiveArray (aPrims);
371 myareagroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_AQUAMARINE1, Aspect_TOL_DASH, 1.0));
372 myareagroup->Structure()->SetDisplayPriority (10);
373 myareagroup->Structure()->Display();
378 //=======================================================================
379 // Function: ClearAreas
381 //=======================================================================
382 void StdSelect_ViewerSelector3d::ClearAreas (const Handle(V3d_View)& theView)
384 if (myareagroup.IsNull())
389 myareagroup->Clear();
391 if (!theView.IsNull())
397 //=======================================================================
398 // Function: UpdateProj
400 //=======================================================================
401 Standard_Boolean StdSelect_ViewerSelector3d::UpdateProj (const Handle(V3d_View)& theView)
403 // Check common properties of camera
404 Standard_Real anUp[3];
405 Standard_Real aProj[3];
406 Standard_Real anAxialScale[3];
407 theView->Up (anUp[0], anUp[1], anUp[2]);
408 theView->Proj (aProj[0], aProj[1], aProj[2]);
409 theView->AxialScale (anAxialScale[0], anAxialScale[1], anAxialScale[2]);
411 Standard_Boolean isOrthographic = theView->Type() == V3d_ORTHOGRAPHIC;
412 Standard_Boolean toUpdateProjector = myPrevOrthographic != isOrthographic
413 || myPrevUp[0] != anUp[0]
414 || myPrevUp[1] != anUp[1]
415 || myPrevUp[2] != anUp[2]
416 || myPrevProj[0] != aProj[0]
417 || myPrevProj[1] != aProj[1]
418 || myPrevProj[2] != aProj[2]
419 || myPrevAxialScale[0] != anAxialScale[0]
420 || myPrevAxialScale[1] != anAxialScale[1]
421 || myPrevAxialScale[2] != anAxialScale[2];
423 // Check properties of perspective camera
424 Standard_Real anAt[3];
425 Standard_Real aScale = theView->Scale();
426 Standard_Real aFOV = theView->Camera()->FOVy();
427 theView->At (anAt[0], anAt[1], anAt[2]);
428 if (!isOrthographic && !toUpdateProjector)
430 toUpdateProjector = myPrevAt[0] != anAt[0]
431 || myPrevAt[1] != anAt[1]
432 || myPrevAt[2] != anAt[2]
433 || myPrevScale != aScale
434 || myPrevFOV != aFOV;
437 myToUpdateTolerance = aScale != myPrevScale;
439 // Update projector if anything changed
440 if (toUpdateProjector)
442 toupdate = Standard_True;
444 myToUpdateTolerance = Standard_True;
448 // For orthographic view use only direction of projection and up vector
449 // Panning, and zooming has no effect on 2D selection sensitives.
450 Handle (Graphic3d_Camera) aCamera = new Graphic3d_Camera();
452 aCamera->SetProjectionType (Graphic3d_Camera::Projection_Orthographic);
453 aCamera->SetCenter (gp::Origin());
454 aCamera->SetDirection (gp_Dir (-aProj[0], -aProj[1], -aProj[2]));
455 aCamera->SetUp (gp_Dir (anUp[0], anUp[1], anUp[2]));
456 aCamera->SetDistance (1.0);
457 aCamera->SetAxialScale (gp_XYZ (anAxialScale[0], anAxialScale[1], anAxialScale[2]));
459 myProjector = new Select3D_Projector (aCamera->OrientationMatrix(), Graphic3d_Mat4d());
463 // For perspective projection panning, zooming and location of view
464 // has effect. Thus, use current view and projection matrices from
465 // view camera. Exception is that the projection transformation
466 // is scaled from NDC to size of displaying frame of view space in order
467 // to maintain consistence with pixel tolerance conversion.
468 const Graphic3d_Mat4d& aMVMatrix = theView->Camera()->OrientationMatrix();
469 const Graphic3d_Mat4d& aProjMatrix = theView->Camera()->ProjectionMatrix();
470 gp_XYZ aViewDimensions = theView->Camera()->ViewDimensions();
472 Graphic3d_Mat4d aScaledProj;
473 aScaledProj.ChangeValue (0, 0) = aViewDimensions.X();
474 aScaledProj.ChangeValue (1, 1) = aViewDimensions.Y();
475 aScaledProj.ChangeValue (2, 2) = aViewDimensions.Z();
476 Graphic3d_Mat4d aScaledProjMatrix = aScaledProj * aProjMatrix;
478 Standard_Real aZNear = theView->Camera()->ZNear();
479 Standard_Real aZFar = theView->Camera()->ZFar();
481 myProjector = new Select3D_Projector (aMVMatrix, aScaledProjMatrix, aZNear, aZFar);
485 myPrevAt[0] = anAt[0];
486 myPrevAt[1] = anAt[1];
487 myPrevAt[2] = anAt[2];
488 myPrevUp[0] = anUp[0];
489 myPrevUp[1] = anUp[1];
490 myPrevUp[2] = anUp[2];
491 myPrevProj[0] = aProj[0];
492 myPrevProj[1] = aProj[1];
493 myPrevProj[2] = aProj[2];
494 myPrevAxialScale[0] = anAxialScale[0];
495 myPrevAxialScale[1] = anAxialScale[1];
496 myPrevAxialScale[2] = anAxialScale[2];
498 myPrevScale = aScale;
499 myPrevOrthographic = isOrthographic;
501 if (myToUpdateTolerance && SensitivityMode() == StdSelect_SM_WINDOW)
503 SetSensitivity (theView->Convert (myPixelTolerance));
504 myToUpdateTolerance = Standard_False;
517 return Standard_True;
521 //=======================================================================
522 // Function: DisplaySensitive.
523 // Purpose : Display active primitives.
524 //=======================================================================
525 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(V3d_View)& theView)
527 if (myToUpdateTolerance && SensitivityMode() == StdSelect_SM_WINDOW)
529 SetSensitivity (theView->Convert (myPixelTolerance));
530 myToUpdateTolerance = Standard_False;
535 UpdateProj (theView);
540 UpdateSort(); // Updates the activated areas
543 // Preparation des structures
544 if (mystruct.IsNull())
546 mystruct = new Graphic3d_Structure (theView->Viewer()->Viewer());
549 if (mysensgroup.IsNull())
551 mysensgroup = mystruct->NewGroup();
554 Quantity_Color aColor (Quantity_NOC_INDIANRED3);
555 Handle(Graphic3d_AspectMarker3d) aMarkerAspect =
556 new Graphic3d_AspectMarker3d (Aspect_TOM_O_PLUS, aColor, 2.0);
558 mysensgroup->SetPrimitivesAspect (aMarkerAspect);
559 mysensgroup->SetPrimitivesAspect (
560 new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
562 SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation anIt (myselections);
564 for (; anIt.More(); anIt.Next())
568 const Handle(SelectMgr_Selection)& aSel = anIt.Key();
569 ComputeSensitivePrs (aSel);
573 mysensgroup->Structure()->SetDisplayPriority (10);
579 //=======================================================================
580 // Function: ClearSensitive
582 //=======================================================================
583 void StdSelect_ViewerSelector3d::ClearSensitive (const Handle(V3d_View)& theView)
585 if (mysensgroup.IsNull())
590 mysensgroup->Clear();
592 if (theView.IsNull())
600 //=======================================================================
601 //function : DisplaySenstive
603 //=======================================================================
604 void StdSelect_ViewerSelector3d::DisplaySensitive (const Handle(SelectMgr_Selection)& theSel,
605 const Handle(V3d_View)& theView,
606 const Standard_Boolean theToClearOthers)
608 if (mystruct.IsNull())
610 mystruct = new Graphic3d_Structure (theView->Viewer()->Viewer());
613 if (mysensgroup.IsNull())
615 mysensgroup = mystruct->NewGroup();
616 Quantity_Color aColor (Quantity_NOC_INDIANRED3);
617 Handle(Graphic3d_AspectMarker3d) aMarkerAspect =
618 new Graphic3d_AspectMarker3d (Aspect_TOM_O_PLUS, aColor, 2.0);
620 mysensgroup-> SetPrimitivesAspect (aMarkerAspect);
621 mysensgroup->SetPrimitivesAspect (
622 new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
625 if (theToClearOthers)
627 mysensgroup->Clear();
630 ComputeSensitivePrs (theSel);
632 mystruct->SetDisplayPriority (10);
638 //=======================================================================
639 //function : DisplayAreas
641 //=======================================================================
642 void StdSelect_ViewerSelector3d::DisplayAreas (const Handle(SelectMgr_Selection)& theSel,
643 const Handle(V3d_View)& theView,
644 const Standard_Boolean theToClearOthers)
646 if (mystruct.IsNull())
648 mystruct = new Graphic3d_Structure (theView->Viewer()->Viewer());
651 if (mysensgroup.IsNull())
653 myareagroup = mystruct->NewGroup();
654 myareagroup->SetGroupPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_AQUAMARINE1, Aspect_TOL_DASH, 1.0));
657 if (theToClearOthers)
659 myareagroup->Clear();
662 ComputeAreasPrs (theSel);
664 mystruct->SetDisplayPriority (10);
670 //=======================================================================
671 //function : ComputeSensitivePrs
673 //=======================================================================
674 void StdSelect_ViewerSelector3d::ComputeSensitivePrs (const Handle(SelectMgr_Selection)& theSel)
676 TColgp_SequenceOfPnt aSeqLines, aSeqFree;
677 TColStd_SequenceOfInteger aSeqBnds;
679 for (theSel->Init(); theSel->More(); theSel->Next())
681 Handle(Select3D_SensitiveEntity) Ent = Handle(Select3D_SensitiveEntity)::DownCast(theSel->Sensitive());
682 const Standard_Boolean hasloc = (Ent.IsNull()? Standard_False : Ent->HasLocation());
684 TopLoc_Location theloc;
686 theloc = Ent->Location();
692 if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveBox))
694 const Bnd_Box& B = Handle(Select3D_SensitiveBox)::DownCast (Ent)->Box();
695 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
696 B.Get (xmin, ymin, zmin, xmax, ymax, zmax);
698 gp_Pnt theboxpoint[8] =
700 gp_Pnt(xmin,ymin,zmin),
701 gp_Pnt(xmax,ymin,zmin),
702 gp_Pnt(xmax,ymax,zmin),
703 gp_Pnt(xmin,ymax,zmin),
704 gp_Pnt(xmin,ymin,zmax),
705 gp_Pnt(xmax,ymin,zmax),
706 gp_Pnt(xmax,ymax,zmax),
707 gp_Pnt(xmin,ymax,zmax)
711 for (i = 0; i <= 7; i++)
712 theboxpoint[i].Transform (theloc.Transformation());
716 for (i = 0; i < 4; i++)
717 aSeqLines.Append(theboxpoint[i]);
718 aSeqLines.Append(theboxpoint[0]);
721 for (i = 4; i < 8; i++)
722 aSeqLines.Append(theboxpoint[i]);
723 aSeqLines.Append(theboxpoint[4]);
725 for (i = 0; i < 4; i++)
728 aSeqLines.Append(theboxpoint[i]);
729 aSeqLines.Append(theboxpoint[i+4]);
735 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveFace))
737 Handle(Select3D_SensitiveFace) aFace = Handle(Select3D_SensitiveFace)::DownCast(Ent);
738 Handle(TColgp_HArray1OfPnt) TheHPts;
739 aFace->Points3D(TheHPts);
740 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
742 aSeqBnds.Append(ThePts.Length());
743 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
746 aSeqLines.Append(ThePts(I).Transformed (theloc.Transformation()));
748 aSeqLines.Append(ThePts(I));
754 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
756 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(Ent);
757 Handle(TColgp_HArray1OfPnt) TheHPts;
758 aCurve->Points3D(TheHPts);
759 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
761 aSeqBnds.Append(ThePts.Length());
762 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
765 aSeqLines.Append(ThePts(I).Transformed (theloc.Transformation()));
767 aSeqLines.Append(ThePts(I));
773 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveWire))
775 Handle(Select3D_SensitiveWire) aWire = Handle(Select3D_SensitiveWire)::DownCast(Ent);
776 Select3D_SensitiveEntitySequence EntitySeq;
777 aWire->GetEdges (EntitySeq);
779 for (int i = 1; i <= EntitySeq.Length(); i++)
781 Handle(Select3D_SensitiveEntity) SubEnt = Handle(Select3D_SensitiveEntity)::DownCast(EntitySeq.Value(i));
784 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
786 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->StartPoint().XYZ());
787 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(SubEnt)->EndPoint().XYZ());
790 P1.Transform(theloc.Transformation());
791 P2.Transform(theloc.Transformation());
794 aSeqLines.Append(P1);
795 aSeqLines.Append(P2);
799 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
801 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(SubEnt);
802 Standard_Integer aFrom, aTo;
803 aCircle->ArrayBounds (aFrom, aTo);
805 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
809 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
810 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
811 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
816 aPnts[0].Transform (theloc.Transformation());
817 aPnts[1].Transform (theloc.Transformation());
818 aPnts[2].Transform (theloc.Transformation());
822 aSeqLines.Append (aPnts[0]);
823 aSeqLines.Append (aPnts[1]);
824 aSeqLines.Append (aPnts[2]);
825 aSeqLines.Append (aPnts[0]);
830 if (SubEnt->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCurve))
832 Handle(Select3D_SensitiveCurve) aCurve = Handle(Select3D_SensitiveCurve)::DownCast(SubEnt);
833 Handle(TColgp_HArray1OfPnt) TheHPts;
834 aCurve->Points3D (TheHPts);
835 const TColgp_Array1OfPnt& ThePts = TheHPts->Array1();
837 aSeqBnds.Append(ThePts.Length());
838 for (Standard_Integer I = ThePts.Lower(); I <= ThePts.Upper(); I++)
841 aSeqLines.Append(ThePts(I).Transformed (theloc.Transformation()));
843 aSeqLines.Append(ThePts(I));
851 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveSegment))
853 gp_Pnt P1 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->StartPoint().XYZ());
854 gp_Pnt P2 (Handle(Select3D_SensitiveSegment)::DownCast(Ent)->EndPoint().XYZ());
857 P1.Transform (theloc.Transformation());
858 P2.Transform (theloc.Transformation());
861 aSeqLines.Append(P1);
862 aSeqLines.Append(P2);
867 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveCircle))
869 Handle(Select3D_SensitiveCircle) aCircle = Handle(Select3D_SensitiveCircle)::DownCast(Ent);
870 Standard_Integer aFrom, aTo;
871 aCircle->ArrayBounds (aFrom, aTo);
873 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; aPntIter += 2)
877 gp_Pnt (aCircle->GetPoint3d (aPntIter + 0).XYZ()),
878 gp_Pnt (aCircle->GetPoint3d (aPntIter + 1).XYZ()),
879 gp_Pnt (aCircle->GetPoint3d (aPntIter + 2).XYZ())
884 aPnts[0].Transform (theloc.Transformation());
885 aPnts[1].Transform (theloc.Transformation());
886 aPnts[2].Transform (theloc.Transformation());
890 aSeqLines.Append (aPnts[0]);
891 aSeqLines.Append (aPnts[1]);
892 aSeqLines.Append (aPnts[2]);
893 aSeqLines.Append (aPnts[0]);
899 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitivePoint))
902 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point() :
903 Handle(Select3D_SensitivePoint)::DownCast(Ent)->Point().Transformed (theloc.Transformation());
904 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
905 anArrayOfPoints->AddVertex (P.X(), P.Y(), P.Z());
906 mysensgroup->AddPrimitiveArray (anArrayOfPoints);
908 //============================================================
909 // Triangulation : On met un petit offset ves l'interieur...
910 //==========================================================
911 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangulation))
913 const Handle(Poly_Triangulation)& PT =
914 (*((Handle(Select3D_SensitiveTriangulation)*) &Ent))->Triangulation();
916 const Poly_Array1OfTriangle& triangles = PT->Triangles();
917 const TColgp_Array1OfPnt& Nodes = PT->Nodes();
918 Standard_Integer n[3];
920 TopLoc_Location iloc, bidloc;
921 if ((*((Handle(Select3D_SensitiveTriangulation)*) &Ent))->HasInitLocation())
922 bidloc = (*((Handle(Select3D_SensitiveTriangulation)*) &Ent))->GetInitLocation();
924 if (bidloc.IsIdentity())
927 iloc = theloc * bidloc;
930 for (i = 1; i <= PT->NbTriangles(); i++)
932 triangles (i).Get (n[0], n[1], n[2]);
933 gp_Pnt P1 (Nodes (n[0]).Transformed (iloc));
934 gp_Pnt P2 (Nodes (n[1]).Transformed (iloc));
935 gp_Pnt P3 (Nodes (n[2]).Transformed (iloc));
936 gp_XYZ V1 (P1.XYZ());
937 gp_XYZ V2 (P2.XYZ());
938 gp_XYZ V3 (P3.XYZ());
939 gp_XYZ CDG (P1.XYZ()); CDG += (P2.XYZ()); CDG += (P3.XYZ()); CDG /= 3.0;
940 V1 -= CDG; V2 -= CDG; V3 -= CDG;
941 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
942 V1 += CDG; V2 += CDG; V3 += CDG;
945 aSeqLines.Append(gp_Pnt(V1));
946 aSeqLines.Append(gp_Pnt(V2));
947 aSeqLines.Append(gp_Pnt(V3));
948 aSeqLines.Append(gp_Pnt(V1));
951 // recherche des bords libres...
953 Handle(TColStd_HArray1OfInteger) FreeEdges = new TColStd_HArray1OfInteger (1, 2 * StdSel_NumberOfFreeEdges (PT));
954 TColStd_Array1OfInteger& FreeE = FreeEdges->ChangeArray1();
955 Poly_Connect pc (PT);
956 Standard_Integer t[3];
958 Standard_Integer fr (1);
959 for (i = 1; i <= PT->NbTriangles(); i++)
961 pc.Triangles (i, t[0], t[1], t[2]);
962 triangles (i).Get (n[0], n[1], n[2]);
963 for (j = 0; j < 3; j++)
965 Standard_Integer k = (j + 1) % 3;
969 FreeE (fr + 1)= n[k];
974 for (Standard_Integer ifri = 1; ifri <= FreeE.Length(); ifri += 2)
976 gp_Pnt pe1 (Nodes (FreeE (ifri)).Transformed (iloc)), pe2 (Nodes (FreeE (ifri + 1)).Transformed (iloc));
977 aSeqFree.Append(pe1);
978 aSeqFree.Append(pe2);
981 else if (Ent->DynamicType()==STANDARD_TYPE(Select3D_SensitiveTriangle))
983 Handle(Select3D_SensitiveTriangle) Str = Handle(Select3D_SensitiveTriangle)::DownCast(Ent);
985 Str->Points3D (P1, P2, P3);
986 gp_Pnt CDG = Str->Center3D();
988 gp_XYZ V1 (P1.XYZ()); V1 -= (CDG.XYZ());
989 gp_XYZ V2 (P2.XYZ()); V2 -= (CDG.XYZ());
990 gp_XYZ V3 (P3.XYZ()); V3 -= (CDG.XYZ());
991 V1 *= 0.9; V2 *= 0.9; V3 *= 0.9;
992 V1 += CDG.XYZ(); V2 += CDG.XYZ(); V3 += CDG.XYZ();
995 aSeqLines.Append(gp_Pnt(V1));
996 aSeqLines.Append(gp_Pnt(V2));
997 aSeqLines.Append(gp_Pnt(V3));
998 aSeqLines.Append(gp_Pnt(V1));
1004 if (aSeqLines.Length())
1006 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqLines.Length(),aSeqBnds.Length());
1007 for (i = 1; i <= aSeqLines.Length(); i++)
1008 aPrims->AddVertex(aSeqLines(i));
1009 for (i = 1; i <= aSeqBnds.Length(); i++)
1010 aPrims->AddBound(aSeqBnds(i));
1011 myareagroup->AddPrimitiveArray(aPrims);
1014 if (aSeqFree.Length())
1016 mysensgroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GREEN, Aspect_TOL_SOLID, 2.0));
1017 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(aSeqFree.Length(),aSeqFree.Length()/2);
1018 for (i = 1; i <= aSeqFree.Length(); i++)
1020 aPrims->AddBound(2);
1021 aPrims->AddVertex(aSeqLines(i++));
1022 aPrims->AddVertex(aSeqLines(i));
1024 mysensgroup->AddPrimitiveArray(aPrims);
1025 mysensgroup->SetPrimitivesAspect (new Graphic3d_AspectLine3d (Quantity_NOC_GRAY40, Aspect_TOL_SOLID, 2.0));
1029 //=======================================================================
1030 //function : ComputeAreaPrs
1032 //=======================================================================
1033 void StdSelect_ViewerSelector3d::ComputeAreasPrs (const Handle(SelectMgr_Selection)& theSel)
1035 Standard_Real aXmin = 0.0;
1036 Standard_Real aYmin = 0.0;
1037 Standard_Real aXmax = 0.0;
1038 Standard_Real aYmax = 0.0;
1041 SelectBasics_ListOfBox2d aBoxList;
1043 TColgp_SequenceOfPnt aSeqLines;
1044 for (theSel->Init(); theSel->More(); theSel->Next())
1046 theSel->Sensitive()->Areas (aBoxList);
1047 for (SelectBasics_ListIteratorOfListOfBox2d aBoxIt (aBoxList); aBoxIt.More(); aBoxIt.Next())
1049 aBoxIt.Value().Get (aXmin, aYmin, aXmax, aYmax);
1051 aPbid.SetCoord (aXmin - mytolerance, aYmin - mytolerance, 0.0);
1052 myProjector->Transform (aPbid, myProjector->InvertedTransformation());
1053 aSeqLines.Append (aPbid);
1055 aPbid.SetCoord (aXmax + mytolerance, aYmin - mytolerance, 0.0);
1056 myProjector->Transform (aPbid, myProjector->InvertedTransformation());
1057 aSeqLines.Append (aPbid);
1059 aPbid.SetCoord (aXmax + mytolerance, aYmax + mytolerance, 0.0);
1060 myProjector->Transform (aPbid, myProjector->InvertedTransformation());
1061 aSeqLines.Append (aPbid);
1063 aPbid.SetCoord (aXmin - mytolerance, aYmax + mytolerance, 0.0);
1064 myProjector->Transform (aPbid, myProjector->InvertedTransformation());
1065 aSeqLines.Append (aPbid);
1069 if (aSeqLines.Length())
1071 Standard_Integer aN = 0;
1072 Standard_Integer aNP = 0;
1073 const Standard_Integer aNBL = aSeqLines.Length() / 4;
1074 Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines (5 * aNBL, aNBL);
1075 for (aNP = 1, aN = 0; aN < aNBL; aN++)
1077 aPrims->AddBound (5);
1078 const gp_Pnt &aP1 = aSeqLines (aNP++);
1079 aPrims->AddVertex (aP1);
1080 aPrims->AddVertex (aSeqLines (aNP++));
1081 aPrims->AddVertex (aSeqLines (aNP++));
1082 aPrims->AddVertex (aSeqLines (aNP++));
1083 aPrims->AddVertex (aP1);
1085 myareagroup->AddPrimitiveArray (aPrims);
1089 //=======================================================================
1090 //function : SetClipping
1092 //=======================================================================
1093 void StdSelect_ViewerSelector3d::SetClipping (const Graphic3d_SequenceOfHClipPlane& thePlanes)
1095 myClipPlanes = thePlanes;
1098 //=======================================================================
1099 //function : ComputeClipRange
1101 //=======================================================================
1102 void StdSelect_ViewerSelector3d::ComputeClipRange (const Graphic3d_SequenceOfHClipPlane& thePlanes,
1103 const gp_Lin& thePickLine,
1104 Standard_Real& theDepthMin,
1105 Standard_Real& theDepthMax) const
1107 theDepthMin = RealFirst();
1108 theDepthMax = RealLast();
1109 Standard_Real aPlaneA, aPlaneB, aPlaneC, aPlaneD;
1111 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
1112 for (; aPlaneIt.More(); aPlaneIt.Next())
1114 const Handle(Graphic3d_ClipPlane)& aClipPlane = aPlaneIt.Value();
1115 if (!aClipPlane->IsOn())
1118 gp_Pln aGeomPlane = aClipPlane->ToPlane();
1120 aGeomPlane.Coefficients (aPlaneA, aPlaneB, aPlaneC, aPlaneD);
1122 const gp_Dir& aPlaneDir = aGeomPlane.Axis().Direction();
1123 const gp_Dir& aPickDir = thePickLine.Direction();
1124 const gp_XYZ& aPntOnLine = thePickLine.Location().XYZ();
1125 const gp_XYZ& aPlaneDirXYZ = aPlaneDir.XYZ();
1127 Standard_Real aDotProduct = aPickDir.Dot (aPlaneDir);
1128 Standard_Real aDistance = -(aPntOnLine.Dot (aPlaneDirXYZ) + aPlaneD);
1130 // check whether the pick line is parallel to clip plane
1131 if (Abs (aDotProduct) < Precision::Angular())
1133 if (aDistance > 0.0)
1135 // line lies above the plane, thus no selection is possible
1141 // line lies below the plane and is not clipped, skip
1145 // compute distance to point of pick line intersection with the plane
1146 Standard_Real aIntDist = aDistance / aDotProduct;
1148 // change depth limits for case of opposite and directed planes
1149 if (aDotProduct < 0.0)
1151 theDepthMax = Min (aIntDist, theDepthMax);
1153 else if (aIntDist > theDepthMin)
1155 theDepthMin = Max (aIntDist, theDepthMin);
1160 //=======================================================================
1161 //function : PickingLine
1163 //=======================================================================
1164 gp_Lin StdSelect_ViewerSelector3d::PickingLine(const Standard_Real theX, const Standard_Real theY) const
1166 return myProjector->Shoot (theX, theY);
1169 //=======================================================================
1170 //function : DepthClipping
1172 //=======================================================================
1173 void StdSelect_ViewerSelector3d::DepthClipping (const Standard_Real theX,
1174 const Standard_Real theY,
1175 Standard_Real& theDepthMin,
1176 Standard_Real& theDepthMax) const
1178 return ComputeClipRange (myClipPlanes, PickingLine (theX, theY), theDepthMin, theDepthMax);
1181 //=======================================================================
1182 //function : DepthClipping
1184 //=======================================================================
1185 void StdSelect_ViewerSelector3d::DepthClipping (const Standard_Real theX,
1186 const Standard_Real theY,
1187 const Handle(SelectMgr_EntityOwner)& theOwner,
1188 Standard_Real& theDepthMin,
1189 Standard_Real& theDepthMax) const
1191 return ComputeClipRange (theOwner->Selectable()->GetClipPlanes(),
1192 PickingLine (theX, theY),
1193 theDepthMin, theDepthMax);
1196 //=======================================================================
1197 //function : HasDepthClipping
1199 //=======================================================================
1200 Standard_Boolean StdSelect_ViewerSelector3d::HasDepthClipping (const Handle(SelectMgr_EntityOwner)& theOwner) const
1202 if (!theOwner->HasSelectable())
1204 return Standard_False;
1207 const Handle(SelectMgr_SelectableObject)& aSelectable = theOwner->Selectable();
1208 return (aSelectable->GetClipPlanes().Size() > 0);