Salome HOME
1311f5719185691c4419e7f44b48c38c12cbe3f4
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_FaceSize.cpp
1 // Copyright (C) 2014-2021  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_FaceSize.h"
21
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
29 #include <GeomAPI_Wire.h>
30
31 #include <Precision.hxx>
32
33 #include <map>
34 #include <cmath>
35
36 //=================================================================================================
37 bool FiltersPlugin_FaceSize::isSupported(GeomAPI_Shape::ShapeType theType) const
38 {
39   return theType == GeomAPI_Shape::FACE;
40 }
41
42 //=================================================================================================
43 bool FiltersPlugin_FaceSize::isOk(const GeomShapePtr& theShape, const ResultPtr&,
44                                   const ModelAPI_FiltersArgs& theArgs) const
45 {
46   AttributePtr anAttr = theArgs.argument("value");
47   AttributeDoublePtr aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
48   if (!aValue.get()|| !anAttr->isInitialized() )
49     return false;
50   double aVal = aValue->value();
51
52   anAttr = theArgs.argument("valueMax");
53   aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
54   if (!aValue.get()|| !anAttr->isInitialized() )
55     return false;
56   double aValMax = aValue->value();
57
58   if (aVal < 0.0)
59     return false;
60
61   double aSurfArea = GeomAlgoAPI_ShapeTools::area(theShape);
62
63   anAttr = theArgs.argument("comparatorType");
64   AttributeStringPtr aCompAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(anAttr);
65   if (!aCompAttr)
66     return false;
67   std::string aCompString = aCompAttr->value();
68
69   bool isOK = false;
70   if (aCompString == "inf")
71     isOK = aSurfArea < aVal - Precision::Confusion();
72   else if (aCompString == "infEq")
73     isOK = aSurfArea < aVal + Precision::Confusion();
74   else if (aCompString == "sup")
75     isOK = aSurfArea > aVal + Precision::Confusion();
76   else if (aCompString == "supEq")
77     isOK = aSurfArea > aVal - Precision::Confusion();
78   else if (aCompString == "isBetween")
79     isOK = aVal <= aValMax
80            && aSurfArea > aVal - Precision::Confusion()
81            && aSurfArea < aValMax + Precision::Confusion();
82   else if (aCompString == "isStrictlyBetween")
83     isOK = aVal <= aValMax
84            && aSurfArea > aVal + Precision::Confusion()
85            && aSurfArea < aValMax - Precision::Confusion();
86   return isOK;
87 }
88
89 //=================================================================================================
90 std::string FiltersPlugin_FaceSize::xmlRepresentation() const
91 {
92   return xmlFromFile("filter-FaceSize.xml");
93 }
94
95 //=================================================================================================
96 void FiltersPlugin_FaceSize::initAttributes(ModelAPI_FiltersArgs& theArguments)
97 {
98   theArguments.initAttribute("comparatorType", ModelAPI_AttributeString::typeId());
99   theArguments.initAttribute("value", ModelAPI_AttributeDouble::typeId());
100   theArguments.initAttribute("valueMax", ModelAPI_AttributeDouble::typeId());
101 }