Salome HOME
Bug PAL8221 - POST-PRO: Pal/Salome crashes if edit presentation in opened study
[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 #else
42 static int MYDEBUG = 0;
43 #endif
44
45 int VISU::CutLines_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
46                                  const char* theFieldName, int theIteration, int isMemoryCheck)
47 {
48   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,isMemoryCheck);
49 }
50
51 int VISU::CutLines_i::myNbPresent = 0;
52 QString VISU::CutLines_i::GenerateName() { return VISU::GenerateName("CutLines",myNbPresent++);}
53
54 const string VISU::CutLines_i::myComment = "CUTLINES";
55 const char* VISU::CutLines_i::GetComment() const { return myComment.c_str();}
56
57
58 VISU::CutLines_i::
59 CutLines_i(Result_i* theResult, 
60            bool theAddToStudy) :
61   PrsObject_i(theResult->GetStudyDocument()), 
62   Prs3d_i(theResult,theAddToStudy),
63   ScalarMap_i(theResult,theAddToStudy),
64   myCutLinesPL(NULL)
65 {
66 }
67
68
69 VISU::CutLines_i::
70 CutLines_i(Result_i* theResult, 
71            SALOMEDS::SObject_ptr theSObject) :
72   PrsObject_i(theResult->GetStudyDocument()), 
73   Prs3d_i(theResult,theSObject),
74   ScalarMap_i(theResult,theSObject),
75   myCutLinesPL(NULL)
76 {
77 }
78
79
80 void VISU::CutLines_i::SameAs(const CutLines_i* theOrigin){
81   VISU::ScalarMap_i::SameAs(theOrigin);
82 }
83
84
85 VISU::Storable* VISU::CutLines_i::Create(const char* theMeshName, VISU::Entity theEntity, 
86                                          const char* theFieldName, int theIteration)
87 {
88   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
89 }
90
91
92 VISU::Storable* VISU::CutLines_i::Restore(const Storable::TRestoringMap& theMap)
93 {
94   DoHook();
95   SetNbLines(VISU::Storable::FindValue(theMap,"myNbLines").toInt());
96   SetDisplacement(VISU::Storable::FindValue(theMap,"myDisplacement[0]").toDouble());
97   SetDisplacement2(VISU::Storable::FindValue(theMap,"myDisplacement[1]").toDouble());
98   SetOrientation(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[0]").toInt()),
99                  Storable::FindValue(theMap,"aRot[0][0]").toDouble(),
100                  Storable::FindValue(theMap,"aRot[0][1]").toDouble());
101   SetOrientation2(CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[1]").toInt()),
102                   Storable::FindValue(theMap,"aRot[1][0]").toDouble(),
103                   Storable::FindValue(theMap,"aRot[1][1]").toDouble());
104   if (VISU::Storable::FindValue(theMap,"myBasePlaneCondition").toInt())
105     SetDefault();
106   else
107     SetBasePlanePosition(VISU::Storable::FindValue(theMap,"myBasePlanePosition").toDouble());
108   
109   QStringList aPosList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLinePosition") );
110   QStringList aCondList = QStringList::split("|",VISU::Storable::FindValue(theMap,"myLineCondition") );
111   for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++)
112     if(aCondList[i].toInt() == 0)
113       SetLinePosition(i,aPosList[i].toDouble());
114
115   return ScalarMap_i::Restore(theMap);
116 }
117
118
119 void VISU::CutLines_i::ToStream(std::ostringstream& theStr){
120   ScalarMap_i::ToStream(theStr);
121
122   int aNbLines = GetNbLines();
123   
124   Storable::DataToStream( theStr, "myNbLines",         aNbLines );
125   Storable::DataToStream( theStr, "myDisplacement[0]", GetDisplacement() );
126   Storable::DataToStream( theStr, "myDisplacement[1]", GetDisplacement2() );
127   Storable::DataToStream( theStr, "myBasePlane[0]",    int(GetOrientationType()) );
128   Storable::DataToStream( theStr, "myBasePlane[1]",    int(GetOrientationType2()) );
129   Storable::DataToStream( theStr, "aRot[0][0]",        GetRotateX() );
130   Storable::DataToStream( theStr, "aRot[0][1]",        GetRotateY() );
131   Storable::DataToStream( theStr, "aRot[1][0]",        GetRotateX2() );
132   Storable::DataToStream( theStr, "aRot[1][1]",        GetRotateY2() );
133   Storable::DataToStream( theStr, "myBasePlanePosition", GetBasePlanePosition() );
134   Storable::DataToStream( theStr, "myBasePlaneCondition", IsDefault() );
135
136   QString aStrPos, aStrCon;
137   for(int i = 0, iEnd = GetNbLines(); i < iEnd; i++){
138     aStrPos.append(QString::number(GetLinePosition(i)) + "|");
139     aStrCon.append(QString::number(IsDefaultPosition(i)) + "|");
140   }
141   Storable::DataToStream( theStr, "myLinePosition",  aStrPos.latin1());
142   Storable::DataToStream( theStr, "myLineCondition", aStrCon.latin1());
143 }
144
145 VISU::CutLines_i::~CutLines_i(){
146   if(MYDEBUG) MESSAGE("CutLines_i::~CutLines_i()");
147 }
148
149
150 void VISU::CutLines_i::SetOrientation(VISU::CutPlanes::Orientation theOrient,
151                                       CORBA::Double theXAngle, CORBA::Double theYAngle)
152 {
153   myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
154                                theXAngle,theYAngle);
155 }
156 void VISU::CutLines_i::SetOrientation2(VISU::CutPlanes::Orientation theOrient,
157                                        CORBA::Double theXAngle, CORBA::Double theYAngle)
158 {
159   myCutLinesPL->SetOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),
160                                theXAngle,theYAngle,1);
161 }
162
163 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType() { 
164   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation());
165 }
166 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType2() { 
167   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation(1));
168 }
169
170 CORBA::Double VISU::CutLines_i::GetRotateX(){
171   return myCutLinesPL->GetRotateX();
172 }
173 CORBA::Double VISU::CutLines_i::GetRotateY(){
174   return myCutLinesPL->GetRotateY();
175 }
176
177 CORBA::Double VISU::CutLines_i::GetRotateX2(){
178   return myCutLinesPL->GetRotateX(1);
179 }
180 CORBA::Double VISU::CutLines_i::GetRotateY2(){
181   return myCutLinesPL->GetRotateY(1);
182 }
183
184
185 void VISU::CutLines_i::SetDisplacement(CORBA::Double theDisp) { 
186   myCutLinesPL->SetDisplacement(theDisp);
187 }
188 void VISU::CutLines_i::SetDisplacement2(CORBA::Double theDisp) { 
189   myCutLinesPL->SetDisplacement(theDisp,1);
190 }
191
192 CORBA::Double VISU::CutLines_i::GetDisplacement() { 
193   return myCutLinesPL->GetDisplacement();
194 }
195 CORBA::Double VISU::CutLines_i::GetDisplacement2() { 
196   return myCutLinesPL->GetDisplacement(1);
197 }
198
199
200 void VISU::CutLines_i::SetBasePlanePosition(CORBA::Double thePlanePosition){
201   myCutLinesPL->SetPosition(thePlanePosition);
202 }
203
204 CORBA::Double VISU::CutLines_i::GetBasePlanePosition(){ 
205   return myCutLinesPL->GetPosition();
206 }
207
208 void VISU::CutLines_i::SetLinePosition(CORBA::Long thePlaneNumber, CORBA::Double thePlanePosition){
209   myCutLinesPL->SetPartPosition(thePlaneNumber,thePlanePosition);
210 }
211
212 CORBA::Double VISU::CutLines_i::GetLinePosition(CORBA::Long thePlaneNumber){ 
213   return myCutLinesPL->GetPartPosition(thePlaneNumber,1);
214 }
215
216
217 void VISU::CutLines_i::SetDefault(){
218   myCutLinesPL->SetDefault();
219 }
220
221 CORBA::Boolean VISU::CutLines_i::IsDefault(){ 
222   return myCutLinesPL->IsDefault();
223 }
224
225 void VISU::CutLines_i::SetDefaultPosition(CORBA::Long thePlaneNumber){
226   myCutLinesPL->SetPartDefault(thePlaneNumber);
227 }
228
229 CORBA::Boolean VISU::CutLines_i::IsDefaultPosition(CORBA::Long thePlaneNumber){ 
230   return myCutLinesPL->IsPartDefault(thePlaneNumber);
231 }
232
233
234 void VISU::CutLines_i::SetNbLines(CORBA::Long theNb) { 
235   myCutLinesPL->SetNbParts(theNb);
236 }
237 CORBA::Long VISU::CutLines_i::GetNbLines() { 
238   return myCutLinesPL->GetNbParts();
239 }
240
241
242 void VISU::CutLines_i::DoHook(){
243   if(!myPipeLine) myPipeLine = VISU_CutLinesPL::New();
244   myCutLinesPL = dynamic_cast<VISU_CutLinesPL*>(myPipeLine);
245
246   ScalarMap_i::DoHook();
247 }
248
249 void VISU::CutLines_i::BuildTableOfReal(SALOMEDS::SObject_ptr theSObject){
250   try{
251     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal");
252     Update();
253     SALOMEDS::GenericAttribute_var anAttr;
254     SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
255     anAttr = aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributeTableOfReal");
256     SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
257     
258     typedef set<float> TXCont;
259     typedef map<float,float> TXYMap;
260     typedef map<int,TXYMap> TXYMapCont;
261     typedef map<long,long> TLineIdCont;
262     
263     QString aTitle;
264     aTitle.sprintf("%s",myTitle.c_str());
265     aTitle = aTitle.simplifyWhiteSpace();
266     aTableOfReal->SetTitle(aTitle.latin1());
267     
268     int iLineEnd = myCutLinesPL->GetAppendPolyData()->GetNumberOfInputs();
269     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLineEnd = "<<iLineEnd);
270     TXCont aXCont;
271     TXYMapCont aXYMapCont; 
272     TLineIdCont aLineIdCont;  // Define internal numeration of lines
273     const float *aDirLn = myCutLinesPL->GetDirLn();
274     const float *aBasePnt = myCutLinesPL->GetBasePnt();
275     const float *aBoundPrjLn = myCutLinesPL->GetBoundPrjLn();
276     for(int iLine = 0, jLine = 0; iLine < iLineEnd; iLine++){
277       vtkDataSet *aDataSet = myCutLinesPL->GetAppendPolyData()->GetInput(iLine);
278       aDataSet->Update();
279       int aNbPoints = aDataSet->GetNumberOfPoints();
280       if(!aNbPoints) continue;
281       vtkPointData *aPointData = aDataSet->GetPointData();
282       vtkDataArray *aScalars = aPointData->GetScalars();
283       vtkCellDataToPointData *aFilter = NULL;
284       if(!aScalars) {
285         aFilter = vtkCellDataToPointData::New();
286         aFilter->SetInput(aDataSet);
287         aFilter->PassCellDataOn();
288         aDataSet = aFilter->GetOutput();
289         aDataSet->Update();
290       }
291       aPointData = aDataSet->GetPointData();
292       aScalars = aPointData->GetScalars();
293       if(!aScalars) continue;
294       if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLine = "<<iLine<<"; aNbPoints = "<<aNbPoints);
295       aLineIdCont[iLine] = jLine++;
296       TXYMap& aXYMap = aXYMapCont[iLine];
297       float aPnt[3], aVect[3], aDist;
298       for(int i = 0; i < aNbPoints; i++){
299         aDataSet->GetPoint(i,aPnt);
300         Sub(aPnt,aBasePnt,aVect);
301         aDist = vtkMath::Dot(aVect,aDirLn) / aBoundPrjLn[2];
302         // the workaround
303         if(aDist < 0.0) aDist = 0.0; 
304         if(aDist > 1.0) aDist = 1.0;
305         aXYMap[aDist] = aScalars->GetTuple1(i);
306       }
307       if(aFilter){
308         aFilter->UnRegisterAllOutputs();
309         aFilter->Delete();
310       }
311     }
312     if(aXYMapCont.size() == 0)
313       throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXYMapCont.size() == 0 !!!");
314     //Resorting of theXYMap
315     TXYMapCont::iterator aXYMapContIter = aXYMapCont.begin();
316     for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
317       TXYMap& aXYMap = aXYMapContIter->second, aNewXYMap;
318       if(aXYMap.size() > 2){  
319         // Try to smooth the values of the line by applying linear approximation
320         TXYMap::const_iterator aXYMapIter[2] = {aXYMap.begin(), ++aXYMap.begin()};
321         aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
322         aXCont.insert(aXYMapIter[0]->first);
323         for(; aXYMapIter[1] != aXYMap.end(); aXYMapIter[0]++, aXYMapIter[1]++){
324           float aY[3] = {aXYMapIter[0]->second, aXYMapIter[1]->second, 0.0};
325           aY[2] = (aY[0] + aY[1])/2.0;
326           float aX[3] = {aXYMapIter[0]->first, aXYMapIter[1]->first, 0.0};
327           aX[2] = (aX[0] + aX[1])/2.0;
328           aNewXYMap[aX[2]] = aY[2];
329           aXCont.insert(aX[2]);
330         }
331         aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
332         aXCont.insert(aXYMapIter[0]->first);
333         aXYMap = aNewXYMap;
334       }else{
335         TXYMap::const_iterator aXYMapIter = aXYMap.begin();
336         for(; aXYMapIter != aXYMap.end(); aXYMapIter++)
337           aXCont.insert(aXYMapIter->first);
338       }
339     }
340     if(aXCont.size() == 0)
341       throw std::runtime_error("CutPlanes_i::BuildTableOfReal aXCont.size() == 0 !!!");
342     QString aString;
343     int iEnd = aXCont.size();
344     aTableOfReal->SetNbColumns(iEnd);
345     TXCont::const_iterator aXContIter = aXCont.begin();
346     for(long i = 0; aXContIter != aXCont.end(); aXContIter++, i++){
347       float aDist = *aXContIter; 
348       aTableOfReal->PutValue(aDist,1,i+1);
349       aString.sprintf("%d",i);
350       aTableOfReal->SetColumnTitle(i+1,aString.latin1());
351       if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal aDist = "<<aDist);
352       TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
353       for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
354         long iLine = aXYMapContIter->first;
355         long iLineId = aLineIdCont[iLine];
356         const TXYMap& aXYMap = aXYMapCont[iLine];
357         TXYMap::const_iterator aXYMapIter = aXYMap.find(aDist);
358         // Can we find some value that belong to the line and have the same X coordinate?
359         if(aXYMapIter == aXYMap.end()) continue;
360         float aVal = aXYMapIter->second;
361         aTableOfReal->PutValue(aVal,iLineId+2,i+1);
362       }
363     }
364     {
365       aTableOfReal->SetRowTitle(1,"X");
366       aTableOfReal->SetRowUnit(1,"-");
367       QString aUnitName = myField->myUnitNames[0].c_str();
368       int aScalarMode = myCutLinesPL->GetScalarMode();
369       if(aScalarMode != 0) 
370         aUnitName = myField->myUnitNames[aScalarMode-1].c_str();
371       aUnitName = aUnitName.simplifyWhiteSpace();
372       if(aUnitName.isEmpty()) aUnitName = "-";
373       TXYMapCont::const_iterator aXYMapContIter = aXYMapCont.begin();
374       for(; aXYMapContIter != aXYMapCont.end(); aXYMapContIter++){
375         long iLine = aXYMapContIter->first;
376         long iLineId = aLineIdCont[iLine];
377         aString.sprintf("Y%d",iLine);
378         if(MYDEBUG) 
379           MESSAGE("CutPlanes_i::BuildTableOfReal - SetRowTitle("<<iLineId+2<<",'"<<aString<<"')");
380         aTableOfReal->SetRowTitle(iLineId+2,aString.latin1());
381         aTableOfReal->SetRowUnit(iLineId+2,aUnitName.latin1());
382       }
383     }
384   }catch(std::exception& exc){
385     INFOS("Follow exception was occured :\n"<<exc.what());
386   }catch (...){
387     INFOS("Unknown exception was occured !!!");
388   }
389 }