Salome HOME
Copyright update 2021
[samples/atomsolv.git] / src / ATOMSOLVGUI / ATOMSOLVGUI_Displayer.cxx
1 // Copyright (C) 2007-2021  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, 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( 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 SALOME_Prs* ATOMSOLVGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* view )
99 {
100   SVTK_Prs* prs = dynamic_cast<SVTK_Prs*>( LightApp_Displayer::buildPresentation( entry, view ) );
101
102   if ( !prs ) return 0;
103
104   double temperature;
105   ATOMGEN_ORB::Atom_var atom = getAtom( entry, temperature );
106   
107   if ( !CORBA::is_nil( atom ) ) {
108     //    PRESENTATION FOR OCC VIEWER
109     //    double x = atom->getX(), y = atom->getY(), z = atom->getZ();
110     //    BRepPrimAPI_MakeSphere makeSphere( gp_Pnt( x, y, z ), radius );
111     //    Handle(AIS_Shape) aisShape = new AIS_Shape( makeSphere.Shell() );
112     //    if( !aisShape.IsNull() ) {
113     //      aisShape->SetOwner( new SALOME_InteractiveObject( entry.latin1(), "ATOMSOLV" ) );
114     //      return new SOCC_Prs( aisShape );
115     //    }
116     double center[ 3 ];
117     center[ 0 ] = atom->getX();
118     center[ 1 ] = atom->getY();
119     center[ 2 ] = atom->getZ();
120     
121     vtkSphereSource* vtkObj = vtkSphereSource::New();
122     vtkObj->SetRadius( radius );
123     vtkObj->SetCenter( center );
124     vtkObj->SetThetaResolution( (int)( vtkObj->GetEndTheta() * quality_coefficient ) );
125     vtkObj->SetPhiResolution( (int)( vtkObj->GetEndPhi() * quality_coefficient ) );
126
127     vtkPolyDataMapper* vtkMapper = vtkPolyDataMapper::New();
128     vtkMapper->SetInputConnection( vtkObj->GetOutputPort() );
129     
130     vtkObj->Delete();
131     
132     SALOME_Actor* actor = SALOME_Actor::New();
133     actor->SetMapper( vtkMapper );
134     actor->setIO( new SALOME_InteractiveObject( entry.toLatin1(), "ATOMSOLV" ) );
135     setTemperature( actor, temperature );
136
137     actor->SetRepresentation( 2 ); // 2 == surface mode
138
139     vtkMapper->Delete();
140     
141     prs->AddObject( actor );
142   }
143
144   return prs;
145 }
146
147 void ATOMSOLVGUI_Displayer::updateActor( SALOME_Actor* actor )
148 {
149   if ( actor && actor->hasIO() ) {
150     double temperature;
151     getAtom( actor->getIO()->getEntry(), temperature );
152     setTemperature( actor, temperature );
153     actor->Update();
154   }
155 }
156
157 void ATOMSOLVGUI_Displayer::setDisplayMode( const QStringList& entries, const QString& mode )
158 {
159   SALOME_View* view = GetActiveView();
160   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
161     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
162       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
163         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
164         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
165         if ( lst ) {
166           lst->InitTraversal();
167           while ( vtkActor* actor = lst->GetNextActor() )
168             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
169               salomeActor->SetRepresentation( mode == "Points" ? 0 : mode == "Wireframe" ? 1 : 2 );
170               salomeActor->Update();
171             }
172         }
173       }
174     }
175   }
176 }
177
178 void ATOMSOLVGUI_Displayer::setColor( const QStringList& entries, const QColor& color  )
179 {
180   SALOME_View* view = GetActiveView();
181   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
182     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
183       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
184         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
185         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
186         if ( lst ) {
187           lst->InitTraversal();
188           while ( vtkActor* actor = lst->GetNextActor() )
189             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
190               salomeActor->SetColor( (float)(color.red()) / 255., (float)(color.green()) / 255., (float)(color.blue()) / 255.);
191               salomeActor->Update();
192             }
193         }
194       }
195     }
196   }
197 }
198
199 QColor ATOMSOLVGUI_Displayer::getColor( const QString& entry )
200 {
201   SALOME_View* view = GetActiveView();
202   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
203     if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
204       SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
205       vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
206       if ( lst ) {
207         lst->InitTraversal();
208         while ( vtkActor* actor = lst->GetNextActor() )
209           if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
210             double r, g, b;
211             salomeActor->GetColor( r, g, b );
212             return QColor( (int)(r * 255.), (int)(g * 255.), (int)(b * 255.) );
213           }
214       } 
215     }
216   }
217   return QColor();
218 }
219
220 void ATOMSOLVGUI_Displayer::setTransparency( const QStringList& entries, const float transparency )
221 {
222   SALOME_View* view = GetActiveView();
223   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
224     for ( QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it ) {    
225       if ( SALOME_Prs* prs = view->CreatePrs( it->toLatin1() ) ) {
226         SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
227         vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
228         if ( lst ) {
229           lst->InitTraversal();
230           while ( vtkActor* actor = lst->GetNextActor() )
231             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
232               salomeActor->SetOpacity( transparency );
233               salomeActor->Update();
234             }
235         }
236       }
237     }
238   }
239 }
240
241 float ATOMSOLVGUI_Displayer::getTransparency( const QString& entry )
242 {
243   SALOME_View* view = GetActiveView();
244   if ( view && dynamic_cast<SVTK_Viewer*>( view ) ) {
245     if ( SALOME_Prs* prs = view->CreatePrs( entry.toLatin1() ) ) {
246       SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
247       vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
248       if ( lst ) {
249         lst->InitTraversal();
250         while ( vtkActor* actor = lst->GetNextActor() )
251           if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
252             return salomeActor->GetOpacity();
253       }
254     }
255   }
256   return -1.;
257 }