summaryrefslogtreecommitdiff
path: root/src/gce/gce_MakeCirc.cxx
blob: 35cd3ec54fa12a1bd8877733d76ed14221b8998b (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// File:	gce_MakeCirc.cxx
// Created:	Wed Sep  2 10:34:28 1992
// Author:	Remi GILET
//		<reg@sdsun1>

#include <gce_MakeCirc.ixx>

#include <StdFail_NotDone.hxx>

#include <gp.hxx>
#include <gp_Ax1.hxx>
#include <gp_Lin.hxx>

#include <gce_MakeDir.hxx>

#include <Extrema_POnCurv.hxx>
#include <Extrema_ExtElC.hxx>
//=======================================================================
//function : gce_MakeCirc
//purpose  : 
//   Creation d un cercle 3d de gp passant par trois points.              +
//   Trois cas de figures :                                               +
//      1/ Les trois points sont confondus.                               +
//      -----------------------------------                               +
//      Le resultat est le cercle centre en Point1 de rayon zero.         +
//      2/ Deux des trois points sont confondus.                          +
//      ----------------------------------------                          +
//      Pas de solution (Erreur : Points confondus).                      +
//      3/ Les trois points sont distinct.                                +
//      ----------------------------------                                +
//      On cree la mediatrice a P1P2 ainsi que la mediatrice a P1P3.      +
//      La solution a pour centre l intersection de ces deux droite et    +
//      pour rayon la distance entre ce centre et l un des trois points.  +
//=========================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Pnt&  P1 ,
			   const gp_Pnt&  P2 ,
			   const gp_Pnt&  P3) {
//=========================================================================
//   Traitement.                                                          +
//=========================================================================
  Standard_Real dist1, dist2, dist3, aResolution;
  //
  aResolution=gp::Resolution();
  //
  dist1 = P1.Distance(P2);
  dist2 = P1.Distance(P3);
  dist3 = P2.Distance(P3);
  //
  if ((dist1<aResolution) && (dist2<aResolution) && (dist3<aResolution)) {
    gp_Dir Dirx(1.,0.,0.);
    gp_Dir Dirz(0.,0.,1.);
    TheCirc = gp_Circ(gp_Ax2(P1,Dirx,Dirz), 0.);
    return;
  }
  if (!(dist1 >= aResolution && dist2 >= aResolution)) {
    TheError = gce_ConfusedPoints;
    return;
  }
  //
  Standard_Real x1,y1,z1,x2,y2,z2,x3,y3,z3;
  //
  P1.Coord(x1,y1,z1);
  P2.Coord(x2,y2,z2);
  P3.Coord(x3,y3,z3);
  gp_Dir Dir1(x2-x1,y2-y1,z2-z1);
  gp_Dir Dir2(x3-x2,y3-y2,z3-z2);
  //
  gp_Ax1 anAx1(P1, Dir1);
  gp_Lin aL12 (anAx1);
  if (aL12.Distance(P3)<aResolution) {
    TheError = gce_ColinearPoints;
    return;
  }
  //
  gp_Dir Dir3 = Dir1.Crossed(Dir2);
  //
  gp_Dir dir = Dir1.Crossed(Dir3);
  gp_Lin L1(gp_Pnt((P1.XYZ()+P2.XYZ())/2.),dir);
  dir = Dir2.Crossed(Dir3);
  gp_Lin L2(gp_Pnt((P3.XYZ()+P2.XYZ())/2.),dir);

  Standard_Real Tol = 0.000000001;
  Extrema_ExtElC distmin(L1,L2,Tol);
  
  if (!distmin.IsDone()) { 
    TheError = gce_IntersectionError; 
  }
  else {
    Standard_Integer nbext;
    //
    //
    if (distmin.IsParallel()) {
      TheError = gce_IntersectionError;
      return;
    }
    nbext = distmin.NbExt();
    //
    //
    if (nbext == 0) {
      TheError = gce_IntersectionError; 
    }
    else {
      Standard_Real TheDist = RealLast();
      gp_Pnt pInt,pon1,pon2;
      Standard_Integer i = 1;
      Extrema_POnCurv Pon1,Pon2;
      while (i<=nbext) {
	if (distmin.SquareDistance(i)<TheDist) {
	  TheDist = distmin.SquareDistance(i);
	  distmin.Points(i,Pon1,Pon2);
	  pon1 = Pon1.Value();
	  pon2 = Pon2.Value();
	  pInt = gp_Pnt((pon1.XYZ()+pon2.XYZ())/2.);
	}
	i++;
      }
      //modified by NIZNHY-PKV Thu Mar  3 11:30:34 2005f
      //Dir2.Cross(Dir1);
      //modified by NIZNHY-PKV Thu Mar  3 11:30:37 2005t
      dist1 = P1.Distance(pInt);
      dist2 = P2.Distance(pInt);
      dist3 = P3.Distance(pInt);
      pInt.Coord(x3,y3,z3);
      Dir1 = gp_Dir(x1-x3,y1-y3,z1-z3);
      //modified by NIZNHY-PKV Thu Mar  3 11:31:11 2005f
      //Dir2 = gp_Dir(x2-x3,y2-y3,z2-z3);
      //modified by NIZNHY-PKV Thu Mar  3 11:31:13 2005t
      //
      TheCirc = gp_Circ(gp_Ax2(pInt, Dir3, Dir1),(dist1+dist2+dist3)/3.);
      TheError = gce_Done;
    }
  }
}
//=======================================================================
//function : gce_MakeCirc
//purpose  : 
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Ax2&       A2      ,
			   const Standard_Real Radius  ) {
  if (Radius < 0.) { 
    TheError = gce_NegativeRadius;
  }
  else {
    TheError = gce_Done;
    TheCirc = gp_Circ(A2,Radius);
  }
}
//=========================================================================
//   Creation d un gp_Circ par son centre <Center>, son plan <Plane> et   +
//   son rayon <Radius>.                                                  +
//=========================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Pnt&       Center  ,
			   const gp_Pln&       Plane   ,
			   const Standard_Real Radius  ) {
  gce_MakeCirc C = gce_MakeCirc(Center,Plane.Position().Direction(),Radius);
  TheCirc = C.Value();
  TheError = C.Status();
}
//=======================================================================
//function : gce_MakeCirc
//purpose  : Creation d un gp_Circ par son centre <Center>, 
//sa normale <Norm> et son rayon <Radius>.  
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Pnt&       Center  ,
			   const gp_Dir&       Norm    ,
			   const Standard_Real Radius  ) {
   if (Radius < 0.) { 
     TheError = gce_NegativeRadius;
   }
   else {
     Standard_Real A = Norm.X();
     Standard_Real B = Norm.Y();
     Standard_Real C = Norm.Z();
     Standard_Real Aabs = Abs(A);
     Standard_Real Babs = Abs(B);
     Standard_Real Cabs = Abs(C);
     gp_Ax2 Pos;

//=========================================================================
//  pour determiner l'axe X :                                             +
//  on dit que le produit scalaire Vx.Norm = 0.                           +
//  et on recherche le max(A,B,C) pour faire la division.                 +
//  l'une des coordonnees du vecteur est nulle.                           +
//=========================================================================

     if( Babs <= Aabs && Babs <= Cabs) {
       if(Aabs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-C,0.,A)); }
       else { Pos = gp_Ax2(Center,Norm,gp_Dir(C,0.,-A)); }
     }
     else if( Aabs <= Babs && Aabs <= Cabs) {
       if(Babs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,-C,B)); }
       else { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,C,-B)); }
     }
     else {
       if(Aabs > Babs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-B,A,0.)); }
       else { Pos = gp_Ax2(Center,Norm,gp_Dir(B,-A,0.)); }
     }
     TheCirc = gp_Circ(Pos,Radius);
     TheError = gce_Done;
   }
 }
