Salome HOME
e52d735d00b52579059ce21d95889aafafddc28c
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ITestOperations.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <GEOMImpl_ITestOperations.hxx>
24
25 #include <BRepBndLib.hxx>
26 #include <BRepBuilderAPI_Copy.hxx>
27 #include <BRepMesh_IncrementalMesh.hxx>
28 #include <BRepTools.hxx>
29 #include <Bnd_Box.hxx>
30 #include <utilities.h>
31
32 #ifndef MAX2
33 #define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
34 #endif
35 #ifndef MAX3
36 #define MAX3(X, Y, Z) (MAX2(MAX2(X, Y), Z))
37 #endif
38
39 //=============================================================================
40 /*!
41  *   constructor:
42  */
43 //=============================================================================
44 GEOMImpl_ITestOperations::GEOMImpl_ITestOperations(GEOM_Engine* theEngine)
45 : GEOM_IOperations(theEngine)
46 {
47   MESSAGE("GEOMImpl_ITestOperations::GEOMImpl_ITestOperations");
48 }
49
50 //=============================================================================
51 /*!
52  *  destructor
53  */
54 //=============================================================================
55 GEOMImpl_ITestOperations::~GEOMImpl_ITestOperations()
56 {
57   MESSAGE("GEOMImpl_ITestOperations::~GEOMImpl_ITestOperations");
58 }
59
60
61 //=============================================================================
62 /*!
63  *  \brief Build a mesh on (a copy of ) the given shape.
64  *
65  *  This test function is aimed for checking performance of OCCT tesselation
66  *  algorithm on particlar geometrical shapes.
67  *
68  *  \param theShape is a source object
69  *  \param theLinearDeflection is a value of deflection coefficient
70  *  \param theIsRelative says if given value of deflection is relative to shape's bounding box
71  *  \param theAngularDeflection is a angular deflection for edges in radians
72  *  \return \c true in case of success; otherwise \c false.
73  */
74 //=============================================================================
75 bool GEOMImpl_ITestOperations::Tesselate(Handle(GEOM_Object) theShape,
76                                          double theLinearDeflection,
77                                          bool theIsRelative,
78                                          double theAngularDeflection)
79 {
80   // ATTENTION!
81   // We don't need results of this method to be present in the data tree;
82   // so we don't need a driver for it.
83
84   // reset error code
85   SetErrorCode(KO);
86   // create a copy of the source shape
87   TopoDS_Shape aShape = BRepBuilderAPI_Copy(theShape->GetValue()).Shape();
88   // use default deflection if necessary
89   if (theLinearDeflection <= 0)
90     theLinearDeflection = 0.001;
91   // compute absolute deflection if necessary: 0.001
92   if (theIsRelative) {
93     Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
94     Bnd_Box bndBox;
95     BRepBndLib::Add(aShape, bndBox);
96     bndBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
97     theLinearDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * theLinearDeflection * 4;
98   }
99   // use default deviation angle if necessary: 20 degrees
100   if (theAngularDeflection <= 0)
101     theAngularDeflection = 20. * M_PI / 180.;
102   // compute triangulation
103   BRepTools::Clean(aShape);
104   BRepMesh_IncrementalMesh aMesh(aShape, theLinearDeflection, Standard_False, theAngularDeflection);
105   // set OK status and return
106   SetErrorCode(OK);
107   return true;
108 }