0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / SelectMgr / SelectMgr_TriangularFrustumSet.cxx
1 // Created on: 2014-11-21
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <BRepMesh_DataStructureOfDelaun.hxx>
17 #include <BRepMesh_Delaun.hxx>
18 #include <NCollection_IncAllocator.hxx>
19
20 #include <SelectMgr_TriangularFrustumSet.hxx>
21 #include <SelectMgr_TriangularFrustum.hxx>
22
23 #define MEMORY_BLOCK_SIZE 512 * 7
24
25 // =======================================================================
26 // function : BuildSelectingVolume
27 // purpose  : Meshes polygon bounded by polyline. Than organizes a set of
28 //            triangular frustums, where each triangle's projection onto
29 //            near and far view frustum planes is considered as a frustum
30 //            base
31 // =======================================================================
32 void SelectMgr_TriangularFrustumSet::Build (const TColgp_Array1OfPnt2d& thePoints)
33 {
34   myFrustums.Clear();
35
36   Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
37   Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
38   Standard_Integer aPtsLower = thePoints.Lower();
39   Standard_Integer aPtsUpper = thePoints.Upper();
40   BRepMesh::Array1OfInteger anIndexes (0, thePoints.Length() - 1);
41   for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
42   {
43     BRepMesh_Vertex aVertex (thePoints.Value (aPtIdx).XY(), aPtIdx, BRepMesh_Frontier);
44     anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
45   }
46
47   Standard_Real aPtSum = 0;
48   for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
49   {
50     Standard_Integer aNextIdx = (aIdx % thePoints.Length()) + 1;
51     aPtSum += (thePoints.Value (aNextIdx).Coord().X() - thePoints.Value (aIdx).Coord().X())
52               * (thePoints.Value (aNextIdx).Coord().Y() + thePoints.Value (aIdx).Coord().Y());
53   }
54   Standard_Boolean isClockwiseOrdered = aPtSum < 0;
55
56   for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
57   {
58     Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
59     Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
60     BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
61                           anIndexes.Value (aNextPtIdx),
62                           BRepMesh_Frontier);
63     aMeshStructure->AddLink (anEdge);
64   }
65
66   BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
67   const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
68   if (aTriangles.Extent() < 1)
69     return;
70
71   BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
72   for (; aTriangleIt.More(); aTriangleIt.Next())
73   {
74     const Standard_Integer aTriangleId = aTriangleIt.Key();
75     const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
76
77     if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
78       continue;
79
80     Standard_Integer aTriangleVerts[3];
81     aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
82
83     gp_Pnt2d aPts[3];
84     for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
85     {
86       const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
87       aPts[aVertIdx] = aVertex.Coord();
88     }
89
90     Handle(SelectMgr_TriangularFrustum) aTrFrustum = new SelectMgr_TriangularFrustum();
91     aTrFrustum->SetBuilder (myBuilder);
92     aTrFrustum->Build (aPts[0], aPts[1], aPts[2]);
93     myFrustums.Append (aTrFrustum);
94   }
95
96   aMeshStructure.Nullify();
97   anAllocator.Nullify();
98 }
99
100 // =======================================================================
101 // function : ScaleAndTransform
102 // purpose  : IMPORTANT: Scaling makes sense only for frustum built on a single point!
103 //            Note that this method does not perform any checks on type of the frustum.
104 //            Returns a copy of the frustum resized according to the scale factor given
105 //            and transforms it using the matrix given.
106 //            There are no default parameters, but in case if:
107 //                - transformation only is needed: @theScaleFactor must be initialized
108 //                  as any negative value;
109 //                - scale only is needed: @theTrsf must be set to gp_Identity.
110 // =======================================================================
111 Handle(SelectMgr_BaseFrustum) SelectMgr_TriangularFrustumSet::ScaleAndTransform (const Standard_Integer theScale,
112                                                                                  const gp_GTrsf& theTrsf) const
113 {
114   Handle(SelectMgr_TriangularFrustumSet) aRes = new SelectMgr_TriangularFrustumSet();
115
116   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
117   {
118     aRes->myFrustums.Append (Handle(SelectMgr_TriangularFrustum)::DownCast (anIter.Value()->ScaleAndTransform (theScale, theTrsf)));
119   }
120
121   return aRes;
122 }
123
124 // =======================================================================
125 // function : Overlaps
126 // purpose  :
127 // =======================================================================
128 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt,
129                                                            const SelectMgr_Vec3& theMaxPnt,
130                                                            Standard_Real& theDepth)
131 {
132   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
133   {
134     if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, theDepth))
135       return Standard_True;
136   }
137
138   return Standard_False;
139 }
140
141 // =======================================================================
142 // function : Overlaps
143 // purpose  :
144 // =======================================================================
145 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const SelectMgr_Vec3& theMinPnt,
146                                                            const SelectMgr_Vec3& theMaxPnt,
147                                                            Standard_Boolean* /*theInside*/)
148 {
149   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
150   {
151     if (anIter.Value()->Overlaps (theMinPnt, theMaxPnt, NULL))
152       return Standard_True;
153   }
154
155   return Standard_False;
156 }
157
158 // =======================================================================
159 // function : Overlaps
160 // purpose  :
161 // =======================================================================
162 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt,
163                                                            Standard_Real& theDepth)
164 {
165   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
166   {
167     if (anIter.Value()->Overlaps (thePnt, theDepth))
168       return Standard_True;
169   }
170
171   return Standard_False;
172 }
173
174 // =======================================================================
175 // function : Overlaps
176 // purpose  :
177 // =======================================================================
178 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const TColgp_Array1OfPnt& theArrayOfPts,
179                                                            Select3D_TypeOfSensitivity theSensType,
180                                                            Standard_Real& theDepth)
181 {
182   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
183   {
184     if (anIter.Value()->Overlaps (theArrayOfPts, theSensType, theDepth))
185       return Standard_True;
186   }
187
188   return Standard_False;
189 }
190
191 // =======================================================================
192 // function : Overlaps
193 // purpose  :
194 // =======================================================================
195 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
196                                                            const gp_Pnt& thePnt2,
197                                                            Standard_Real& theDepth)
198 {
199   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
200   {
201     if (anIter.Value()->Overlaps (thePnt1, thePnt2, theDepth))
202       return Standard_True;
203   }
204
205   return Standard_False;
206 }
207
208 // =======================================================================
209 // function : Overlaps
210 // purpose  :
211 // =======================================================================
212 Standard_Boolean SelectMgr_TriangularFrustumSet::Overlaps (const gp_Pnt& thePnt1,
213                                                            const gp_Pnt& thePnt2,
214                                                            const gp_Pnt& thePnt3,
215                                                            Select3D_TypeOfSensitivity theSensType,
216                                                            Standard_Real& theDepth)
217 {
218   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
219   {
220     if (anIter.Value()->Overlaps (thePnt1, thePnt2, thePnt3, theSensType, theDepth))
221       return Standard_True;
222   }
223
224   return Standard_False;
225 }
226
227 // =======================================================================
228 // function : GetPlanes
229 // purpose  :
230 // =======================================================================
231 void SelectMgr_TriangularFrustumSet::GetPlanes (NCollection_Vector<SelectMgr_Vec4>& thePlaneEquations) const
232 {
233   thePlaneEquations.Clear();
234
235   for (SelectMgr_TriangFrustumsIter anIter (myFrustums); anIter.More(); anIter.Next())
236   {
237     anIter.Value()->GetPlanes (thePlaneEquations);
238   }
239 }
240
241 #undef MEMORY_BLOCK_SIZE