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