1 /**********************************************************************
2 Primitive - Wrapper class around the OpenBabel classes
4 Copyright (C) 2007 Donald Ephraim Curtis
6 This file is part of the Avogadro molecular editor project.
7 For more information, see <http://avogadro.sourceforge.net/>
9 Avogadro is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 Avogadro is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 **********************************************************************/
28 #include <avogadro/global.h>
30 #include <openbabel/mol.h>
32 #include <QAbstractItemModel>
34 #include <eigen/vector.h>
42 * Base class for all primitives (Molecule, Atom, Bond, Residue, ...).
45 class PrimitivePrivate
;
46 class A_EXPORT Primitive
: public QObject
49 Q_PROPERTY(Type type READ type
)
54 * This enum allows us to iterate through the various types
60 /// Molecule Primitive
68 /// Chain Primitive (i.e., a set of residues)
76 /// Points (i.e., non-atoms)
78 /// Vectors (i.e., arrows, dipole moments)
80 /// Non-bonded interactions (i.e., non-bond connections)
92 * Default constructor.
93 * @param parent the object parent
95 Primitive(QObject
*parent
= 0);
98 * @param type the primitive type
99 * @param parent the object parent
101 explicit Primitive(Type type
, QObject
*parent
=0);
105 virtual ~Primitive();
108 * Function used to push changes to a primitive to
109 * the rest of the system. At this time there is no
110 * way (other than this) to generate a signal when
111 * properties of a primitive change.
113 * In the case of the Atom primitive, this should be called
114 * when changes to coordinates have been made.
120 * Holds the primitive type
124 * @return the primitive type (one of Primitive::Type)
128 QReadWriteLock
*lock();
132 * Emitted when the primitive has been updated.
137 PrimitivePrivate
* const d_ptr
;
138 Primitive(PrimitivePrivate
&dd
, QObject
*parent
= 0);
139 Primitive(PrimitivePrivate
&dd
, Type type
, QObject
*parent
=0);
142 Q_DECLARE_PRIVATE(Primitive
);
149 * @author Donald Ephraim Curtis
151 * The Atom class is a Primitive subclass that provides a wrapper around
152 * OpenBabel::OBAtom. This class is provided to give more control of
153 * the OpenBabel::OBAtom class through slots/signals provided by the
154 * Primitive superclass.
156 class A_EXPORT Atom
: public Primitive
, public OpenBabel::OBAtom
164 * @param parent the object parent.
166 Atom(QObject
*parent
=0) : Primitive(AtomType
, parent
), OpenBabel::OBAtom() { }
168 /** Returns the position of the atom, as a Eigen::Vector3d. This is similar to
169 * the OBAtom::GetVector() method, which returns the position as a OpenBabel::vector3.
171 * Rationale for inlining: this method only does a cast on the return value of OBAtom::GetVector().
172 * The memory layouts of the types between which it casts are not likely to change: both
173 * types represent 3D vectors of doubles, and there's only one sane way to represent them:
174 * struct{ double x,y,z; }.
176 * @return OBAtom::GetVector() but reinterpret_casted as a const Eigen::Vector3d &
178 inline const Eigen::Vector3d
&pos () const
180 return *reinterpret_cast<const Eigen::Vector3d
*>(&GetVector());
183 /** Sets the position of the atom, from a Eigen::Vector3d. This is similar to
184 * the OBAtom::SetVector() method, which sets the position from a OpenBabel::vector3.
186 * Rationale for inlining: this method only does a cast on the argument of OBAtom::SetVector().
187 * The memory layouts of the types between which it casts are not likely to change: both
188 * types represent 3D vectors of doubles, and there's only one sane way to represent them:
189 * struct{ double x,y,z; }.
191 inline void setPos(const Eigen::Vector3d
&vec
)
193 SetVector( *reinterpret_cast<const OpenBabel::vector3
*>(&vec
) );
200 * @author Donald Ephraim Curtis
202 * The Bond class is a Primitive subclass that provides a wrapper around
203 * OpenBabel::OBBond. This class is provided to give more control of
204 * the OpenBabel::OBBond class through slots/signals provided by the
205 * Primitive superclass.
207 class A_EXPORT Bond
: public Primitive
, public OpenBabel::OBBond
215 * @param parent the object parent.
217 Bond(QObject
*parent
=0): Primitive(BondType
, parent
), OpenBabel::OBBond() { }
222 * @brief Residue Class
223 * @author Donald Ephraim Curtis
225 * The Residue class is a Primitive subclass that provides a wrapper around
226 * OpenBabel::OBResidue. This class is provided to give more control of
227 * the OpenBabel::OBResidue class through slots/signals provided by the
228 * Primitive superclass.
230 class A_EXPORT Residue
: public Primitive
, public OpenBabel::OBResidue
238 * @param parent the object parent.
240 Residue(QObject
*parent
=0): Primitive(ResidueType
, parent
), OpenBabel::OBResidue() { }
245 * @brief Molecule Class
246 * @author Donald Ephraim Curtis
248 * The Molecule class implements the OpenBabel::OBMol virtual functions
249 * in order to not only use our primitive objects but also to provide signals
250 * based on internal OpenBabel actions. In terms of a Model-View architecture,
251 * this is our model class and is used by our various views to hold
254 class MoleculePrivate
;
255 class A_EXPORT Molecule
: public Primitive
, public OpenBabel::OBMol
263 * @param parent the object parent.
265 Molecule(QObject
*parent
=0);
266 Molecule(const Molecule
&other
);
271 * Virtual function inherited from OpenBabel::OBMol.
272 * Creates a new Atom object.
274 * @return pointer to a newly allocated Atom object
276 Atom
*CreateAtom(void);
279 * Virtual function inherited from OpenBabel::OBMol.
280 * Creates a new Bond object.
282 * @return pointer to a newly allocated Bond object
284 Bond
* CreateBond(void);
287 * Virtual function inherited from OpenBabel::OBMol.
288 * Creates a new Residue object.
290 * @return pointer to a newly allocated Residue object
292 Residue
* CreateResidue(void);
295 * Virtual function inherited from OpenBabel::OBMol.
296 * Deletes an Atom object.
298 * @param atom the atom to delete
300 void DestroyAtom(OpenBabel::OBAtom
* atom
);
303 * Virtual function inherited from OpenBabel::OBMol.
304 * Deletes an Bond object.
306 * @param atom the bond to delete
308 void DestroyBond(OpenBabel::OBBond
* bond
);
311 * Virtual function inherited from OpenBabel::OBMol.
312 * Deletes an Residue object.
314 * @param atom the residue to delete
316 void DestroyResidue(OpenBabel::OBResidue
* residue
);
318 const Eigen::Vector3d
& center() const;
319 const Eigen::Vector3d
& normalVector() const;
320 const double & radius() const;
321 const Atom
*farthestAtom() const;
323 Molecule
& operator=(const Molecule
& other
);
325 Molecule
& operator+=(const Molecule
& other
);
328 /* shared d_ptr with Primitive */
329 Q_DECLARE_PRIVATE(Molecule
);
331 void computeGeomInfo() const;
335 * Function which handles when a child primitive has been
336 * updated. The response is to find the sender object
337 * and then emit a signal passing the sender as a parameter.
340 * @sa primitiveUpdated
341 * @sa primitiveRemoved
343 void updatePrimitive();
347 * Emitted when a child primitive is added.
349 * @param primitive pointer to the primitive that was added
351 void primitiveAdded(Primitive
*primitive
);
353 * Emitted when a child primitive is updated.
355 * @param primitive pointer to the primitive that was updated
357 void primitiveUpdated(Primitive
*primitive
);
359 * Emitted when a child primitive is deleted.
361 * @param primitive pointer to the primitive that was updated before it is free'd
363 void primitiveRemoved(Primitive
*primitive
);
366 } // namespace Avogadro
368 Q_DECLARE_METATYPE(Avogadro::Primitive
*);
370 #endif // __PRIMITIVES_H