Salome HOME
Copyright update 2020
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Filling.cpp
1 // Copyright (C) 2017-2020  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 "BuildPlugin_Filling.h"
21
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultBody.h>
28
29 #include <GeomAlgoAPI_Copy.h>
30 #include <GeomAlgoAPI_Filling.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_Tools.h>
33
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_Wire.h>
37
38 #include <cmath>
39
40 struct FillingParameters
41 {
42   std::string method;
43   int minDegree;
44   int maxDegree;
45   int nbIter;
46   double tol2D;
47   double tol3D;
48   bool isApprox;
49 };
50
51
52 //=================================================================================================
53 BuildPlugin_Filling::BuildPlugin_Filling()
54 {
55 }
56
57 //=================================================================================================
58 void BuildPlugin_Filling::initAttributes()
59 {
60   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
61   data()->addAttribute(ADVANCED_OPTIONS_ID(), ModelAPI_AttributeString::typeId());
62   data()->addAttribute(METHOD_ID(), ModelAPI_AttributeString::typeId());
63   data()->addAttribute(MINIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
64   data()->addAttribute(MAXIMAL_DEGREE_ID(), ModelAPI_AttributeInteger::typeId());
65   data()->addAttribute(NUMBER_OF_ITERATIONS_ID(), ModelAPI_AttributeInteger::typeId());
66   data()->addAttribute(TOLERANCE_2D_ID(), ModelAPI_AttributeDouble::typeId());
67   data()->addAttribute(TOLERANCE_3D_ID(), ModelAPI_AttributeDouble::typeId());
68   data()->addAttribute(APPROXIMATION_ID(), ModelAPI_AttributeBoolean::typeId());
69
70   if (string(ADVANCED_OPTIONS_ID())->value().empty())
71     restoreDefaultParameters();
72 }
73
74 //=================================================================================================
75 void BuildPlugin_Filling::execute()
76 {
77   // get parameters of algorithm
78   FillingParameters aParameters;
79   aParameters.method = string(METHOD_ID())->value();
80   aParameters.minDegree = integer(MINIMAL_DEGREE_ID())->value();
81   aParameters.maxDegree = integer(MAXIMAL_DEGREE_ID())->value();
82   aParameters.nbIter = integer(NUMBER_OF_ITERATIONS_ID())->value();
83   aParameters.tol2D = real(TOLERANCE_2D_ID())->value();
84   aParameters.tol3D = real(TOLERANCE_3D_ID())->value();
85   aParameters.isApprox = boolean(APPROXIMATION_ID())->value();
86
87   if (aParameters.minDegree > aParameters.maxDegree) {
88     setError("Error: " + getKind() + " algorithm failed (max deg < min deg).");
89     return;
90   }
91
92   std::shared_ptr<GeomAlgoAPI_Filling> aFilling(
93       new GeomAlgoAPI_Filling(aParameters.minDegree, aParameters.maxDegree, aParameters.nbIter,
94                               aParameters.tol2D, aParameters.tol3D));
95
96   // get base objects list
97   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
98   if (aSelectionList->size() <= 1) {
99     setError("Not enough objects is selected");
100     return;
101   }
102
103   // collect base shapes
104   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
105     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
106     GeomEdgePtr anEdge = toEdge(aSelection->value(), aParameters.method);
107     if (!anEdge) {
108       myLastEdgeStartPoint = GeomPointPtr();
109       myLastEdgeEndPoint = GeomPointPtr();
110       return;
111     }
112     aFilling->add(anEdge);
113   }
114   myLastEdgeStartPoint = GeomPointPtr();
115   myLastEdgeEndPoint = GeomPointPtr();
116
117   // build result
118   aFilling->build(aParameters.isApprox);
119   std::string anError;
120   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFilling, getKind(), anError)) {
121     setError(anError);
122     removeResults(0);
123     return;
124   }
125
126   /// store result
127   GeomShapePtr aCreatedFace = aFilling->shape();
128   ResultBodyPtr aResultBody = document()->createBody(data());
129   aResultBody->store(aCreatedFace);
130   // store edges
131   int anEdgeInd = 0;
132   for(GeomAPI_ShapeExplorer anExp(aCreatedFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
133     GeomShapePtr anEdge = anExp.current();
134     aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeInd));
135   }
136   setResult(aResultBody, 0);
137 }
138
139 //=================================================================================================
140 void BuildPlugin_Filling::attributeChanged(const std::string& theID)
141 {
142   if (theID == ADVANCED_OPTIONS_ID() && string(ADVANCED_OPTIONS_ID())->value().empty()) {
143     // Advanced options flag just unchecked => restore default state of all parameters
144     restoreDefaultParameters();
145   }
146 }
147
148 //=================================================================================================
149 GeomEdgePtr BuildPlugin_Filling::toEdge(const GeomShapePtr& theShape, const std::string& theMethod)
150 {
151   GeomEdgePtr anEdge;
152   switch (theShape->shapeType()) {
153   case GeomAPI_Shape::EDGE:
154     anEdge = GeomEdgePtr(new GeomAPI_Edge(GeomAlgoAPI_Copy(theShape).shape()));
155     break;
156   case GeomAPI_Shape::WIRE:
157     anEdge = GeomAlgoAPI_ShapeTools::wireToEdge(
158         GeomWirePtr(new GeomAPI_Wire(theShape)));
159     break;
160   default:
161     break;
162   }
163
164   if (!anEdge || anEdge->empty()) {
165     static const std::string aFeatureError =
166         "Error: incorrect type of input feature (edges/wire are supported only).";
167     setError(aFeatureError);
168     return anEdge;
169   }
170
171   // correct edge orientation according to filling method
172   if (theMethod == Method::AUTO_CORRECT_ORIENTATION()) {
173     // check the distance to previous edge boundaries, reverse edge if necessary
174     GeomPointPtr aStartPnt = anEdge->firstPoint();
175     GeomPointPtr aEndPnt = anEdge->lastPoint();
176     if (anEdge->orientation() == GeomAPI_Shape::REVERSED) {
177       aStartPnt = anEdge->lastPoint();
178       aEndPnt = anEdge->firstPoint();
179     }
180     bool isReverse = false;
181     if (myLastEdgeStartPoint) {
182       double d1 = myLastEdgeStartPoint->distance(aStartPnt)
183                 + myLastEdgeEndPoint->distance(aEndPnt);
184       double d2 = myLastEdgeStartPoint->distance(aEndPnt)
185                 + myLastEdgeEndPoint->distance(aStartPnt);
186       if (fabs(d1 - d2) < 1.e-7) {
187         // undefined case => check distance to start point only
188         d1 = myLastEdgeStartPoint->distance(aStartPnt);
189         d2 = myLastEdgeStartPoint->distance(aEndPnt);
190       }
191       isReverse = d2 < d1;
192     }
193
194     if (isReverse) {
195       anEdge->reverse();
196       myLastEdgeStartPoint = aEndPnt;
197       myLastEdgeEndPoint = aStartPnt;
198     } else {
199       myLastEdgeStartPoint = aStartPnt;
200       myLastEdgeEndPoint = aEndPnt;
201     }
202   }
203   else if (theMethod == Method::USE_CURVE_INFORMATION()) {
204     // make all edges FORWARD to avoid reversing the curves by GeomAlgoAPI_Filling algorithm
205     anEdge->setOrientation(GeomAPI_Shape::FORWARD);
206   }
207   return anEdge;
208 }
209
210 //=================================================================================================
211 void BuildPlugin_Filling::restoreDefaultParameters()
212 {
213   string(METHOD_ID())->setValue(METHOD_DEFAULT());
214   integer(MINIMAL_DEGREE_ID())->setValue(MINIMAL_DEGREE_DEFAULT());
215   integer(MAXIMAL_DEGREE_ID())->setValue(MAXIMAL_DEGREE_DEFAULT());
216   integer(NUMBER_OF_ITERATIONS_ID())->setValue(NUMBER_OF_ITERATIONS_DEFAULT());
217   real(TOLERANCE_2D_ID())->setValue(TOLERANCE_2D_DEFAULT());
218   real(TOLERANCE_3D_ID())->setValue(TOLERANCE_3D_DEFAULT());
219   boolean(APPROXIMATION_ID())->setValue(APPROXIMATION_DEFAULT());
220 }