File: ballAndStickModel.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 (516 lines) | stat: -rw-r--r-- 13,296 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: ballAndStickModel.C,v 1.23.18.1 2007/03/25 22:02:29 oliver Exp $
//

#include <BALL/VIEW/MODELS/ballAndStickModel.h>
#include <BALL/KERNEL/atom.h>
#include <BALL/KERNEL/bond.h>
#include <BALL/KERNEL/forEach.h>
#include <BALL/VIEW/PRIMITIVES/tube.h>
#include <BALL/VIEW/PRIMITIVES/sphere.h>
#include <BALL/VIEW/PRIMITIVES/disc.h>
#include <BALL/VIEW/PRIMITIVES/twoColoredTube.h>
#include <BALL/VIEW/KERNEL/common.h>

using namespace std;

namespace BALL
{
	namespace VIEW
	{

		AddBallAndStickModel::AddBallAndStickModel()
			: AtomBondModelBaseProcessor(),
				ball_radius_((float)0.4),
				stick_radius_((float)0.2),
				ball_and_stick_(true),
				dashed_bonds_(false)
		{
		}

		AddBallAndStickModel::AddBallAndStickModel(const AddBallAndStickModel &add_ball_and_stick)
			: AtomBondModelBaseProcessor(add_ball_and_stick),
				ball_radius_(add_ball_and_stick.ball_radius_),
				stick_radius_(add_ball_and_stick.stick_radius_),
				ball_and_stick_(add_ball_and_stick.ball_and_stick_),
				dashed_bonds_(add_ball_and_stick.dashed_bonds_)
		{
		}

		AddBallAndStickModel::~AddBallAndStickModel()
		{
			#ifdef BALL_VIEW_DEBUG
				Log.info() << "Destructing object " << this << " of class AddBallAndStickModel" << std::endl;
			#endif 
		}

		void AddBallAndStickModel::clear()
		{
			AtomBondModelBaseProcessor::clear();

			ball_radius_ = (float)0.4;
			stick_radius_ = (float)0.2;
			ball_and_stick_ = true;
			dashed_bonds_ = false;
		}

		void AddBallAndStickModel::set(const AddBallAndStickModel &add_ball_and_stick)
		{
			AtomBondModelBaseProcessor::set(add_ball_and_stick);

			ball_radius_ = add_ball_and_stick.ball_radius_;
			stick_radius_ = add_ball_and_stick.stick_radius_;
			ball_and_stick_ = add_ball_and_stick.ball_and_stick_;
			dashed_bonds_ = add_ball_and_stick.dashed_bonds_;
		}

		const AddBallAndStickModel &AddBallAndStickModel::operator = (const AddBallAndStickModel &processor)
		{
			set(processor);
			return *this;
		}

		void AddBallAndStickModel::setBallRadius(const float radius)
		{
			// radius can never be lower or equal 0
			if (radius <= (float)0)
			{
				throw Exception::OutOfRange(__FILE__, __LINE__);
			}
			
			ball_radius_ = radius;
		}

		void AddBallAndStickModel::setStickRadius(const float radius)
		{
			// radius can never be lower or equal 0
			if (radius <= (float)0)
			{
				throw Exception::OutOfRange(__FILE__, __LINE__);
			}

			stick_radius_ = radius;
			special_radius_ = radius / 2.4;
		}

		Processor::Result AddBallAndStickModel::operator() (Composite& composite)
		{
			if (dashed_bonds_)
			{
				AtomBondModelBaseProcessor::operator() (composite);
			}

			Atom *atom = RTTI::castTo<Atom>(composite);

			if (atom == 0) return Processor::CONTINUE;

			Sphere* sphere_ptr = new Sphere;

			if (sphere_ptr == 0) throw Exception::OutOfMemory (__FILE__, __LINE__, sizeof(Sphere));

			sphere_ptr->setComposite(atom);

			if (ball_and_stick_)
			{
				sphere_ptr->setRadius(ball_radius_);
			}
			else
			{
				sphere_ptr->setRadius(stick_radius_);
			}

			sphere_ptr->setPositionAddress(atom->getPosition());
			
			// append sphere in Atom
			geometric_objects_.push_back(sphere_ptr);

			// collect used atoms
			insertAtom_(atom);

			return Processor::CONTINUE;
		}

