summaryrefslogtreecommitdiff
path: root/src/Select3D/Select3D_SensitiveSegment.cxx
blob: c55e6e9183109e0c7bc5c3af3daa809f610a7268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright: 	Matra-Datavision 1995
// File:	Select3D_SensitiveSegment.cxx
// Created:	Wed Jan 25 11:27:54 1995
// Author:	Mister rmi
//		<rmi>


#include <Select3D_SensitiveSegment.ixx>
#include <SelectBasics_BasicTool.hxx>
#include <gp_Vec.hxx>
#include <gp_Vec2d.hxx>
#include <Bnd_Box2d.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Dir2d.hxx>
#include <gp_Lin2d.hxx>
#include <ElCLib.hxx>
#include <Extrema_ExtElC.hxx>
#include <Extrema_POnCurv.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <Precision.hxx>
#include <SelectBasics_ListIteratorOfListOfBox2d.hxx>

#include <CSLib_Class2d.hxx>

//=====================================================
// Function : Create
// Purpose  :Constructor
//=====================================================


Select3D_SensitiveSegment::
Select3D_SensitiveSegment(const Handle(SelectBasics_EntityOwner)& OwnerId,
			  const gp_Pnt& FirstP,
			  const gp_Pnt& LastP,
			  const Standard_Integer MaxRect):
Select3D_SensitiveEntity(OwnerId),
mymaxrect(MaxRect)
{
  mystart = FirstP;
  myend = LastP;
}

//=====================================================
// Function :
// Purpose  :
//=====================================================
void Select3D_SensitiveSegment
::Project(const Handle(Select3D_Projector)& aProj)
{
  Select3D_SensitiveEntity::Project(aProj); // to set the field last proj...
  gp_Pnt2d aPoint2dStart;
  gp_Pnt2d aPoint2dEnd;
  
  if(HasLocation()){
    gp_Pnt aStart(mystart.x, mystart.y, mystart.z);
    gp_Pnt aEnd(myend.x, myend.y, myend.z);
    aProj->Project(aStart.Transformed(Location().Transformation()),aPoint2dStart);
    aProj->Project(aEnd.Transformed(Location().Transformation()),aPoint2dEnd);
  }
  else{
    aProj->Project(mystart,aPoint2dStart);
    aProj->Project(myend,aPoint2dEnd);
  }
  myprojstart = aPoint2dStart;
  myprojend = aPoint2dEnd;
}

//=====================================================
// Function : Areas 
// Purpose  :
//=====================================================
void Select3D_SensitiveSegment
::Areas(SelectBasics_ListOfBox2d& theareas)
{
//  gp_Dir2d dy (0.,1.);
  gp_Pnt2d aPStart(myprojstart.x,myprojstart.y);
  if(aPStart.Distance(myprojend)<=Precision::Confusion()){
      Bnd_Box2d curbox;
      curbox.Set(myprojstart);
      theareas.Append(curbox);
    }
  else {
    gp_Vec2d MyVec(myprojstart,myprojend);//,VAxx(gp_Dir2d(0.,1.));
    Standard_Real theangle = Abs(gp_Dir2d(0.,1.).Angle(gp_Vec2d(myprojstart,myprojend)));
    if(theangle>=PI/2.) theangle-=PI/2;
    if(theangle>=PI/12. && theangle <=5*PI/12.)
      {
	TColgp_Array1OfPnt2d BoxPoint (1,mymaxrect+1);
	BoxPoint (1) = myprojstart;
        BoxPoint(mymaxrect+1)=myprojend;
	gp_Vec2d Vtr = MyVec/mymaxrect;
//	for (Standard_Integer i=2;i<=mymaxrect;i++)
	Standard_Integer i;
	for ( i=2;i<=mymaxrect;i++)
	  {BoxPoint (i) = BoxPoint (i-1).Translated(Vtr);}
	for (i=2;i<=mymaxrect+1;i++)
	  { Bnd_Box2d curbox;
	    curbox.Set(BoxPoint(i-1));
	    curbox.Add(BoxPoint(i));
	    theareas.Append(curbox);
	  }
      }
    else
      {
	Bnd_Box2d curbox;
	curbox.Set(myprojstart);
	curbox.Add(myprojend);
	theareas.Append(curbox);
      }
  }
}

