-
Notifications
You must be signed in to change notification settings - Fork 4
/
Cube.h
138 lines (117 loc) · 2.49 KB
/
Cube.h
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
/*
Pharmit
Copyright (c) David Ryan Koes, University of Pittsburgh and contributors.
All rights reserved.
Pharmit is licensed under both the BSD 3-clause license and the GNU
Public License version 2. Any use of the code that retains its reliance
on the GPL-licensed OpenBabel library is subject to the terms of the GPL2.
Use of the Pharmit code independently of OpenBabel (or any other
GPL2 licensed software) may choose between the BSD or GPL licenses.
See the LICENSE file provided with the distribution for more information.
*/
/*
* Cube.h
*
* A simple class for representing a cube.
* Created on: Sep 28, 2011
* Author: dkoes
*/
#ifndef CUBE_H_
#define CUBE_H_
#include <cmath>
#include <cfloat>
#include <algorithm>
using namespace std;
/* A cube. Functions for consistently dividing into octants */
class Cube
{
public:
float x, y, z; //bottom corner
float dim;
inline float squared(float v) const
{
return v * v;
}
//find the minimum distance between line segements a1-a2 and b1-b2
inline float min1dist(float a1, float a2, float b1, float b2) const
{
//eh, just try all combos
return min(min(fabs(a1-b2),fabs(a1-b1)),min(fabs(a2-b2),fabs(a2-b1)));
}
public:
Cube(float d) :
x(0), y(0), z(0), dim(d)
{
}
Cube(float d, float X, float Y, float Z): x(X), y(Y), z(Z), dim(d) {}
float getDimension() const
{
return dim;
}
float volume() const
{
return dim*dim*dim;
}
void getCenter(float& cx, float& cy, float& cz) const
{
cx = x+dim/2;
cy = y+dim/2;
cz = z+dim/2;
}
void getBottomCorner(float& bx, float& by, float& bz) const
{
bx = x;
by = y;
bz = z;
}
//return i'th octant
Cube getOctant(unsigned i) const
{
Cube res = *this;
res.dim /= 2.0;
switch (i)
{
case 0:
break;
case 1:
res.x += res.dim;
break;
case 2:
res.y += res.dim;
break;
case 3:
res.x += res.dim;
res.y += res.dim;
break;
case 4:
res.z += res.dim;
break;
case 5:
res.x += res.dim;
res.z += res.dim;
break;
case 6:
res.y += res.dim;
res.z += res.dim;
break;
case 7:
res.x += res.dim;
res.y += res.dim;
res.z += res.dim;
break;
default:
abort();
break;
}
return res;
}
//shortest distance from this cube to rhs
float minDist(const Cube& rhs) const
{
float minx = min1dist(x, x+dim, rhs.x, rhs.x+rhs.dim);
float miny = min1dist(y, y+dim, rhs.y, rhs.y+rhs.dim);
float minz = min1dist(z, z+dim, rhs.z, rhs.z+rhs.dim);
return sqrt(minx*minx+miny*miny+minz*minz);
}
};
#endif /* CUBE_H_ */