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 double getNodeNodeDistance (const SMDS_MeshNode* theNode1,
99 const SMDS_MeshNode* theNode2)
101 double dist = 0., dd = 0.;
102 if (!theNode1 || !theNode2)
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;
112 static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource)
114 if (!CORBA::is_nil( theSource ))
116 SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theSource->GetMesh());
118 return anImplPtr->GetImpl().GetMeshDS();
123 static bool isNodeType (SMESH::array_of_ElementType_var theTypes)
125 return theTypes->length() > 0 && theTypes[0] == SMESH::NODE;
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)
136 SMESH::Measure aMeasure;
137 initMeasure(aMeasure);
139 if (CORBA::is_nil( theSource1 ) || CORBA::is_nil( theSource2 ))
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);
149 SMESH::long_array_var aElementsId1 = theSource1->GetIDs();
150 SMESH::long_array_var aElementsId2 = theSource2->GetIDs();
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
158 if (isNode1 && isNode2)
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)
168 aMeasure.node1 = theNode1->GetID();
169 aMeasure.node2 = theNode2->GetID();
180 //=======================================================================
181 // name : enlargeBoundingBox
183 //=======================================================================
184 static void enlargeBoundingBox(const SMDS_MeshNode* theNode,
185 SMESH::Measure& theMeasure)
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() );
197 //=======================================================================
198 // name : enlargeBoundingBox
200 //=======================================================================
201 static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject,
202 SMESH::Measure& theMeasure)
204 if ( CORBA::is_nil( theObject ) )
206 const SMESHDS_Mesh* aMesh = getMesh( theObject );
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++)
216 enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure);
219 const SMDS_MeshElement * elem = aMesh->FindElement( aElementsId[i] );
222 SMDS_ElemIteratorPtr aNodeIter = elem->nodesIterator();
223 while( aNodeIter->more() )
224 enlargeBoundingBox( dynamic_cast<const SMDS_MeshNode*>( aNodeIter->next() ), theMeasure);
229 //=======================================================================
230 // name : BoundingBox
231 // Purpose : compute common bounding box of entities
232 //=======================================================================
233 SMESH::Measure Measurements_i::BoundingBox (const SMESH::ListOfIDSources& theSources)
235 SMESH::Measure aMeasure;
236 initMeasure(aMeasure);
238 // calculate bounding box on sources
239 for ( int i = 0, n = theSources.length(); i < n ; ++i )
240 enlargeBoundingBox( theSources[i], aMeasure );