summaryrefslogtreecommitdiff
path: root/src/LocOpe/LocOpe.cxx
blob: d9a4b23aaf2d4745a684035553fbc3d71b8b9a18 (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
// File:	LocOpe.cxx
// Created:	Tue Apr  2 10:44:51 1996
// Author:	Jacques GOUSSARD
//		<jag@bravox>


#include <LocOpe.ixx>

#include <TopExp_Explorer.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <Geom2d_Curve.hxx>
#include <gp_Pnt2d.hxx>
#include <BRepAdaptor_Curve2d.hxx>
#include <BRepAdaptor_HSurface.hxx>
#include <BRepAdaptor_HCurve2d.hxx>
#include <Geom2dInt_GInter.hxx>
#include <Geom_Curve.hxx>

#include <TopTools_MapOfShape.hxx>

#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <Precision.hxx>

#define NECHANT 10


//=======================================================================
//function : Closed
//purpose  : 
//=======================================================================

Standard_Boolean LocOpe::Closed(const TopoDS_Wire& W,
				const TopoDS_Face& F)
{
  TopoDS_Vertex Vf,Vl;
  TopExp::Vertices(W,Vf,Vl);
  if (!Vf.IsSame(Vl)) {
    return Standard_False;
  }

  // On recherche l`edge contenant Vf FORWARD

  TopExp_Explorer exp,exp2;
  for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
       exp.More();exp.Next()) {
    for (exp2.Init(exp.Current(),TopAbs_VERTEX);exp2.More();exp2.Next()) {
      if (exp2.Current().IsSame(Vf) && 
	  exp2.Current().Orientation() == TopAbs_FORWARD) {
	break;
      }
    }
    if (exp2.More()) {
      break;
    }
  }
  TopoDS_Edge Ef = TopoDS::Edge(exp.Current());
  
  // On recherche l`edge contenant Vl REVERSED

  for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
       exp.More();exp.Next()) {
    for (exp2.Init(exp.Current(),TopAbs_VERTEX);exp2.More();exp2.Next()) {
      if (exp2.Current().IsSame(Vl) && 
	  exp2.Current().Orientation() == TopAbs_REVERSED) {
	break;
      }
    }
    if (exp2.More()) {
      break;
    }
  }
  TopoDS_Edge El = TopoDS::Edge(exp.Current());
  
  Standard_Real f,l;
  gp_Pnt2d pf,pl;
  Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(Ef,F,f,l);
  if (Ef.Orientation() == TopAbs_FORWARD) {
    pf = C2d->Value(f);
  }
  else {
    pf = C2d->Value(l);
  }
  C2d = BRep_Tool::CurveOnSurface(El,F,f,l);
  if (El.Orientation() == TopAbs_FORWARD) {
    pl = C2d->Value(l);
  }
  else {
    pl = C2d->Value(f);
  }

  if (pf.Distance(pl) <= Precision::PConfusion(Precision::Confusion())) {
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : Closed
//purpose  : 
//=======================================================================

Standard_Boolean LocOpe::Closed(const TopoDS_Edge& E,
				const TopoDS_Face& F)
{
  BRep_Builder B;
  TopoDS_Wire W;
  B.MakeWire(W);
  B.Add(W,E.Oriented(TopAbs_FORWARD));
  return LocOpe::Closed(W,F);
}



//=======================================================================
//function : Closed
//purpose  : 
//=======================================================================

Standard_Boolean LocOpe::TgtFaces(const TopoDS_Edge& E,
				  const TopoDS_Face& F1,
				  const TopoDS_Face& F2)
{
  BRepAdaptor_Surface bs(F1, Standard_False);
  Standard_Real u;
  Standard_Real ta = 0.0001;

  TopoDS_Edge e = E;

  Handle(BRepAdaptor_HSurface) HS1 = new BRepAdaptor_HSurface(F1);
  Handle(BRepAdaptor_HSurface) HS2 = new BRepAdaptor_HSurface(F2);
  e.Orientation(TopAbs_FORWARD);
  Handle(BRepAdaptor_HCurve2d) HC2d = new BRepAdaptor_HCurve2d();
  Handle(BRepAdaptor_HCurve2d) HC2d2 = new BRepAdaptor_HCurve2d();
  HC2d->ChangeCurve2d().Initialize(e,F1);
  HC2d2->ChangeCurve2d().Initialize(e,F2);
  

//  Adaptor3d_CurveOnSurface C1(HC2d,HS1);

  Standard_Boolean rev1 = (F1.Orientation() == TopAbs_REVERSED);
  Standard_Boolean rev2 = (F2.Orientation() == TopAbs_REVERSED);
  Standard_Real f,l,eps,angmin = PI,angmax = -PI,ang;
  BRep_Tool::Range(e,f,l);

  eps = (l - f)/100.;
  f += eps; // pour eviter de faire des calculs sur les 
  l -= eps; // pointes des carreaux pointus.
  gp_Pnt2d p;
  gp_Pnt pp1;
  gp_Vec du,dv;
  gp_Vec d1,d2;

  Standard_Real uu, vv;

  Standard_Integer i;
  for(i = 0; i <= 20; i++){
    u = f + (l-f)*i/20;
    HC2d->D0(u,p);
    HS1->D1(p.X(),p.Y(),pp1,du,dv);
    d1 = (du.Crossed(dv)).Normalized();
    if(rev1) d1.Reverse();
    HC2d2->D0(u,p);
    p.Coord(uu,vv);
    HS2->D1(uu,vv,pp1,du,dv);
    d2 = (du.Crossed(dv)).Normalized();
    if(rev2) d2.Reverse();
    ang = d1.Angle(d2);
    if(ang <= angmin) angmin = ang;
    if(ang >= angmax) angmax = ang;
  }
  return (angmax<=ta);
}


//=======================================================================
//function : SampleEdges
//purpose  : 
//=======================================================================

void LocOpe::SampleEdges(const TopoDS_Shape& theShape,
			 TColgp_SequenceOfPnt& theSeq)
{
  theSeq.Clear();
  TopTools_MapOfShape theMap;
  
  TopExp_Explorer exp(theShape,TopAbs_EDGE);
  TopLoc_Location Loc;
  Handle(Geom_Curve) C;
  Standard_Real f,l,prm;
  Standard_Integer i;

  // Computes points on edge, but does not take the extremities into account
  for (; exp.More(); exp.Next()) {
    const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
    if (!theMap.Add(edg)) {
      continue;
    }
    if (!BRep_Tool::Degenerated(edg)) {
      C = BRep_Tool::Curve(edg,Loc,f,l);
      C = Handle(Geom_Curve)::
	DownCast(C->Transformed(Loc.Transformation()));
      Standard_Real delta = (l - f)/NECHANT*0.123456;
      for (i=1; i<NECHANT; i++) {
	prm = delta + ((NECHANT-i)*f+i*l)/NECHANT;
	theSeq.Append(C->Value(prm));
      }
    }
  }

  // Adds every vertex
  for (exp.Init(theShape,TopAbs_VERTEX); exp.More(); exp.Next()) {
    if (theMap.Add(exp.Current())) {
      theSeq.Append(BRep_Tool::Pnt(TopoDS::Vertex(exp.Current())));
    }
  }
}





/*
Standard_Boolean LocOpe::IsInside(const TopoDS_Face& F1,
				  const TopoDS_Face& F2)
{
  Standard_Boolean Result = Standard_True;

  TopExp_Explorer exp1, exp2;

  for(exp1.Init(F1, TopAbs_EDGE); exp1.More(); exp1.Next())  {
    TopoDS_Edge e1 = TopoDS::Edge(exp1.Current());
    BRepAdaptor_Curve2d C1(e1, F1); 
    for(exp2.Init(F2, TopAbs_EDGE); exp2.More(); exp2.Next())  {
      TopoDS_Edge e2 = TopoDS::Edge(exp2.Current());
      BRepAdaptor_Curve2d C2(e2, F2); 
      Geom2dInt_GInter C;
      C.Perform(C1, C2, Precision::Confusion(), Precision::Confusion());
      if(!C.IsEmpty()) Result = Standard_False;
      if(Result == Standard_False) {
	for(exp3.Init(e2, TopAbs_VERTEX); exp3.More(); exp3.Next())  {
	  
        }
      }
    }
    if(Result == Standard_False) break;
  }
  return Result;
}

*/