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