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