Example #1
0
void ThreeDMolDialog::updateTextBrowser()
{
  QString text = "<script src='http://3Dmol.csb.pitt.edu/build/3Dmol-min.js'></script>\n";
  // MDL representation
  std::string mol;
  bool writeSDF = Io::FileFormatManager::instance().writeString(*m_molecule, mol, "sdf");
  text.append("<div style='height: 400px; width: 400px; position: relative;' class='viewer_3Dmoljs' data-element='molecule'");
  if (writeSDF)
    text.append(" data-type='sdf' data-backgroundcolor='0xffffff' data-style='stick'></div>\n");
  else
    text.append(" data-type='xyz' data-backgroundcolor='0xffffff' data-style='stick'></div>\n");

  text.append("<textarea id='molecule' style='display: none;'>");
  if (writeSDF) {
    text.append(mol.c_str());
  } else {
    // couldn't write MDL for some reason, write XYZ
    size_t numAtoms = m_molecule->atomCount();
    text.append(QString::number(numAtoms) + "\n");
    text.append("Generated by Avogadro.\n");

    for(size_t i = 0; i < numAtoms; ++i) {
      Core::Atom atom = m_molecule->atom(i);
      QString line("%1 %2 %3 %4\n");
      text.append( line.arg(Core::Elements::symbol(atom.atomicNumber()))
        .arg(atom.position3d().x())
        .arg(atom.position3d().y())
        .arg(atom.position3d().z()) );
    } // end for(atoms)
  }

  text.append("</textarea>");
  m_ui->plainTextEdit->setPlainText(text);
}
void BallAndStick::process(const Molecule &molecule,
                           Rendering::GroupNode &node)
{
  // Add a sphere node to contain all of the spheres.
  GeometryNode *geometry = new GeometryNode;
  node.addChild(geometry);
  SphereGeometry *spheres = new SphereGeometry;
  spheres->identifier().molecule = &molecule;
  spheres->identifier().type = Rendering::AtomType;
  geometry->addDrawable(spheres);

  for (Index i = 0; i < molecule.atomCount(); ++i) {
    Core::Atom atom = molecule.atom(i);
    unsigned char atomicNumber = atom.atomicNumber();
    const unsigned char *c = Elements::color(atomicNumber);
    Vector3ub color(c[0], c[1], c[2]);
    spheres->addSphere(atom.position3d().cast<float>(), color,
                       static_cast<float>(Elements::radiusVDW(atomicNumber))
                       * 0.3f);
  }

  float bondRadius = 0.1f;
  CylinderGeometry *cylinders = new CylinderGeometry;
  cylinders->identifier().molecule = &molecule;
  cylinders->identifier().type = Rendering::BondType;
  geometry->addDrawable(cylinders);
  for (Index i = 0; i < molecule.bondCount(); ++i) {
    Core::Bond bond = molecule.bond(i);
    Vector3f pos1 = bond.atom1().position3d().cast<float>();
    Vector3f pos2 = bond.atom2().position3d().cast<float>();
    Vector3ub color1(Elements::color(bond.atom1().atomicNumber()));
    Vector3ub color2(Elements::color(bond.atom2().atomicNumber()));
    Vector3f bondVector = pos2 - pos1;
    float bondLength = bondVector.norm();
    bondVector /= bondLength;
    switch (bond.order()) {
    case 3: {
      Vector3f delta = bondVector.unitOrthogonal() * (2.0f * bondRadius);
      cylinders->addCylinder(pos1 + delta, bondVector, bondLength, bondRadius,
                             color1, color2, i);
      cylinders->addCylinder(pos1 - delta, bondVector, bondLength, bondRadius,
                             color1, color2, i);
    }
    default:
    case 1:
      cylinders->addCylinder(pos1, bondVector, bondLength, bondRadius,
                             color1, color2, i);
      break;
    case 2: {
      Vector3f delta = bondVector.unitOrthogonal() * bondRadius;
      cylinders->addCylinder(pos1 + delta, bondVector, bondLength, bondRadius,
                             color1, color2, i);
      cylinders->addCylinder(pos1 - delta, bondVector, bondLength, bondRadius,
                             color1, color2, i);
    }
    }
  }
}
Example #3
0
void VanDerWaals::process(const Core::Molecule &molecule,
                          Rendering::GroupNode &node)
{
  // Add a sphere node to contain all of the VdW spheres.
  GeometryNode *geometry = new GeometryNode;
  node.addChild(geometry);
  SphereGeometry *spheres = new SphereGeometry;
  spheres->identifier().molecule = &molecule;
  spheres->identifier().type = Rendering::AtomType;
  geometry->addDrawable(spheres);

  for (size_t i = 0; i < molecule.atomCount(); ++i) {
    Core::Atom atom = molecule.atom(i);
    unsigned char atomicNumber = atom.atomicNumber();
    const unsigned char *c = Elements::color(atomicNumber);
    Vector3ub color(c[0], c[1], c[2]);
    spheres->addSphere(atom.position3d().cast<float>(), color,
                       static_cast<float>(Elements::radiusVDW(atomicNumber)));
  }
}
Example #4
0
void OpenBabel::onHydrogenOperationFinished(const QByteArray &mdl)
{
  m_progress->setLabelText(tr("Reading obabel output..."));

  // MDL --> molecule
  Core::Molecule mol;
  if (!Io::FileFormatManager::instance().readString(mol, mdl.constData(),
                                                    "mol")) {
    m_progress->reset();
    qWarning() << "Bad MDL: " << mdl;
    QMessageBox::critical(qobject_cast<QWidget*>(parent()),
                          tr("Error"),
                          tr("Error interpreting obabel MDL output."),
                          QMessageBox::Ok);
    return;
  }

  /// @todo cache a pointer to the current molecule in the above slot, and
  /// verify that we're still operating on the same molecule.

  // Update molecule
  m_molecule->clearAtoms();
  for (Index i = 0; i < mol.atomCount(); ++i) {
    Core::Atom atom = mol.atom(i);
    m_molecule->addAtom(atom.atomicNumber()).setPosition3d(atom.position3d());
  }
  for (Index i = 0; i < mol.bondCount(); ++i) {
    Core::Bond bond = mol.bond(i);
    m_molecule->addBond(m_molecule->atom(bond.atom1().index()),
                        m_molecule->atom(bond.atom2().index()),
                        bond.order());
  }

  m_molecule->emitChanged(Molecule::Atoms | Molecule::Bonds | Molecule::Added |
                          Molecule::Removed | Molecule::Modified);

  m_progress->reset();
}