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