Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_Different.cpp
4 // Created:     08 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include <GeomValidators_Different.h>
8
9 #include <Events_InfoMessage.h>
10
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomAPI_Pnt2d.h>
13
14 #include <algorithm>
15 #include <map>
16 #include <list>
17
18 const double tolerance = 1e-7;
19
20 //=================================================================================================
21 /* Help
22 To extend GeomValidators_Different validator with new attribute types:
23 1. Modify function isEqualAttributes
24 2. Create new implementation of isEqual() for the new type
25 */
26 //=================================================================================================
27
28 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
29 {
30   return theLeft->isInitialized() && theRight->isInitialized() &&
31     theLeft->pnt()->distance(theRight->pnt()) < tolerance;
32 }
33
34 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
35 {
36   if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() && 
37       theRight->attributeType() == GeomDataAPI_Point2D::typeId())
38     return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
39                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
40   return false;
41 }
42
43 /** \class IsEqual
44  *  \ingroup Validators
45  *  \brief Auxiliary class used in std::find_if
46  */
47 class IsEqual {
48   AttributePtr myAttribute;
49 public:
50   /// Constructor
51   IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
52   /// \return true in case if AttributePtr is equal with myAttribute
53   bool operator()(const AttributePtr& theAttribute) {
54     return isEqualAttributes(myAttribute, theAttribute);
55   }
56 };
57
58 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
59                                        const std::list<std::string>& theArguments,
60                                        Events_InfoMessage& theError) const
61 {
62   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
63   // For all attributes referred by theArguments 
64   // sort it using attributeType() and store into anAttributesMap 
65   std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
66   for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
67     AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
68     anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
69   }
70
71   // Search differences inside each attribute list
72   std::map<std::string, std::list<AttributePtr> >::const_iterator 
73     anAttributesMapIt = anAttributesMap.begin();
74   for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
75     const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
76     // for the list of attributes check that all elements are unique
77     std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
78     if (anAttributeIt != anAttributes.end()) {
79       std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
80       while (aNextIt != anAttributes.end()) {
81         // if equal attribute is found then all attributes are not different
82         std::list<AttributePtr>::const_iterator aFindIt =
83             std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
84         if (aFindIt != anAttributes.end()) {
85           theError = "Attributes " + (*anAttributeIt)->id() + " and " + 
86             (*aFindIt)->id() + " are equal." ;
87           return false;
88         }
89         ++anAttributeIt;
90         ++aNextIt; 
91       }
92     }
93   }
94
95   return true;
96 }
97
98 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
99 {
100   return true;
101 }