forked from ecell/epdp
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCylinder.hpp
324 lines (276 loc) · 9.62 KB
/
Cylinder.hpp
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
#ifndef CYLINDER_HPP
#define CYLINDER_HPP
#include <ostream>
#include <cmath>
#include "Vector3.hpp"
#include "Shape.hpp"
#include "linear_algebra.hpp"
// Todo. Make sure cylinder is never larger than 1 cellsize or something.
template<typename T_>
class Cylinder
{
public:
typedef T_ value_type;
typedef Vector3<T_> position_type;
typedef T_ length_type;
typedef enum side_enum_type {LEFT=0, RIGHT=1} side_enum_type; // The typedef is a little bit C style but doesn't matter for C++
public:
// constructors
Cylinder()
: position_(), radius_(0), unit_z_(), half_length_(0) {}
Cylinder(position_type const& position, length_type const& radius,
position_type const& unit_z, length_type const& half_length )
: position_(position), radius_(radius), unit_z_(unit_z),
half_length_(half_length) {}
bool operator==(const Cylinder& rhs) const
{
return position_ == rhs.position() && radius_ == rhs.radius() && unit_z_ == rhs.unit_z() && half_length_ == rhs.half_length();
}
bool operator!=(const Cylinder& rhs) const
{
return !operator==(rhs);
}
position_type const& position() const
{
return position_;
}
position_type& position()
{
return position_;
}
length_type const& radius() const
{
return radius_;
}
length_type& radius()
{
return radius_;
}
position_type const& unit_z() const
{
return unit_z_;
}
position_type& unit_z()
{
return unit_z_;
}
length_type const& half_length() const
{
return half_length_;
}
length_type& half_length()
{
return half_length_;
}
const int dof() const
{ // degrees of freedom for particle movement
return 1;
}
std::string show(int precision)
{
std::ostringstream strm;
strm.precision(precision);
strm << *this;
return strm.str();
}
/////// Member variables
private:
position_type position_; // centre.
length_type radius_;
position_type unit_z_; // Z-unit_z. should be normalized.
length_type half_length_;
};
//////// Inline functions
template<typename Tstrm_, typename T_>
inline std::basic_ostream<Tstrm_>& operator<<(std::basic_ostream<Tstrm_>& strm,
const Cylinder<T_>& v)
{
strm << "{" << v.position() << ", " << v.radius() << ", " << v.unit_z() << ", " << v.half_length() << "}";
return strm;
}
template<typename T_>
inline std::pair<typename Cylinder<T_>::length_type,
typename Cylinder<T_>::length_type>
to_internal(Cylinder<T_> const& obj, typename Cylinder<T_>::position_type const& pos)
{
// Return pos relative to position of cylinder.
typedef typename Cylinder<T_>::position_type position_type;
typedef typename Cylinder<T_>::length_type length_type;
const position_type pos_vector(subtract(pos, obj.position()));
const length_type z(dot_product(pos_vector, obj.unit_z())); // z can be < 0
const length_type r(length(subtract(pos_vector, multiply(obj.unit_z(), z)))); // r is always >= 0
return std::make_pair(r, z);
}
// The following projects 'pos' on the cylinder
// It returns a pair of which the first component is the projected position.
// The second is again a pair of which the first entry is the distance of 'pos'
// from the cylinder axis, the second a length l which indicates whether the
// projected position is in the cylinder (true if l negative; l is the negative
// distance of the projected position to the cylinder edge).
template<typename T_>
inline std::pair<typename Cylinder<T_>::position_type,
std::pair<typename Cylinder<T_>::length_type,
typename Cylinder<T_>::length_type> >
project_point(Cylinder<T_> const& obj, typename Cylinder<T_>::position_type const& pos)
{
typedef typename Cylinder<T_>::length_type length_type;
// The projection lies on the z-axis.
std::pair<length_type, length_type> r_z(to_internal(obj, pos));
return std::make_pair( add(obj.position(), multiply(obj.unit_z(), r_z.second)),
std::make_pair(r_z.first,
subtract(abs(r_z.second), obj.half_length())) );
}
//Almost equal to projected point method, but for the substraction of the cylinder radius from the radial distance r.
//And projected point now lies on the surface, not on the central axis.
template<typename T_>
inline std::pair<typename Cylinder<T_>::position_type,
std::pair<typename Cylinder<T_>::length_type,
typename Cylinder<T_>::length_type> >
project_point_on_surface(Cylinder<T_> const& obj,
typename Cylinder<T_>::position_type const& pos)
{
typedef typename Cylinder<T_>::length_type length_type;
typedef typename Cylinder<T_>::position_type position_type;
// Here we do not call 'to_internal' for efficiency
const position_type pos_vector(subtract(pos, obj.position()));
const length_type z ( dot_product(pos_vector, obj.unit_z()) );
const position_type z_vector (multiply(obj.unit_z(), z));
const position_type r_vector (subtract(pos_vector, z_vector));
const length_type r (length(r_vector));
const position_type projected_point( add(obj.position(), z_vector) );
return std::make_pair( add(projected_point, multiply( normalize(r_vector), obj.radius() )),
std::make_pair(subtract(r, obj.radius()),
subtract(abs(z), obj.half_length())) );
}
template<typename T_>
inline typename Cylinder<T_>::length_type
distance(Cylinder<T_> const& obj,
typename Cylinder<T_>::position_type const& pos)
{
//typedef typename Cylinder<T_>::position_type position_type;
typedef typename Cylinder<T_>::length_type length_type;
/* First compute the (r,z) components of pos in a coordinate system
* defined by the vectors unitR and unit_z, where unitR is
* choosen such that unitR and unit_z define a plane in which
* pos lies. */
const std::pair<length_type, length_type> r_z(to_internal(obj, pos));
/* Then compute distance to cylinder. */
const length_type dz(std::fabs(r_z.second) - obj.half_length());
const length_type dr(r_z.first - obj.radius());
length_type distance;
if (dz > 0)
{
// pos is (either) to the right or to the left of the cylinder.
if (r_z.first > obj.radius())
{
// Compute distance to edge.
distance = std::sqrt( dz * dz + dr * dr );
}
else
{
distance = dz;
}
}
else
{
if (dr > obj.radius())
{
// pos is somewhere 'parallel' to the cylinder.
distance = dr;
}
else
{
// Inside cylinder.
distance = std::max(dr, dz);
}
}
return distance;
}
template<typename T_>
inline std::pair<typename Cylinder<T_>::position_type, bool>
deflect(Cylinder<T_> const& obj,
typename Cylinder<T_>::position_type const& r0,
typename Cylinder<T_>::position_type const& d )
{
// Displacements are not deflected on cylinders (yet),
// but this function has to be defined for every shape to be used in structure.
// For now it just returns original pos. + displacement. The changeflage = false.
return std::make_pair( add(r0, d), false );
}
/*
template<typename T_>
inline typename Cylinder<T_>::position_type
deflect_back(Cylinder<T_> const& obj,
typename Cylinder<T_>::position_type const& r,
typename Cylinder<T_>::position_type const& u_z )
{
// Return the vector r without any changes
return r;
}
*/
template<typename T, typename Trng>
inline typename Cylinder<T>::position_type
random_position(Cylinder<T> const& shape, Trng& rng)
{
// -1 < rng() < 1. See for example CylindricalSurface.hpp.
return add(shape.position(),
multiply(shape.unit_z(), rng() * shape.half_length()));
}
template<typename T_>
inline Cylinder<T_> const& shape(Cylinder<T_> const& shape)
{
return shape;
}
template<typename T_>
inline Cylinder<T_>& shape(Cylinder<T_>& shape)
{
return shape;
}
template<typename T_>
struct is_shape<Cylinder<T_> >: public boost::mpl::true_ {};
template<typename T_>
struct shape_position_type<Cylinder<T_> >
{
typedef typename Cylinder<T_>::position_type type;
};
template<typename T_>
struct shape_length_type<Cylinder<T_> > {
typedef typename Cylinder<T_>::length_type type;
};
template<typename T>
inline typename shape_length_type<Cylinder<T> >::type const& shape_size(Cylinder<T> const& shape)
{
return shape.radius();
}
template<typename T>
inline typename shape_length_type<Cylinder<T> >::type& shape_size(Cylinder<T>& shape)
{
return shape.radius();
}
#if defined(HAVE_TR1_FUNCTIONAL)
namespace std { namespace tr1 {
#elif defined(HAVE_STD_HASH)
namespace std {
#elif defined(HAVE_BOOST_FUNCTIONAL_HASH_HPP)
namespace boost {
#endif
template<typename T_>
struct hash<Cylinder<T_> >
{
typedef Cylinder<T_> argument_type;
std::size_t operator()(argument_type const& val)
{
return hash<typename argument_type::position_type>()(val.position()) ^
hash<typename argument_type::length_type>()(val.radius()) ^
hash<typename argument_type::position_type>()(val.unit_z()) ^
hash<typename argument_type::length_type>()(val.half_length());
}
};
#if defined(HAVE_TR1_FUNCTIONAL)
} } // namespace std::tr1
#elif defined(HAVE_STD_HASH)
} // namespace std
#elif defined(HAVE_BOOST_FUNCTIONAL_HASH_HPP)
} // namespace boost
#endif
#endif /* CYLINDER_HPP */