Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISU_I / VISU_StreamLines_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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //  File   : VISU_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_StreamLines_i.hh"
28 #include "VISU_Prs3dUtils.hh"
29 #include "VISU_Result_i.hh"
30
31 #include "VISU_Actor.h"
32 #include "VISU_StreamLinesPL.hxx"
33 #include "VISU_Convertor.hxx"
34
35 #include "SUIT_ResourceMgr.h"
36 #include "SALOME_Event.hxx"
37
38 #include <vtkDataSetMapper.h>
39 #include <vtkAppendFilter.h>
40 #include <vtkUnstructuredGrid.h>
41
42 #ifdef _DEBUG_
43 static int MYDEBUG = 0;
44 #else
45 static int MYDEBUG = 0;
46 #endif
47
48
49 //---------------------------------------------------------------
50 size_t
51 VISU::StreamLines_i
52 ::IsPossible(Result_i* theResult, 
53              const std::string& theMeshName, 
54              VISU::Entity theEntity,
55              const std::string& theFieldName, 
56              CORBA::Long theTimeStampNumber,
57              bool theIsMemoryCheck)
58 {
59   try{
60     if(!TSuperClass::IsPossible(theResult, theMeshName, theEntity, theFieldName, theTimeStampNumber, false)) 
61       return 0;
62
63     VISU::Result_i::PInput anInput = theResult->GetInput(theMeshName,
64                                                          theEntity,
65                                                          theFieldName,
66                                                          theTimeStampNumber);
67     VISU::PUnstructuredGridIDMapper anIDMapper = 
68       anInput->GetTimeStampOnMesh(theMeshName,
69                                   VISU::TEntity(theEntity),
70                                   theFieldName,
71                                   theTimeStampNumber);
72
73     vtkUnstructuredGrid* aDataSet = anIDMapper->GetUnstructuredGridOutput();
74     size_t aResult = VISU_StreamLinesPL::IsPossible(aDataSet);
75     MESSAGE("StreamLines_i::IsPossible - aResult = "<<aResult);
76     return aResult;
77   }catch(std::exception& exc){
78     INFOS("Follow exception was occured :\n"<<exc.what());
79   }catch(...){
80     INFOS("Unknown exception was occured!");
81   }
82   return 0;
83 }
84
85 //---------------------------------------------------------------
86 int VISU::StreamLines_i::myNbPresent = 0;
87
88 //---------------------------------------------------------------
89 QString
90 VISU::StreamLines_i
91 ::GenerateName() 
92 {
93   return VISU::GenerateName("StreamLines",myNbPresent++);
94 }
95
96 //---------------------------------------------------------------
97 const string VISU::StreamLines_i::myComment = "STREAMLINES";
98
99 //---------------------------------------------------------------
100 const char* 
101 VISU::StreamLines_i
102 ::GetComment() const 
103
104   return myComment.c_str();
105 }
106
107 //---------------------------------------------------------------
108 const char*
109 VISU::StreamLines_i
110 ::GetIconName()
111 {
112   if (!IsGroupsUsed())
113     return "ICON_TREE_STREAM_LINES";
114   else
115     return "ICON_TREE_STREAM_LINES_GROUPS";
116 }
117
118 //---------------------------------------------------------------
119 VISU::StreamLines_i
120 ::StreamLines_i(EPublishInStudyMode thePublishInStudyMode) :
121   ColoredPrs3d_i(thePublishInStudyMode),
122   ScalarMap_i(thePublishInStudyMode),
123   DeformedShape_i(thePublishInStudyMode),
124   myStreamLinesPL(NULL),
125   myAppendFilter(vtkAppendFilter::New())
126 {}
127
128
129 //---------------------------------------------------------------
130 void
131 VISU::StreamLines_i
132 ::SameAs(const Prs3d_i* theOrigin)
133 {
134   TSuperClass::SameAs(theOrigin);
135
136   if(const StreamLines_i* aPrs3d = dynamic_cast<const StreamLines_i*>(theOrigin)) {
137     StreamLines_i* anOrigin = const_cast<StreamLines_i*>(aPrs3d);
138     SetSource(anOrigin->GetSource());
139   }
140 }
141
142
143 //---------------------------------------------------------------
144 VISU::Storable* 
145 VISU::StreamLines_i
146 ::Create(const std::string& theMeshName, 
147          VISU::Entity theEntity,
148          const std::string& theFieldName, 
149          CORBA::Long theTimeStampNumber)
150 {
151   return TSuperClass::Create(theMeshName,theEntity,theFieldName,theTimeStampNumber);
152 }
153
154
155 //---------------------------------------------------------------
156 VISU::Storable* 
157 VISU::StreamLines_i
158 ::Restore(SALOMEDS::SObject_ptr theSObject,
159           const Storable::TRestoringMap& theMap)
160 {
161   if(!TSuperClass::Restore(theSObject, theMap))
162     return NULL;
163
164   double anIntegrationStep = VISU::Storable::FindValue(theMap,"myIntegrationStep").toDouble();
165   double aPropagationTime = VISU::Storable::FindValue(theMap,"myPropagationTime").toDouble();
166   double aStepLength = VISU::Storable::FindValue(theMap,"myStepLength").toDouble();
167   int aDirection = VISU::StreamLines::Direction(VISU::Storable::FindValue(theMap,"myDirection").toInt());
168   double aPercents = VISU::Storable::FindValue(theMap,"myPercents").toDouble();
169   SetParams(anIntegrationStep,
170             aPropagationTime,
171             aStepLength,
172             VISU::Prs3d::_nil(),
173             aPercents,
174             VISU::StreamLines::Direction(aDirection));
175   mySourceEntry = VISU::Storable::FindValue(theMap,"mySourceEntry").latin1();
176
177   return this;
178 }
179
180
181 //---------------------------------------------------------------
182 void
183 VISU::StreamLines_i
184 ::ToStream(std::ostringstream& theStr)
185 {
186   TSuperClass::ToStream(theStr);
187
188   Storable::DataToStream( theStr, "myIntegrationStep", GetIntegrationStep());
189   Storable::DataToStream( theStr, "myPropagationTime", GetPropagationTime());
190   Storable::DataToStream( theStr, "myStepLength", GetStepLength());
191
192   Storable::DataToStream( theStr, "myDirection", int(GetDirection()));
193   Storable::DataToStream( theStr, "myPercents", GetUsedPoints());
194
195   Storable::DataToStream( theStr, "mySourceEntry", mySourceEntry.c_str());
196 }
197
198
199 //---------------------------------------------------------------
200 VISU::StreamLines_i
201 ::~StreamLines_i()
202 {
203   if(MYDEBUG) MESSAGE("StreamLines_i::~StreamLines_i()");
204   myAppendFilter->Delete();
205 }
206
207
208 //---------------------------------------------------------------
209 CORBA::Boolean
210 VISU::StreamLines_i
211 ::SetParams(CORBA::Double theIntStep,
212             CORBA::Double thePropogationTime,
213             CORBA::Double theStepLength,
214             VISU::Prs3d_ptr thePrs3d,
215             CORBA::Double thePercents,
216             VISU::StreamLines::Direction theDirection)
217 {
218   struct TEvent: public SALOME_Event 
219   {
220     CORBA::Double myIntStep;
221     CORBA::Double myPropogationTime;
222     CORBA::Double myStepLength;
223     VISU::Prs3d_ptr myPrs3d;
224     CORBA::Double myPercents;
225     VISU::StreamLines::Direction myDirection;
226     
227     VISU_StreamLinesPL* myPipeLine;
228     VISU::Prs3d_i*& myPrs3dServant;
229     vtkAppendFilter* myAppendFilter;
230     size_t& myIsAccepted;
231     
232     TEvent(CORBA::Double theIntStep,
233            CORBA::Double thePropogationTime,
234            CORBA::Double theStepLength,
235            VISU::Prs3d_ptr thePrs3d,
236            CORBA::Double thePercents,
237            VISU::StreamLines::Direction theDirection,
238            VISU_StreamLinesPL* thePipeLine,
239            VISU::Prs3d_i*& thePrs3dServant,
240            vtkAppendFilter* theAppendFilter,
241            size_t& theIsAccepted):
242       myIntStep(theIntStep),
243       myPropogationTime(thePropogationTime),
244       myStepLength(theStepLength),
245       myPrs3d(thePrs3d),
246       myPercents(thePercents),
247       myDirection(theDirection),
248       myPipeLine(thePipeLine),
249       myPrs3dServant(thePrs3dServant),
250       myAppendFilter(theAppendFilter),
251       myIsAccepted(theIsAccepted)
252     {}
253
254     virtual
255     void
256     Execute()
257     {
258       myPrs3dServant = NULL;
259       vtkPointSet* aSource = NULL;
260       if (!CORBA::is_nil(myPrs3d)){
261         myPrs3dServant = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(myPrs3d).in());
262         if(myPrs3dServant){
263           myAppendFilter->RemoveAllInputs();
264           myAppendFilter->AddInput(myPrs3dServant->GetPipeLine()->GetMapper()->GetInput());
265           aSource = myAppendFilter->GetOutput();
266         }
267       }
268       myIsAccepted = myPipeLine->SetParams(myIntStep,
269                                            myPropogationTime,
270                                            myStepLength,
271                                            aSource,
272                                            myPercents,
273                                            myDirection);
274     }
275   };
276
277   size_t anIsAccepted = false;
278   VISU::Prs3d_i* aPrs3dServant = NULL;
279   ProcessVoidEvent(new TEvent(theIntStep,
280                               thePropogationTime,
281                               theStepLength,
282                               thePrs3d,
283                               thePercents,
284                               theDirection,
285                               GetSpecificPL(),
286                               aPrs3dServant,
287                               myAppendFilter,
288                               anIsAccepted));
289   if(anIsAccepted)
290     SetSource(aPrs3dServant);
291
292   return anIsAccepted;
293 }
294
295
296 //---------------------------------------------------------------
297 void
298 VISU::StreamLines_i
299 ::SetSource(VISU::Prs3d_ptr thePrs3d)
300 {
301   if(!thePrs3d->_is_nil()){
302     VISU::Prs3d_i* aPrs3di = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs3d).in());
303     SetSource(aPrs3di);
304   }
305 }
306
307 //---------------------------------------------------------------
308 void
309 VISU::StreamLines_i
310 ::SetSource(VISU::Prs3d_i* thePrs3d)
311 {
312   mySourceEntry = "";
313   if(!thePrs3d)
314     return;
315
316   SALOMEDS::SObject_var aSObject = thePrs3d->GetSObject();
317   CORBA::String_var aString = aSObject->GetID();
318   if(mySourceEntry == aString.in())
319     return;
320   
321   VISU::TSetModified aModified(this);
322   
323   mySourceEntry = aString.in();
324   myParamsTime.Modified();
325 }
326
327 //---------------------------------------------------------------
328 void
329 VISU::StreamLines_i
330 ::SetSource()
331 {
332   if(!myStreamLinesPL->GetSource() && mySourceEntry == "") 
333     return;
334
335   if(myStreamLinesPL->GetSource() == myAppendFilter->GetOutput()) 
336     return;
337
338   VISU::Prs3d_var aPrs3d = GetSource();
339   SetParams(GetIntegrationStep(),
340             GetPropagationTime(),
341             GetStepLength(),
342             aPrs3d,
343             GetUsedPoints(),
344             GetDirection());
345 }
346
347
348 //---------------------------------------------------------------
349 CORBA::Double 
350 VISU::StreamLines_i
351 ::GetIntegrationStep() 
352
353   return myStreamLinesPL->GetIntegrationStep();
354 }
355
356 //---------------------------------------------------------------
357 CORBA::Double
358 VISU::StreamLines_i
359 ::GetPropagationTime() 
360
361   return myStreamLinesPL->GetPropagationTime();
362 }
363
364 //---------------------------------------------------------------
365 CORBA::Double
366 VISU::StreamLines_i
367 ::GetStepLength() 
368
369   return myStreamLinesPL->GetStepLength();
370 }
371
372 //---------------------------------------------------------------
373 VISU::StreamLines::Direction
374 VISU::StreamLines_i
375 ::GetDirection() 
376
377   return VISU::StreamLines::Direction(myStreamLinesPL->GetDirection());
378 }
379
380
381 //---------------------------------------------------------------
382 VISU::Prs3d_ptr
383 VISU::StreamLines_i
384 ::GetSource()
385 {
386   VISU::Prs3d_var aPrs3d;
387   if(MYDEBUG) MESSAGE("StreamLines_i::GetSource() mySourceEntry = '"<<mySourceEntry<<"'");
388   if(mySourceEntry != ""){
389     SALOMEDS::SObject_var aSObject = GetStudyDocument()->FindObjectID(mySourceEntry.c_str());
390     CORBA::Object_var anObj = SObjectToObject(aSObject);
391     if(!CORBA::is_nil(anObj)) aPrs3d = VISU::Prs3d::_narrow(anObj);
392   }
393   return aPrs3d._retn();
394 }
395
396 //---------------------------------------------------------------
397 CORBA::Double 
398 VISU::StreamLines_i
399 ::GetUsedPoints() 
400
401   return myStreamLinesPL->GetUsedPoints();
402 }
403
404
405 //---------------------------------------------------------------
406 void 
407 VISU::StreamLines_i
408 ::CreatePipeLine(VISU_PipeLine* thePipeLine)
409 {
410   if(!thePipeLine){
411     myStreamLinesPL = VISU_StreamLinesPL::New();
412   }else
413     myStreamLinesPL = dynamic_cast<VISU_StreamLinesPL*>(thePipeLine);
414
415   TSuperClass::CreatePipeLine(myStreamLinesPL);
416 }
417
418
419 //----------------------------------------------------------------------------
420 bool
421 VISU::StreamLines_i
422 ::CheckIsPossible() 
423 {
424   return IsPossible(GetCResult(),GetCMeshName(),GetEntity(),GetCFieldName(),GetTimeStampNumber(),true);
425 }
426
427 //---------------------------------------------------------------
428 void
429 VISU::StreamLines_i
430 ::Update() 
431 {
432   SetSource();
433   TSuperClass::Update();
434 }
435
436
437 //---------------------------------------------------------------
438 VISU_Actor* 
439 VISU::StreamLines_i
440 ::CreateActor() 
441 {
442   if(VISU_Actor* anActor = TSuperClass::CreateActor(true)){
443     anActor->SetVTKMapping(true);
444     SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
445     int  aDispMode = aResourceMgr->integerValue("VISU", "stream_lines_represent", 1);
446     anActor->SetRepresentation(aDispMode);
447     return anActor;
448   }
449   return NULL;
450 }
451
452
453 //---------------------------------------------------------------
454 void
455 VISU::StreamLines_i
456 ::UpdateActor(VISU_Actor* theActor) 
457 {
458   TSuperClass::UpdateActor(theActor);
459 }