]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_PrsObject_i.cc
Salome HOME
NRI : Merge from V1_2.
[modules/visu.git] / src / VISU_I / VISU_PrsObject_i.cc
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_PrsObject_i.cxx
6 //  Author : Alexey PETROV
7 //  Module : VISU
8
9 using namespace std;
10 #include "VISU_PrsObject_i.hh"
11 #include "VISU_Extractor.hxx"
12 #include "VISU_FieldTransform.hxx"
13 #include "VISU_LookupTable.hxx"
14 #include "VISU_ScalarBarActor.hxx"
15 #include "VISU_Actor.h"
16 using namespace VISU;
17
18 #include "QAD_Config.h"
19
20 #include <vtkUnstructuredGridReader.h>
21 #include <vtkUnstructuredGridWriter.h>
22 #include <vtkPolyDataWriter.h>
23
24 #include <vtkDataSet.h>
25 #include <vtkDataSetMapper.h>
26 #include <vtkProperty.h>
27
28 #include <vtkPolyData.h>
29 #include <vtkPolyDataMapper.h>
30 #include <vtkGeometryFilter.h>
31
32 #include <vtkWarpVector.h>
33 #include <vtkContourFilter.h>
34 #include <vtkAppendPolyData.h>
35 #include <vtkCutter.h>
36 #include <vtkPlane.h>
37 #include <vtkCellDataToPointData.h>
38
39 #include <vtkGlyph3D.h>
40 #include <vtkHedgeHog.h>
41 #include <vtkPolyDataSource.h>
42 #include <vtkGlyphSource2D.h>
43 #include <vtkTransformPolyDataFilter.h>
44 #include <vtkCellCenters.h>
45 #include <vtkConeSource.h>
46 #include <vtkStreamLine.h>
47 #include <vtkMaskPoints.h>
48
49 #ifdef DEBUG
50 static int MYDEBUG = 0;
51 static int MYDEBUGWITHFILES = 0;
52 #else
53 static int MYDEBUG = 0;
54 static int MYDEBUGWITHFILES = 0;
55 #endif
56 static float EPS = 1.0E-3;
57 const float PI2 = 2.0*PI;
58
59 void WriteToFile(vtkPolyData* theDataSet, const string& theFileName){
60   vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New();
61   aWriter->SetFileName(theFileName.c_str());
62   aWriter->SetInput(theDataSet);
63   aWriter->Write();
64   aWriter->Delete();
65 }
66
67 //==============================================================================
68 void SetIOR(SALOMEDS::SObject_var& theSObject, const char* theIOR){
69   SALOMEDS::GenericAttribute_var anAttr;
70   theSObject->FindAttribute(anAttr, "AttributeIOR");
71   SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
72   CORBA::String_var aValue(theIOR);
73   anIOR->SetValue(aValue);
74 }
75
76 float CalcScaleFactor(float theBound[6], int theNbElem){
77   float aVolume = 1, vol, idim = 0;
78   for(int i = 0; i < 6; i += 2){
79     vol = fabs(theBound[i+1] - theBound[i]);
80     if(vol > 0) {
81       idim++; 
82       aVolume *= vol;
83     }
84   }
85   aVolume /= theNbElem;
86   float aFactor = pow(aVolume,(float)1./idim);
87   return aFactor;
88 }
89
90 VISU::Result_i* GetResult(SALOMEDS::SObject_ptr theSObject){
91   VISU::Result_var aResult = FindResult(theSObject);
92   if(!aResult->_is_nil())
93     return dynamic_cast<VISU::Result_i*>(VISU::GetServant(aResult.in()));
94   return NULL;
95 }
96
97
98 //----------------------------------------------------------------
99 //                      PrsObject
100 //----------------------------------------------------------------
101 void VISU::PrsObject_i::SetName( const char* theName )
102 {
103   myName = theName;
104   SALOMEDS::SObject_var mySO = myStudy->FindObjectID( GetEntry() );
105   if ( !mySO->_is_nil() ) {
106     SALOMEDS::StudyBuilder_var aBuilder = myStudy->NewBuilder();
107     SALOMEDS::GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute( mySO, "AttributeName" );
108     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
109     aNameAttr->SetValue( theName );
110   }
111 }
112 QString VISU::PrsObject_i::GetEntry() { 
113   CORBA::String_var anIOR = GetID();
114   SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(anIOR);
115   CORBA::String_var anEntry = aSObject->GetID();
116   return anEntry.in();
117 }
118
119
120 //----------------------------------------------------------------
121 //                      Prs3d Object
122 //----------------------------------------------------------------
123 VISU::Prs3d_i::Prs3d_i(Result_i* theResult) : 
124        PrsObject_i(theResult->GetStudyDocument()), 
125        myResult(theResult) 
126 {
127   myMapper = MapperType::New();
128   myGeomFilter = vtkGeometryFilter::New();
129   myGeomFilter->PointClippingOff();
130   myGeomFilter->CellClippingOff();
131   myGeomFilter->ExtentClippingOff();
132   myGeomFilter->MergingOff();
133   myColor.R = 0.5;
134   myColor.G = 0.5;
135   myColor.B = 0.5;
136   myAddToStudy = true;
137 }
138
139 void VISU::Prs3d_i::SameAs(const Prs3d_i* theOrigin)
140 {
141   Prs3d_i* aOrigin = const_cast<Prs3d_i*>(theOrigin);
142   SetColor(aOrigin->GetColor());
143   myMapper->ShallowCopy(aOrigin->GetMapper());
144 }
145
146 void VISU::Prs3d_i::SetColor(const SALOMEDS::Color& theColor){
147   myColor = theColor;
148 }
149
150 VISU::Prs3d_i::~Prs3d_i() {
151   CORBA::release(myStudy);
152   myMapper->Delete();
153   myGeomFilter->Delete();
154 }
155
156 void VISU::Prs3d_i::Destroy() {
157   CORBA::Object_var anObj = _this();
158   PortableServer::POA_ptr aPOA = Base_i::GetPOA();
159   PortableServer::ObjectId_var anId = aPOA->reference_to_id(anObj);
160   aPOA->deactivate_object(anId.in());
161   //this->_remove_ref();
162 }
163
164
165 /**
166  * Create Actor referred on existing IO
167  */
168 bool VISU::Prs3d_i::CreateActor(VISU_Actor* theActor, const Handle(SALOME_InteractiveObject)& theIO){
169   //MESSAGE("Prs3d_i::CreateActor");
170   Update();
171   myMapper->Update(); 
172   static float eps = VTK_LARGE_FLOAT * 0.1 ;
173   float aLength = myMapper->GetInput()->GetLength();
174   if(aLength > eps){
175     MESSAGE("Prs3d_i::CreateActor >> Diagonal of the actor is too large : "<<aLength);
176     return false;
177   }
178   theActor->SetShrinkable(myMapper->GetInput()->GetNumberOfCells() > 10);
179   myMapper->SetResolveCoincidentTopologyToShiftZBuffer();
180   myMapper->SetResolveCoincidentTopologyZShift(0.02);
181   theActor->SetMapper(myMapper);
182   if (myAddToStudy) {
183     if (theIO.IsNull()){
184       //Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject(GetEntry(),"VISU",GetName()); 
185       //There is a bug in gcc compiler - const Standard_CString is mapping as char * const not as const char *
186       //(where Standard_CString is defined as typedef char* Standard_CString)
187       Handle(SALOME_InteractiveObject) anIO = 
188         new SALOME_InteractiveObject(strdup(GetEntry()),"VISU",(const Standard_CString)GetName());
189       theActor->setIO(anIO); 
190     }else
191       theActor->setIO(theIO); 
192   }
193   theActor->setPrs3d(this);
194   return true;
195 }
196
197 void VISU::Prs3d_i::UpdateActor(VISU_Actor* theActor) {
198   if(MYDEBUG) MESSAGE("Prs3d_i::UpdateActor");
199   myMapper->Update(); 
200   theActor->Modified();
201 }
202
203 VISU::Storable* VISU::Prs3d_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
204      throw(std::logic_error&)
205 {
206   if(MYDEBUG) MESSAGE(GetComment());
207   myName = (const char*)(VISU::Storable::FindValue(theMap,"myName"));
208   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
209   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
210   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
211   return this;
212 }
213
214 void VISU::Prs3d_i::ToStream(std::ostringstream& theStr){
215   Storable::DataToStream( theStr, "myName",   myName.c_str() );
216   Storable::DataToStream( theStr, "myColor.R",   myColor.R );
217   Storable::DataToStream( theStr, "myColor.G",   myColor.G );
218   Storable::DataToStream( theStr, "myColor.B",   myColor.B );
219 }
220
221 void VISU::Prs3d_i::Update(){ 
222   myMapper->Modified();
223 }
224
225
226 //----------------------------------------------------------------
227 //                      Mesh Object
228 //----------------------------------------------------------------
229 int VISU::Mesh_i::myNbPresent = 0;
230
231 QString VISU::Mesh_i::GenerateName() { return VISU::GenerateName("Mesh",myNbPresent++);}
232
233 const string VISU::Mesh_i::myComment = "MESH";
234
235 const char* VISU::Mesh_i::GetComment() const { return myComment.c_str();}
236
237
238 VISU::Mesh_i::Mesh_i(Result_i* theResult) : 
239        PrsObject_i(theResult->GetStudyDocument()), 
240        Prs3d_i(theResult) 
241 {
242   myInput = NULL;
243   SALOMEDS::Color aColor = {0,1,1};
244   SetCellColor(aColor);
245   SetNodeColor(aColor);
246   SALOMEDS::Color aLinkColor = {83/255.,83/255.,83/255.};
247   SetLinkColor(aLinkColor);
248 }
249
250 void VISU::Mesh_i::SameAs(const Mesh_i* theOrigin)
251 {
252   return; // Same as command for mesh is not valid in current architecture
253 //   VISU::Prs3d_i::SameAs(theOrigin);
254 //   Mesh_i* aMesh = (Mesh_i*) theOrigin;
255 //   SetCellColor(aMesh->GetCellColor());
256 //   SetNodeColor(aMesh->GetNodeColor());
257 //   SetLinkColor(aMesh->GetLinkColor());
258 //   SetPresentationType(aMesh->GetPresentationType());
259 //   Build(false);
260 }
261
262 void VISU::Mesh_i::Destroy(){
263   SALOMEDS::SObject_var aSObj = myStudy->FindObjectID(GetEntry());
264   if(!aSObj->_is_nil()){
265     SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
266     aStudyBuilder->RemoveAttribute(aSObj,"AttributeIOR");
267   }
268   Prs3d_i::Destroy();
269 }
270
271 VISU::Storable* VISU::Mesh_i::Create(const char* theMeshName, int theEntity){
272   myMeshName = theMeshName;
273   myEntity = theEntity;
274   mySubMeshName = "";
275   return Build(false);
276 }
277
278 VISU::Storable* VISU::Mesh_i::Create(const char* theMeshName, int theEntity, const char* theFamilyName){
279   myMeshName = theMeshName;
280   myEntity = theEntity;
281   mySubMeshName = theFamilyName;
282   return Build(false);
283 }
284 VISU::Storable* VISU::Mesh_i::Create(const char* theMeshName, const char* theGroupName){
285   myMeshName = theMeshName;
286   myEntity = -1;
287   mySubMeshName = theGroupName;
288   return Build(false);
289 }
290
291 VISU::Storable* VISU::Mesh_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
292      throw(std::logic_error&)
293 {
294   Prs3d_i::Restore(theMap,false);
295
296   myMeshName = VISU::Storable::FindValue(theMap,"myMeshName").latin1();
297   myEntity = VISU::Storable::FindValue(theMap,"myEntity").toInt();
298   mySubMeshName = VISU::Storable::FindValue(theMap,"mySubMeshName").latin1();
299
300   myColor.R = VISU::Storable::FindValue(theMap,"myCellColor.R").toDouble();
301   myColor.G = VISU::Storable::FindValue(theMap,"myCellColor.G").toDouble();
302   myColor.B = VISU::Storable::FindValue(theMap,"myCellColor.B").toDouble();
303
304   myNodeColor.R = VISU::Storable::FindValue(theMap,"myNodeColor.R").toDouble();
305   myNodeColor.G = VISU::Storable::FindValue(theMap,"myNodeColor.G").toDouble();
306   myNodeColor.B = VISU::Storable::FindValue(theMap,"myNodeColor.B").toDouble();
307
308   myLinkColor.R = VISU::Storable::FindValue(theMap,"myLinkColor.R").toDouble();
309   myLinkColor.G = VISU::Storable::FindValue(theMap,"myLinkColor.G").toDouble();
310   myLinkColor.B = VISU::Storable::FindValue(theMap,"myLinkColor.B").toDouble();
311
312   if(theBuilding)
313     return Build(true);
314   return NULL;
315 }
316
317 void VISU::Mesh_i::ToStream(std::ostringstream& theStr){
318   Prs3d_i::ToStream(theStr);
319
320   Storable::DataToStream( theStr, "myMeshName", myMeshName.c_str() );
321   Storable::DataToStream( theStr, "myEntity", myEntity );
322   Storable::DataToStream( theStr, "mySubMeshName", mySubMeshName.c_str() );
323
324   Storable::DataToStream( theStr, "myCellColor.R", myColor.R );
325   Storable::DataToStream( theStr, "myCellColor.G", myColor.G );
326   Storable::DataToStream( theStr, "myCellColor.B", myColor.B );
327
328   Storable::DataToStream( theStr, "myNodeColor.R", myNodeColor.R );
329   Storable::DataToStream( theStr, "myNodeColor.G", myNodeColor.G );
330   Storable::DataToStream( theStr, "myNodeColor.B", myNodeColor.B );
331
332   Storable::DataToStream( theStr, "myLinkColor.R", myLinkColor.R );
333   Storable::DataToStream( theStr, "myLinkColor.G", myLinkColor.G );
334   Storable::DataToStream( theStr, "myLinkColor.B", myLinkColor.B );
335 }
336
337 VISU::Storable* VISU::MeshRestore(SALOMEDS::SObject_ptr theSObject, 
338                                   const string& thePrefix, const Storable::TRestoringMap& theMap)
339      throw(std::logic_error&)
340 {
341   VISU::Result_i* pResult = GetResult(theSObject);
342   if(pResult != NULL){
343     VISU::Mesh_i* pResent = new VISU::Mesh_i(pResult);
344     return pResent->Restore(theMap);
345   }
346   return NULL;
347 }
348
349
350 VISU::Mesh_i::~Mesh_i(){
351   if(MYDEBUG) MESSAGE("Mesh_i::~Mesh_i()");
352 }
353
354
355 VISU::Storable* VISU::Mesh_i::Build(int theRestoring){
356   try{
357     if(myResult->GetInput() == NULL) 
358       throw std::runtime_error("Mesh_i::Build - myResult->GetInput() == NULL !!!");
359     if(!theRestoring) myName = (const char*)(GenerateName());
360     if(myEntity >= 0) 
361       if(mySubMeshName == "") 
362         myType = VISU::TENTITY; 
363       else 
364         myType = VISU::TFAMILY;
365     else 
366       myType = VISU::TGROUP;
367     if(MYDEBUG) MESSAGE("Mesh_i::Build - myType = "<<myType);
368     //Building Input and comment for searching according label
369     QString aComment;
370     switch(myType){
371     case VISU::TENTITY : 
372       myInput = myResult->GetInput()->GetMeshOnEntity(myMeshName,(VISU::TEntity)myEntity); 
373       aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
374                        VISU::TENTITY,myMeshName.c_str(),myEntity);
375       break;
376     case VISU::TFAMILY : 
377       myInput = myResult->GetInput()->GetMeshOnEntity(myMeshName,(VISU::TEntity)myEntity,mySubMeshName);
378       aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
379                        VISU::TFAMILY,myMeshName.c_str(),myEntity,mySubMeshName.c_str());
380       break;
381     case VISU::TGROUP : 
382       myInput = myResult->GetInput()->GetMeshOnGroup(myMeshName,mySubMeshName);
383       aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
384                        VISU::TGROUP,myMeshName.c_str(),mySubMeshName.c_str());
385       break;
386     }
387     if(myInput == NULL) throw std::runtime_error("Mesh_i::Build - myInput == NULL !!!");
388     //Binding pipeline
389     vtkUnstructuredGrid *aDataSet = myInput->GetOutput();
390     //Avoid selection points that don't belong any of cells
391     myGeomFilter->SetInput(aDataSet);
392     myMapper->SetInput(myGeomFilter->GetOutput());
393     myMapper->ScalarVisibilityOff();
394     //Setting IOR on the label
395     if(!theRestoring) {
396       string aResultEntry = myResult->GetEntry();
397       string anEntry = FindEntryWithComment(myStudy,aResultEntry.c_str(),aComment.latin1());
398       if(anEntry == "") throw std::runtime_error("Mesh_i::Build - anEntry was not found !!!");
399       SALOMEDS::SObject_var aSObj = myStudy->FindObjectID(anEntry.c_str());
400       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
401       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObj,"AttributeIOR");
402       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
403       CORBA::String_var aString = GetID();
404       anIOR->SetValue(aString); 
405     }
406     aDataSet->GetBounds(myBounds);
407     if(MYDEBUGWITHFILES){
408       string aFileName = string("/users/")+getenv("USER")+"/"+getenv("USER");
409       aFileName += "-Mesh.vtk";
410       ::WriteToFile(myGeomFilter->GetOutput(),aFileName.c_str());
411     }
412     return this;
413   }catch(std::runtime_error& exc){
414     MESSAGE("Follow exception was accured :\n"<<exc.what());
415   }catch(...){
416     MESSAGE("Unknown exception was accured!");
417   }
418   return NULL;
419 }
420
421
422 VISU_Actor* VISU::Mesh_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) {
423   VISUMesh_Actor* anActor = VISUMesh_Actor::New();
424   if(MYDEBUG) MESSAGE("Prs3d_i::CreateActor");
425   if(VISU::Prs3d_i::CreateActor(anActor,theIO)){  
426     UpdateActor(anActor);
427     return anActor;
428   }else{
429     anActor->Delete();
430     return NULL;
431   }
432 }
433  
434 void VISU::Mesh_i::UpdateActor(VISU_Actor* theActor) {
435   if(VISUMesh_Actor* anActor = dynamic_cast<VISUMesh_Actor*>(theActor)){
436     if(MYDEBUG) MESSAGE("Mesh_i::UpdateActor");
437     VISU::Prs3d_i::UpdateActor(theActor);
438     vtkProperty* aSurfaceProperty = anActor->GetProperty();
439     aSurfaceProperty->SetColor(myColor.R, myColor.G, myColor.B);
440     vtkProperty* anEdgeProperty = anActor->GetEdgeProperty();
441     anEdgeProperty->SetColor(myLinkColor.R, myLinkColor.G, myLinkColor.B);
442   }
443 }
444
445
446
447 //==============================================================================
448 int VISU::ScalarMap_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
449                                   const char* theFieldName, double theIteration){
450   try{
451     const VISU::TField& aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
452     const VISU::TField::TValField& aValField = aField.myValField;
453     bool aRes = aValField.find((int)theIteration) != aValField.end();
454     if(MYDEBUG) MESSAGE("ScalarMap_i::IsPossible = "<<aRes)
455     return aRes;
456   }catch(std::runtime_error& exc){
457     MESSAGE("Follow exception was accured :\n"<<exc.what());
458   }catch(...){
459     MESSAGE("Unknown exception was accured!");
460   }
461   return 0;
462 }
463
464 int VISU::ScalarMap_i::myNbPresent = 0;
465 QString VISU::ScalarMap_i::GenerateName() { return VISU::GenerateName("ScalarMap",myNbPresent++);}
466
467 const string VISU::ScalarMap_i::myComment = "SCALARMAP";
468 const char* VISU::ScalarMap_i::GetComment() const { return myComment.c_str();}
469
470 VISU::ScalarMap_i::ScalarMap_i(Result_i* theResult, bool theAddToStudy) : 
471        PrsObject_i(theResult->GetStudyDocument()), 
472        Prs3d_i(theResult) 
473 {
474   myAddToStudy = theAddToStudy;
475   myInput = VISU_Extractor::New();
476   myFieldTransform = VISU_FieldTransform::New();
477   myMapperTable = VISU_LookupTable::New();
478   myBarTable = VISU_LookupTable::New();
479   myIsColored = true;
480   myMapScale = 1.0;
481 }
482
483 void VISU::ScalarMap_i::Destroy(){
484   if(myAddToStudy){
485     SALOMEDS::SObject_var aSObj = myStudy->FindObjectID(GetEntry());
486     if(!aSObj->_is_nil()){
487       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
488       aStudyBuilder->RemoveObject(aSObj);
489     }
490   }
491   Prs3d_i::Destroy();
492 }
493
494 void VISU::ScalarMap_i::SameAs(const ScalarMap_i* theOrigin)
495 {
496   ScalarMap_i* aScalarMap = const_cast<ScalarMap_i*>(theOrigin);
497   VISU::Prs3d_i::SameAs(aScalarMap);
498   myField = aScalarMap->GetField();
499   myMeshName = myField->myMeshName;
500   myEntity = myField->myEntity;
501   myIteration = aScalarMap->GetIteration();
502   myFieldName = aScalarMap->GetFieldName();
503
504   SetScalarMode(aScalarMap->GetScalarMode());
505   SetRange(aScalarMap->GetMin(), aScalarMap->GetMax());
506   SetScaling(aScalarMap->GetScaling());
507   SetOrientation(aScalarMap->GetOrientation());
508   SetPosition(aScalarMap->GetPosX(), aScalarMap->GetPosY());
509   SetSize(aScalarMap->GetWidth(), aScalarMap->GetHeight());
510   SetNbColors(aScalarMap->GetNbColors());
511   SetLabels(aScalarMap->GetLabels());
512   SetTitle(aScalarMap->GetTitle());
513   Build(-1);
514   Update();
515 }
516
517
518 /**
519  * Creates Scalar Map and initialises it from resources
520  */
521 VISU::Storable* VISU::ScalarMap_i::Create(const char* theMeshName, VISU::Entity theEntity, 
522                                           const char* theFieldName, CORBA::Double theIteration){
523   myIsFixedRange = false;
524   // Orientation
525   QString aOrient = QAD_CONFIG->getSetting("Visu:SBOrientation");
526   if ( !aOrient.isEmpty() ) 
527     myOrientation = (VISU::ScalarMap::Orientation) aOrient.toInt();
528   else
529     myOrientation = VISU::ScalarMap::VERTICAL;
530
531   // Scalar Bar origin
532   QString aXorigin = QAD_CONFIG->getSetting("Visu:SBXorigin");
533   if ( !aXorigin.isEmpty() )
534     myPosition[0] = aXorigin.toFloat();
535   else {
536     if(myOrientation == VISU::ScalarMap::VERTICAL) {
537       myPosition[0] = 0.03;
538     } else {
539       myPosition[0] = 0.2;
540     }
541   }
542   
543   QString aYorigin = QAD_CONFIG->getSetting("Visu:SBYorigin");
544   if ( !aYorigin.isEmpty() )
545     myPosition[1] = aYorigin.toFloat();
546   else {
547     if(myOrientation == VISU::ScalarMap::VERTICAL) {
548       myPosition[1] = 0.1;
549     } else {
550       myPosition[1] = 0.012;
551     }
552   }
553
554   // Scalar Bar size
555   QString aWidth = QAD_CONFIG->getSetting("Visu:SBWidth");
556   if ( !aWidth.isEmpty() )
557     myWidth = aWidth.toFloat();
558   else {
559     myWidth =(myOrientation == VISU::ScalarMap::VERTICAL)? 0.17:0.6;
560   }
561   QString aHeight = QAD_CONFIG->getSetting("Visu:SBHeight");
562   if ( !aHeight.isEmpty() )
563     myHeight = aHeight.toFloat();
564   else {
565     myHeight =(myOrientation == VISU::ScalarMap::VERTICAL)? 0.8:0.12;
566   }
567
568   // Nb of Colors
569   QString aColors = QAD_CONFIG->getSetting("Visu:SBNumberOfColors");
570   myNumberOfColors = (aColors.isEmpty())? 64 : aColors.toInt();
571   if (myNumberOfColors > 64) 
572     myNumberOfColors = 64;
573
574   // Nb of Labels
575   QString aLabels = QAD_CONFIG->getSetting("Visu:SBNumberOfLabels");
576   myNumberOfLabels = (aLabels.isEmpty())? 5 : aLabels.toInt();
577   if (myNumberOfLabels > 65) 
578     myNumberOfLabels = 65;
579   
580   // Scalar Range
581   QString aFixRange = QAD_CONFIG->getSetting("Visu:SBImposeRange");
582   myIsFixedRange = (aFixRange.compare("true") == 0);
583   
584   QString aRangeMin = QAD_CONFIG->getSetting("Visu:SBMinimumValue");
585   myScalarRange[0] = (aRangeMin.isEmpty())? 0 : aRangeMin.toFloat();
586   
587   QString aRangeMax = QAD_CONFIG->getSetting("Visu:SBMaximumValue");
588   myScalarRange[1] = (aRangeMax.isEmpty())? 0 : aRangeMax.toFloat();
589
590   QString aScaling = QAD_CONFIG->getSetting("Visu:SBScaling");
591   if(aScaling.compare("LOGARITHMIC") == 0) 
592     myScaling = VISU::LOGARITHMIC;
593   else 
594     myScaling = VISU::LINEAR;
595
596 //    myName = GenerateName();
597   myMeshName = theMeshName;
598   myEntity = theEntity;
599   myFieldName =theFieldName;
600   myScalarMode = 0;
601   myIteration = theIteration;
602   return Build(false);
603 }
604
605
606
607 VISU::Storable* VISU::ScalarMap_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
608      throw(std::logic_error&)
609 {
610   Prs3d_i::Restore(theMap,false);
611
612   myAddToStudy = false; //SRN Added 21/06/2003 SAL2983: to avoid addition of the new ScalarMap to study.
613
614   myMeshName = (const char*)(VISU::Storable::FindValue(theMap,"myMeshName"));
615   myEntity = VISU::Storable::FindValue(theMap,"myEntity").toInt();
616   myFieldName = (const char*)(VISU::Storable::FindValue(theMap,"myFieldName"));
617   myIteration = VISU::Storable::FindValue(theMap,"myIteration").toInt();
618
619   myScalarMode = VISU::Storable::FindValue(theMap,"myScalarMode").toInt();
620   myScalarRange[0] = VISU::Storable::FindValue(theMap,"myScalarRange[0]").toDouble();
621   myScalarRange[1] = VISU::Storable::FindValue(theMap,"myScalarRange[1]").toDouble();
622   myIsFixedRange = VISU::Storable::FindValue(theMap,"myIsFixedRange").toInt();
623   myScaling = (VISU::Scaling)VISU::Storable::FindValue(theMap,"myScaling").toInt();
624
625   myTitle = (const char*)(VISU::Storable::FindValue(theMap,"myTitle"));
626   myOrientation = (VISU::ScalarMap::Orientation)VISU::Storable::FindValue(theMap,"myOrientation").toInt();
627   myNumberOfColors = VISU::Storable::FindValue(theMap,"myNumberOfColors").toInt();
628   myNumberOfLabels = VISU::Storable::FindValue(theMap,"myNumberOfLabels").toInt();
629   myPosition[0] = VISU::Storable::FindValue(theMap,"myPosition[0]").toDouble();
630   myPosition[1] = VISU::Storable::FindValue(theMap,"myPosition[1]").toDouble();
631   myWidth = VISU::Storable::FindValue(theMap,"myWidth").toDouble();
632   myHeight = VISU::Storable::FindValue(theMap,"myHeight").toDouble();
633
634   if(theBuilding)
635     return Build(true);
636   return NULL;
637 }
638
639 void VISU::ScalarMap_i::ToStream(std::ostringstream& theStr){
640   Prs3d_i::ToStream(theStr);
641
642   Storable::DataToStream( theStr, "myMeshName",       myMeshName.c_str() );
643   Storable::DataToStream( theStr, "myEntity",         myEntity );
644   Storable::DataToStream( theStr, "myFieldName",      myFieldName.c_str() );
645   Storable::DataToStream( theStr, "myIteration",      myIteration );
646
647   Storable::DataToStream( theStr, "myScalarMode",     myScalarMode );
648   Storable::DataToStream( theStr, "myScalarRange[0]", myScalarRange[0] );
649   Storable::DataToStream( theStr, "myScalarRange[1]", myScalarRange[1] );
650   Storable::DataToStream( theStr, "myIsFixedRange",   myIsFixedRange );
651   Storable::DataToStream( theStr, "myScaling",        myScaling );
652
653   Storable::DataToStream( theStr, "myTitle",          myTitle.c_str() );
654   Storable::DataToStream( theStr, "myOrientation",    myOrientation );
655   Storable::DataToStream( theStr, "myNumberOfColors", myNumberOfColors );
656   Storable::DataToStream( theStr, "myNumberOfLabels", myNumberOfLabels );
657   Storable::DataToStream( theStr, "myPosition[0]",    myPosition[0] );
658   Storable::DataToStream( theStr, "myPosition[1]",    myPosition[1] );
659   Storable::DataToStream( theStr, "myWidth",          myWidth );
660   Storable::DataToStream( theStr, "myHeight",         myHeight );
661 }
662
663 void VISU::ScalarMap_i::SetScaling(VISU::Scaling theScaling){
664   VISU_FieldTransform::TTransformFun aFun;
665   if(MYDEBUG)  MESSAGE("ScalarMap_i::SetScaling - theScaling = "<<theScaling);
666   switch(theScaling){
667   case VISU::LOGARITHMIC : 
668     aFun = &Logarithmic10;  
669     break;
670   default:  
671     aFun = &Identical;
672   }
673   myScaling = theScaling;
674   vtkUnstructuredGrid *aDataSet = myInput->GetUnstructuredGridOutput();
675   myFieldTransform->SetInput(aDataSet);
676   myFieldTransform->SetTransformFunction(aFun);
677   myFieldTransform->SetScalarRange(myScalarRange);
678   if(MYDEBUGWITHFILES){
679     string aFileName = string("/users/")+getenv("USER")+"/"+getenv("USER");
680     aFileName += "-PrsScaling.vtk";
681     WriteToFile(myFieldTransform->GetUnstructuredGridOutput(),aFileName.c_str());
682   }
683 }
684
685 VISU::Scaling VISU::ScalarMap_i::GetScaling(){
686   return myScaling;
687 }
688
689
690 void VISU::ScalarMap_i::SetRange(CORBA::Double theMin, CORBA::Double theMax) { 
691   if(theMin > theMax) return;
692   myScalarRange[0] = theMin; 
693   myScalarRange[1] = theMax; 
694   myFieldTransform->SetScalarRange(myScalarRange);
695   myIsFixedRange = true;
696   if(MYDEBUG) MESSAGE("ScalarMap_i::SetRange = "<<myScalarRange[0]<<"; "<<myScalarRange[1]);
697 }
698  
699    
700 void VISU::ScalarMap_i::CalculateRange() {
701   GetFieldRange(myScalarRange);
702 }
703
704 void VISU::ScalarMap_i::GetFieldRange(float theRange[2]) {
705   myInput->Update();
706   myInput->GetUnstructuredGridOutput()->GetScalarRange(theRange);
707 }
708
709 void VISU::ScalarMap_i::SetScalarMode(CORBA::Long theScalarMode){
710   myScalarMode = theScalarMode;
711   vtkUnstructuredGridReader* aReader = 
712     myResult->GetInput()->GetFieldOnMesh(myMeshName,(VISU::TEntity)myEntity,myFieldName,(int)myIteration);
713   if(aReader != NULL) {
714     if( MYDEBUG) MESSAGE("ScalarMap_i::SetScalarMode = "<<myIteration);
715     string aFieldName = VISU_Convertor::GenerateName(myFieldName,(int)myIteration);
716     myInput->Extract(aReader,aFieldName,myScalarMode,myField->myNbComp,myField->myEntity);
717     if(MYDEBUGWITHFILES){
718       string aFileName = string("/users/")+getenv("USER")+"/"+getenv("USER");
719       aFileName += "-ScalarMode.vtk";
720       WriteToFile(myInput->GetUnstructuredGridOutput(),aFileName.c_str());
721     }
722   }
723 }
724
725 VISU::Storable* VISU::ScalarMapRestore(SALOMEDS::SObject_ptr theSObject, 
726                                        const string& thePrefix, const Storable::TRestoringMap& theMap)
727      throw(std::logic_error&)
728 {
729   VISU::Result_i* pResult = GetResult(theSObject);
730   if(pResult != NULL){
731     VISU::ScalarMap_i* pResent = new VISU::ScalarMap_i(pResult);
732     return pResent->Restore(theMap);
733   }
734   return NULL;
735 }
736
737
738 VISU::ScalarMap_i::~ScalarMap_i(){
739   if(MYDEBUG) MESSAGE("ScalarMap_i::~ScalarMap_i()");
740   myInput->Delete();
741   myFieldTransform->Delete();
742   myMapperTable->Delete();
743   myBarTable->Delete();
744 }
745
746
747 VISU::Storable* VISU::ScalarMap_i::Build(int theRestoring){
748   if(MYDEBUG) 
749     MESSAGE("ScalarMap_i::Build - "<<myFieldName<<"; theRestoring = "<<theRestoring);
750   try{
751     if(myResult->GetInput() == NULL) 
752       throw std::runtime_error("Mesh_i::Build - myResult->GetInput() == NULL !!!");
753     myField = &(myResult->GetInput()->GetField(myMeshName,(VISU::TEntity)myEntity,myFieldName));
754     if(myField == NULL) throw std::runtime_error("There is no Field with the parameters !!!");
755     SetScalarMode(myScalarMode);
756     if(myInput->GetInput() == NULL) throw std::runtime_error("Can't build build vtk representation !!!");
757     vtkUnstructuredGrid *aDataSet = myInput->GetUnstructuredGridOutput();
758     SetScaling(myScaling);
759     aDataSet->GetBounds(myBounds);
760     CORBA::String_var anIOR = GetID();
761     const VISU::TField::TValField& aValField = myField->myValField;
762     const VISU::TField::TValForTime& aValForTime = aValField.find((int)myIteration)->second;
763     const VISU::TField::TTime& aTime = aValForTime.myTime;
764     QString aComment;
765     myName = "NoName";
766     if(theRestoring == 0){
767       if (!myIsFixedRange) aDataSet->GetScalarRange(myScalarRange);
768       aComment.sprintf("%s %s",myFieldName.c_str(),VISU_Convertor::GenerateName(aValForTime.myTime).c_str());
769       //aComment.sprintf("%s, %g",myFieldName.c_str(),aTime.first);
770       myTitle = (const char*)(aComment.simplifyWhiteSpace());
771     }
772     if (myAddToStudy) {
773       myName = (const char*)(GenerateName());
774       aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
775                        VISU::TTIMESTAMP,myMeshName.c_str(),myEntity,myFieldName.c_str(),(int)myIteration,myField->myNbComp);
776       string aResultEntry = myResult->GetEntry();
777       string aRefFatherEntry = myResult->GetRefFatherEntry();
778       string anEntry = FindEntryWithComment(myStudy,aResultEntry.c_str(),aComment.latin1());
779       if(anEntry == "") throw std::runtime_error("There is no Entry for binding the presentation !!!");
780       aComment.sprintf("myComment=%s;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d;myComponentId=%d",
781                        GetComment(),myMeshName.c_str(),myEntity,myFieldName.c_str(),(int)myIteration,myField->myNbComp,myScalarMode);
782       CreateAttributes(myStudy,anEntry.c_str(),aRefFatherEntry.c_str(),
783                        anIOR,myName.c_str(),"",aComment.latin1(),true);
784     }
785     if(MYDEBUG) 
786       MESSAGE("ScalarMap_i::Build - myFieldName = "<<myFieldName<<"; myName = "<<myName<<
787               "; myScalarRange[0] = "<<myScalarRange[0]<<"; myScalarRange[1] = "<<myScalarRange[1]<<
788               "; myNumberOfColors = "<<myNumberOfColors);
789     myGeomFilter->SetInput(DoHook(theRestoring));
790     myMapper->SetInput(myGeomFilter->GetOutput());
791     myMapper->SetColorModeToMapScalars();
792     myMapper->ScalarVisibilityOn();
793     return this;
794   }catch(std::runtime_error& exc){
795     MESSAGE("Follow exception was accured :\n"<<exc.what());
796   }catch(...){
797     MESSAGE("Unknown exception was accured!");
798   }
799   return NULL;
800 }
801
802
803 VISU::ScalarMap_i::DataType* VISU::ScalarMap_i::DoHook(int theRestoring){
804   myMapper->ScalarVisibilityOn();
805   Update();
806   return myFieldTransform->GetUnstructuredGridOutput();
807 }
808
809 void VISU::ScalarMap_i::Update() {
810   if (!myIsFixedRange) CalculateRange();
811
812   int aScaling = GetScaling() == VISU::LOGARITHMIC? VTK_SCALE_LOG10: VTK_SCALE_LINEAR;
813   float aScalarRange[2] = {myScalarRange[0], myScalarRange[1]};
814   myMapperTable->SetScale(VTK_SCALE_LINEAR);
815   if(aScaling == VTK_SCALE_LOG10)
816     VISU_LookupTable::ComputeLogRange(myScalarRange,aScalarRange);
817   myMapperTable->SetRange(aScalarRange);
818   myMapper->SetScalarRange(aScalarRange);
819   myMapperTable->SetHueRange(0.667,0.0);
820   myMapperTable->SetNumberOfColors(myNumberOfColors);
821   myMapperTable->SetMapScale(myMapScale);
822   myMapperTable->Build();
823   myMapper->SetLookupTable(myMapperTable);   
824   myMapper->MapScalars(1.0f);
825   
826   myBarTable->SetHueRange(0.667,0.0);
827   myBarTable->SetNumberOfColors(myNumberOfColors);
828   myBarTable->SetRange(myScalarRange);
829   myBarTable->SetScale(aScaling);
830   myBarTable->Build();
831
832   VISU::Prs3d_i::Update();
833 }
834
835 void VISU::ScalarMap_i::SetMapScale(double theMapScale){
836   if(MYDEBUG) MESSAGE("ScalarMap_i::SetMapScale() - theMapScale = "<<theMapScale);
837   myMapScale = theMapScale;
838   Update();
839 }
840
841 VISU_Actor* VISU::ScalarMap_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) {
842   VISUScalarMap_Actor* anActor = VISUScalarMap_Actor::New();
843   if(VISU::Prs3d_i::CreateActor(anActor,theIO)){  
844     anActor->SetBarVisibility(&myIsColored);
845     anActor->myFieldName = myFieldName;
846     anActor->GetProperty()->EdgeVisibilityOn();
847     UpdateActor(anActor);
848     return anActor;
849   }else{
850     anActor->Delete();
851     return NULL;
852   }
853 }
854
855 void VISU::ScalarMap_i::UpdateActor(VISU_Actor* theActor) {
856   if(VISUScalarMap_Actor* anActor = dynamic_cast<VISUScalarMap_Actor*>(theActor)){
857     VISU::Prs3d_i::UpdateActor(theActor);
858     theActor->SetVisibility(theActor->GetVisibility());
859     VISU_ScalarBarActor * aScalarBar = anActor->GetScalarBar();
860     aScalarBar->SetLookupTable(myBarTable);
861     aScalarBar->SetTitle(myTitle.c_str());
862     aScalarBar->SetOrientation(myOrientation);
863     aScalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
864     aScalarBar->GetPositionCoordinate()->SetValue(myPosition[0],myPosition[1]);
865     aScalarBar->SetWidth(myWidth);
866     aScalarBar->SetHeight(myHeight);
867     aScalarBar->SetNumberOfLabels(myNumberOfLabels);
868     aScalarBar->Modified();
869   }
870 }
871
872 //==============================================================================
873 int VISU::DeformedShape_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
874                                       const char* theFieldName, double theIteration){
875   try{
876     if(!VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration)) return 0;
877     const VISU::TField& aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
878     return aField.myNbComp > 1;
879   }catch(std::runtime_error& exc){
880     MESSAGE("Follow exception was accured :\n"<<exc.what());
881   }catch(...){
882     MESSAGE("Unknown exception was accured!");
883   }
884   return 0;
885 }
886
887 int VISU::DeformedShape_i::myNbPresent = 0;
888 QString VISU::DeformedShape_i::GenerateName() { return VISU::GenerateName("Def.Shape",myNbPresent++);}
889
890 const string VISU::DeformedShape_i::myComment = "DEFORMEDSHAPE";
891 const char* VISU::DeformedShape_i::GetComment() const { return myComment.c_str();}
892
893 VISU::DeformedShape_i::DeformedShape_i(Result_i* theResult, bool theAddToStudy) : 
894        PrsObject_i(theResult->GetStudyDocument()), 
895        Prs3d_i(theResult),       
896        ScalarMap_i(theResult, theAddToStudy)
897 {
898   if(MYDEBUG) MESSAGE("DeformedShape_i::DeformedShape_i");
899   myWarpVector = vtkWarpVector::New();
900 }
901
902 void VISU::DeformedShape_i::Destroy(){
903   ScalarMap_i::Destroy();
904 }
905
906 void VISU::DeformedShape_i::SameAs(const DeformedShape_i* theOrigin)
907 {
908   DeformedShape_i* aDefShape = const_cast<DeformedShape_i*>(theOrigin);
909   setColored(aDefShape->isColored());
910   SetScale(aDefShape->GetScale());
911   VISU::ScalarMap_i::SameAs(theOrigin);
912 }
913
914
915 VISU::Storable* VISU::DeformedShape_i::Create(const char* theMeshName, VISU::Entity theEntity, 
916                                               const char* theFieldName, double theIteration){
917   myIsColored = false;
918   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
919 }
920
921 VISU::Storable* VISU::DeformedShape_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
922      throw(std::logic_error&)
923 {
924   ScalarMap_i::Restore(theMap,false);
925
926   myFactor = VISU::Storable::FindValue(theMap,"myFactor").toDouble();
927   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
928
929   if(theBuilding)
930     return Build(true);
931   return NULL;
932 }
933
934 void VISU::DeformedShape_i::ToStream(std::ostringstream& theStr){
935   ScalarMap_i::ToStream(theStr);
936
937   Storable::DataToStream( theStr, "myFactor", myFactor );
938   Storable::DataToStream( theStr, "myIsColored", myIsColored );
939 }
940
941 VISU::Storable* VISU::DeformedShapeRestore(SALOMEDS::SObject_ptr theSObject, 
942                                            const string& thePrefix, const Storable::TRestoringMap& theMap)
943      throw(std::logic_error&)
944 {
945   VISU::Result_i* pResult = GetResult(theSObject);
946   if(pResult != NULL){
947     VISU::DeformedShape_i* pResent = new VISU::DeformedShape_i(pResult);
948     return pResent->Restore(theMap);
949   }
950   return NULL;
951 }
952
953
954 VISU::DeformedShape_i::~DeformedShape_i(){
955   if(MYDEBUG) MESSAGE("DeformedShape_i::~DeformedShape_i()");
956   myWarpVector->Delete();
957 }
958
959
960 VISU::ScalarMap_i::DataType* VISU::DeformedShape_i::DoHook(int theRestoring){
961   if(theRestoring == 0)
962     myFactor = ScaleFactor()/GetMax();
963   if(MYDEBUG) MESSAGE("DeformedShape_i::DoHook() - myFactor = "<<myFactor);
964   Update();
965   return myWarpVector->GetOutput();
966 }
967
968
969 float VISU::DeformedShape_i::ScaleFactor() { 
970   vtkUnstructuredGrid *aDataSet = myInput->GetUnstructuredGridOutput();
971   aDataSet->Update();
972   int nbCells = aDataSet->GetNumberOfCells(), nbPoints = aDataSet->GetNumberOfPoints();
973   return ::CalcScaleFactor(myBounds, nbCells? nbCells: nbPoints);
974 }
975
976 void VISU::DeformedShape_i::Update(){
977   if(myField->myNbComp > 1){
978     if(myField->myEntity == VISU::CELL_ENTITY){
979       vtkCellDataToPointData *aFilter = vtkCellDataToPointData::New();
980       aFilter->SetInput(myFieldTransform->GetUnstructuredGridOutput());
981       aFilter->PassCellDataOn();
982       myWarpVector->SetInput(aFilter->GetUnstructuredGridOutput());
983       aFilter->Delete();
984     }else
985       myWarpVector->SetInput(myFieldTransform->GetUnstructuredGridOutput());
986     myWarpVector->SetScaleFactor(myFactor*myMapScale);
987     myMapper->SetScalarVisibility(myIsColored);
988   }
989   ScalarMap_i::Update();
990 }
991
992 void VISU::DeformedShape_i::SetColor(const SALOMEDS::Color& theColor){
993   VISU::Prs3d_i::SetColor(theColor);
994   myIsColored = false;
995 }
996
997 void VISU::DeformedShape_i::SetMapScale(double theMapScale){
998   myMapScale = theMapScale;
999   Update();
1000 }
1001
1002 VISU_Actor* VISU::DeformedShape_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) {
1003   VISU_Actor* anActor = VISU::ScalarMap_i::CreateActor(theIO);
1004   anActor->SetRepresentation(1);
1005   return anActor;
1006 }
1007
1008 void VISU::DeformedShape_i::UpdateActor(VISU_Actor* theActor) {
1009   VISU::ScalarMap_i::UpdateActor(theActor);
1010   if(!myIsColored)
1011     theActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
1012 }
1013
1014 //==============================================================================
1015 int VISU::CutPlanes_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
1016                                   const char* theFieldName, double theIteration){
1017   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
1018 }
1019
1020 int VISU::CutPlanes_i::myNbPresent = 0;
1021 QString VISU::CutPlanes_i::GenerateName() { return VISU::GenerateName("CutPlanes",myNbPresent++);}
1022
1023 const string VISU::CutPlanes_i::myComment = "CUTPLANES";
1024 const char* VISU::CutPlanes_i::GetComment() const { return myComment.c_str();}
1025
1026 VISU::CutPlanes_i::CutPlanes_i(Result_i* theResult, bool theAddToStudy) :
1027        PrsObject_i(theResult->GetStudyDocument()), 
1028        Prs3d_i(theResult),       
1029        ScalarMap_i(theResult, theAddToStudy)
1030 {
1031   if(MYDEBUG) MESSAGE("CutPlanes_i::CutPlanes_i");
1032   myAppendPolyData = vtkAppendPolyData::New();
1033 }
1034
1035 void VISU::CutPlanes_i::Destroy(){
1036   ScalarMap_i::Destroy();
1037 }
1038
1039 void VISU::CutPlanes_i::SameAs(const CutPlanes_i* theOrigin)
1040 {
1041   CutPlanes_i* aCutPlanes = const_cast<CutPlanes_i*>(theOrigin);
1042   SetNbPlanes(aCutPlanes->GetNbPlanes());
1043   SetDisplacement(aCutPlanes->GetDisplacement());
1044   SetOrientationType(aCutPlanes->GetOrientationType());
1045   SetRotateX(aCutPlanes->GetRotateX());
1046   SetRotateY(aCutPlanes->GetRotateY());
1047   VISU::ScalarMap_i::SameAs(theOrigin);
1048 }
1049
1050 VISU::Storable* VISU::CutPlanes_i::Create(const char* theMeshName, VISU::Entity theEntity, 
1051                                           const char* theFieldName, double theIteration){
1052   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
1053 }
1054
1055 VISU::Storable* VISU::CutPlanes_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
1056      throw(std::logic_error&)
1057 {
1058   ScalarMap_i::Restore(theMap,false);
1059
1060   myNbPlanes = VISU::Storable::FindValue(theMap,"myNbPlanes").toInt();
1061   myDisplacement = VISU::Storable::FindValue(theMap,"myDisplacement").toDouble();
1062   myBasePlane = (VISU::CutPlanes::Orientation)VISU::Storable::FindValue(theMap,"myBasePlane").toInt();
1063   SetOrientationType(myBasePlane);
1064   float aRot[2];
1065   aRot[0] = VISU::Storable::FindValue(theMap,"aRot[0]").toDouble();
1066   SetRotateX(aRot[0]);
1067   aRot[1] = VISU::Storable::FindValue(theMap,"aRot[1]").toDouble();
1068   SetRotateY(aRot[1]);
1069
1070   if(theBuilding)
1071     return Build(true);
1072   return NULL;
1073 }
1074
1075 void VISU::CutPlanes_i::ToStream(std::ostringstream& theStr){
1076   ScalarMap_i::ToStream(theStr);
1077
1078   Storable::DataToStream( theStr, "myNbPlanes",  myNbPlanes );
1079   Storable::DataToStream( theStr, "myDisplacement",  myDisplacement );
1080   Storable::DataToStream( theStr, "myBasePlane", myBasePlane );
1081   Storable::DataToStream( theStr, "aRot[0]",     GetRotateX() );
1082   Storable::DataToStream( theStr, "aRot[1]",     GetRotateY() );
1083 }
1084
1085 VISU::Storable* VISU::CutPlanesRestore(SALOMEDS::SObject_ptr theSObject, 
1086                                        const string& thePrefix, const Storable::TRestoringMap& theMap)
1087      throw(std::logic_error&)
1088 {
1089   VISU::Result_i* pResult = GetResult(theSObject);
1090   if(pResult != NULL){
1091     VISU::CutPlanes_i* pResent = new VISU::CutPlanes_i(pResult);
1092     return pResent->Restore(theMap);
1093   }
1094   return NULL;
1095 }
1096
1097
1098 VISU::CutPlanes_i::~CutPlanes_i(){
1099   if(MYDEBUG) MESSAGE("CutPlanes_i::~CutPlanes_i()");
1100   myAppendPolyData->Delete();
1101 }
1102
1103
1104 float* GetIdent(float theR[3][3]){
1105   for(int i = 0; i < 3; i++)
1106     for(int j = 0; j < 3; j++)
1107       theR[i][j] = (i==j? 1.0: 0.0);
1108   return theR[0];
1109 }
1110
1111
1112 float* GetRx(float theRx[3][3], float thaAng){
1113   theRx[0][0] = 1.0;            theRx[0][1] = 0.0;            theRx[0][2] = 0.0;
1114   theRx[1][0] = 0.0;            theRx[1][1] = cos(thaAng);    theRx[1][2] = -sin(thaAng);
1115   theRx[2][0] = 0.0;            theRx[2][1] = sin(thaAng);    theRx[2][2] = cos(thaAng);
1116   return theRx[0];
1117 }
1118
1119
1120 float* GetRy(float theRy[3][3], float thaAng){
1121   theRy[0][0] = cos(thaAng);    theRy[0][1] = 0.0;            theRy[0][2] = sin(thaAng);
1122   theRy[1][0] = 0.0;            theRy[1][1] = 1.0;            theRy[1][2] = 0.0;   
1123   theRy[2][0] = -sin(thaAng);   theRy[2][1] = 0.0;            theRy[2][2] = cos(thaAng);   
1124   return theRy[0];
1125 }
1126
1127
1128 float* GetRz(float theRz[3][3], float thaAng){
1129   theRz[0][0] = cos(thaAng);    theRz[0][1] = -sin(thaAng);   theRz[0][2] = 0.0;
1130   theRz[1][0] = sin(thaAng);    theRz[1][1] = cos(thaAng);    theRz[1][2] = 0.0;   
1131   theRz[2][0] = 0.0;            theRz[2][1] = 0.0;            theRz[2][2] = 1.0;   
1132   return theRz[0];
1133 }
1134
1135
1136 void MulMatrix(float C[3][3], const float A[3][3], const float B[3][3]){ // C = A*B;
1137   for(int i = 0; i < 3; i++)
1138     for(int j = 0; j < 3; j++){
1139       C[i][j] = 0.0;
1140       for(int k = 0; k < 3; k++)
1141         C[i][j] += A[i][k]*B[k][j];
1142     }
1143 }
1144
1145
1146 void Sub(float C[3], const float A[3], float B[3]){ // AxB;
1147   for(int i = 0; i < 3; i++)  C[i] = B[i] - A[i];
1148 }
1149
1150 float DotMul(const float A[3], const float B[3]){ // A*B;
1151   float C = 0.0;
1152   for(int i = 0; i < 3; i++)  C += A[i]*B[i];
1153   return C;
1154 }
1155
1156 void Mul(float C[3], const float A[3], float B[3]){ // AxB;
1157   C[0] = A[1]*B[2] - A[2]*B[1];
1158   C[1] = A[2]*B[0] - A[0]*B[2];
1159   C[2] = A[0]*B[1] - A[1]*B[0];
1160 }
1161
1162 void Mul(float C[3], const float A[3], float B){ // A*B;
1163   for(int i = 0; i < 3; i++)  C[i] = A[i]*B;
1164 }
1165
1166 void CorrectPnt(float thePnt[3], const float BoundPrj[3]){
1167   for(int i = 0, j = 0; i < 3; ++i, j=2*i){
1168     if(thePnt[i] < BoundPrj[j]) thePnt[i] = BoundPrj[j];
1169     if(thePnt[i] > BoundPrj[j+1]) thePnt[i] = BoundPrj[j+1];
1170   }
1171 }
1172
1173 void GetBoundProject(float BoundPrj[3], const float BoundBox[6], const float Dir[3]){
1174   float BoundPoints[8][3] = { {BoundBox[0],BoundBox[2],BoundBox[4]},
1175                               {BoundBox[1],BoundBox[2],BoundBox[4]},
1176                               {BoundBox[0],BoundBox[3],BoundBox[4]},
1177                               {BoundBox[1],BoundBox[3],BoundBox[4]},
1178                               {BoundBox[0],BoundBox[2],BoundBox[5]},
1179                               {BoundBox[1],BoundBox[2],BoundBox[5]}, 
1180                               {BoundBox[0],BoundBox[3],BoundBox[5]}, 
1181                               {BoundBox[1],BoundBox[3],BoundBox[5]}};
1182   BoundPrj[0] = DotMul(Dir,BoundPoints[0]), BoundPrj[1] = BoundPrj[0];
1183   float tmp;
1184   for(int i = 1; i < 8; i++){
1185     tmp = DotMul(Dir,BoundPoints[i]);
1186     if(BoundPrj[1] < tmp) BoundPrj[1] = tmp;
1187     if(BoundPrj[0] > tmp) BoundPrj[0] = tmp;
1188   }
1189   BoundPrj[2] = BoundPrj[1] - BoundPrj[0];
1190   BoundPrj[1] = BoundPrj[0] + (1.0 - EPS)*BoundPrj[2];
1191   BoundPrj[0] = BoundPrj[0] + EPS*BoundPrj[2];
1192   BoundPrj[2] = BoundPrj[1] - BoundPrj[0];
1193 }
1194
1195 void VISU::CutPlanes_i::SetRotateX(CORBA::Double theAng){
1196   if(myBasePlane == VISU::CutPlanes::XY)
1197     myAng[0] = theAng;   
1198   else if(myBasePlane == VISU::CutPlanes::YZ)
1199     myAng[1] = theAng; 
1200   else if(myBasePlane == VISU::CutPlanes::ZX)
1201     myAng[2] = theAng;
1202 }
1203
1204 CORBA::Double VISU::CutPlanes_i::GetRotateX(){
1205   if(myBasePlane == VISU::CutPlanes::XY)
1206     return myAng[0];   
1207   else if(myBasePlane == VISU::CutPlanes::YZ)
1208     return myAng[1]; 
1209   else if(myBasePlane == VISU::CutPlanes::ZX)
1210     return myAng[2];
1211 }
1212
1213 void VISU::CutPlanes_i::SetRotateY(CORBA::Double theAng){
1214   if(myBasePlane == VISU::CutPlanes::XY)
1215     myAng[1] = theAng;   
1216   else if(myBasePlane == VISU::CutPlanes::YZ)
1217     myAng[2] = theAng; 
1218   else if(myBasePlane == VISU::CutPlanes::ZX)
1219     myAng[0] = theAng;
1220 }
1221
1222 CORBA::Double VISU::CutPlanes_i::GetRotateY(){
1223   if(myBasePlane == VISU::CutPlanes::XY)
1224     return myAng[1];   
1225   else if(myBasePlane == VISU::CutPlanes::YZ)
1226     return myAng[2]; 
1227   else if(myBasePlane == VISU::CutPlanes::ZX)
1228     return myAng[0];
1229 }
1230
1231 void GetDir(float theDir[3], float theAng[3],
1232             const VISU::CutPlanes::Orientation& theBasePlane)
1233 {
1234   int iPlane = 0;
1235   float aRx[3][3], aRy[3][3], aRz[3][3], aRotation[3][3];
1236   if(theBasePlane == VISU::CutPlanes::XY){ // X-Y
1237     if(fabs(theAng[0]) > EPS) ::GetRx(aRx,theAng[0]); else ::GetIdent(aRx);
1238     if(fabs(theAng[1]) > EPS) ::GetRy(aRy,theAng[1]); else ::GetIdent(aRy);
1239     ::MulMatrix(aRotation,aRx,aRy);
1240     iPlane = 2;
1241   }else if(theBasePlane == VISU::CutPlanes::YZ){ // Y-Z
1242     if(fabs(theAng[1]) > EPS) ::GetRy(aRy,theAng[1]); else ::GetIdent(aRy);
1243     if(fabs(theAng[2]) > EPS) ::GetRz(aRz,theAng[2]); else ::GetIdent(aRz);
1244     ::MulMatrix(aRotation,aRy,aRz);
1245      iPlane = 0;
1246   }else if(theBasePlane == VISU::CutPlanes::ZX){ // Z-X
1247     if(fabs(theAng[2]) > EPS) ::GetRz(aRz,theAng[2]); else ::GetIdent(aRz);
1248     if(fabs(theAng[0]) > EPS) ::GetRx(aRx,theAng[0]); else ::GetIdent(aRx);
1249     ::MulMatrix(aRotation,aRz,aRx);
1250     iPlane = 1;
1251   }
1252   for(int i = 0; i < 3; i++)  theDir[i] = aRotation[i][iPlane];
1253 }
1254
1255 void CutWithPlane(vtkAppendPolyData* theAppendPolyData, vtkDataSet* theDataSet,
1256                   float theDir[3], float theOrig[3])
1257 {
1258   vtkCutter *aCutPlane = vtkCutter::New();
1259   aCutPlane->SetInput(theDataSet);
1260   vtkPlane *aPlane = vtkPlane::New();
1261   aPlane->SetOrigin(theOrig);
1262
1263   aPlane->SetNormal(theDir);
1264   aCutPlane->SetCutFunction(aPlane);
1265   aPlane->Delete();
1266   theAppendPolyData->AddInput(aCutPlane->GetOutput());
1267   aCutPlane->Delete();
1268 }
1269
1270 void CutWithPlanes(vtkAppendPolyData* theAppendPolyData, vtkDataSet* theDataSet,
1271                    int theNbPlanes, float theDir[3], float theBounds[6], float theDisplacement)
1272 {
1273   float aInvDir[3], aBoundPrj[3], aOrig[3], aPosition;
1274   ::Mul(aInvDir,theDir,-1.0);
1275   ::GetBoundProject(aBoundPrj, theBounds, theDir);
1276   if(theNbPlanes > 1){
1277     float aDBoundPrj = aBoundPrj[2]/(theNbPlanes - 1);
1278     float aDisplacement = aDBoundPrj*theDisplacement;
1279     float aStartPosition = aBoundPrj[0] - 0.5*aDBoundPrj + aDisplacement;
1280     for (int i = 0; i < theNbPlanes; i++){
1281       aPosition = aStartPosition + i*aDBoundPrj;
1282       float aDelta = (aBoundPrj[0] - aPosition) / aBoundPrj[2];
1283       if(aDelta > 0)
1284         aPosition = aBoundPrj[0];
1285         //if(fabs(aDelta) < EPS) aPosition = aBoundPrj[0]; else continue;
1286       aDelta = (aBoundPrj[2] - aPosition) / aBoundPrj[2];
1287       if(aDelta < 0) 
1288         aPosition = aBoundPrj[2];
1289         //if(fabs(aDelta) < EPS) aPosition = aBoundPrj[2]; else continue;
1290       Mul(aOrig,theDir,aPosition);
1291       if(i != 0) 
1292         CutWithPlane(theAppendPolyData,theDataSet,theDir,aOrig);
1293       else
1294         CutWithPlane(theAppendPolyData,theDataSet,aInvDir,aOrig);
1295     }
1296   }else{
1297     aPosition = aBoundPrj[0] + aBoundPrj[2]*theDisplacement;
1298     Mul(aOrig,theDir,aPosition);
1299     CutWithPlane(theAppendPolyData,theDataSet,theDir,aOrig);
1300   }
1301 }
1302
1303 VISU::ScalarMap_i::DataType* VISU::CutPlanes_i::DoHook(int theRestoring){
1304   if(theRestoring == 0){
1305     myNbPlanes = 10;
1306     myDisplacement = 0.5;
1307     myBasePlane = VISU::CutPlanes::XY;
1308     myAng[0] = myAng[1] = myAng[2] = 0.0;
1309   }
1310   if(MYDEBUG) 
1311     MESSAGE("CutPlanes_i::DoHook() - myNbPlanes = "<<myNbPlanes<<"; myBasePlane = "<<myBasePlane<<
1312             "; myAng[0] = "<<myAng[0]<<"; myAng[1] = "<<myAng[1]<<"; myAng[2] = "<<myAng[2]);
1313   Update();
1314   return myAppendPolyData->GetOutput();
1315 }
1316
1317 void VISU::CutPlanes_i::Update(){
1318   int iEnd = myAppendPolyData->GetNumberOfInputs();
1319   if(MYDEBUG) MESSAGE("CutPlanes_i::Update - iEnd = "<<iEnd);
1320   for(int i = iEnd-1; i >= 0; i--)
1321     myAppendPolyData->RemoveInput(myAppendPolyData->GetInput(i));
1322   float aDir[3];
1323   ::GetDir(aDir,myAng,myBasePlane);
1324   vtkDataSet* aDataSet = myFieldTransform->GetUnstructuredGridOutput();
1325   ::CutWithPlanes(myAppendPolyData,aDataSet,GetNbPlanes(),aDir,myBounds,myDisplacement);
1326   myMapper->ScalarVisibilityOn();
1327   VISU::ScalarMap_i::Update();
1328 }
1329
1330 //==============================================================================
1331 int VISU::CutLines_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
1332                                  const char* theFieldName, double theIteration){
1333   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
1334 }
1335
1336 int VISU::CutLines_i::myNbPresent = 0;
1337 QString VISU::CutLines_i::GenerateName() { return VISU::GenerateName("CutLines",myNbPresent++);}
1338
1339 const string VISU::CutLines_i::myComment = "CUTLINES";
1340 const char* VISU::CutLines_i::GetComment() const { return myComment.c_str();}
1341
1342 VISU::CutLines_i::CutLines_i(Result_i* theResult, bool theAddToStudy) :
1343        PrsObject_i(theResult->GetStudyDocument()), 
1344        Prs3d_i(theResult),       
1345        ScalarMap_i(theResult, theAddToStudy)
1346 {
1347   if(MYDEBUG) MESSAGE("CutLines_i::CutLines_i");
1348   myAppendPolyData = vtkAppendPolyData::New();
1349 }
1350
1351 void VISU::CutLines_i::Destroy(){
1352   ScalarMap_i::Destroy();
1353 }
1354
1355 void VISU::CutLines_i::SameAs(const CutLines_i* theOrigin)
1356 {
1357   CutLines_i* aCutLines = const_cast<CutLines_i*>(theOrigin);
1358   SetNbLines(aCutLines->GetNbLines());
1359   SetDisplacement(aCutLines->GetDisplacement());
1360   SetDisplacement2(aCutLines->GetDisplacement2());
1361   SetOrientationType(aCutLines->GetOrientationType());
1362   SetOrientationType2(aCutLines->GetOrientationType2());
1363   SetRotateX(aCutLines->GetRotateX());
1364   SetRotateY(aCutLines->GetRotateY());
1365   SetRotateX2(aCutLines->GetRotateX2());
1366   SetRotateY2(aCutLines->GetRotateY2());
1367   VISU::ScalarMap_i::SameAs(theOrigin);
1368 }
1369
1370 VISU::Storable* VISU::CutLines_i::Create(const char* theMeshName, VISU::Entity theEntity, 
1371                                        const char* theFieldName, double theIteration){
1372   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
1373 }
1374
1375 VISU::Storable* VISU::CutLines_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
1376      throw(std::logic_error&)
1377 {
1378   ScalarMap_i::Restore(theMap,false);
1379   myNbLines = VISU::Storable::FindValue(theMap,"myNbLines").toInt();
1380   myDisplacement[0] = VISU::Storable::FindValue(theMap,"myDisplacement[0]").toDouble();
1381   myDisplacement[1] = VISU::Storable::FindValue(theMap,"myDisplacement[1]").toDouble();
1382   myBasePlane[0] = (VISU::CutPlanes::Orientation)VISU::Storable::FindValue(theMap,"myBasePlane[0]").toInt();
1383   myBasePlane[1] = (VISU::CutPlanes::Orientation)VISU::Storable::FindValue(theMap,"myBasePlane[1]").toInt();
1384   SetOrientationType(myBasePlane[0]);
1385   SetOrientationType2(myBasePlane[1]);
1386   float aRot[2];
1387   aRot[0] = VISU::Storable::FindValue(theMap,"aRot[0][0]").toDouble();
1388   SetRotateX(aRot[0]);
1389   aRot[1] = VISU::Storable::FindValue(theMap,"aRot[0][1]").toDouble();
1390   SetRotateY(aRot[1]);
1391   aRot[0] = VISU::Storable::FindValue(theMap,"aRot[1][0]").toDouble();
1392   SetRotateX2(aRot[0]);
1393   aRot[1] = VISU::Storable::FindValue(theMap,"aRot[1][1]").toDouble();
1394   SetRotateY2(aRot[1]);
1395   if(theBuilding)
1396     return Build(true);
1397   return NULL;
1398 }
1399
1400 void VISU::CutLines_i::ToStream(std::ostringstream& theStr){
1401   ScalarMap_i::ToStream(theStr);
1402   Storable::DataToStream( theStr, "myNbLines",      myNbLines );
1403   Storable::DataToStream( theStr, "myDisplacement[0]", myDisplacement[0] );
1404   Storable::DataToStream( theStr, "myDisplacement[1]", myDisplacement[1] );
1405   Storable::DataToStream( theStr, "myBasePlane[0]", myBasePlane[0] );
1406   Storable::DataToStream( theStr, "myBasePlane[1]", myBasePlane[1] );
1407   Storable::DataToStream( theStr, "aRot[0][0]",     GetRotateX() );
1408   Storable::DataToStream( theStr, "aRot[0][1]",     GetRotateY() );
1409   Storable::DataToStream( theStr, "aRot[1][0]",     GetRotateX2() );
1410   Storable::DataToStream( theStr, "aRot[1][1]",     GetRotateY2() );
1411 }
1412
1413 VISU::Storable* VISU::CutLinesRestore(SALOMEDS::SObject_ptr theSObject, 
1414                                       const string& thePrefix, const Storable::TRestoringMap& theMap)
1415      throw(std::logic_error&)
1416 {
1417   VISU::Result_i* pResult = GetResult(theSObject);
1418   if(pResult != NULL){
1419     VISU::CutLines_i* pResent = new VISU::CutLines_i(pResult);
1420     return pResent->Restore(theMap);
1421   }
1422   return NULL;
1423 }
1424
1425
1426 VISU::CutLines_i::~CutLines_i(){
1427   if(MYDEBUG) MESSAGE("CutLines_i::~CutLines_i()");
1428   myAppendPolyData->Delete();
1429 }
1430
1431 void VISU::CutLines_i::SetRotateX(CORBA::Double theAng){
1432   if(myBasePlane[0] == VISU::CutPlanes::XY)
1433     myAng[0][0] = theAng;   
1434   else if(myBasePlane[0] == VISU::CutPlanes::YZ)
1435     myAng[0][1] = theAng; 
1436   else if(myBasePlane[0] == VISU::CutPlanes::ZX)
1437     myAng[0][2] = theAng;
1438 }
1439
1440 CORBA::Double VISU::CutLines_i::GetRotateX(){
1441   if(myBasePlane[0] == VISU::CutPlanes::XY)
1442     return myAng[0][0];   
1443   else if(myBasePlane[0] == VISU::CutPlanes::YZ)
1444     return myAng[0][1]; 
1445   else if(myBasePlane[0] == VISU::CutPlanes::ZX)
1446     return myAng[0][2];
1447 }
1448
1449 void VISU::CutLines_i::SetRotateY(CORBA::Double theAng){
1450   if(myBasePlane[0] == VISU::CutPlanes::XY)
1451     myAng[0][1] = theAng;   
1452   else if(myBasePlane[0] == VISU::CutPlanes::YZ)
1453     myAng[0][2] = theAng; 
1454   else if(myBasePlane[0] == VISU::CutPlanes::ZX)
1455     myAng[0][0] = theAng;
1456 }
1457
1458 CORBA::Double VISU::CutLines_i::GetRotateY(){
1459   if(myBasePlane[0] == VISU::CutPlanes::XY)
1460     return myAng[0][1];   
1461   else if(myBasePlane[0] == VISU::CutPlanes::YZ)
1462     return myAng[0][2]; 
1463   else if(myBasePlane[0] == VISU::CutPlanes::ZX)
1464     return myAng[0][0];
1465 }
1466
1467 void VISU::CutLines_i::SetRotateX2(CORBA::Double theAng){
1468   if(myBasePlane[1] == VISU::CutPlanes::XY)
1469     myAng[1][0] = theAng;   
1470   else if(myBasePlane[1] == VISU::CutPlanes::YZ)
1471     myAng[1][1] = theAng; 
1472   else if(myBasePlane[1] == VISU::CutPlanes::ZX)
1473     myAng[1][2] = theAng;
1474 }
1475
1476 CORBA::Double VISU::CutLines_i::GetRotateX2(){
1477   if(myBasePlane[1] == VISU::CutPlanes::XY)
1478     return myAng[1][0];   
1479   else if(myBasePlane[1] == VISU::CutPlanes::YZ)
1480     return myAng[1][1]; 
1481   else if(myBasePlane[1] == VISU::CutPlanes::ZX)
1482     return myAng[1][2];
1483 }
1484
1485 void VISU::CutLines_i::SetRotateY2(CORBA::Double theAng){
1486   if(myBasePlane[1] == VISU::CutPlanes::XY)
1487     myAng[1][1] = theAng;   
1488   else if(myBasePlane[1] == VISU::CutPlanes::YZ)
1489     myAng[1][2] = theAng; 
1490   else if(myBasePlane[1] == VISU::CutPlanes::ZX)
1491     myAng[1][0] = theAng;
1492 }
1493
1494 CORBA::Double VISU::CutLines_i::GetRotateY2(){
1495   if(myBasePlane[1] == VISU::CutPlanes::XY)
1496     return myAng[1][1];   
1497   else if(myBasePlane[1] == VISU::CutPlanes::YZ)
1498     return myAng[1][2]; 
1499   else if(myBasePlane[1] == VISU::CutPlanes::ZX)
1500     return myAng[1][0];
1501 }
1502
1503 VISU::ScalarMap_i::DataType* VISU::CutLines_i::DoHook(int theRestoring){
1504   if(theRestoring == 0){
1505     myNbLines = 10;
1506     myDisplacement[0] = myDisplacement[1] = 0.5;
1507     myAng[0][0] = myAng[0][1] = myAng[0][2] = 0.0;
1508     myAng[1][0] = myAng[1][1] = myAng[1][2] = 0.0;
1509     myBasePlane[0] = VISU::CutPlanes::ZX;
1510     myBasePlane[1] = VISU::CutPlanes::XY;
1511   }
1512   Update();
1513   return myAppendPolyData->GetOutput();
1514 }
1515
1516 void ClearAppendPolyData(vtkAppendPolyData *theAppendPolyData){
1517   int iEnd = theAppendPolyData->GetNumberOfInputs();
1518   for(int i = iEnd-1; i >= 0; i--)
1519     theAppendPolyData->RemoveInput(theAppendPolyData->GetInput(i));
1520 }
1521
1522 void VISU::CutLines_i::Update(){
1523   ::ClearAppendPolyData(myAppendPolyData);
1524   vtkAppendPolyData *anAppendPolyData = vtkAppendPolyData::New();
1525   //Build base plane
1526   float aDir[2][3];
1527   ::GetDir(aDir[0],myAng[0],myBasePlane[0]);
1528   ::CutWithPlanes(anAppendPolyData,myFieldTransform->GetUnstructuredGridOutput(),1,aDir[0],myBounds,myDisplacement[0]);
1529   anAppendPolyData->Update();
1530   vtkPolyData *aPolyData = anAppendPolyData->GetOutput();
1531   if(MYDEBUG) MESSAGE("CutPlanes_i::Update - aPolyData->GetNumberOfCells() = "<<aPolyData->GetNumberOfCells());
1532   if(aPolyData->GetNumberOfCells() == 0){
1533     ::ClearAppendPolyData(anAppendPolyData);
1534     vtkGeometryFilter *aGeomFilter = vtkGeometryFilter::New();
1535     aGeomFilter->SetInput(myFieldTransform->GetUnstructuredGridOutput());
1536     anAppendPolyData->AddInput(aGeomFilter->GetOutput());
1537     aGeomFilter->Delete();
1538     anAppendPolyData->Update();
1539   }
1540   //Build lines
1541   float aBounds[6];
1542   aPolyData = anAppendPolyData->GetOutput();
1543   aPolyData->GetBounds(aBounds);
1544   ::GetDir(aDir[1],myAng[1],myBasePlane[1]);
1545   ::CutWithPlanes(myAppendPolyData,aPolyData,GetNbLines(),aDir[1],aBounds,myDisplacement[1]);
1546   anAppendPolyData->Delete();
1547   //Calculate values for building of table
1548   ::Mul(myDirLn,aDir[0],aDir[1]); 
1549   ::GetBoundProject(myBoundPrjLn, myBounds, myDirLn);
1550   Mul(myBasePnt,myDirLn,myBoundPrjLn[0]);
1551   CorrectPnt(myBasePnt,myBounds);
1552   myMapper->ScalarVisibilityOn();
1553   VISU::ScalarMap_i::Update();
1554 }
1555
1556 void VISU::CutLines_i::BuildTableOfReal(SALOMEDS::SObject_ptr theSObject){
1557   if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal");
1558   Update();
1559   SALOMEDS::GenericAttribute_var anAttr;
1560   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
1561   anAttr = aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributeTableOfReal");
1562   SALOMEDS::AttributeTableOfReal_var aTableOfReal = SALOMEDS::AttributeTableOfReal::_narrow(anAttr);
1563
1564   typedef set<long> TLineCont;
1565   typedef map<float,TLineCont> TXMap;
1566   typedef map<float,float> TXYMap;
1567   typedef vector<TXYMap> TCurveVect;
1568
1569   const VISU::TField::TValField& aValField = myField->myValField;
1570   const VISU::TField::TValForTime& aValForTime = aValField.find((int)myIteration)->second;
1571   const VISU::TField::TTime& aTime = aValForTime.myTime;
1572   QString aTitle;
1573   //aTitle.sprintf("%s %s",myTitle.c_str(),aTime.second.c_str());
1574   aTitle.sprintf("%s",myTitle.c_str());
1575   aTitle = aTitle.simplifyWhiteSpace();
1576   aTableOfReal->SetTitle(aTitle.latin1());
1577
1578   myAppendPolyData->Update();
1579   int iLineEnd = myAppendPolyData->GetNumberOfInputs();
1580   if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLineEnd = "<<iLineEnd);
1581   TCurveVect aCurveVect(iLineEnd); 
1582   TLineCont aLineCont;
1583   TXMap aXMap;
1584   for(int iLine = 0; iLine < iLineEnd; iLine++){
1585     vtkDataSet *aDataSet = myAppendPolyData->GetInput(iLine);
1586     aDataSet->Update();
1587     int aNbPoints = aDataSet->GetNumberOfPoints();
1588     if(MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal iLine = "<<iLine<<"; aNbPoints = "<<aNbPoints);
1589     vtkPointData *aPointData = aDataSet->GetPointData();
1590     vtkDataArray *aScalars = aPointData->GetScalars();
1591     vtkCellDataToPointData *aFilter = NULL;
1592     if(!aScalars) {
1593       aFilter = vtkCellDataToPointData::New();
1594       aFilter->SetInput(aDataSet);
1595       aFilter->PassCellDataOn();
1596       aDataSet = aFilter->GetOutput();
1597       aDataSet->Update();
1598     }
1599     aPointData = aDataSet->GetPointData();
1600     aScalars = aPointData->GetScalars();
1601     if(!aScalars) continue;
1602     aLineCont.insert(iLine);
1603     TXYMap& aXYMap = aCurveVect[iLine];
1604     float aPnt[3], aVect[3], aDist;
1605     for(int i = 0; i < aNbPoints; i++){
1606       aDataSet->GetPoint(i,aPnt);
1607       Sub(aVect,myBasePnt,aPnt);
1608       aDist = DotMul(aVect,myDirLn) / myBoundPrjLn[2];
1609       // the workaround
1610       if(aDist < 0.0) aDist = 0.0; 
1611       if(aDist > 1.0) aDist = 1.0;
1612       aXYMap[aDist] = aScalars->GetTuple1(i);
1613       //aXMap[aDist].insert(iLine);
1614     }
1615     if(aFilter) aFilter->Delete();
1616   }
1617   iLineEnd = aLineCont.size();
1618   if(iLineEnd == 0){
1619     MESSAGE("CutPlanes_i::BuildTableOfReal aLineCont.size() == 0 !!!");
1620     return;
1621   }
1622   //Resorting of theXYMap
1623   for(int iLine = 0; iLine < iLineEnd; iLine++){
1624     TXYMap& aXYMap = aCurveVect[iLine], aNewXYMap;
1625     if(aXYMap.size() > 2){
1626       TXYMap::const_iterator aXYMapIter[2] = {aXYMap.begin(), ++aXYMap.begin()};
1627       aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
1628       for(; aXYMapIter[1] != aXYMap.end(); aXYMapIter[0]++, aXYMapIter[1]++){
1629         float aY[3] = {aXYMapIter[0]->second, aXYMapIter[1]->second, 0.0};
1630         aY[2] = (aY[0] + aY[1])/2.0;
1631         float aX[3] = {aXYMapIter[0]->first, aXYMapIter[1]->first, 0.0};
1632         aX[2] = (aX[0] + aX[1])/2.0;
1633         aNewXYMap[aX[2]] = aY[2];
1634         aXMap[aX[2]].insert(iLine);
1635       }
1636       aNewXYMap[aXYMapIter[0]->first] = aXYMapIter[0]->second;
1637       aXYMap = aNewXYMap;
1638     }
1639   }
1640   QString aString;
1641   aTableOfReal->SetNbColumns(aXMap.size());
1642   TXMap::const_iterator aXMapIter = aXMap.begin();
1643   for(long i = 0; aXMapIter != aXMap.end(); aXMapIter++, i++){
1644     float aDist = aXMapIter->first; 
1645     aTableOfReal->PutValue(aDist,1,i+1);
1646     aString.sprintf("%d",i);
1647     aTableOfReal->SetColumnTitle(i+1,aString.latin1());
1648     const TLineCont& aLineCont = aXMapIter->second;
1649     long jEnd = aLineCont.size();
1650     if(0&& MYDEBUG) MESSAGE("CutPlanes_i::BuildTableOfReal aDist = "<<aDist<<"; jEnd = "<<jEnd);
1651     TLineCont::const_iterator aLineContIter = aLineCont.begin();
1652     for(; aLineContIter != aLineCont.end(); aLineContIter++){
1653       long iLine = *aLineContIter;
1654       const TXYMap& aXYMap = aCurveVect[iLine];
1655       TXYMap::const_iterator aXYMapIter = aXYMap.find(aDist);
1656       if(aXYMapIter == aXYMap.end()) continue;
1657       float aVal = aXYMapIter->second;
1658       aTableOfReal->PutValue(aVal,iLine+2,i+1);
1659     }
1660   }
1661   
1662   aTableOfReal->SetRowTitle(1,"X");
1663   TLineCont::const_iterator aLineContIter = aLineCont.begin();
1664   QString aUnitName = myField->myUnitNames[0].c_str();
1665   if(myScalarMode != 0) 
1666     aUnitName = myField->myUnitNames[myScalarMode-1].c_str();
1667   aUnitName = aUnitName.simplifyWhiteSpace();
1668   for(long i = 0; aLineContIter != aLineCont.end(); aLineContIter++, i++){
1669     long iLine = *aLineContIter;
1670     aString.sprintf("Y%d",iLine);
1671     aTableOfReal->SetRowTitle(i+2,aString.latin1());
1672     aTableOfReal->SetRowUnit(i+2,aUnitName.latin1());
1673   }
1674 }
1675
1676 //==============================================================================
1677 int VISU::IsoSurfaces_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
1678                                     const char* theFieldName, double theIteration){
1679   return VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
1680 }
1681
1682 int VISU::IsoSurfaces_i::myNbPresent = 0;
1683 QString VISU::IsoSurfaces_i::GenerateName() { return VISU::GenerateName("IsoSurfaces",myNbPresent++);}
1684
1685 const string VISU::IsoSurfaces_i::myComment = "ISOSURFACES";
1686 const char* VISU::IsoSurfaces_i::GetComment() const { return myComment.c_str();}
1687
1688 VISU::IsoSurfaces_i::IsoSurfaces_i(Result_i* theResult, bool theAddToStudy) :
1689        PrsObject_i(theResult->GetStudyDocument()), 
1690        Prs3d_i(theResult),       
1691        ScalarMap_i(theResult, theAddToStudy)
1692 {
1693   if(MYDEBUG) MESSAGE("IsoSurfaces_i::IsoSurfaces_i");
1694   myContourFilter = vtkContourFilter::New();
1695 }
1696
1697 void VISU::IsoSurfaces_i::Destroy(){
1698   ScalarMap_i::Destroy();
1699 }
1700
1701 void VISU::IsoSurfaces_i::SameAs(const IsoSurfaces_i* theOrigin) {
1702   IsoSurfaces_i* aIsoSurfaces = const_cast<IsoSurfaces_i*>(theOrigin);
1703   SetNbSurfaces(aIsoSurfaces->GetNbSurfaces());
1704   SetRange(aIsoSurfaces->GetRangeMin(), aIsoSurfaces->GetRangeMax());
1705   VISU::ScalarMap_i::SameAs(theOrigin);
1706 }
1707
1708 VISU::Storable* VISU::IsoSurfaces_i::Create(const char* theMeshName, VISU::Entity theEntity, 
1709                                             const char* theFieldName, double theIteration){
1710   myNbSurface = 10;
1711   myRange[0] = 0;
1712   myRange[1] = 0;
1713   return ScalarMap_i::Create(theMeshName,theEntity,theFieldName,theIteration);
1714 }
1715
1716 VISU::Storable* VISU::IsoSurfaces_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
1717      throw(std::logic_error&)
1718 {
1719   ScalarMap_i::Restore(theMap,false);
1720
1721   myNbSurface = VISU::Storable::FindValue(theMap,"myNbSurface").toInt();
1722   myRange[0] = VISU::Storable::FindValue(theMap,"myRange[0]").toDouble();
1723   myRange[1] = VISU::Storable::FindValue(theMap,"myRange[1]").toDouble();
1724
1725   if(theBuilding)
1726     return Build(true);
1727   return NULL;
1728 }
1729
1730 void VISU::IsoSurfaces_i::ToStream(std::ostringstream& theStr){
1731   ScalarMap_i::ToStream(theStr);
1732
1733   Storable::DataToStream( theStr, "myNbSurface", myNbSurface );
1734   Storable::DataToStream( theStr, "myRange[0]", myRange[0] );
1735   Storable::DataToStream( theStr, "myRange[1]", myRange[1] );
1736 }
1737
1738 VISU::Storable* VISU::IsoSurfacesRestore(SALOMEDS::SObject_ptr theSObject, 
1739                                          const string& thePrefix, const Storable::TRestoringMap& theMap)
1740      throw(std::logic_error&)
1741 {
1742   VISU::Result_i* pResult = GetResult(theSObject);
1743   if(pResult != NULL){
1744     VISU::IsoSurfaces_i* pResent = new VISU::IsoSurfaces_i(pResult);
1745     return pResent->Restore(theMap);
1746   }
1747   return NULL;
1748 }
1749
1750
1751 VISU::IsoSurfaces_i::~IsoSurfaces_i(){
1752   if(MYDEBUG) MESSAGE("IsoSurfaces_i::~IsoSurfaces_i()");
1753   myContourFilter->Delete();
1754 }
1755
1756 void VISU::IsoSurfaces_i::SetRange(float theMin, float theMax){ 
1757   if(theMin <= theMax){
1758     myRange[0] = theMin;  myRange[1] = theMax; 
1759   }
1760 }
1761
1762 VISU::ScalarMap_i::DataType* VISU::IsoSurfaces_i::DoHook(int theRestoring){
1763   if(MYDEBUG)  MESSAGE("IsoSurfaces_i::DoHook() - myNbSurface = "<<myNbSurface);
1764   if(theRestoring == 0) {
1765     myRange[0] = myScalarRange[0];
1766     myRange[1] = myScalarRange[1];
1767   }
1768   Update();
1769   return myContourFilter->GetOutput();
1770 }
1771
1772
1773 void VISU::IsoSurfaces_i::Update(){
1774   if(myField->myEntity == VISU::CELL_ENTITY){
1775     vtkCellDataToPointData *aFilter = vtkCellDataToPointData::New();
1776     aFilter->SetInput(myFieldTransform->GetUnstructuredGridOutput());
1777     aFilter->PassCellDataOn();
1778     myContourFilter->SetInput(aFilter->GetOutput());
1779     aFilter->Delete();
1780   } else
1781     myContourFilter->SetInput(myFieldTransform->GetUnstructuredGridOutput());
1782   int aNbSurface = (int)(myMapScale*myNbSurface);
1783   if(aNbSurface < 1) aNbSurface = 1;
1784   if(GetScaling() == VISU::LOGARITHMIC){ 
1785     float aLogRange[2];
1786     VISU_LookupTable::ComputeLogRange(myRange,aLogRange);
1787     myContourFilter->GenerateValues(aNbSurface,aLogRange);
1788   }else
1789     myContourFilter->GenerateValues(aNbSurface,myRange);
1790   myMapper->ScalarVisibilityOn();
1791   VISU::ScalarMap_i::Update();
1792 }
1793
1794 void VISU::IsoSurfaces_i::SetMapScale(double theMapScale){
1795   myMapScale = theMapScale;
1796   Update();
1797 }
1798
1799 //==============================================================================
1800 int VISU::StreamLines_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
1801                                     const char* theFieldName, double theIteration){
1802   return DeformedShape_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
1803 }
1804
1805 int VISU::StreamLines_i::myNbPresent = 0;
1806 QString VISU::StreamLines_i::GenerateName() { return VISU::GenerateName("StreamLines",myNbPresent++);}
1807
1808 const string VISU::StreamLines_i::myComment = "STREAMLINES";
1809 const char* VISU::StreamLines_i::GetComment() const { return myComment.c_str();}
1810
1811 VISU::StreamLines_i::StreamLines_i(Result_i* theResult, bool theAddToStudy) :
1812        PrsObject_i(theResult->GetStudyDocument()), 
1813        Prs3d_i(theResult),       
1814        ScalarMap_i(theResult, theAddToStudy),
1815        DeformedShape_i(theResult, theAddToStudy)
1816 {
1817   if(MYDEBUG) MESSAGE("StreamLines_i::StreamLines_i");
1818   myStream = vtkStreamLine::New();
1819 }
1820
1821 void VISU::StreamLines_i::Destroy(){
1822   DeformedShape_i::Destroy();
1823 }
1824
1825 VISU::Storable* VISU::StreamLines_i::Create(const char* theMeshName, VISU::Entity theEntity, 
1826                                             const char* theFieldName, double theIteration)
1827 {
1828   return DeformedShape_i::Create(theMeshName,theEntity,theFieldName,theIteration);
1829 }
1830
1831 void VISU::StreamLines_i::SameAs(const StreamLines_i* theOrigin) {
1832   myPrs3di = NULL;
1833   StreamLines_i* aIsoLines = const_cast<StreamLines_i*>(theOrigin);
1834   SetDirection(aIsoLines->GetDirection());
1835   SetStepLength(aIsoLines->GetStepLength());
1836   SetIntegrationStep(aIsoLines->GetIntegrationStep());
1837   SetPropagationTime(aIsoLines->GetPropagationTime());
1838   VISU::DeformedShape_i::SameAs(theOrigin);
1839   SetUsedPoints(aIsoLines->GetUsedPoints());
1840 }
1841
1842 VISU::Storable* VISU::StreamLines_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
1843      throw(std::logic_error&)
1844 {
1845   DeformedShape_i::Restore(theMap,false);
1846
1847   myStepLength = VISU::Storable::FindValue(theMap,"myStepLength").toDouble();
1848   myIntegrationStep = VISU::Storable::FindValue(theMap,"myIntegrationStep").toDouble();
1849   myPropagationTime = VISU::Storable::FindValue(theMap,"myPropagationTime").toDouble();
1850   myDirection = (VISU::StreamLines::Direction) VISU::Storable::FindValue(theMap,"myDirection").toInt();
1851   mySourceEntry = VISU::Storable::FindValue(theMap,"mySourceEntry").latin1();
1852   myPercents = VISU::Storable::FindValue(theMap,"myPercents").toDouble();
1853   myPrs3di = NULL;
1854
1855   if(theBuilding)
1856     return Build(true);
1857   return NULL;
1858 }
1859
1860 void VISU::StreamLines_i::ToStream(std::ostringstream& theStr){
1861   DeformedShape_i::ToStream(theStr);
1862
1863   Storable::DataToStream( theStr, "myStepLength", myStepLength );
1864   Storable::DataToStream( theStr, "myIntegrationStep", myIntegrationStep );
1865   Storable::DataToStream( theStr, "myPropagationTime", myPropagationTime );
1866   Storable::DataToStream( theStr, "myDirection", (int)myDirection );
1867   Storable::DataToStream( theStr, "mySourceEntry", mySourceEntry.c_str());
1868   Storable::DataToStream( theStr, "myPercents", myPercents );
1869
1870 }
1871
1872 VISU::Storable* VISU::StreamLinesRestore(SALOMEDS::SObject_ptr theSObject, 
1873                                          const string& thePrefix, const Storable::TRestoringMap& theMap)
1874      throw(std::logic_error&)
1875 {
1876   VISU::Result_i* pResult = GetResult(theSObject);
1877   if(pResult != NULL){
1878     VISU::StreamLines_i* pResent = new VISU::StreamLines_i(pResult);
1879     return pResent->Restore(theMap);
1880   }
1881   return NULL;
1882 }
1883
1884
1885 VISU::StreamLines_i::~StreamLines_i(){
1886   if(MYDEBUG) MESSAGE("StreamLines_i::~StreamLines_i()");
1887   myStream->Delete();
1888 }
1889
1890 void VISU::StreamLines_i::SetSource(VISU::Prs3d_ptr thePrs3d){
1891   if(thePrs3d->_is_nil()) {
1892     myPrs3di = NULL;
1893     mySourceEntry = "";
1894   } else {
1895     myPrs3di = dynamic_cast<VISU::Prs3d_i*>(GetServant(thePrs3d));
1896     CORBA::String_var aString = thePrs3d->GetID();
1897     SALOMEDS::SObject_var aSObject = myStudy->FindObjectIOR(aString);
1898     aString = aSObject->GetID();
1899     mySourceEntry = aString.in();
1900   }
1901 }
1902
1903 VISU::Prs3d_ptr VISU::StreamLines_i::GetSource(){
1904   VISU::Prs3d_var aPrs3d;
1905   if(MYDEBUG) MESSAGE("StreamLines_i::GetSource() mySourceEntry = '"<<mySourceEntry<<"'");
1906   if(mySourceEntry != ""){
1907     SALOMEDS::SObject_var aSObject = myStudy->FindObjectID(mySourceEntry.c_str());
1908     CORBA::Object_var anObj = SObjectToObject(aSObject);
1909     if(!CORBA::is_nil(anObj)){
1910       aPrs3d = VISU::Prs3d::_narrow(anObj);
1911       if(!aPrs3d->_is_nil())
1912         myPrs3di = dynamic_cast<VISU::Prs3d_i*>(GetServant(aPrs3d.in()));
1913     }
1914   }
1915   return aPrs3d._retn();
1916 }
1917
1918 // in this method we calculate minimal width of the dataset
1919 // at least three integration steps could be in this width
1920 float VISU::StreamLines_i::GetMaxIntegrationStep() {
1921   float aMaxSizeY = fabs(myBounds[2] - myBounds[3]);
1922   float aMaxSizeZ = fabs(myBounds[4] - myBounds[5]);
1923   float aMinMax = fabs(myBounds[0] - myBounds[1]);
1924   if (aMinMax < 1.0e-20 || (aMaxSizeY < aMinMax && aMaxSizeY > 1.0e-20)) aMinMax = aMaxSizeY;
1925   if (aMinMax < 1.0e-20 || (aMaxSizeZ < aMinMax && aMaxSizeZ > 1.0e-20)) aMinMax = aMaxSizeZ;
1926   return aMinMax / 2.0;
1927 }
1928
1929 float VISU::StreamLines_i::GetMinIntegrationStep() {
1930   float aVolume = 1;
1931   float degree = 0;
1932   if (fabs(myBounds[0] - myBounds[1]) > 1.0e-20 ) {
1933     aVolume *= fabs(myBounds[0] - myBounds[1]);
1934     degree+=1.0;
1935   }
1936   if (fabs(myBounds[2] - myBounds[3]) > 1.0e-20 ) {
1937     aVolume *= fabs(myBounds[2] - myBounds[3]);
1938     degree+=1.0;
1939   }
1940   if (fabs(myBounds[4] - myBounds[5]) > 1.0e-20 ) {
1941     aVolume *= fabs(myBounds[4] - myBounds[5]);
1942     degree+=1.0;
1943   }
1944   if (degree < 0.5) return 0.0; // absolutely empty object
1945   float anAverageVolume = aVolume / 1.0e+8; // we have to use no more, than 1GB memory (approx)
1946   return pow((double)(anAverageVolume), (double)(1.0 / degree));
1947 }
1948
1949 float VISU::StreamLines_i::GetBasePropagationTime() {
1950   return myFieldTransform->GetUnstructuredGridOutput()->GetLength() / (GetMax() + GetMin()) * 2.0;
1951 }
1952
1953 VISU::ScalarMap_i::DataType* VISU::StreamLines_i::DoHook(int theRestoring){
1954   if(theRestoring == 0){
1955     myDirection = VISU::StreamLines::BOTH;
1956     vtkDataSet *aDataSet = myFieldTransform->GetUnstructuredGridOutput();
1957     aDataSet->Update();
1958
1959     myIntegrationStep = GetMaxIntegrationStep() / 10.0;
1960     float aMinMax = aDataSet->GetLength() /
1961       (sqrt((float)(aDataSet->GetNumberOfPoints() + aDataSet->GetNumberOfCells()))+1.0);
1962     if (aMinMax > myIntegrationStep) myIntegrationStep = (myIntegrationStep * 9.0 + aMinMax) / 10.0;
1963     if (myIntegrationStep < GetMinIntegrationStep()) myIntegrationStep = GetMinIntegrationStep();
1964
1965     myPropagationTime = GetBasePropagationTime();
1966     myStepLength = myPropagationTime / 20.;
1967     if (myStepLength > myIntegrationStep * 2) myStepLength = myIntegrationStep * 2;
1968     if (myStepLength  < myPropagationTime / 100.) myStepLength = myPropagationTime / 100.;
1969
1970     myPrs3di = NULL;
1971     myPercents = 0.3;
1972   }
1973   Update();
1974   return myStream->GetOutput();
1975 }
1976
1977
1978 void SetStreamerSource(vtkStreamer *theStreamer, vtkDataSet* theDataSet, 
1979                        VISU::Prs3d_i* thePrs3di, float thePercents)
1980 {
1981   if(thePrs3di)
1982     theDataSet = thePrs3di->GetMapper()->GetInput();
1983   theDataSet->Modified();
1984   theDataSet->Update();
1985   vtkMaskPoints *aMaskPoints = vtkMaskPoints::New();
1986   aMaskPoints->SetInput(theDataSet);
1987   //aMaskPoints->RandomModeOn();
1988   int aNbPoints = (int)(theDataSet->GetNumberOfPoints()*thePercents);
1989   if(MYDEBUG) MESSAGE("SetStreamerSource aNbPoints = "<<theDataSet->GetNumberOfPoints()<<";\t"<<aNbPoints);
1990   aMaskPoints->SetMaximumNumberOfPoints(aNbPoints);
1991   theStreamer->SetSource(aMaskPoints->GetOutput());
1992   aMaskPoints->Delete();
1993 }
1994
1995 void VISU::StreamLines_i::Update(){
1996   //apo - myStream->DebugOn();
1997   VISU::Prs3d_var aPrs3d = GetSource();
1998   if(myField->myNbComp > 1){
1999     myStream->SetStepLength(myStepLength);
2000     myStream->SetIntegrationStepLength(myIntegrationStep);
2001     myStream->SetMaximumPropagationTime(myPropagationTime);
2002     if(myField->myEntity == VISU::CELL_ENTITY){
2003       vtkCellDataToPointData *aFilter = vtkCellDataToPointData::New();
2004       aFilter->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2005       aFilter->PassCellDataOn();
2006       myStream->SetInput(aFilter->GetOutput());
2007       aFilter->Delete();
2008       vtkCellCenters *centers = vtkCellCenters::New(); // for vectors on cells
2009       centers->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2010       centers->VertexCellsOn();
2011       //myStream->SetSource(centers->GetOutput());
2012       SetStreamerSource(myStream,centers->GetOutput(),myPrs3di,myPercents);
2013       centers->Delete();
2014     }else{
2015       myStream->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2016       //myStream->SetSource(myFieldTransform->GetUnstructuredGridOutput());
2017       SetStreamerSource(myStream,myFieldTransform->GetUnstructuredGridOutput(),myPrs3di,myPercents);
2018     }
2019     switch (myDirection) {
2020     case VISU::StreamLines::FORWARD:
2021       myStream->SetIntegrationDirectionToForward();
2022       break;
2023     case VISU::StreamLines::BACKWARD:
2024       myStream->SetIntegrationDirectionToBackward();
2025       break;
2026     case VISU::StreamLines::BOTH:
2027       myStream->SetIntegrationDirectionToIntegrateBothDirections();
2028     }
2029   }
2030   myStream->Modified();
2031   VISU::DeformedShape_i::Update();
2032 }
2033
2034 //==============================================================================
2035 int VISU::Vectors_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
2036                                 const char* theFieldName, double theIteration){
2037   return DeformedShape_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration);
2038 }
2039
2040 int VISU::Vectors_i::myNbPresent = 0;
2041 QString VISU::Vectors_i::GenerateName() { return VISU::GenerateName("Vectors",myNbPresent++);}
2042
2043 const string VISU::Vectors_i::myComment = "VECTORS";
2044 const char* VISU::Vectors_i::GetComment() const { return myComment.c_str();}
2045
2046 VISU::Vectors_i::Vectors_i(Result_i* theResult, bool theAddToStudy) :
2047        PrsObject_i(theResult->GetStudyDocument()), 
2048        Prs3d_i(theResult),       
2049        ScalarMap_i(theResult, theAddToStudy),
2050        DeformedShape_i(theResult, theAddToStudy)
2051 {
2052   if(MYDEBUG) MESSAGE("Vectors_i::Vectors_i");
2053   myGlyph = vtkGlyph3D::New();
2054   myHog = vtkHedgeHog::New();
2055 }
2056
2057 void VISU::Vectors_i::Destroy(){
2058   DeformedShape_i::Destroy();
2059 }
2060
2061 void VISU::Vectors_i::SameAs(const VISU::Vectors_i* theOrigin) {
2062   Vectors_i* aVectors = const_cast<Vectors_i*>(theOrigin);
2063   SetLineWidth(aVectors->GetLineWidth());
2064   SetGlyphType(aVectors->GetGlyphType());
2065   SetGlyphPos(aVectors->GetGlyphPos());
2066   VISU::DeformedShape_i::SameAs(theOrigin);
2067 }
2068
2069 VISU::Storable* VISU::Vectors_i::Create(const char* theMeshName, VISU::Entity theEntity, 
2070                                         const char* theFieldName, double theIteration){
2071   myLineWidth = 1.0;
2072   myTypeGlyph = VISU::Vectors::ARROW;
2073   myPosGlyph = VISU::Vectors::CENTER;
2074   return DeformedShape_i::Create(theMeshName,theEntity,theFieldName,theIteration);
2075 }
2076
2077 VISU::Storable* VISU::Vectors_i::Restore(const Storable::TRestoringMap& theMap, int theBuilding)
2078      throw(std::logic_error&)
2079 {
2080   DeformedShape_i::Restore(theMap,false);
2081
2082   myTypeGlyph = (VISU::Vectors::GlyphType)VISU::Storable::FindValue(theMap,"myTypeGlyph").toInt();
2083   myLineWidth = VISU::Storable::FindValue(theMap,"myLineWidth").toDouble();
2084   myPosGlyph = (VISU::Vectors::GlyphPos)VISU::Storable::FindValue(theMap,"myPosGlyph").toInt();
2085
2086   if(theBuilding)
2087     return Build(true);
2088   return NULL;
2089 }
2090
2091 void VISU::Vectors_i::ToStream(std::ostringstream& theStr){
2092   DeformedShape_i::ToStream(theStr);
2093
2094   Storable::DataToStream( theStr, "myTypeGlyph", myTypeGlyph );
2095   Storable::DataToStream( theStr, "myLineWidth", myLineWidth );
2096   Storable::DataToStream( theStr, "myPosGlyph",  myPosGlyph );
2097 }
2098
2099 VISU::Storable* VISU::VectorsRestore(SALOMEDS::SObject_ptr theSObject, 
2100                                      const string& thePrefix, const Storable::TRestoringMap& theMap)
2101      throw(std::logic_error&)
2102 {
2103   VISU::Result_i* pResult = GetResult(theSObject);
2104   if(pResult != NULL){
2105     VISU::Vectors_i* pResent = new VISU::Vectors_i(pResult);
2106     return pResent->Restore(theMap);
2107   }
2108   return NULL;
2109 }
2110
2111
2112 VISU::ScalarMap_i::DataType* VISU::Vectors_i::DoHook(int theRestoring){
2113   if(theRestoring == 0){
2114     myTypeGlyph = VISU::Vectors::ARROW;
2115     myLineWidth = 1;
2116     myPosGlyph = VISU::Vectors::TAIL;
2117     myFactor = ScaleFactor()/GetMax();
2118   }
2119   if(MYDEBUG) 
2120     MESSAGE("Vectors_i::DoHook() - myFactor = "<<myFactor<<"; myTypeGlyph = "<<myTypeGlyph<<
2121             "; myLineWidth = "<<myLineWidth<<"; myPosGlyph = "<<myPosGlyph);
2122   Update();
2123   if (myTypeGlyph == VISU::Vectors::NONE) return myHog->GetOutput();
2124   return myGlyph->GetOutput();
2125 }
2126
2127 VISU::Vectors_i::~Vectors_i(){
2128   if(MYDEBUG) MESSAGE("Vectors_i::~Vectors_i()");
2129   myGlyph->Delete();
2130   myHog->Delete();
2131 }
2132
2133 void VISU::Vectors_i::Update() {
2134   switch (myTypeGlyph) {
2135   case VISU::Vectors::ARROW:
2136     {
2137       vtkGlyphSource2D *aGlyphSource = vtkGlyphSource2D::New();
2138       aGlyphSource->SetGlyphTypeToArrow();
2139       aGlyphSource->SetFilled(0);
2140       if(myPosGlyph == VISU::Vectors::TAIL)
2141         aGlyphSource->SetCenter(0.5, 0.0, 0.0);
2142       else if(myPosGlyph == VISU::Vectors::HEAD)
2143         aGlyphSource->SetCenter(-0.5, 0.0, 0.0);
2144       myGlyph->SetSource(aGlyphSource->GetOutput());
2145       aGlyphSource->Delete();
2146     }
2147     break;
2148
2149   case VISU::Vectors::CONE2:
2150   case VISU::Vectors::CONE6:
2151     {
2152       vtkConeSource *aGlyphSource = vtkConeSource::New(); //by default
2153       if (myTypeGlyph == VISU::Vectors::CONE6)
2154         aGlyphSource->SetResolution(7);
2155       else
2156         aGlyphSource->SetResolution(3);
2157       aGlyphSource->SetHeight(1.0);
2158       aGlyphSource->SetRadius(.1);
2159       vtkTransform *aTransform = vtkTransform::New();
2160
2161       if(myPosGlyph == VISU::Vectors::TAIL)
2162         aGlyphSource->SetCenter(0.5, 0.0, 0.0);
2163       else if(myPosGlyph == VISU::Vectors::HEAD)
2164         aGlyphSource->SetCenter(-0.5, 0.0, 0.0);
2165       
2166       vtkTransformPolyDataFilter *aFilter = vtkTransformPolyDataFilter::New();
2167       aFilter->SetInput(aGlyphSource->GetOutput());
2168       aGlyphSource->Delete();
2169       aFilter->SetTransform(aTransform);
2170       aTransform->Delete();
2171       myGlyph->SetSource(aGlyphSource->GetOutput());
2172       aFilter->Delete();
2173     }
2174     break;
2175   case VISU::Vectors::NONE:
2176     {
2177       if(myField->myEntity == VISU::CELL_ENTITY){
2178         vtkCellCenters *centers = vtkCellCenters::New(); // for vectors on cells
2179         centers->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2180         centers->VertexCellsOn();
2181         myHog->SetInput(centers->GetOutput());
2182         centers->Delete();
2183       }else
2184         myHog->SetInput(myFieldTransform->GetInput());
2185       
2186       myHog->SetScaleFactor(myFactor);
2187       myHog->Update();
2188       myMapper->SetInput(myHog->GetOutput());
2189       myMapper->Update();
2190       return;
2191     }
2192   }
2193   if(myField->myNbComp > 1){
2194     if(myField->myEntity == VISU::CELL_ENTITY){
2195       vtkCellCenters *centers = vtkCellCenters::New(); // for vectors on cells
2196       centers->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2197       centers->VertexCellsOn();
2198       myGlyph->SetInput(centers->GetOutput());
2199       centers->Delete();
2200     }else
2201       myGlyph->SetInput(myFieldTransform->GetUnstructuredGridOutput());
2202     myGlyph->SetVectorModeToUseVector();
2203     myGlyph->SetScaleModeToScaleByVector();
2204     //myGlyph->SetColorModeToColorByVector();
2205     myGlyph->SetColorModeToColorByScalar();
2206     myGlyph->SetScaleFactor(2*myFactor);
2207   }
2208   myGlyph->Modified();
2209   VISU::DeformedShape_i::Update();
2210 }
2211
2212 VISU_Actor* VISU::Vectors_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) {
2213   VISU_Actor* anActor = VISU::DeformedShape_i::CreateActor(theIO);
2214   return anActor;
2215 }
2216
2217 void VISU::Vectors_i::UpdateActor(VISU_Actor* theActor) {
2218   VISU::DeformedShape_i::UpdateActor(theActor);
2219   theActor->GetProperty()->SetLineWidth(myLineWidth);
2220   theActor->GetProperty()->SetRepresentation(2);
2221   theActor->SetShrinkable(false);
2222 }
2223
2224 //==============================================================================
2225