		void AddBallAndStickModel::dump(std::ostream& s, Size depth) const
		{
			BALL_DUMP_STREAM_PREFIX(s);
			
			BALL_DUMP_DEPTH(s, depth);
			BALL_DUMP_HEADER(s, this, this);

			AtomBondModelBaseProcessor::dump(s, depth + 1);

			BALL_DUMP_DEPTH(s, depth);
			s << "ball radius: " << ball_radius_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "stick radius: " << stick_radius_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "b&s model: " << ball_and_stick_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "dashed bonds: " << dashed_bonds_ << endl;

			BALL_DUMP_STREAM_SUFFIX(s);
		}

		void AddBallAndStickModel::renderStandardBond_(const Bond& bond)
		{
			// generate two colored tube
			TwoColoredTube *tube = new TwoColoredTube;
			tube->setRadius(stick_radius_);
			tube->setComposite(&bond);
			geometric_objects_.push_back(tube);

			if (!ball_and_stick_)
			{
				tube->setVertex1Address(bond.getFirstAtom()->getPosition());
				tube->setVertex2Address(bond.getSecondAtom()->getPosition());
			}
			else
			{
				const Vector3& v1 = bond.getFirstAtom()->getPosition();
				const Vector3& v2 = bond.getSecondAtom()->getPosition();
				Vector3 dir = v2 - v1;
				float f = dir.getLength();
				if (!Maths::isZero(f)) dir /= f;
 				dir *= (ball_radius_ - 0.4 * stick_radius_);
				tube->setVertex1(v1 + dir);
				tube->setVertex2(v2 - dir);
			}
		}

		void AddBallAndStickModel::renderMultipleBond_(const Bond& bond, Vector3 normal, Vector3 dir)
		{
			TwoColoredTube* tube = new TwoColoredTube;
			tube->setRadius(special_radius_);
			tube->setComposite(&bond);
			geometric_objects_.push_back(tube);
				
			TwoColoredTube* tube2 = new TwoColoredTube(*tube);
			geometric_objects_.push_back(tube2);

			if (bond.getOrder() == Bond::ORDER__DOUBLE)
			{
				normal *= stick_radius_ / (float) 1.5;
				tube->setVertex1(bond.getFirstAtom()->getPosition() - normal);
				tube->setVertex2(bond.getSecondAtom()->getPosition() - normal);

				tube2->setVertex1(bond.getFirstAtom()->getPosition() + normal);
				tube2->setVertex2(bond.getSecondAtom()->getPosition() + normal);
			}
			else
			{
				Vector3 normal2 = dir % normal;
				try
				{
					normal2.normalize();
				}
				catch(...)
				{
					return;
				}

				normal *= stick_radius_ / (float) 1.5;
				normal2 *= stick_radius_ / (float) 1.5;
				
				tube->setVertex1(bond.getFirstAtom()->getPosition() - normal - normal2);
				tube->setVertex2(bond.getSecondAtom()->getPosition() - normal - normal2);
				
				tube2->setVertex1(bond.getFirstAtom()->getPosition() + normal - normal2);
				tube2->setVertex2(bond.getSecondAtom()->getPosition() + normal - normal2);

				TwoColoredTube* tube3 = new TwoColoredTube(*tube);
				tube3->setVertex1(bond.getFirstAtom()->getPosition() + normal2);
				tube3->setVertex2(bond.getSecondAtom()->getPosition() + normal2);
				geometric_objects_.push_back(tube3);
			}
		}

