Salome HOME
debug of DTM/Stream presentations
[modules/hydro.git] / src / HYDROData / HYDROData_SinusX.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_SinusX.h"
20
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Bathymetry.h>
23 #include <HYDROData_Entity.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_Profile.h>
26 #include <HYDROData_Iterator.h>
27
28 #include <gp_XYZ.hxx>
29 #include <gp_XY.hxx>
30
31 #include <QFile>
32 #include <QFileInfo>
33 #include <QString>
34 #include <QStringList>
35 #include <QTextStream>
36 #include <QColor>
37
38 HYDROData_SinusX::HYDROData_SinusX( ) 
39 {
40 }
41
42 HYDROData_SinusX::~HYDROData_SinusX()
43 {
44 }
45
46 void HYDROData_SinusX::CollectExistingNames(Handle_HYDROData_Document theDocument)
47 {
48   HYDROData_Iterator anIter( theDocument );
49   int anInd = 0;
50   myExistingNames.clear();
51   std::vector<int> anAllowedIndexes;
52   for( ; anIter.More(); anIter.Next() )
53     myExistingNames.push_back(anIter.Current()->GetName());
54 }
55
56 QString HYDROData_SinusX::GetName(const QString& theName)
57 {
58   if (myExistingNames.contains(theName))
59   { 
60     QString aName = theName; 
61     while (myExistingNames.contains(aName)) 
62     {
63       QStringList aList = aName.split("_");
64       int aLastInd = aName.lastIndexOf("_");
65       bool IsNum = false; 
66       int anInd = -1;
67       anInd = aList.last().toInt(&IsNum);
68       if (IsNum)
69       {
70         aName = aName.left(aLastInd) + "_";
71         aName+= QString::number(++anInd);
72       }
73       else
74         aName = theName + "_0";
75     }
76
77     myExistingNames.push_back(aName);
78     return aName;
79   }
80   else
81   {
82     myExistingNames.push_back(theName);
83     return theName;
84   }
85
86 }
87
88
89 bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
90 {
91   if ( theFilePath.isEmpty() )
92   { 
93     return false;
94   }
95
96   QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
97
98   if (anExt == "sx")
99   {
100     QFile aFile (theFilePath);
101     
102     aFile.open(QIODevice::ReadOnly);
103
104     Parse(aFile);
105    
106     CollectExistingNames(theDocument);
107     SXToHydro(theDocument, theEntities);
108     
109     aFile.close();
110     
111     return true;
112   }
113   else
114     return false;
115
116 }
117
118 void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
119
120   for ( size_t i = 0; i < myCurveBlocks.size(); i++ )
121   {
122     if (myCurveBlocks[i].myType == 4) ///  scatter plot -> to bathy
123     {
124       Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
125       HYDROData_Bathymetry::AltitudePoints aAPoints;
126       size_t n = myCurveBlocks[i].myXYZPoints.size();
127       aAPoints.reserve( n );
128       for (size_t j = 0; j < n; j++)
129       {
130         gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
131         theDocument->Transform(aLocalPoint, true);
132         HYDROData_Bathymetry::AltitudePoint p;
133         p.X = aLocalPoint.X();
134         p.Y = aLocalPoint.Y();
135         p.Z = aLocalPoint.Z();
136         aAPoints.push_back(p);
137       }
138
139       aBath->SetAltitudePoints(aAPoints);
140       aBath->SetName(GetName(myCurveBlocks[i].myName));
141       theEntities.Append(aBath);
142     }
143     if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3) // XYZ curve or isocontour
144     {
145       NCollection_Sequence<gp_XYZ> aPoints;
146       for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
147       {
148         gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
149         theDocument->Transform(aLocalPoint, true);
150         aPoints.Append(aLocalPoint);
151       }
152       /*if (myCurveBlocks[i].myIsClosed)
153         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));*/
154       Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( theDocument->CreateObject( KIND_PROFILEUZ ) );
155       Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
156       aPolyXY->AddSection( "",  
157         myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE, 
158         myCurveBlocks[i].myIsClosed ? true : false); 
159       aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
160       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
161       aProfile->SetParametricPoints(aProfileUZ->GetPoints());
162       aPolyXY->SetName(GetName(myCurveBlocks[i].myName));
163       aProfileUZ->SetName(GetName(myCurveBlocks[i].myName));
164       aProfile->SetName(GetName(myCurveBlocks[i].myName));
165       aPolyXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
166       theEntities.Append(aPolyXY);
167       theEntities.Append(aProfileUZ);
168       theEntities.Append(aProfile);
169     }
170     if (myCurveBlocks[i].myType == 2) // XYZ profile
171     {
172       if (myCurveBlocks[i].myCurvePlane == 2) // plane XoZ
173       {
174         if (myCurveBlocks[i].myAdditionalCurveInfo.size() == 4)
175         {
176           Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
177           HYDROData_ProfileUZ::PointsList aPointList;
178           for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
179             aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
180           aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
181           aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
182           aProfile->SetParametricPoints(aPointList);
183           if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 &&  myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 && 
184             myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
185           { // georeferenced profile
186             double xl = myCurveBlocks[i].myAdditionalCurveInfo[0];
187             double yl = myCurveBlocks[i].myAdditionalCurveInfo[1];
188             double xr = myCurveBlocks[i].myAdditionalCurveInfo[2];
189             double yr = myCurveBlocks[i].myAdditionalCurveInfo[3];
190             theDocument->Transform(xl, yl , true);
191             theDocument->Transform(xr, yr , true);
192             aProfile->SetLeftPoint(gp_XY(xl, yl));
193             aProfile->SetRightPoint(gp_XY(xr, yr));
194             aProfile->Update();
195           }
196           aProfile->SetName(GetName(myCurveBlocks[i].myName));
197           theEntities.Append(aProfile);
198         }
199       }
200       if (myCurveBlocks[i].myCurvePlane == 0) // plane XoY
201       {
202         Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
203         HYDROData_Profile::ProfilePoints aPointList;
204         for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
205         {
206           gp_XYZ aLocalPoint = gp_XYZ (myCurveBlocks[i].myXYZPoints[j]);
207           theDocument->Transform(aLocalPoint, true);
208           aPointList.Append(aLocalPoint);
209         }
210         aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
211         aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
212         aProfile->SetProfilePoints(aPointList);
213         aProfile->SetName(GetName(myCurveBlocks[i].myName));
214         theEntities.Append(aProfile);
215       }
216     }
217   }
218
219 }
220  
221
222 bool HYDROData_SinusX::Parse(QFile& theFile)
223 {
224   if ( !theFile.isOpen() )
225     return false;
226
227   QString aLine;
228   QString aBLine;
229   QStringList aList;
230   QStringList aBList;
231   myCurveBlocks.clear();
232   bool aTotStat = true;
233
234   aLine = theFile.readLine().simplified();
235   aList = aLine.split( ' ', QString::SkipEmptyParts );
236
237   for (;!theFile.atEnd();) 
238   {
239     if (aList[0] == "B" && (aList[1] == "C" || aList[1] == "P" || aList[1] == "N" || aList[1] == "S" ))
240     {  
241       HYDROGUI_CurveBlock aCurveBlockInfo;
242       if (aList[1] == "C")
243         aCurveBlockInfo.myType = 1; // XYZ curve
244       else if (aList[1] == "P")
245         aCurveBlockInfo.myType = 2; // XYZ profile
246       else if (aList[1] == "N")
247         aCurveBlockInfo.myType = 3; // isocontour
248       else if (aList[1] == "S")
249         aCurveBlockInfo.myType = 4; // Scatter plot
250
251       if (aList.size() == 9)
252       {
253         for (int j = 2; j < 8; j++)
254           aCurveBlockInfo.myRefCoords.push_back(aList[j].toDouble());
255         aCurveBlockInfo.myRefRatio = aList[8].toDouble();
256       }
257
258       QString Name;
259       do
260       {
261         aBLine = theFile.readLine().simplified();
262         aBList = aBLine.split( ' ', QString::SkipEmptyParts );
263          
264         if (aBList[0] == "CP")
265         {
266           if (aBList.size() == 2 && (aBList[1] == "0" || aBList[1] == "1" || aBList[1] == "2"))
267             aCurveBlockInfo.myCurvePlane = aBList[1].toInt();
268           else if (aBList.size() == 3 && (aBList[1] == "0" || aBList[1] == "1") && (aBList[2] == "0" || aBList[2] == "1"))
269           {
270             aCurveBlockInfo.myIsClosed = aBList[1].toInt();
271             aCurveBlockInfo.myIsSpline = aBList[2].toInt();
272           }
273           else
274           {
275             for (int j = 1; j < aBList.size(); j++)
276               aCurveBlockInfo.myAdditionalCurveInfo.push_back(aBList[j].toDouble());
277           }
278         }
279         if (aBList[0] == "CN")
280         {
281            for (int i = 1; i < aBList.size(); i++)
282              Name += aBList[i] + "_";
283            if (Name.size() <= 1)
284              Name = "noname_";
285            Name.remove(Name.size() - 1, 1);
286            aCurveBlockInfo.myName = Name;
287         }
288       } while (!theFile.atEnd() && aBLine[0] == 'C' );
289
290       bool aStat;
291       aTotStat = true;
292       do
293       {
294         if (aBList.size() >= 3 && aBLine[0] != 'B' && aBLine[0] != 'C') {
295           gp_XYZ anXYZ;
296           anXYZ.SetX (aBList[0].toDouble(&aStat));  
297           aTotStat = aTotStat && aStat;
298           anXYZ.SetY (aBList[1].toDouble(&aStat));
299           aTotStat = aTotStat && aStat;
300           anXYZ.SetZ (aBList[2].toDouble(&aStat));
301           aTotStat = aTotStat && aStat;
302
303           aCurveBlockInfo.myXYZPoints.push_back(anXYZ);
304           
305           aBLine = theFile.readLine().simplified();
306           aBList = aBLine.split( ' ', QString::SkipEmptyParts );
307         }
308         else 
309           break;
310     
311       } while (!theFile.atEnd() || !aBLine.isEmpty());
312       if (aTotStat)
313         myCurveBlocks.push_back(aCurveBlockInfo);
314
315       aLine = aBLine;
316       aList = aBList;
317
318     }
319     else
320     {
321       aLine = theFile.readLine().simplified();
322       aList = aLine.split( ' ', QString::SkipEmptyParts );
323     }
324
325   }
326
327   return true;
328
329 }
330
331 bool HYDROData_SinusX::Export(const QString& theFilePath, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
332 {
333   if ( theFilePath.isEmpty() )
334   { 
335     return false;
336   }
337
338   QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
339
340   if (anExt == "sx")
341   {
342     QFile aFile (theFilePath);
343     
344     aFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
345
346     HydroToSX(aFile, theEntities);
347     
348     aFile.close();
349     
350     return true;
351   }
352   else
353     return false;
354
355 }
356
357 void HYDROData_SinusX::HydroToSX(QFile& theFile, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
358 {
359   QTextStream aTextStream(&theFile);
360   aTextStream << "C  Generated by HYDRO Module\n";
361   aTextStream << "C\n";
362
363   for (int i = 1; i <= theEntities.Size(); i++)
364   {
365     Handle_HYDROData_Entity anEnt = theEntities.Value(i);
366     if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Bathymetry) ))
367     {
368       Handle(HYDROData_Bathymetry) aBathy = Handle(HYDROData_Bathymetry)::DownCast( anEnt );
369       HYDROData_Bathymetry::AltitudePoints anXYZPoints = aBathy->GetAltitudePoints(true);
370       //Write to stream
371       aTextStream << "B S\n";
372       aTextStream << "CN " << aBathy->GetName() << "\n";
373       aTextStream << "CP 0 0\n";
374       aTextStream << "CP 0\n";
375       for (size_t j = 0, m = anXYZPoints.size(); j < m; j++)
376         aTextStream << " " << QString::number(anXYZPoints[j].X, 'f', 3)  
377                     << " " << QString::number(anXYZPoints[j].Y, 'f', 3)  
378                     << " " << QString::number(anXYZPoints[j].Z, 'f', 3) << "\n"; 
379     }
380     else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ))
381     {
382       Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( anEnt );
383       for (int j = 0; j < aPolyXY->NbSections(); j++)
384       { 
385         //Collect data
386         bool IsClosed = aPolyXY->IsClosedSection(j);
387         bool IsSpline = false;
388         if (aPolyXY->GetSectionType(j) == HYDROData_PolylineXY::SECTION_SPLINE)
389           IsSpline = true;
390         HYDROData_PolylineXY::PointsList anXYPoints = aPolyXY->GetPoints(j, true);
391         //Write to stream
392         aTextStream << "B N\n";
393         aTextStream << "CN " << aPolyXY->GetName() << "\n";
394         aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
395         aTextStream << "CP 0.0\n";
396         aTextStream << "CP 0\n";
397         if (aPolyXY->NbSections() > 1)
398           aTextStream << "C " << aPolyXY->GetName() << "_section_" << QString::number(j) << "\n";
399         for (int k = anXYPoints.Lower(); k <= anXYPoints.Upper(); k++)
400          aTextStream << " " << QString::number(anXYPoints(k).X(), 'f', 3)  
401                      << " " << QString::number(anXYPoints(k).Y(), 'f', 3)  
402                      << " 0.000\n"; 
403       }
404     }
405     else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
406     {
407       //Collect data
408       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anEnt );
409       HYDROData_ProfileUZ::PointsList aPointList = aProfile->GetParametricPoints();
410       bool IsClosed = aProfile->GetProfileUZ(false)->IsClosedSection(0);;
411       bool IsSpline = false;
412       if (aProfile->GetProfileUZ(false)->GetSectionType(0) == HYDROData_PolylineXY::SECTION_SPLINE)
413         IsSpline = true;
414       //Write to stream
415       aTextStream << "B P\n";
416       aTextStream << "CN " << aProfile->GetName() << "\n";
417       aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
418       gp_XY aLeftPoint(0.0, 0.0);
419       gp_XY aRightPoint(0.0, 0.0);
420       if (aProfile->GetLeftPoint(aLeftPoint, true) && aProfile->GetRightPoint(aRightPoint, true))
421         aTextStream << "CP " << QString::number(aLeftPoint.X(), 'f', 3) << " " <<
422                                 QString::number(aLeftPoint.Y(), 'f', 3) << " " <<
423                                 QString::number(aRightPoint.X(), 'f', 3) << " " <<
424                                 QString::number(aRightPoint.Y(), 'f', 3) << "\n";
425       else
426         aTextStream << "CP 0.0 0.0 0.0 0.0\n";
427       aTextStream << "CP 2\n";
428       for (int k = aPointList.Lower(); k <= aPointList.Upper(); k++)
429          aTextStream << " " << QString::number(aPointList(k).X(), 'f', 3)  
430                      << " 0.000 "
431                      << QString::number(aPointList(k).Y(), 'f', 3) << "\n";  
432     }
433   }
434 }