summaryrefslogtreecommitdiff
path: root/src/ChFi3d/ChFi3d.cxx
blob: 668ab722e44f365bdf2834f36fb1330b527ca11b (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
// File:	ChFi3d.cxx
// Created:	Tue Dec 21 16:19:36 1993
// Author:	Isabelle GRIGNON
//		<isg@sdsun2>


#include <ChFi3d.ixx>
#include <ChFi3d_Builder_0.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepAdaptor_Curve2d.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRep_Tool.hxx>

#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Vec2d.hxx>
#include <Precision.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>


//=======================================================================
//function : ConcaveSide
//purpose  : calculate the concave face at the neighborhood of the border of
//           2 faces.
//=======================================================================

Standard_Integer ChFi3d::ConcaveSide(const BRepAdaptor_Surface& S1, 
				     const BRepAdaptor_Surface& S2, 
				     const TopoDS_Edge& E, 
				     TopAbs_Orientation& Or1, 
				     TopAbs_Orientation& Or2)

{
  Standard_Integer ChoixConge;
  Or1 = Or2 = TopAbs_FORWARD;
  BRepAdaptor_Curve CE(E);
  Standard_Real first = CE.FirstParameter();
  Standard_Real last = CE.LastParameter();
  Standard_Real par = 0.691254*first + 0.308746*last;

  gp_Pnt pt, pt1, pt2; gp_Vec tgE, tgE1, tgE2, ns1, ns2, dint1, dint2;
  TopoDS_Face F1 = S1.Face();
  TopoDS_Face F2 = S2.Face();
  F1.Orientation(TopAbs_FORWARD);
  F2.Orientation(TopAbs_FORWARD);
  
  CE.D1(par,pt,tgE);
  tgE.Normalize();
  tgE2 = tgE1 = tgE;
  if(E.Orientation() == TopAbs_REVERSED) tgE.Reverse();

  TopoDS_Edge E1 = E, E2 = E;
  E1.Orientation(TopAbs_FORWARD);
  E2.Orientation(TopAbs_FORWARD);

  if(F1.IsSame(F2) && BRep_Tool::IsClosed(E,F1)) {
    E2.Orientation(TopAbs_REVERSED);
    tgE2.Reverse();
  }
  else {
    TopExp_Explorer Exp;
    Standard_Boolean found = 0;
    for (Exp.Init(F1,TopAbs_EDGE); 
	 Exp.More() && !found; 
	 Exp.Next()) {
      if (E.IsSame(TopoDS::Edge(Exp.Current()))){
	if(Exp.Current().Orientation() == TopAbs_REVERSED) tgE1.Reverse();
	found = Standard_True;
      }
    }
    if (!found) { return 0; }
    found = 0;
    for (Exp.Init(F2,TopAbs_EDGE); 
	 Exp.More() && !found; 
	 Exp.Next()) {
      if (E.IsSame(TopoDS::Edge(Exp.Current()))){
	if(Exp.Current().Orientation() == TopAbs_REVERSED) tgE2.Reverse();
	found = Standard_True;
      }
    }
    if (!found) { return 0; }
  }
  BRepAdaptor_Curve2d pc1(E1,F1);
  BRepAdaptor_Curve2d pc2(E2,F2);
  gp_Pnt2d p2d1,p2d2;
  gp_Vec DU1,DV1,DU2,DV2;
  p2d1 = pc1.Value(par);
  p2d2 = pc2.Value(par);
  S1.D1(p2d1.X(),p2d1.Y(),pt1,DU1,DV1);
  ns1 = DU1.Crossed(DV1);
  ns1.Normalize();
  S2.D1(p2d2.X(),p2d2.Y(),pt2,DU2,DV2);
  ns2 = DU2.Crossed(DV2);
  ns2.Normalize();

  dint1 = ns1.Crossed(tgE1);
  dint2 = ns2.Crossed(tgE2);
  Standard_Real ang = ns1.CrossMagnitude(ns2);
  if(ang > 0.0001*PI){
    Standard_Real scal = ns2.Dot(dint1);
    if ( scal <= 0. ){
      ns2.Reverse();
      Or2 = TopAbs_REVERSED; 
    }
    scal = ns1.Dot(dint2);
    if ( scal <= 0. ){
      ns1.Reverse();
      Or1 = TopAbs_REVERSED; 
    }
  }
  else { 
    //the faces are locally tangent - this is fake!
    if(dint1.Dot(dint2) < 0.){
      //This is a forgotten regularity
      gp_Vec DDU, DDV, DDUV;
      S1.D2(p2d1.X(),p2d1.Y(),pt1,DU1,DV1,DDU,DDV,DDUV);
      DU1 += ( DU1 * dint1 < 0) ? -DDU : DDU;
      DV1 += ( DV1 * dint1 < 0) ? -DDV : DDV;
      ns1 = DU1.Crossed(DV1);
      ns1.Normalize();
      S2.D2(p2d2.X(),p2d2.Y(),pt2,DU2,DV2,DDU,DDV,DDUV);
      DU2 += ( DU2 * dint2 < 0) ? -DDU : DDU;
      DV2 += ( DV2 * dint2 < 0) ? -DDV : DDV;
      ns2 = DU2.Crossed(DV2);
      ns2.Normalize();
      
      dint1 = ns1.Crossed(tgE1);
      dint2 = ns2.Crossed(tgE2);
      ang = ns1.CrossMagnitude(ns2);
      if(ang > 0.0001*PI){
        Standard_Real scal = ns2.Dot(dint1);
        if ( scal <= 0. ){
          ns2.Reverse();
          Or2 = TopAbs_REVERSED; 
        }
        scal = ns1.Dot(dint2);
        if ( scal <= 0. ){
          ns1.Reverse();
          Or1 = TopAbs_REVERSED; 
        }
      }
      else {
#ifdef DEB
        cout<<"ConcaveSide : no concave face"<<endl;
#endif
	//This 10 shows that the face at end is in the extension of one of two base faces
	return 10;
      }
    }
    else {
      //here it turns back, the points are taken in faces
      //neither too close nor too far as much as possible.
      Standard_Real u,v;
#ifdef DEB
//      Standard_Real deport = 1000*BRep_Tool::Tolerance(E);
#endif
      ChFi3d_Coefficient(dint1,DU1,DV1,u,v);
      p2d1.SetX(p2d1.X() + u); p2d1.SetY(p2d1.Y() + v);
      ChFi3d_Coefficient(dint1,DU2,DV2,u,v);
      p2d2.SetX(p2d2.X() + u); p2d2.SetY(p2d2.Y() + v);
      S1.D1(p2d1.X(),p2d1.Y(),pt1,DU1,DV1);
      ns1 = DU1.Crossed(DV1);
      S2.D1(p2d2.X(),p2d2.Y(),pt2,DU2,DV2);
      ns2 = DU2.Crossed(DV2);
      gp_Vec vref(pt1,pt2);
      if(ns1.Dot(vref) < 0.){
	Or1 = TopAbs_REVERSED;
      }
      if(ns2.Dot(vref) > 0.){
	Or2 = TopAbs_REVERSED;
      }
    }
  }


  if (Or1 == TopAbs_FORWARD) {
    if (Or2 == TopAbs_FORWARD) ChoixConge = 1;
    else ChoixConge = 7;
  }
  else {
    if (Or2 == TopAbs_FORWARD) ChoixConge = 3;
    else ChoixConge = 5;
  }
  if ((ns1.Crossed(ns2)).Dot(tgE) >= 0.) ChoixConge++ ;
  return ChoixConge;
}

//=======================================================================
//function : NextSide
//purpose  : 
//           
//=======================================================================

Standard_Integer  ChFi3d::NextSide(TopAbs_Orientation& Or1, 
				   TopAbs_Orientation& Or2,
				   const TopAbs_Orientation OrSave1, 
				   const TopAbs_Orientation OrSave2,
				   const Standard_Integer ChoixSave)
{
  if (Or1 == TopAbs_FORWARD){Or1 = OrSave1;}
  else {
    Or1 = TopAbs::Reverse(OrSave1);
  }
  if (Or2 == TopAbs_FORWARD){Or2 = OrSave2;}
  else {
    Or2 = TopAbs::Reverse(OrSave2);
  }

  Standard_Integer ChoixConge;
  if (Or1 == TopAbs_FORWARD) {
    if (Or2 == TopAbs_FORWARD) ChoixConge = 1;
    else {
      if(ChoixSave < 0) ChoixConge = 3;
      else ChoixConge = 7;
    }
  }
  else {
    if (Or2 == TopAbs_FORWARD) {
      if(ChoixSave < 0) ChoixConge = 7;
      else ChoixConge = 3;
    }
    else ChoixConge = 5;
  }
  if (Abs(ChoixSave)%2 == 0) ChoixConge++;
  return ChoixConge;
}


//=======================================================================
//function : NextSide
//purpose  : 
//           
//=======================================================================

void ChFi3d::NextSide(TopAbs_Orientation& Or, 
		     const TopAbs_Orientation OrSave, 
		     const TopAbs_Orientation OrFace) 
{
  if (Or == OrFace){Or = OrSave;}
  else {
    Or = TopAbs::Reverse(OrSave);
  }
}



//=======================================================================
//function : SameSide
//purpose  : 
//           
//=======================================================================

Standard_Boolean  ChFi3d::SameSide(const TopAbs_Orientation Or, 
				   const TopAbs_Orientation OrSave1, 
				   const TopAbs_Orientation OrSave2,
				   const TopAbs_Orientation OrFace1, 
				   const TopAbs_Orientation OrFace2)
{
  TopAbs_Orientation o1,o2;
  if (Or == OrFace1){o1 = OrSave1;}
  else {
    o1 = TopAbs::Reverse(OrSave1);
  }
  if (Or == OrFace2){o2 = OrSave2;}
  else {
    o2 = TopAbs::Reverse(OrSave2);
  }
  return (o1 == o2);
}