Salome HOME
Implement filter "F7: Opposite to an edge"
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_OppositeToEdge.cpp
1 // Copyright (C) 2014-2019  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 "FiltersPlugin_OppositeToEdge.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_Tools.h>
25
26 #include <GeomAPI_Shape.h>
27 #include <GeomAPI_ShapeExplorer.h>
28
29 #include <map>
30
31 typedef std::map<GeomShapePtr, SetOfShapes, GeomAPI_Shape::Comparator> MapShapeAndAncestors;
32
33 static void mapEdgesAndFaces(const GeomShapePtr theShape, MapShapeAndAncestors& theMap)
34 {
35   GeomAPI_ShapeExplorer aFExp(theShape, GeomAPI_Shape::FACE);
36   for (; aFExp.more(); aFExp.next()) {
37     GeomShapePtr aFace = aFExp.current();
38     GeomAPI_ShapeExplorer aEExp(aFace, GeomAPI_Shape::EDGE);
39     for (; aEExp.more(); aEExp.next())
40       theMap[aEExp.current()].insert(aFace);
41   }
42 }
43
44 // Return edge in the quadratic face opposite to the given one.
45 // If the face is not quadratic, returns empty shape.
46 static GeomShapePtr oppositeEdgeInQuadFace(const GeomShapePtr theEdge,
47                                            const GeomShapePtr theFace)
48 {
49   static int THE_QUAD = 4;
50
51   int aNbEdges = 0;
52   int anOriginalEdgeIndex = -THE_QUAD;
53   GeomShapePtr anOppositeEdge;
54   GeomAPI_ShapeExplorer anExp(theFace, GeomAPI_Shape::EDGE);
55   while (anExp.more()) {
56     if (anExp.current()->isSame(theEdge))
57       anOriginalEdgeIndex = aNbEdges;
58     else if (aNbEdges == anOriginalEdgeIndex + THE_QUAD / 2) {
59       anOppositeEdge = anExp.current();
60       if (aNbEdges >= THE_QUAD)
61         break;
62     }
63
64     ++aNbEdges;
65     anExp.next();
66     if (!anExp.more()) {
67       if (aNbEdges != THE_QUAD) {
68         // not quad face
69         anOppositeEdge = GeomShapePtr();
70         break;
71       }
72       if (!anOppositeEdge)
73         anExp.reinit();
74     }
75   }
76   return anOppositeEdge;
77 }
78
79 // Find all opposite edges for the given.
80 static void cacheOppositeEdge(const GeomShapePtr theEdge,
81                               const MapShapeAndAncestors& theEdgeToFaces,
82                               SetOfShapes& theCache)
83 {
84   MapShapeAndAncestors::const_iterator aFound = theEdgeToFaces.find(theEdge);
85   if (aFound == theEdgeToFaces.end())
86     return;
87
88   for (SetOfShapes::const_iterator aFIt = aFound->second.begin();
89        aFIt != aFound->second.end(); ++aFIt) {
90     GeomShapePtr anOpposite = oppositeEdgeInQuadFace(theEdge, *aFIt);
91     if (anOpposite && theCache.find(anOpposite) == theCache.end()) {
92       theCache.insert(anOpposite);
93       cacheOppositeEdge(anOpposite, theEdgeToFaces, theCache);
94     }
95   }
96 }
97
98 static void cacheOppositeEdges(const GeomShapePtr theTopLevelShape,
99                                const GeomShapePtr theEdge,
100                                SetOfShapes& theCache)
101 {
102   if (!theTopLevelShape || !theEdge)
103     return;
104
105   MapShapeAndAncestors anEdgesToFaces;
106   mapEdgesAndFaces(theTopLevelShape, anEdgesToFaces);
107
108   // keep the original edge
109   theCache.insert(theEdge);
110   // cache opposite edges
111   cacheOppositeEdge(theEdge, anEdgesToFaces, theCache);
112 }
113
114
115 bool FiltersPlugin_OppositeToEdge::isSupported(GeomAPI_Shape::ShapeType theType) const
116 {
117   return theType == GeomAPI_Shape::EDGE;
118 }
119
120 bool FiltersPlugin_OppositeToEdge::isOk(const GeomShapePtr& theShape,
121                                         const ModelAPI_FiltersArgs& theArgs) const
122 {
123   AttributePtr aAttr = theArgs.argument("OppositeToEdge");
124   AttributeSelectionPtr aList = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
125   if (!aList.get())
126     return false;
127   GeomShapePtr anEdge = aList->value();
128   if (!myOriginalEdge || !myOriginalEdge->isSame(anEdge)) {
129     // new edge is selected, need to update the cache
130     const_cast<FiltersPlugin_OppositeToEdge*>(this)->myOriginalEdge = anEdge;
131     const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes.clear();
132   }
133
134   if (myCachedShapes.empty()) {
135     ResultBodyPtr aBaseResult = ModelAPI_Tools::bodyOwner(aList->context(), true);
136     if (!aBaseResult.get())
137       return false;
138
139     cacheOppositeEdges(aBaseResult->shape(), anEdge,
140         const_cast<FiltersPlugin_OppositeToEdge*>(this)->myCachedShapes);
141   }
142
143   return myCachedShapes.find(theShape) != myCachedShapes.end();
144 }
145
146 static std::string XMLRepresentation =
147 "<filter id = \"OppositeToEdge\">"
148 " <shape_selector id=\"OppositeToEdge__OppositeToEdge\""
149 "   label=\"Edge:\""
150 "   tooltip=\"Select edge.\""
151 "   shape_types=\"edges\">"
152 "   <validator id=\"GeomValidators_ShapeType\" parameters=\"line\"/>"
153 " </shape_selector>"
154 "</filter>";
155
156
157 std::string FiltersPlugin_OppositeToEdge::xmlRepresentation() const
158 {
159   return XMLRepresentation;
160 }
161
162 void FiltersPlugin_OppositeToEdge::initAttributes(ModelAPI_FiltersArgs& theArguments)
163 {
164   theArguments.initAttribute("OppositeToEdge", ModelAPI_AttributeSelection::typeId());
165 }