summaryrefslogtreecommitdiff
path: root/src/MeshVS/MeshVS_SensitivePolyhedron.cxx
blob: ed9eddffa1d73a2a0a6d29266b4eafe83bc85e9d (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// File:	MeshVS_SensitivePolyhedron.cxx
// Created:	Fri Jan 21 2005
// Author:	Alexander SOLOVYOV
// Copyright:	 Open CASCADE 2005


#include <MeshVS_SensitivePolyhedron.ixx>

#include <MeshVS_Tool.hxx>
#include <CSLib_Class2d.hxx>
#include <ElCLib.hxx>

//================================================================
// Function : Constructor MeshVS_SensitivePolyhedron
// Purpose  :
//================================================================
MeshVS_SensitivePolyhedron::
MeshVS_SensitivePolyhedron( const Handle( SelectBasics_EntityOwner )& Owner,
                            const TColgp_Array1OfPnt& Nodes,
                            const Handle( MeshVS_HArray1OfSequenceOfInteger )& Topo )
: Select3D_SensitiveEntity( Owner ),  
  myTopo( Topo )
{
  Standard_Integer low = Nodes.Lower(), up = Nodes.Upper(), i;

  myNodes   = new TColgp_HArray1OfPnt  ( low, up );
  for( i=low; i<=up; i++ )
    myNodes->SetValue( i, Nodes.Value( i ) );

  myNodes2d = new TColgp_HArray1OfPnt2d( low, up );
}

//================================================================
// Function : Project
// Purpose  :
//================================================================
void MeshVS_SensitivePolyhedron::Project( const Handle(Select3D_Projector)& aProjector )
{
  Select3D_SensitiveEntity::Project( aProjector );

  if( myNodes.IsNull() || myNodes2d.IsNull() )
    return;

  Standard_Integer low = myNodes->Lower(),
                   up  = myNodes->Upper();

  gp_Pnt   pnt;
  gp_Pnt2d proj;

  Standard_Boolean hasLoc = HasLocation();

  myCenter = gp_XY( 0, 0 );

  for( Standard_Integer i=low; i<=up; i++ )
  {
    pnt = myNodes->Value( i );
    if( !hasLoc )
      aProjector->Project( pnt, proj );
    else
      aProjector->Project( pnt.Transformed( Location().Transformation() ), proj );

    myNodes2d->SetValue( i, proj.XY() );
    myCenter += proj.XY();
  }

  myCenter /= ( up-low+1 );
}

//================================================================
// Function : GetConnected
// Purpose  :
//================================================================
Handle( Select3D_SensitiveEntity ) MeshVS_SensitivePolyhedron::GetConnected
    ( const TopLoc_Location& aLocation )
{
  Handle( MeshVS_SensitivePolyhedron ) NewEnt = new
    MeshVS_SensitivePolyhedron( myOwnerId, myNodes->Array1(), myTopo );

  if( HasLocation() )
    NewEnt->SetLocation( Location() );

  NewEnt->UpdateLocation( aLocation );

  return NewEnt;
}

//================================================================
// Function : sort
// Purpose  :
//================================================================
void sort( Standard_Real& a, Standard_Real& b )
{
  if( a>b )
  {
    Standard_Real temp = a; a = b; b = temp;
  }
}

//================================================================
// Function : Matches
// Purpose  :
//================================================================
Standard_Boolean MeshVS_SensitivePolyhedron::Matches( const Standard_Real X,
                                                      const Standard_Real Y,
                                                      const Standard_Real aTol,
                                                      Standard_Real& DMin )
{
  if( myNodes2d.IsNull() || myTopo.IsNull() )
    return Standard_False;

  Standard_Integer R1  = myTopo->Lower(),
                   R2  = myTopo->Upper(),
                   low = myNodes2d->Lower();

  Standard_Real rTol = aTol*SensitivityFactor();

  Standard_Boolean inside = Standard_False;

  // "odd-even" algorithm: with ray parallel axis of absciss and toward positive
  for( Standard_Integer i=R1; i<=R2 && !inside; i++ )
  {
    Standard_Integer intersect = 0, cur, next, C1 = 1, C2 = myTopo->Value( i ).Length();
    Standard_Real k, b,                         // y=kx+b -- equation of polygon's edge
                  x1, y1, x2, y2, xp;           // auxiliary points

    for( Standard_Integer j=C1; j<=C2; j++ )
    {
      cur = myTopo->Value( i ).Value( j );
      next = myTopo->Value( i ).Value( j<C2 ? j+1 : C1 );

      x1 = myNodes2d->Value( low+cur ).X(),
      y1 = myNodes2d->Value( low+cur ).Y(),
      x2 = myNodes2d->Value( low+next ).X(),
      y2 = myNodes2d->Value( low+next ).Y();

      if( Abs( x2-x1 )<Precision::Confusion() )
      {  
        //vertical edge!!!

        sort( y1, y2 );
        if( Y>=y1-rTol && Y<=y2+rTol && x1>X-rTol )
          intersect++;
      }
      else
      {  
        //inclined edge!!!

        k = ( y2-y1 ) / ( x2-x1 );
        b = y1 - k*x1;

        if( Abs( k )>Precision::Confusion() )
        {
          xp = ( Y-b ) / k; // absciss of point of intersection
          sort( x1, x2 );
          if( xp>=x1 && xp<=x2 && xp>X-rTol )
            intersect++;
        }
      }
    }
    inside = ( intersect%2 ) == 1;
  }

  if( inside )
  {
    return Select3D_SensitiveEntity::Matches( X, Y, aTol, DMin );
  }
  return Standard_False;
}

//================================================================
// Function : Matches
// Purpose  :
//================================================================
Standard_Boolean MeshVS_SensitivePolyhedron::Matches( const Standard_Real XMin,
                                                      const Standard_Real YMin,
                                                      const Standard_Real XMax,
                                                      const Standard_Real YMax,
                                                      const Standard_Real aTol )
{
  Standard_Real rTol = aTol*SensitivityFactor();

  return myCenter.X()>=XMin-rTol && myCenter.X()<=XMax+rTol &&
         myCenter.Y()>=YMin-rTol && myCenter.Y()<=YMax+rTol;
}

//================================================================
// Function : Matches
// Purpose  :
//================================================================
Standard_Boolean MeshVS_SensitivePolyhedron::Matches
    ( const TColgp_Array1OfPnt2d& Polyline,
      const Bnd_Box2d& aBox,
      const Standard_Real aTol )
{
  Standard_Real Umin, Vmin, Umax, Vmax;
  aBox.Get( Umin, Vmin, Umax, Vmax );

  CSLib_Class2d aClassifier2d( Polyline, aTol, aTol, Umin, Vmin, Umax, Vmax );

  Standard_Integer res = aClassifier2d.SiDans( myCenter );
  return( res==1 );
}

//================================================================
// Function : FindIntersection
// Purpose  :
//================================================================
Standard_Real MeshVS_SensitivePolyhedron::FindIntersection
    ( const TColStd_SequenceOfInteger& NodesIndices,
      const gp_Lin& EyeLine) const
{
  Standard_Real val( Precision::Infinite() );
  for( Standard_Integer i=1, n=NodesIndices.Length(); i<=n; i++ )
    val = Min( val, ElCLib::Parameter( 
      EyeLine, myNodes->Value( myNodes->Lower()+NodesIndices.Value( i ) ) ) );

  return val;
}

//================================================================
// Function : ComputeDepth
// Purpose  :
//================================================================
Standard_Real MeshVS_SensitivePolyhedron::ComputeDepth( const gp_Lin& EyeLine ) const
{
  Standard_Real val = Precision::Infinite();

  if( !myTopo.IsNull() )
    for( Standard_Integer i=myTopo->Lower(), up=myTopo->Upper(); i<=up; i++ )
      val = Min( val, FindIntersection( myTopo->Value( i ), EyeLine ) );

  return val;
}

//================================================================
// Function : ComputeSize
// Purpose  :
//================================================================
/*Standard_Real MeshVS_SensitivePolyhedron::ComputeSize() const
{
  Bnd_Box2d aBox;
  GetBox2d( aBox );

  Standard_Real aXmin, aYmin, aXmax, aYmax;
  aBox.Get( aXmin, aYmin, aXmax, aYmax );
  return Abs( ( aXmax-aXmin ) * ( aYmax-aYmin ) );
} */

//================================================================
// Function : Areas
// Purpose  :
//================================================================
void MeshVS_SensitivePolyhedron::Areas( SelectBasics_ListOfBox2d& aResult ) 
{
  Bnd_Box2d aBox;
  GetBox2d( aBox );
  aResult.Append( aBox );
}

//================================================================
// Function : GetBox2d
// Purpose  :
//================================================================
void MeshVS_SensitivePolyhedron::GetBox2d( Bnd_Box2d& aBox ) const
{
  aBox.SetVoid();

  Standard_Real xmin = 0., ymin = 0., xmax = 0., ymax = 0., x, y;
  Standard_Integer low = myNodes2d->Lower(),
                   up  = myNodes2d->Upper();

  if( !myNodes2d.IsNull() )
  {
    xmin = xmax = myNodes2d->Value( low ).X();
    ymin = ymax = myNodes2d->Value( low ).Y();
    for( Standard_Integer i=low+1; i<=up; i++ )
    {
      x = myNodes2d->Value( i ).X();
      y = myNodes2d->Value( i ).Y();
      if( x>xmax )
        xmax = x;
      else if( x<xmin )
        xmin = x;
      if( y>ymax )
        ymax = y;
      else if( y<ymin )
        ymin = y;
    }
  }

  aBox.Update( xmin, ymin, xmax, ymax );
}