Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
1 // Copyright (C) 2014-2022  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 <GeomValidators_Different.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomAPI_Pnt2d.h>
26
27 #include <algorithm>
28 #include <map>
29 #include <list>
30
31 const double tolerance = 1e-7;
32
33 //=================================================================================================
34 /* Help
35 To extend GeomValidators_Different validator with new attribute types:
36 1. Modify function isEqualAttributes
37 2. Create new implementation of isEqual() for the new type
38 */
39 //=================================================================================================
40
41 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
42 {
43   return theLeft->isInitialized() && theRight->isInitialized() &&
44     theLeft->pnt()->distance(theRight->pnt()) < tolerance;
45 }
46
47 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
48 {
49   if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() &&
50       theRight->attributeType() == GeomDataAPI_Point2D::typeId())
51     return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
52                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
53   return false;
54 }
55
56 /** \class IsEqual
57  *  \ingroup Validators
58  *  \brief Auxiliary class used in std::find_if
59  */
60 class IsEqual {
61   AttributePtr myAttribute;
62 public:
63   /// Constructor
64   IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
65   /// \return true in case if AttributePtr is equal with myAttribute
66   bool operator()(const AttributePtr& theAttribute) {
67     return isEqualAttributes(myAttribute, theAttribute);
68   }
69 };
70
71 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
72                                        const std::list<std::string>& theArguments,
73                                        Events_InfoMessage& theError) const
74 {
75   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
76   // For all attributes referred by theArguments
77   // sort it using attributeType() and store into anAttributesMap
78   std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
79   for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
80     AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
81     anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
82   }
83
84   // Search differences inside each attribute list
85   std::map<std::string, std::list<AttributePtr> >::const_iterator
86     anAttributesMapIt = anAttributesMap.begin();
87   for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
88     const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
89     // for the list of attributes check that all elements are unique
90     std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
91     if (anAttributeIt != anAttributes.end()) {
92       std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
93       while (aNextIt != anAttributes.end()) {
94         // if equal attribute is found then all attributes are not different
95         std::list<AttributePtr>::const_iterator aFindIt =
96             std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
97         if (aFindIt != anAttributes.end()) {
98           theError = "Attributes " + (*anAttributeIt)->id() + " and " +
99             (*aFindIt)->id() + " are equal." ;
100           return false;
101         }
102         ++anAttributeIt;
103         ++aNextIt;
104       }
105     }
106   }
107
108   return true;
109 }
110
111 // LCOV_EXCL_START
112 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
113 {
114   return true;
115 }
116 // LCOV_EXCL_STOP