Salome HOME
Fix line containing trailing whitespaces to meet coding rules
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeAPI.cpp
1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeAPI.cpp
4 // Created:     17 Mar 2016
5 // Author:      Clarisse Genrault (CEA)
6
7 #include "GeomAlgoAPI_ShapeAPI.h"
8
9 #include <GeomAlgoAPI_Box.h>
10 #include <GeomAlgoAPI_Cylinder.h>
11 #include <GeomAlgoAPI_CompoundBuilder.h>
12 #include <GeomAlgoAPI_ConeSegment.h>
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomAlgoAPI_Scale.h>
15 #include <GeomAlgoAPI_Symmetry.h>
16 #include <GeomAlgoAPI_Translation.h>
17
18 #include <GeomAPI_Lin.h>
19
20 #include <math.h>
21
22 namespace GeomAlgoAPI_ShapeAPI
23 {
24   //===============================================================================================
25   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
26     const double theDx, const double theDy,
27     const double theDz) throw (GeomAlgoAPI_Exception)
28   {
29     GeomAlgoAPI_Box aBoxAlgo(theDx,theDy,theDz);
30
31     if (!aBoxAlgo.check()) {
32       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
33     }
34
35     aBoxAlgo.build();
36
37     if(!aBoxAlgo.isDone()) {
38       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
39     }
40     if (!aBoxAlgo.checkValid("Box builder with dimensions")) {
41       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
42     }
43     return aBoxAlgo.shape();
44   }
45
46   //===============================================================================================
47   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeBox(
48     std::shared_ptr<GeomAPI_Pnt> theFirstPoint,
49     std::shared_ptr<GeomAPI_Pnt> theSecondPoint) throw (GeomAlgoAPI_Exception)
50   {
51     GeomAlgoAPI_Box aBoxAlgo(theFirstPoint, theSecondPoint);
52
53     if (!aBoxAlgo.check()) {
54       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
55     }
56
57     aBoxAlgo.build();
58
59     if(!aBoxAlgo.isDone()) {
60       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
61     }
62     if (!aBoxAlgo.checkValid("Box builder with two points")) {
63       throw GeomAlgoAPI_Exception(aBoxAlgo.getError());
64     }
65     return aBoxAlgo.shape();
66   }
67
68   //===============================================================================================
69   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
70     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
71     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
72   {
73     // Check if the base point is OK
74     if (!theBasePoint) {
75       throw GeomAlgoAPI_Exception("Cylinder builder :: the base point is not valid.");
76     }
77     // Check if the edge is OK
78     if (!theEdge) {
79       throw GeomAlgoAPI_Exception("Cylinder builder :: the axis is not valid.");
80     }
81
82     std::shared_ptr<GeomAPI_Ax2> anAxis;
83     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
84                                                           theEdge->line()->direction()));
85
86     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
87
88     if (!aCylinderAlgo.check()) {
89       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
90     }
91
92     aCylinderAlgo.build();
93
94     if(!aCylinderAlgo.isDone()) {
95       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
96     }
97     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
98       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
99     }
100     return aCylinderAlgo.shape();
101   }
102
103   //===============================================================================================
104   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
105     std::shared_ptr<GeomAPI_Pnt> theBasePoint, std::shared_ptr<GeomAPI_Edge> theEdge,
106     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
107   {
108     std::shared_ptr<GeomAPI_Ax2> anAxis;
109     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(theBasePoint,
110                                                           theEdge->line()->direction()));
111
112     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
113
114     if (!aCylinderAlgo.check()) {
115       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
116     }
117
118     aCylinderAlgo.build();
119
120     if(!aCylinderAlgo.isDone()) {
121       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
122     }
123     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
124       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
125     }
126     return aCylinderAlgo.shape();
127   }
128
129   //===============================================================================================
130   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
131     double theRadius, double theHeight) throw (GeomAlgoAPI_Exception)
132   {
133     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
134       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
135     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
136     std::shared_ptr<GeomAPI_Ax2> anAxis;
137     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
138                                                           aEdge->line()->direction()));
139
140     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight);
141
142     if (!aCylinderAlgo.check()) {
143       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
144     }
145
146     aCylinderAlgo.build();
147
148     if(!aCylinderAlgo.isDone()) {
149       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
150     }
151     if (!aCylinderAlgo.checkValid("Cylinder builder")) {
152       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
153     }
154     return aCylinderAlgo.shape();
155   }
156
157   //===============================================================================================
158   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeCylinder(
159     double theRadius, double theHeight, double theAngle) throw (GeomAlgoAPI_Exception)
160   {
161     std::shared_ptr<GeomAPI_Pnt> aBasePoint =
162       std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
163     std::shared_ptr<GeomAPI_Edge> aEdge = GeomAlgoAPI_EdgeBuilder::line(0., 0., 1.);
164     std::shared_ptr<GeomAPI_Ax2> anAxis;
165     anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
166                                                           aEdge->line()->direction()));
167
168     GeomAlgoAPI_Cylinder aCylinderAlgo(anAxis, theRadius, theHeight, theAngle);
169
170     if (!aCylinderAlgo.check()) {
171       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
172     }
173
174     aCylinderAlgo.build();
175
176     if(!aCylinderAlgo.isDone()) {
177       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
178     }
179     if (!aCylinderAlgo.checkValid("Cylinder portion builder")) {
180       throw GeomAlgoAPI_Exception(aCylinderAlgo.getError());
181     }
182     return aCylinderAlgo.shape();
183   }
184
185   //===============================================================================================
186   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
187     std::shared_ptr<GeomAPI_Shape> theSourceShape,
188     std::shared_ptr<GeomAPI_Ax1>   theAxis,
189     const double theDistance) throw (GeomAlgoAPI_Exception)
190   {
191     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance);
192
193     if (!aTranslationAlgo.check()) {
194       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
195     }
196
197     aTranslationAlgo.build();
198
199     if(!aTranslationAlgo.isDone()) {
200       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
201     }
202     if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) {
203       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
204     }
205     return aTranslationAlgo.shape();
206   }
207
208   //===============================================================================================
209   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
210     std::shared_ptr<GeomAPI_Shape> theSourceShape,
211     const double theDx,
212     const double theDy,
213     const double theDz) throw (GeomAlgoAPI_Exception)
214   {
215     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz);
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     if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) {
227       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
228     }
229     return aTranslationAlgo.shape();
230   }
231
232   //===============================================================================================
233   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeTranslation(
234     std::shared_ptr<GeomAPI_Shape> theSourceShape,
235     std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
236     std::shared_ptr<GeomAPI_Pnt>   theEndPoint) throw (GeomAlgoAPI_Exception)
237   {
238     GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theStartPoint, theEndPoint);
239
240     if (!aTranslationAlgo.check()) {
241       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
242     }
243
244     aTranslationAlgo.build();
245
246     if(!aTranslationAlgo.isDone()) {
247       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
248     }
249     if (!aTranslationAlgo.checkValid("Translation builder with two points")) {
250       throw GeomAlgoAPI_Exception(aTranslationAlgo.getError());
251     }
252     return aTranslationAlgo.shape();
253   }
254
255   //===============================================================================================
256   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
257     std::shared_ptr<GeomAPI_Shape> theSourceShape,
258     std::shared_ptr<GeomAPI_Pnt>   thePoint) throw (GeomAlgoAPI_Exception)
259   {
260     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePoint);
261
262     if (!aSymmetryAlgo.check()) {
263       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
264     }
265
266     aSymmetryAlgo.build();
267
268     if(!aSymmetryAlgo.isDone()) {
269       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
270     }
271     if (!aSymmetryAlgo.checkValid("Symmetry builder by a point")) {
272       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
273     }
274     return aSymmetryAlgo.shape();
275   }
276
277   //===============================================================================================
278   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
279     std::shared_ptr<GeomAPI_Shape> theSourceShape,
280     std::shared_ptr<GeomAPI_Ax1>   theAxis) throw (GeomAlgoAPI_Exception)
281   {
282     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, theAxis);
283
284     if (!aSymmetryAlgo.check()) {
285       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
286     }
287
288     aSymmetryAlgo.build();
289
290     if(!aSymmetryAlgo.isDone()) {
291       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
292     }
293     if (!aSymmetryAlgo.checkValid("Symmetry builder by an axis")) {
294       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
295     }
296     return aSymmetryAlgo.shape();
297   }
298
299   //===============================================================================================
300   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeSymmetry(
301     std::shared_ptr<GeomAPI_Shape> theSourceShape,
302     std::shared_ptr<GeomAPI_Ax2>   thePlane) throw (GeomAlgoAPI_Exception)
303   {
304     GeomAlgoAPI_Symmetry aSymmetryAlgo(theSourceShape, thePlane);
305
306     if (!aSymmetryAlgo.check()) {
307       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
308     }
309
310     aSymmetryAlgo.build();
311
312     if(!aSymmetryAlgo.isDone()) {
313       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
314     }
315     if (!aSymmetryAlgo.checkValid("Symmetry builder by a plane")) {
316       throw GeomAlgoAPI_Exception(aSymmetryAlgo.getError());
317     }
318     return aSymmetryAlgo.shape();
319   }
320
321   //===============================================================================================
322   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
323     std::shared_ptr<GeomAPI_Shape> theSourceShape,
324     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
325     const double                   theScaleFactor) throw (GeomAlgoAPI_Exception)
326   {
327     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint, theScaleFactor);
328
329     if (!aScaleAlgo.check()) {
330       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
331     }
332
333     aScaleAlgo.build();
334
335     if(!aScaleAlgo.isDone()) {
336       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
337     }
338     if (!aScaleAlgo.checkValid("Scale builder by a scale factor")) {
339       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
340     }
341     return aScaleAlgo.shape();
342   }
343
344   //===============================================================================================
345   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeScale(
346     std::shared_ptr<GeomAPI_Shape> theSourceShape,
347     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
348     const double                   theScaleFactorX,
349     const double                   theScaleFactorY,
350     const double                   theScaleFactorZ) throw (GeomAlgoAPI_Exception)
351   {
352     GeomAlgoAPI_Scale aScaleAlgo(theSourceShape, theCenterPoint,
353                                  theScaleFactorX, theScaleFactorY, theScaleFactorZ);
354
355     if (!aScaleAlgo.check()) {
356       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
357     }
358
359     aScaleAlgo.build();
360
361     if(!aScaleAlgo.isDone()) {
362       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
363     }
364     if (!aScaleAlgo.checkValid("Scale builder by dimensions")) {
365       throw GeomAlgoAPI_Exception(aScaleAlgo.getError());
366     }
367     return aScaleAlgo.shape();
368   }
369
370   //===============================================================================================
371   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
372     std::shared_ptr<GeomAPI_Shape> theSourceShape,
373     std::shared_ptr<GeomAPI_Ax1> theAxis,
374     const double theStep,
375     const int theNumber) throw (GeomAlgoAPI_Exception)
376   {
377     ListOfShape aListOfShape;
378     for (int i=0; i<theNumber; i++) {
379       aListOfShape.
380         push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, theAxis, i*theStep));
381     }
382     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
383   }
384
385   //===============================================================================================
386   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeMultiTranslation(
387     std::shared_ptr<GeomAPI_Shape> theSourceShape,
388     std::shared_ptr<GeomAPI_Ax1> theFirstAxis,
389     const double theFirstStep,
390     const int theFirstNumber,
391     std::shared_ptr<GeomAPI_Ax1> theSecondAxis,
392     const double theSecondStep,
393     const int theSecondNumber) throw (GeomAlgoAPI_Exception)
394   {
395     // Coord theFirstAxis
396     double x1 = theFirstAxis->dir()->x();
397     double y1 = theFirstAxis->dir()->y();
398     double z1 = theFirstAxis->dir()->z();
399     double norm1 = sqrt(x1*x1 + y1*y1 + z1*z1);
400
401     // Coord theSecondAxis
402     double x2 = theSecondAxis->dir()->x();
403     double y2 = theSecondAxis->dir()->y();
404     double z2 = theSecondAxis->dir()->z();
405     double norm2 = sqrt(x2*x2 + y2*y2 + z2*z2);
406
407     ListOfShape aListOfShape;
408     for (int j=0; j<theSecondStep; j++) {
409       for (int i=0; i<theFirstNumber; i++) {
410         double dx = i*theFirstStep*x1/norm1+j*theSecondStep*x2/norm2;
411         double dy = i*theFirstStep*y1/norm1+j*theSecondStep*y2/norm2;
412         double dz = i*theFirstStep*z1/norm1+j*theSecondStep*z2/norm2;
413         aListOfShape.push_back(GeomAlgoAPI_ShapeAPI::makeTranslation(theSourceShape, dx, dy, dz));
414       }
415     }
416     return GeomAlgoAPI_CompoundBuilder::compound(aListOfShape);
417   }
418
419   //===============================================================================================
420   std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeAPI::makeConeSegment(
421     const double theRMin1, const double theRMax1,
422     const double theRMin2, const double theRMax2,
423     const double theZ,
424     const double theStartPhi, const double theDeltaPhi) throw (GeomAlgoAPI_Exception)
425   {
426     GeomAlgoAPI_ConeSegment aConeSegmentAlgo(theRMin1, theRMax1, theRMin2, theRMax2,
427                                              theZ, theStartPhi, theDeltaPhi);
428
429     if (!aConeSegmentAlgo.check()) {
430       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
431     }
432
433     aConeSegmentAlgo.build();
434
435     if(!aConeSegmentAlgo.isDone()) {
436       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
437     }
438     if (!aConeSegmentAlgo.checkValid("Cone Segment builder")) {
439       throw GeomAlgoAPI_Exception(aConeSegmentAlgo.getError());
440     }
441     return aConeSegmentAlgo.shape();
442   }
443 }