X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FSMESH_I%2FSMESH_Measurements_i.cxx;h=f81c0573b65f65c2c9acd0841956b550ef89b23d;hp=f078bdc10fe6c1f67188b8d756128b08b67beee6;hb=refs%2Ftags%2FV9_7_0a1;hpb=6bac08c1a81f34d3f21c550bd92f83654b2546a5 diff --git a/src/SMESH_I/SMESH_Measurements_i.cxx b/src/SMESH_I/SMESH_Measurements_i.cxx index f078bdc10..f81c0573b 100644 --- a/src/SMESH_I/SMESH_Measurements_i.cxx +++ b/src/SMESH_I/SMESH_Measurements_i.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -22,19 +22,23 @@ // File : SMESH_Measurements_i.cxx // Author : Pavel TELKOV, Open CASCADE S.A.S. (pavel.telkov@opencascade.com) -#include "SMESH_Measurements_i.hxx" +#ifdef WIN32 +#define NOMINMAX +#endif -#include "SMESH_Gen_i.hxx" -#include "SMESH_Filter_i.hxx" -#include "SMESH_PythonDump.hxx" +#include "SMESH_Measurements_i.hxx" +#include "SMDS_ElemIterator.hxx" #include "SMDS_Mesh.hxx" -#include "SMDS_MeshNode.hxx" #include "SMDS_MeshElement.hxx" -#include "SMDS_ElemIterator.hxx" - +#include "SMDS_MeshNode.hxx" #include "SMESHDS_Mesh.hxx" +#include "SMESH_Filter_i.hxx" +#include "SMESH_Gen_i.hxx" +#include "SMESH_MeshAlgos.hxx" +#include "SMESH_PythonDump.hxx" +#include using namespace SMESH; @@ -116,11 +120,39 @@ static bool getNodeNodeDistance (SMESH::Measure& theMeasure, return true; } +static bool getNodeElemDistance (SMESH::Measure& theMeasure, + const SMDS_MeshNode* theNode, + SMESH_ElementSearcher* theElemSearcher) +{ + if ( !theNode || !theElemSearcher ) + return false; + + const SMDS_MeshElement* closestElement = 0; + gp_Pnt point = SMESH_NodeXYZ( theNode ); + gp_Pnt closestPoint = theElemSearcher->Project( point, SMDSAbs_All, &closestElement ); + + if ( closestElement ) + { + theMeasure.value = point.Distance( closestPoint ); + theMeasure.node1 = theNode->GetID(); + theMeasure.elem2 = closestElement->GetID(); + theMeasure.maxX = closestPoint.X(); + theMeasure.maxY = closestPoint.Y(); + theMeasure.maxZ = closestPoint.Z(); + theMeasure.minX = closestPoint.X() - point.X(); + theMeasure.minY = closestPoint.Y() - point.Y(); + theMeasure.minZ = closestPoint.Z() - point.Z(); + } + + return closestElement; +} + static SMESHDS_Mesh* getMesh(SMESH::SMESH_IDSource_ptr theSource) { if (!CORBA::is_nil( theSource )) { - SMESH_Mesh_i* anImplPtr = DownCast(theSource->GetMesh()); + SMESH::SMESH_Mesh_var mesh = theSource->GetMesh(); + SMESH_Mesh_i* anImplPtr = DownCast( mesh ); if (anImplPtr) return anImplPtr->GetImpl().GetMeshDS(); } @@ -132,7 +164,8 @@ static bool isNodeType (SMESH::array_of_ElementType_var theTypes) return theTypes->length() > 0 && theTypes[0] == SMESH::NODE; } -static double getNumericalValue(SMESH::SMESH_IDSource_ptr theSource, SMESH::Controls::NumericalFunctorPtr theFunctor) +static double getNumericalValue(SMESH::SMESH_IDSource_ptr theSource, + SMESH::Controls::NumericalFunctorPtr theFunctor) { double value = 0; @@ -141,8 +174,8 @@ static double getNumericalValue(SMESH::SMESH_IDSource_ptr theSource, SMESH::Cont if ( aMesh ) { theFunctor->SetMesh( aMesh ); - SMESH::long_array_var anElementsId = theSource->GetIDs(); - for (int i = 0; i < anElementsId->length(); i++) { + SMESH::smIdType_array_var anElementsId = theSource->GetIDs(); + for ( CORBA::ULong i = 0; i < anElementsId->length(); i++) { value += theFunctor->GetValue( anElementsId[i] ); } } @@ -176,9 +209,8 @@ SMESH::Measure Measurements_i::MinDistance bool isNode1 = isNodeType(types1); bool isNode2 = isOrigin || isNodeType(types2); - SMESH::long_array_var aElementsId1 = theSource1->GetIDs(); - SMESH::long_array_var aElementsId2; - if ( !isOrigin ) aElementsId2 = theSource2->GetIDs(); + SMESH::smIdType_array_var aElementsId1 = theSource1->GetIDs(); + SMESH::smIdType_array_var aElementsId2; // compute distance between two entities /* NOTE: currently only node-to-node case is implemented @@ -191,10 +223,25 @@ SMESH::Measure Measurements_i::MinDistance // node - node const SMESHDS_Mesh* aMesh1 = getMesh( theSource1 ); const SMESHDS_Mesh* aMesh2 = isOrigin ? 0 : getMesh( theSource2 ); + if ( !isOrigin ) aElementsId2 = theSource2->GetIDs(); const SMDS_MeshNode* theNode1 = aMesh1 ? aMesh1->FindNode( aElementsId1[0] ) : 0; const SMDS_MeshNode* theNode2 = aMesh2 ? aMesh2->FindNode( aElementsId2[0] ) : 0; getNodeNodeDistance( aMeasure, theNode1, theNode2 ); } + if (isNode1 && !isNode2 && aElementsId1->length() == 1 ) + { + // node - elements + SMESHDS_Mesh* aMesh1 = getMesh( theSource1 ); + SMESHDS_Mesh* aMesh2 = getMesh( theSource2 ); + if ( aMesh1 && aMesh2 ) + { + const SMDS_MeshNode* aNode = aMesh1->FindNode( aElementsId1[0] ); + SMDS_ElemIteratorPtr anElemIt = SMESH_Mesh_i::GetElements( theSource2, SMESH::ALL ); + std::unique_ptr< SMESH_ElementSearcher > aSearcher + ( SMESH_MeshAlgos::GetElementSearcher( *aMesh2, anElemIt )); + getNodeElemDistance( aMeasure, aNode, aSearcher.get() ); + } + } else { // NOT_IMPLEMENTED @@ -220,12 +267,12 @@ static void enlargeBoundingBox(const SMDS_MeshNode* theNode, theMeasure.node1 = theNode->GetID(); } else { - theMeasure.minX = min( theMeasure.minX, theNode->X() ); - theMeasure.maxX = max( theMeasure.maxX, theNode->X() ); - theMeasure.minY = min( theMeasure.minY, theNode->Y() ); - theMeasure.maxY = max( theMeasure.maxY, theNode->Y() ); - theMeasure.minZ = min( theMeasure.minZ, theNode->Z() ); - theMeasure.maxZ = max( theMeasure.maxZ, theNode->Z() ); + theMeasure.minX = std::min( theMeasure.minX, theNode->X() ); + theMeasure.maxX = std::max( theMeasure.maxX, theNode->X() ); + theMeasure.minY = std::min( theMeasure.minY, theNode->Y() ); + theMeasure.maxY = std::max( theMeasure.maxY, theNode->Y() ); + theMeasure.minZ = std::min( theMeasure.minZ, theNode->Z() ); + theMeasure.maxZ = std::max( theMeasure.maxZ, theNode->Z() ); } } @@ -241,26 +288,32 @@ static void enlargeBoundingBox(const SMESH::SMESH_IDSource_ptr theObject, const SMESHDS_Mesh* aMesh = getMesh( theObject ); if ( !aMesh ) return; - SMESH::array_of_ElementType_var types = theObject->GetTypes(); - SMESH::long_array_var aElementsId = theObject->GetIDs(); - // here we assume that type of all IDs defined by first type in array - const bool isNode = isNodeType( types ); - for(int i = 0, n = aElementsId->length(); i < n; i++) + + if ( DownCast( theObject )) // theObject is mesh + { + for (SMDS_NodeIteratorPtr aNodeIter = aMesh->nodesIterator(); aNodeIter->more(); ) + enlargeBoundingBox( aNodeIter->next(), theMeasure); + } + else { - if (isNode) - enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure); - else + SMESH::array_of_ElementType_var types = theObject->GetTypes(); + SMESH::smIdType_array_var aElementsId = theObject->GetIDs(); + // here we assume that type of all IDs defined by first type in array + const bool isNode = isNodeType( types ); + for(int i = 0, n = aElementsId->length(); i < n; i++) { - const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] ); - if (!elem) - continue; - SMDS_ElemIteratorPtr aNodeIter = elem->nodesIterator(); - while( aNodeIter->more() ) - enlargeBoundingBox( dynamic_cast( aNodeIter->next() ), theMeasure); + if (isNode) + enlargeBoundingBox( aMesh->FindNode( aElementsId[i] ), theMeasure); + else + { + if ( const SMDS_MeshElement* elem = aMesh->FindElement( aElementsId[i] )) + for (SMDS_NodeIteratorPtr aNodeIter = elem->nodeIterator(); aNodeIter->more(); ) + enlargeBoundingBox( aNodeIter->next(), theMeasure); + } } } } - + //======================================================================= // name : BoundingBox // Purpose : compute common bounding box of entities @@ -303,3 +356,72 @@ double Measurements_i::Volume(SMESH::SMESH_IDSource_ptr theSource) { return getNumericalValue( theSource, SMESH::Controls::NumericalFunctorPtr(new SMESH::Controls::Volume()) ); } + +//======================================================================= +//function : GravityCenter +//purpose : return gravity center of the source: average coordinates of all nodes +//======================================================================= + +SMESH::PointStruct Measurements_i::GravityCenter(SMESH::SMESH_IDSource_ptr theSource) +{ + SMESH::PointStruct grCenter = { 0.,0.,0. }; + const SMESHDS_Mesh* mesh = getMesh( theSource ); + if ( !mesh ) + return grCenter; + + // unmark all nodes; visited nodes will be marked + SMESH_MeshAlgos::MarkElems( mesh->nodesIterator(), /*isMarked=*/false ); + + gp_XYZ sumCoord( 0,0,0 ); + int nodeCount = 0; + + SMDS_ElemIteratorPtr eIt = SMESH_Mesh_i::GetElements( theSource, SMESH::ALL ); + while ( eIt->more() ) + { + const SMDS_MeshElement* elem = eIt->next(); + for ( SMDS_NodeIteratorPtr nIt = elem->nodeIterator(); nIt->more(); ) + { + const SMDS_MeshNode* n = nIt->next(); + if ( !n->isMarked() ) + { + sumCoord += SMESH_NodeXYZ( n ); + ++nodeCount; + n->setIsMarked( true ); + } + } + } + sumCoord /= nodeCount; + + grCenter.x = sumCoord.X(); + grCenter.y = sumCoord.Y(); + grCenter.z = sumCoord.Z(); + + return grCenter; +} + +//======================================================================= +//function : Angle +//purpose : Return angle in radians defined by 3 points <(p1,p2,p3) +//======================================================================= + +CORBA::Double Measurements_i::Angle(const SMESH::PointStruct& p1, + const SMESH::PointStruct& p2, + const SMESH::PointStruct& p3 ) +{ + gp_Vec v1( p1.x - p2.x, p1.y - p2.y, p1.z - p2.z ); + gp_Vec v2( p3.x - p2.x, p3.y - p2.y, p3.z - p2.z ); + + double angle = -1; + + try + { + angle = v1.Angle( v2 ); + } + catch(...) + { + } + if ( std::isnan( angle )) + angle = -1; + + return angle; +}