Salome HOME
updated copyright message
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_ContinuousFaces.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 "FiltersPlugin_ContinuousFaces.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_Wire.h>
31
32 #include <GeomAlgoAPI_ShapeTools.h>
33
34 #include <map>
35 #include <math.h>
36 #include <iostream>
37
38 typedef std::map<GeomShapePtr, SetOfShapes, GeomAPI_Shape::Comparator> MapShapeAndAncestors;
39
40 //=================================================================================================
41 static void mapEdgesAndFaces(const GeomShapePtr theShape, MapShapeAndAncestors& theMap)
42 {
43   GeomAPI_ShapeExplorer aFExp(theShape, GeomAPI_Shape::FACE);
44   for (; aFExp.more(); aFExp.next()) {
45     GeomShapePtr aFace = aFExp.current();
46     GeomAPI_ShapeExplorer aEExp(aFace, GeomAPI_Shape::EDGE);
47     for (; aEExp.more(); aEExp.next())
48       theMap[aEExp.current()].insert(aFace);
49   }
50 }
51
52 //=================================================================================================
53 // Find all continuous faces for the given.
54 static void cacheContinuousFace(const GeomShapePtr theFace,
55                                 const MapShapeAndAncestors& theEdgeToFaces,
56                                 SetOfShapes& theCache,
57                                 const double & theAngle)
58 {
59
60   MapShapeAndAncestors::const_iterator aFound;
61   GeomAPI_ShapeExplorer aEExp(theFace, GeomAPI_Shape::EDGE);
62   for (; aEExp.more(); aEExp.next()){
63     aFound = theEdgeToFaces.find(aEExp.current());
64     if (aFound == theEdgeToFaces.end())
65       continue;
66
67     GeomEdgePtr anEdge;
68     anEdge = GeomEdgePtr(new GeomAPI_Edge(aEExp.current()));
69
70     for (SetOfShapes::const_iterator aFIt = aFound->second.begin();
71        aFIt != aFound->second.end(); ++aFIt) {
72       std::string anError = "";
73       if (theCache.find(*aFIt) == theCache.end()) {
74        GeomPointPtr aPoint = anEdge->middlePoint();
75        if (GeomAlgoAPI_ShapeTools::isContinuousFaces(theFace,
76                                                      *aFIt,
77                                                      aPoint,
78                                                      theAngle,
79                                                      anError)) {
80           theCache.insert(*aFIt);
81           cacheContinuousFace(*aFIt, theEdgeToFaces, theCache, theAngle);
82         }
83       }
84     }
85   }
86 }
87
88 //=================================================================================================
89 static void cacheContinuousFaces(const GeomShapePtr theTopLevelShape,
90                                  const SetOfShapes& theFaces,
91                                  SetOfShapes& theCache,
92                                  const double & theAngle)
93 {
94   if (!theTopLevelShape || theFaces.empty())
95     return;
96
97   MapShapeAndAncestors anEdgesToFaces;
98   mapEdgesAndFaces(theTopLevelShape, anEdgesToFaces);
99
100   for (SetOfShapes::const_iterator aFIt = theFaces.begin();
101        aFIt != theFaces.end(); ++aFIt) {
102     // keep the original face
103     theCache.insert(*aFIt);
104     // cache continuous face
105     cacheContinuousFace(*aFIt, anEdgesToFaces, theCache,theAngle);
106   }
107 }
108
109 //=================================================================================================
110 static bool updateFaces(const AttributeSelectionListPtr& theList,
111                         SetOfShapes& theFaces)
112 {
113   bool aNewCache = false;
114   if ((int)theFaces.size() != theList->size()) {
115     aNewCache = true;
116   } else {
117     for (int i = 0; i < theList->size(); i++) {
118       AttributeSelectionPtr aCurAttr = theList->value(i);
119       GeomShapePtr aFace = aCurAttr->value();
120       if (theFaces.empty() || theFaces.find(aFace) == theFaces.end()) {
121         aNewCache = true;
122         break;
123       }
124     }
125   }
126   if (aNewCache) {
127     theFaces.clear();
128     for (int i = 0; i < theList->size(); i++) {
129       AttributeSelectionPtr aCurAttr = theList->value(i);
130       GeomShapePtr aFace = aCurAttr->value();
131       theFaces.insert(aFace);
132     }
133   }
134   return aNewCache;
135 }
136
137 //=================================================================================================
138 bool FiltersPlugin_ContinuousFaces::isSupported(GeomAPI_Shape::ShapeType theType) const
139 {
140   return theType == GeomAPI_Shape::FACE;
141 }
142
143 //=================================================================================================
144 bool FiltersPlugin_ContinuousFaces::isOk(const GeomShapePtr& theShape, const ResultPtr&,
145                                          const ModelAPI_FiltersArgs& theArgs) const
146 {
147   AttributePtr aAttr = theArgs.argument("faces");
148   AttributeSelectionListPtr aList =
149     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
150   if (!aList.get())
151     return false;
152
153   AttributePtr anAttr = theArgs.argument("value");
154   AttributeDoublePtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
155   if (!aValue.get()|| !anAttr->isInitialized())
156     return false;
157   double anAngle= aValue->value();
158
159   bool aNewCache = updateFaces(aList,
160                                const_cast<FiltersPlugin_ContinuousFaces*>(this)->myFaces);
161
162   if (aNewCache || fabs(myAngle - anAngle) > 1e-10 ) {
163     const_cast<FiltersPlugin_ContinuousFaces*>(this)->myAngle = anAngle;
164     const_cast<FiltersPlugin_ContinuousFaces*>(this)->myCachedShapes.clear();
165   }
166
167   if (myCachedShapes.empty()) {
168     for (int i = 0; i < aList->size(); i++)
169     {
170       ResultBodyPtr aBaseResult = ModelAPI_Tools::bodyOwner(aList->value(i)->context(), true);
171       if (!aBaseResult.get()) {
172         aBaseResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aList->value(i)->context());
173         if (!aBaseResult.get())
174           return false;
175       }
176       cacheContinuousFaces(aBaseResult->shape(),
177                           const_cast<FiltersPlugin_ContinuousFaces*>(this)->myFaces,
178                           const_cast<FiltersPlugin_ContinuousFaces*>(this)->myCachedShapes,anAngle);
179     }
180   }
181   return myCachedShapes.find(theShape) != myCachedShapes.end();
182 }
183
184 //=================================================================================================
185 std::string FiltersPlugin_ContinuousFaces::xmlRepresentation() const
186 {
187   return xmlFromFile("filter-ContinuousFaces.xml");
188 }
189
190 //=================================================================================================
191 void FiltersPlugin_ContinuousFaces::initAttributes(ModelAPI_FiltersArgs& theArguments)
192 {
193   theArguments.initAttribute("value", ModelAPI_AttributeDouble::typeId());
194   theArguments.initAttribute("faces", ModelAPI_AttributeSelectionList::typeId());
195 }