File: atom.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 7,337
  • sloc: cpp: 72,977; sh: 11,381; xml: 6,304; makefile: 1,663; ansic: 1,061
file content (407 lines) | stat: -rw-r--r-- 8,925 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// -*- C++ -*-

/*
 * Gnome Chemistry Utils
 * atom.cc
 *
 * Copyright (C) 2001-2011 Jean Bréfort <jean.brefort@normalesup.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "config.h"
#include "element.h"
#include "atom.h"
#include "bond.h"
#include "document.h"
#include "molecule.h"
#include "objprops.h"
#include "vector.h"
#include "xml-utils.h"
#include <glib/gi18n-lib.h>
#include <cmath>
#include <cstring>
#include <sstream>

using namespace std;

namespace gcu
{

Atom::Atom (): Object (AtomType)
{
	m_Z = -1;
	m_x = m_y = m_z = 0.0;
	m_Charge = 0;
}

Atom::~Atom ()
{
	m_Bonds.clear ();
	Molecule *mol = static_cast < Molecule * > (GetMolecule ());
	if (mol)
		mol->Remove (this);
}

Atom::Atom (int Z, double x, double y, double z):
	Object (AtomType)
{
	SetZ (Z);
	m_x = x;
	m_y = y;
	m_z = z;
	m_Charge = 0;
}

Atom::Atom (Atom& a):
	Object (AtomType)
{
	SetZ (a.m_Z);
	m_x = a.m_x;
	m_y = a.m_y;
	m_z = a.m_z;
	m_Charge = a.m_Charge;
}

Atom& Atom::operator= (Atom& a)
{
	SetZ (a.m_Z);
	m_x = a.m_x;
	m_y = a.m_y;
	m_z = a.m_z;
	m_Charge = a.m_Charge;
	return *this ;
}

void Atom::SetZ (int Z)
{
	m_Z = Z;
}

double Atom::Distance (Atom* pAtom)
{
	return sqrt (square (m_x - pAtom->m_x) + square (m_y - pAtom->m_y) + square (m_z - pAtom->m_z));
}

void Atom::zoom (double ZoomFactor)
{
	m_x *= ZoomFactor;
	m_y *= ZoomFactor;
	m_z *= ZoomFactor;
}

bool Atom::GetCoords (double *x, double *y, double *z) const
{
	if (!x || !y)
		return false;
	*x = m_x;
	*y = m_y;
	if (z)
		*z = m_z;
	return true;
}

const gchar* Atom::GetSymbol () const
{
	Element* Elt = Element::GetElement(m_Z);
	return (Elt)? Element::Symbol(m_Z): NULL;
}

void Atom::AddBond (Bond* pBond)
{
	m_Bonds[pBond->GetAtom (this)] = pBond;
}

void Atom::RemoveBond (Bond* pBond)
{
	m_Bonds.erase (pBond->GetAtom (this));
}

Bond* Atom::GetFirstBond (map<Atom*, Bond*>::iterator& i)
{
	i = m_Bonds.begin ();
	if (i == m_Bonds.end ())
		return NULL;
	return (*i).second;
}

Bond const * Atom::GetFirstBond (std::map< Atom *, Bond * >::const_iterator& i) const
{
	i = m_Bonds.begin ();
	if (i == m_Bonds.end ())
		return NULL;
	return (*i).second;
}


Bond const * Atom::GetNextBond (std::map< Atom *, Bond * >::const_iterator& i) const
{
	i++;
	if (i == m_Bonds.end())
		return NULL;
	return (*i).second;
}

Bond* Atom::GetNextBond (map<Atom*, Bond*>::iterator& i)
{
	i++;
	if (i == m_Bonds.end())
		return NULL;
	return (*i).second;
}

void Atom::Move (double x, double y, double z)
{
	m_x += x;
	m_y += y;
	m_z += z;
}

void Atom::Transform2D (Matrix2D& m, double x, double y)
{
	m_x -= x;
	m_y -= y;
	m.Transform (m_x, m_y);
	m_x += x;
	m_y += y;
}

xmlNodePtr Atom::Save (xmlDocPtr xml) const
{
	xmlNodePtr parent;
	gchar buf[16];
	parent = xmlNewDocNode (xml, NULL, (xmlChar*) "atom", NULL);
	if (!parent)
		return NULL;
	SaveId (parent);

	if (m_Z > 0) {
		strncpy(buf, GetSymbol(), sizeof(buf));
		xmlNewProp(parent, (xmlChar*) "element", (xmlChar*) buf);
	}

	if (m_Charge) {
		snprintf (buf, sizeof (buf), "%d", m_Charge);
		xmlNewProp (parent, (xmlChar*) "charge", (xmlChar*) buf);
	}
	if (!WritePosition (xml, parent, NULL, m_x, m_y, m_z)) {
		xmlFreeNode (parent);
		return NULL;
	}
	if (!SaveNode (xml, parent)) {
		xmlFreeNode (parent);
		return NULL;
	}
	return parent;
}

bool Atom::Load (xmlNodePtr node)
{
	char* tmp;
	tmp = (char*) xmlGetProp (node, (xmlChar*) "id");
	if (tmp) {
		SetId (tmp);
		xmlFree (tmp);
	}
	tmp = (char*) xmlGetProp (node, (xmlChar*) "element");
	if (tmp) {
		m_Z = Element::Z(tmp);	//Don't check if element exists. Applications that do not accept unknown elements should check
		xmlFree (tmp);
	}
	tmp = (char*) xmlGetProp (node, (xmlChar*) "charge");
	if (tmp) {
		m_Charge = (char) atoi (tmp);
		xmlFree (tmp);
	} else
		m_Charge = 0;
	if (!ReadPosition (node, NULL, &m_x, &m_y, &m_z) || (!LoadNode (node)))
		return false;
	GetDocument ()->ObjectLoaded (this);
	return true;
}

bool Atom::LoadNode (G_GNUC_UNUSED xmlNodePtr node)
{
	return true;
}

bool Atom::SaveNode (G_GNUC_UNUSED xmlDocPtr xml, G_GNUC_UNUSED xmlNodePtr node) const
{
	return true;
}

Bond* Atom::GetBond (Atom* pAtom) const
{
	std::map<Atom*, Bond*>::const_iterator i;
	i = m_Bonds.find (pAtom);
	return (i != m_Bonds.end ())? (*i).second: NULL;
}

bool Atom::SetProperty (unsigned property, char const *value)
{
	switch (property) {
	case GCU_PROP_POS2D: {
		double x, y;
		istringstream is (value);
		is >> x >> y;
		Document *doc = GetDocument ();
		if (doc) {
			x *= doc->GetScale ();
			y *= doc->GetScale ();
		}
		SetCoords (x, y);
		break;
	}
	case GCU_PROP_X:
		m_x = g_ascii_strtod (value, NULL) * GetDocument ()->GetScale ();
		break;
	case GCU_PROP_Y:
		m_y = g_ascii_strtod (value, NULL) * GetDocument ()->GetScale ();
		break;
	case GCU_PROP_Z:
		m_z = g_ascii_strtod (value, NULL) * GetDocument ()->GetScale ();
		break;
	case GCU_PROP_XFRACT:
		m_x = g_ascii_strtod (value, NULL);
		break;
	case GCU_PROP_YFRACT:
		m_y = g_ascii_strtod (value, NULL);
		break;
	case GCU_PROP_ZFRACT:
		m_z = g_ascii_strtod (value, NULL);
		break;
	case GCU_PROP_ID: {
		char *Id = (*value == 'a')? g_strdup (value): g_strdup_printf ("a%s", value);
		SetId (Id);
		break;
	}
	case GCU_PROP_ATOM_SYMBOL:
		SetZ (Element::Z (value));
		break;
	case GCU_PROP_ATOM_Z:
		SetZ (atoi (value));
		break;
	case GCU_PROP_ATOM_CHARGE:
		SetCharge (atoi (value));
		break;
	}
	return  true;
}

string Atom::GetProperty (unsigned property) const
{
	ostringstream res;
	switch (property) {
	case GCU_PROP_POS2D: {
		Document *doc = const_cast <Atom*> (this)->GetDocument ();
		if (doc)
			res << m_x / doc->GetScale () << " " << m_y / doc->GetScale ();
		else
			res << m_x << " " << m_y;
		break;
	}
	case GCU_PROP_POS3D: {
		Document *doc = const_cast <Atom*> (this)->GetDocument ();
		if (doc)
			res << m_x / doc->GetScale () << " " << m_y / doc->GetScale () << " " << m_z / doc->GetScale ();
		else
			res << m_x << " " << m_y << " " << m_z;
		break;
	}
	case GCU_PROP_ATOM_Z:
		res << GetZ ();
		break;
	case GCU_PROP_ATOM_SYMBOL:
		res << GetSymbol ();
		break;
	case GCU_PROP_ATOM_CHARGE:
		res << static_cast <int> (m_Charge);
		break;
	default:
		return Object::GetProperty (property);
	}
	return res.str ();
}

bool Atom::IsInCycle (Cycle* pCycle)
{
	map<gcu::Atom*, gcu::Bond*>::iterator i, end = m_Bonds.end ();
	for (i = m_Bonds.begin (); i != end; i++)
		if (((Bond*) (*i).second)->IsInCycle (pCycle))
		return true;
	return false;
}

bool Atom::Match (Atom *atom, AtomMatchState &state)
{
	if (m_Z != atom->m_Z)
		return false;
	if (m_Bonds.size () != atom->m_Bonds.size ())
		return false;
	unsigned n = state.mol1.size ();
	// add the atoms to state
	state.mol1[this] = n;
	state.mol2[atom] = n;
	state.atoms[n] = AtomPair (this, atom);
	// compare bonded atoms
	map<gcu::Atom*, gcu::Bond*>::iterator i, iend = m_Bonds.end ();
	map<gcu::Atom*, gcu::Bond*>::iterator j, jend = atom->m_Bonds.end ();
	for (i = m_Bonds.begin (); i != iend; i++) {
		if (state.mol1.find ((*i).first) != state.mol1.end ())
			continue; /* may be not enough: we might search which atom is
		  				associated with this one and see if it is effectively
			  			bonded to *atom */
		for (j = atom->m_Bonds.begin (); j != jend; j++) {
			if (state.mol2.find ((*j).first) != state.mol2.end ())
				continue;
			if ((*i).first->Match ((*j).first, state))
				break;
		}
		if (j == jend)
			break; // no matching atom has been fond;
	}
	if (i == iend)
		return true;
	// if we are there, something wrong happened, clean and return
	unsigned max = state.mol1.size ();
	for (unsigned i = n; i < max; i++) {
		state.mol1.erase (state.atoms[n].atom1);
		state.mol2.erase (state.atoms[n].atom2);
	}
	return false;
}

Vector Atom::GetVector () const
{
	return Vector (m_x, m_y, m_z);
}

std::string Atom::Name ()
{
	return _("Atom");
}

void Atom::NetToCartesian (double a, double b, double c, double alpha, double beta, double gamma)
{
	double dx = x() * a ;
	double dy = y() * b ;
	double dz = z() * c ;
	SetCoords (dx * sqrt (1 - square (cos (beta)) - square ((cos (gamma) - cos (beta) * cos (alpha)) / sin (alpha))),
		dx * (cos (gamma) - cos (beta) * cos (alpha)) / sin (alpha) + dy * sin (alpha),
		(dx * cos (beta) + dy * cos (alpha) + dz));
}

}	//	namespace gcu