Debian Codegraph @

avogadro/qtgui/rwmolecule.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "rwmolecule.h"
7 #include "rwmolecule_undo.h"
8
9 #include <algorithm>
10 #include <cassert>
11
12 #ifdef USE_SPGLIB
13 #include <avogadro/core/avospglib.h>
14 #endif
15 #include <avogadro/core/layermanager.h>
16 #include <avogadro/core/residue.h>
17 #include <avogadro/core/spacegroups.h>
18 #include <avogadro/qtgui/hydrogentools.h>
19
20 #include <QtCore/QDebug>
21
22 namespace Avogadro::QtGui {
23
24 using Core::Array;
25 using Core::AtomHybridization;
26 using Core::CrystalTools;
27 using Core::UnitCell;
28 using std::swap;
29
30 RWMolecule::RWMolecule(Molecule& mol, QObject* p)
31 : QObject(p), m_molecule(mol), m_interactive(false)
32 {
33 }
34
35 RWMolecule::~RWMolecule() {}
36
37 RWMolecule::AtomType RWMolecule::addAtom(unsigned char num, bool usingPositions)
38 {
39 auto atomId = static_cast<Index>(m_molecule.atomCount());
40 auto atomUid = static_cast<Index>(m_molecule.m_atomUniqueIds.size());
41
42 auto* comm = new AddAtomCommand(*this, num, usingPositions, atomId, atomUid);
43 comm->setText(tr("Add Atom"));
44 m_undoStack.push(comm);
45 return AtomType(this, atomId);
46 }
47
48 RWMolecule::AtomType RWMolecule::addAtom(unsigned char num,
49 const Vector3& position3d)
50 {
51 // We will combine the actions in this command.
52 m_undoStack.beginMacro(tr("Add Atom"));
53 AtomType atom = addAtom(num);
54 setAtomPosition3d(atomCount() - 1, position3d);
55 m_undoStack.endMacro();
56 return atom;
57 }
58
59 Index RWMolecule::atomCount(unsigned char num) const
60 {
61 return m_molecule.atomCount(num);
62 }
63
64 bool RWMolecule::removeAtom(Index atomId)
65 {
66 if (atomId >= atomCount())
67 return false;
68
69 Index uniqueId = findAtomUniqueId(atomId);
70 if (uniqueId == MaxIndex)
71 return false;
72
73 // Lump all operations into a single undo command:
74 m_undoStack.beginMacro(tr("Remove Atom"));
75
76 // Remove any bonds containing this atom first.
77 Array<BondType> atomBonds = bonds(atomId);
78 while (atomBonds.size()) {
79 // Ensure that indices aren't invalidated as we remove them:
80 assert("atomBonds have ascending indices" &&
81 (atomBonds.size() == 1 ||
82 ((atomBonds.end() - 2)->index() < (atomBonds.end() - 1)->index())));
83 removeBond(atomBonds.back());
84 atomBonds.pop_back();
85 }
86
87 auto* comm = new RemoveAtomCommand(
88 *this, atomId, uniqueId, atomicNumber(atomId), atomPosition3d(atomId));
89 comm->setText(tr("Remove Atom"));
90
91 m_undoStack.push(comm);
92
93 m_undoStack.endMacro();
94 return true;
95 }
96
97 void RWMolecule::clearAtoms()
98 {
99 m_undoStack.beginMacro(tr("Clear Atoms"));
100
101 while (atomCount() != 0)
102 removeAtom(0);
103
104 m_undoStack.endMacro();
105 }
106
107 void RWMolecule::adjustHydrogens(Index atomId)
108 {
109 RWAtom atom = this->atom(atomId);
110 if (atom.isValid()) {
111 m_undoStack.beginMacro(tr("Adjust Hydrogens"));
112 QtGui::HydrogenTools::adjustHydrogens(atom);
113 m_undoStack.endMacro();
114 }
115 }
116
117 void RWMolecule::adjustHydrogens(const Core::Array<Index>& atomIds)
118 {
119 m_undoStack.beginMacro(tr("Adjust Hydrogens"));
120
121 // convert the indexes to unique ids
122 // since we're adding and removing atoms
123 // (so the index will change)
124 Core::Array<Index> uniqueIds;
125 for (auto id : atomIds) {
126 Index uniqueId = findAtomUniqueId(id);
127 if (uniqueId != MaxIndex)
128 uniqueIds.push_back(uniqueId);
129 }
130
131 for (auto uniqueId : uniqueIds) {
132 RWAtom atom = this->atomByUniqueId(uniqueId);
133 if (atom.isValid()) {
134 QtGui::HydrogenTools::adjustHydrogens(atom);
135 }
136 }
137 m_undoStack.endMacro();
138 }
139
140 bool RWMolecule::setAtomicNumbers(const Core::Array<unsigned char>& nums)
141 {
142 if (nums.size() != m_molecule.atomCount())
143 return false;
144
145 auto* comm =
146 new SetAtomicNumbersCommand(*this, m_molecule.atomicNumbers(), nums);
147 comm->setText(tr("Change Elements"));
148 m_undoStack.push(comm);
149 return true;
150 }
151
152 bool RWMolecule::setAtomicNumber(Index atomId, unsigned char num)
153 {
154 if (atomId >= atomCount())
155 return false;
156
157 auto* comm = new SetAtomicNumberCommand(*this, atomId,
158 m_molecule.atomicNumber(atomId), num);
159 comm->setText(tr("Change Element"));
160 m_undoStack.push(comm);
161 return true;
162 }
163
164 bool RWMolecule::setAtomPositions3d(const Core::Array<Vector3>& pos,
165 const QString& undoText)
166 {
167 if (pos.size() != m_molecule.atomCount())
168 return false;
169
170 auto* comm = new SetPositions3dCommand(*this, m_molecule.m_positions3d, pos);
171 comm->setText(undoText);
172 comm->setCanMerge(m_interactive);
173 m_undoStack.push(comm);
174 return true;
175 }
176
177 bool RWMolecule::setAtomLabel(Index atomId, const std::string& label,
178 const QString& undoText)
179 {
180 if (atomId >= atomCount())
181 return false;
182
183 auto* comm = new ModifyAtomLabelCommand(*this, atomId, label);
184 comm->setText(undoText);
185 m_undoStack.push(comm);
186 return true;
187 }
188
189 bool RWMolecule::setBondLabel(Index bondId, const std::string& label,
190 const QString& undoText)
191 {
192 if (bondId >= bondCount())
193 return false;
194
195 auto* comm = new ModifyBondLabelCommand(*this, bondId, label);
196 comm->setText(undoText);
197 m_undoStack.push(comm);
198 return true;
199 }
200
201 bool RWMolecule::setAtomPosition3d(Index atomId, const Vector3& pos,
202 const QString& undoText)
203 {
204 if (atomId >= atomCount())
205 return false;
206
207 if (m_molecule.m_positions3d.size() != m_molecule.atomCount())
208 m_molecule.m_positions3d.resize(m_molecule.atomCount(), Vector3::Zero());
209
210 auto* comm = new SetPosition3dCommand(*this, atomId,
211 m_molecule.m_positions3d[atomId], pos);
212 comm->setText(undoText);
213 comm->setCanMerge(m_interactive);
214 m_undoStack.push(comm);
215 return true;
216 }
217
218 void RWMolecule::setAtomSelected(Index atomId, bool selected,
219 const QString& undoText)
220 {
221 auto* comm = new ModifySelectionCommand(*this, atomId, selected);
222 comm->setText(undoText);
223 comm->setCanMerge(true);
224 m_undoStack.push(comm);
225 // m_molecule.setAtomSelected(atomId, selected);
226 }
227
228 bool RWMolecule::atomSelected(Index atomId) const
229 {
230 return m_molecule.atomSelected(atomId);
231 }
232
233 bool RWMolecule::setHybridization(Index atomId, Core::AtomHybridization hyb)
234 {
235 if (atomId >= atomCount())
236 return false;
237
238 auto* comm = new SetAtomicNumberCommand(
239 *this, atomId, m_molecule.hybridization(atomId), hyb);
240 comm->setText(tr("Change Atom Hybridization"));
241 m_undoStack.push(comm);
242 return true;
243 }
244
245 bool RWMolecule::setFormalCharge(Index atomId, signed char charge)
246 {
247 if (atomId >= atomCount())
248 return false;
249
250 auto* comm = new SetAtomFormalChargeCommand(
251 *this, atomId, m_molecule.formalCharge(atomId), charge);
252 comm->setText(tr("Change Atom Formal Charge"));
253 m_undoStack.push(comm);
254 return true;
255 }
256
257 bool RWMolecule::setIsotope(Index atomId, unsigned short isotope)
258 {
259 if (atomId >= atomCount())
260 return false;
261
262 // TODO: implement an undo command
263 m_molecule.setIsotope(atomId, isotope);
264 return true;
265 }
266
267 bool RWMolecule::setColor(Index atomId, Vector3ub color)
268 {
269 if (atomId >= atomCount())
270 return false;
271
272 auto* comm =
273 new SetAtomColorCommand(*this, atomId, m_molecule.color(atomId), color);
274 comm->setText(tr("Change Atom Color"));
275 m_undoStack.push(comm);
276 return true;
277 }
278
279 bool RWMolecule::setResidueColor(Index residueId, Vector3ub color)
280 {
281 if (residueId >= m_molecule.residueCount())
282 return false;
283
284 auto* comm = new SetResidueColorCommand(
285 *this, residueId, m_molecule.residue(residueId).color(), color);
286 comm->setText(tr("Change Residue Color"));
287 m_undoStack.push(comm);
288 return true;
289 }
290
291 bool RWMolecule::setLayer(Index atomId, size_t layer)
292 {
293 if (atomId >= atomCount())
294 return false;
295
296 auto* comm =
297 new SetLayerCommand(*this, atomId, m_molecule.layer(atomId), layer);
298 comm->setText(tr("Change Atom Layer"));
299 m_undoStack.push(comm);
300 return true;
301 }
302
303 RWMolecule::BondType RWMolecule::addBond(Index atom1, Index atom2,
304 unsigned char order)
305 {
306 if (atom1 == atom2 || std::max(atom1, atom2) >= atomCount())
307 return BondType();
308
309 Index bondId = bondCount();
310 auto bondUid = static_cast<Index>(m_molecule.m_bondUniqueIds.size());
311
312 auto* comm = new AddBondCommand(
313 *this, order, Molecule::makeBondPair(atom1, atom2), bondId, bondUid);
314 comm->setText(tr("Add Bond"));
315 m_undoStack.push(comm);
316 return BondType(this, bondId);
317 }
318
319 RWMolecule::BondType RWMolecule::bond(Index atom1, Index atom2) const
320 {
321 Molecule::BondType b = m_molecule.bond(atom1, atom2);
322 if (b.isValid())
323 return BondType(const_cast<RWMolecule*>(this), b.index());
324 else
325 return BondType();
326 }
327
328 bool RWMolecule::removeBond(Index bondId)
329 {
330 if (bondId >= bondCount())
331 return false;
332
333 Index bondUid = findBondUniqueId(bondId);
334 if (bondUid == MaxIndex)
335 return false;
336
337 auto* comm =
338 new RemoveBondCommand(*this, bondId, bondUid, m_molecule.bondPair(bondId),
339 m_molecule.bondOrder(bondId));
340 comm->setText(tr("Removed Bond"));
341 m_undoStack.push(comm);
342 return true;
343 }
344
345 void RWMolecule::clearBonds()
346 {
347 m_undoStack.beginMacro(tr("Clear Bonds"));
348
349 while (bondCount() != 0)
350 removeBond(0);
351
352 m_undoStack.endMacro();
353 }
354
355 bool RWMolecule::setBondOrders(const Core::Array<unsigned char>& orders)
356 {
357 if (orders.size() != m_molecule.bondCount())
358 return false;
359
360 auto* comm = new SetBondOrdersCommand(*this, m_molecule.bondOrders(), orders);
361 comm->setText(tr("Set Bond Orders"));
362 m_undoStack.push(comm);
363 return true;
364 }
365
366 bool RWMolecule::setBondOrder(Index bondId, unsigned char order)
367 {
368 if (bondId >= bondCount())
369 return false;
370
371 auto* comm =
372 new SetBondOrderCommand(*this, bondId, m_molecule.bondOrder(bondId), order);
373 comm->setText(tr("Change Bond Order"));
374 // Always allow merging, but only if bondId is the same.
375 comm->setCanMerge(true);
376 m_undoStack.push(comm);
377 return true;
378 }
379
380 bool RWMolecule::setBondPairs(const Array<std::pair<Index, Index>>& pairs)
381 {
382 if (pairs.size() != m_molecule.bondCount())
383 return false;
384
385 // Correct any pairs that are ordered improperly:
386 typedef std::pair<Index, Index> BondPair;
387 Array<BondPair> p(pairs);
388 // Use for reading to prevent copies unless needed (Array is copy-on-write):
389 const Array<BondPair>& p_const = p;
390 for (size_t i = 0; i < p.size(); ++i)
391 if (p_const[i].first > p_const[i].second)
392 swap(p[i].first, p[i].second);
393
394 auto* comm = new SetBondPairsCommand(*this, m_molecule.bondPairs(), p);
395 comm->setText(tr("Update Bonds"));
396 m_undoStack.push(comm);
397 return true;
398 }
399
400 bool RWMolecule::setBondPair(Index bondId, const std::pair<Index, Index>& pair)
401 {
402 if (bondId >= bondCount() || pair.first == pair.second)
403 return false;
404
405 SetBondPairCommand* comm = nullptr;
406 if (pair.first < pair.second) {
407 comm =
408 new SetBondPairCommand(*this, bondId, m_molecule.bondPair(bondId), pair);
409 } else {
410 comm =
411 new SetBondPairCommand(*this, bondId, m_molecule.bondPair(bondId),
412 Molecule::makeBondPair(pair.first, pair.second));
413 }
414 comm->setText(tr("Update Bond"));
415 m_undoStack.push(comm);
416 return true;
417 }
418
419 void RWMolecule::addUnitCell()
420 {
421 // If there is already a unit cell, there is nothing to do
422 if (m_molecule.unitCell())
423 return;
424
425 auto* cell = new UnitCell;
426 cell->setCellParameters(
427 static_cast<Real>(3.0), static_cast<Real>(3.0), static_cast<Real>(3.0),
428 static_cast<Real>(90.0) * DEG_TO_RAD, static_cast<Real>(90.0) * DEG_TO_RAD,
429 static_cast<Real>(90.0) * DEG_TO_RAD);
430 m_molecule.setUnitCell(cell);
431
432 auto* comm = new AddUnitCellCommand(*this, *m_molecule.unitCell());
433 comm->setText(tr("Add Unit Cell…"));
434 m_undoStack.push(comm);
435 emitChanged(Molecule::UnitCell | Molecule::Added);
436 }
437
438 void RWMolecule::removeUnitCell()
439 {
440 // If there is no unit cell, there is nothing to do
441 if (!m_molecule.unitCell())
442 return;
443
444 auto* comm = new RemoveUnitCellCommand(*this, *m_molecule.unitCell());
445 comm->setText(tr("Remove Unit Cell"));
446 m_undoStack.push(comm);
447
448 m_molecule.setUnitCell(nullptr);
449 emitChanged(Molecule::UnitCell | Molecule::Removed);
450 }
451
452 void RWMolecule::modifyMolecule(const Molecule& newMolecule,
453 Molecule::MoleculeChanges changes,
454 const QString& undoText)
455 {
456 auto* comm = new ModifyMoleculeCommand(*this, m_molecule, newMolecule);
457
458 comm->setText(undoText);
459 m_undoStack.push(comm);
460
461 m_molecule = newMolecule;
462 emitChanged(changes);
463 }
464
465 void RWMolecule::appendMolecule(const Molecule& mol, const QString& undoText)
466 {
467 // We add atoms and bonds, nothing else
468 Molecule::MoleculeChanges changes =
469 (Molecule::Atoms | Molecule::Bonds | Molecule::Added);
470
471 beginMergeMode(undoText);
472 // loop through and add the atoms
473 Index offset = atomCount();
474 for (size_t i = 0; i < mol.atomCount(); ++i) {
475 Core::Atom atom = mol.atom(i);
476 AtomType new_atom = addAtom(atom.atomicNumber(), atom.position3d());
477 new_atom.setFormalCharge(atom.formalCharge());
478
479 setAtomSelected(atomCount() - 1, true);
480 }
481 // now loop through and add the bonds
482 for (size_t i = 0; i < mol.bondCount(); ++i) {
483 Core::Bond bond = mol.bond(i);
484 addBond(bond.atom1().index() + offset, bond.atom2().index() + offset,
485 bond.order());
486 }
487 // now loop through and add the resiudes
488 for (size_t i = 0; i < mol.residueCount(); ++i) {
489 const Core::Residue res = mol.residue(i);
490 addResidue(res, offset);
491 }
492 endMergeMode();
493 emitChanged(changes);
494 }
495
496 void RWMolecule::addResidue(const Core::Residue& residue, Index offset)
497 {
498 // copy the residue name, chain, etc.
499 std::string name = residue.residueName();
500 Index id = residue.residueId();
501 char chain = residue.chainId();
502 m_molecule.addResidue(name, id, chain);
503 Core::Residue& newResidue = m_molecule.residue(m_molecule.residueCount() - 1);
504 newResidue.setHeterogen(residue.isHeterogen());
505 newResidue.setSecondaryStructure(residue.secondaryStructure());
506
507 // now go through all the atoms and add them using the offset
508 for (Core::Atom atom : residue.residueAtoms()) {
509 // get the new index
510 Index newIndex = atom.index() + offset;
511 Core::Atom myAtom = m_molecule.atom(newIndex);
512 // get the atom name
513 std::string atomName = residue.atomName(atom);
514 newResidue.addResidueAtom(atomName, myAtom);
515 }
516 }
517
518 void RWMolecule::editUnitCell(Matrix3 cellMatrix, CrystalTools::Options options)
519 {
520 // If there is no unit cell, there is nothing to do
521 if (!m_molecule.unitCell())
522 return;
523
524 if (!UnitCell::isRegular(cellMatrix)) {
525 qWarning() << "cell matrix is singular";
526 return;
527 }
528
529 // Make a copy of the molecule to edit so we can store the old one
530 // If the user has "TransformAtoms" set in the options, then
531 // the atom positions will move as well.
532 Molecule newMolecule = m_molecule;
533 CrystalTools::setCellMatrix(newMolecule, cellMatrix, options);
534
535 // We will just modify the whole molecule since there may be many changes
536 Molecule::MoleculeChanges changes = Molecule::UnitCell | Molecule::Modified;
537 // If TransformAtoms is set in the options, then the atoms may be modified
538 // as well.
539 if (options & CrystalTools::TransformAtoms)
540 changes |= Molecule::Atoms | Molecule::Modified;
541 QString undoText = tr("Edit Unit Cell");
542
543 modifyMolecule(newMolecule, changes, undoText);
544 }
545
546 void RWMolecule::wrapAtomsToCell()
547 {
548 // If there is no unit cell, there is nothing to do
549 if (!m_molecule.unitCell())
550 return;
551
552 Core::Array<Vector3> oldPos = m_molecule.atomPositions3d();
553 CrystalTools::wrapAtomsToUnitCell(m_molecule);
554 Core::Array<Vector3> newPos = m_molecule.atomPositions3d();
555
556 auto* comm = new SetPositions3dCommand(*this, oldPos, newPos);
557 comm->setText(tr("Wrap Atoms to Cell"));
558 m_undoStack.push(comm);
559
560 Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Modified;
561 emitChanged(changes);
562 }
563
564 void RWMolecule::setCellVolume(double newVolume, CrystalTools::Options options)
565 {
566 // If there is no unit cell, there is nothing to do
567 if (!m_molecule.unitCell())
568 return;
569
570 // Make a copy of the molecule to edit so we can store the old one
571 // The unit cell and atom positions may change
572 Molecule newMolecule = m_molecule;
573
574 CrystalTools::setVolume(newMolecule, newVolume, options);
575
576 // We will just modify the whole molecule since there may be many changes
577 Molecule::MoleculeChanges changes = Molecule::UnitCell | Molecule::Modified;
578 if (options & CrystalTools::TransformAtoms)
579 changes |= Molecule::Atoms | Molecule::Modified;
580 QString undoText = tr("Scale Cell Volume");
581
582 modifyMolecule(newMolecule, changes, undoText);
583 }
584
585 void RWMolecule::buildSupercell(unsigned int a, unsigned int b, unsigned int c)
586 {
587 // If there is no unit cell, there is nothing to do
588 if (!m_molecule.unitCell())
589 return;
590
591 // Make a copy of the molecule to edit so we can store the old one
592 // The unit cell and atom positions may change
593 Molecule newMolecule = m_molecule;
594
595 CrystalTools::buildSupercell(newMolecule, a, b, c);
596
597 // We will just modify the whole molecule since there may be many changes
598 Molecule::MoleculeChanges changes =
599 Molecule::UnitCell | Molecule::Modified | Molecule::Atoms | Molecule::Added;
600 QString undoText = tr("Build Super Cell");
601
602 modifyMolecule(newMolecule, changes, undoText);
603 }
604
605 void RWMolecule::niggliReduceCell()
606 {
607 // If there is no unit cell, there is nothing to do
608 if (!m_molecule.unitCell())
609 return;
610
611 // Make a copy of the molecule to edit so we can store the old one
612 // The unit cell and atom positions may change
613 Molecule newMolecule = m_molecule;
614
615 // We need to perform all three of these operations...
616 CrystalTools::niggliReduce(newMolecule, CrystalTools::TransformAtoms);
617 CrystalTools::rotateToStandardOrientation(newMolecule,
618 CrystalTools::TransformAtoms);
619 CrystalTools::wrapAtomsToUnitCell(newMolecule);
620
621 // We will just modify the whole molecule since there may be many changes
622 Molecule::MoleculeChanges changes =
623 Molecule::UnitCell | Molecule::Atoms | Molecule::Modified;
624 QString undoText = tr("Niggli Reduction");
625
626 modifyMolecule(newMolecule, changes, undoText);
627 }
628
629 void RWMolecule::rotateCellToStandardOrientation()
630 {
631 // If there is no unit cell, there is nothing to do
632 if (!m_molecule.unitCell())
633 return;
634
635 // Store a copy of the old molecule
636 // The atom positions may move as well.
637 Molecule newMolecule = m_molecule;
638
639 CrystalTools::rotateToStandardOrientation(newMolecule,
640 CrystalTools::TransformAtoms);
641
642 // Since most components of the molecule will be modified (atom positions
643 // and the unit cell), we will just modify the whole thing...
644 Molecule::MoleculeChanges changes =
645 Molecule::UnitCell | Molecule::Atoms | Molecule::Modified;
646 QString undoText = tr("Rotate to Standard Orientation");
647
648 modifyMolecule(newMolecule, changes, undoText);
649 }
650
651 bool RWMolecule::reduceCellToPrimitive(double cartTol, double copyTol)
652 {
653 // If there is no unit cell, there is nothing to do
654 if (!m_molecule.unitCell())
655 return false;
656
657 // Make a copy of the molecule to edit so we can store the old one
658 // The unit cell, atom positions, and numbers of atoms may change
659 Molecule newMolecule = m_molecule;
660 #ifdef USE_SPGLIB
661 if (!Core::AvoSpglib::reduceToPrimitive(newMolecule, cartTol))
662 return false;
663 #else
664 return false;
665 #endif
666
667 // Add boundary copies before committing, so it's a single undo step
668 if (copyTol > 0.0)
669 Core::SpaceGroups::fillTranslationalCopies(newMolecule, copyTol);
670
671 // Since most components of the molecule will be modified,
672 // we will just modify the whole thing...
673 Molecule::MoleculeChanges changes =
674 Molecule::UnitCell | Molecule::Atoms | Molecule::Added;
675 QString undoText = tr("Reduce to Primitive");
676
677 modifyMolecule(newMolecule, changes, undoText);
678
679 // Resize layer info to match the new atom count
680 Core::LayerManager::getMoleculeInfo(&m_molecule)
681 ->layer.resize(m_molecule.atomCount());
682
683 return true;
684 }
685
686 bool RWMolecule::conventionalizeCell(double cartTol, double copyTol)
687 {
688 // If there is no unit cell, there is nothing to do
689 if (!m_molecule.unitCell())
690 return false;
691
692 // Make a copy of the molecule to edit so we can store the old one
693 // The unit cell, atom positions, and numbers of atoms may all change
694 Molecule newMolecule = m_molecule;
695
696 #ifdef USE_SPGLIB
697 if (!Core::AvoSpglib::conventionalizeCell(newMolecule, cartTol))
698 return false;
699 #else
700 return false;
701 #endif
702
703 // Add boundary copies before committing, so it's a single undo step
704 if (copyTol > 0.0)
705 Core::SpaceGroups::fillTranslationalCopies(newMolecule, copyTol);
706
707 Molecule::MoleculeChanges changes =
708 Molecule::UnitCell | Molecule::Atoms | Molecule::Added;
709 QString undoText = tr("Conventionalize Cell");
710
711 modifyMolecule(newMolecule, changes, undoText);
712
713 // Resize layer info to match the new atom count
714 Core::LayerManager::getMoleculeInfo(&m_molecule)
715 ->layer.resize(m_molecule.atomCount());
716
717 return true;
718 }
719
720 bool RWMolecule::symmetrizeCell(double cartTol, double copyTol)
721 {
722 // If there is no unit cell, there is nothing to do
723 if (!m_molecule.unitCell())
724 return false;
725
726 // Make a copy of the molecule to edit so we can store the old one
727 // The unit cell, atom positions, and numbers of atoms may all change
728 Molecule newMolecule = m_molecule;
729
730 #ifdef USE_SPGLIB
731 if (!Core::AvoSpglib::symmetrize(newMolecule, cartTol))
732 return false;
733 #else
734 return false;
735 #endif
736
737 // Add boundary copies before committing, so it's a single undo step
738 if (copyTol > 0.0)
739 Core::SpaceGroups::fillTranslationalCopies(newMolecule, copyTol);
740
741 Molecule::MoleculeChanges changes =
742 Molecule::UnitCell | Molecule::Atoms | Molecule::Added;
743 QString undoText = tr("Symmetrize Cell");
744
745 modifyMolecule(newMolecule, changes, undoText);
746
747 // Resize layer info to match the new atom count
748 Core::LayerManager::getMoleculeInfo(&m_molecule)
749 ->layer.resize(m_molecule.atomCount());
750
751 return true;
752 }
753
754 bool RWMolecule::fillUnitCell(unsigned short hallNumber, double cartTol,
755 bool allCopies)
756 {
757 // If there is no unit cell, there is nothing to do
758 if (!m_molecule.unitCell())
759 return false;
760
761 // Make a copy of the molecule to edit so we can store the old one
762 // The atom positions and numbers of atoms may change
763 Molecule newMolecule = m_molecule;
764 newMolecule.setHallNumber(hallNumber);
765
766 Core::SpaceGroups::fillUnitCell(newMolecule, hallNumber, cartTol, true,
767 allCopies);
768
769 Molecule::MoleculeChanges changes = Molecule::Added | Molecule::Atoms;
770 QString undoText = tr("Fill Unit Cell");
771
772 modifyMolecule(newMolecule, changes, undoText);
773 return true;
774 }
775
776 bool RWMolecule::fillTranslationalCopies(double cartTol)
777 {
778 if (!m_molecule.unitCell())
779 return false;
780
781 Molecule newMolecule = m_molecule;
782
783 Core::SpaceGroups::fillTranslationalCopies(newMolecule, cartTol);
784
785 Molecule::MoleculeChanges changes = Molecule::Added | Molecule::Atoms;
786 QString undoText = tr("Fill Translational Copies");
787
788 modifyMolecule(newMolecule, changes, undoText);
789 return true;
790 }
791
792 bool RWMolecule::reduceCellToAsymmetricUnit(unsigned short hallNumber,
793 double cartTol)
794 {
795 // If there is no unit cell, there is nothing to do
796 if (!m_molecule.unitCell())
797 return false;
798
799 // Make a copy of the molecule to edit so we can store the old one
800 // The atom positions and numbers of atoms may change
801 Molecule newMolecule = m_molecule;
802
803 Core::SpaceGroups::reduceToAsymmetricUnit(newMolecule, hallNumber, cartTol);
804
805 Molecule::MoleculeChanges changes = Molecule::Removed | Molecule::Atoms;
806 QString undoText = tr("Reduce Cell to Asymmetric Unit");
807
808 modifyMolecule(newMolecule, changes, undoText);
809
810 // Resize layer info to match the new atom count
811 Core::LayerManager::getMoleculeInfo(&m_molecule)
812 ->layer.resize(m_molecule.atomCount());
813
814 return true;
815 }
816
817 void RWMolecule::emitChanged(unsigned int change)
818 {
819 m_molecule.emitChanged(change);
820 }
821
822 Index RWMolecule::findAtomUniqueId(Index atomId) const
823 {
824 return m_molecule.findAtomUniqueId(atomId);
825 }
826
827 Index RWMolecule::findBondUniqueId(Index bondId) const
828 {
829 return m_molecule.findBondUniqueId(bondId);
830 }
831
832 bool RWMolecule::setForceVector(Index atomId, const Vector3& forces,
833 const QString& undoText)
834 {
835 if (atomId >= atomCount())
836 return false;
837
838 if (m_molecule.m_positions3d.size() != m_molecule.atomCount())
839 m_molecule.m_positions3d.resize(m_molecule.atomCount(), Vector3::Zero());
840
841 auto* comm = new SetForceVectorCommand(
842 *this, atomId, m_molecule.m_positions3d[atomId], forces);
843 comm->setText(undoText);
844 comm->setCanMerge(m_interactive);
845 m_undoStack.push(comm);
846 return true;
847 }
848
849 } // namespace Avogadro::QtGui