		void AddBallAndStickModel::visualiseBond_(const Bond& bond)
		{
			// no visualisation for hydrogen bonds
			if (bond.getType() == Bond::TYPE__HYDROGEN) return;

			if (!dashed_bonds_ 													||
					bond.getOrder() < Bond::ORDER__DOUBLE 	||
					bond.getOrder() > Bond::ORDER__AROMATIC )
			{
				renderStandardBond_(bond);
				return;
			}

			try
			{
				Vector3 dir = bond.getSecondAtom()->getPosition() - bond.getFirstAtom()->getPosition();
				dir.normalize();
				Vector3 normal = VIEW::getNormal(dir);

				if (bond.getFirstAtom()->countBonds() == 3)
				{
					normal = getSP2Plane_(*bond.getFirstAtom(), bond, dir);
				}
				else if(bond.getSecondAtom()->countBonds() == 3)
				{
					normal = getSP2Plane_(*bond.getSecondAtom(), bond, dir);
				}

				if (bond.getOrder() == Bond::ORDER__AROMATIC)
				{
					renderDashedBond_(bond, normal);
				}
				else
				{
					renderMultipleBond_(bond, normal, dir);
				}
			}
			catch(...)
			{
			}
		}

		Vector3 AddBallAndStickModel::getSP2Plane_(const Atom& atom, const Bond& bond, const Vector3& dir) const
		{
			vector<Vector3> vs;
			AtomBondConstIterator bit = atom.beginBond();
			for (; +bit; ++bit)
			{
				if (&*bit != &bond)
				{
					Vector3 x = bit->getPartner(atom)->getPosition() - atom.getPosition();
					float xl = x.getLength();
					if (!Maths::isZero(xl)) vs.push_back(x / xl);
					else 									 vs.push_back(Vector3(1, 0, 0));
				}
			}

			Vector3 n = vs[0] % vs[1];
			float nl = n.getLength();
			if (!Maths::isZero(nl)) n /= nl;
			else 									  n = Vector3(1,0,0);

			n = n % dir;
			nl = n.getLength();
			if (!Maths::isZero(nl)) n /= nl;
			else 									  n = Vector3(1,0,0);

			return n;
		}


		void AddBallAndStickModel::visualiseRings_()
		{
			for (Position r = 0; r < rings_.size(); r++)
			{
				vector<Atom*>& ring = rings_[r];

				vector<Bond*>& bonds = ring_bonds_[r];
				if (bonds.size() == 0) continue;

				vector<Atom*>::iterator ait = ring.begin();

				Vector3 center;
				for (; ait != ring.end(); ait++)
				{
					center += (**ait).getPosition();
				}
				center /= ring.size();


				vector<Vector3> bond_vectors;
				for (Position p = 0; p < bonds.size(); p++)
				{
					Vector3 v1 = bonds[p]->getFirstAtom()->getPosition() - 
											 bonds[p]->getSecondAtom()->getPosition();
					float f = v1.getLength();
					if (Maths::isZero(f)) bond_vectors.push_back(Vector3(1,0,0));
					else 									bond_vectors.push_back(v1 / f);
				}
				
				bond_vectors.push_back(bond_vectors[0]);

				Vector3 rn1 = bond_vectors[0] % bond_vectors[1];
				float f = rn1.getLength();
				if (Maths::isZero(f)) rn1 = Vector3(1,0,0);
				else 								  rn1 / f;

				Vector3 rn = rn1;

				for (Position p = 2; p < bond_vectors.size(); p++)
				{
					Vector3 rnx = bond_vectors[p - 1] % bond_vectors[p];
					float f = rnx.getLength();
					if (Maths::isZero(f)) rnx = Vector3(1,0,0);
					else 								  rnx / f;

					if (rn1.getSquareDistance(rnx) > rn1.getSquareDistance(-rnx)) rnx *= -1;

					rn += rnx;
				}

				if (!Maths::isZero(rn.getSquareLength())) rn.normalize();

				for (Position b = 0; b < bonds.size(); b++)
				{
					Vector3 n = rn % bond_vectors[b];
					float f = n.getLength();
					if (Maths::isZero(f)) n = rn;
					else 									n /= f;

					Vector3 p = bonds[b]->getFirstAtom()->getPosition() + bond_vectors[b] / 2.0;
					Vector3 pc = p - center;

					if (n.getSquareDistance(pc) < n.getSquareDistance(-pc)) n *= -1;

					if (bonds[b]->getOrder() == Bond::ORDER__AROMATIC)
					{
						renderDashedBond_(*bonds[b], n);
					}
					else
					{
						try
						{
							renderMultipleBond_(*bonds[b], n, bond_vectors[b]);
						}
						catch(...)
						{
						}
					}
				}
			}
		}

