summaryrefslogtreecommitdiff
path: root/src/IntPatch/IntPatch_PolyArc.cxx
blob: 1bca940a7547be07e98cc60e86e5cb8b18b550f9 (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
// File:      IntPatch_PolyArc.cxx
// Created:   Wed Jan 27 09:44:03 1993
// Author:    Isabelle GRIGNON
// Copyright: Matra Datavision 1993

#include <IntPatch_PolyArc.ixx>

#include <Standard_ConstructionError.hxx>

inline void MinMax (const Standard_Real a1, const Standard_Real a2,
		    Standard_Real& amin, Standard_Real& amax)
{
  if (a1 < a2) {
    amin = a1; amax = a2;
  }
  else {
    amin = a2; amax = a1;
  }
}

IntPatch_PolyArc::IntPatch_PolyArc(const Handle(Adaptor2d_HCurve2d)& Line ,
				   const Standard_Integer NbSample,
				   const Standard_Real aPdeb,
				   const Standard_Real aPfin,
				   const Bnd_Box2d& BoxOtherPolygon):
				   brise(1,Max(1,NbSample)),
				   param(1,Max(1,NbSample)),
				   offsetx(0.0),
				   offsety(0.0)
{
  Standard_Real Pdeb = aPdeb;
  Standard_Real Pfin = aPfin;
  gp_Pnt2d p2d;
  
  if (Pdeb == RealFirst() || Pfin == RealLast() || NbSample <= 1) {
    Standard_ConstructionError::Raise();
  }
  //----------------------------------------------------------------------
  //-- On veut eviter les cas ou  le present polygone est beaucoup plus 
  //-- grand que l objet en second.
  //-- 
  //-- Par exemple lorsque l objet en second est une ligne de cheminement
  //-- qui contient de nombreux segments (>>100), une fleche nulle 
  //-- et ce polygone quelques segments et une fleche qui contient
  //-- toute la ligne de cheminement. 
  //--
  //-- Dans ce cas (tout un polygone compris dans la zone d influence)
  //-- les calculs deviennent tres longs (N2)
  //----------------------------------------------------------------------
  Standard_Integer IndexInf = NbSample+1;
  Standard_Integer IndexSup = 0;
  
  Standard_Real bx0,by0,bxmin,bxmax,bymin,bymax,r,r2;
  
  BoxOtherPolygon.Get(bxmin,bymin,bxmax,bymax);
  r=(bxmax-bxmin)+(bymax-bymin);
  bx0=(bxmax+bxmin)*0.5;
  by0=(bymax+bymin)*0.5;
  
  Standard_Real Pas;
  Standard_Real X,Y,Xs,Ys,Xm,Ym,XXs,YYs;
  
  r*=0.8;
  r2 = r*r*49.;
  Standard_Integer nbloop=0;
  
  do { 
    nbloop++;
    Pas = (Pfin-Pdeb)/(NbSample-1);
    param(1) = Pdeb;
    Line->D0(Pdeb,p2d);
    Xs = p2d.X(); Ys = p2d.Y();
    brise(1).SetCoord(Xs,Ys);
    
    boite.SetVoid();
    
    boite.Add(brise(1));
    fleche =0.;
    
    for (Standard_Integer i =2; i<=NbSample;i++) {
      param(i) = Pdeb + (i-1)*Pas;
      Line->D0(param(i),p2d);
      X = p2d.X(); Y = p2d.Y();
      brise(i).SetCoord(X,Y);
      XXs = 0.5*(Xs+X);
      YYs = 0.5*(Ys+Y);
      //------------------------------------------------------------
      //-- On recherche le debut et la fin de la zone significative
      //------------------------------------------------------------
      // MSV: (see cda 002 H2) if segment is too large (>>r) we have
      //      a risk to jump through BoxOtherPolygon, therefore we should
      //      check this condition if the first one is failure.
      Standard_Boolean isMidPtInBox = (Abs(bx0-XXs) + Abs(by0-YYs)) < r;
      Standard_Boolean isSegOut = Standard_True;
      if(!isMidPtInBox) {
	Standard_Real d = (X-Xs)*(X-Xs)+(Y-Ys)*(Y-Ys);
	if (d > r2) {
	  Standard_Real xmin,xmax,ymin,ymax;
	  MinMax (Xs,X, xmin,xmax);
	  MinMax (Ys,Y, ymin,ymax);
	  isSegOut = (xmax < bxmin || xmin > bxmax ||
		      ymax < bymin || ymin > bymax);
	}
      }
      if(isMidPtInBox || !isSegOut) { 
	// MSV: take the previous and the next segments too, because of
	//      we check only the middle point (see BUC60946)
	//if(IndexInf>i) IndexInf=i-1;
	//if(IndexSup<i) IndexSup=i;
	if(IndexInf>i) IndexInf=Max(i-2,1);
	if(IndexSup<i) IndexSup=Min(i+1,NbSample);
      }
      
      boite.Add(brise(i));
      Line->D0(param(i)-Pas*0.5,p2d);
      Xm = p2d.X() - XXs;
      Ym = p2d.Y() - YYs;
      Xm = Sqrt(Xm*Xm+Ym*Ym);
      fleche =Max (fleche , Xm);
      Xs = X;
      Ys = Y;
    }
    if(IndexInf > IndexSup) { 
      r+=r; 
      r2 = r*r*49.;
      //-- cout<<" Le rayon : "<<r<<" est insuffisant "<<endl;
    }
    else {
      //----------------------------------------------
      //-- Si le nombre de points significatifs est
      //-- insuffisant, on reechantillonne une fois
      //-- encore
      //----------------------------------------------
      if((IndexSup-IndexInf)<(NbSample/2)) { 
	//-- cout<<" --- On remet ca entre les index "<<IndexInf<<" et "<<IndexSup<<endl;
	nbloop = 10;
	//if(IndexInf>1) IndexInf--;
	//if(IndexSup<NbSample) IndexSup++;
	Pdeb = param(IndexInf); 
	Pfin = param(IndexSup);
	//IndexInf = IndexSup+1;
	IndexInf = NbSample+1;
	IndexSup = 0;
      }
    }
  }
  while((IndexInf > IndexSup) && nbloop<=10); 
  fleche*=1.2;
  if(fleche<0.00000001) 
    fleche = 0.00000001;
  boite.Enlarge(fleche);

  if(Line->Value(aPdeb).Distance(Line->Value(aPfin))<=1e-7) {
    ferme=Standard_True;
  }
  else { 
    ferme=Standard_False;
  }
}

const Bnd_Box2d& IntPatch_PolyArc::Bounding() const {
  return(boite);
}

Standard_Real IntPatch_PolyArc::Error() const {return fleche;}

Standard_Boolean IntPatch_PolyArc::Closed() const { return ferme;}

Standard_Integer IntPatch_PolyArc::NbPoints() const {return brise.Length();}

gp_Pnt2d IntPatch_PolyArc::Point(const Standard_Integer Index ) const 
{ 
  if(offsetx == 0.0 && offsety==0.0) 
    return(brise(Index));
  
  const gp_Pnt2d& P = brise(Index);
  return (gp_Pnt2d(P.X()+offsetx,P.Y()+offsety));
}

Standard_Real IntPatch_PolyArc::Parameter(const Standard_Integer Index ) const
{ return param(Index);}


void IntPatch_PolyArc::SetOffset(const Standard_Real ox,const Standard_Real oy) { 
  Standard_Real xmin,ymin,xmax,ymax,g;
  boite.Get(xmin,ymin,xmax,ymax);
  g = boite.GetGap();
  
  boite.SetVoid();
  
  boite.Update(xmin-offsetx,ymin-offsety,
	       xmax-offsetx,ymax-offsety);
  offsetx = ox;
  offsety = oy;
  boite.Update(xmin+offsetx,ymin+offsety,
	       xmax+offsetx,ymax+offsety);
  boite.SetGap(g);
}