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