Salome HOME
Analytical interpolation. Minor changes.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Pipe.h"
21
22 #include "GeomAlgoAPI_DFLoader.h"
23
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Lin.h>
27 #include <GeomAPI_ShapeExplorer.h>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepExtrema_DistShapeShape.hxx>
31 #include <BRepOffsetAPI_MakePipe.hxx>
32 #include <BRepOffsetAPI_MakePipeShell.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
36 #include <gp_Lin.hxx>
37 #include <NCollection_List.hxx>
38 #include <TopExp.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <Precision.hxx>
43
44 static bool getBase(TopoDS_Shape& theBaseOut,
45                     TopAbs_ShapeEnum& theBaseTypeOut,
46                     const GeomShapePtr theBaseShape);
47 static bool getPath(TopoDS_Wire& thePathOut,
48                     const GeomShapePtr thePathShape);
49 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
50                                         const TopoDS_Shape& thePath);
51 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
52 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
53
54 //==================================================================================================
55 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
56                                    const GeomShapePtr thePathShape)
57 {
58   build(theBaseShape, thePathShape);
59 }
60
61 //==================================================================================================
62 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
63                                    const GeomShapePtr thePathShape,
64                                    const GeomShapePtr theBiNormal)
65 {
66   build(theBaseShape, thePathShape, theBiNormal);
67 }
68
69 //==================================================================================================
70 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
71                                    const ListOfShape& theLocations,
72                                    const GeomShapePtr thePathShape)
73 {
74   build(theBaseShapes, theLocations, thePathShape);
75 }
76
77 //==================================================================================================
78 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
79                              const GeomShapePtr thePathShape)
80 {
81   // Getting base shape.
82   if(!theBaseShape.get()) {
83     return;
84   }
85   TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
86   if(aBaseShape.IsNull()) {
87     return;
88   }
89   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
90   if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
91      aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
92      aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
93     return;
94   }
95
96   // Getting path.
97   TopoDS_Wire aPathWire;
98   if(!getPath(aPathWire, thePathShape)) {
99     return;
100   }
101   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
102   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
103   aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
104   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
105   if (!anOldPath->isSame(aNewPath))
106     addMovedPath(anOldPath, aNewPath);
107
108   // Making pipe.
109   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
110   if(!aPipeBuilder) {
111     return;
112   }
113   aPipeBuilder->Build();
114
115   // Checking result.
116   if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
117     delete aPipeBuilder;
118     return;
119   }
120   this->initialize(aPipeBuilder);
121
122   // Setting naming.
123   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
124   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
125
126   // Setting result.
127   TopoDS_Shape aResult = aPipeBuilder->Shape();
128   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
129   GeomShapePtr aGeomSh(new GeomAPI_Shape());
130   aGeomSh->setImpl(new TopoDS_Shape(aResult));
131   this->setShape(aGeomSh);
132   this->setDone(true);
133 }
134
135 //==================================================================================================
136 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
137                              const GeomShapePtr thePathShape,
138                              const GeomShapePtr theBiNormal)
139 {
140   // Getting base shape and path.
141   TopoDS_Shape aBaseShape;
142   TopAbs_ShapeEnum aBaseShapeType;
143   TopoDS_Wire aPathWire;
144   if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
145       !getPath(aPathWire, thePathShape) ||
146       !theBiNormal.get()) {
147     return;
148   }
149
150   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
151   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
152   aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
153   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
154   if (!anOldPath->isSame(aNewPath))
155     addMovedPath(anOldPath, aNewPath);
156
157   // Getting Bi-Normal.
158   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
159   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
160     return;
161   }
162   TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
163   Standard_Real aFirst, aLast;
164   Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
165   Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
166   if(aBiNormalLine.IsNull()) {
167     return;
168   }
169   gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
170
171   // Making pipe.
172   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
173   if(!aPipeBuilder) {
174     return;
175   }
176   aPipeBuilder->Add(aBaseShape);
177   aPipeBuilder->SetMode(aBiNormalDir);
178   if(!buildPipe(aPipeBuilder)) {
179     delete aPipeBuilder;
180     return;
181   }
182   this->initialize(aPipeBuilder);
183
184   // Checking result.
185   if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
186     return;
187   }
188   TopoDS_Shape aResult = aPipeBuilder->Shape();
189   if(aResult.IsNull()) {
190     return;
191   }
192
193   // Setting naming.
194   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
195   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
196
197   // Setting result.
198   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
199   GeomShapePtr aGeomSh(new GeomAPI_Shape());
200   aGeomSh->setImpl(new TopoDS_Shape(aResult));
201   this->setShape(aGeomSh);
202   this->setDone(true);
203 }
204
205 //==================================================================================================
206 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
207                              const ListOfShape& theLocations,
208                              const GeomShapePtr thePathShape)
209 {
210   if(theBaseShapes.empty() ||
211      (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
212     return;
213   }
214
215   // Getting base shape and path.
216   TopoDS_Shape aBaseShape;
217   TopAbs_ShapeEnum aBaseShapeType;
218   TopoDS_Wire aPathWire;
219   if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
220       !getPath(aPathWire, thePathShape)) {
221     return;
222   }
223
224   TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
225   gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
226
227   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
228   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
229   aPathWire.Move(aTrsf);
230   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
231   if (!anOldPath->isSame(aNewPath))
232     addMovedPath(anOldPath, aNewPath);
233
234   // Get locations after moving path shape.
235   std::list<TopoDS_Vertex> aLocations;
236   for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
237        aLocIt != theLocations.cend();
238        ++aLocIt)
239   {
240     GeomShapePtr aLocation = *aLocIt;
241     if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
242       return;
243     }
244
245     TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
246     TopoDS_Vertex aMovedVertex;
247     for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
248       if (anExp.Current().IsPartner(aLocationVertex)) {
249         aMovedVertex = TopoDS::Vertex(anExp.Current());
250         aLocations.push_back(aMovedVertex);
251         break;
252       }
253     }
254     if (aMovedVertex.IsNull()) {
255       return;
256     }
257   }
258
259   if (theLocations.size() != aLocations.size()) {
260     return;
261   }
262
263   bool aHasLocations = !aLocations.empty();
264
265   // Making pipe.
266   Standard_Boolean isDone = Standard_False;
267   bool anIsSolidNeeded = false;
268   BRepOffsetAPI_MakePipeShell* aPipeBuilder;
269   for(int i = 0; i < 2; ++i) {
270     aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
271     if(!aPipeBuilder) {
272       return;
273     }
274     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
275     std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
276     while(aBaseIt != theBaseShapes.cend()) {
277       GeomShapePtr aBase = *aBaseIt;
278       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
279         delete aPipeBuilder;
280         return;
281       }
282       ++aBaseIt;
283       if(aBaseShapeType == TopAbs_FACE) {
284         anIsSolidNeeded = true;
285       }
286
287       if(aHasLocations) {
288         aPipeBuilder->Add(aBaseShape, *aLocationsIt);
289         ++aLocationsIt;
290       } else {
291         aPipeBuilder->Add(aBaseShape);
292       }
293     }
294
295     if(aPipeBuilder->IsReady() == Standard_False) {
296       delete aPipeBuilder;
297       return;
298     }
299
300     if (i == 1) {
301        // Try to use Descrete Trihedron mode.
302       aPipeBuilder->SetDiscreteMode();
303     }
304     aPipeBuilder->Build();
305     isDone = aPipeBuilder->IsDone();
306
307     if (isDone) {
308       break;
309     }
310
311     delete aPipeBuilder;
312   }
313
314   if (!isDone) {
315     return;
316   }
317
318   this->initialize(aPipeBuilder);
319
320   // Checking result.
321   if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
322     return;
323   }
324   TopoDS_Shape aResult = aPipeBuilder->Shape();
325
326   // Setting naming.
327   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
328   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
329   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
330   fixOrientation(aFromShape);
331   fixOrientation(aToShape);
332   this->addFromShape(aFromShape);
333   this->addToShape(aToShape);
334
335   // Setting result.
336   if(aResult.IsNull()) {
337     return;
338   }
339   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
340   GeomShapePtr aGeomSh(new GeomAPI_Shape());
341   aGeomSh->setImpl(new TopoDS_Shape(aResult));
342   this->setShape(aGeomSh);
343   this->setDone(true);
344 }
345
346 //==================================================================================================
347 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
348                                  ListOfShape& theHistory)
349 {
350   if (myMovedPath.isBound(theShape))
351     GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
352   else
353     GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
354 }
355
356 // Auxilary functions:
357 //==================================================================================================
358 bool getBase(TopoDS_Shape& theBaseOut,
359              TopAbs_ShapeEnum& theBaseTypeOut,
360              const GeomShapePtr theBaseShape)
361 {
362   if(!theBaseShape.get()) {
363     return false;
364   }
365
366   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
367   if(theBaseOut.IsNull()) {
368     return false;
369   }
370   theBaseTypeOut = theBaseOut.ShapeType();
371   if(theBaseTypeOut == TopAbs_VERTEX) {
372     // Do nothing.
373   } else if(theBaseTypeOut == TopAbs_EDGE) {
374     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
375   } else if(theBaseTypeOut == TopAbs_WIRE) {
376     // Do nothing.
377   } else if(theBaseTypeOut == TopAbs_FACE) {
378     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
379     theBaseOut = anExp.Current();
380   } else {
381     return false;
382   }
383
384   return true;
385 }
386
387 //==================================================================================================
388 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
389 {
390   if(!thePathShape.get()) {
391     return false;
392   }
393
394   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
395   if(aPathShape.IsNull()) {
396     return false;
397   }
398   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
399   if(aPathShapeType == TopAbs_EDGE) {
400     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
401     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
402   } else if(aPathShapeType == TopAbs_WIRE) {
403     thePathOut = TopoDS::Wire(aPathShape);
404   } else {
405     return false;
406   }
407
408   return true;
409 }
410
411 //==================================================================================================
412 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
413 {
414   gp_Trsf aTranslation;
415
416   BRepExtrema_DistShapeShape aDist(theBase, thePath);
417   aDist.Perform();
418   if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
419     gp_Pnt aPntBase = aDist.PointOnShape1(1);
420     gp_Pnt aPntPath = aDist.PointOnShape2(1);
421     // get the end point on the path nearest to the found point,
422     // because the path is almost usually started from the one of the sections
423     if (!BRep_Tool::IsClosed(thePath)) {
424       TopoDS_Wire aPathWire = TopoDS::Wire(thePath);
425       if (!aPathWire.IsNull()) {
426         TopoDS_Vertex aV1, aV2;
427         TopExp::Vertices(aPathWire, aV1, aV2);
428
429         gp_Pnt aStart = BRep_Tool::Pnt(aV1);
430         gp_Pnt anEnd = BRep_Tool::Pnt(aV2);
431
432         // compare 3 distances
433         double aDistToStart = aPntBase.Distance(aStart);
434         double aDistToEnd = aPntBase.Distance(anEnd);
435         double aMinDist = aPntBase.Distance(aPntPath);
436
437         static const double THE_THRESHOLD = 0.01; // threshold for distance ratio
438         double aDeltaStart = Abs(aDistToStart - aMinDist);
439         double aDeltaEnd = Abs(aDistToEnd - aMinDist);
440         if (aDeltaStart < THE_THRESHOLD * aDeltaEnd)
441           aPntPath = aStart;
442         else if (aDeltaEnd < THE_THRESHOLD * aDeltaStart)
443           aPntPath = anEnd;
444       }
445     }
446     aTranslation.SetTranslation(aPntPath, aPntBase);
447   }
448
449   return aTranslation;
450 }
451
452 //==================================================================================================
453 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
454 {
455   thePipeBuilder->Build();
456
457   Standard_Boolean isDone = thePipeBuilder->IsDone();
458
459   if (!isDone) {
460     // Try to use Descrete Trihedron mode.
461     thePipeBuilder->SetDiscreteMode();
462     thePipeBuilder->Build();
463     isDone = thePipeBuilder->IsDone();
464   }
465
466   return isDone == Standard_True;
467 }
468
469 //==================================================================================================
470 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
471 {
472   ListOfShape aList;
473
474   TopAbs_ShapeEnum aType = theShape.ShapeType();
475   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
476     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
477       GeomShapePtr aGeomShape(new GeomAPI_Shape());
478       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
479       aList.push_back(aGeomShape);
480     }
481   } else {
482     GeomShapePtr aGeomShape(new GeomAPI_Shape());
483     aGeomShape->setImpl(new TopoDS_Shape(theShape));
484     aList.push_back(aGeomShape);
485   }
486
487   return aList;
488 }
489
490 //==================================================================================================
491 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
492 {
493   myMovedPath.clear();
494   GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
495   GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
496   for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
497     myMovedPath.bind(anOldExp.current(), aNewExp.current());
498   }
499 }