summaryrefslogtreecommitdiff
path: root/src/HLRTopoBRep/HLRTopoBRep_FaceIsoLiner.cxx
blob: 62e34277b3d2deb40edcc68047b21b94b1c03a83 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// File:	HLRTopoBRep_FaceIsoLiner.cxx
// Created:	Thu Jan  5 10:57:30 1995
// Author:	Christophe MARION
//		<cma@ecolox>


#include <HLRTopoBRep_FaceIsoLiner.ixx>
#include <TopAbs.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopTools_Array1OfShape.hxx>
#include <TColStd_Array1OfBoolean.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
//#include <BRepAdaptor_Curve2d.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepTools.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2dHatch_Hatcher.hxx>
#include <Geom2dHatch_Intersector.hxx>
#include <HatchGen_ErrorStatus.hxx>
#include <HatchGen_Domain.hxx>
#include <HatchGen_PointOnHatching.hxx>
#include <HatchGen_PointOnElement.hxx>
#include <Precision.hxx>

const Standard_Real IntersectorConfusion = 1.e-10;
const Standard_Real IntersectorTangency  = 1.e-10;
const Standard_Real HatcherConfusion2d   = 1.e-8;
const Standard_Real HatcherConfusion3d   = 1.e-8;
const Standard_Real Infinite = 100.;

//=======================================================================
// Function : Perform
// Purpose  : Builds isoparametric curves with a hatcher.
//=======================================================================

