Salome HOME
Unicode support
[modules/geom.git] / src / CurveCreator / CurveCreator_UtilsICurve.cxx
1 // Copyright (C) 2013-2016  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, or (at your option) any later version.
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 #include <gp_Pnt.hxx>
24
25 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
26
27 int CurveCreator_UtilsICurve::findLocalPointIndex( const CurveCreator_ICurve* theCurve,
28                                              int theSectionId, double theX, double theY )
29 {
30   int aPntIndex = -1;
31   if ( !theCurve )
32     return aPntIndex;
33
34   CurveCreator::Coordinates aCoords;
35   for ( int i = 0, aNb = theCurve->getNbPoints( theSectionId ); i < aNb && aPntIndex < 0; i++ ) {
36     aCoords = theCurve->getPoint( theSectionId, i );
37     if ( aCoords.size() < 2 )
38       continue;
39     if ( fabs( aCoords[0] - theX ) < LOCAL_SELECTION_TOLERANCE &&
40          fabs( aCoords[1] - theY ) < LOCAL_SELECTION_TOLERANCE )
41       aPntIndex = i;
42   }
43
44   return aPntIndex;
45 }
46
47 void CurveCreator_UtilsICurve::findSectionsToPoints( const CurveCreator_ICurve* theCurve,
48                                  const double theX, const double theY,
49                                  CurveCreator_ICurve::SectionToPointList& thePoints )
50 {
51   thePoints.clear();
52
53   int aPointId = -1;
54   for ( int i = 0, aNb = theCurve->getNbSections(); i < aNb; i++ ) {
55     aPointId = CurveCreator_UtilsICurve::findLocalPointIndex( theCurve, i, theX, theY );
56     if ( aPointId < 0 )
57       continue;
58     CurveCreator_ICurve::SectionToPoint aPoint = std::make_pair( i, aPointId );
59     if ( !CurveCreator_UtilsICurve::contains( thePoints, aPoint ) )
60       thePoints.push_back( aPoint );
61   }
62 }
63
64 void CurveCreator_UtilsICurve::convert( const CurveCreator_ICurve::SectionToPointList& thePoints,
65                                   QMap<int, QList<int> >& theConvPoints )
66 {
67   theConvPoints.clear();
68
69   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
70                                                           aLast = thePoints.end();
71   QList<int> aPoints;
72   int aSectionId, aPointId;
73   for ( ; anIt != aLast; anIt++ ) {
74     aSectionId = anIt->first;
75     aPointId = anIt->second;
76     aPoints.clear();
77     if ( theConvPoints.contains( aSectionId ) )
78       aPoints = theConvPoints[aSectionId];
79     if ( aPoints.contains( aPointId ) )
80       continue;
81     aPoints.append( aPointId );
82     theConvPoints[aSectionId] = aPoints;
83   }
84 }
85
86 #include "CurveCreator_Curve.hxx" // TODO
87 void CurveCreator_UtilsICurve::getPoint( const CurveCreator_ICurve* theCurve, const int theISection,
88                                          const int theIPoint, gp_Pnt& thePoint )
89 {
90   double anX, anY, aZ;
91   // TODO
92   const CurveCreator_Curve* aCurve = dynamic_cast<const CurveCreator_Curve*>( theCurve );
93   if ( aCurve )
94     aCurve->getCoordinates( theISection, theIPoint, anX, anY, aZ );
95   thePoint = gp_Pnt( anX, anY, aZ);
96 }
97
98 std::string CurveCreator_UtilsICurve::getUniqSectionName( CurveCreator_ICurve* theCurve )
99 {
100   for( int i = 0 ; i < 1000000 ; i++ ){
101     char aBuffer[255];
102     sprintf( aBuffer, "Section_%d", i+1 );
103     std::string aName(aBuffer);
104     int j;
105     for( j = 0 ; j < theCurve->getNbSections() ; j++ ){
106       if( theCurve->getSectionName(j) == aName )
107         break;
108     }
109     if( j == theCurve->getNbSections() )
110       return aName;
111   }
112   return "";
113 }
114
115 bool CurveCreator_UtilsICurve::contains( const CurveCreator_ICurve::SectionToPointList& theList,
116                                          const CurveCreator_ICurve::SectionToPoint& theValue )
117 {
118   bool isFound = false;
119
120   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = theList.begin(),
121                                                           aLast = theList.end();
122   for ( ; anIt != aLast && !isFound; anIt++ )
123     isFound = anIt->first == theValue.first && anIt->second == theValue.second;
124
125   return isFound;
126 }