Salome HOME
Synchronize adm files
[samples/atomsolv.git] / src / ATOMSOLVGUI / ATOMSOLVGUI_Displayer.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ATOMSOLVGUI_Displayer.h"
21 #include "ATOMSOLVGUI.h"
22
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Application.h>
26 #include <SUIT_Study.h>
27
28 #include <SVTK_ViewModel.h>
29 #include <SVTK_Prs.h>
30 #include <SALOME_Actor.h>
31
32 #include <SALOMEconfig.h>
33 #include CORBA_CLIENT_HEADER(ATOMSOLV)
34
35 #include <vtkSphereSource.h>
36 #include <vtkPolyDataMapper.h>
37 #include <vtkActorCollection.h>
38
39 const float radius = 5.; // radius of a sphere that represents an atom in VTK viewer
40 const float quality_coefficient = .1; // for VTK viewer presentation: parameter that controls
41 // the quality of presentation of sphere (atom).  0 - very bad, 1. - very good (and slow).
42
43 // default representation mode, is taken from preferences
44 int ATOMSOLVGUI_Displayer::myRepresentation = SUIT_Session::session()->resourceMgr(
45                                               )->integerValue( "ATOMSOLV", "Representation", 2 );
46 // default radius, is taken from preferences
47 double ATOMSOLVGUI_Displayer::myRadius = SUIT_Session::session()->resourceMgr(
48                                          )->doubleValue( "ATOMSOLV", "Radius", 5. );
49
50 ATOMSOLVGUI_Displayer::ATOMSOLVGUI_Displayer()
51 {
52 }
53
54 ATOMSOLVGUI_Displayer::~ATOMSOLVGUI_Displayer()
55 {
56 }
57
58 bool ATOMSOLVGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
59 {
60   QStringList es = entry.split( "_" );
61   bool result = ( es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI" && viewer_type == SVTK_Viewer::Type() ); 
62   //  printf ( " canBeDisplayed : entry = %s, count = %d, res = %d \n", entry.latin1(), es.count(), result );
63   return result; // entry of an atom for sure
64 }
65
66 ATOMGEN_ORB::Atom_ptr getAtom( const QString& entry, const int studyID, double& temprature )
67 {
68   QStringList es = entry.split( "_" );
69
70   bool isAtom = (es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI");
71   if (!isAtom)
72     return ATOMGEN_ORB::Atom::_nil();
73
74   const int molID = es[1].toInt();
75   const int atomID = es[2].toInt();
76
77   ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = ATOMSOLVGUI::GetATOMSOLVGen();
78   ATOMSOLV_ORB::TMoleculeList_var molecules;
79   if ( engine->getData( studyID, molecules ) && molID >= 0 && molID < molecules->length() ) {
80     ATOMSOLV_ORB::TMolecule tmol = molecules[ molID ];
81     ATOMGEN_ORB::Molecule_var mol = tmol.molecule;
82     if ( atomID < mol->getNbAtoms() ) {
83       temprature = tmol.temperature;
84       return mol->getAtom( atomID );
85     }
86   }
87   return ATOMGEN_ORB::Atom::_nil();
88 }
89
90 void setTemperature( SALOME_Actor* actor, double temperature )
91 {
92   if ( actor ) {
93     temperature = (((int)temperature) % 10 + 1.) / 10.;
94     actor->SetColor( temperature, 0., 1. - temperature );
95   }
96 }
97
98 int getStudyID()
99 {
100   int studyID = -1;
101   if ( SUIT_Application* app = SUIT_Session::session()->activeApplication() )
102     if ( SUIT_Study* study = app->activeStudy() )
103       studyID = study->id();
104   return studyID;
105 }
106
107 SALOME_Prs* ATOMSOLVGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* view )
108 {
109   const int studyID = getStudyID();
110   if ( studyID == -1 )
111     return 0;
112
113   SVTK_Prs* prs = dynamic_cast<SVTK_Prs*>( LightApp_Displayer::buildPresentation( entry, view ) );
114
115   if ( !prs ) return 0;
116
117   double temperature;
118   ATOMGEN_ORB::Atom_var atom = getAtom( entry, studyID, temperature );
119   
120   if ( !CORBA::is_nil( atom ) ) {
121     //    PRESENTATION FOR OCC VIEWER
122     //    double x = atom->getX(), y = atom->getY(), z = atom->getZ();
123     //    BRepPrimAPI_MakeSphere makeSphere( gp_Pnt( x, y, z ), radius );
124     //    Handle(AIS_Shape) aisShape = new AIS_Shape( makeSphere.Shell() );
125     //    if( !aisShape.IsNull() ) {
126     //      aisShape->SetOwner( new SALOME_InteractiveObject( entry.latin1(), "ATOMSOLV" ) );
127     //      return new SOCC_Prs( aisShape );
128     //    }
129     double center[ 3 ];
130     center[ 0 ] = atom->getX();
131     center[ 1 ] = atom->getY();
132     center[ 2 ] = atom->getZ();
133     
134     vtkSphereSource* vtkObj = vtkSphereSource::New();
135     vtkObj->SetRadius( radius );
136     vtkObj->SetCenter( center );
137     vtkObj->SetThetaResolution( (int)( vtkObj->GetEndTheta() * quality_coefficient ) );
138     vtkObj->SetPhiResolution( (int)( vtkObj->GetEndPhi() * quality_coefficient ) );
139
140     vtkPolyDataMapper* vtkMapper = vtkPolyDataMapper::New();
141     vtkMapper->SetInputConnection( vtkObj->GetOutputPort() );
142     
143     vtkObj->Delete();
144     
145     SALOME_Actor* actor = SALOME_Actor::New();
146     actor->SetMapper( vtkMapper );
147     actor->setIO( new SALOME_InteractiveObject( entry.toLatin1(), "ATOMSOLV" ) );
148     setTemperature( actor, temperature );
149
150     actor->SetRepresentation( 2 ); // 2 == surface mode
151
152     vtkMapper->Delete();
153     
154     prs->AddObject( actor );
155   }
156
157   return prs;
158 }
159
160 void ATOMSOLVGUI_Displayer::updateActor( SALOME_Actor* actor )
161 {
162   const int studyID = getStudyID();
163   if ( actor && actor->hasIO() && studyID >= 0 ) {
164     double temperature;
165     getAtom( actor->getIO()->getEntry(), studyID, temperature );
166     setTemperature( actor, temperature );
167     actor->Update();
168   }
169 }
170
171 void ATOMSOLVGUI_Displayer::setDisplayMode( const QStringList& entries, const QString& mode )
172 {
173   SALOME_View* view = GetActiveView();
174   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
175     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
176       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
177         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
178         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
179         if ( lst ) {
180           lst->InitTraversal();
181           while ( vtkActor* actor = lst->GetNextActor() )
182             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
183               salomeActor->SetRepresentation( mode == "Points" ? 0 : mode == "Wireframe" ? 1 : 2 );
184               salomeActor->Update();
185             }
186         }
187       }
188     }
189   }
190 }
191
192 void ATOMSOLVGUI_Displayer::setColor( const QStringList& entries, const QColor& color  )
193 {
194   SALOME_View* view = GetActiveView();
195   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
196     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
197       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
198         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
199         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
200         if ( lst ) {
201           lst->InitTraversal();
202           while ( vtkActor* actor = lst->GetNextActor() )
203             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
204               salomeActor->SetColor( (float)(color.red()) / 255., (float)(color.green()) / 255., (float)(color.blue()) / 255.);
205               salomeActor->Update();
206             }
207         }
208       }
209     }
210   }
211 }
212
213 QColor ATOMSOLVGUI_Displayer::getColor( const QString& entry )
214 {
215   SALOME_View* view = GetActiveView();
216   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
217     if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
218       SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
219       vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
220       if ( lst ) {
221         lst->InitTraversal();
222         while ( vtkActor* actor = lst->GetNextActor() )
223           if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
224             double r, g, b;
225             salomeActor->GetColor( r, g, b );
226             return QColor( (int)(r * 255.), (int)(g * 255.), (int)(b * 255.) );
227           }
228       } 
229     }
230   }
231   return QColor();
232 }
233
234 void ATOMSOLVGUI_Displayer::setTransparency( const QStringList& entries, const float transparency )
235 {
236   SALOME_View* view = GetActiveView();
237   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
238     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
239       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
240         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
241         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
242         if ( lst ) {
243           lst->InitTraversal();
244           while ( vtkActor* actor = lst->GetNextActor() )
245             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
246               salomeActor->SetOpacity( transparency );
247               salomeActor->Update();
248             }
249         }
250       }
251     }
252   }
253 }
254
255 float ATOMSOLVGUI_Displayer::getTransparency( const QString& entry )
256 {
257   SALOME_View* view = GetActiveView();
258   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
259     if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
260       SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
261       vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
262       if ( lst ) {
263         lst->InitTraversal();
264         while ( vtkActor* actor = lst->GetNextActor() )
265           if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
266             return salomeActor->GetOpacity();
267       }
268     }
269   }
270   return -1.;
271 }