Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/visu.git] / src / VISU_I / VISU_CutLines_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISU_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_CutLinesPL.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_CutLines_i.hh"
30 #include "VISU_Actor.h"
31
32 #include "VISU_PipeLineUtils.hxx"
33
34 #include <vtkAppendPolyData.h>
35
36 using namespace VISU;
37 using namespace std;
38
39 #ifdef _DEBUG_
40 static int MYDEBUG = 0;
41 static int MYDEBUGWITHFILES = 0;
42 #else
43 static int MYDEBUG = 0;
44 static int MYDEBUGWITHFILES = 0;
45 #endif
46
47 int VISU::CutLines_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
48                                  const char* theFieldName, int theIteration, int isMemoryCheck)
49 {
50   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,isMemoryCheck);
51 }
52
53 int VISU::CutLines_i::myNbPresent = 0;
54 QString VISU::CutLines_i::GenerateName() { return VISU::GenerateName("CutLines",myNbPresent++);}
55
56 const string VISU::CutLines_i::myComment = "CUTLINES";
57 const char* VISU::CutLines_i::GetComment() const { return myComment.c_str();}
58
59
60 VISU::CutLines_i::CutLines_i(Result_i* theResult, bool theAddToStudy, SALOMEDS::SObject_ptr theSObject) :
61        PrsObject_i(theResult->GetStudyDocument()), 
62        Prs3d_i(theResult,theSObject),
63        ScalarMap_i(theResult,theAddToStudy,theSObject)
64 {
65   myCutLinesPL = NULL;
66 }
67
68
69
70 void VISU::CutLines_i::SameAs(const CutLines_i* theOrigin){
71   CutLines_i* aCutLines = const_cast<CutLines_i*>(theOrigin);
72   VISU::ScalarMap_i::SameAs(theOrigin);
73 }
74
75
76 VISU::Storable* VISU::CutLines_i::Create(const char* theMeshName, VISU::Entity theEntity, 
77                                          const char* theFieldName, int theIteration)
78 {
79   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
80 }
81
82
83 VISU::Storable* VISU::CutLines_i::Restore(const Storable::TRestoringMap& theMap)
84 {
85   DoHook();
86   SetNbLines(VISU::Storable::FindValue(theMap,"myNbLines").toInt());
87   SetDisplacement(VISU::Storable::FindValue(theMap,"myDisplacement[0]").toDouble());
88   SetDisplacement2(VISU::Storable::FindValue(theMap,"myDisplacement[1]").toDouble());
89   SetOrientation(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[0]").toInt()),
90                  Storable::FindValue(theMap,"aRot[0][0]").toDouble(),
91                  Storable::FindValue(theMap,"aRot[0][1]").toDouble());
92   SetOrientation2(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[1]").toInt()),
93                   Storable::FindValue(theMap,"aRot[1][0]").toDouble(),
94                   Storable::FindValue(theMap,"aRot[1][1]").toDouble());
95   if (VISU::Storable::FindValue(theMap,"myBasePlaneCondition").toInt())
96     SetDefault();
97   else
98     SetBasePlanePosition(VISU::Storable::FindValue(theMap,"myBasePlanePosition").toDouble());
99   
100   QStringList aPosList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLinePosition") );
101   QStringList aCondList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLineCondition") );
102   for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++)
103     if(aCondList[i].toInt() == 0)
104       SetLinePosition(i,aPosList[i].toDouble());
105
106   return ScalarMap_i::Restore(theMap);
107 }
108
109
110 void VISU::CutLines_i::ToStream(std::ostringstream& theStr){
111   ScalarMap_i::ToStream(theStr);
112
113   int aNbLines = GetNbLines();
114   
115   Storable::DataToStream( theStr, "myNbLines",         aNbLines );
116   Storable::DataToStream( theStr, "myDisplacement[0]", GetDisplacement() );
117   Storable::DataToStream( theStr, "myDisplacement[1]", GetDisplacement2() );
118   Storable::DataToStream( theStr, "myBasePlane[0]",    int(GetOrientationType()) );
119   Storable::DataToStream( theStr, "myBasePlane[1]",    int(GetOrientationType2()) );
120   Storable::DataToStream( theStr, "aRot[0][0]",        GetRotateX() );
121   Storable::DataToStream( theStr, "aRot[0][1]",        GetRotateY() );
122   Storable::DataToStream( theStr, "aRot[1][0]",        GetRotateX2() );
123   Storable::DataToStream( theStr, "aRot[1][1]",        GetRotateY2() );
124   Storable::DataToStream( theStr, "myBasePlanePosition", GetBasePlanePosition() );
125   Storable::DataToStream( theStr, "myBasePlaneCondition", IsDefault() );
126
127   QString aStrPos, aStrCon;
128   for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++){
129     aStrPos.append(QString::number(GetLinePosition(i)) + "|");
130     aStrCon.append(QString::number(IsDefaultPosition(i)) + "|");
131   }
132   Storable::DataToStream( theStr, "myLinePosition",  aStrPos.latin1());
133   Storable::DataToStream( theStr, "myLineCondition", aStrCon.latin1());
134 }
135
136 VISU::CutLines_i::~CutLines_i(){
137   if(MYDEBUG) MESSAGE("CutLines_i::~CutLines_i()");
138 }
139
140
141 void VISU::CutLines_i::SetOrientation(VISU::CutPlanes::Orientation theOrient,
142                                       CORBA::Double theXAngle, CORBA::Double theYAngle)
143 {
144   myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
145                                theXAngle,theYAngle);
146 }
147 void VISU::CutLines_i::SetOrientation2(VISU::CutPlanes::Orientation theOrient,
148                                        CORBA::Double theXAngle, CORBA::Double theYAngle)
149 {
150   myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
151                                theXAngle,theYAngle,1);
152 }
153
154 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType() { 
155   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation());
156 }
157 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType2() { 
158   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation(1));
159 }
160
161 CORBA::Double VISU::CutLines_i::GetRotateX(){
162   return myCutLinesPL->GetRotateX();
163 }
164 CORBA::Double VISU::CutLines_i::GetRotateY(){
165   return myCutLinesPL->GetRotateY();
166 }
167
168 CORBA::Double VISU::CutLines_i::GetRotateX2(){
169   return myCutLinesPL->GetRotateX(1);
170 }
171 CORBA::Double VISU::CutLines_i::GetRotateY2(){
172   return myCutLinesPL->GetRotateY(1);
173 }
174
175
176 void VISU::CutLines_i::SetDisplacement(CORBA::Double theDisp) { 
177   myCutLinesPL->SetDisplacement(theDisp);
178 }
179 void VISU::CutLines_i::SetDisplacement2(CORBA::Double theDisp) { 
180   myCutLinesPL->SetDisplacement(theDisp,1);
181 }
182
183 CORBA::Double VISU::CutLines_i::GetDisplacement() { 
184   return myCutLinesPL->GetDisplacement();
185 }
186 CORBA::Double VISU::CutLines_i::GetDisplacement2() { 
187   return myCutLinesPL->GetDisplacement(1);
188 }
189
190
191 void VISU::CutLines_i::SetBasePlanePosition(CORBA::Double thePlanePosition){
192   myCutLinesPL->SetPosition(thePlanePosition);
193 }
194
195 CORBA::Double VISU::CutLines_i::GetBasePlanePosition(){ 
196   return myCutLinesPL->GetPosition();
197 }
198
199 void VISU::CutLines_i::SetLinePosition(CORBA::Long thePlaneNumber, CORBA::Double thePlanePosition){
200   myCutLinesPL->SetPartPosition(thePlaneNumber,thePlanePosition);
201 }
202
203 CORBA::Double VISU::CutLines_i::GetLinePosition(CORBA::Long thePlaneNumber){ 
204   return myCutLinesPL->GetPartPosition(thePlaneNumber,1);
205 }
206
207
208 void VISU::CutLines_i::SetDefault(){
209   myCutLinesPL->SetDefault();
210 }
211
212 CORBA::Boolean VISU::CutLines_i::IsDefault(){ 
213   return myCutLinesPL->IsDefault();
214 }
215
216 void VISU::CutLines_i::SetDefaultPosition(CORBA::Long thePlaneNumber){
217   myCutLinesPL->SetPartDefault(thePlaneNumber);
218 }
219
220 CORBA::Boolean VISU::CutLines_i::IsDefaultPosition(CORBA::Long thePlaneNumber){ 
221   return myCutLinesPL->IsPartDefault(thePlaneNumber);
222 }
223
224
225 void VISU::CutLines_i::SetNbLines(CORBA::Long theNb) { 
226   myCutLinesPL->SetNbParts(theNb);
227 }
228 CORBA::Long VISU::CutLines_i::GetNbLines() { 
229   return myCutLinesPL->GetNbParts();
230 }
231
232
233 void VISU::CutLines_i::DoHook(){
234   if(!myPipeLine) myPipeLine = VISU_CutLinesPL::New();
235   myCutLinesPL = dynamic_cast<VISU_CutLinesPL*>(myPipeLine);
236
237   ScalarMap_i::DoHook();
238 }
239
240 void VISU::CutLines_i::BuildTableOfReal(SALOMEDS::SObject_ptr theSObject){
241   try{
242     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal");
243     Update();
244     SALOMEDS::GenericAttribute_var anAttr;
245     SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
246     anAttr = aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributeTableOfReal");
247     SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
248     
249     typedef set<float> TXCont;
250     typedef map<float,float> TXYMap;
251     typedef map<int,TXYMap> TXYMapCont;
252     typedef map<long,long> TLineIdCont;
253     
254     const VISU::TField::TValField& aValField = myField->myValField;
255     const VISU::TField::TValForTime& aValForTime = aValField.find((int)myIteration)->second;
256     const VISU::TField::TTime& aTime = aValForTime.myTime;
257     QString aTitle;
258     //aTitle.sprintf("%s %s",myTitle.c_str(),aTime.second.c_str());
259     aTitle.sprintf("%s",myTitle.c_str());
260     aTitle = aTitle.simplifyWhiteSpace();
261     aTableOfReal->SetTitle(aTitle.latin1());
262     
263     int iLineEnd = myCutLinesPL->GetAppendPolyData()->GetNumberOfInputs();
264     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLineEnd = "<<iLineEnd);
265     TXCont aXCont;
266     TXYMapCont aXYMapCont; 
267     TLineIdCont aLineIdCont;  // Define internal numeration of lines
268     const float *aDirLn = myCutLinesPL->GetDirLn();
269     const float *aBasePnt = myCutLinesPL->GetBasePnt();
270     const float *aBoundPrjLn = myCutLinesPL->GetBoundPrjLn();
271     for(int iLine = 0, jLine = 0; iLine < iLineEnd; iLine++){
272       vtkDataSet *aDataSet = myCutLinesPL->GetAppendPolyData()->GetInput(iLine);
273       aDataSet->Update();
274       int aNbPoints = aDataSet->GetNumberOfPoints();
275       if(!aNbPoints) continue;
276       vtkPointData *aPointData = aDataSet->GetPointData();
277       vtkDataArray *aScalars = aPointData->GetScalars();
278       vtkCellDataToPointData *aFilter = NULL;
279       if(!aScalars) {
280         aFilter = vtkCellDataToPointData::New();
281         aFilter->SetInput(aDataSet);
282         aFilter->PassCellDataOn();
283         aDataSet = aFilter->GetOutput();
284         aDataSet->Update();
285       }
286       aPointData = aDataSet->GetPointData();
287       aScalars = aPointData->GetScalars();
288       if(!aScalars) continue;
289       if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLine = "<<iLine<<"; aNbPoints = "<<aNbPoints);
290       aLineIdCont[iLine] = jLine++;
291       TXYMap& aXYMap = aXYMapCont[iLine];
292       float aPnt[3], aVect[3], aDist;
293       for(int i = 0; i < aNbPoints; i++){
294         aDataSet->GetPoint(i,aPnt);
295         Sub(aPnt,aBasePnt,aVect);
296         aDist = vtkMath::Dot(aVect,aDirLn) / aBoundPrjLn[2];
297         // the workaround
298         if(aDist < 0.0) aDist = 0.0; 
299         if(aDist > 1.0) aDist = 1.0;
300         aXYMap[aDist] = aScalars->GetTuple1(i);
301       }
302       if(aFilter){
303         aFilter->UnRegisterAllOutputs();
304         aFilter->Delete();
305       }
306     }
307     if(aXYMapCont.size() == 0)
308       throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXYMapCont.size() == 0 !!!");
309     //Resorting of theXYMap
310     TXYMapCont::iterator aXYMapContIter = aXYMapCont.begin();
311     for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
312       TXYMap& aXYMap = aXYMapContIter->second, aNewXYMap;
313       if(aXYMap.size() > 2){  
314         // Try to smooth the values of the line by applying linear approximation
315         TXYMap::const_iterator aXYMapIter[2] = {aXYMap.begin(), ++aXYMap.begin()};
316         aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
317         aXCont.insert(aXYMapIter[0]->first);
318         for(; aXYMapIter[1] != aXYMap.end(); aXYMapIter[0]++, aXYMapIter[1]++){
319           float aY[3] = {aXYMapIter[0]->second, aXYMapIter[1]->second, 0.0};
320           aY[2] = (aY[0] + aY[1])/2.0;
321           float aX[3] = {aXYMapIter[0]->first, aXYMapIter[1]->first, 0.0};
322           aX[2] = (aX[0] + aX[1])/2.0;
323           aNewXYMap[aX[2]] = aY[2];
324           aXCont.insert(aX[2]);
325         }
326         aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
327         aXCont.insert(aXYMapIter[0]->first);
328         aXYMap = aNewXYMap;
329       }else{
330         TXYMap::const_iterator aXYMapIter = aXYMap.begin();
331         for(; aXYMapIter != aXYMap.end(); aXYMapIter++)
332           aXCont.insert(aXYMapIter->first);
333       }
334     }
335     if(aXCont.size() == 0)
336       throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXCont.size() == 0 !!!");
337     QString aString;
338     int iEnd = aXCont.size();
339     aTableOfReal->SetNbColumns(iEnd);
340     TXCont::const_iterator aXContIter = aXCont.begin();
341     for(long i = 0; aXContIter != aXCont.end(); aXContIter++, i++){
342       float aDist = *aXContIter; 
343       aTableOfReal->PutValue(aDist,1,i+1);
344       aString.sprintf("%d",i);
345       aTableOfReal->SetColumnTitle(i+1,aString.latin1());
346       if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal aDist = "<<aDist);
347       TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
348       for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
349         long iLine = aXYMapContIter->first;
350         long iLineId = aLineIdCont[iLine];
351         const TXYMap& aXYMap = aXYMapCont[iLine];
352         TXYMap::const_iterator aXYMapIter = aXYMap.find(aDist);
353         // Can we find some value that belong to the line and have the same X coordinate?
354         if(aXYMapIter == aXYMap.end()) continue;
355         float aVal = aXYMapIter->second;
356         aTableOfReal->PutValue(aVal,iLineId+2,i+1);
357       }
358     }
359     {
360       aTableOfReal->SetRowTitle(1,"X");
361       aTableOfReal->SetRowUnit(1,"-");
362       QString aUnitName = myField->myUnitNames[0].c_str();
363       int aScalarMode = myCutLinesPL->GetScalarMode();
364       if(aScalarMode != 0) 
365         aUnitName = myField->myUnitNames[aScalarMode-1].c_str();
366       aUnitName = aUnitName.simplifyWhiteSpace();
367       if(aUnitName.isEmpty()) aUnitName = "-";
368       TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
369       for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
370         long iLine = aXYMapContIter->first;
371         long iLineId = aLineIdCont[iLine];
372         aString.sprintf("Y%d",iLine);
373         if(MYDEBUG) 
374           MESSAGE("CutPlanes_i::BuildTableOfReal - SetRowTitle("<<iLineId+2<<",'"<<aString<<"')");
375         aTableOfReal->SetRowTitle(iLineId+2,aString.latin1());
376         aTableOfReal->SetRowUnit(iLineId+2,aUnitName.latin1());
377       }
378     }
379   }catch(std::exception& exc){
380     INFOS("Follow exception was occured :\n"<<exc.what());
381   }catch (...){
382     INFOS("Unknown exception was occured !!!");
383   }
384 }