		void AddBallAndStickModel::renderDashedBond_(const Bond& bond, Vector3 n)
		{
			const Atom& a1 = *bond.getFirstAtom();
			const Atom& a2 = *bond.getSecondAtom();

 			n *= stick_radius_ / (float) 1.5;

			// render one tube with full length			
			TwoColoredTube *tube = new TwoColoredTube;
			tube->setRadius(special_radius_);
			tube->setVertex1(a1.getPosition() - n);
			tube->setVertex2(a2.getPosition() - n);
			tube->setComposite(&bond);
			geometric_objects_.push_back(tube);

			// render dashed tubes
 			Vector3 v = a2.getPosition() + n - (a1.getPosition() + n);
			Vector3 v8 = v / 8.;
			Vector3 last = a1.getPosition() + n + v / (float) 4.5;
			for (Position p = 0; p < 3; p++)
			{
				if (p == 1)
				{
					Vector3 middle((a1.getPosition() - a2.getPosition()) / 2 + a2.getPosition());
					TwoColoredTube *tube = new TwoColoredTube;
					tube->setRadius(special_radius_);
					tube->setComposite(&bond);
					tube->setVertex1(middle - (v8) + n);
					tube->setVertex2(middle + (v8) + n);
					geometric_objects_.push_back(tube);

					Disc* disc = new Disc(Circle3(middle - (v8) + n, -v, stick_radius_ / (float) 2.4));
					disc->setComposite(&a1);
					geometric_objects_.push_back(disc);

					disc = new Disc(Circle3(middle + (v8) + n, v, stick_radius_ / (float) 2.4));
					disc->setComposite(&a2);
					geometric_objects_.push_back(disc);

					last += (v /4);

					continue;
				}

				last += (v /4);
			}
		}

		void AddBallAndStickModel::collectRingBonds_()
		{
			vector<HashSet<Atom*> > ring_atom_set;
			for (Position i = 0; i < rings_.size(); i++)
			{
				ring_atom_set.push_back(HashSet<Atom*>());
				for (Position j = 0; j < rings_[i].size(); j++)
				{
					ring_atom_set[i].insert(rings_[i][j]);
				}
			}

			ring_bonds_.clear();
			ring_bonds_.resize(rings_.size());

			const Atom* second_atom_ptr = 0;
			AtomBondConstIterator bond_it;

			// for all used atoms
			list<const Atom*>::const_iterator atom_it;
			for (atom_it  = getAtomList_().begin();
					 atom_it != getAtomList_().end(); ++atom_it)
			{
				// for all bonds connected from first- to second atom
				BALL_FOREACH_ATOM_BOND(**atom_it, bond_it)
				{
					second_atom_ptr = bond_it->getPartner(**atom_it);
					
					// no visualisation for hydrogen bonds
					if (bond_it->getType() == Bond::TYPE__HYDROGEN) continue;

					// use only atoms with greater handles than first atom
					if (**atom_it > *second_atom_ptr || !getAtomSet_().has(second_atom_ptr))
					{
						continue;
					}

					bool special_ring = false;

					if (dashed_bonds_ &&
							bond_it->getOrder() > Bond::ORDER__SINGLE)
					{
						for (Position p = 0; p < ring_atom_set.size(); p++)
						{
							if (ring_atom_set[p].has((Atom*)*atom_it) &&
									ring_atom_set[p].has((Atom*)second_atom_ptr))
							{
								special_ring = true;
								ring_bonds_[p].push_back((Bond*)&*bond_it);
								break;
							}
						}
					}

					if (!special_ring)
					{
 						visualiseBond_(*bond_it);
					}
				}
			}
		}


		bool AddBallAndStickModel::createGeometricObjects()
		{
			if (!dashed_bonds_) 
			{
				buildBondModels_();
				return true;
			}

			collectRingBonds_();
			visualiseRings_();
			rings_.clear();
			ring_bonds_.clear();
			return true;
		}

#		ifdef BALL_NO_INLINE_FUNCTIONS
#			include <BALL/VIEW/MODELS/ballAndStickModel.iC>
#		endif

	} // namespace VIEW
} // namespace BALL