0024784: Move documentation in CDL files to proper location
[occt.git] / src / SelectBasics / SelectBasics_BasicTool.cxx
1 // Created on: 1995-06-08
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <SelectBasics_BasicTool.ixx>
18 #include <Precision.hxx>
19 #include <gp_Vec2d.hxx>
20
21
22 //==================================================
23 // Function: 
24 // Purpose :
25 //==================================================
26
27 Standard_Boolean SelectBasics_BasicTool::
28 MatchSegments(const gp_Pnt2d & A,
29               const gp_Pnt2d & B,
30               const gp_Pnt2d & C,
31               const gp_Pnt2d & D)
32 {
33
34   Standard_Real d[6],det,deta,detb;
35
36   if(Max(A.X(),B.X())<Min(C.X(),D.X())) return Standard_False;
37   if(Min(A.X(),B.X())>Max(C.X(),D.X())) return Standard_False;
38   if(Max(A.Y(),B.Y())<Min(C.Y(),D.Y())) return Standard_False;
39   if(Min(A.Y(),B.Y())>Max(C.Y(),D.Y())) return Standard_False;
40   
41   d[0] = B.X()-A.X();d[1]=C.X()-D.X();d[2]=C.X()-A.X();
42   d[3] = B.Y()-A.Y();d[4]=C.Y()-D.Y();d[5]=C.Y()-A.Y();
43   
44   det  = d[0]*d[4]-d[3]*d[1];
45   deta = d[4]*d[2]-d[5]*d[1];
46   detb = d[0]*d[5]-d[3]*d[2];
47
48   if(Abs(det)<=Precision::Confusion()) return Standard_False;
49   if(deta/det<Precision::Confusion()) return Standard_False;
50   if(deta/det>1+Precision::Confusion()) return Standard_False;
51   if(detb/det<Precision::Confusion()) return Standard_False;
52   if(detb/det>1+Precision::Confusion()) return Standard_False;
53
54   return Standard_True;
55 }
56
57
58
59 //==================================================
60 // Function: MatchSegment
61 // Purpose : Return True if Segment(pBegin, pEnd) is Selected
62 //==================================================
63 Standard_Boolean SelectBasics_BasicTool::MatchSegment(const gp_Pnt2d& pBegin,const gp_Pnt2d& pEnd,
64                                                       const Standard_Real X,
65                                                       const Standard_Real Y,
66                                                       const Standard_Real aTol,
67                                                       Standard_Real& DMin)
68 {
69   const Standard_Real SqTol = aTol * aTol;      
70   gp_Vec2d AB, AC, BC; 
71   const gp_Pnt2d apoint(X,Y);
72   
73   AB.SetCoord(pEnd.X()-pBegin.X(),pEnd.Y()-pBegin.Y());
74   AC.SetCoord(X-pBegin.X(),Y-pBegin.Y());
75   BC.SetCoord(pEnd.X()-X,pEnd.Y()-Y);
76   
77   //1. Check the ends, do not estimate distance to the segment itself here
78   if((apoint.SquareDistance(pBegin)<SqTol) ||
79      (apoint.SquareDistance(pEnd)<SqTol)){
80     DMin = 0.; 
81     return Standard_True;
82   }
83
84   //2. Checking if the mouse point projection onto the segment`s line
85   //   falls inside the segment.
86   if(AB.Dot(AC)>=0. && AB.Dot(BC)>=0.){
87     //3. Estimate distance from the mouse point to the segment 
88     //   if length of segment exceeds tolerance
89     const Standard_Real aSegLen = AB.Magnitude();
90     if (aSegLen>aTol){
91       DMin=Abs(AB.Crossed(gp_Vec2d(pBegin,apoint))/aSegLen);
92       if (DMin<aTol){
93           return Standard_True;
94         }
95     }
96   }
97     
98   return Standard_False;
99
100
101
102
103 //==================================================
104 // Function: 
105 // Purpose :
106 //==================================================
107
108 Standard_Boolean SelectBasics_BasicTool:: 
109 AutoInter (const TColgp_Array1OfPnt2d& points)
110 {
111   for (Standard_Integer i=3;i<=points.Length()-1;i++)
112     {     
113       for (Standard_Integer j=1;j<=i-2;j++)
114         {
115           if (MatchSegments (points(i),
116                              points(i+1),
117                              points(j),
118                              points(j+1))) return Standard_True;
119         }
120     }
121   return Standard_False;
122 }
123
124
125 //==================================================
126 // Function: 
127 // Purpose :
128 //==================================================
129
130 Standard_Boolean SelectBasics_BasicTool::
131 MatchPolyg2d (const TColgp_Array1OfPnt2d& tabpoint,
132               const Standard_Real X,
133               const Standard_Real Y,
134               const Standard_Real aTol,
135               Standard_Real& DMin,
136               Standard_Integer& Rank)
137 {
138         Rank =0;
139         Standard_Boolean Found= Standard_False;
140
141         //In the cycle towarded enumeration of possibilities segment, which is selected from wire
142         for(Standard_Integer i=tabpoint.Lower();i<=tabpoint.Upper()-1&& !Found;i++)
143         {       
144                 if(MatchSegment(tabpoint.Value(i),tabpoint.Value(i+1),X,Y,aTol,DMin))
145                 {
146                         Rank=i;
147                         return Standard_True;
148                 }       
149         }
150         return Standard_False;
151 }