summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_ShapeConvertToBezier.cxx
blob: 2b0d9782f7e95a8aec9fa0e78f5f7103377f2b7d (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
// 15.06 2000 gka fix against small edges ; merging ends pcurves and 3d curves

// File:	ShapeUpgrade_ShapeConvertToBezier.cxx
// Created:	Fri May 14 10:00:01 1999
// Author:	data exchange team
//		<det@friendox.nnov.matra-dtv.fr>

#include <ShapeUpgrade_ShapeConvertToBezier.ixx>
#include <ShapeUpgrade_WireDivide.hxx>
#include <ShapeUpgrade_ConvertCurve3dToBezier.hxx>
#include <ShapeUpgrade_ConvertCurve2dToBezier.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS.hxx>
#include <BRep_Builder.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <ShapeBuild_Edge.hxx>
#include <Geom_Curve.hxx>
#include <Geom_BezierCurve.hxx>
#include <Precision.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom2d_BezierCurve.hxx>
#include <BRep_Tool.hxx>
#include <GeomLib.hxx>
#include <TopoDS_Edge.hxx>
#include <ShapeUpgrade_ConvertSurfaceToBezierBasis.hxx>
#include <TopExp.hxx>
#include <TopExp.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <ShapeFix_Wire.hxx>
#include <ShapeExtend_WireData.hxx>
#include <TopoDS_Vertex.hxx>
#include <ShapeUpgrade_FixSmallBezierCurves.hxx>
#include <ShapeUpgrade_FixSmallBezierCurves.hxx>
#include <ShapeUpgrade_FixSmallBezierCurves.hxx>
#include <BRepTools.hxx>

//=======================================================================
//function : ShapeUpgrade_ShapeConvertToBezier
//purpose  : 
//=======================================================================

ShapeUpgrade_ShapeConvertToBezier::ShapeUpgrade_ShapeConvertToBezier()
{
  myLevel = 0;
  my2dMode = Standard_False;
  my3dMode = Standard_False;
  mySurfaceMode = Standard_False;
  //set spesial flags to true
  my3dLineMode = Standard_True;
  my3dCircleMode = Standard_True;
  my3dConicMode = Standard_True;
  myPlaneMode      = Standard_True;
  myRevolutionMode = Standard_True;
  myExtrusionMode  = Standard_True;
  myBSplineMode    = Standard_True;
}

//=======================================================================
//function : ShapeUpgrade_ShapeConvertToBezier
//purpose  : 
//=======================================================================

ShapeUpgrade_ShapeConvertToBezier::ShapeUpgrade_ShapeConvertToBezier(const TopoDS_Shape& S):
       ShapeUpgrade_ShapeDivide(S)
{
  myLevel = 0;
  my2dMode = Standard_False;
  my3dMode = Standard_False;
  mySurfaceMode = Standard_False;
  //set spesial flags to true
  my3dLineMode = Standard_True;
  my3dCircleMode = Standard_True;
  my3dConicMode = Standard_True;
  myPlaneMode      = Standard_True;
  myRevolutionMode = Standard_True;
  myExtrusionMode  = Standard_True;
  myBSplineMode    = Standard_True;
}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_ShapeConvertToBezier::Perform (const Standard_Boolean newContext)
{
  myLevel++;
  
  Standard_Boolean res = Standard_False;
  if ( myLevel ==1 ) {
    Standard_Boolean isNewContext = newContext;
    if ( mySurfaceMode ) {
      Standard_Boolean mode2d = my2dMode, mode3d = my3dMode;
      my2dMode = my3dMode = Standard_False;
      res = ShapeUpgrade_ShapeDivide::Perform(isNewContext);
      isNewContext = Standard_False;
      myShape = myResult;
      my2dMode = mode2d;
      my3dMode = mode3d;
    }
    if ( my2dMode || my3dMode ) {
      Standard_Boolean modeS = mySurfaceMode;
      mySurfaceMode = Standard_False;
      res = ShapeUpgrade_ShapeDivide::Perform(isNewContext);
      mySurfaceMode = modeS;
    }
  }
  else res = ShapeUpgrade_ShapeDivide::Perform(newContext);
  //pdn Hereafter the fix on GeomLib:SameParameter.
  //In order to fix this bug all edges that are based on 
  //bezier curves (2d or 3d) and have range not equal to [0,1]
  //are performed the following sequence:
  // 1. Segment on bezier curve 
  // 2. Changing rande of edge to [0,1]
  if(myLevel == 1) {
    BRep_Builder B;
    ShapeAnalysis_Edge sae;
    ShapeBuild_Edge sbe;
    Standard_Real preci = Precision::PConfusion();
    for(TopExp_Explorer exp(myResult,TopAbs_FACE); exp.More(); exp.Next()) {
      TopoDS_Face face = TopoDS::Face(exp.Current());
      face.Orientation(TopAbs_FORWARD);
      for(TopExp_Explorer exp1(face, TopAbs_WIRE); exp1.More(); exp1.Next()) {
 	TopoDS_Wire wire= TopoDS::Wire(exp1.Current());
	Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(wire,face,myPrecision);
	sfw->FixReorder();
	sfw->FixShifted(); // for cylinders.brep
	Handle(ShapeExtend_WireData) sewd = sfw->WireData();
	for(Standard_Integer i =1; i <= sewd->NbEdges(); i++) {
	  TopoDS_Edge edge = sewd->Edge(i);
	  //TopoDS_Edge edge = TopoDS::Edge(exp1.Current());
	  Handle(Geom_Curve) c3d;
	  Standard_Real first, last;
	  TopoDS_Vertex V1,V2;
	  TopExp::Vertices(edge,V1,V2);
	  if(sae.Curve3d(edge,c3d,first,last,Standard_False)) {
	    if(c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve))) {
	      //B.SameRange(edge, Standard_False);
	      Handle(Geom_BezierCurve) bezier = Handle(Geom_BezierCurve)::DownCast(c3d);
	      if(first!= 0 || last !=1) {
		if(first > preci || last < 1 - preci) {
		  bezier->Segment(first,last);
		  B.UpdateEdge(edge,bezier,0.);
		}
		sbe.SetRange3d(edge,0,1);
	      } 
	      if(!bezier.IsNull()) { //gka fix against small edges ; merging ends of 3d curves 
		gp_Pnt p1 = bezier->Value(first);
		gp_Pnt p2 = bezier->Value(last);
		gp_Pnt p1v = BRep_Tool::Pnt(V1);
 		gp_Pnt p2v = BRep_Tool::Pnt(V2);
		if(p1.Distance(p1v) > Precision::Confusion())
		  bezier->SetPole(1,p1v);
		if(p2.Distance(p2v) > Precision::Confusion())
		  bezier->SetPole(bezier->NbPoles(),p2v);    
		B.UpdateEdge(edge,bezier,0.);
	      }
	    }
	    else if(my2dMode)
	      B.SameRange(edge, Standard_True);
	  }
 	  Handle(Geom2d_Curve) c2d,c2drev;
 	  Handle(Geom2d_BezierCurve) bezier,bezierR;
 	  Standard_Boolean isSeam = BRep_Tool::IsClosed ( edge, face );
	  if(!sae.PCurve(edge,face,c2d,first,last,Standard_False)) continue ;
 	  if(!c2d->IsKind(STANDARD_TYPE(Geom2d_BezierCurve))) continue;
 	  Handle(Geom2d_Curve) newRevCurve,newCurve;
 	  bezier = Handle(Geom2d_BezierCurve)::DownCast(c2d);
	  if(isSeam) {
	   TopoDS_Shape aLocalShape = edge.Reversed();
	   TopoDS_Edge tmpedge = TopoDS::Edge(aLocalShape);
//	   TopoDS_Edge tmpedge = TopoDS::Edge(edge.Reversed());
	   if(sae.PCurve(tmpedge,face,c2drev,first,last,Standard_False)) {
	      if(c2drev->IsKind(STANDARD_TYPE(Geom2d_BezierCurve))) {
		bezierR = Handle(Geom2d_BezierCurve)::DownCast(c2drev);
	      }
	    }
	 }
	  if(first!= 0 || last !=1) {
	    if(first > preci || last < 1 - preci) {
	      if(!bezier.IsNull()) {
		bezier->Segment(first,last);
		newCurve = bezier;
	      }
	      else 
		GeomLib::SameRange(preci, c2d, first, last, 0, 1, newCurve);
	      if(isSeam) {
		if(!bezierR.IsNull()) {
		  bezierR->Segment(first, last);
		  newRevCurve = bezierR;
		}
		else if(!c2drev.IsNull())
		  GeomLib::SameRange(preci, c2drev, first, last, 0, 1, newRevCurve);
		if(edge.Orientation()==TopAbs_FORWARD)
		  B.UpdateEdge ( edge, newCurve, newRevCurve, face, 0. );
		else
		  B.UpdateEdge ( edge, newRevCurve, newCurve, face, 0. );
	      }
	    }
	    B.Range(edge,face,0,1);
	  }
	
	  
	  TopoDS_Edge edgenext = sewd->Edge((i == sewd->NbEdges() ? 1 : i+1));
	  Handle(Geom2d_Curve) c2dnext,c2drevnext,newnextCurve;
	  Standard_Real first2,last2;
	  Handle(Geom2d_BezierCurve) beziernext,bezierRnext;
	  if(!sae.PCurve(edgenext,face,c2dnext,first2,last2,Standard_False)) continue ;
	  if(!c2dnext->IsKind(STANDARD_TYPE(Geom2d_BezierCurve)) ) continue;
	     beziernext = Handle(Geom2d_BezierCurve)::DownCast(c2dnext);
	  //Handle(Geom2d_Curve) newRevCurve;
	  if(isSeam) {
	    TopoDS_Shape aLocalShape = edgenext.Reversed();
	    TopoDS_Edge tmpedge = TopoDS::Edge(aLocalShape);
//	    TopoDS_Edge tmpedge = TopoDS::Edge(edgenext.Reversed());
	    if(sae.PCurve(tmpedge,face,c2drevnext,first,last,Standard_False)) {
	      if(c2drevnext->IsKind(STANDARD_TYPE(Geom2d_BezierCurve))) {
		bezierRnext = Handle(Geom2d_BezierCurve)::DownCast(c2drevnext);
	      }
	   }
	  }
	  if(first2!= 0 || last2 !=1) {
	    if(first2 > preci || last2 < 1 - preci) {
	     if(!beziernext.IsNull()) {
	       beziernext->Segment(first2,last2);
	       newnextCurve = beziernext;
	     }
	     else 
	       GeomLib::SameRange(preci, c2dnext, first2, last2, 0, 1, newnextCurve);
	      if(isSeam) {  
		if(!bezierRnext.IsNull()) {
		  bezierRnext->Segment(first2, last2);
		  newRevCurve = bezierRnext;
		}
		else if(!c2drevnext.IsNull())
		  GeomLib::SameRange(preci, c2drevnext, first2, last2, 0, 1, newRevCurve);
		if(edge.Orientation()==TopAbs_FORWARD)
		  B.UpdateEdge ( edgenext, newnextCurve, newRevCurve, face, 0. );
		else
		  B.UpdateEdge ( edgenext, newRevCurve, newnextCurve, face, 0. );
	      }
	   }
	    B.Range(edgenext,face,0,1);
	  }
	   
	  if(bezier.IsNull()  || beziernext.IsNull() ) continue; //gka fix against small edges ; merging ends of pcurves 
	  Standard_Real f1,l1,f2,l2;
	  f1 = bezier->FirstParameter();
	  l1 = bezier->LastParameter();
	  f2 = beziernext->FirstParameter();
	  l2 = beziernext->LastParameter();
	  gp_Pnt2d p2d1 = bezier->Value(f1);
	  gp_Pnt2d p2d2 = bezier->Value(l1);
	  gp_Pnt2d p2d1next = beziernext->Value(f2);
	  gp_Pnt2d p2d2next = beziernext->Value(l2);
	  if(edge.Orientation() == TopAbs_FORWARD && edgenext.Orientation() == TopAbs_FORWARD) {
	    if(p2d2.Distance(p2d1next) > Precision::PConfusion()) {
	      gp_Pnt2d pmid = 0.5*(p2d2.XY() + p2d1next.XY());
//	      gp_Pnt2d p1 =  bezier->Pole(bezier->NbPoles());
//	      gp_Pnt2d p2 =  beziernext->Pole(1);
	      bezier->SetPole(bezier->NbPoles(),pmid);
	      beziernext->SetPole(1,pmid);
	    }
	  }
	  if(edge.Orientation() == TopAbs_FORWARD && edgenext.Orientation() == TopAbs_REVERSED) {
	    if(p2d2.Distance(p2d2next) > Precision::PConfusion()) {
	      gp_Pnt2d pmid = 0.5*(p2d2.XY() + p2d2next.XY());
//	      gp_Pnt2d p1 =  bezier->Pole(bezier->NbPoles());
//	      gp_Pnt2d p2 =  beziernext->Pole(beziernext->NbPoles());
	      bezier->SetPole(bezier->NbPoles(),pmid);
	      beziernext->SetPole(beziernext->NbPoles(),pmid);
	    }
	  }
	  if(edge.Orientation() == TopAbs_REVERSED && edgenext.Orientation() == TopAbs_FORWARD) {
	    if(p2d1.Distance(p2d1next) > Precision::PConfusion()) {
	      gp_Pnt2d pmid = 0.5*(p2d1.XY() + p2d1next.XY());
//	      gp_Pnt2d p1 =  bezier->Pole(1);
//	      gp_Pnt2d p2 =  beziernext->Pole(1);
	      bezier->SetPole(1,pmid);
	      beziernext->SetPole(1,pmid);
	    }
	  }
	  if(edge.Orientation() == TopAbs_REVERSED && edgenext.Orientation() == TopAbs_REVERSED) {
	    if(p2d1.Distance(p2d2next) > Precision::PConfusion()) {
	      gp_Pnt2d pmid = 0.5*(p2d1.XY() + p2d2next.XY());
//	      gp_Pnt2d p1 =  bezier->Pole(1);
//	      gp_Pnt2d p2 =  beziernext->Pole(beziernext->NbPoles());
	      bezier->SetPole(1,pmid);
	      beziernext->SetPole(beziernext->NbPoles(),pmid);
	    }
	  }
	 
	}
	BRepTools::Update(face);
      }
    }
    
  }

  myLevel--;
  
  return res;
}
      
