File: atomVector.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (196 lines) | stat: -rw-r--r-- 4,957 bytes parent folder | download | duplicates (9)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: atomVector.C,v 1.13.30.1 2007/05/18 12:05:59 oliver Exp $
//
// Author:
//   Oliver Kohlbacher

#include <BALL/MOLMEC/COMMON/atomVector.h>
#include <BALL/MOLMEC/COMMON/gradient.h>
#include <BALL/MATHS/vector3.h>
#include <BALL/CONCEPT/composite.h>
#include <BALL/KERNEL/atom.h>

#undef BALL_DEBUG

using namespace std;

namespace BALL
{
	AtomVector::AtomVector()
	{
	}

	AtomVector::AtomVector(const AtomVector& atoms, bool /* deep */)
		: vector<Atom*>()
	{
		set(atoms);
	}

	AtomVector::AtomVector(const Composite& composite, bool selected_only)
	{
		set(composite, selected_only);
	}

	AtomVector::~AtomVector()
	{
	}

	const AtomVector& AtomVector::operator = (const AtomVector& rhs)
	{
		set(rhs);
		return *this;
	}

	const AtomVector& AtomVector::operator = (const Composite& rhs)
	{
		set(rhs, rhs.containsSelection());
		return *this;
	}

	void AtomVector::set(const AtomVector& atoms)
	{
		// resize the vector and copy the contents of atoms
		resize((Size)atoms.size());
		copy(atoms.begin(), atoms.end(), begin());

		// copy the saved positions
		saved_position_.resize(atoms.saved_position_.size());
		copy(atoms.saved_position_.begin(), atoms.saved_position_.end(), saved_position_.begin());
	}

	void AtomVector::set(const Composite& composite, bool selected_only)
	{
		// clear all saved positions and atom pointers
		saved_position_.clear();
		clear();

		// iterate over all subcomposites and
		// extract pointers to the atoms
		Composite::CompositeConstIterator it = composite.beginComposite();
		for (; +it; ++it)
		{
			Atom* atom = const_cast<Atom*>(dynamic_cast<const Atom*>(&*it));
			if (atom != 0)
			{
				// store this atom only if it is selected or selected_only == false
				if (!selected_only || atom->isSelected())
				{
					push_back(atom);
				}
			}
		}
	}

	void AtomVector::savePositions()
	{
		// resize the saved positions
		saved_position_.resize(size());

		// copy all positions
		ConstIterator it(begin());
		vector<Vector3>::iterator pos_it(saved_position_.begin());
		for (; it != end(); ++it, ++pos_it)
		{
			*pos_it = (*it)->getPosition();
		}
	}

	void AtomVector::resetPositions()
	{
		// move only if a saved position exists for every atom
		if (saved_position_.size() == size())
		{
			Iterator it(begin());
			vector<Vector3>::const_iterator pos_it(saved_position_.begin());
			for (; it != end(); ++it, ++pos_it)
			{
				(*it)->setPosition(*pos_it);
			}
		}
	}

	void AtomVector::moveTo(const Gradient& gradient, double step)
	{
		// move only if a saved position exists for every atom
		if (gradient.size() == size())
		{
			// use the saves positions
			if (saved_position_.size() == size())
			{
				Iterator it(begin());
				vector<Vector3>::const_iterator pos_it(saved_position_.begin());
				Gradient::ConstIterator grad_it(gradient.begin());
				for (; it != end(); ++it, ++pos_it, ++grad_it)
				{
					(*it)->setPosition(*pos_it + *grad_it * step);
					#ifdef BALL_DEBUG
						Log << "   - atom " << (*it)->getFullName() << " @ " << (*it)->getPosition() << std::endl;
					#endif
				}
			}
			// we don't have saved positions, use the current atom positions
			else 
			{
				Iterator it(begin());
				Gradient::ConstIterator grad_it(gradient.begin());
				for (; it != end(); ++it, ++grad_it)
				{
					(*it)->getPosition() += *grad_it * step;
					#ifdef BALL_DEBUG
						Log << "   - atom " << (*it)->getFullName() << " @ " << (*it)->getPosition() << std::endl;
					#endif
				}				
			}
		}
	}

	void AtomVector::moveTo(const Gradient& gradient, double step, Size number)
	{
		// move only if a saved position exists for every atom
		if (gradient.size() == size())
		{
			// use the saves positions
			if (saved_position_.size() == size())
			{
				Iterator it(begin());
				vector<Vector3>::const_iterator pos_it(saved_position_.begin());
				vector<Vector3>::const_iterator end(saved_position_.begin() + number);
				Gradient::ConstIterator grad_it(gradient.begin());
				for (; pos_it != end; ++it, ++pos_it, ++grad_it)
				{
					(*it)->setPosition(*pos_it + *grad_it * step);
					#ifdef BALL_DEBUG
						Log << "   - atom " << (*it)->getFullName() << " @ " << (*it)->getPosition() << std::endl;
					#endif
				}
			}
			// we don't have saved positions, use the current atom positions
			else 
			{
				Iterator it(begin());
				Gradient::ConstIterator grad_it(gradient.begin());
				Gradient::ConstIterator end(gradient.begin() + number);
				for (; grad_it != end; ++it, ++grad_it)
				{
					(*it)->getPosition() += *grad_it * step;
					#ifdef BALL_DEBUG
						Log << "   - atom " << (*it)->getFullName() << " @ " << (*it)->getPosition() << std::endl;
					#endif
				}				
			}
		}
	}

	void AtomVector::resize(Size new_size)
	{
		Size old_size = (Size)size();
		::std::vector<Atom*>::resize(new_size);
		for (Position p = old_size; p < new_size; p++)
		{
			(*this)[p] = 0;
		}
	}
	
} // namespace BALL