//=======================================================================
//function : gce_MakeCirc
//purpose  :  Creation d un gp_Circ par son centre <Center>, 
// sa normale <Ptaxis> et  son rayon <Radius>
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Pnt&       Center  ,
			   const gp_Pnt&       Ptaxis  ,
			   const Standard_Real Radius  ) {
   if (Radius < 0.) { 
     TheError = gce_NegativeRadius;
   }
   else {
     if (Center.Distance(Ptaxis) <= gp::Resolution()) {
       TheError = gce_NullAxis;
     }
     else {
       Standard_Real A = Ptaxis.X()-Center.X();
       Standard_Real B = Ptaxis.Y()-Center.Y();
       Standard_Real C = Ptaxis.Z()-Center.Z();
       Standard_Real Aabs = Abs(A);
       Standard_Real Babs = Abs(B);
       Standard_Real Cabs = Abs(C);
       gp_Ax2 Pos;

//=========================================================================
//  pour determiner l'axe X :                                             +
//  on dit que le produit scalaire Vx.Norm = 0.                           +
//  et on recherche le max(A,B,C) pour faire la division.                 +
//  l'une des coordonnees du vecteur est nulle.                           +
//=========================================================================

       gp_Dir Norm = gce_MakeDir(Center,Ptaxis);
       if( Babs <= Aabs && Babs <= Cabs) {
	 if(Aabs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-C,0.,A)); }
	 else { Pos = gp_Ax2(Center,Norm,gp_Dir(C,0.,-A)); }
       }
       else if( Aabs <= Babs && Aabs <= Cabs) {
	 if(Babs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,-C,B)); }
	 else { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,C,-B)); }
       }
       else {
	 if(Aabs > Babs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-B,A,0.)); }
	 else { Pos = gp_Ax2(Center,Norm,gp_Dir(B,-A,0.)); }
       }
       TheCirc = gp_Circ(Pos,Radius);
       TheError = gce_Done;
     }
   }
 }
