Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_EllipticArc.cpp
1 // Copyright (C) 2017-2019  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 <SketchPlugin_EllipticArc.h>
21 #include <SketchPlugin_Sketch.h>
22
23 #include <GeomAlgoAPI_EdgeBuilder.h>
24
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Ellipse.h>
28 #include <GeomAPI_Ellipse2d.h>
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_XY.h>
31
32 #include <GeomDataAPI_Point2D.h>
33
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_ResultConstruction.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
38
39 #include <cmath>
40
41 static const double tolerance = 1e-7;
42 static const double paramTolerance = 1.e-4;
43 static const double PI = 3.141592653589793238463;
44
45
46 SketchPlugin_EllipticArc::SketchPlugin_EllipticArc()
47   : SketchPlugin_SketchEntity(),
48     myParamDelta(0.0)
49 {
50 }
51
52 void SketchPlugin_EllipticArc::initDerivedClassAttributes()
53 {
54   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(FIRST_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
56   data()->addAttribute(SECOND_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
57   data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
58   data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
59   data()->addAttribute(MINOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
60   data()->addAttribute(MINOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
61   data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
63   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
64   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
65
66   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
67
68   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_FOCUS_ID());
69   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_START_ID());
70   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_ID());
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_START_ID());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_END_ID());
73   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_RADIUS_ID());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_RADIUS_ID());
75
76   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
77   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
78 }
79
80 void SketchPlugin_EllipticArc::execute()
81 {
82   SketchPlugin_Sketch* aSketch = sketch();
83   if(!aSketch) {
84     return;
85   }
86
87   // Calculate all characteristics of the ellipse.
88   fillCharacteristicPoints();
89
90   // Make a visible ellipse.
91   createEllipticArc(aSketch);
92 }
93
94 bool SketchPlugin_EllipticArc::isFixed() {
95   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
96 }
97
98 void SketchPlugin_EllipticArc::attributeChanged(const std::string& theID) {
99   // the second condition for unability to move external segments anywhere
100   if (theID == EXTERNAL_ID() || isFixed()) {
101     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
102     if (!aSelection) {
103       // empty shape in selection shows that the shape is equal to context
104       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
105       if (anExtRes)
106         aSelection = anExtRes->shape();
107     }
108     // update arguments due to the selection value
109     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
110       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelection));
111       std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
112
113       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
114         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
115       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
116
117       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
118         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
119       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
120
121       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
122         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
123       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
124
125       std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
126         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
127       aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
128
129       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
130       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
131     }
132   }
133   else if (theID == CENTER_ID() || theID == FIRST_FOCUS_ID() ||
134            theID == START_POINT_ID() || theID == END_POINT_ID())
135     fillCharacteristicPoints();
136 }
137
138 static void calculateRadii(const GeomPnt2dPtr& theCenter,
139                            const GeomPnt2dPtr& theFocus,
140                            const GeomPnt2dPtr& thePassed,
141                            double& theMajorRadius,
142                            double& theMinorRadius)
143 {
144   GeomPnt2dPtr aSecondFocus(new GeomAPI_Pnt2d(
145       theCenter->xy()->multiplied(2.0)->decreased(theFocus->xy())));
146   theMajorRadius = 0.5 * (thePassed->distance(theFocus) + thePassed->distance(aSecondFocus));
147
148   GeomDir2dPtr aXAxis(new GeomAPI_Dir2d(theFocus->x() - theCenter->x(),
149                                         theFocus->y() - theCenter->y()));
150   std::shared_ptr<GeomAPI_XY> aPassedVec = thePassed->xy()->decreased(theCenter->xy());
151
152   double x = aPassedVec->dot(aXAxis->xy()) / theMajorRadius;
153   double y = abs(aPassedVec->cross(aXAxis->xy()));
154   theMinorRadius = y / sqrt(1.0 - x * x);
155 }
156
157 bool SketchPlugin_EllipticArc::fillCharacteristicPoints()
158 {
159   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
160     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
161   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
162     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
163   std::shared_ptr<GeomDataAPI_Point2D> aStartPointAttr =
164     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
165
166   if (!aCenterAttr->isInitialized() ||
167     !aFocusAttr->isInitialized() ||
168     !aStartPointAttr->isInitialized()) {
169     return false;
170   }
171
172   data()->blockSendAttributeUpdated(true);
173   GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
174   GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
175   GeomPnt2dPtr aStart2d = aStartPointAttr->pnt();
176
177   double aMajorRadius = 0.0, aMinorRadius = 0.0;
178   calculateRadii(aCenter2d, aFocus2d, aStart2d, aMajorRadius, aMinorRadius);
179   if (aMinorRadius < tolerance)
180     return false;
181   real(MAJOR_RADIUS_ID())->setValue(aMajorRadius);
182   real(MINOR_RADIUS_ID())->setValue(aMinorRadius);
183
184   GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
185     aFocus2d->y() - aCenter2d->y()));
186   GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
187
188   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
189     ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
190   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
191     ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
192       aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
193   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
194     ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
195       aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
196   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
197     ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
198       aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
199   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
200     ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
201       aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
202
203   std::shared_ptr<GeomDataAPI_Point2D> aEndPointAttr =
204     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
205   if (aEndPointAttr->isInitialized()) {
206     // recalculate REVERSED flag
207     std::shared_ptr<GeomAPI_Ellipse2d> anEllipseForArc(
208         new GeomAPI_Ellipse2d(aCenter2d, aMajorDir2d, aMajorRadius, aMinorRadius));
209     GeomPnt2dPtr anEnd = aEndPointAttr->pnt();
210     std::shared_ptr<GeomAPI_Pnt2d> aProjection = anEllipseForArc->project(anEnd);
211     double aParamStart = 0.0, aParamEnd = 0.0;
212     if (aProjection && anEnd->distance(aProjection) <= tolerance &&
213         anEllipseForArc->parameter(anEnd, paramTolerance, aParamEnd)) {
214       // do not recalculate REVERSED flag if the arc is not consistent
215       anEllipseForArc->parameter(aStart2d, paramTolerance, aParamStart);
216       aParamEnd -= aParamStart;
217
218       if (myParamDelta >= 0.0 && myParamDelta <= PI * 0.5 &&
219           aParamEnd < 0 && aParamEnd >= -PI * 0.5) {
220         boolean(REVERSED_ID())->setValue(true);
221       }
222       else if (myParamDelta <= 0.0 && myParamDelta >= -PI * 0.5 &&
223                aParamEnd > 0.0 && aParamEnd <= PI * 0.5) {
224         boolean(REVERSED_ID())->setValue(false);
225       }
226     }
227     myParamDelta = aParamEnd;
228   }
229   data()->blockSendAttributeUpdated(false);
230
231   return true;
232 }
233
234 void SketchPlugin_EllipticArc::createEllipticArc(SketchPlugin_Sketch* theSketch)
235 {
236   // Compute a ellipse in 3D view.
237   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
238     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
239   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
240     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
241
242   double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
243   double aMinorRadius = real(MINOR_RADIUS_ID())->value();
244
245   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
246     theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
247
248   GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
249   GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
250   GeomDirPtr aNormal = aNDir->dir();
251   std::shared_ptr<GeomAPI_Shape> anEllipseShape;
252   if (aFocus->distance(aCenter) > tolerance) {
253     GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
254         aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
255
256     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
257         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
258     std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
259         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
260
261     GeomPointPtr aStartPnt(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
262     GeomPointPtr aEndPnt(theSketch->to3D(aEndAttr->x(), aEndAttr->y()));
263     if (boolean(REVERSED_ID())->value())
264       std::swap(aStartPnt, aEndPnt);
265
266     anEllipseShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxis,
267         aMajorRadius, aMinorRadius, aStartPnt, aEndPnt);
268   }
269   else {
270     // build circle instead of ellipse
271     anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
272   }
273
274   ResultConstructionPtr aResult = document()->createConstruction(data());
275   aResult->setShape(anEllipseShape);
276   aResult->setIsInHistory(false);
277   setResult(aResult);
278 }