void HLRTopoBRep_FaceIsoLiner::Perform (const Standard_Integer FI,
					const TopoDS_Face& F,
					HLRTopoBRep_Data& DS,
					const Standard_Integer nbIsos)
{
  Standard_Real UMin, UMax, VMin, VMax, U1, U2;
  Standard_Integer ne = 0;
  //BRep_Builder Builder;
  TopoDS_Edge Edge;
  TopExp_Explorer ExpEdges;
  TopoDS_Face TF = F;
  TF.Orientation (TopAbs_FORWARD);
  gp_Pnt2d P;
  gp_Pnt P1, P2;
  TopoDS_Vertex V1U, V2U, V1V, V2V;

  Geom2dHatch_Intersector Intersector
    (IntersectorConfusion,IntersectorTangency);
  Geom2dHatch_Hatcher Hatcher 
    (Intersector,HatcherConfusion2d,HatcherConfusion3d,Standard_True);

  BRepTools::UVBounds (TF, UMin, UMax, VMin, VMax);
  Standard_Boolean InfiniteUMin = Precision::IsNegativeInfinite (UMin);
  Standard_Boolean InfiniteUMax = Precision::IsPositiveInfinite (UMax);
  Standard_Boolean InfiniteVMin = Precision::IsNegativeInfinite (VMin);
  Standard_Boolean InfiniteVMax = Precision::IsPositiveInfinite (VMax);

  if (InfiniteUMin && InfiniteUMax) {
    UMin = - Infinite;
    UMax =   Infinite;
  }
  else if (InfiniteUMin)
    UMin = UMax - Infinite;
  else if (InfiniteUMax)
    UMax = UMin + Infinite;

  if (InfiniteVMin && InfiniteVMax) {
    VMin = - Infinite;
    VMax =   Infinite;
  }
  else if (InfiniteVMin)
    VMin = VMax - Infinite;
  else if (InfiniteVMax)
    VMax = VMin + Infinite;
  
  for (ExpEdges.Init (TF, TopAbs_EDGE); // Edges of the face TF
       ExpEdges.More();
       ExpEdges.Next()) ne++;

  if (DS.FaceHasIntL(TF)) {             // OutLines built on face TF.
    
    TopTools_ListIteratorOfListOfShape itE;
    for(itE.Initialize(DS.FaceIntL(TF));
	itE.More();
	itE.Next()) 
      ne++;
  }

  TopTools_Array1OfShape SH(1,ne);
  TColStd_Array1OfBoolean IL(1,ne); // internal OutLine

  for (ExpEdges.Init (TF, TopAbs_EDGE);
       ExpEdges.More();
       ExpEdges.Next()) {
    Standard_Integer IndE;
    const TopoDS_Edge& newE = TopoDS::Edge(ExpEdges.Current());
    const Handle(Geom2d_Curve) PC =
      BRep_Tool::CurveOnSurface (newE, TF, U1, U2);
    if(   Abs(PC->FirstParameter() - U1) <= Precision::PConfusion() 
       && Abs(PC->LastParameter()  - U2) <= Precision::PConfusion()) { 
      IndE = Hatcher.AddElement (PC,  newE.Orientation());      
    }
    else { 
      Handle (Geom2d_TrimmedCurve) TPC =
	new Geom2d_TrimmedCurve (PC, U1, U2);
      IndE = Hatcher.AddElement (TPC, newE.Orientation());
    }
    SH(IndE) = newE;
    if (DS.IsOutLFaceEdge(TF,newE)) IL(IndE) = Standard_True;
    else                            IL(IndE) = Standard_False;
  }
  
  if (DS.FaceHasIntL(TF)) { // get the internal OutLines built on face F.
    TopTools_ListIteratorOfListOfShape itE;
    for(itE.Initialize(DS.FaceIntL(TF));
	itE.More();
	itE.Next()) {
      Standard_Integer IndE;
      const TopoDS_Edge& newE = TopoDS::Edge(itE.Value());
      const Handle(Geom2d_Curve) PC =
	BRep_Tool::CurveOnSurface (newE, TF, U1, U2);
      if(   Abs(PC->FirstParameter() - U1) <= Precision::PConfusion() 
	 && Abs(PC->LastParameter()  - U2) <= Precision::PConfusion()) { 
	IndE = Hatcher.AddElement (PC,  TopAbs_INTERNAL);      
      }
      else { 
	Handle (Geom2d_TrimmedCurve) TPC =
	  new Geom2d_TrimmedCurve (PC, U1, U2);
	IndE = Hatcher.AddElement (TPC, TopAbs_INTERNAL);
      }
      SH(IndE) = newE;
      IL(IndE) = Standard_True;
    }
  }

  //-----------------------------------------------------------------------
  // Creation des hachures.
  //-----------------------------------------------------------------------
  
  BRepAdaptor_Surface Surface (TF);
  Standard_Real Tolerance = BRep_Tool::Tolerance (TF);
  
  Standard_Integer IIso;
  Standard_Real DeltaU = Abs (UMax - UMin);
  Standard_Real DeltaV = Abs (VMax - VMin);
  Standard_Real Confusion = Min (DeltaU, DeltaV) * HatcherConfusion3d;
  Hatcher.Confusion3d (Confusion);
  
  //-----------------------------------------------------------------------
  // Courbes Iso U.
  //-----------------------------------------------------------------------
  
  Standard_Real StepU = DeltaU / (Standard_Real) nbIsos;
  if (StepU > Confusion) {
    Standard_Real UPrm = UMin + StepU / 2.;
    gp_Dir2d Dir (0., 1.);

    for (IIso = 1; IIso <= nbIsos; IIso++) {
      gp_Pnt2d Ori (UPrm, 0.);
      Handle (Geom2d_Line) IsoLine = new Geom2d_Line (Ori, Dir);
      
      Standard_Integer IndH = Hatcher.AddHatching (IsoLine);
      Hatcher.Trim (IndH);
      if (Hatcher.TrimDone (IndH) && !Hatcher.TrimFailed (IndH))
	Hatcher.ComputeDomains (IndH);
      if (!Hatcher.IsDone (IndH)) {
	cout << "HLRTopoBRep::MakeIsoLines : Face " << FI << endl;
	cout << "U iso of parameter: " << UPrm;
	switch (Hatcher.Status (IndH)) {
	case HatchGen_NoProblem          :
	  cout << " No Problem"          << endl;
	  break;
	case HatchGen_TrimFailure        :
	  cout << " Trim Failure"        << endl;
	  break;
	case HatchGen_TransitionFailure  :
	  cout << " Transition Failure"  << endl;
	  break;
	case HatchGen_IncoherentParity   :
	  cout << " Incoherent Parity"   << endl;
	  break;
	case HatchGen_IncompatibleStates :
	  cout << " Incompatible States" << endl;
	  break;
	}
	Hatcher.RemHatching (IndH);
	continue;
      }
      
      Standard_Integer NbDom = Hatcher.NbDomains (IndH);
      if (NbDom > 0) {

	for (Standard_Integer IDom = 1; IDom <= NbDom; IDom++) {
	  const HatchGen_Domain& Dom = Hatcher.Domain (IndH, IDom);
	  Standard_Real U1 = Dom.HasFirstPoint()  ? 
	    Dom.FirstPoint().Parameter()  : VMin - Infinite;
	  Standard_Real U2 = Dom.HasSecondPoint() ? 
	    Dom.SecondPoint().Parameter() : VMax + Infinite;
	  IsoLine->D0 (U1, P);
	  Surface.D0 (P.X(), P.Y(), P1);
	  IsoLine->D0 (U2, P);
	  Surface.D0 (P.X(), P.Y(), P2);
	  if (Dom.HasFirstPoint()) { // Iso U - Premier point
	    const HatchGen_PointOnHatching& PntH = Dom.FirstPoint();

	    for (Standard_Integer IPntE = 1;
		 IPntE <= PntH.NbPoints();
		 IPntE++) {
	      const HatchGen_PointOnElement& PntE = PntH.Point(IPntE);
	      V1U = HLRTopoBRep_FaceIsoLiner::MakeVertex
		(TopoDS::Edge(SH(PntE.Index())),
		 P1,PntE.Parameter(),Tolerance,DS);
	      if (IL(PntE.Index())) DS.AddOutV(V1U);
	    }
	  }
	  if (Dom.HasSecondPoint()) { // Iso U - Deuxieme point
	    const HatchGen_PointOnHatching& PntH = Dom.SecondPoint();
	    
	    for (Standard_Integer IPntE = 1;
		 IPntE <= PntH.NbPoints();
		 IPntE++) {
	      const HatchGen_PointOnElement& PntE = PntH.Point(IPntE);
	      V2U = HLRTopoBRep_FaceIsoLiner::MakeVertex
		(TopoDS::Edge(SH(PntE.Index())),
		 P2,PntE.Parameter(),Tolerance,DS);
	      if (IL(PntE.Index())) DS.AddOutV(V2U);
	    }
	  }
	  if(!V1U.IsNull() && !V2U.IsNull())
	    HLRTopoBRep_FaceIsoLiner::MakeIsoLine
	      (F,IsoLine,V1U,V2U,U1,U2,Tolerance,DS);
	}
      }
      
      Hatcher.RemHatching (IndH);
      UPrm += StepU;
    }
  }
  
  //-----------------------------------------------------------------------
  // Courbes Iso V.
  //-----------------------------------------------------------------------
  
  Standard_Real StepV = DeltaV / (Standard_Real) nbIsos;
  if (StepV > Confusion) {
    Standard_Real VPrm = VMin + StepV / 2.;
    gp_Dir2d Dir (1., 0.);

    for (IIso = 1; IIso <= nbIsos; IIso++) {
      gp_Pnt2d Ori (0., VPrm);
      Handle (Geom2d_Line) IsoLine = new Geom2d_Line (Ori, Dir);
      
      Standard_Integer IndH = Hatcher.AddHatching (IsoLine);
      Hatcher.Trim (IndH);
      if (Hatcher.TrimDone (IndH) && !Hatcher.TrimFailed (IndH))
	Hatcher.ComputeDomains (IndH);
      if (!Hatcher.IsDone (IndH)) {
	cout << "HLRTopoBRep::MakeIsoLines : Face " << FI << endl;
	cout << "V iso of parameter: " << VPrm;
	switch (Hatcher.Status (IndH)) {
	case HatchGen_NoProblem          :
	  cout << " No Problem"          << endl;
	  break;
	case HatchGen_TrimFailure        :
	  cout << " Trim Failure"        << endl;
	  break;
	case HatchGen_TransitionFailure  :
	  cout << " Transition Failure"  << endl;
	  break;
	case HatchGen_IncoherentParity   :
	  cout << " Incoherent Parity"   << endl;
	  break;
	case HatchGen_IncompatibleStates :
	  cout << " Incompatible States" << endl;
	  break;
	}
	Hatcher.RemHatching (IndH);
	continue;
      }
      
      Standard_Integer NbDom = Hatcher.NbDomains (IndH);
      if (NbDom > 0) {

	for (Standard_Integer IDom = 1; IDom <= NbDom; IDom++) {
	  const HatchGen_Domain& Dom = Hatcher.Domain (IndH, IDom);
	  Standard_Real U1 = Dom.HasFirstPoint()  ? 
	    Dom.FirstPoint().Parameter()  : VMin - Infinite;
	  Standard_Real U2 = Dom.HasSecondPoint() ? 
	    Dom.SecondPoint().Parameter() : VMax + Infinite;
	  IsoLine->D0 (U1, P);
	  Surface.D0 (P.X(), P.Y(), P1);
	  IsoLine->D0 (U2, P);
	  Surface.D0 (P.X(), P.Y(), P2);
	  if (Dom.HasFirstPoint()) { // Iso V - Premier point
	    const HatchGen_PointOnHatching& PntH = Dom.FirstPoint();

	    for (Standard_Integer IPntE = 1; 
		 IPntE <= PntH.NbPoints();
		 IPntE++) {
	      const HatchGen_PointOnElement& PntE = PntH.Point(IPntE);
	      V1V = HLRTopoBRep_FaceIsoLiner::MakeVertex
		(TopoDS::Edge(SH(PntE.Index())),
		 P1,PntE.Parameter(),Tolerance,DS);
	 
     if (IL(PntE.Index())) DS.AddOutV(V1V);
	    }
	  }
	  if (Dom.HasSecondPoint()) { // Iso V - Deuxieme point
	    const HatchGen_PointOnHatching& PntH = Dom.SecondPoint();

	    for (Standard_Integer IPntE = 1;
		 IPntE <= PntH.NbPoints();
		 IPntE++) {
	      const HatchGen_PointOnElement& PntE = PntH.Point(IPntE);
	      V2V = HLRTopoBRep_FaceIsoLiner::MakeVertex
		(TopoDS::Edge(SH(PntE.Index())),
		 P2,PntE.Parameter(),Tolerance,DS);
	      if (IL(PntE.Index())) DS.AddOutV(V2V);
	    }
	  }
	  if(!V1V.IsNull() && !V2V.IsNull())
	    HLRTopoBRep_FaceIsoLiner::MakeIsoLine
	      (F,IsoLine,V1V,V2V,U1,U2,Tolerance,DS);
	}
      }
      
      Hatcher.RemHatching (IndH);
      VPrm += StepV;
    }
  }
}

