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