Salome HOME
8da1cd7cf9c4b944f293a54e1319fa537968b978
[modules/smesh.git] / src / SMESH_I / SMESH_Measurements_i.cxx
1 // Copyright (C) 2007-2015  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 #include "SMESH_Measurements_i.hxx"
26
27 #include "SMESH_Gen_i.hxx"
28 #include "SMESH_Filter_i.hxx"
29 #include "SMESH_PythonDump.hxx"
30
31 #include "SMDS_Mesh.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_ElemIterator.hxx"
35
36 #include "SMESHDS_Mesh.hxx"
37
38
39 using namespace SMESH;
40
41 /**
42  * this local function to avoid uninitialized fields
43  */
44 static void initMeasure( SMESH::Measure& theMeasure)
45 {
46
47   theMeasure.minX = theMeasure.minY = theMeasure.minZ = 0.;
48   theMeasure.maxX = theMeasure.maxY = theMeasure.maxZ = 0.;
49   theMeasure.node1 = theMeasure.node2 = -1;
50   theMeasure.elem1 = theMeasure.elem2 = -1;
51   theMeasure.value = 0.;
52 }
53
54 //=============================================================================
55 /*!
56  *  SMESH_Gen_i::CreateMeasurements
57  *
58  *  Create measurement instance
59  */
60 //=============================================================================
61
62 SMESH::Measurements_ptr SMESH_Gen_i::CreateMeasurements()
63 {
64   SMESH::Measurements_i* aMeasure = new SMESH::Measurements_i();
65   SMESH::Measurements_var anObj = aMeasure->_this();
66   return anObj._retn();
67 }
68
69   
70 /*
71   Class       : Measurements
72   Description : make measure of mesh qunatities
73 */
74
75 //=======================================================================
76 // name    : Measurements_i
77 // Purpose : Constructor
78 //=======================================================================
79 Measurements_i::Measurements_i()
80 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
81 {
82   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
83   //PortableServer::ObjectId_var anObjectId =
84   //  SMESH_Gen_i::GetPOA()->activate_object( this );
85 }
86
87 //=======================================================================
88 // name    : ~Measurements_i
89 // Purpose : Destructor
90 //=======================================================================
91 Measurements_i::~Measurements_i()
92 {
93   //TPythonDump()<<this<<".UnRegister()";
94 }
95
96 static bool getNodeNodeDistance (SMESH::Measure& theMeasure,
97                                  const SMDS_MeshNode* theNode1,
98                                  const SMDS_MeshNode* theNode2 = 0)
99 {
100   double dist = 0., dd = 0.;
101
102   if (!theNode1)
103     return false;
104
105   dd = theNode1->X(); if (theNode2) dd -= theNode2->X(); theMeasure.minX = dd; dd *= dd; dist += dd;
106   dd = theNode1->Y(); if (theNode2) dd -= theNode2->Y(); theMeasure.minY = dd; dd *= dd; dist += dd;
107   dd = theNode1->Z(); if (theNode2) dd -= theNode2->Z(); theMeasure.minZ = dd; dd *= dd; dist += dd;
108
109   if (dist < 0)
110     return false;
111   
112   theMeasure.value = sqrt(dist);
113   theMeasure.node1 = theNode1->GetID();
114   theMeasure.node2 = theNode2 ? theNode2->GetID() : 0;
115
116   return true;
117 }
118
119 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
120 {
121   if (!CORBA::is_nil( theSource ))
122   {
123     SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
124     if (anImplPtr)
125       return anImplPtr->GetImpl().GetMeshDS();
126   }
127   return 0;
128 }
129
130 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
131 {
132   return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
133 }
134
135 static double getNumericalValue(SMESH::SMESH_IDSource_ptr            theSource,
136                                 SMESH::Controls::NumericalFunctorPtr theFunctor)
137 {
138   double value = 0;
139
140   if ( !CORBA::is_nil( theSource ) ) {
141     const SMESHDS_Mesh* aMesh = getMesh( theSource );
142     if ( aMesh ) {
143       theFunctor->SetMesh( aMesh );
144       
145       SMESH::long_array_var anElementsId = theSource->GetIDs();
146       for ( CORBA::ULong i = 0; i < anElementsId->length(); i++) {
147         value += theFunctor->GetValue( anElementsId[i] );
148       }
149     }
150   }
151   return value;
152 }
153
154 //=======================================================================
155 // name    : MinDistance
156 // Purpose : minimal distance between two given entities
157 //=======================================================================
158 SMESH::Measure Measurements_i::MinDistance
159  (SMESH::SMESH_IDSource_ptr theSource1,
160   SMESH::SMESH_IDSource_ptr theSource2)
161 {
162   SMESH::Measure aMeasure;
163   initMeasure(aMeasure);
164
165   if (CORBA::is_nil( theSource1 ))
166     return aMeasure;
167   
168   // if second source is null, min distance from theSource1 to the origin is calculated
169   bool isOrigin =  CORBA::is_nil( theSource2 );
170
171   // calculate minimal distance between two mesh entities
172   SMESH::array_of_ElementType_var types1 = theSource1->GetTypes();
173   SMESH::array_of_ElementType_var types2;
174   if ( !isOrigin ) types2 = theSource2->GetTypes();
175
176   // here we assume that type of all IDs defined by first type in array
177   bool isNode1 = isNodeType(types1);
178   bool isNode2 = isOrigin || isNodeType(types2);
179
180   SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
181   SMESH::long_array_var aElementsId2;
182   if ( !isOrigin ) aElementsId2 = theSource2->GetIDs();
183
184   // compute distance between two entities
185   /* NOTE: currently only node-to-node case is implemented
186    * all other cases will be implemented later
187    * below IF should be replaced by complete switch
188    * on mesh entities types
189    */
190   if (isNode1 && isNode2)
191   {
192     // node - node
193     const SMESHDS_Mesh* aMesh1 = getMesh( theSource1 );
194     const SMESHDS_Mesh* aMesh2 = isOrigin ? 0 : getMesh( theSource2 );
195     const SMDS_MeshNode* theNode1 = aMesh1 ? aMesh1->FindNode( aElementsId1[0] ) : 0;
196     const SMDS_MeshNode* theNode2 = aMesh2 ? aMesh2->FindNode( aElementsId2[0] ) : 0;
197     getNodeNodeDistance( aMeasure, theNode1, theNode2 );
198   }
199   else
200   {
201     // NOT_IMPLEMENTED
202   }
203
204   return aMeasure;
205 }
206
207 //=======================================================================
208 // name    : enlargeBoundingBox
209 // Purpose : 
210 //=======================================================================
211 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
212                                SMESH::Measure&      theMeasure)
213 {
214   if (!theNode)
215     return;
216   if ( theMeasure.node1 == -1 ) {
217     // we use this attribute as a flag that it is the first node added to the bnd box 
218     theMeasure.minX = theMeasure.maxX = theNode->X();
219     theMeasure.minY = theMeasure.maxY = theNode->Y();
220     theMeasure.minZ = theMeasure.maxZ = theNode->Z();
221     theMeasure.node1 = theNode->GetID();
222   }
223   else {
224     theMeasure.minX = std::min( theMeasure.minX, theNode->X() );
225     theMeasure.maxX = std::max( theMeasure.maxX, theNode->X() );
226     theMeasure.minY = std::min( theMeasure.minY, theNode->Y() );
227     theMeasure.maxY = std::max( theMeasure.maxY, theNode->Y() );
228     theMeasure.minZ = std::min( theMeasure.minZ, theNode->Z() );
229     theMeasure.maxZ = std::max( theMeasure.maxZ, theNode->Z() );
230   }
231 }
232
233 //=======================================================================
234 // name    : enlargeBoundingBox
235 // Purpose : 
236 //=======================================================================
237 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
238                                SMESH::Measure&                 theMeasure)
239 {
240   if ( CORBA::is_nil( theObject ) )
241     return;
242   const SMESHDS_Mesh* aMesh = getMesh( theObject );
243   if ( !aMesh )
244     return;
245   SMESH::array_of_ElementType_var types = theObject->GetTypes();
246   SMESH::long_array_var     aElementsId = theObject->GetIDs();
247   // here we assume that type of all IDs defined by first type in array
248   const bool isNode = isNodeType( types );
249   for(int i = 0, n = aElementsId->length(); i < n; i++)
250   {
251     if (isNode)
252       enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
253     else
254     {
255       const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] );
256       if (!elem)
257         continue;
258       SMDS_ElemIteratorPtr aNodeIter = elem->nodesIterator();
259       while( aNodeIter->more() )
260         enlargeBoundingBox( dynamic_cast<const SMDS_MeshNode*>( 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 }