Back to index

salome-geom  6.5.0
GEOMAlgo_FinderShapeOnQuad.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File      : GEOMAlgo_FinderShapeOnQuad.cxx
00024 // Created   : Mon Oct 17 17:31:45 2005
00025 // Author    : Edward AGAPOV (eap)
00026 //
00027 #include "GEOMAlgo_FinderShapeOnQuad.hxx"
00028 #include "GEOMAlgo_SurfaceTools.hxx"
00029 
00030 #include <gp_Pnt.hxx>
00031 #include <gp_Vec.hxx>
00032 #include <Geom_Plane.hxx>
00033 
00034 
00035 GEOMAlgo_FinderShapeOnQuad::GEOMAlgo_FinderShapeOnQuad(const gp_Pnt & theTopLeftPoint,
00036                                                        const gp_Pnt & theTopRigthPoint,
00037                                                        const gp_Pnt & theBottomLeftPoint,
00038                                                        const gp_Pnt & theBottomRigthPoint)
00039 {
00040   myPoints.resize(6);
00041   myPoints[0] = theTopLeftPoint    ;
00042   myPoints[1] = theTopRigthPoint   ;
00043   myPoints[2] = theBottomRigthPoint;
00044   myPoints[3] = theBottomLeftPoint ;
00045   myPoints[4] = myPoints[0];
00046   myPoints[5] = myPoints[1];
00047 
00048   // Find plane normal defined by corner points, it will be used to define a plane
00049   // for each quadrangle side.
00050   myQuadNormal.SetCoord (0,0,0);
00051   for ( int i = 1; i <= 4; ++i )
00052     myQuadNormal += gp_Vec( myPoints[i], myPoints[i+1] ) ^ gp_Vec( myPoints[i], myPoints[i-1] );
00053   //std::cout<<std::endl<<" X Vec : "<<myQuadNormal.X()<<" "<<myQuadNormal.Y()<<" "<<myQuadNormal.Z()<<" "<<endl;
00054 
00055   if ( myQuadNormal.SquareMagnitude() <= DBL_MIN ) {
00056     myErrorStatus = 101;
00057     return;
00058   }
00059 
00060   // detect concave quadrangle sides
00061   myConcaveQuad = false;
00062   myConcaveSide.resize (4, false);
00063   for ( int i = 1; i <= 4; ++i ) {
00064     gp_Vec localQN = gp_Vec( myPoints[i], myPoints[i+1] ) ^ gp_Vec( myPoints[i], myPoints[i-1] );
00065     if ( myQuadNormal * localQN < 0 )
00066       myConcaveSide[i-1] = myConcaveSide[i] = myConcaveQuad = true;
00067   }
00068 
00069   // loop on quadrangle sides
00070   myPlanes.reserve( 4 );
00071   for ( int i = 0; i < 4; ++i )
00072   {
00073     // point1 -> point2 vector
00074     gp_Vec aSideVec( myPoints[ i ], myPoints[ i + 1 ]);
00075     //std::cout<<" Y Vec : "<<aSideVec.X()<<" "<<aSideVec.Y()<<" "<<aSideVec.Z()<<" "<<endl;
00076 
00077     // plane normal
00078     gp_Vec aSideNorm = aSideVec ^ myQuadNormal;
00079     if ( aSideNorm.SquareMagnitude() <= DBL_MIN )
00080       continue;
00081     //std::cout<<" Z Vec : "<<aSideNorm.X()<<" "<<aSideNorm.Y()<<" "<<aSideNorm.Z()<<" "<<endl;
00082 
00083     // make plane
00084     Handle(Geom_Plane) aPlane = new Geom_Plane( myPoints[ i ], aSideNorm );
00085     myPlanes.push_back( GeomAdaptor_Surface() );
00086     myPlanes.back().Load( aPlane );
00087   }
00088 }
00089 
00090 //=======================================================================
00091 //function : CheckData
00092 //purpose  :
00093 //=======================================================================
00094 
00095 void GEOMAlgo_FinderShapeOnQuad::CheckData()
00096 {
00097   if ( !myPlanes.empty() )
00098     mySurface = myPlanes[0].Surface();
00099   GEOMAlgo_FinderShapeOn1::CheckData();
00100 }
00101 
00102 //=======================================================================
00103 //function : GetPointState
00104 //purpose  :
00105 //=======================================================================
00106 
00107 TopAbs_State GEOMAlgo_FinderShapeOnQuad::GetPointState(const gp_Pnt& aP)
00108 {
00109   // Return IN if aP has TopAbs_IN with all sides.
00110   // In the case of concave quadrangle, return IN if
00111   // aP is OUT of only one concave side
00112   double nbIn = 0.;
00113   for ( int i = 0; i < myPlanes.size(); ++i )
00114   {
00115     TopAbs_State aSt;
00116     GEOMAlgo_SurfaceTools::GetState(aP, myPlanes[i], myTolerance, aSt);
00117     if ( aSt == TopAbs_IN )
00118     {
00119       nbIn += myConcaveSide[i] ? 0.5 : 1.0;
00120     }
00121     else if ( aSt == TopAbs_ON )
00122     {
00123       // check that aP is between quadrangle corners
00124       Handle(Geom_Plane) aSidePlane = Handle(Geom_Plane)::DownCast( myPlanes[i].Surface() );
00125       gp_Vec aSideNorm = aSidePlane->Axis().Direction();
00126       gp_Vec aSideVec = myQuadNormal ^ aSideNorm;
00127       gp_Vec c1p ( myPoints[i], aP );
00128       gp_Vec pc2 ( aP, myPoints[i+1] );
00129       if ( aSideVec * c1p >= 0. && aSideVec * pc2 >= 0. )
00130         return TopAbs_ON;
00131       // consider to be IN (???????????)
00132       //nbIn += myConcaveSide[i] ? 0.5 : 1.0;
00133     }
00134   }
00135   Standard_Real inThreshold = myPlanes.size(); // usually 4.0
00136   if ( myConcaveQuad )
00137     inThreshold = 2.5; // 1.0 + 1.0 + 0.5
00138 
00139   if ( nbIn >= inThreshold )
00140     return TopAbs_IN;
00141 
00142   return TopAbs_OUT;
00143 }
00144