//=======================================================================
//function : GetSplitFaceTool
//purpose  : 
//=======================================================================

Handle(ShapeUpgrade_FaceDivide) ShapeUpgrade_ShapeConvertToBezier::GetSplitFaceTool() const
{
  Handle(ShapeUpgrade_FaceDivide) tool = new ShapeUpgrade_FaceDivide;
  Handle(ShapeUpgrade_WireDivide) wtool = new ShapeUpgrade_WireDivide;
  if(my3dMode) {
    Handle(ShapeUpgrade_ConvertCurve3dToBezier) curve3dConverter = new ShapeUpgrade_ConvertCurve3dToBezier;
    curve3dConverter->SetLineMode(my3dLineMode);
    curve3dConverter->SetCircleMode(my3dCircleMode);
    curve3dConverter->SetConicMode(my3dConicMode);
    wtool->SetSplitCurve3dTool(curve3dConverter);
  }
  if(my2dMode)
    wtool->SetSplitCurve2dTool(new ShapeUpgrade_ConvertCurve2dToBezier);
  wtool->SetEdgeMode(myEdgeMode);
  Handle(ShapeUpgrade_FixSmallBezierCurves) FixSmallBezierCurvesTool = new ShapeUpgrade_FixSmallBezierCurves; 
  wtool->SetFixSmallCurveTool(FixSmallBezierCurvesTool);
  tool->SetWireDivideTool(wtool);
  if(mySurfaceMode) {
    Handle(ShapeUpgrade_ConvertSurfaceToBezierBasis) stool = new ShapeUpgrade_ConvertSurfaceToBezierBasis;
    stool->SetPlaneMode(myPlaneMode);
    stool->SetRevolutionMode(myRevolutionMode);
    stool->SetExtrusionMode(myExtrusionMode);
    stool->SetBSplineMode(myBSplineMode);
    tool->SetSplitSurfaceTool(stool);
  }
  return tool;
}