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