Salome HOME
Save for a list of the local selected points.
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_UtilsICurve.cxx
1 // Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CurveCreator_UtilsICurve.hxx"
21
22 #include "CurveCreator.hxx"
23
24 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
25
26 int CurveCreator_UtilsICurve::findLocalPointIndex( const CurveCreator_ICurve* theCurve,
27                                              int theSectionId, float theX, float theY )
28 {
29   int aPntIndex = -1;
30   if ( !theCurve )
31     return aPntIndex;
32
33   CurveCreator::Coordinates aCoords;
34   for ( int i = 0, aNb = theCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
35     aCoords = theCurve->getPoint( theSectionId, i );
36     if ( aCoords.size() < 2 )
37       continue;
38     if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
39          fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
40       aPntIndex = i;
41   }
42
43   return aPntIndex;
44 }
45
46 void CurveCreator_UtilsICurve::findSectionsToPoints( const CurveCreator_ICurve* theCurve,
47                                  const double theX, const double theY,
48                                  CurveCreator_ICurve::SectionToPointList& thePoints )
49 {
50   thePoints.clear();
51
52   int aPointId = -1;
53   for ( int i = 0, aNb = theCurve->getNbSections(); i < aNb; i++ ) {
54     aPointId = CurveCreator_UtilsICurve::findLocalPointIndex( theCurve, i, theX, theY );
55     if ( aPointId < 0 )
56       continue;
57     CurveCreator_ICurve::SectionToPoint aPoint = std::make_pair( i, aPointId );
58     if ( !CurveCreator_UtilsICurve::contains( thePoints, aPoint ) )
59       thePoints.push_back( aPoint );
60   }
61 }
62
63 void CurveCreator_UtilsICurve::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
64                                   QMap<int, QList<int> >& theConvPoints )
65 {
66   theConvPoints.clear();
67
68   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
69                                                           aLast = thePoints.end();
70   QList<int> aPoints;
71   int aSectionId, aPointId;
72   for ( ; anIt != aLast; anIt++ ) {
73     aSectionId = anIt->first;
74     aPointId = anIt->second;
75     aPoints.clear();
76     if ( theConvPoints.contains( aSectionId ) )
77       aPoints = theConvPoints[aSectionId];
78     if ( aPoints.contains( aPointId ) )
79       continue;
80     aPoints.append( aPointId );
81     theConvPoints[aSectionId] = aPoints;
82   }
83 }
84
85 #include "CurveCreator_Curve.hxx"
86 void CurveCreator_UtilsICurve::getPoint( const CurveCreator_ICurve* theCurve, const int theISection,
87                                          const int theIPoint, gp_Pnt& thePoint )
88 {
89   double anX, anY, aZ;
90   // TODO
91   const CurveCreator_Curve* aCurve = dynamic_cast<const CurveCreator_Curve*>( theCurve );
92   if ( aCurve )
93     aCurve->getCoordinates( theISection, theIPoint, anX, anY, aZ );
94   thePoint = gp_Pnt( anX, anY, aZ);
95 }
96
97 std::string CurveCreator_UtilsICurve::getUniqSectionName( CurveCreator_ICurve* theCurve )
98 {
99   for( int i = 0 ; i < 1000000 ; i++ ){
100     char aBuffer[255];
101     sprintf( aBuffer, "Section_%d", i+1 );
102     std::string aName(aBuffer);
103     int j;
104     for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
105       if( theCurve->getSectionName(j) == aName )
106         break;
107     }
108     if( j == theCurve->getNbSections() )
109       return aName;
110   }
111   return "";
112 }
113
114 bool CurveCreator_UtilsICurve::contains( const CurveCreator_ICurve::SectionToPointList& theList,
115                                          const CurveCreator_ICurve::SectionToPoint& theValue )
116 {
117   bool isFound = false;
118
119   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = theList.begin(),
120                                                           aLast = theList.end();
121   for ( ; anIt != aLast && !isFound; anIt++ )
122     isFound = anIt->first == theValue.first && anIt->second == theValue.second;
123
124   return isFound;
125 }