//=====================================================
// Function : Matches
// Purpose  :
//=====================================================
Standard_Boolean Select3D_SensitiveSegment
::Matches(const Standard_Real X,
	  const Standard_Real Y,
	  const Standard_Real aTol,
	  Standard_Real&  DMin)
{
  gp_Pnt2d aPStart(myprojstart.x,myprojstart.y);
  gp_Pnt2d aPEnd(myprojend.x,myprojend.y);
  if ( ! SelectBasics_BasicTool::MatchSegment (aPStart, aPEnd, X, Y, aTol, DMin) )
    return Standard_False;
  return Select3D_SensitiveEntity::Matches (X, Y, aTol, DMin); // compute and validate depth
}

Standard_Boolean Select3D_SensitiveSegment::
Matches (const Standard_Real XMin,
	 const Standard_Real YMin,
	 const Standard_Real XMax,
	 const Standard_Real YMax,
	 const Standard_Real aTol)
{
  Bnd_Box2d BoundBox;
  BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
  if(BoundBox.IsOut(myprojstart)) return Standard_False;
  if( BoundBox.IsOut(myprojend)) return Standard_False;
  return Standard_True;
}


//=======================================================================
//function : Matches
//purpose  : 
//=======================================================================

Standard_Boolean Select3D_SensitiveSegment::
Matches (const TColgp_Array1OfPnt2d& aPoly,
	 const Bnd_Box2d& aBox,
	 const Standard_Real aTol)
{ 
  Standard_Real Umin,Vmin,Umax,Vmax;
  aBox.Get(Umin,Vmin,Umax,Vmax);
  CSLib_Class2d aClassifier2d(aPoly,aTol,aTol,Umin,Vmin,Umax,Vmax);

  Standard_Integer RES = aClassifier2d.SiDans(myprojstart);
  if (RES!=1) return Standard_False;

  RES = aClassifier2d.SiDans(myprojend);
  if (RES!=1) return Standard_False;

  return Standard_True;
}


//=======================================================================
//function : GetConnected
//purpose  : 
//=======================================================================
Handle(Select3D_SensitiveEntity) Select3D_SensitiveSegment::GetConnected(const TopLoc_Location& aLoc)  
{
  Handle(Select3D_SensitiveSegment) NiouEnt = new Select3D_SensitiveSegment(myOwnerId,mystart,myend,mymaxrect);
  if(HasLocation()) NiouEnt->SetLocation(Location());
  NiouEnt->UpdateLocation(aLoc);
  return NiouEnt;
}

//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void Select3D_SensitiveSegment::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
{
  S<<"\tSensitivePoint 3D :"<<endl;
  if(HasLocation())
    S<<"\t\tExisting Location"<<endl;
  S<<"\t\t P1 [ "<<mystart.x<<" , "<<mystart.y <<" , "<<mystart.z <<" ]"<<endl;
  S<<"\t\t P2 [ "<<myend.x<<" , "<<myend.y <<" , "<<myend.z <<" ]"<<endl;
  S<<"\t\t maxrect ="<<mymaxrect<<endl;
  
}
//=======================================================================
//function : ComputeDepth
//purpose  : 
//=======================================================================

Standard_Real Select3D_SensitiveSegment::ComputeDepth(const gp_Lin& EyeLine) const
{
#ifdef __BORLANDC__
  gp_Pnt aP0 = mystart.operator gp_Pnt();
  gp_Pnt aP1 = myend.operator gp_Pnt();
#else
  gp_Pnt aP0 = mystart;
  gp_Pnt aP1 = myend;
#endif

  // if segment is degenerated (zero length), just use depth of the end
  gp_XYZ aV = aP1.XYZ() - aP0.XYZ();
  Standard_Real aNorm = aV.Modulus();
  if ( aNorm <= gp::Resolution() )
    return ElCLib::Parameter (EyeLine, aP0);

  // else compute point on segment closest to the line
  gp_Lin aLine (aP0, aV);
  Extrema_ExtElC aTool (aLine, EyeLine, Precision::Angular());
  if ( aTool.IsDone() && ! aTool.IsParallel() )
  {
    for (Standard_Integer i=1; i <= aTool.NbExt(); i++)
    {
      Extrema_POnCurv POL1, POL2;
      aTool.Points (i, POL1, POL2);
      // use point found by extrema only if it is inside segment
      if ( POL1.Parameter() > 0. && POL1.Parameter() < aNorm )
        return ElCLib::Parameter (EyeLine, POL1.Value());
    }
  }

  // if extrema failed or lines are parallel, return closest of the segment ends
  return Min (ElCLib::Parameter (EyeLine, aP0),
              ElCLib::Parameter (EyeLine, aP1));
}