Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeAPI.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_ShapeAPI.h"
22
23 #include <GeomAlgoAPI_Box.h>
24 #include <GeomAlgoAPI_Cylinder.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
26 #include <GeomAlgoAPI_ConeSegment.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
28 #include <GeomAlgoAPI_Rotation.h>
29 #include <GeomAlgoAPI_Scale.h>
30 #include <GeomAlgoAPI_Symmetry.h>
31 #include <GeomAlgoAPI_Translation.h>
32
33 #include <GeomAPI_Lin.h>
34
35 #include <math.h>
36
37 namespace GeomAlgoAPI_ShapeAPI
38 {
39   //===============================================================================================
40   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
41     const double theDx, const double theDy,
42     const double theDz) throw (GeomAlgoAPI_Exception)
43   {
44     GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
45
46     if (!aBoxAlgo.check()) {
47       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
48     }
49
50     aBoxAlgo.build();
51
52     if(!aBoxAlgo.isDone()) {
53       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
54     }
55     if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
56       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
57     }
58     return aBoxAlgo.shape();
59   }
60
61   //===============================================================================================
62   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
63     std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
64     std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
65   {
66     GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
67
68     if (!aBoxAlgo.check()) {
69       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
70     }
71
72     aBoxAlgo.build();
73
74     if(!aBoxAlgo.isDone()) {
75       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
76     }
77     if (!aBoxAlgo.checkValid("Box builder with two points")) {
78       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
79     }
80     return aBoxAlgo.shape();
81   }
82
83   //===============================================================================================
84   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
85     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
86     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
87   {
88     // Check if the base point is OK
89     if (!theBasePoint) {
90       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
91     }
92     // Check if the edge is OK
93     if (!theEdge) {
94       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
95     }
96
97     std::shared_ptr<GeomAPI_Ax2> anAxis;
98     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
99                                                           theEdge->line()->direction()));
100
101     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
102
103     if (!aCylinderAlgo.check()) {
104       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
105     }
106
107     aCylinderAlgo.build();
108
109     if(!aCylinderAlgo.isDone()) {
110       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
111     }
112     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
113       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
114     }
115     return aCylinderAlgo.shape();
116   }
117
118   //===============================================================================================
119   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
120     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
121     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
122   {
123     // Check if the base point is OK
124     if (!theBasePoint) {
125       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
126     }
127     // Check if the edge is OK
128     if (!theEdge) {
129       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
130     }
131
132     std::shared_ptr<GeomAPI_Ax2> anAxis;
133     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
134                                                           theEdge->line()->direction()));
135
136     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
137
138     if (!aCylinderAlgo.check()) {
139       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
140     }
141
142     aCylinderAlgo.build();
143
144     if(!aCylinderAlgo.isDone()) {
145       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
146     }
147     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
148       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
149     }
150     return aCylinderAlgo.shape();
151   }
152
153   //===============================================================================================
154   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
155     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
156   {
157     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
158       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
159     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
160     std::shared_ptr<GeomAPI_Ax2> anAxis;
161     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
162                                                           aEdge->line()->direction()));
163
164     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
165
166     if (!aCylinderAlgo.check()) {
167       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
168     }
169
170     aCylinderAlgo.build();
171
172     if(!aCylinderAlgo.isDone()) {
173       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
174     }
175     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
176       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
177     }
178     return aCylinderAlgo.shape();
179   }
180
181   //===============================================================================================
182   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
183     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
184   {
185     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
186       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
187     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
188     std::shared_ptr<GeomAPI_Ax2> anAxis;
189     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
190                                                           aEdge->line()->direction()));
191
192     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
193
194     if (!aCylinderAlgo.check()) {
195       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
196     }
197
198     aCylinderAlgo.build();
199
200     if(!aCylinderAlgo.isDone()) {
201       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
202     }
203     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
204       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
205     }
206     return aCylinderAlgo.shape();
207   }
208
209   //===============================================================================================
210   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
211     std::shared_ptr<GeomAPI_Shape> theSourceShape,
212     std::shared_ptr<GeomAPI_Ax1>   theAxis,
213     const double theDistance) throw (GeomAlgoAPI_Exception)
214   {
215     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
216
217     if (!aTranslationAlgo.check()) {
218       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
219     }
220
221     aTranslationAlgo.build();
222
223     if(!aTranslationAlgo.isDone()) {
224       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
225     }
226
227     return aTranslationAlgo.shape();
228   }
229
230   //===============================================================================================
231   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
232     std::shared_ptr<GeomAPI_Shape> theSourceShape,
233     const double theDx,
234     const double theDy,
235     const double theDz) throw (GeomAlgoAPI_Exception)
236   {
237     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
238
239     if (!aTranslationAlgo.check()) {
240       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
241     }
242
243     aTranslationAlgo.build();
244
245     if(!aTranslationAlgo.isDone()) {
246       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
247     }
248
249     return aTranslationAlgo.shape();
250   }
251
252   //===============================================================================================
253   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
254     std::shared_ptr<GeomAPI_Shape> theSourceShape,
255     std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
256     std::shared_ptr<GeomAPI_Pnt>   theEndPoint) throw (GeomAlgoAPI_Exception)
257   {
258     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
259
260     if (!aTranslationAlgo.check()) {
261       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
262     }
263
264     aTranslationAlgo.build();
265
266     if(!aTranslationAlgo.isDone()) {
267       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
268     }
269
270     return aTranslationAlgo.shape();
271   }
272
273   //===============================================================================================
274   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
275     std::shared_ptr<GeomAPI_Shape> theSourceShape,
276     std::shared_ptr<GeomAPI_Ax1> theAxis,
277     const double theAngle) throw (GeomAlgoAPI_Exception)
278   {
279     GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theAxis, theAngle);
280
281     if (!aRotationAlgo.check()) {
282       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
283     }
284
285     aRotationAlgo.build();
286
287     if(!aRotationAlgo.isDone()) {
288       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
289     }
290
291     return aRotationAlgo.shape();
292   }
293
294   //===============================================================================================
295   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeRotation(
296     std::shared_ptr<GeomAPI_Shape> theSourceShape,
297     std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
298     std::shared_ptr<GeomAPI_Pnt> theStartPoint,
299     std::shared_ptr<GeomAPI_Pnt> theEndPoint) throw (GeomAlgoAPI_Exception)
300   {
301     GeomAlgoAPI_Rotation aRotationAlgo(theSourceShape, theCenterPoint, theStartPoint, theEndPoint);
302
303     if (!aRotationAlgo.check()) {
304       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
305     }
306
307     aRotationAlgo.build();
308
309     if(!aRotationAlgo.isDone()) {
310       throw GeomAlgoAPI_Exception(aRotationAlgo.getError());
311     }
312
313     return aRotationAlgo.shape();
314   }
315
316   //===============================================================================================
317   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
318     std::shared_ptr<GeomAPI_Shape> theSourceShape,
319     std::shared_ptr<GeomAPI_Pnt>   thePoint) throw (GeomAlgoAPI_Exception)
320   {
321     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
322
323     if (!aSymmetryAlgo.check()) {
324       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
325     }
326
327     aSymmetryAlgo.build();
328
329     if(!aSymmetryAlgo.isDone()) {
330       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
331     }
332
333     return aSymmetryAlgo.shape();
334   }
335
336   //===============================================================================================
337   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
338     std::shared_ptr<GeomAPI_Shape> theSourceShape,
339     std::shared_ptr<GeomAPI_Ax1>   theAxis) throw (GeomAlgoAPI_Exception)
340   {
341     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
342
343     if (!aSymmetryAlgo.check()) {
344       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
345     }
346
347     aSymmetryAlgo.build();
348
349     if(!aSymmetryAlgo.isDone()) {
350       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
351     }
352
353     return aSymmetryAlgo.shape();
354   }
355
356   //===============================================================================================
357   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
358     std::shared_ptr<GeomAPI_Shape> theSourceShape,
359     std::shared_ptr<GeomAPI_Ax2>   thePlane) throw (GeomAlgoAPI_Exception)
360   {
361     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
362
363     if (!aSymmetryAlgo.check()) {
364       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
365     }
366
367     aSymmetryAlgo.build();
368
369     if(!aSymmetryAlgo.isDone()) {
370       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
371     }
372
373     return aSymmetryAlgo.shape();
374   }
375
376   //===============================================================================================
377   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
378     std::shared_ptr<GeomAPI_Shape> theSourceShape,
379     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
380     const double                   theScaleFactor) throw (GeomAlgoAPI_Exception)
381   {
382     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
383
384     if (!aScaleAlgo.check()) {
385       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
386     }
387
388     aScaleAlgo.build();
389
390     if(!aScaleAlgo.isDone()) {
391       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
392     }
393
394     return aScaleAlgo.shape();
395   }
396
397   //===============================================================================================
398   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
399     std::shared_ptr<GeomAPI_Shape> theSourceShape,
400     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
401     const double                   theScaleFactorX,
402     const double                   theScaleFactorY,
403     const double                   theScaleFactorZ) throw (GeomAlgoAPI_Exception)
404   {
405     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
406                                  theScaleFactorX, theScaleFactorY, theScaleFactorZ);
407
408     if (!aScaleAlgo.check()) {
409       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
410     }
411
412     aScaleAlgo.build();
413
414     if(!aScaleAlgo.isDone()) {
415       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
416     }
417
418     return aScaleAlgo.shape();
419   }
420
421   //===============================================================================================
422   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
423     std::shared_ptr<GeomAPI_Shape> theSourceShape,
424     std::shared_ptr<GeomAPI_Ax1> theAxis,
425     const double theStep,
426     const int theNumber) throw (GeomAlgoAPI_Exception)
427   {
428     if (!theAxis) {
429       std::string aError = "Multitranslation builder ";
430       aError+=":: the first axis is not valid";
431       throw GeomAlgoAPI_Exception(aError);
432     }
433
434     if (theNumber <=0) {
435       std::string aError = "Multitranslation builder ";
436       aError+=":: the number of copies for the first direction is null or negative.";
437       throw GeomAlgoAPI_Exception(aError);
438     }
439
440     ListOfShape aListOfShape;
441     for (int i=0; i<theNumber; i++) {
442       aListOfShape.
443         push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
444     }
445     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
446   }
447
448   //===============================================================================================
449   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
450     std::shared_ptr<GeomAPI_Shape> theSourceShape,
451     std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
452     const double theFirstStep,
453     const int theFirstNumber,
454     std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
455     const double theSecondStep,
456     const int theSecondNumber) throw (GeomAlgoAPI_Exception)
457   {
458     if (!theFirstAxis) {
459       std::string aError = "Multitranslation builder ";
460       aError+=":: the first axis is not valid";
461       throw GeomAlgoAPI_Exception(aError);
462     }
463
464     if (!theSecondAxis) {
465       std::string aError = "Multitranslation builder ";
466       aError+=":: the second axis is not valid";
467       throw GeomAlgoAPI_Exception(aError);
468     }
469
470     if (theFirstNumber <=0) {
471       std::string aError = "Multitranslation builder ";
472       aError+=":: the number of copies for the first direction is null or negative.";
473       throw GeomAlgoAPI_Exception(aError);
474     }
475
476     if (theSecondNumber <=0) {
477       std::string aError = "Multitranslation builder ";
478       aError+=":: the number of copies for the second direction is null or negative.";
479       throw GeomAlgoAPI_Exception(aError);
480     }
481
482     // Coord theFirstAxis
483     double x1 = theFirstAxis->dir()->x();
484     double y1 = theFirstAxis->dir()->y();
485     double z1 = theFirstAxis->dir()->z();
486     double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
487
488     // Coord theSecondAxis
489     double x2 = theSecondAxis->dir()->x();
490     double y2 = theSecondAxis->dir()->y();
491     double z2 = theSecondAxis->dir()->z();
492     double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
493
494     ListOfShape aListOfShape;
495     for (int j=0; j<theSecondStep; j++) {
496       for (int i=0; i<theFirstNumber; i++) {
497         double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
498         double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
499         double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
500         aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
501       }
502     }
503     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
504   }
505
506   //===============================================================================================
507   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
508     const double theRMin1, const double theRMax1,
509     const double theRMin2, const double theRMax2,
510     const double theZ,
511     const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
512   {
513     GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
514                                              theZ, theStartPhi, theDeltaPhi);
515
516     if (!aConeSegmentAlgo.check()) {
517       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
518     }
519
520     aConeSegmentAlgo.build();
521
522     if(!aConeSegmentAlgo.isDone()) {
523       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
524     }
525     if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
526       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
527     }
528     return aConeSegmentAlgo.shape();
529   }
530 }