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 "crystalscene.h"
18 
19 #include <avogadro/core/array.h>
20 #include <avogadro/core/molecule.h>
21 #include <avogadro/core/unitcell.h>
22 #include <avogadro/rendering/geometrynode.h>
23 #include <avogadro/rendering/groupnode.h>
24 #include <avogadro/rendering/linestripgeometry.h>
25 
26 namespace Avogadro {
27 namespace QtPlugins {
28 
29 using Core::Array;
30 using Core::Molecule;
31 using Core::UnitCell;
32 using Rendering::GeometryNode;
33 using Rendering::GroupNode;
34 using Rendering::LineStripGeometry;
35 
CrystalScene(QObject * p)36 CrystalScene::CrystalScene(QObject* p) : ScenePlugin(p), m_enabled(true)
37 {
38 }
39 
~CrystalScene()40 CrystalScene::~CrystalScene()
41 {
42 }
43 
process(const Molecule & molecule,GroupNode & node)44 void CrystalScene::process(const Molecule& molecule, GroupNode& node)
45 {
46   if (const UnitCell* cell = molecule.unitCell()) {
47     GeometryNode* geometry = new GeometryNode;
48     node.addChild(geometry);
49     LineStripGeometry* lines = new LineStripGeometry;
50     geometry->addDrawable(lines);
51 
52     lines->setColor(Vector3ub(255, 255, 255));
53 
54     float width = 2.0;
55 
56     Vector3f a = cell->aVector().cast<float>();
57     Vector3f b = cell->bVector().cast<float>();
58     Vector3f c = cell->cVector().cast<float>();
59 
60     Vector3f vertex(Vector3f::Zero());
61 
62     Array<Vector3f> strip;
63     strip.reserve(5);
64     strip.push_back(vertex);
65     strip.push_back(vertex += a);
66     strip.push_back(vertex += b);
67     strip.push_back(vertex -= a);
68     strip.push_back(vertex -= b);
69     lines->addLineStrip(strip, width);
70 
71     for (Array<Vector3f>::iterator it = strip.begin(), itEnd = strip.end();
72          it != itEnd; ++it) {
73       *it += c;
74     }
75     lines->addLineStrip(strip, width);
76 
77     strip.resize(2);
78     strip[0] = Vector3f::Zero();
79     strip[1] = c;
80     lines->addLineStrip(strip, width);
81 
82     strip[0] += a;
83     strip[1] += a;
84     lines->addLineStrip(strip, width);
85 
86     strip[0] += b;
87     strip[1] += b;
88     lines->addLineStrip(strip, width);
89 
90     strip[0] -= a;
91     strip[1] -= a;
92     lines->addLineStrip(strip, width);
93   }
94 }
95 
isEnabled() const96 bool CrystalScene::isEnabled() const
97 {
98   return m_enabled;
99 }
100 
setEnabled(bool enable)101 void CrystalScene::setEnabled(bool enable)
102 {
103   m_enabled = enable;
104 }
105 }
106 }
107