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