//=======================================================================
//function : gce_MakeCirc
//purpose  : Creation d un gp_Circ par son axe <Axis> et son rayon <Radius>.  
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Ax1&       Axis    ,
			   const Standard_Real Radius  ) 
{
  if (Radius < 0.) { 
    TheError = gce_NegativeRadius;
  }
  else {
    gp_Dir Norm(Axis.Direction());
    gp_Pnt Center(Axis.Location());
    Standard_Real A = Norm.X();
    Standard_Real B = Norm.Y();
    Standard_Real C = Norm.Z();
    Standard_Real Aabs = Abs(A);
    Standard_Real Babs = Abs(B);
    Standard_Real Cabs = Abs(C);
    gp_Ax2 Pos;

//=========================================================================
//  pour determiner l'axe X :                                             +
//  on dit que le produit scalaire Vx.Norm = 0.                           +
//  et on recherche le max(A,B,C) pour faire la division.                 +
//  l'une des coordonnees du vecteur est nulle.                           +
//=========================================================================

    if( Babs <= Aabs && Babs <= Cabs) {
      if(Aabs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-C,0.,A)); }
      else { Pos = gp_Ax2(Center,Norm,gp_Dir(C,0.,-A)); }
    }
    else if( Aabs <= Babs && Aabs <= Cabs) {
      if(Babs > Cabs) { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,-C,B)); }
      else { Pos = gp_Ax2(Center,Norm,gp_Dir(0.,C,-B)); }
    }
    else {
      if(Aabs > Babs) { Pos = gp_Ax2(Center,Norm,gp_Dir(-B,A,0.)); }
      else { Pos = gp_Ax2(Center,Norm,gp_Dir(B,-A,0.)); }
    }
    TheCirc = gp_Circ(Pos,Radius);
    TheError = gce_Done;
  }
}
//=======================================================================
//function : gce_MakeCirc
//purpose  : Creation d un gp_Circ concentrique a un autre gp_circ a une distance +
//   donnee.                
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Circ&      Circ    ,
			   const Standard_Real Dist    )
{
  Standard_Real Rad = Circ.Radius()+Dist;
  if (Rad < 0.) { 
    TheError = gce_NegativeRadius;
  }
  else {
    TheCirc = gp_Circ(Circ.Position(),Rad);
    TheError = gce_Done;
  }
}
//=======================================================================
//function : gce_MakeCirc
//purpose  : Creation d un gp_Circ concentrique a un autre gp_circ dont le rayon 
//   est egal a la distance de <Point> a l axe de <Circ>. 
//=======================================================================
gce_MakeCirc::gce_MakeCirc(const gp_Circ& Circ ,
			   const gp_Pnt&  P    ) 
{
  Standard_Real Rad = gp_Lin(Circ.Axis()).Distance(P);
  TheCirc = gp_Circ(Circ.Position(),Rad);
  TheError = gce_Done;
}
//=======================================================================
//function : Value
//purpose  : 
//=======================================================================
const gp_Circ& gce_MakeCirc::Value() const
{ 
  StdFail_NotDone_Raise_if(!TheError == gce_Done,"");
  return TheCirc;
}
//=======================================================================
//function : Operator
//purpose  : 
//=======================================================================
const gp_Circ& gce_MakeCirc::Operator() const 
{
  return Value();
}
//=======================================================================
//function : operator
//purpose  : 
//=======================================================================
gce_MakeCirc::operator gp_Circ() const
{
  return Value();
}