8364169a99c9ee6d6a77ed7462bf38fb21c61cf3
[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 <Precision.hxx>
20 #include <Bnd_Box.hxx>
21 #include <TopLoc_Location.hxx>
22
23 IMPLEMENT_STANDARD_RTTIEXT(Select3D_SensitiveTriangle,Select3D_SensitiveEntity)
24
25 //==================================================
26 // Function: Creation
27 // Purpose :
28 //==================================================
29 Select3D_SensitiveTriangle::Select3D_SensitiveTriangle (const Handle(SelectMgr_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   if (!theMgr.IsOverlapAllowed())
52   {
53     if (theMgr.GetActiveSelectionType() == SelectMgr_SelectionType_Polyline)
54     {
55       SelectBasics_PickResult aDummy;
56       return theMgr.OverlapsTriangle (myPoints[0], myPoints[1], myPoints[2], mySensType, aDummy);
57     }
58     return theMgr.OverlapsPoint (myPoints[0])
59         && theMgr.OverlapsPoint (myPoints[1])
60         && theMgr.OverlapsPoint (myPoints[2]);
61   }
62
63   if (!theMgr.OverlapsTriangle (myPoints[0], myPoints[1], myPoints[2], mySensType, thePickResult))
64   {
65     return Standard_False;
66   }
67
68   thePickResult.SetDistToGeomCenter (theMgr.DistToGeometryCenter(myCentroid));
69   return Standard_True;
70 }
71
72 //==================================================
73 // Function: GetConnected
74 // Purpose :
75 //==================================================
76 Handle(Select3D_SensitiveEntity) Select3D_SensitiveTriangle::GetConnected()
77 {
78   // Create a copy of this
79   Handle(Select3D_SensitiveEntity) aNewEntity =
80     new Select3D_SensitiveTriangle (myOwnerId, myPoints[0], myPoints[1], myPoints[2], mySensType);
81
82   return aNewEntity;
83 }
84
85 //==================================================
86 // Function: BoundingBox
87 // Purpose : Returns bounding box of the triangle.
88 //           If location transformation is set, it
89 //           will be applied
90 //==================================================
91 Select3D_BndBox3d Select3D_SensitiveTriangle::BoundingBox()
92 {
93   const SelectMgr_Vec3 aMinPnt = SelectMgr_Vec3 (Min (myPoints[0].X(), Min (myPoints[1].X(), myPoints[2].X())),
94                                                  Min (myPoints[0].Y(), Min (myPoints[1].Y(), myPoints[2].Y())),
95                                                  Min (myPoints[0].Z(), Min (myPoints[1].Z(), myPoints[2].Z())));
96   const SelectMgr_Vec3 aMaxPnt = SelectMgr_Vec3 (Max (myPoints[0].X(), Max (myPoints[1].X(), myPoints[2].X())),
97                                                  Max (myPoints[0].Y(), Max (myPoints[1].Y(), myPoints[2].Y())),
98                                                  Max (myPoints[0].Z(), Max (myPoints[1].Z(), myPoints[2].Z())));
99   return Select3D_BndBox3d (aMinPnt, aMaxPnt);
100 }
101
102 //=======================================================================
103 //function : DumpJson
104 //purpose  :
105 //=======================================================================
106 void Select3D_SensitiveTriangle::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
107 {
108   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
109   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Select3D_SensitiveEntity)
110
111   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mySensType)
112
113   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPoints[0])
114   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPoints[1])
115   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPoints[2])
116
117   Select3D_BndBox3d aBoundingBox = ((Select3D_SensitiveTriangle*)this)->BoundingBox();
118   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &aBoundingBox)
119 }