~ubuntu-branches/ubuntu/natty/gnome-chemistry-utils/natty

« back to all changes in this revision

Viewing changes to gcu/crystalatom.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Mailänder
  • Date: 2009-12-19 11:00:02 UTC
  • mfrom: (1.1.5 upstream) (2.1.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091219110002-el6iiqb23qoh8uxf
Tags: 0.10.9-1ubuntu1
* manual merge from Debian testing, fixes LP: #233963
  * gcu-plugin.dirs, gcu-plugin.links: iceweasel → firefox; iceape → seamonkey
  * debian/control: added MOTU, iceweasel → firefox

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- C++ -*-
2
 
 
3
 
/* 
4
 
 * Gnome Chemistry Utils
5
 
 * crystalatom.cc 
6
 
 *
7
 
 * Copyright (C) 2002-2005 Jean Bréfort <jean.brefort@normalesup.org>
8
 
 *
9
 
 * This program is free software; you can redistribute it and/or 
10
 
 * modify it under the terms of the GNU General Public License as 
11
 
 * published by the Free Software Foundation; either version 2 of the
12
 
 * License, or (at your option) any later version.
13
 
 *
14
 
 * This program 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.
18
 
 *
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 St, Fifth Floor, Boston, MA  02110-1301
22
 
 * USA
23
 
 */
24
 
 
25
 
#include "config.h"
26
 
#include "element.h"
27
 
#include "crystalatom.h"
28
 
#include "element.h"
29
 
#include "xml-utils.h"
30
 
#include <GL/glu.h>
31
 
#include <cmath>
32
 
#include <string>
33
 
 
34
 
using namespace gcu;
35
 
 
36
 
CrystalAtom::CrystalAtom(): Atom()
37
 
{
38
 
        m_Radius.Z = (unsigned char) GetZ();
39
 
        m_Radius.type = GCU_RADIUS_UNKNOWN;
40
 
        m_Radius.scale = NULL;
41
 
        m_Radius.spin = GCU_N_A_SPIN,
42
 
        m_Radius.charge = 0;
43
 
        m_Radius.value.value = 0.0;
44
 
        m_Radius.value.prec = 0;
45
 
        m_Radius.cn = -1;
46
 
        m_bCustomColor = false;
47
 
        m_fRed = m_fBlue = m_fGreen = 0;
48
 
        m_fAlpha = 1;
49
 
        m_nCleave = 0;
50
 
}
51
 
 
52
 
CrystalAtom::~CrystalAtom()
53
 
{
54
 
}
55
 
 
56
 
CrystalAtom::CrystalAtom(int Z, double x, double y, double z): Atom(Z, x, y, z)
57
 
{
58
 
        m_Radius.Z = (unsigned char) GetZ();
59
 
        m_Radius.type = GCU_RADIUS_UNKNOWN;
60
 
        m_Radius.scale = NULL;
61
 
        m_Radius.spin = GCU_N_A_SPIN,
62
 
        m_Radius.charge = 0;
63
 
        m_Radius.value.value = 0.0;
64
 
        m_Radius.value.prec = 0;
65
 
        m_Radius.cn = -1;
66
 
        m_fAlpha = 1.;
67
 
        if (Z) SetDefaultColor();
68
 
        m_nCleave = 0;
69
 
}
70
 
 
71
 
CrystalAtom::CrystalAtom(CrystalAtom& caAtom): Atom(caAtom)
72
 
{
73
 
        m_Radius.scale = NULL;
74
 
        SetRadius(caAtom.m_Radius);
75
 
        m_bCustomColor = caAtom.m_bCustomColor;
76
 
        m_fRed = caAtom.m_fRed;
77
 
        m_fGreen = caAtom.m_fGreen;
78
 
        m_fBlue = caAtom.m_fBlue;
79
 
        m_fAlpha = caAtom.m_fAlpha;
80
 
        m_nCleave = 0;
81
 
}
82
 
 
83
 
CrystalAtom& CrystalAtom::operator=(CrystalAtom& caAtom)
84
 
{
85
 
        SetZ(caAtom.GetZ());
86
 
        double x, y, z;
87
 
        caAtom.GetCoords(&x, &y, &z);
88
 
        SetCoords(x, y, z);
89
 
        SetRadius(caAtom.m_Radius);
90
 
        m_bCustomColor = caAtom.m_bCustomColor;
91
 
        m_fRed = caAtom.m_fRed ;
92
 
        m_fGreen = caAtom.m_fGreen ;
93
 
        m_fBlue = caAtom.m_fBlue ;
94
 
        m_fAlpha = caAtom.m_fAlpha ;
95
 
        m_nCleave = 0 ;
96
 
        return *this ;
97
 
}
98
 
 
99
 
void CrystalAtom::Draw()
100
 
{
101
 
        if (m_nCleave) return ;
102
 
        GLUquadricObj *quadObj ;
103
 
        glPushMatrix() ;
104
 
        glTranslated(y(), z(), x()) ;
105
 
        glColor4f(m_fRed, m_fGreen, m_fBlue, m_fAlpha) ;
106
 
        quadObj = gluNewQuadric() ;
107
 
    gluQuadricDrawStyle(quadObj, GL_FILL);
108
 
        gluQuadricNormals(quadObj, GL_SMOOTH) ;
109
 
        gluSphere(quadObj, m_Radius.value.value, 20, 10) ;
110
 
        gluDeleteQuadric(quadObj) ;
111
 
        glPopMatrix() ;
112
 
}
113
 
 
114
 
void CrystalAtom::SetColor(float red, float green, float blue, float alpha)
115
 
{
116
 
        m_bCustomColor = true;
117
 
        m_fRed = red ;
118
 
        m_fGreen = green ;
119
 
        m_fBlue = blue ;
120
 
        m_fAlpha = alpha ;
121
 
}
122
 
 
123
 
void CrystalAtom::SetDefaultColor()
124
 
{
125
 
        m_bCustomColor = false;
126
 
        double *Colors = Element::GetElement(GetZ())->GetDefaultColor();
127
 
        m_fRed = (float) Colors[0];
128
 
        m_fGreen = (float) Colors[1];
129
 
        m_fBlue = (float) Colors[2];
130
 
}
131
 
 
132
 
void CrystalAtom::GetColor(double *red, double *green, double *blue, double *alpha)
133
 
{
134
 
        *red = m_fRed ;
135
 
        *green = m_fGreen ;
136
 
        *blue = m_fBlue ;
137
 
        *alpha = m_fAlpha ;
138
 
}
139
 
 
140
 
void CrystalAtom::SetSize(double r)
141
 
{
142
 
        m_Radius.Z = (unsigned char) GetZ();
143
 
        m_Radius.type = GCU_RADIUS_UNKNOWN;
144
 
        m_Radius.scale = NULL;
145
 
        m_Radius.spin = GCU_N_A_SPIN,
146
 
        m_Radius.charge = 0;
147
 
        m_Radius.value.value = 0.0;
148
 
        m_Radius.value.prec = 0;
149
 
        m_Radius.cn = -1;
150
 
        m_Radius.value.value = 0.0;
151
 
        m_Radius.value.prec = 0;
152
 
}
153
 
 
154
 
 
155
 
double CrystalAtom::GetSize()
156
 
{
157
 
        return m_Radius.value.value;
158
 
}
159
 
 
160
 
bool CrystalAtom::operator==(CrystalAtom& caAtom)
161
 
{
162
 
        return (x() == caAtom.x()) &&
163
 
                        (y() == caAtom.y()) &&
164
 
                        (z() == caAtom.z()) ;
165
 
}
166
 
 
167
 
double CrystalAtom::ScalProd(int h, int k, int l)
168
 
{
169
 
        return x() * h + y() * k + z() * l ;
170
 
}
171
 
 
172
 
double CrystalAtom::Distance(double dx, double dy, double dz, bool bFixed)
173
 
{
174
 
        if ((m_nCleave > 0) && ! bFixed) return 0. ;
175
 
        dx -= x() ;
176
 
        dy -= y() ;
177
 
        dz -= z() ;
178
 
        return sqrt(dx * dx + dy * dy + dz * dz) + m_Radius.value.value ;
179
 
}
180
 
 
181
 
void CrystalAtom::NetToCartesian(double a, double b, double c, double alpha, double beta, double gamma)
182
 
{
183
 
        double dx = x() * a ;
184
 
        double dy = y() * b ;
185
 
        double dz = z() * c ;
186
 
        SetCoords(dx * sqrt(1-square(cos(beta)) - square((cos(gamma) - cos(beta)*cos(alpha))/sin(alpha))),
187
 
                dx * (cos(gamma) - cos(beta)*cos(alpha))/sin(alpha) + dy * sin(alpha),
188
 
                (dx * cos(beta) + dy * cos(alpha) + dz));
189
 
}
190
 
 
191
 
bool CrystalAtom::SaveNode (xmlDocPtr xml, xmlNodePtr node)
192
 
{
193
 
        if (!WriteRadius (xml, node, m_Radius))
194
 
                return false;
195
 
 
196
 
        if (m_bCustomColor && !WriteColor (xml, node, NULL, m_fRed, m_fGreen, m_fBlue, m_fAlpha))
197
 
                return false;
198
 
 
199
 
        return true;
200
 
}
201
 
 
202
 
bool CrystalAtom::LoadNode(xmlNodePtr node)
203
 
{
204
 
        xmlNodePtr child = FindNodeByNameAndId(node, "color");
205
 
        if (!child) SetDefaultColor();
206
 
        else
207
 
        {
208
 
                if (!ReadColor(node, NULL, &m_fRed, &m_fGreen, &m_fBlue, &m_fAlpha)) return false;
209
 
                m_bCustomColor = true;
210
 
        }
211
 
        child = FindNodeByNameAndId(node, "radius");
212
 
        if (!child) return false;
213
 
        m_Radius.Z = GetZ();
214
 
        bool result = ReadRadius(child, m_Radius);
215
 
        return result;
216
 
}
217
 
 
218
 
void CrystalAtom::SetRadius(const GcuAtomicRadius& r)
219
 
{
220
 
        m_Radius.type = r.type;
221
 
        m_Radius.value = r.value;
222
 
        m_Radius.charge = r.charge;
223
 
        m_Radius.scale = r.scale;
224
 
        m_Radius.cn = r.cn;     //coordination number: -1: unspecified
225
 
        m_Radius.spin = r.spin;
226
 
}