1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_Filter_i.cxx
25 // Author : Alexey Petrov, OCC
28 #include "SMESH_Measurements_i.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_PythonDump.hxx"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_ElemIterator.hxx"
38 #include "SMESHDS_Mesh.hxx"
41 using namespace SMESH;
44 * this local function to avoid uninitialized fields
46 static void initMeasure( SMESH::Measure& theMeasure)
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.;
56 //=============================================================================
58 * SMESH_Gen_i::CreateMeasurements
60 * Create measurement instance
62 //=============================================================================
64 SMESH::Measurements_ptr SMESH_Gen_i::CreateMeasurements()
66 SMESH::Measurements_i* aMeasure = new SMESH::Measurements_i();
67 SMESH::Measurements_var anObj = aMeasure->_this();
74 Description : make measure of mesh qunatities
77 //=======================================================================
78 // name : Measurements_i
79 // Purpose : Constructor
80 //=======================================================================
81 Measurements_i::Measurements_i()
82 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
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 );
89 //=======================================================================
90 // name : ~Measurements_i
91 // Purpose : Destructor
92 //=======================================================================
93 Measurements_i::~Measurements_i()
95 //TPythonDump()<<this<<".Destroy()";
98 static bool getNodeNodeDistance (SMESH::Measure& theMeasure,
99 const SMDS_MeshNode* theNode1,
100 const SMDS_MeshNode* theNode2 = 0)
102 double dist = 0., dd = 0.;
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;
114 theMeasure.value = sqrt(dist);
115 theMeasure.node1 = theNode1->GetID();
116 theMeasure.node2 = theNode2 ? theNode2->GetID() : 0;
121 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
123 if (!CORBA::is_nil( theSource ))
125 SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
127 return anImplPtr->GetImpl().GetMeshDS();
132 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
134 return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
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)
145 SMESH::Measure aMeasure;
146 initMeasure(aMeasure);
148 if (CORBA::is_nil( theSource1 ))
151 // if second source is null, min distance from theSource1 to the origin is calculated
152 bool isOrigin = CORBA::is_nil( theSource2 );
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();
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);
163 SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
164 SMESH::long_array_var aElementsId2;
165 if ( !isOrigin ) aElementsId2 = theSource2->GetIDs();
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
173 if (isNode1 && isNode2)
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 );
190 //=======================================================================
191 // name : enlargeBoundingBox
193 //=======================================================================
194 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
195 SMESH::Measure& theMeasure)
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() );
207 //=======================================================================
208 // name : enlargeBoundingBox
210 //=======================================================================
211 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
212 SMESH::Measure& theMeasure)
214 if ( CORBA::is_nil( theObject ) )
216 const SMESHDS_Mesh* aMesh = getMesh( theObject );
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++)
226 enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
229 const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] );
232 SMDS_ElemIteratorPtr aNodeIter = elem->nodesIterator();
233 while( aNodeIter->more() )
234 enlargeBoundingBox( dynamic_cast<const SMDS_MeshNode*>( aNodeIter->next() ), theMeasure);
239 //=======================================================================
240 // name : BoundingBox
241 // Purpose : compute common bounding box of entities
242 //=======================================================================
243 SMESH::Measure Measurements_i::BoundingBox (const SMESH::ListOfIDSources& theSources)
245 SMESH::Measure aMeasure;
246 initMeasure(aMeasure);
248 // calculate bounding box on sources
249 for ( int i = 0, n = theSources.length(); i < n ; ++i )
250 enlargeBoundingBox( theSources[i], aMeasure );