Salome HOME
2fcac5a7f9122551b4efb276f0f3c77fd0daf3a0
[modules/smesh.git] / src / SMESH_I / SMESH_Measurements_i.cxx
1 // Copyright (C) 2007-2016  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 //  File   : SMESH_Measurements_i.cxx
23 //  Author : Pavel TELKOV, Open CASCADE S.A.S. (pavel.telkov@opencascade.com)
24
25 #ifdef WIN32
26 #define NOMINMAX
27 #endif
28
29 #include "SMESH_Measurements_i.hxx"
30
31 #include "SMDS_ElemIterator.hxx"
32 #include "SMDS_Mesh.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Filter_i.hxx"
37 #include "SMESH_Gen_i.hxx"
38 #include "SMESH_MeshAlgos.hxx"
39 #include "SMESH_PythonDump.hxx"
40
41
42 using namespace SMESH;
43
44 /**
45  * this local function to avoid uninitialized fields
46  */
47 static void initMeasure( SMESH::Measure& theMeasure)
48 {
49
50   theMeasure.minX = theMeasure.minY = theMeasure.minZ = 0.;
51   theMeasure.maxX = theMeasure.maxY = theMeasure.maxZ = 0.;
52   theMeasure.node1 = theMeasure.node2 = -1;
53   theMeasure.elem1 = theMeasure.elem2 = -1;
54   theMeasure.value = 0.;
55 }
56
57 //=============================================================================
58 /*!
59  *  SMESH_Gen_i::CreateMeasurements
60  *
61  *  Create measurement instance
62  */
63 //=============================================================================
64
65 SMESH::Measurements_ptr SMESH_Gen_i::CreateMeasurements()
66 {
67   SMESH::Measurements_i* aMeasure = new SMESH::Measurements_i();
68   SMESH::Measurements_var anObj = aMeasure->_this();
69   return anObj._retn();
70 }
71
72   
73 /*
74   Class       : Measurements
75   Description : make measure of mesh qunatities
76 */
77
78 //=======================================================================
79 // name    : Measurements_i
80 // Purpose : Constructor
81 //=======================================================================
82 Measurements_i::Measurements_i()
83 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
84 {
85   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
86   //PortableServer::ObjectId_var anObjectId =
87   //  SMESH_Gen_i::GetPOA()->activate_object( this );
88 }
89
90 //=======================================================================
91 // name    : ~Measurements_i
92 // Purpose : Destructor
93 //=======================================================================
94 Measurements_i::~Measurements_i()
95 {
96   //TPythonDump()<<this<<".UnRegister()";
97 }
98
99 static bool getNodeNodeDistance (SMESH::Measure& theMeasure,
100                                  const SMDS_MeshNode* theNode1,
101                                  const SMDS_MeshNode* theNode2 = 0)
102 {
103   double dist = 0., dd = 0.;
104
105   if (!theNode1)
106     return false;
107
108   dd = theNode1->X(); if (theNode2) dd -= theNode2->X(); theMeasure.minX = dd; dd *= dd; dist += dd;
109   dd = theNode1->Y(); if (theNode2) dd -= theNode2->Y(); theMeasure.minY = dd; dd *= dd; dist += dd;
110   dd = theNode1->Z(); if (theNode2) dd -= theNode2->Z(); theMeasure.minZ = dd; dd *= dd; dist += dd;
111
112   if (dist < 0)
113     return false;
114   
115   theMeasure.value = sqrt(dist);
116   theMeasure.node1 = theNode1->GetID();
117   theMeasure.node2 = theNode2 ? theNode2->GetID() : 0;
118
119   return true;
120 }
121
122 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
123 {
124   if (!CORBA::is_nil( theSource ))
125   {
126     SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
127     if (anImplPtr)
128       return anImplPtr->GetImpl().GetMeshDS();
129   }
130   return 0;
131 }
132
133 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
134 {
135   return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
136 }
137
138 static double getNumericalValue(SMESH::SMESH_IDSource_ptr            theSource,
139                                 SMESH::Controls::NumericalFunctorPtr theFunctor)
140 {
141   double value = 0;
142
143   if ( !CORBA::is_nil( theSource ) ) {
144     const SMESHDS_Mesh* aMesh = getMesh( theSource );
145     if ( aMesh ) {
146       theFunctor->SetMesh( aMesh );
147       
148       SMESH::long_array_var anElementsId = theSource->GetIDs();
149       for ( CORBA::ULong i = 0; i < anElementsId->length(); i++) {
150         value += theFunctor->GetValue( anElementsId[i] );
151       }
152     }
153   }
154   return value;
155 }
156
157 //=======================================================================
158 // name    : MinDistance
159 // Purpose : minimal distance between two given entities
160 //=======================================================================
161 SMESH::Measure Measurements_i::MinDistance
162  (SMESH::SMESH_IDSource_ptr theSource1,
163   SMESH::SMESH_IDSource_ptr theSource2)
164 {
165   SMESH::Measure aMeasure;
166   initMeasure(aMeasure);
167
168   if (CORBA::is_nil( theSource1 ))
169     return aMeasure;
170   
171   // if second source is null, min distance from theSource1 to the origin is calculated
172   bool isOrigin =  CORBA::is_nil( theSource2 );
173
174   // calculate minimal distance between two mesh entities
175   SMESH::array_of_ElementType_var types1 = theSource1->GetTypes();
176   SMESH::array_of_ElementType_var types2;
177   if ( !isOrigin ) types2 = theSource2->GetTypes();
178
179   // here we assume that type of all IDs defined by first type in array
180   bool isNode1 = isNodeType(types1);
181   bool isNode2 = isOrigin || isNodeType(types2);
182
183   SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
184   SMESH::long_array_var aElementsId2;
185   if ( !isOrigin ) aElementsId2 = theSource2->GetIDs();
186
187   // compute distance between two entities
188   /* NOTE: currently only node-to-node case is implemented
189    * all other cases will be implemented later
190    * below IF should be replaced by complete switch
191    * on mesh entities types
192    */
193   if (isNode1 && isNode2)
194   {
195     // node - node
196     const SMESHDS_Mesh* aMesh1 = getMesh( theSource1 );
197     const SMESHDS_Mesh* aMesh2 = isOrigin ? 0 : getMesh( theSource2 );
198     const SMDS_MeshNode* theNode1 = aMesh1 ? aMesh1->FindNode( aElementsId1[0] ) : 0;
199     const SMDS_MeshNode* theNode2 = aMesh2 ? aMesh2->FindNode( aElementsId2[0] ) : 0;
200     getNodeNodeDistance( aMeasure, theNode1, theNode2 );
201   }
202   else
203   {
204     // NOT_IMPLEMENTED
205   }
206
207   return aMeasure;
208 }
209
210 //=======================================================================
211 // name    : enlargeBoundingBox
212 // Purpose : 
213 //=======================================================================
214 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
215                                SMESH::Measure&      theMeasure)
216 {
217   if (!theNode)
218     return;
219   if ( theMeasure.node1 == -1 ) {
220     // we use this attribute as a flag that it is the first node added to the bnd box 
221     theMeasure.minX = theMeasure.maxX = theNode->X();
222     theMeasure.minY = theMeasure.maxY = theNode->Y();
223     theMeasure.minZ = theMeasure.maxZ = theNode->Z();
224     theMeasure.node1 = theNode->GetID();
225   }
226   else {
227     theMeasure.minX = std::min( theMeasure.minX, theNode->X() );
228     theMeasure.maxX = std::max( theMeasure.maxX, theNode->X() );
229     theMeasure.minY = std::min( theMeasure.minY, theNode->Y() );
230     theMeasure.maxY = std::max( theMeasure.maxY, theNode->Y() );
231     theMeasure.minZ = std::min( theMeasure.minZ, theNode->Z() );
232     theMeasure.maxZ = std::max( theMeasure.maxZ, theNode->Z() );
233   }
234 }
235
236 //=======================================================================
237 // name    : enlargeBoundingBox
238 // Purpose : 
239 //=======================================================================
240 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
241                                SMESH::Measure&                 theMeasure)
242 {
243   if ( CORBA::is_nil( theObject ) )
244     return;
245   const SMESHDS_Mesh* aMesh = getMesh( theObject );
246   if ( !aMesh )
247     return;
248   SMESH::array_of_ElementType_var types = theObject->GetTypes();
249   SMESH::long_array_var     aElementsId = theObject->GetIDs();
250   // here we assume that type of all IDs defined by first type in array
251   const bool isNode = isNodeType( types );
252   for(int i = 0, n = aElementsId->length(); i < n; i++)
253   {
254     if (isNode)
255       enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
256     else
257     {
258       if ( const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] ))
259         for (SMDS_NodeIteratorPtr aNodeIter = elem->nodeIterator(); aNodeIter->more(); )
260           enlargeBoundingBox( aNodeIter->next(), theMeasure);
261     }
262   }
263 }
264
265 //=======================================================================
266 // name    : BoundingBox
267 // Purpose : compute common bounding box of entities
268 //=======================================================================
269 SMESH::Measure Measurements_i::BoundingBox (const SMESH::ListOfIDSources& theSources)
270 {
271   SMESH::Measure aMeasure;
272   initMeasure(aMeasure);
273
274   // calculate bounding box on sources
275   for ( int i = 0, n = theSources.length(); i < n ; ++i )
276     enlargeBoundingBox( theSources[i], aMeasure );
277
278   return aMeasure;
279 }
280
281 //=======================================================================
282 // name    : Length
283 // Purpose : sum of length of 1D elements of the source
284 //=======================================================================
285 double Measurements_i::Length(SMESH::SMESH_IDSource_ptr theSource)
286 {
287   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Length()) );
288 }
289
290 //=======================================================================
291 // name    : Area
292 // Purpose : sum of area of 2D elements of the source
293 //=======================================================================
294 double Measurements_i::Area(SMESH::SMESH_IDSource_ptr theSource)
295 {
296   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Area()) );
297 }
298
299 //=======================================================================
300 // name    : Volume
301 // Purpose : sum of volume of 3D elements of the source
302 //=======================================================================
303 double Measurements_i::Volume(SMESH::SMESH_IDSource_ptr theSource)
304 {
305   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Volume()) );
306 }
307
308 //=======================================================================
309 //function : GravityCenter
310 //purpose  : return gravity center of the source: average coordinates of all nodes
311 //=======================================================================
312
313 SMESH::PointStruct Measurements_i::GravityCenter(SMESH::SMESH_IDSource_ptr theSource)
314 {
315   SMESH::PointStruct grCenter = { 0.,0.,0. };
316   const SMESHDS_Mesh* mesh = getMesh( theSource );
317   if ( !mesh )
318     return grCenter;
319
320   // unmark all nodes; visited nodes will be marked
321   SMESH_MeshAlgos::MarkElems( mesh->nodesIterator(), /*isMarked=*/false );
322
323   gp_XYZ sumCoord( 0,0,0 );
324   int nodeCount = 0;
325
326   SMDS_ElemIteratorPtr eIt = SMESH_Mesh_i::GetElements( theSource, SMESH::ALL );
327   while ( eIt->more() )
328   {
329     const SMDS_MeshElement*   elem = eIt->next();
330     for ( SMDS_NodeIteratorPtr nIt = elem->nodeIterator(); nIt->more(); )
331     {
332       const SMDS_MeshNode* n = nIt->next();
333       if ( !n->isMarked() )
334       {
335         sumCoord += SMESH_NodeXYZ( n );
336         ++nodeCount;
337         n->setIsMarked( true );
338       }
339     }
340   }
341   sumCoord /= nodeCount;
342
343   grCenter.x = sumCoord.X();
344   grCenter.y = sumCoord.Y();
345   grCenter.z = sumCoord.Z();
346
347   return grCenter;
348 }