1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ATOMSOLVGUI_Displayer.h"
21 #include "ATOMSOLVGUI.h"
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Application.h>
26 #include <SUIT_Study.h>
28 #include <SVTK_ViewModel.h>
30 #include <SALOME_Actor.h>
32 #include <SALOMEconfig.h>
33 #include CORBA_CLIENT_HEADER(ATOMSOLV)
35 #include <vtkSphereSource.h>
36 #include <vtkPolyDataMapper.h>
37 #include <vtkActorCollection.h>
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).
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. );
50 ATOMSOLVGUI_Displayer::ATOMSOLVGUI_Displayer()
54 ATOMSOLVGUI_Displayer::~ATOMSOLVGUI_Displayer()
58 bool ATOMSOLVGUI_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
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
66 ATOMGEN_ORB::Atom_ptr getAtom( const QString& entry, double& temprature )
68 QStringList es = entry.split( "_" );
70 bool isAtom = (es.count() == 3 && es[ 0 ] == "ATOMSOLVGUI");
72 return ATOMGEN_ORB::Atom::_nil();
74 const int molID = es[1].toInt();
75 const int atomID = es[2].toInt();
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 );
87 return ATOMGEN_ORB::Atom::_nil();
90 void setTemperature( SALOME_Actor* actor, double temperature )
93 temperature = (((int)temperature) % 10 + 1.) / 10.;
94 actor->SetColor( temperature, 0., 1. - temperature );
98 SALOME_Prs* ATOMSOLVGUI_Displayer::buildPresentation( const QString& entry, SALOME_View* view )
100 SVTK_Prs* prs = dynamic_cast<SVTK_Prs*>( LightApp_Displayer::buildPresentation( entry, view ) );
102 if ( !prs ) return 0;
105 ATOMGEN_ORB::Atom_var atom = getAtom( entry, temperature );
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 );
117 center[ 0 ] = atom->getX();
118 center[ 1 ] = atom->getY();
119 center[ 2 ] = atom->getZ();
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 ) );
127 vtkPolyDataMapper* vtkMapper = vtkPolyDataMapper::New();
128 vtkMapper->SetInputConnection( vtkObj->GetOutputPort() );
132 SALOME_Actor* actor = SALOME_Actor::New();
133 actor->SetMapper( vtkMapper );
134 actor->setIO( new SALOME_InteractiveObject( entry.toLatin1(), "ATOMSOLV" ) );
135 setTemperature( actor, temperature );
137 actor->SetRepresentation( 2 ); // 2 == surface mode
141 prs->AddObject( actor );
147 void ATOMSOLVGUI_Displayer::updateActor( SALOME_Actor* actor )
149 if ( actor && actor->hasIO() ) {
151 getAtom( actor->getIO()->getEntry(), temperature );
152 setTemperature( actor, temperature );
157 void ATOMSOLVGUI_Displayer::setDisplayMode( const QStringList& entries, const QString& mode )
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;
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();
178 void ATOMSOLVGUI_Displayer::setColor( const QStringList& entries, const QColor& color )
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;
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();
199 QColor ATOMSOLVGUI_Displayer::getColor( const QString& entry )
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;
207 lst->InitTraversal();
208 while ( vtkActor* actor = lst->GetNextActor() )
209 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) ) {
211 salomeActor->GetColor( r, g, b );
212 return QColor( (int)(r * 255.), (int)(g * 255.), (int)(b * 255.) );
220 void ATOMSOLVGUI_Displayer::setTransparency( const QStringList& entries, const float transparency )
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;
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();
241 float ATOMSOLVGUI_Displayer::getTransparency( const QString& entry )
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;
249 lst->InitTraversal();
250 while ( vtkActor* actor = lst->GetNextActor() )
251 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
252 return salomeActor->GetOpacity();