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