Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Molecule.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlMolecule/Molecule.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39  mActorTree(new ActorTree),
40  mTransformTree(new Transform),
41  mTags(new KeyValues),
42  mAtomLabelTemplate(new Text),
43  mAtomLabelEffect(new Effect)
44 {
45  VL_DEBUG_SET_OBJECT_NAME()
46  mAtomLabelEffect->shader()->enable(EN_BLEND);
47  reset();
48 }
49 //-----------------------------------------------------------------------------
51 {
53  mBondDetail = 20;
54  mAtomDetail = 2;
55  mRingOffset = 0.45f;
56  mAromaticRingColor = fvec4(0,1.0f,0,1.0f);
57  mId = 0;
58  mAtoms.clear();
59  mBonds.clear();
60  mCycles.clear();
62  tags()->clear();
63  mActorTree->actors()->clear();
64  mLineWidth= 1.0f;
65  mSmoothLines = false;
66  mShowAtomNames = false;
67  // molecule / actor maps
70  mAtomToActorMap.clear();
71  mActorToAtomMap.clear();
72  mBondToActorMap.clear();
73  mActorToBondMap.clear();
74 }
75 //-----------------------------------------------------------------------------
77 {
78  reset();
79  super::operator=(other);
80 
82  *mTags = *other.mTags;
84  mAtomDetail = other.mAtomDetail;
85  mBondDetail = other.mBondDetail;
86  mRingOffset = other.mRingOffset;
88  mLineWidth = other.mLineWidth;
89  mSmoothLines = other.mSmoothLines;
90 
91  std::map<const Atom*, Atom*> atom_map;
92  for(unsigned i=0; i<other.atoms().size(); ++i)
93  {
94  atoms().push_back( new Atom( *other.atom(i) ) );
95  atom_map[ other.atom(i) ] = atoms().back().get();
96  }
97 
98  for(unsigned i=0; i<other.bonds().size(); ++i)
99  {
100  bonds().push_back( new Bond( *other.bond(i) ) );
101  bonds().back()->setAtom1( atom_map[ other.bond(i)->atom1() ] );
102  bonds().back()->setAtom2( atom_map[ other.bond(i)->atom2() ] );
103  }
104 
105  mCycles.resize( other.mCycles.size() );
106  for(unsigned i=0; i<other.mCycles.size(); ++i)
107  {
108  cycle(i).resize( other.cycle(i).size() );
109  for(unsigned j=0; j<other.cycle(i).size(); ++j)
110  cycle(i)[j] = atom_map[ other.cycle(i)[j].get() ];
111  }
112  return *this;
113 }
114 //-----------------------------------------------------------------------------
115 const Atom* Molecule::atom(int index) const { return mAtoms[index].get(); }
116 //-----------------------------------------------------------------------------
117 Atom* Molecule::atom(int index) { return mAtoms[index].get(); }
118 //-----------------------------------------------------------------------------
120 {
122  atoms().push_back(atom);
123 }
124 //-----------------------------------------------------------------------------
126 {
127  mAtoms.clear();
128  mBonds.clear();
129  mCycles.clear();
130 }
131 //-----------------------------------------------------------------------------
133 {
134  std::vector<Bond*> incident_bonds;
135  incidentBonds(incident_bonds, atom(i));
136  for(unsigned j=0; j<incident_bonds.size(); ++j)
137  eraseBond( incident_bonds[j] );
138  atoms().erase(atoms().begin() + i);
139 }
140 //-----------------------------------------------------------------------------
142 {
143  for(unsigned i=0; i<atoms().size(); ++i)
144  {
145  if (atom(i) == a)
146  {
147  std::vector<Bond*> incident_bonds;
148  incidentBonds(incident_bonds, a);
149  for(unsigned j=0; j<incident_bonds.size(); ++j)
150  eraseBond( incident_bonds[j] );
151  atoms().erase(atoms().begin() + i);
152  return;
153  }
154  }
155 }
156 //-----------------------------------------------------------------------------
158 {
160  ref<Bond> bond = new Bond;
161  bond->setAtom1(a1);
162  bond->setAtom2(a2);
163  bonds().push_back(bond);
164  return bond.get();
165 }
166 //-----------------------------------------------------------------------------
167 const Bond* Molecule::bond(int index) const { return mBonds[index].get(); }
168 //-----------------------------------------------------------------------------
169 Bond* Molecule::bond(int index) { return mBonds[index].get(); }
170 //-----------------------------------------------------------------------------
171 const Bond* Molecule::bond(Atom* a1, Atom* a2) const
172 {
173  for(unsigned i=0; i<bonds().size(); ++i)
174  if ( (bond(i)->atom1() == a1 && bond(i)->atom2() == a2) || (bond(i)->atom1() == a2 && bond(i)->atom2() == a1) )
175  return bonds()[i].get();
176  return NULL;
177 }
178 //-----------------------------------------------------------------------------
180 {
181  for(unsigned i=0; i<bonds().size(); ++i)
182  if ( (bond(i)->atom1() == a1 && bond(i)->atom2() == a2) || (bond(i)->atom1() == a2 && bond(i)->atom2() == a1) )
183  return bonds()[i].get();
184  return NULL;
185 }
186 //-----------------------------------------------------------------------------
188 {
190  bonds().push_back(bond);
191 }
192 //-----------------------------------------------------------------------------
194 {
195  for(unsigned i=0; i<bonds().size(); ++i)
196  {
197  if (bond(i) == b)
198  {
199  bonds().erase(bonds().begin() + i);
200  return;
201  }
202  }
203 }
204 //-----------------------------------------------------------------------------
205 void Molecule::eraseBond(int bond) { bonds().erase(bonds().begin() + bond); }
206 //-----------------------------------------------------------------------------
207 void Molecule::eraseAllBonds() { bonds().clear(); }
208 //-----------------------------------------------------------------------------
210 {
211  for(unsigned i=0; i<bonds().size(); ++i)
212  {
213  if ( (bond(i)->atom1() == a1 && bond(i)->atom2() == a2) ||
214  (bond(i)->atom1() == a2 && bond(i)->atom2() == a1) )
215  {
216  bonds().erase(bonds().begin() + i);
217  return;
218  }
219  }
220 }
221 //-----------------------------------------------------------------------------
222 void Molecule::eraseBond(int a1, int a2) { eraseBond(atom(a1), atom(a2)); }
223 //-----------------------------------------------------------------------------
225 {
226  for(int i=0; i<atomCount(); ++i)
227  atom(i)->adjacentAtoms().clear();
228  for(int i=0; i<bondCount(); ++i)
229  {
230  bond(i)->atom1()->adjacentAtoms().push_back( bond(i)->atom2() );
231  bond(i)->atom2()->adjacentAtoms().push_back( bond(i)->atom1() );
232  }
233 }
234 //-----------------------------------------------------------------------------
235 void Molecule::incidentBonds(std::vector<Bond*>& incident_bonds, Atom* atom)
236 {
237  incident_bonds.clear();
238  for(int i=0; i<bondCount(); ++i)
239  if(bond(i)->atom1() == atom || bond(i)->atom2() == atom)
240  incident_bonds.push_back( bond(i) );
241 }
242 //-----------------------------------------------------------------------------
244 {
245  for(unsigned i=0; i<atoms().size(); ++i)
246  atoms()[i]->setColor( atomInfo(atoms()[i]->atomType()).cpkColor() );
247 }
248 //-----------------------------------------------------------------------------
249 void Molecule::setAtomColors(const fvec4& color)
250 {
251  for(unsigned i=0; i<atoms().size(); ++i)
252  atoms()[i]->setColor( color );
253 }
254 //-----------------------------------------------------------------------------
255 void Molecule::setCalculatedAtomRadii(float percentage)
256 {
257  for(unsigned i=0; i<atoms().size(); ++i)
258  atoms()[i]->setRadius( (float)atomInfo(atoms()[i]->atomType()).calculatedRadius() * percentage );
259 }
260 //-----------------------------------------------------------------------------
261 void Molecule::setEmpiricalAtomRadii(float percentage)
262 {
263  for(unsigned i=0; i<atoms().size(); ++i)
264  atoms()[i]->setRadius( (float)atomInfo(atoms()[i]->atomType()).empiricalRadius() * percentage );
265 }
266 //-----------------------------------------------------------------------------
267 void Molecule::setCovalentAtomRadii(float percentage)
268 {
269  for(unsigned i=0; i<atoms().size(); ++i)
270  atoms()[i]->setRadius( (float)atomInfo(atoms()[i]->atomType()).covalentRadius() * percentage );
271 }
272 //-----------------------------------------------------------------------------
273 void Molecule::setVanDerWaalsAtomRadii(float percentage)
274 {
275  for(unsigned i=0; i<atoms().size(); ++i)
276  atoms()[i]->setRadius( (float)atomInfo(atoms()[i]->atomType()).vanDerWaalsRadius() * percentage );
277 }
278 //-----------------------------------------------------------------------------
279 void Molecule::setAtomRadii(float radius)
280 {
281  for(unsigned i=0; i<atoms().size(); ++i)
282  atoms()[i]->setRadius( radius );
283 }
284 //-----------------------------------------------------------------------------
285 void Molecule::setBondRadii(float radius)
286 {
287  for(unsigned i=0; i<bonds().size(); ++i)
288  bonds()[i]->setRadius( radius );
289 }
290 //-----------------------------------------------------------------------------
291 void Molecule::setAtomTypeVisible(EAtomType type, bool visible)
292 {
293  for(unsigned i=0; i<atoms().size(); ++i)
294  if (atom(i)->atomType() == type)
295  atom(i)->setVisible(visible);
296 }
297 //-----------------------------------------------------------------------------
299 {
300  for(unsigned i=0; i<bonds().size(); ++i)
301  {
302  if(bonds()[i]->bondType() == BT_Aromatic)
303  bonds()[i]->setColor(color);
304  }
305 }
306 //-----------------------------------------------------------------------------
The Molecule class is used to manage and render 3D molecular structures.
Definition: Molecule.hpp:64
void addBond(Bond *bond)
Definition: Molecule.cpp:187
bool mSmoothLines
Definition: Molecule.hpp:271
std::map< ref< Bond >, ref< Actor > > mBondToActorMap
Definition: Molecule.hpp:259
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
const T * get() const
Definition: Object.hpp:128
Vector4< float > fvec4
A 4 components vector with float precision.
Definition: Vector4.hpp:280
void eraseBond(Bond *bond)
Definition: Molecule.cpp:193
void clear()
Definition: KeyValues.hpp:59
float mLineWidth
Definition: Molecule.hpp:270
void setAtomRadii(float radius)
Sets all the atoms&#39; radii to the specified one.
Definition: Molecule.cpp:279
A Renderable that renders text with a given Font.
Definition: Text.hpp:50
void setAtom1(Atom *atom)
Definition: Bond.hpp:81
const std::vector< ref< Atom > > & cycle(int i) const
Returns the i-th cycle.
Definition: Molecule.hpp:116
void setEmpiricalAtomRadii(float percentage=1.0f)
Sets all the atoms&#39; radii to their empirical atom radii.
Definition: Molecule.cpp:261
EMoleculeStyle mMoleculeStyle
Definition: Molecule.hpp:266
const Atom * atom(int index) const
Definition: Molecule.cpp:115
KeyValues * tags()
Definition: Molecule.hpp:82
If enabled, blend the incoming RGBA color values with the values in the color buffers, see also BlendFunc for more information.
const std::vector< ref< Bond > > & bonds() const
Definition: Molecule.hpp:96
void setVisible(bool visible)
Definition: Atom.hpp:122
bool mMoleculeToActorMapEnabled
Definition: Molecule.hpp:273
ref< ActorTree > mActorTree
Definition: Molecule.hpp:252
void setVanDerWaalsAtomRadii(float percentage=1.0f)
Sets all the atoms&#39; radii to their van der Waals atom radii.
Definition: Molecule.cpp:273
const std::vector< ref< Atom > > & atoms() const
Definition: Molecule.hpp:85
Atom * atom2() const
Definition: Bond.hpp:85
Atom * atom1() const
Definition: Bond.hpp:82
int bondCount() const
Definition: Molecule.hpp:99
String mMoleculeName
Definition: Molecule.hpp:261
std::vector< ref< Bond > > mBonds
Definition: Molecule.hpp:255
Visualization Library main namespace.
void setAromaticBondsColor(const fvec4 &color)
Definition: Molecule.cpp:298
const ActorCollection * actors() const
Returns the actors contained in a ActorTree node.
void prepareBondInsert(int bonus=100)
Definition: Molecule.hpp:237
std::map< ref< Actor >, ref< Atom > > mActorToAtomMap
Definition: Molecule.hpp:258
float mRingOffset
Definition: Molecule.hpp:269
EAtomType atomType(const char *type)
Translates a string containing atom type name, atom symbol or a Sybyl type into an EAtomType...
void setAtom2(Atom *atom)
Definition: Bond.hpp:84
const std::vector< Atom *> & adjacentAtoms() const
Definition: Atom.hpp:97
const Bond * bond(int index) const
Definition: Molecule.cpp:167
int atomCount() const
Definition: Molecule.hpp:88
void setAtomTypeVisible(EAtomType type, bool visible)
Definition: Molecule.cpp:291
#define NULL
Definition: OpenGLDefs.hpp:81
void eraseAllAtoms()
Definition: Molecule.cpp:125
std::vector< ref< Atom > > mAtoms
Definition: Molecule.hpp:254
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
void setCovalentAtomRadii(float percentage=1.0f)
Sets all the atoms&#39; radii to their covalent atom radii.
Definition: Molecule.cpp:267
fvec4 mAromaticRingColor
Definition: Molecule.hpp:251
const AtomInfo & atomInfo(EAtomType type)
Returns an AtomInfo representing the properties of the given atom type.
EAtomType atomType() const
Definition: Atom.hpp:100
std::map< ref< Actor >, ref< Bond > > mActorToBondMap
Definition: Molecule.hpp:260
void computeAtomAdjacency()
Definition: Molecule.cpp:224
void setAtomColors(const fvec4 &color)
Sets all the atoms&#39; color to the specified color.
Definition: Molecule.cpp:249
void prepareAtomInsert(int bonus=100)
Definition: Molecule.hpp:232
void eraseAllBonds()
Definition: Molecule.cpp:207
bool mShowAtomNames
Definition: Molecule.hpp:272
void setCPKAtomColors()
Sets all the atoms&#39; color to their CPK color.
Definition: Molecule.cpp:243
void eraseAtom(Atom *atom)
Definition: Molecule.cpp:141
ref< Effect > mAtomLabelEffect
Definition: Molecule.hpp:264
void setBondRadii(float radius)
Sets all the bonds&#39; radii to the specified one.
Definition: Molecule.cpp:285
void addAtom(Atom *atom)
Definition: Molecule.cpp:119
The Bond class represents a bond to be used with the Molecule class.
Definition: Bond.hpp:62
std::map< ref< Atom >, ref< Actor > > mAtomToActorMap
Definition: Molecule.hpp:257
EAtomType
Element types.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
The Atom class represents an atom to be used with the Molecule class.
Definition: Atom.hpp:51
void setCalculatedAtomRadii(float percentage=1.0f)
Sets all the atoms&#39; radii to their calculated atom radii.
Definition: Molecule.cpp:255
void reset()
Definition: Molecule.cpp:50
bool mActorToMoleculeMapEnabled
Definition: Molecule.hpp:274
The ActorTree class implements a generic tree whose nodes contain Actors.
Definition: ActorTree.hpp:62
Molecule & operator=(const Molecule &other)
Definition: Molecule.cpp:76
ref< KeyValues > mTags
Definition: Molecule.hpp:262
String & clear()
Clears the string.
Definition: String.hpp:142
A set of key/value pairs usually used to associate generic information, tags, attributes etc...
Definition: KeyValues.hpp:42
unsigned int mId
Definition: Molecule.hpp:265
std::vector< std::vector< ref< Atom > > > mCycles
Definition: Molecule.hpp:256
void incidentBonds(std::vector< Bond *> &inc_bonds, Atom *atom)
Definition: Molecule.cpp:235