• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kalzium

bsdyengine.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002   BSDYEngine - Dynamic detail engine for "balls and sticks" display
00003 
00004   Copyright (C) 2007 Donald Ephraim Curtis <donald-curtis@uiowa.edu>
00005 
00006   This file is part of the Avogadro molecular editor project.
00007   For more information, see <http://avogadro.sourceforge.net/>
00008 
00009   Avogadro is free software; you can redistribute it and/or modify 
00010   it under the terms of the GNU General Public License as published by 
00011   the Free Software Foundation; either version 2 of the License, or 
00012   (at your option) any later version.
00013 
00014   Avogadro is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with this program; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00022   02110-1301, USA.
00023  **********************************************************************/
00024 
00025 #include "bsdyengine.h"
00026 #include <config.h>
00027 
00028 #include <avogadro/primitive.h>
00029 #include <avogadro/color.h>
00030 #include <avogadro/glwidget.h>
00031 #include <avogadro/camera.h>
00032 
00033 #include <openbabel/obiter.h>
00034 #include <eigen/regression.h>
00035 
00036 #include <QMessageBox>
00037 #include <QSlider>
00038 #include <QtPlugin>
00039 #include <QVBoxLayout>
00040 
00041 using namespace std;
00042 using namespace OpenBabel;
00043 using namespace Eigen;
00044 using namespace Avogadro;
00045 
00046 BSDYEngine::BSDYEngine(QObject *parent) : Engine(parent), m_glwidget(0), m_update(true),
00047   m_settingsWidget(0), m_atomRadiusPercentage(0.3), m_bondRadius(0.1)
00048 {
00049 }
00050 
00051 BSDYEngine::~BSDYEngine()
00052 {
00053   if(m_settingsWidget) {
00054     m_settingsWidget->deleteLater();
00055   }
00056 
00057 }
00058 
00059 bool BSDYEngine::render(GLWidget *gl)
00060 {
00061   m_glwidget = gl;
00062   Color map = colorMap();
00063 
00064   QList<Primitive *> list;
00065 
00066 
00067   m_update = false;
00068   glPushAttrib(GL_TRANSFORM_BIT);
00069   glDisable( GL_NORMALIZE );
00070   glEnable( GL_RESCALE_NORMAL );
00071   list = queue().primitiveList(Primitive::AtomType);
00072   glPushName(Primitive::AtomType);
00073   foreach( Primitive *p, list ) {
00074     Atom * a = static_cast<Atom *>(p);
00075     glPushName(a->GetIdx());
00076 
00077     map.set(a);
00078     map.applyAsMaterials();
00079 
00080     m_glwidget->painter()->drawSphere( a->pos(), radius(a) );
00081 
00082     if (m_glwidget->selectedItem(a))
00083     {
00084       map.set( 0.3, 0.6, 1.0, 0.7 );
00085       map.applyAsMaterials();
00086       glEnable( GL_BLEND );
00087       m_glwidget->painter()->drawSphere( a->pos(), SEL_ATOM_EXTRA_RADIUS + radius(a) );
00088       glDisable( GL_BLEND );
00089     }
00090 
00091     glPopName();
00092 
00093   }
00094   glPopName();
00095 
00096   // normalize normal vectors of bonds
00097   glDisable( GL_RESCALE_NORMAL);
00098   glEnable( GL_NORMALIZE );
00099 
00100   list = queue().primitiveList(Primitive::BondType);
00101   Eigen::Vector3d normalVector;
00102   if(m_glwidget) {
00103     normalVector = m_glwidget->normalVector();
00104   }
00105   Atom *atom1;
00106   Atom *atom2;
00107   foreach( Primitive *p, list ) {
00108     Bond *b = static_cast<Bond *>(p);
00109 
00110     atom1 = static_cast<Atom *>(b->GetBeginAtom());
00111     atom2 = static_cast<Atom *>(b->GetEndAtom());
00112     Vector3d v1 (atom1->pos());
00113     Vector3d v2 (atom2->pos());
00114     Vector3d d = v2 - v1;
00115     d.normalize();
00116     Vector3d v3 ( (v1 + v2 + d*(radius(atom1)-radius(atom2))) / 2 );
00117 
00118     double shift = 0.15;
00119     int order = b->GetBO();
00120 
00121     map.set(atom1);
00122     map.applyAsMaterials();
00123     m_glwidget->painter()->drawMultiCylinder( v1, v3, m_bondRadius, order, shift );
00124 
00125     map.set(atom2);
00126     map.applyAsMaterials();
00127     m_glwidget->painter()->drawMultiCylinder( v3, v2, m_bondRadius, order, shift );
00128     //  glPopName();
00129     //  glPopName();
00130   }
00131 
00132   glPopAttrib();
00133 
00134   return true;
00135 }
00136 
00137 inline double BSDYEngine::radius(const Atom *atom)
00138 {
00139   return etab.GetVdwRad(atom->GetAtomicNum()) * m_atomRadiusPercentage;
00140 }
00141 
00142 void BSDYEngine::setAtomRadiusPercentage(int percent)
00143 {
00144   m_atomRadiusPercentage = 0.1 * percent;
00145   emit changed();
00146 }
00147 
00148 void BSDYEngine::setBondRadius(int value)
00149 {
00150   m_bondRadius = value * 0.1;
00151   emit changed();
00152 }
00153 
00154 double BSDYEngine::radius(const Primitive *p)
00155 {
00156   if (p->type() == Primitive::AtomType)
00157   {
00158     const Atom *a = static_cast<const Atom *>(p);
00159     double r = radius(a);
00160     if (m_glwidget)
00161     {
00162       if (m_glwidget->selectedItem(p))
00163         return r + SEL_ATOM_EXTRA_RADIUS;
00164     }
00165     return r;
00166   }
00167   else
00168     return 0.;
00169 }
00170 
00171 bool BSDYEngine::render(const Atom *a)
00172 {
00173   Q_UNUSED(a);
00174   return true;
00175 }
00176 
00177 bool BSDYEngine::render(const Bond *b)
00178 {
00179   Q_UNUSED(b);
00180   return true;
00181 }
00182 
00183 bool BSDYEngine::render(const Molecule *m)
00184 {
00185   // Disabled
00186   Q_UNUSED(m);
00187   return false;
00188 }
00189 
00190 void BSDYEngine::addPrimitive(Primitive *primitive)
00191 {
00192   Engine::addPrimitive(primitive);
00193   m_update = true;
00194 }
00195 
00196 void BSDYEngine::updatePrimitive(Primitive *primitive)
00197 {
00198   Engine::updatePrimitive(primitive);
00199   m_update = true;
00200 }
00201 
00202 void BSDYEngine::removePrimitive(Primitive *primitive)
00203 {
00204   Engine::removePrimitive(primitive);
00205   m_update = true;
00206 }
00207 
00208 QWidget *BSDYEngine::settingsWidget()
00209 {
00210   if(!m_settingsWidget)
00211   {
00212     m_settingsWidget = new BSDYSettingsWidget();
00213     connect(m_settingsWidget->atomRadiusSlider, SIGNAL(valueChanged(int)), this, SLOT(setAtomRadiusPercentage(int)));
00214     connect(m_settingsWidget->bondRadiusSlider, SIGNAL(valueChanged(int)), this, SLOT(setBondRadius(int)));
00215     connect(m_settingsWidget, SIGNAL(destroyed()), this, SLOT(settingsWidgetDestroyed()));
00216   }
00217   return m_settingsWidget;
00218 }
00219 
00220 void BSDYEngine::settingsWidgetDestroyed()
00221 {
00222   qDebug() << "Destroyed Settings Widget";
00223   m_settingsWidget = 0;
00224 }
00225 
00226 #include "bsdyengine.moc"
00227 
00228 Q_EXPORT_PLUGIN2(bsdyengine, Avogadro::BSDYEngineFactory)

kalzium

Skip menu "kalzium"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal