Salome HOME
23627: [IMACS] ASERIS: project point to the mesh and create a slot
[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 #include <cmath>
42
43 using namespace SMESH;
44
45 /**
46  * this local function to avoid uninitialized fields
47  */
48 static void initMeasure( SMESH::Measure& theMeasure)
49 {
50
51   theMeasure.minX = theMeasure.minY = theMeasure.minZ = 0.;
52   theMeasure.maxX = theMeasure.maxY = theMeasure.maxZ = 0.;
53   theMeasure.node1 = theMeasure.node2 = -1;
54   theMeasure.elem1 = theMeasure.elem2 = -1;
55   theMeasure.value = 0.;
56 }
57
58 //=============================================================================
59 /*!
60  *  SMESH_Gen_i::CreateMeasurements
61  *
62  *  Create measurement instance
63  */
64 //=============================================================================
65
66 SMESH::Measurements_ptr SMESH_Gen_i::CreateMeasurements()
67 {
68   SMESH::Measurements_i* aMeasure = new SMESH::Measurements_i();
69   SMESH::Measurements_var anObj = aMeasure->_this();
70   return anObj._retn();
71 }
72
73   
74 /*
75   Class       : Measurements
76   Description : make measure of mesh qunatities
77 */
78
79 //=======================================================================
80 // name    : Measurements_i
81 // Purpose : Constructor
82 //=======================================================================
83 Measurements_i::Measurements_i()
84 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
85 {
86   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
87   //PortableServer::ObjectId_var anObjectId =
88   //  SMESH_Gen_i::GetPOA()->activate_object( this );
89 }
90
91 //=======================================================================
92 // name    : ~Measurements_i
93 // Purpose : Destructor
94 //=======================================================================
95 Measurements_i::~Measurements_i()
96 {
97   //TPythonDump()<<this<<".UnRegister()";
98 }
99
100 static bool getNodeNodeDistance (SMESH::Measure& theMeasure,
101                                  const SMDS_MeshNode* theNode1,
102                                  const SMDS_MeshNode* theNode2 = 0)
103 {
104   double dist = 0., dd = 0.;
105
106   if (!theNode1)
107     return false;
108
109   dd = theNode1->X(); if (theNode2) dd -= theNode2->X(); theMeasure.minX = dd; dd *= dd; dist += dd;
110   dd = theNode1->Y(); if (theNode2) dd -= theNode2->Y(); theMeasure.minY = dd; dd *= dd; dist += dd;
111   dd = theNode1->Z(); if (theNode2) dd -= theNode2->Z(); theMeasure.minZ = dd; dd *= dd; dist += dd;
112
113   if (dist < 0)
114     return false;
115   
116   theMeasure.value = sqrt(dist);
117   theMeasure.node1 = theNode1->GetID();
118   theMeasure.node2 = theNode2 ? theNode2->GetID() : 0;
119
120   return true;
121 }
122
123 static bool getNodeElemDistance (SMESH::Measure&        theMeasure,
124                                  const SMDS_MeshNode*   theNode,
125                                  SMESH_ElementSearcher* theElemSearcher)
126 {
127   if ( !theNode || !theElemSearcher )
128     return false;
129
130   const SMDS_MeshElement* closestElement = 0;
131   gp_Pnt        point = SMESH_NodeXYZ( theNode );
132   gp_Pnt closestPoint = theElemSearcher->Project( point, SMDSAbs_All, &closestElement );
133
134   if ( closestElement )
135   {
136     theMeasure.value = point.Distance( closestPoint );
137     theMeasure.node1 = theNode->GetID();
138     theMeasure.elem2 = closestElement->GetID();
139     theMeasure.maxX  = closestPoint.X();
140     theMeasure.maxY  = closestPoint.Y();
141     theMeasure.maxZ  = closestPoint.Z();
142     theMeasure.minX  = closestPoint.X() - point.X();
143     theMeasure.minY  = closestPoint.Y() - point.Y();
144     theMeasure.minZ  = closestPoint.Z() - point.Z();
145   }
146
147   return closestElement;
148 }
149
150 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
151 {
152   if (!CORBA::is_nil( theSource ))
153   {
154     SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
155     if (anImplPtr)
156       return anImplPtr->GetImpl().GetMeshDS();
157   }
158   return 0;
159 }
160
161 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
162 {
163   return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
164 }
165
166 static double getNumericalValue(SMESH::SMESH_IDSource_ptr            theSource,
167                                 SMESH::Controls::NumericalFunctorPtr theFunctor)
168 {
169   double value = 0;
170
171   if ( !CORBA::is_nil( theSource ) ) {
172     const SMESHDS_Mesh* aMesh = getMesh( theSource );
173     if ( aMesh ) {
174       theFunctor->SetMesh( aMesh );
175       
176       SMESH::long_array_var anElementsId = theSource->GetIDs();
177       for ( CORBA::ULong i = 0; i < anElementsId->length(); i++) {
178         value += theFunctor->GetValue( anElementsId[i] );
179       }
180     }
181   }
182   return value;
183 }
184
185 //=======================================================================
186 // name    : MinDistance
187 // Purpose : minimal distance between two given entities
188 //=======================================================================
189 SMESH::Measure Measurements_i::MinDistance
190  (SMESH::SMESH_IDSource_ptr theSource1,
191   SMESH::SMESH_IDSource_ptr theSource2)
192 {
193   SMESH::Measure aMeasure;
194   initMeasure(aMeasure);
195
196   if (CORBA::is_nil( theSource1 ))
197     return aMeasure;
198   
199   // if second source is null, min distance from theSource1 to the origin is calculated
200   bool isOrigin =  CORBA::is_nil( theSource2 );
201
202   // calculate minimal distance between two mesh entities
203   SMESH::array_of_ElementType_var types1 = theSource1->GetTypes();
204   SMESH::array_of_ElementType_var types2;
205   if ( !isOrigin ) types2 = theSource2->GetTypes();
206
207   // here we assume that type of all IDs defined by first type in array
208   bool isNode1 = isNodeType(types1);
209   bool isNode2 = isOrigin || isNodeType(types2);
210
211   SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
212   SMESH::long_array_var aElementsId2;
213
214   // compute distance between two entities
215   /* NOTE: currently only node-to-node case is implemented
216    * all other cases will be implemented later
217    * below IF should be replaced by complete switch
218    * on mesh entities types
219    */
220   if (isNode1 && isNode2)
221   {
222     // node - node
223     const SMESHDS_Mesh* aMesh1 = getMesh( theSource1 );
224     const SMESHDS_Mesh* aMesh2 = isOrigin ? 0 : getMesh( theSource2 );
225     if ( !isOrigin ) aElementsId2 = theSource2->GetIDs();
226     const SMDS_MeshNode* theNode1 = aMesh1 ? aMesh1->FindNode( aElementsId1[0] ) : 0;
227     const SMDS_MeshNode* theNode2 = aMesh2 ? aMesh2->FindNode( aElementsId2[0] ) : 0;
228     getNodeNodeDistance( aMeasure, theNode1, theNode2 );
229   }
230   if (isNode1 && !isNode2 && aElementsId1->length() == 1 )
231   {
232     // node - elements
233     SMESHDS_Mesh* aMesh1 = getMesh( theSource1 );
234     SMESHDS_Mesh* aMesh2 = getMesh( theSource2 );
235     if ( aMesh1 && aMesh2 )
236     {
237       const SMDS_MeshNode* aNode    = aMesh1->FindNode( aElementsId1[0] );
238       SMDS_ElemIteratorPtr anElemIt = SMESH_Mesh_i::GetElements( theSource2, SMESH::ALL );
239       std::unique_ptr< SMESH_ElementSearcher > aSearcher
240         ( SMESH_MeshAlgos::GetElementSearcher( *aMesh2, anElemIt ));
241       getNodeElemDistance( aMeasure, aNode, aSearcher.get() );
242     }
243   }
244   else
245   {
246     // NOT_IMPLEMENTED
247   }
248
249   return aMeasure;
250 }
251
252 //=======================================================================
253 // name    : enlargeBoundingBox
254 // Purpose : 
255 //=======================================================================
256 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
257                                SMESH::Measure&      theMeasure)
258 {
259   if (!theNode)
260     return;
261   if ( theMeasure.node1 == -1 ) {
262     // we use this attribute as a flag that it is the first node added to the bnd box 
263     theMeasure.minX = theMeasure.maxX = theNode->X();
264     theMeasure.minY = theMeasure.maxY = theNode->Y();
265     theMeasure.minZ = theMeasure.maxZ = theNode->Z();
266     theMeasure.node1 = theNode->GetID();
267   }
268   else {
269     theMeasure.minX = std::min( theMeasure.minX, theNode->X() );
270     theMeasure.maxX = std::max( theMeasure.maxX, theNode->X() );
271     theMeasure.minY = std::min( theMeasure.minY, theNode->Y() );
272     theMeasure.maxY = std::max( theMeasure.maxY, theNode->Y() );
273     theMeasure.minZ = std::min( theMeasure.minZ, theNode->Z() );
274     theMeasure.maxZ = std::max( theMeasure.maxZ, theNode->Z() );
275   }
276 }
277
278 //=======================================================================
279 // name    : enlargeBoundingBox
280 // Purpose : 
281 //=======================================================================
282 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
283                                SMESH::Measure&                 theMeasure)
284 {
285   if ( CORBA::is_nil( theObject ) )
286     return;
287   const SMESHDS_Mesh* aMesh = getMesh( theObject );
288   if ( !aMesh )
289     return;
290   SMESH::array_of_ElementType_var types = theObject->GetTypes();
291   SMESH::long_array_var     aElementsId = theObject->GetIDs();
292   // here we assume that type of all IDs defined by first type in array
293   const bool isNode = isNodeType( types );
294   for(int i = 0, n = aElementsId->length(); i < n; i++)
295   {
296     if (isNode)
297       enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
298     else
299     {
300       if ( const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] ))
301         for (SMDS_NodeIteratorPtr aNodeIter = elem->nodeIterator(); aNodeIter->more(); )
302           enlargeBoundingBox( aNodeIter->next(), theMeasure);
303     }
304   }
305 }
306
307 //=======================================================================
308 // name    : BoundingBox
309 // Purpose : compute common bounding box of entities
310 //=======================================================================
311 SMESH::Measure Measurements_i::BoundingBox (const SMESH::ListOfIDSources& theSources)
312 {
313   SMESH::Measure aMeasure;
314   initMeasure(aMeasure);
315
316   // calculate bounding box on sources
317   for ( int i = 0, n = theSources.length(); i < n ; ++i )
318     enlargeBoundingBox( theSources[i], aMeasure );
319
320   return aMeasure;
321 }
322
323 //=======================================================================
324 // name    : Length
325 // Purpose : sum of length of 1D elements of the source
326 //=======================================================================
327 double Measurements_i::Length(SMESH::SMESH_IDSource_ptr theSource)
328 {
329   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Length()) );
330 }
331
332 //=======================================================================
333 // name    : Area
334 // Purpose : sum of area of 2D elements of the source
335 //=======================================================================
336 double Measurements_i::Area(SMESH::SMESH_IDSource_ptr theSource)
337 {
338   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Area()) );
339 }
340
341 //=======================================================================
342 // name    : Volume
343 // Purpose : sum of volume of 3D elements of the source
344 //=======================================================================
345 double Measurements_i::Volume(SMESH::SMESH_IDSource_ptr theSource)
346 {
347   return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Volume()) );
348 }
349
350 //=======================================================================
351 //function : GravityCenter
352 //purpose  : return gravity center of the source: average coordinates of all nodes
353 //=======================================================================
354
355 SMESH::PointStruct Measurements_i::GravityCenter(SMESH::SMESH_IDSource_ptr theSource)
356 {
357   SMESH::PointStruct grCenter = { 0.,0.,0. };
358   const SMESHDS_Mesh* mesh = getMesh( theSource );
359   if ( !mesh )
360     return grCenter;
361
362   // unmark all nodes; visited nodes will be marked
363   SMESH_MeshAlgos::MarkElems( mesh->nodesIterator(), /*isMarked=*/false );
364
365   gp_XYZ sumCoord( 0,0,0 );
366   int nodeCount = 0;
367
368   SMDS_ElemIteratorPtr eIt = SMESH_Mesh_i::GetElements( theSource, SMESH::ALL );
369   while ( eIt->more() )
370   {
371     const SMDS_MeshElement*   elem = eIt->next();
372     for ( SMDS_NodeIteratorPtr nIt = elem->nodeIterator(); nIt->more(); )
373     {
374       const SMDS_MeshNode* n = nIt->next();
375       if ( !n->isMarked() )
376       {
377         sumCoord += SMESH_NodeXYZ( n );
378         ++nodeCount;
379         n->setIsMarked( true );
380       }
381     }
382   }
383   sumCoord /= nodeCount;
384
385   grCenter.x = sumCoord.X();
386   grCenter.y = sumCoord.Y();
387   grCenter.z = sumCoord.Z();
388
389   return grCenter;
390 }
391
392 //=======================================================================
393 //function : Angle
394 //purpose  : Return angle in radians defined by 3 points <(p1,p2,p3)
395 //=======================================================================
396
397 CORBA::Double Measurements_i::Angle(const SMESH::PointStruct& p1,
398                                     const SMESH::PointStruct& p2,
399                                     const SMESH::PointStruct& p3 )
400 {
401   gp_Vec v1( p1.x - p2.x, p1.y - p2.y, p1.z - p2.z );
402   gp_Vec v2( p3.x - p2.x, p3.y - p2.y, p3.z - p2.z );
403
404   double angle = -1;
405
406   try
407   {
408     angle = v1.Angle( v2 );
409   }
410   catch(...)
411   {
412   }
413   if ( std::isnan( angle ))
414     angle = -1;
415
416   return angle;
417 }