BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
particles.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/model/particles.cpp
6 //! @brief Implements Particle class
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <cmath>
17 
18 namespace RealSpace {
19 namespace Particles {
20 
21 QString const& name(EShape k)
22 {
23  static QString names[] = {
24  "",
25  "FullSphere",
26  "FullSpheroid",
27  "Cylinder",
28  "TruncatedSphere",
29  "TruncatedSpheroid",
30  "Cone",
31  "Icosahedron",
32  "Dodecahedron",
33  "TruncatedCube",
34  "Prism6",
35  "Cone6",
36  "Pyramid",
37  "Cuboctahedron",
38  "Prism3",
39  "Tetrahedron",
40  "EllipsoidalCylinder",
41  "BarGauss",
42  "BarLorentz",
43  "Box",
44  "HemiEllipsoid",
45  "Dot",
46  "CosineRippleBox",
47  "CosineRippleGauss",
48  "CosineRippleLorentz",
49  "SawtoothRippleBox",
50  "SawtoothRippleGauss",
51  "SawtoothRippleLorentz",
52  "AnisoPyramid",
53  };
54  return names[uint(k)];
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 using namespace GeometricID;
60 
61 Particle::Particle(Key key) : Object(key), scale(Vector3D::_1) {}
62 
64 {
66 }
67 
68 void Particle::transform(Vector3D rotate_, Vector3D translate_)
69 {
70  Object::transform(turn, scale, (rotate = rotate_), offset + (translate = translate_));
71 }
72 
73 void Particle::fancy(Vector3D rotate, float r)
74 {
76 }
77 
78 void Particle::addTransform(Vector3D rotate_, Vector3D translate_)
79 {
80  Object::transform(turn, scale, (rotate = rotate + rotate_),
81  offset + (translate = translate + translate_));
82 }
83 
85 {
87 }
88 
90 {
91  Object::addExtrinsicRotation(turn, scale, rotate, rotateExtrinsic,
92  (translate = offset + translate));
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 static float const pi = float(M_PI);
98 static float const pi2f = float(M_PI_2);
99 static float const sqrt2f = std::sqrt(2.f);
100 static float const sqrt3f = std::sqrt(3.f);
101 
102 // see ~/BornAgain/GUI/ba3d/ba3d/model/geometry/ for BaseShape construction
103 
104 AnisoPyramid::AnisoPyramid(float L, float W, float H, float alpha)
106  (1.0f - H / (std::sqrt((L * L / 4) + (W * W / 4)) * std::tan(alpha))), 4))
107 {
108  isNull = (L <= 0 || W <= 0 || H <= 0 || alpha <= 0);
109  turn = Vector3D(0, 0, 45 * pi / 180.0f);
110  scale = Vector3D(L * sqrt2f, W * sqrt2f, H);
111  offset = Vector3D(0, 0, 0);
112  set();
113 }
114 
115 BarGauss::BarGauss(float L, float W, float H) : Particle(Key(BaseShape::Column, 1.0f, 4))
116 {
117  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
118  turn = Vector3D(0, 0, 45 * pi / 180.0f);
119  scale = Vector3D(L * sqrt2f, W * sqrt2f, H);
120  offset = Vector3D(0, 0, 0);
121  set();
122 }
123 
124 BarLorentz::BarLorentz(float L, float W, float H) : Particle(Key(BaseShape::Column, 1.0f, 4))
125 {
126  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
127  turn = Vector3D(0, 0, 45 * pi / 180.0f);
128  scale = Vector3D(L * sqrt2f, W * sqrt2f, H);
129  offset = Vector3D(0, 0, 0);
130  set();
131 }
132 
133 Box::Box(float L, float W, float H) : Particle(Key(BaseShape::Column, 1.0f, 4))
134 {
135  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
136  turn = Vector3D(0, 0, 45 * pi / 180.0f);
137  scale = Vector3D(L * sqrt2f, W * sqrt2f, H);
138  offset = Vector3D(0, 0, 0);
139  set();
140 }
141 
142 Cone::Cone(float R, float H, float alpha)
143  : Particle(Key(BaseShape::Column, (1.0f - H / (R * std::tan(alpha))), 0))
144 {
145  isNull = (R <= 0 || H <= 0 || alpha <= 0);
146  scale = Vector3D(R * 2, R * 2, H);
147  offset = Vector3D(0, 0, 0);
148  set();
149 }
150 
151 Cone6::Cone6(float R, float H, float alpha)
152  : Particle(Key(BaseShape::Column, (1.0f - H / (R * std::tan(alpha))), 6))
153 {
154  isNull = (R <= 0 || H <= 0 || alpha <= 0);
155  scale = Vector3D(R * 2, R * 2, H);
156  offset = Vector3D(0, 0, 0);
157  set();
158 }
159 
160 Cuboctahedron::Cuboctahedron(float L, float H, float rH, float alpha)
161  : Particle(Key(BaseShape::Cuboctahedron, rH, alpha, H / L))
162 {
163  isNull = (L <= 0 || H <= 0 || rH <= 0 || alpha >= pi2f);
164  scale = Vector3D(L, L, L);
165  offset = Vector3D(0, 0, 0);
166  set();
167 }
168 
169 Cylinder::Cylinder(float R, float H) : Particle(Key(BaseShape::Column, 1.0f, 0))
170 {
171  isNull = (R <= 0 || H <= 0);
172  scale = Vector3D(R * 2, R * 2, H);
173  offset = Vector3D(0, 0, 0);
174  set();
175 }
176 
178 {
179  isNull = (L <= 0);
180  float R = L / DodecahedronL2R;
181  scale = Vector3D(R * 2, R * 2, R * 2);
182  offset = Vector3D(0, 0, 0);
183  set();
184 }
185 
187 {
188  float R = 1.0f;
189  scale = Vector3D(R * 2);
190  offset = Vector3D(0, 0, 0);
191  set();
192 }
193 
194 EllipsoidalCylinder::EllipsoidalCylinder(float Ra, float Rb, float H)
195  : Particle(Key(BaseShape::Column, 1.0f, 0))
196 {
197  isNull = (Ra <= 0 || Rb <= 0 || H <= 0);
198  scale = Vector3D(Ra * 2, Rb * 2, H);
199  offset = Vector3D(0, 0, 0);
200  set();
201 }
202 
204 {
205  isNull = (R <= 0);
206  scale = Vector3D(R * 2);
207  offset = Vector3D(0, 0, 0);
208  set();
209 }
210 
211 FullSpheroid::FullSpheroid(float R, float H) : Particle(Key(BaseShape::Sphere, 0, 0.5f))
212 {
213  isNull = (R <= 0 || H <= 0);
214  scale = Vector3D(R * 2, R * 2, H);
215  offset = Vector3D(0, 0, 0);
216  set();
217 }
218 
219 HemiEllipsoid::HemiEllipsoid(float Ra, float Rb, float H)
220  : Particle(Key(BaseShape::Sphere, .5f, 0.0f))
221 {
222  isNull = (Ra <= 0 || Rb <= 0 || H <= 0);
223  scale = Vector3D(Ra * 2, Rb * 2, H * 2);
224  offset = Vector3D(0, 0, 0);
225  set();
226 }
227 
229 {
230  isNull = (L <= 0);
231  scale = Vector3D(L, L, L);
232  offset = Vector3D(0, 0, 0);
233  set();
234 }
235 
236 Prism3::Prism3(float L, float H) : Particle(Key(BaseShape::Column, 1.0f, 3))
237 {
238  isNull = (L <= 0 || H <= 0);
239  float D = L / sqrt3f;
240  scale = Vector3D(D * 2, D * 2, H);
241  offset = Vector3D(0, 0, 0);
242  set();
243 }
244 
245 Prism6::Prism6(float R, float H) : Particle(Key(BaseShape::Column, 1.0f, 6))
246 {
247  isNull = (R <= 0 || H <= 0);
248  scale = Vector3D(R * 2, R * 2, H);
249  offset = Vector3D(0, 0, 0);
250  set();
251 }
252 
253 Pyramid::Pyramid(float L, float H, float alpha)
254  : Particle(Key(BaseShape::Column, (1.0f - H / (std::sqrt(L * L / 2) * std::tan(alpha))), 4))
255 {
256  isNull = (L <= 0 || H <= 0 || alpha <= 0);
257  float L2 = L * sqrt2f;
258  turn = Vector3D(0, 0, 45 * pi / 180.0f);
259  scale = Vector3D(L2, L2, H);
260  offset = Vector3D(0, 0, 0);
261  set();
262 }
263 
264 CosineRippleBox::CosineRippleBox(float L, float W, float H) : Particle(Key(BaseShape::Ripple, 0, 0))
265 {
266  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
267  turn = Vector3D(0, 0, 0);
268  scale = Vector3D(L, W, H);
269  offset = Vector3D(0, 0, 0);
270  set();
271 }
272 
273 CosineRippleGauss::CosineRippleGauss(float L, float W, float H)
274  : Particle(Key(BaseShape::Ripple, 0, 0))
275 {
276  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
277  turn = Vector3D(0, 0, 0);
278  scale = Vector3D(L, W, H);
279  offset = Vector3D(0, 0, 0);
280  set();
281 }
282 
284  : Particle(Key(BaseShape::Ripple, 0, 0))
285 {
286  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
287  turn = Vector3D(0, 0, 0);
288  scale = Vector3D(L, W, H);
289  offset = Vector3D(0, 0, 0);
290  set();
291 }
292 
293 SawtoothRippleBox::SawtoothRippleBox(float L, float W, float H)
294  : Particle(Key(BaseShape::Ripple, 0, 0))
295 {
296  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
297  turn = Vector3D(0, 0, 0);
298  scale = Vector3D(L, W, H);
299  offset = Vector3D(0, 0, 0);
300  set();
301 }
302 
304  : Particle(Key(BaseShape::Ripple, 0, 0))
305 {
306  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
307  turn = Vector3D(0, 0, 0);
308  scale = Vector3D(L, W, H);
309  offset = Vector3D(0, 0, 0);
310  set();
311 }
312 
314  : Particle(Key(BaseShape::Ripple, 0, 0))
315 {
316  isNull = (L < 0 || W < 0 || H < 0) || (L <= 0 && W <= 0 && H <= 0);
317  turn = Vector3D(0, 0, 0);
318  scale = Vector3D(L, W, H);
319  offset = Vector3D(0, 0, 0);
320  set();
321 }
322 
323 Tetrahedron::Tetrahedron(float L, float H, float alpha)
324  : Particle(Key(BaseShape::Column, (1.0f - H / (L / sqrt3f * std::tan(alpha))), 3))
325 {
326  isNull = (L <= 0 || H <= 0 || alpha <= 0);
327  float D = L / sqrt3f;
328  scale = Vector3D(D * 2, D * 2, H);
329  offset = Vector3D(0, 0, 0);
330  set();
331 }
332 
334 {
335  isNull = (L <= 0);
336  scale = Vector3D(L, L, L);
337  offset = Vector3D(0, 0, 0);
338  set();
339 }
340 
341 TruncatedSphere::TruncatedSphere(float R, float H, float deltaH)
342  : Particle(Key(BaseShape::Sphere, 1 - H / R / 2, (H - R) / R / 2, deltaH / R / 2))
343 {
344  isNull = (R <= 0 || H <= 0);
345  scale = Vector3D(R * 2);
346  offset = Vector3D(0, 0, 0);
347  set();
348 }
349 
350 TruncatedSpheroid::TruncatedSpheroid(float R, float H, float fp, float deltaH)
351  : Particle(
352  Key(BaseShape::Sphere, 1 - H / fp / R / 2, (H - fp * R) / fp / R / 2, deltaH / fp / R / 2))
353 {
354  isNull = (R <= 0 || H <= 0 || fp <= 0);
355  scale = Vector3D(R * 2, R * 2, fp * R * 2);
356  offset = Vector3D(0, 0, 0);
357  set();
358 }
359 } // namespace Particles
360 } // namespace RealSpace
#define M_PI_2
Definition: Constants.h:45
#define M_PI
Definition: Constants.h:44
void addExtrinsicRotation(Vector3D turn, Vector3D scale, Vector3D &rotate, Vector3D rotateExtrinsic, Vector3D &translate)
Definition: object.cpp:73
void transform(float scale, Vector3D rotate, Vector3D translate)
Definition: object.cpp:45
AnisoPyramid(float L, float W, float H, float alpha)
Definition: particles.cpp:104
BarGauss(float L, float W, float H)
Definition: particles.cpp:115
BarLorentz(float L, float W, float H)
Definition: particles.cpp:124
Box(float L, float W, float H)
Definition: particles.cpp:133
Cone6(float R, float H, float alpha)
Definition: particles.cpp:151
Cone(float R, float H, float alpha)
Definition: particles.cpp:142
CosineRippleBox(float L, float W, float H)
Definition: particles.cpp:264
CosineRippleGauss(float L, float W, float H)
Definition: particles.cpp:273
CosineRippleLorentz(float L, float W, float H)
Definition: particles.cpp:283
Cuboctahedron(float L, float H, float rH, float alpha)
Definition: particles.cpp:160
Cylinder(float R, float H)
Definition: particles.cpp:169
EllipsoidalCylinder(float Ra, float Rb, float H)
Definition: particles.cpp:194
HemiEllipsoid(float Ra, float Rb, float H)
Definition: particles.cpp:219
void addTransform(Vector3D rotate, Vector3D translate)
Definition: particles.cpp:78
void addExtrinsicRotation(Vector3D rotateExtrinsic)
Definition: particles.cpp:89
void addTranslation(Vector3D translate_)
Definition: particles.cpp:84
void fancy(Vector3D rotate, float r)
Definition: particles.cpp:73
void transform(Vector3D rotate, Vector3D translate)
Definition: particles.cpp:68
Particle(GeometricID::Key)
Definition: particles.cpp:61
Prism3(float L, float H)
Definition: particles.cpp:236
Prism6(float R, float H)
Definition: particles.cpp:245
Pyramid(float L, float H, float alpha)
Definition: particles.cpp:253
SawtoothRippleBox(float L, float W, float H)
Definition: particles.cpp:293
SawtoothRippleGauss(float L, float W, float H)
Definition: particles.cpp:303
SawtoothRippleLorentz(float L, float W, float H)
Definition: particles.cpp:313
Tetrahedron(float L, float H, float alpha)
Definition: particles.cpp:323
TruncatedSphere(float R, float H, float deltaH=0.0f)
Definition: particles.cpp:341
TruncatedSpheroid(float R, float H, float fp, float deltaH=0.0f)
Definition: particles.cpp:350
QString const & name(EShape k)
Definition: particles.cpp:21
static float const pi
Definition: particles.cpp:97
static float const pi2f
Definition: particles.cpp:98
static float const sqrt3f
Definition: particles.cpp:100
static float const sqrt2f
Definition: particles.cpp:99
const float DodecahedronL2R
Definition: filesystem.h:81
Defines Particle class.
static Vector3D const _0
Definition: def.h:49