1 // Copyright (c) 2020 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <SelectMgr.hxx>
16 #include <Geom_Circle.hxx>
17 #include <Graphic3d_ArrayOfPoints.hxx>
18 #include <Graphic3d_AspectMarker3d.hxx>
19 #include <Poly_Triangulation.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_NListOfSequenceOfPnt.hxx>
23 #include <TColgp_HArray1OfPnt.hxx>
24 #include <TColgp_SequenceOfPnt.hxx>
25 #include <Select3D_SensitiveBox.hxx>
26 #include <Select3D_SensitiveCircle.hxx>
27 #include <Select3D_SensitiveCylinder.hxx>
28 #include <Select3D_SensitiveEntity.hxx>
29 #include <Select3D_SensitiveFace.hxx>
30 #include <Select3D_SensitivePoint.hxx>
31 #include <Select3D_SensitiveSegment.hxx>
32 #include <Select3D_SensitiveTriangle.hxx>
33 #include <Select3D_SensitiveTriangulation.hxx>
34 #include <Select3D_SensitiveWire.hxx>
35 #include <SelectMgr_Selection.hxx>
39 //! Compute polyline of shrunk triangle.
40 static Handle(TColgp_HSequenceOfPnt) shrunkTriangle (const gp_Pnt* thePnts,
41 const gp_XYZ& theCenter)
43 const gp_XYZ aV1 = theCenter + (thePnts[0].XYZ() - theCenter) * 0.9;
44 const gp_XYZ aV2 = theCenter + (thePnts[1].XYZ() - theCenter) * 0.9;
45 const gp_XYZ aV3 = theCenter + (thePnts[2].XYZ() - theCenter) * 0.9;
46 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
47 aPoints->Append (aV1);
48 aPoints->Append (aV2);
49 aPoints->Append (aV3);
50 aPoints->Append (aV1);
54 //! Fill in triangulation polylines.
55 static void addTriangulation (Prs3d_NListOfSequenceOfPnt& theSeqLines,
56 Prs3d_NListOfSequenceOfPnt& theSeqFree,
57 const Handle(Select3D_SensitiveTriangulation)& theTri,
58 const gp_Trsf& theLoc)
60 gp_Trsf aTrsf = theLoc;
61 if (theTri->HasInitLocation())
63 aTrsf = theLoc * theTri->GetInitLocation();
65 const Handle(Poly_Triangulation)& aPolyTri = theTri->Triangulation();
66 for (Standard_Integer aTriIter = 1; aTriIter <= aPolyTri->NbTriangles(); ++aTriIter)
68 const Poly_Triangle& aTri = aPolyTri->Triangle (aTriIter);
69 const gp_Pnt aPnts[3] =
71 aPolyTri->Node (aTri (1)).Transformed (aTrsf),
72 aPolyTri->Node (aTri (2)).Transformed (aTrsf),
73 aPolyTri->Node (aTri (3)).Transformed (aTrsf)
75 const gp_XYZ aCenter = (aPnts[0].XYZ() + aPnts[1].XYZ() + aPnts[2].XYZ()) / 3.0;
76 theSeqLines.Append (shrunkTriangle (aPnts, aCenter));
79 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
80 Prs3d::AddFreeEdges (*aPoints, aPolyTri, aTrsf);
81 if (!aPoints->IsEmpty())
83 theSeqFree.Append (aPoints);
87 //! Fill in bounding box polylines.
88 static void addBoundingBox (Prs3d_NListOfSequenceOfPnt& theSeqLines,
89 const Handle(Select3D_SensitiveBox)& theSensBox,
90 const gp_Trsf& theLoc)
92 Graphic3d_Vec3d aMin, aMax;
93 theSensBox->Box().Get (aMin.x(), aMin.y(), aMin.z(), aMax.x(), aMax.y(), aMax.z());
96 gp_Pnt(aMin.x(), aMin.y(), aMin.z()),
97 gp_Pnt(aMax.x(), aMin.y(), aMin.z()),
98 gp_Pnt(aMax.x(), aMax.y(), aMin.z()),
99 gp_Pnt(aMin.x(), aMax.y(), aMin.z()),
100 gp_Pnt(aMin.x(), aMin.y(), aMax.z()),
101 gp_Pnt(aMax.x(), aMin.y(), aMax.z()),
102 gp_Pnt(aMax.x(), aMax.y(), aMax.z()),
103 gp_Pnt(aMin.x(), aMax.y(), aMax.z())
105 for (Standard_Integer aPntIter = 0; aPntIter <= 7; ++aPntIter)
107 aPnts[aPntIter].Transform (theLoc);
111 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
112 for (Standard_Integer i = 0; i < 4; ++i)
114 aPoints->Append (aPnts[i]);
116 aPoints->Append (aPnts[0]);
117 theSeqLines.Append (aPoints);
120 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
121 for (Standard_Integer i = 4; i < 8; i++)
123 aPoints->Append (aPnts[i]);
125 aPoints->Append (aPnts[4]);
126 theSeqLines.Append (aPoints);
128 for (Standard_Integer i = 0; i < 4; i++)
130 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
131 aPoints->Append (aPnts[i]);
132 aPoints->Append (aPnts[i+4]);
133 theSeqLines.Append (aPoints);
137 //! Fill in circle polylines.
138 static void addCircle (Prs3d_NListOfSequenceOfPnt& theSeqLines,
139 const Standard_Real theRadius,
140 const gp_Trsf& theTrsf,
141 const Standard_Real theHeight = 0)
143 const Standard_Real anUStep = 0.1;
144 gp_XYZ aVec (0, 0, theHeight);
146 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
147 Geom_Circle aGeom (gp_Ax2(), theRadius);
148 for (Standard_Real anU = 0.0f; anU < (2.0 * M_PI + anUStep); anU += anUStep)
150 gp_Pnt aCircPnt = aGeom.Value (anU).Coord() + aVec;
151 aCircPnt.Transform (theTrsf);
152 aPoints->Append (aCircPnt);
154 theSeqLines.Append (aPoints);
157 //! Fill in cylinder polylines.
158 static void addCylinder (Prs3d_NListOfSequenceOfPnt& theSeqLines,
159 const Handle(Select3D_SensitiveCylinder)& theSensCyl,
160 const gp_Trsf& theLoc)
162 Handle(TColgp_HSequenceOfPnt) aVertLine1 = new TColgp_HSequenceOfPnt();
163 Handle(TColgp_HSequenceOfPnt) aVertLine2 = new TColgp_HSequenceOfPnt();
165 const gp_Trsf& aTrsf = theLoc.Multiplied (theSensCyl->Transformation());
166 const Standard_Real aHeight = theSensCyl->Height();
168 for (int aCircNum = 0; aCircNum < 3; aCircNum++)
170 Standard_Real aRadius = 0.5 * (2 - aCircNum) * theSensCyl->BottomRadius()
171 + 0.5 * aCircNum * theSensCyl->TopRadius();
172 const gp_XYZ aVec (0, 0, aHeight * 0.5 * aCircNum);
176 aVertLine1->Append (gp_Pnt (gp_XYZ (aRadius, 0, 0) + aVec).Transformed (aTrsf));
177 aVertLine2->Append (gp_Pnt (gp_XYZ (-aRadius, 0, 0) + aVec).Transformed (aTrsf));
180 if (aRadius > Precision::Confusion())
182 addCircle (theSeqLines, aRadius, aTrsf, aVec.Z());
185 theSeqLines.Append (aVertLine1);
186 theSeqLines.Append (aVertLine2);
190 //=======================================================================
191 //function : ComputeSensitivePrs
193 //=======================================================================
194 void SelectMgr::ComputeSensitivePrs (const Handle(Graphic3d_Structure)& thePrs,
195 const Handle(SelectMgr_Selection)& theSel,
196 const gp_Trsf& theLoc,
197 const Handle(Graphic3d_TransformPers)& theTrsfPers)
199 thePrs->SetTransformPersistence (theTrsfPers);
201 Prs3d_NListOfSequenceOfPnt aSeqLines, aSeqFree;
202 TColgp_SequenceOfPnt aSeqPoints;
203 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSel->Entities()); aSelEntIter.More(); aSelEntIter.Next())
205 const Handle(Select3D_SensitiveEntity)& anEnt = aSelEntIter.Value()->BaseSensitive();
206 if (Handle(Select3D_SensitiveBox) aSensBox = Handle(Select3D_SensitiveBox)::DownCast (anEnt))
208 addBoundingBox (aSeqLines, aSensBox, theLoc);
210 else if (Handle(Select3D_SensitiveCylinder) aSensCyl = Handle(Select3D_SensitiveCylinder)::DownCast (anEnt))
212 addCylinder (aSeqLines, aSensCyl, theLoc);
214 else if (Handle(Select3D_SensitiveCircle) aSensCircle = Handle(Select3D_SensitiveCircle)::DownCast (anEnt))
216 addCircle (aSeqLines, aSensCircle->Radius(), theLoc.Multiplied (aSensCircle->Transformation()));
218 else if (Handle(Select3D_SensitiveFace) aFace = Handle(Select3D_SensitiveFace)::DownCast(anEnt))
220 Handle(TColgp_HArray1OfPnt) aSensPnts;
221 aFace->GetPoints (aSensPnts);
222 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
223 for (TColgp_HArray1OfPnt::Iterator aPntIter (*aSensPnts); aPntIter.More(); aPntIter.Next())
225 aPoints->Append (aPntIter.Value().Transformed (theLoc));
227 aSeqLines.Append (aPoints);
229 else if (Handle(Select3D_SensitivePoly) aSensPoly = Handle(Select3D_SensitivePoly)::DownCast (anEnt))
231 Standard_Integer aFrom = 0, aTo = 0;
232 aSensPoly->ArrayBounds (aFrom, aTo);
233 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
234 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; ++aPntIter)
236 aPoints->Append (aSensPoly->GetPoint3d (aPntIter).Transformed (theLoc));
238 aSeqLines.Append (aPoints);
240 else if (Handle(Select3D_SensitiveWire) aWire = Handle(Select3D_SensitiveWire)::DownCast(anEnt))
242 const NCollection_Vector<Handle(Select3D_SensitiveEntity)>& anEntities = aWire->GetEdges();
243 for (NCollection_Vector<Handle(Select3D_SensitiveEntity)>::Iterator aSubIter (anEntities); aSubIter.More(); aSubIter.Next())
245 const Handle(Select3D_SensitiveEntity)& aSubEnt = aSubIter.Value();
246 if (Handle(Select3D_SensitiveSegment) aSensSeg = Handle(Select3D_SensitiveSegment)::DownCast (aSubEnt))
248 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
249 aPoints->Append (aSensSeg->StartPoint().Transformed (theLoc));
250 aPoints->Append (aSensSeg->EndPoint() .Transformed (theLoc));
251 aSeqLines.Append (aPoints);
253 else if (Handle(Select3D_SensitivePoly) aSubSensPoly = Handle(Select3D_SensitivePoly)::DownCast (aSubEnt))
255 Standard_Integer aFrom = 0, aTo = 0;
256 aSubSensPoly->ArrayBounds (aFrom, aTo);
257 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
258 for (Standard_Integer aPntIter = aFrom; aPntIter <= aTo; ++aPntIter)
260 aPoints->Append (aSubSensPoly->GetPoint3d (aPntIter).Transformed (theLoc));
262 aSeqLines.Append (aPoints);
266 else if (Handle(Select3D_SensitiveSegment) aSensSeg = Handle(Select3D_SensitiveSegment)::DownCast(anEnt))
268 Handle(TColgp_HSequenceOfPnt) aPoints = new TColgp_HSequenceOfPnt();
269 aPoints->Append (aSensSeg->StartPoint().Transformed (theLoc));
270 aPoints->Append (aSensSeg->EndPoint() .Transformed (theLoc));
271 aSeqLines.Append (aPoints);
273 else if (Handle(Select3D_SensitivePoint) aSensPnt = Handle(Select3D_SensitivePoint)::DownCast(anEnt))
275 aSeqPoints.Append (aSensPnt->Point().Transformed (theLoc));
277 else if (Handle(Select3D_SensitiveTriangulation) aSensTri = Handle(Select3D_SensitiveTriangulation)::DownCast (anEnt))
279 addTriangulation (aSeqLines, aSeqFree, aSensTri, theLoc);
281 else if (Handle(Select3D_SensitiveTriangle) aSensTri1 = Handle(Select3D_SensitiveTriangle)::DownCast(anEnt))
284 aSensTri1->Points3D (aPnts[0], aPnts[1], aPnts[2]);
285 aPnts[0].Transform (theLoc);
286 aPnts[1].Transform (theLoc);
287 aPnts[2].Transform (theLoc);
288 aSeqLines.Append (shrunkTriangle (aPnts, aSensTri1->Center3D().XYZ()));
292 if (!aSeqPoints.IsEmpty())
294 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (aSeqPoints.Size());
295 for (TColgp_SequenceOfPnt::Iterator aPntIter (aSeqPoints); aPntIter.More(); aPntIter.Next())
297 anArrayOfPoints->AddVertex (aPntIter.Value());
300 Handle(Graphic3d_Group) aSensPntGroup = thePrs->NewGroup();
301 aSensPntGroup->SetPrimitivesAspect (new Graphic3d_AspectMarker3d (Aspect_TOM_O_PLUS, Quantity_NOC_INDIANRED3, 2.0));
302 aSensPntGroup->AddPrimitiveArray (anArrayOfPoints);
304 if (!aSeqLines.IsEmpty())
306 Prs3d::AddPrimitivesGroup (thePrs, new Prs3d_LineAspect (Quantity_NOC_AQUAMARINE1, Aspect_TOL_DASH, 1.0), aSeqLines);
308 if (!aSeqFree.IsEmpty())
310 Prs3d::AddPrimitivesGroup (thePrs, new Prs3d_LineAspect (Quantity_NOC_GREEN, Aspect_TOL_SOLID, 2.0), aSeqFree);