1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2013 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #include "licorice.h"
18 
19 #include <avogadro/core/elements.h>
20 #include <avogadro/core/molecule.h>
21 #include <avogadro/rendering/cylindergeometry.h>
22 #include <avogadro/rendering/geometrynode.h>
23 #include <avogadro/rendering/groupnode.h>
24 #include <avogadro/rendering/spheregeometry.h>
25 
26 namespace Avogadro {
27 namespace QtPlugins {
28 
29 using Core::Elements;
30 using Core::Molecule;
31 using Rendering::GeometryNode;
32 using Rendering::GroupNode;
33 using Rendering::SphereGeometry;
34 using Rendering::CylinderGeometry;
35 
Licorice(QObject * p)36 Licorice::Licorice(QObject* p) : ScenePlugin(p), m_enabled(false)
37 {
38 }
39 
~Licorice()40 Licorice::~Licorice()
41 {
42 }
43 
process(const Molecule & molecule,Rendering::GroupNode & node)44 void Licorice::process(const Molecule& molecule, Rendering::GroupNode& node)
45 {
46   // Use a common radius for all spheres and cylinders.
47   float radius(0.2f);
48 
49   // Add a sphere node to contain all of the spheres.
50   GeometryNode* geometry = new GeometryNode;
51   node.addChild(geometry);
52   SphereGeometry* spheres = new SphereGeometry;
53   spheres->identifier().molecule = &molecule;
54   spheres->identifier().type = Rendering::AtomType;
55   geometry->addDrawable(spheres);
56   for (Index i = 0; i < molecule.atomCount(); ++i) {
57     Core::Atom atom = molecule.atom(i);
58     Vector3ub color = atom.color();
59     spheres->addSphere(atom.position3d().cast<float>(), color, radius);
60   }
61 
62   CylinderGeometry* cylinders = new CylinderGeometry;
63   cylinders->identifier().molecule = &molecule;
64   cylinders->identifier().type = Rendering::BondType;
65   geometry->addDrawable(cylinders);
66   for (Index i = 0; i < molecule.bondCount(); ++i) {
67     Core::Bond bond = molecule.bond(i);
68     Vector3f pos1 = bond.atom1().position3d().cast<float>();
69     Vector3f pos2 = bond.atom2().position3d().cast<float>();
70     Vector3ub color1 = bond.atom1().color();
71     Vector3ub color2 = bond.atom2().color();
72     Vector3f bondVector = pos2 - pos1;
73     float bondLength = bondVector.norm();
74     bondVector /= bondLength;
75 
76     cylinders->addCylinder(pos1, pos2, radius, color1, color2, i);
77   }
78 }
79 
isEnabled() const80 bool Licorice::isEnabled() const
81 {
82   return m_enabled;
83 }
84 
setEnabled(bool enable)85 void Licorice::setEnabled(bool enable)
86 {
87   m_enabled = enable;
88 }
89 }
90 }
91