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