Salome HOME
fa52f66e6308e5ae9f56fb41281fc456467eec7d
[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 double getNodeNodeDistance (const SMDS_MeshNode* theNode1,
99                                    const SMDS_MeshNode* theNode2)
100 {
101   double dist = 0., dd = 0.;
102   if (!theNode1 || !theNode2)
103     return dist;
104   dd = theNode1->X(); dd -= theNode2->X(); dd *= dd; dist += dd;
105   dd = theNode1->Y(); dd -= theNode2->Y(); dd *= dd; dist += dd;
106   dd = theNode1->Z(); dd -= theNode2->Z(); dd *= dd; dist += dd;
107   if (dist < 0)
108     return 0;
109   return sqrt(dist);
110 }
111
112 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
113 {
114   if (!CORBA::is_nil( theSource ))
115   {
116     SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
117     if (anImplPtr)
118       return anImplPtr->GetImpl().GetMeshDS();
119   }
120   return 0;
121 }
122
123 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
124 {
125   return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
126 }
127
128 //=======================================================================
129 // name    : MinDistance
130 // Purpose : minimal distance between two given entities
131 //=======================================================================
132 SMESH::Measure Measurements_i::MinDistance
133  (SMESH::SMESH_IDSource_ptr theSource1,
134   SMESH::SMESH_IDSource_ptr theSource2)
135 {
136   SMESH::Measure aMeasure;
137   initMeasure(aMeasure);
138
139   if (CORBA::is_nil( theSource1 ) || CORBA::is_nil( theSource2 ))
140     return aMeasure;
141
142   // calculate minimal distance between two mesh entities
143   SMESH::array_of_ElementType_var types1 = theSource1->GetTypes();
144   SMESH::array_of_ElementType_var types2 = theSource2->GetTypes();
145   // here we assume that type of all IDs defined by first type in array
146   const bool isNode1 = isNodeType(types1);
147   const bool isNode2 = isNodeType(types2);
148
149   SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
150   SMESH::long_array_var aElementsId2 = theSource2->GetIDs();
151
152   // compute distance between two entities
153   /** NOTE: currently only node-node case implemented
154    * all other cases could be implemented later
155    * this IF should be replaced by comples swtich
156    * on mesh entities types
157    */
158   if (isNode1 && isNode2)
159   {
160     // node - node
161     const SMESHDS_Mesh* aMesh1 = getMesh( theSource1 );
162     const SMESHDS_Mesh* aMesh2 = getMesh( theSource2 );
163     const SMDS_MeshNode* theNode1 = aMesh1 ? aMesh1->FindNode( aElementsId1[0] ) : 0;
164     const SMDS_MeshNode* theNode2 = aMesh2 ? aMesh2->FindNode( aElementsId2[0] ) : 0;
165     aMeasure.value = getNodeNodeDistance( theNode1, theNode2 );
166     if (theNode1 && theNode2)
167     {
168       aMeasure.node1 = theNode1->GetID();
169       aMeasure.node2 = theNode2->GetID();
170     }
171   }
172   else
173   {
174     // NOT_IMPLEMENTED
175   }
176
177   return aMeasure;
178 }
179
180 //=======================================================================
181 // name    : enlargeBoundingBox
182 // Purpose : 
183 //=======================================================================
184 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
185                                SMESH::Measure&      theMeasure)
186 {
187   if (!theNode)
188     return;
189   theMeasure.minX = min( theMeasure.minX, theNode->X() );
190   theMeasure.maxX = max( theMeasure.maxX, theNode->X() );
191   theMeasure.minY = min( theMeasure.minY, theNode->Y() );
192   theMeasure.maxY = max( theMeasure.maxY, theNode->Y() );
193   theMeasure.minZ = min( theMeasure.minZ, theNode->Z() );
194   theMeasure.maxZ = max( theMeasure.maxZ, theNode->Z() );
195 }
196
197 //=======================================================================
198 // name    : enlargeBoundingBox
199 // Purpose : 
200 //=======================================================================
201 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
202                                SMESH::Measure&                 theMeasure)
203 {
204   if ( CORBA::is_nil( theObject ) )
205     return;
206   const SMESHDS_Mesh* aMesh = getMesh( theObject );
207   if ( !aMesh )
208     return;
209   SMESH::array_of_ElementType_var types = theObject->GetTypes();
210   SMESH::long_array_var     aElementsId = theObject->GetIDs();
211   // here we assume that type of all IDs defined by first type in array
212   const bool isNode = isNodeType( types );
213   for(int i = 0, n = aElementsId->length(); i < n; i++)
214   {
215     if (isNode)
216       enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
217     else
218     {
219       const SMDS_MeshElement * elem = aMesh->FindElement( aElementsId[i] );
220       if (!elem)
221         continue;
222       SMDS_ElemIteratorPtr aNodeIter = elem->nodesIterator();
223       while( aNodeIter->more() )
224         enlargeBoundingBox( dynamic_cast<const SMDS_MeshNode*>( aNodeIter->next() ), theMeasure);
225     }
226   }
227 }
228                                
229 //=======================================================================
230 // name    : BoundingBox
231 // Purpose : compute common bounding box of entities
232 //=======================================================================
233 SMESH::Measure Measurements_i::BoundingBox (const SMESH::ListOfIDSources& theSources)
234 {
235   SMESH::Measure aMeasure;
236   initMeasure(aMeasure);
237
238   // calculate bounding box on sources
239   for ( int i = 0, n = theSources.length(); i < n ; ++i )
240     enlargeBoundingBox( theSources[i], aMeasure );
241
242   return aMeasure;
243 }