//=======================================================================
//function : MakeVertex
//purpose  :
//=======================================================================

TopoDS_Vertex
HLRTopoBRep_FaceIsoLiner::MakeVertex (const TopoDS_Edge& E,
				      const gp_Pnt& P,
				      const Standard_Real Par,
				      const Standard_Real Tol,
				      HLRTopoBRep_Data& DS)
{
  TopoDS_Vertex V, VF, VL;
  BRep_Builder B;
  TopExp::Vertices (E, VF, VL);
  if (P.IsEqual(BRep_Tool::Pnt(VF),BRep_Tool::Tolerance(VF)))
    return VF;
  if (P.IsEqual(BRep_Tool::Pnt(VL),BRep_Tool::Tolerance(VL)))
    return VL;

  for (DS.InitVertex(E); DS.MoreVertex(); DS.NextVertex()) {
    TopoDS_Vertex curV = DS.Vertex();
    Standard_Real curP = DS.Parameter();
    if (P.IsEqual(BRep_Tool::Pnt(curV),BRep_Tool::Tolerance(curV))) {
      V = curV;
      break;
    }
    else if (Par < curP) { 
      B.MakeVertex(V,P,Tol);
      V.Orientation(TopAbs_INTERNAL);
      DS.InsertBefore(V,Par);
      break;
    }
  }
  
  if (!DS.MoreVertex()) {
    B.MakeVertex(V,P,Tol);
    V.Orientation(TopAbs_INTERNAL);
    DS.Append(V,Par);
  }
  
  return V;
}

//=======================================================================
//function : MakeIsoLine
//purpose  :
//=======================================================================

void HLRTopoBRep_FaceIsoLiner::MakeIsoLine (const TopoDS_Face& F,
					    const Handle(Geom2d_Line)& Iso,
					    TopoDS_Vertex& V1,
					    TopoDS_Vertex& V2,
					    const Standard_Real U1,
					    const Standard_Real U2,
					    const Standard_Real Tol,
					    HLRTopoBRep_Data& DS)
{
  BRep_Builder B;
  TopoDS_Edge E;
  E.Orientation (TopAbs_INTERNAL);
  V1.Orientation (TopAbs_FORWARD);
  V2.Orientation (TopAbs_REVERSED);
  B.MakeEdge (E);
  B.UpdateEdge (E, Iso, F, Tol);
  B.Add (E, V1);
  B.UpdateVertex (V1, U1, E, Tol);
  B.Add (E, V2);
  B.UpdateVertex (V2, U2, E, Tol);
  DS.AddIsoL(F).Append(E);
}