]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelGeomAlgo/ModelGeomAlgo_Point2D.cpp
Salome HOME
Split preview correction: it is provided by AISObject of presentable interface
[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::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
124                             std::map<std::shared_ptr<GeomDataAPI_Point2D>,
125                                      std::shared_ptr<GeomAPI_Pnt> >& theAttributeToPoint)
126   {
127     std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator anIt = theAttributes.begin(),
128                                                             aLast = theAttributes.end();
129     for (; anIt != aLast; anIt++) {
130       std::shared_ptr<GeomDataAPI_Point2D> anAttribute = *anIt;
131       std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = anAttribute->pnt();
132       std::shared_ptr<GeomAPI_Pnt> aPoint = aPnt2d->to3D(theOrigin, theDirX, theDirY);
133       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
134       if (isPointOnEdge(theBaseShape, aPoint, aProjectedPoint)) {
135         thePoints.push_back(aProjectedPoint);
136         theAttributeToPoint[anAttribute] = aProjectedPoint;
137       }
138     }
139   }
140
141   bool isPointOnEdge(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
142                      const std::shared_ptr<GeomAPI_Pnt>& thePoint,
143                      std::shared_ptr<GeomAPI_Pnt>& theProjectedPoint)
144   {
145     bool isInside = false;
146     if (theBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
147       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theBaseShape));
148       if (anEdge->isLine()) {
149         std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
150         theProjectedPoint = aLine->project(thePoint);
151       }
152       else if (anEdge->isCircle() || anEdge->isArc()) {
153         std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
154         theProjectedPoint = aCircle->project(thePoint);
155       }
156       if (theProjectedPoint.get()) {
157         std::shared_ptr<GeomAPI_Vertex> aVertexShape(new GeomAPI_Vertex(theProjectedPoint->x(),
158                                                   theProjectedPoint->y(), theProjectedPoint->z()));
159         isInside = GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(aVertexShape, theBaseShape);
160       }
161     }
162     return isInside;
163   }
164
165   std::string doubleToString(double theValue)
166   {
167     std::string aValueStr;
168     char aBuf[50];
169     int n = sprintf(aBuf, "%g", theValue);
170     aValueStr = std::string(aBuf);
171     return aValueStr;
172   }
173
174   std::string getPontAttributesInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
175                                     const std::set<std::shared_ptr<ModelAPI_Attribute> >& theAttributesOnly)
176   {
177     std::string anInfo;
178
179     std::list<AttributePtr> anAttrs = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
180     std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
181
182     for(; anIt != aLast; anIt++) {
183       AttributePtr anAttribute = *anIt;
184       if (anAttribute.get() && (theAttributesOnly.empty() ||
185           theAttributesOnly.find(anAttribute) != theAttributesOnly.end())) {
186       if (!anInfo.empty()) {
187         anInfo.append(", ");
188         anInfo.append("\n");
189       }
190       anInfo.append("    " + getPointAttributeInfo(anAttribute));
191       }
192     }
193     return anInfo;
194   }
195
196   std::string getPointAttributeInfo(const std::shared_ptr<ModelAPI_Attribute>& theAttribute)
197   {
198     std::string anInfo;
199     std::string aValue = "not defined";
200     std::string aType = theAttribute->attributeType();
201     if (aType == GeomDataAPI_Point2D::typeId()) {
202       std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203                                                                                       theAttribute);
204       if (aPoint.get() && aPoint->isInitialized()) {
205         aValue = std::string("(" + doubleToString(aPoint->x()) + ", "+ doubleToString(aPoint->y()) + ")");
206       }
207     }
208     anInfo.append(theAttribute->id() + ": " + aValue);
209
210     return anInfo;
211   }
212
213 } // namespace ModelGeomAlgo_Point2D