9e6dfb7a8479ddc546b18027e284059853007f2a
[occt.git] / src / Select3D / Select3D_SensitiveTriangle.cxx
1 // Created on: 1997-05-14
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Select3D_SensitiveTriangle.hxx>
18
19 #include <gp_Pnt.hxx>
20 #include <Precision.hxx>
21 #include <Bnd_Box.hxx>
22 #include <TopLoc_Location.hxx>
23
24
25 //==================================================
26 // Function: Creation
27 // Purpose :
28 //==================================================
29 Select3D_SensitiveTriangle::Select3D_SensitiveTriangle (const Handle(SelectBasics_EntityOwner)& theOwnerId,
30                                                         const gp_Pnt& thePnt0,
31                                                         const gp_Pnt& thePnt1,
32                                                         const gp_Pnt& thePnt2,
33                                                         const Select3D_TypeOfSensitivity theType)
34 : Select3D_SensitiveEntity (theOwnerId),
35   mySensType (theType)
36 {
37   myPoints[0] = thePnt0;
38   myPoints[1] = thePnt1;
39   myPoints[2] = thePnt2;
40   myCentroid = (thePnt0.XYZ() + thePnt1.XYZ() + thePnt2.XYZ()) * (1.0 / 3.0);
41 }
42
43 //==================================================
44 // Function: Matches
45 // Purpose : Checks whether the triangle overlaps
46 //           current selecting volume
47 //==================================================
48 Standard_Boolean Select3D_SensitiveTriangle::Matches (SelectBasics_SelectingVolumeManager& theMgr,
49                                                       SelectBasics_PickResult& thePickResult)
50 {
51   Standard_Real aDepth     = RealLast();
52   Standard_Real aDistToCOG = RealLast();
53   if (!theMgr.IsOverlapAllowed())
54   {
55     Standard_Real aDummy;
56     return theMgr.Overlaps (myPoints[0], aDummy)
57         && theMgr.Overlaps (myPoints[1], aDummy)
58         && theMgr.Overlaps (myPoints[2], aDummy);
59   }
60
61   if (!theMgr.Overlaps (myPoints[0], myPoints[1], myPoints[2], mySensType, aDepth))
62   {
63     thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
64     return Standard_False;
65   }
66
67   aDistToCOG = theMgr.DistToGeometryCenter (myCentroid);
68   thePickResult = SelectBasics_PickResult (aDepth, aDistToCOG);
69   return Standard_True;
70 }
71
72 //==================================================
73 // Function: Points3D
74 // Purpose :
75 //==================================================
76 void Select3D_SensitiveTriangle::Points3D (gp_Pnt& thePnt0, gp_Pnt& thePnt1, gp_Pnt& thePnt2) const
77 {
78   thePnt0 = myPoints[0];
79   thePnt1 = myPoints[1];
80   thePnt2 = myPoints[2];
81 }
82
83 //==================================================
84 // Function: Center3D
85 // Purpose :
86 //==================================================
87 gp_Pnt Select3D_SensitiveTriangle::Center3D() const
88 {
89   return myCentroid;
90 }
91
92 //==================================================
93 // Function: GetConnected
94 // Purpose :
95 //==================================================
96 Handle(Select3D_SensitiveEntity) Select3D_SensitiveTriangle::GetConnected()
97 {
98   // Create a copy of this
99   Handle(Select3D_SensitiveEntity) aNewEntity =
100     new Select3D_SensitiveTriangle (myOwnerId, myPoints[0], myPoints[1], myPoints[2], mySensType);
101
102   return aNewEntity;
103 }
104
105 //==================================================
106 // Function: BoundingBox
107 // Purpose : Returns bounding box of the triangle.
108 //           If location transformation is set, it
109 //           will be applied
110 //==================================================
111 Select3D_BndBox3d Select3D_SensitiveTriangle::BoundingBox()
112 {
113   const SelectMgr_Vec3 aMinPnt = SelectMgr_Vec3 (Min (myPoints[0].X(), Min (myPoints[1].X(), myPoints[2].X())),
114                                                  Min (myPoints[0].Y(), Min (myPoints[1].Y(), myPoints[2].Y())),
115                                                  Min (myPoints[0].Z(), Min (myPoints[1].Z(), myPoints[2].Z())));
116   const SelectMgr_Vec3 aMaxPnt = SelectMgr_Vec3 (Max (myPoints[0].X(), Max (myPoints[1].X(), myPoints[2].X())),
117                                                  Max (myPoints[0].Y(), Max (myPoints[1].Y(), myPoints[2].Y())),
118                                                  Max (myPoints[0].Z(), Max (myPoints[1].Z(), myPoints[2].Z())));
119   return Select3D_BndBox3d (aMinPnt, aMaxPnt);
120 }