source: molecuilder/src/linkedcell.cpp@ e78824

Last change on this file since e78824 was 834ff3, checked in by Frederik Heber <heber@…>, 16 years ago

Huge refactoring of Tesselation routines, but not finished yet.

  • new file tesselation.cpp with all of classes tesselation, Boundary..Set and CandidatesForTesselationOB
  • new file tesselationhelper.cpp with all auxiliary functions.
  • boundary.cpp just contains super functions, combininb molecule and Tesselation pointers
  • new pointer molecule::TesselStruct
  • PointMap, LineMap, TriangleMap DistanceMap have been moved from molecules.hpp to tesselation.hpp
  • new abstract class PointCloud and TesselPoint
  • atom inherits TesselPoint
  • molecule inherits PointCloud (i.e. a set of TesselPoints) and implements all virtual functions for the chained list
  • TriangleFilesWritten is thrown out, intermediate steps are written in find_nonconvex_border and not in find_next_triangle()
  • LinkedCell class uses TesselPoint as its nodes, i.e. as long as any class inherits TesselPoint, it may make use of LinkedCell as well and a PointCloud is used to initialize
  • class atom and bond definitions have been moved to own header files

NOTE: This is not bugfree yet. Tesselation of heptan produces way too many triangles, but runs without faults or leaks.

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[95183e]1#include "linkedcell.hpp"
2#include "molecules.hpp"
[834ff3]3#include "tesselation.hpp"
4
5// ========================================================= class LinkedCell ===========================================
6
[95183e]7
8/** Constructor for class LinkedCell.
9 */
10LinkedCell::LinkedCell()
11{
[a048fa]12 LC = NULL;
13 for(int i=0;i<NDIM;i++)
14 N[i] = 0;
15 index = -1;
16 RADIUS = 0.;
17 max.Zero();
18 min.Zero();
[95183e]19};
20
21/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
[834ff3]22 * \param *set LCNodeSet class with all LCNode's
[95183e]23 * \param RADIUS edge length of cells
24 */
[834ff3]25LinkedCell::LinkedCell(PointCloud *set, double radius)
[95183e]26{
[834ff3]27 TesselPoint *Walker = NULL;
[95183e]28
[a048fa]29 RADIUS = radius;
30 LC = NULL;
31 for(int i=0;i<NDIM;i++)
32 N[i] = 0;
33 index = -1;
34 max.Zero();
35 min.Zero();
36 cout << Verbose(1) << "Begin of LinkedCell" << endl;
[834ff3]37 if (set->IsEmpty()) {
38 cerr << "ERROR: set contains no linked cell nodes!" << endl;
[a048fa]39 return;
40 }
41 // 1. find max and min per axis of atoms
[834ff3]42 set->GoToFirst();
43 Walker = set->GetPoint();
[a048fa]44 for (int i=0;i<NDIM;i++) {
[834ff3]45 max.x[i] = Walker->node->x[i];
46 min.x[i] = Walker->node->x[i];
[a048fa]47 }
[834ff3]48 set->GoToFirst();
49 while (!set->IsLast()) {
50 Walker = set->GetPoint();
[a048fa]51 for (int i=0;i<NDIM;i++) {
[834ff3]52 if (max.x[i] < Walker->node->x[i])
53 max.x[i] = Walker->node->x[i];
54 if (min.x[i] > Walker->node->x[i])
55 min.x[i] = Walker->node->x[i];
[a048fa]56 }
[834ff3]57 set->GoToNext();
[a048fa]58 }
59 cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
[e08f45]60
[834ff3]61 // 2. find then number of cells per axis
[a048fa]62 for (int i=0;i<NDIM;i++) {
63 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
64 }
65 cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
[e08f45]66
[a048fa]67 // 3. allocate the lists
68 cout << Verbose(2) << "Allocating cells ... ";
69 if (LC != NULL) {
70 cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
71 return;
72 }
[834ff3]73 LC = new LinkedNodes[N[0]*N[1]*N[2]];
[a048fa]74 for (index=0;index<N[0]*N[1]*N[2];index++) {
75 LC [index].clear();
76 }
77 cout << "done." << endl;
[e08f45]78
[a048fa]79 // 4. put each atom into its respective cell
[8bbe43]80 cout << Verbose(2) << "Filling cells ... ";
[834ff3]81 set->GoToFirst();
82 while (!set->IsLast()) {
83 Walker = set->GetPoint();
[a048fa]84 for (int i=0;i<NDIM;i++) {
[834ff3]85 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
[a048fa]86 }
87 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
88 LC[index].push_back(Walker);
89 //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[834ff3]90 set->GoToNext();
[a048fa]91 }
[8bbe43]92 cout << "done." << endl;
[a048fa]93 cout << Verbose(1) << "End of LinkedCell" << endl;
[95183e]94};
95
96/** Destructor for class LinkedCell.
97 */
98LinkedCell::~LinkedCell()
99{
[a048fa]100 if (LC != NULL)
101 for (index=0;index<N[0]*N[1]*N[2];index++)
102 LC[index].clear();
103 delete[](LC);
104 for(int i=0;i<NDIM;i++)
105 N[i] = 0;
106 index = -1;
107 max.Zero();
108 min.Zero();
[95183e]109};
110
111/** Checks whether LinkedCell::n[] is each within [0,N[]].
112 * \return if all in intervals - true, else -false
113 */
114bool LinkedCell::CheckBounds()
115{
[a048fa]116 bool status = true;
117 for(int i=0;i<NDIM;i++)
118 status = status && ((n[i] >=0) && (n[i] < N[i]));
119 if (!status)
120 cerr << "ERROR: indices are out of bounds!" << endl;
121 return status;
[95183e]122};
123
124
125/** Returns a pointer to the current cell.
126 * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds.
127 */
[834ff3]128LinkedNodes* LinkedCell::GetCurrentCell()
[95183e]129{
[a048fa]130 if (CheckBounds()) {
131 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
132 return (&(LC[index]));
133 } else {
134 return NULL;
135 }
[95183e]136};
137
[834ff3]138/** Calculates the index for a given LCNode *Walker.
139 * \param *Walker LCNode to set index tos
[95183e]140 * \return if the atom is also found in this cell - true, else - false
141 */
[834ff3]142bool LinkedCell::SetIndexToNode(TesselPoint *Walker)
[95183e]143{
[a048fa]144 bool status = false;
145 for (int i=0;i<NDIM;i++) {
[834ff3]146 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
[a048fa]147 }
148 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
149 if (CheckBounds()) {
[834ff3]150 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[a048fa]151 status = status || ((*Runner) == Walker);
152 return status;
153 } else {
[834ff3]154 cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl;
[a048fa]155 return false;
156 }
[95183e]157};
158
159/** Calculates the index for a given Vector *x.
160 * \param *x Vector with coordinates
161 * \return Vector is inside bounding box - true, else - false
162 */
163bool LinkedCell::SetIndexToVector(Vector *x)
164{
[a048fa]165 bool status = true;
166 for (int i=0;i<NDIM;i++) {
167 n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS);
168 if (max.x[i] < x->x[i])
169 status = false;
170 if (min.x[i] > x->x[i])
171 status = false;
172 }
173 return status;
[95183e]174};
[e08f45]175
Note: See TracBrowser for help on using the repository browser.