File: bond.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (345 lines) | stat: -rw-r--r-- 7,358 bytes parent folder | download | duplicates (4)
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
// -*- C++ -*-

/*
 * Gnome Chemistry Utils
 * bond.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 "bond.h"
#include "atom.h"
#include "objprops.h"
#include "document.h"
#include "molecule.h"
#include <glib/gi18n-lib.h>
#include <cmath>
#include <sstream>

using namespace std;

namespace gcu
{

Bond::Bond(): Object(BondType)
{
	SetId ("b1");
	m_Begin = NULL;
	m_End = NULL;
	m_order = 0;
}

Bond::Bond(Atom* first, Atom* last, unsigned char order): Object(BondType)
{
	SetId ("b1");
	m_Begin = first;
	m_End = last;
	m_order = order;
	first->AddBond(this);
	last->AddBond(this);
}

Bond::~Bond()
{
	Molecule *mol = static_cast < Molecule * > (GetMolecule ());
	if (mol)
		mol->Remove (this);
}

Atom* Bond::GetAtom(int which) const
{
	switch (which)
	{
	case 0: return m_Begin;
	case 1: return m_End;
	default: return NULL;
	}
}

Atom* Bond::GetAtom(Atom* pAtom, G_GNUC_UNUSED int which) const
{
	return (pAtom == m_Begin)? m_End: (pAtom == m_End)? m_Begin: NULL;
}

Atom const * Bond::GetAtom (Atom const * pAtom, G_GNUC_UNUSED int which) const
{
	return (pAtom == m_Begin)? m_End: (pAtom == m_End)? m_Begin: NULL;
}

unsigned char Bond::GetOrder() const
{
	return m_order;
}

void Bond::SetOrder(unsigned char Order)
{
	m_order = Order;
}

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

	SaveId(parent);

	buf[0] = m_order + '0';
	buf[1] = 0;
	xmlNewProp(parent, (xmlChar*)"order", (xmlChar*)buf);

	if (m_Begin)
		xmlNewProp (parent, (xmlChar*) "begin", (xmlChar*) m_Begin->GetId ());


	if (m_End)
		xmlNewProp (parent, (xmlChar*) "end", (xmlChar*) m_End->GetId ());

	if (!SaveNode(xml, parent)) {xmlFreeNode(parent); return NULL;}
	return parent;
}

bool Bond::Load (xmlNodePtr node)
{
	char* tmp;
	xmlNodePtr child;
	Document *doc = GetDocument ();
	tmp = (char*) xmlGetProp (node, (xmlChar*) "id");
	if (tmp) {
		SetId (tmp);
		xmlFree (tmp);
	}
	tmp = (char*) xmlGetProp (node, (xmlChar*) "order");
	if (!tmp) m_order = 1;
	else {
		m_order = *tmp - '0';
		xmlFree (tmp);
	}
	if (m_order > 4)
		return false;
	tmp = (char*) xmlGetProp (node, (xmlChar*) "begin");
	if (!tmp) {
		child = GetNodeByName(node, "begin");
		tmp = (char*) xmlNodeGetContent(child); //necessary to read version 0.1.0 files
		if (!tmp)
			return false;
	}
	doc->SetTarget (tmp, reinterpret_cast <Object **> (&m_Begin), GetParent (), this, ActionDelete);
	xmlFree (tmp);
	tmp = (char*) xmlGetProp (node, (xmlChar*) "end");
	if (!tmp) {
		child = GetNodeByName (node, "end");
		tmp = (char*) xmlNodeGetContent (child); //necessary to read version 0.1.0 files
		if (!tmp)
			return false;
	}
	if (doc->SetTarget (tmp, reinterpret_cast <Object **> (&m_End), GetParent (), this, ActionDelete))
		m_End->AddBond (this);
	xmlFree (tmp);
	bool result = LoadNode (node);
	doc->ObjectLoaded (this);
	OnLoaded ();
	return result;
}

bool Bond::LoadNode(xmlNodePtr)
{
	return true;
}

bool Bond::SaveNode(xmlDocPtr, xmlNodePtr) const
{
	return true;
}

void Bond::IncOrder(int n)
{
	m_order += n;
	if (m_order > 4)  m_order %= 4;
}

void Bond::Move(G_GNUC_UNUSED double x, G_GNUC_UNUSED double y, G_GNUC_UNUSED double z)
{
}

void Bond::Transform2D(G_GNUC_UNUSED Matrix2D& m, G_GNUC_UNUSED double x, G_GNUC_UNUSED double y)
{
}

bool Bond::SetProperty (unsigned property, char const *value)
{
	switch (property) {
	case GCU_PROP_ID: {
		char *Id = (*value == 'b')? g_strdup (value): g_strdup_printf ("b%s", value);
		SetId (Id);
		break;
	}
	case GCU_PROP_BOND_BEGIN: {
		char *tmp = (*value == 'a')? g_strdup (value): g_strdup_printf ("a%s", value);
		if (!GetDocument ()->SetTarget (tmp, reinterpret_cast <Object **> (&m_Begin), GetParent (), this))
			return false;
		g_free (tmp);
		if (m_End) {
			m_Begin->AddBond (this);
			m_End->AddBond (this);
		}
		break;
	}
	case GCU_PROP_BOND_END: {
		char *tmp = (*value == 'a')? g_strdup (value): g_strdup_printf ("a%s", value);
		if (!GetDocument ()->SetTarget (tmp, reinterpret_cast <Object **> (&m_End), GetParent (), this))
			return false;
		g_free (tmp);
		if (m_Begin) {
			m_Begin->AddBond (this);
			m_End->AddBond (this);
		}
		break;
	}
	case GCU_PROP_BOND_ORDER:
		m_order = atoi (value);
		if (m_Begin && m_End) {
			m_Begin->AddBond (this);
			m_End->AddBond (this);
		}
		break;
	}
	return  true;
}

string Bond::GetProperty (unsigned property) const
{
	ostringstream res;
	switch (property) {
	case GCU_PROP_BOND_BEGIN:
		res << m_Begin->GetId ();
		break;
	case GCU_PROP_BOND_END:
		res << m_End->GetId ();
		break;
	case GCU_PROP_BOND_ORDER:
		res << static_cast <unsigned> (m_order);
		break;
	default:
			return Object::GetProperty (property);
	}
	return res.str ();
}

void Bond::AddCycle (Cycle* pCycle)
{
	m_Cycles.push_back (pCycle);
}

void Bond::RemoveCycle (Cycle* pCycle)
{
	m_Cycles.remove (pCycle);
}

void Bond::RemoveAllCycles ()
{
	m_Cycles.clear ();
}

Cycle* Bond::GetFirstCycle (std::list<Cycle*>::iterator& i, Cycle * pCycle)
{
	i = m_Cycles.begin ();
	return GetNextCycle (i, pCycle);
}

Cycle* Bond::GetNextCycle (std::list<Cycle*>::iterator& i, Cycle * pCycle)
{
	if (*i == pCycle)
		i++;
	if (i == m_Cycles.end ())
		return NULL;
	pCycle = *i;
	i++;
	return pCycle;
}

bool Bond::IsInCycle (Cycle* pCycle)
{
	std::list<Cycle*>::iterator i, end = m_Cycles.end ();
	for (i = m_Cycles.begin (); i != end; i++)
		if ((*i) == pCycle)
			return true;
	return false;
}

double Bond::Get2DLength ()
{
	double x1, y1, x2, y2;
	m_Begin->GetCoords (&x1, &y1);
	m_End->GetCoords (&x2, &y2);
	return sqrt (square (x1 - x2) + square (y1 - y2));
}

double Bond::GetAngle2DRad (Atom* pAtom)
{
	double x1, y1, x2, y2;
	if (m_Begin == NULL || m_End == NULL)
		return HUGE_VAL;
	m_Begin->GetCoords (&x1, &y1);
	m_End->GetCoords (&x2, &y2);
	x2 -= x1;
	y2 -= y1;
	double length = square (x2) + square (y2);
	if (length == 0.0)
		return HUGE_VAL;
	if (pAtom == m_Begin)
		return atan2 (-y2, x2);
	else if (pAtom == m_End)
		return atan2 (y2, -x2);
	return HUGE_VAL;
}

bool Bond::ReplaceAtom (Atom* oldAtom, Atom* newAtom)
{
	if (oldAtom == m_Begin) {
		if (m_End)
			m_End->RemoveBond (this);
		m_Begin = newAtom;
		if (m_Begin && m_End)
			m_End->AddBond (this);
	} else if (oldAtom == m_End) {
		if (m_Begin)
			m_Begin->RemoveBond (this);
		m_End = newAtom;
		if (m_Begin && m_End)
			m_Begin->AddBond (this);
	}
	return true;
}

void Bond::OnLoaded ()
{
	if (m_Begin && m_End) {
		m_Begin->AddBond (this);
		m_End->AddBond (this);
	}
}

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

}	//	namespace gcu