Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[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)
49 {
50   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
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 VISU::CutLines_i::CutLines_i(Result_i* theResult, bool theAddToStudy) :
60        PrsObject_i(theResult->GetStudyDocument()), 
61        Prs3d_i(theResult),       
62        ScalarMap_i(theResult, theAddToStudy)
63 {
64   myCutLinesPL = NULL;
65 }
66
67 void VISU::CutLines_i::Destroy(){
68   ScalarMap_i::Destroy();
69 }
70
71 void VISU::CutLines_i::SameAs(const CutLines_i* theOrigin){
72   CutLines_i* aCutLines = const_cast<CutLines_i*>(theOrigin);
73   VISU::ScalarMap_i::SameAs(theOrigin);
74
75   //SetNbLines(aCutLines->GetNbLines());
76   //SetDisplacement(aCutLines->GetDisplacement());
77   //SetDisplacement2(aCutLines->GetDisplacement2());
78   //SetOrientationType(aCutLines->GetOrientationType());
79   //SetOrientationType2(aCutLines->GetOrientationType2());
80   //SetRotateX(aCutLines->GetRotateX());
81   //SetRotateY(aCutLines->GetRotateY());
82   //SetRotateX2(aCutLines->GetRotateX2());
83   //SetRotateY2(aCutLines->GetRotateY2());
84 }
85
86 VISU::Storable* VISU::CutLines_i::Create(const char* theMeshName, VISU::Entity theEntity, 
87                                          const char* theFieldName, int theIteration)
88 {
89   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
90 }
91
92 VISU::Storable* VISU::CutLines_i::Restore(const Storable::TRestoringMap& theMap)
93      throw(std::logic_error&)
94 {
95   ScalarMap_i::Restore(theMap);
96   SetNbLines(VISU::Storable::FindValue(theMap,"myNbLines").toInt());
97   SetDisplacement(VISU::Storable::FindValue(theMap,"myDisplacement[0]").toDouble());
98   SetDisplacement2(VISU::Storable::FindValue(theMap,"myDisplacement[1]").toDouble());
99   SetOrientationType(VISU::CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[0]").toInt()));
100   SetOrientationType(VISU::CutPlanes::Orientation(VISU::Storable::FindValue(theMap,"myBasePlane[1]").toInt()));
101   SetRotateX(VISU::Storable::FindValue(theMap,"aRot[0][0]").toDouble());
102   SetRotateY(VISU::Storable::FindValue(theMap,"aRot[0][1]").toDouble());
103   SetRotateX2(VISU::Storable::FindValue(theMap,"aRot[1][0]").toDouble());
104   SetRotateY2(VISU::Storable::FindValue(theMap,"aRot[1][1]").toDouble());
105   
106   return Build(true);
107 }
108
109 void VISU::CutLines_i::ToStream(std::ostringstream& theStr){
110   ScalarMap_i::ToStream(theStr);
111   Storable::DataToStream( theStr, "myNbLines",         int(GetNbLines()) );
112   Storable::DataToStream( theStr, "myDisplacement[0]", GetDisplacement() );
113   Storable::DataToStream( theStr, "myDisplacement[1]", GetDisplacement2() );
114   Storable::DataToStream( theStr, "myBasePlane[0]",    int(GetOrientationType()) );
115   Storable::DataToStream( theStr, "myBasePlane[1]",    int(GetOrientationType2()) );
116   Storable::DataToStream( theStr, "aRot[0][0]",        GetRotateX() );
117   Storable::DataToStream( theStr, "aRot[0][1]",        GetRotateY() );
118   Storable::DataToStream( theStr, "aRot[1][0]",        GetRotateX2() );
119   Storable::DataToStream( theStr, "aRot[1][1]",        GetRotateY2() );
120 }
121
122 VISU::Storable* VISU::CutLines_i::Restore(SALOMEDS::SObject_ptr theSObject, 
123                                           const string& thePrefix, const Storable::TRestoringMap& theMap)
124      throw(std::logic_error&)
125 {
126   VISU::Result_i* pResult = GetResult(theSObject);
127   if(pResult != NULL){
128     VISU::CutLines_i* pResent = new VISU::CutLines_i(pResult);
129     return pResent->Restore(theMap);
130   }
131   return NULL;
132 }
133
134
135 VISU::CutLines_i::~CutLines_i(){
136   if(MYDEBUG) MESSAGE("CutLines_i::~CutLines_i()");
137 }
138
139
140 void VISU::CutLines_i::SetOrientationType(VISU::CutPlanes::Orientation theOrient){
141   myCutLinesPL->SetPlaneOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient));
142 }
143 void VISU::CutLines_i::SetOrientationType2(VISU::CutPlanes::Orientation theOrient){
144   myCutLinesPL->SetPlaneOrientation(VISU_CutPlanesPL::PlaneOrientation(theOrient),1);
145 }
146
147 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType() { 
148   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation());
149 }
150 VISU::CutPlanes::Orientation VISU::CutLines_i::GetOrientationType2() { 
151   return VISU::CutPlanes::Orientation(myCutLinesPL->GetPlaneOrientation(1));
152 }
153
154
155 void VISU::CutLines_i::SetDisplacement(CORBA::Double theDisp) { 
156   myCutLinesPL->SetDisplacement(theDisp);
157 }
158 void VISU::CutLines_i::SetDisplacement2(CORBA::Double theDisp) { 
159   myCutLinesPL->SetDisplacement(theDisp,1);
160 }
161
162 CORBA::Double VISU::CutLines_i::GetDisplacement() { 
163   return myCutLinesPL->GetDisplacement();
164 }
165 CORBA::Double VISU::CutLines_i::GetDisplacement2() { 
166   return myCutLinesPL->GetDisplacement(1);
167 }
168
169
170 void VISU::CutLines_i::SetBasePlanePosition(CORBA::Double thePlanePosition){
171   myCutLinesPL->SetPosition(thePlanePosition);
172 }
173
174 CORBA::Double VISU::CutLines_i::GetBasePlanePosition(){ 
175   return myCutLinesPL->GetPosition();
176 }
177
178 void VISU::CutLines_i::SetLinePosition(CORBA::Long thePlaneNumber, CORBA::Double thePlanePosition){
179   myCutLinesPL->SetPartPosition(thePlaneNumber,thePlanePosition);
180 }
181
182 CORBA::Double VISU::CutLines_i::GetLinePosition(CORBA::Long thePlaneNumber){ 
183   return myCutLinesPL->GetPartPosition(thePlaneNumber,1);
184 }
185
186
187 void VISU::CutLines_i::SetDefault(){
188   myCutLinesPL->SetDefault();
189 }
190
191 CORBA::Boolean VISU::CutLines_i::IsDefault(){ 
192   return myCutLinesPL->IsDefault();
193 }
194
195 void VISU::CutLines_i::SetDefaultPosition(CORBA::Long thePlaneNumber){
196   myCutLinesPL->SetPartDefault(thePlaneNumber);
197 }
198
199 CORBA::Boolean VISU::CutLines_i::IsDefaultPosition(CORBA::Long thePlaneNumber){ 
200   return myCutLinesPL->IsPartDefault(thePlaneNumber);
201 }
202
203
204 void VISU::CutLines_i::SetNbLines(CORBA::Long theNb) { 
205   myCutLinesPL->SetNbParts(theNb);
206 }
207 CORBA::Long VISU::CutLines_i::GetNbLines() { 
208   return myCutLinesPL->GetNbParts();
209 }
210
211
212 void VISU::CutLines_i::SetRotateX(CORBA::Double theAngle){
213   myCutLinesPL->SetRotateX(theAngle);
214 }
215 CORBA::Double VISU::CutLines_i::GetRotateX(){
216   return myCutLinesPL->GetRotateX();
217 }
218
219 void VISU::CutLines_i::SetRotateY(CORBA::Double theAngle){
220   myCutLinesPL->SetRotateY(theAngle);
221 }
222 CORBA::Double VISU::CutLines_i::GetRotateY(){
223   return myCutLinesPL->GetRotateY();
224 }
225
226 void VISU::CutLines_i::SetRotateX2(CORBA::Double theAngle){
227   myCutLinesPL->SetRotateX(theAngle,1);
228 }
229 CORBA::Double VISU::CutLines_i::GetRotateX2(){
230   return myCutLinesPL->GetRotateX(1);
231 }
232
233 void VISU::CutLines_i::SetRotateY2(CORBA::Double theAngle){
234   myCutLinesPL->SetRotateY(theAngle,1);
235 }
236 CORBA::Double VISU::CutLines_i::GetRotateY2(){
237   return myCutLinesPL->GetRotateY(1);
238 }
239
240 void VISU::CutLines_i::DoHook(){
241   if(!myPipeLine) myPipeLine = VISU_CutLinesPL::New();
242   myCutLinesPL = dynamic_cast<VISU_CutLinesPL*>(myPipeLine);
243
244   ScalarMap_i::DoHook();
245 }
246
247 void VISU::CutLines_i::BuildTableOfReal(SALOMEDS::SObject_ptr theSObject){
248
249   if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal");
250   Update();
251   SALOMEDS::GenericAttribute_var anAttr;
252   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
253   anAttr = aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributeTableOfReal");
254   SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
255
256   typedef set<long> TLineCont;
257   typedef map<float,TLineCont> TXMap;
258   typedef map<float,float> TXYMap;
259   typedef vector<TXYMap> TCurveVect;
260
261   const VISU::TField::TValField& aValField = myField->myValField;
262   const VISU::TField::TValForTime& aValForTime = aValField.find((int)myIteration)->second;
263   const VISU::TField::TTime& aTime = aValForTime.myTime;
264   QString aTitle;
265   //aTitle.sprintf("%s %s",myTitle.c_str(),aTime.second.c_str());
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   TCurveVect aCurveVect(iLineEnd); 
273   TLineCont aLineCont;
274   TXMap aXMap;
275   const float *aDirLn = myCutLinesPL->GetDirLn();
276   const float *aBasePnt = myCutLinesPL->GetBasePnt();
277   const float *aBoundPrjLn = myCutLinesPL->GetBoundPrjLn();
278   for(int iLine = 0; iLine < iLineEnd; iLine++){
279     vtkDataSet *aDataSet = myCutLinesPL->GetAppendPolyData()->GetInput(iLine);
280     aDataSet->Update();
281     int aNbPoints = aDataSet->GetNumberOfPoints();
282     vtkPointData *aPointData = aDataSet->GetPointData();
283     vtkDataArray *aScalars = aPointData->GetScalars();
284     vtkCellDataToPointData *aFilter = NULL;
285     if(!aScalars) {
286       aFilter = vtkCellDataToPointData::New();
287       aFilter->SetInput(aDataSet);
288       aFilter->PassCellDataOn();
289       aDataSet = aFilter->GetOutput();
290       aDataSet->Update();
291     }
292     aPointData = aDataSet->GetPointData();
293     aScalars = aPointData->GetScalars();
294     if(!aScalars) continue;
295     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLine = "<<iLine<<"; aNbPoints = "<<aNbPoints);
296     aLineCont.insert(iLine);
297     TXYMap& aXYMap = aCurveVect[iLine];
298     float aPnt[3], aVect[3], aDist;
299     for(int i = 0; i < aNbPoints; i++){
300       aDataSet->GetPoint(i,aPnt);
301       Sub(aPnt,aBasePnt,aVect);
302       aDist = vtkMath::Dot(aVect,aDirLn) / aBoundPrjLn[2];
303       // the workaround
304       if(aDist < 0.0) aDist = 0.0; 
305       if(aDist > 1.0) aDist = 1.0;
306       aXYMap[aDist] = aScalars->GetTuple1(i);
307       //aXMap[aDist].insert(iLine);
308     }
309     if(aFilter) aFilter->Delete();
310   }
311   iLineEnd = aLineCont.size();
312   if(iLineEnd == 0){
313     MESSAGE("CutPlanes_i::BuildTableOfReal aLineCont.size() == 0 !!!");
314     return;
315   }
316   //Resorting of theXYMap
317   for(int iLine = 0; iLine < iLineEnd; iLine++){
318     TXYMap& aXYMap = aCurveVect[iLine], aNewXYMap;
319     if(aXYMap.size() > 2){
320       TXYMap::const_iterator aXYMapIter[2] = {aXYMap.begin(), ++aXYMap.begin()};
321       aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
322       aXMap[aXYMapIter[0]->first].insert(iLine);
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         aXMap[aX[2]].insert(iLine);
330       }
331       aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
332       aXMap[aXYMapIter[0]->first].insert(iLine);
333       aXYMap = aNewXYMap;
334     }else{
335       TXYMap::const_iterator aXYMapIter = aXYMap.begin();
336       for(; aXYMapIter != aXYMap.end(); aXYMapIter++){
337         aXMap[aXYMapIter->first].insert(iLine);
338       }  
339     }
340   }
341   QString aString;
342   aTableOfReal->SetNbColumns(aXMap.size());
343   TXMap::const_iterator aXMapIter = aXMap.begin();
344   for(long i = 0; aXMapIter != aXMap.end(); aXMapIter++, i++){
345     float aDist = aXMapIter->first; 
346     aTableOfReal->PutValue(aDist,1,i+1);
347     aString.sprintf("%d",i);
348     aTableOfReal->SetColumnTitle(i+1,aString.latin1());
349     const TLineCont& aLineCont = aXMapIter->second;
350     long jEnd = aLineCont.size();
351     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal aDist = "<<aDist<<"; jEnd = "<<jEnd);
352     TLineCont::const_iterator aLineContIter = aLineCont.begin();
353     for(; aLineContIter != aLineCont.end(); aLineContIter++){
354       long iLine = *aLineContIter;
355       const TXYMap& aXYMap = aCurveVect[iLine];
356       TXYMap::const_iterator aXYMapIter = aXYMap.find(aDist);
357       if(aXYMapIter == aXYMap.end()) continue;
358       float aVal = aXYMapIter->second;
359       aTableOfReal->PutValue(aVal,iLine+2,i+1);
360     }
361   }
362   
363   aTableOfReal->SetRowTitle(1,"X");
364   TLineCont::const_iterator aLineContIter = aLineCont.begin();
365   QString aUnitName = myField->myUnitNames[0].c_str();
366   int aScalarMode = myCutLinesPL->GetScalarMode();
367   if(aScalarMode != 0) 
368     aUnitName = myField->myUnitNames[aScalarMode-1].c_str();
369   aUnitName = aUnitName.simplifyWhiteSpace();
370   for(long i = 0; aLineContIter != aLineCont.end(); aLineContIter++, i++){
371     long iLine = *aLineContIter;
372     aString.sprintf("Y%d",iLine);
373     aTableOfReal->SetRowTitle(i+2,aString.latin1());
374     aTableOfReal->SetRowUnit(i+2,aUnitName.latin1());
375   }
376 }