0024070: OpenGL capped object-level clipping planes
[occt.git] / src / Select3D / Select3D_SensitiveCurve.cxx
CommitLineData
b311480e 1// Created on: 1995-03-13
2// Created by: Robert COUBLANC
3// Copyright (c) 1995-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22
23
24#include <Select3D_SensitiveCurve.ixx>
25#include <SelectBasics_BasicTool.hxx>
26#include <gp_Lin2d.hxx>
27#include <Precision.hxx>
28#include <ElCLib.hxx>
29#include <CSLib_Class2d.hxx>
30
31
32//==================================================
ac04d101 33// Function: Creation
7fd59977 34// Purpose :
35//==================================================
36
37Select3D_SensitiveCurve
38::Select3D_SensitiveCurve(const Handle(SelectBasics_EntityOwner)& OwnerId,
ac04d101
SA
39 const Handle(Geom_Curve)& C,
40 const Standard_Integer NbPoints):
7fd59977 41Select3D_SensitivePoly(OwnerId, NbPoints),
aec37c15 42mylastseg(0),
ac04d101 43myCurve(C)
7fd59977 44{
45 LoadPoints(C,NbPoints);
46}
ac04d101 47
7fd59977 48//==================================================
49// Function: Creation
50// Purpose :
51//==================================================
ac04d101 52
7fd59977 53Select3D_SensitiveCurve
54::Select3D_SensitiveCurve(const Handle(SelectBasics_EntityOwner)& OwnerId,
ac04d101 55 const Handle(TColgp_HArray1OfPnt)& ThePoints):
7fd59977 56Select3D_SensitivePoly(OwnerId, ThePoints),
ac04d101 57mylastseg(0)
7fd59977 58{
59}
ac04d101 60
7fd59977 61//==================================================
62// Function: Creation
63// Purpose :
64//==================================================
ac04d101 65
7fd59977 66Select3D_SensitiveCurve
67::Select3D_SensitiveCurve(const Handle(SelectBasics_EntityOwner)& OwnerId,
ac04d101 68 const TColgp_Array1OfPnt& ThePoints):
7fd59977 69Select3D_SensitivePoly(OwnerId, ThePoints),
ac04d101 70mylastseg(0)
7fd59977 71{
72}
73
74//==================================================
75// Function: Matches
76// Purpose :
77//==================================================
ac04d101 78
4269bd1b 79Standard_Boolean Select3D_SensitiveCurve::Matches (const SelectBasics_PickArgs& thePickArgs,
80 Standard_Real& theMatchDMin,
81 Standard_Real& theMatchDepth)
7fd59977 82{
83 Standard_Integer Rank;
ceae62f0 84 TColgp_Array1OfPnt2d aArrayOf2dPnt(1, mypolyg.Size());
7fd59977 85 Points2D(aArrayOf2dPnt);
4952a30a 86 if (SelectBasics_BasicTool::MatchPolyg2d (aArrayOf2dPnt,
4269bd1b 87 thePickArgs.X(), thePickArgs.Y(),
88 thePickArgs.Tolerance(),
89 theMatchDMin,
4952a30a 90 Rank))
91 {
4269bd1b 92 // remember detected segment (for GetLastDetected)
7fd59977 93 mylastseg = Rank;
4269bd1b 94
95 theMatchDepth = ComputeDepth (thePickArgs.PickLine(), Rank);
96
97 return !thePickArgs.IsClipped (theMatchDepth);
7fd59977 98 }
4269bd1b 99
4952a30a 100 return Standard_False;
7fd59977 101}
ac04d101 102
7fd59977 103//==================================================
104// Function: Matches
105// Purpose : know if a box touches the projected polygon
106// of the Curve.
107//==================================================
108
109Standard_Boolean Select3D_SensitiveCurve::
110Matches (const Standard_Real XMin,
ac04d101
SA
111 const Standard_Real YMin,
112 const Standard_Real XMax,
113 const Standard_Real YMax,
114 const Standard_Real aTol)
115{
7fd59977 116 Bnd_Box2d BoundBox;
117 BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
aec37c15 118
ceae62f0 119 for(Standard_Integer anIndex=0; anIndex<mypolyg.Size(); ++anIndex)
7fd59977 120 {
ceae62f0
A
121 if(BoundBox.IsOut(mypolyg.Pnt2d(anIndex)))
122 return Standard_False;
7fd59977 123 }
124 return Standard_True;
125}
126
127//=======================================================================
128//function : Matches
aec37c15 129//purpose :
7fd59977 130//=======================================================================
131
132Standard_Boolean Select3D_SensitiveCurve::
133Matches (const TColgp_Array1OfPnt2d& aPoly,
ac04d101
SA
134 const Bnd_Box2d& aBox,
135 const Standard_Real aTol)
136{
7fd59977 137 Standard_Real Umin,Vmin,Umax,Vmax;
138 aBox.Get(Umin,Vmin,Umax,Vmax);
7fd59977 139 CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);
140
ceae62f0 141 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
ac04d101 142 {
ceae62f0
A
143 Standard_Integer RES = aClassifier2d.SiDans(mypolyg.Pnt2d(anIndex));
144 if(RES!=1)
145 return Standard_False;
7fd59977 146 }
147 return Standard_True;
148}
149
7fd59977 150//==================================================
ac04d101 151// Function: LoadPoints
7fd59977 152// Purpose :
153//==================================================
154
155void Select3D_SensitiveCurve
156::LoadPoints (const Handle(Geom_Curve)& aCurve,const Standard_Integer NbP)
157{
aec37c15 158 /*this method is private and it used only inside of constructor.
7fd59977 159 That's why check !NbP==mypolyg3d->Length() was removed*/
160
161 Standard_Real Step = (aCurve->LastParameter()- aCurve->FirstParameter())/(NbP-1);
162 Standard_Real Curparam = aCurve->FirstParameter();
ceae62f0
A
163 for(Standard_Integer anIndex=0;anIndex<mypolyg.Size();++anIndex)
164 {
165 mypolyg.SetPnt(anIndex, aCurve->Value(Curparam));
166 Curparam+=Step;
167 }
7fd59977 168}
169
170//=======================================================================
171//function : Dump
aec37c15 172//purpose :
7fd59977 173//=======================================================================
174
aec37c15 175void Select3D_SensitiveCurve::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
7fd59977 176{
177 S<<"\tSensitiveCurve 3D :"<<endl;
ceae62f0 178 if (HasLocation())
7fd59977 179 S<<"\t\tExisting Location"<<endl;
180
ceae62f0 181 S<<"\t\tNumber Of Points :"<<mypolyg.Size()<<endl;
7fd59977 182
ac04d101
SA
183 if(FullDump)
184 {
7fd59977 185 Select3D_SensitiveEntity::DumpBox(S,mybox2d);
186 }
187}
ac04d101 188
7fd59977 189//=======================================================================
190//function : ComputeDepth
aec37c15 191//purpose :
7fd59977 192//=======================================================================
193
4269bd1b 194Standard_Real Select3D_SensitiveCurve::ComputeDepth (const gp_Lin& thePickLine,
195 const Standard_Integer theSegment) const
7fd59977 196{
4269bd1b 197 if (theSegment == 0)
198 {
199 return Precision::Infinite();
200 }
ceae62f0 201
4269bd1b 202 // In case if theSegment and theSegment + 1 are not valid
ceae62f0 203 // the depth will be infinite
4269bd1b 204 if (theSegment >= mypolyg.Size())
ceae62f0 205 {
4269bd1b 206 return Precision::Infinite();
207 }
208
209 gp_XYZ aCDG = mypolyg.Pnt (theSegment);
210
211 if (theSegment + 1 < mypolyg.Size())
212 {
213 aCDG += mypolyg.Pnt(theSegment + 1);
214 aCDG /= 2.;
ceae62f0
A
215 }
216
4269bd1b 217 return ElCLib::Parameter (thePickLine, gp_Pnt (aCDG));
7fd59977 218}
ac04d101
SA
219
220//=======================================================================
221//function : GetConnected
aec37c15 222//purpose :
223//=======================================================================
ac04d101 224
aec37c15 225Handle(Select3D_SensitiveEntity) Select3D_SensitiveCurve::GetConnected(const TopLoc_Location &theLocation)
ac04d101 226{
aec37c15 227 // Create a copy of this
ac04d101 228 Handle(Select3D_SensitiveEntity) aNewEntity;
aec37c15 229 // this was constructed using Handle(Geom_Curve)
230 if (!myCurve.IsNull())
ac04d101
SA
231 {
232 aNewEntity = new Select3D_SensitiveCurve(myOwnerId, myCurve);
233 }
234 // this was constructed using TColgp_HArray1OfPnt
aec37c15 235 else
ac04d101 236 {
ceae62f0
A
237 Standard_Integer aSize = mypolyg.Size();
238 Handle(TColgp_HArray1OfPnt) aPoints = new TColgp_HArray1OfPnt(1, aSize);
ac04d101 239 // Fill the array with points from mypolyg3d
aec37c15 240 for (Standard_Integer anIndex = 1; anIndex <= aSize; ++anIndex)
ac04d101 241 {
ceae62f0 242 aPoints->SetValue(anIndex, mypolyg.Pnt(anIndex-1));
ac04d101 243 }
ceae62f0 244 aNewEntity = new Select3D_SensitiveCurve(myOwnerId, aPoints);
ac04d101 245 }
aec37c15 246
247 if (HasLocation())
248 aNewEntity->SetLocation(Location());
ac04d101
SA
249
250 aNewEntity->UpdateLocation(theLocation);
251
252 return aNewEntity;
aec37c15 253}