Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment. Validator for selecti...
[modules/shaper.git] / src / ModelGeomAlgo / ModelGeomAlgo_Point2D.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Tools.cpp
4 // Created:     20 Jul 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "ModelGeomAlgo_Point2D.h"
8
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12
13 #include <GeomAlgoAPI_ShapeTools.h>
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <GeomAPI_Pnt.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Vertex.h>
19 #include <GeomAPI_Dir.h>
20 #include <GeomAPI_Edge.h>
21 #include <GeomAPI_Lin.h>
22 #include <GeomAPI_Circ.h>
23
24 namespace ModelGeomAlgo_Point2D {
25   std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(ModelAPI_Feature* theFeature,
26                                                          const std::string& theAttribute,
27                                                          const std::string& theObjectFeatureKind,
28                                                          const std::string& theObjectFeatureAttribute)
29   {
30     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
31
32     /// essential check as it is called in openGl thread
33     if (!theFeature || !theFeature->data().get() || !theFeature->data()->isValid())
34       return std::shared_ptr<GeomDataAPI_Point2D>();
35
36     FeaturePtr aFeature;
37     std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
38         ModelAPI_AttributeRefAttr>(theFeature->data()->attribute(theAttribute));
39     if(anAttr.get() && anAttr->isInitialized()) {
40       aFeature = ModelAPI_Feature::feature(anAttr->object());
41       if (aFeature.get()) {
42         bool aFeatureOfObjectKind = !theObjectFeatureKind.empty() &&
43                                     !theObjectFeatureAttribute.empty() &&
44                                     aFeature->getKind() == theObjectFeatureKind;
45         if(aFeatureOfObjectKind)
46             aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
47                                     aFeature->data()->attribute(theObjectFeatureAttribute));
48         else if (anAttr->attr())
49           aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
50       }
51     }
52     return aPointAttr;
53   }
54
55   void getPointsOfReference(const std::shared_ptr<ModelAPI_Object>& theObject,
56                             const std::string& theReferenceFeatureKind,
57                             std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
58                             const std::string& theObjectFeatureKind,
59                             const std::string& theObjectFeatureAttribute,
60                             const bool isSkipFeatureAttributes)
61   {
62     // find by feature
63     FeaturePtr aSourceFeature = ModelAPI_Feature::feature(theObject);
64
65     const std::set<AttributePtr>& aRefsList = theObject->data()->refsToMe();
66     std::set<AttributePtr>::const_iterator aIt;
67     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
68       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
69       FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
70       if (aRefFeature->getKind() == theReferenceFeatureKind) {
71         std::list<AttributePtr> anAttributes =
72                          aRefFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
73         std::list<AttributePtr>::iterator anIter = anAttributes.begin(), aLast = anAttributes.end();
74         bool isSkippedAttribute = false;
75         if (isSkipFeatureAttributes) {
76           for(anIter = anAttributes.begin(); anIter != aLast && !isSkippedAttribute; anIter++) {
77             AttributeRefAttrPtr aRefAttribute =
78               std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
79             if (aRefAttribute.get() && !aRefAttribute->isObject()) {
80               std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
81                              std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttribute->attr());
82               FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aPointAttr->owner());
83               isSkippedAttribute = aSourceFeature == anAttributeFeature;
84             }
85           }
86         }
87         if (isSkippedAttribute)
88           continue;
89
90         // it searches the first point of AttributeRefAtt
91         std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
92         for(anIter = anAttributes.begin(); anIter != aLast && !aPointAttr.get(); anIter++) {
93           AttributeRefAttrPtr aRefAttribute =
94             std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
95           if (aRefAttribute.get()) {
96             aPointAttr = getPointOfRefAttr(aRefFeature.get(), aRefAttribute->id(),
97                          theObjectFeatureKind, theObjectFeatureAttribute);
98           }
99         }
100         if (aPointAttr.get()) {
101           theAttributes.insert(aPointAttr);
102         }
103       }
104     }
105     // find by results
106     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
107     if (aFeature.get()) {
108       const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
109       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
110       for (; aRIter != aResults.cend(); aRIter++) {
111         ResultPtr aResult = *aRIter;
112         getPointsOfReference(aResult, theReferenceFeatureKind, theAttributes, theObjectFeatureKind,
113                              theObjectFeatureAttribute);
114       }
115     }
116   }
117
118   void getPointsInsideShape(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
119                             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
120                             const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
121                             const std::shared_ptr<GeomAPI_Dir>& theDirX,
122                             const std::shared_ptr<GeomAPI_Dir>& theDirY,
123                             std::set<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
124   {
125     std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator anIt = theAttributes.begin(),
126                                                             aLast = theAttributes.end();
127     for (; anIt != aLast; anIt++) {
128       std::shared_ptr<GeomDataAPI_Point2D> anAttribute = *anIt;
129       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = anAttribute->pnt();
130       std::shared_ptr<GeomAPI_Pnt> aPoint = aPnt2d->to3D(theOrigin, theDirX, theDirY);
131       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
132       if (isPointOnEdge(theBaseShape, aPoint, aProjectedPoint))
133         thePoints.insert(aProjectedPoint);
134     }
135   }
136
137   bool isPointOnEdge(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
138                      const std::shared_ptr<GeomAPI_Pnt>& thePoint,
139                      std::shared_ptr<GeomAPI_Pnt>& theProjectedPoint)
140   {
141     bool isInside = false;
142     if (theBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
143       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theBaseShape));
144       if (anEdge->isLine()) {
145         std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
146         theProjectedPoint = aLine->project(thePoint);
147       }
148       else if (anEdge->isCircle() || anEdge->isArc()) {
149         std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
150         theProjectedPoint = aCircle->project(thePoint);
151       }
152       if (theProjectedPoint.get()) {
153         std::shared_ptr<GeomAPI_Vertex> aVertexShape(new GeomAPI_Vertex(theProjectedPoint->x(),
154                                                   theProjectedPoint->y(), theProjectedPoint->z()));
155         isInside = GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aVertexShape, theBaseShape);
156       }
157     }
158     return isInside;
159   }
160
161 } // namespace ModelGeomAlgo_Point2D