File: Polyhedra.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (458 lines) | stat: -rw-r--r-- 13,592 bytes parent folder | download
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/HardParticle/Polyhedra.cpp
//! @brief     Implements class Box.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Sample/HardParticle/Polyhedra.h"
#include "Base/Const/Units.h"
#include <ff/Make.h>
#include <numbers>

using std::numbers::pi;

//  ************************************************************************************************
//  Prisms
//  ************************************************************************************************

Box::Box(const std::vector<double> P)
    : IFormfactorPrism(P)
    , m_length(m_P[0])
    , m_width(m_P[1])
    , m_height(m_P[2])
{
    pimpl.reset(ff::make::Box(m_length, m_width, m_height));
    m_validated = true;
}

bool Box::contains(const R3& position) const
{
    if (std::abs(position.x()) <= length() / 2 && std::abs(position.y()) <= width() / 2
        && (position.z() >= 0 && position.z() <= height()))
        return true;

    return false;
}

Box::Box(double length, double width, double height)
    : Box(std::vector<double>{length, width, height})
{
}


Prism3::Prism3(const std::vector<double> P)
    : IFormfactorPrism(P)
    , m_base_edge(m_P[0])
    , m_height(m_P[1])
{
    pimpl.reset(ff::make::Prism3(m_base_edge, m_height));
    m_validated = true;
}

bool Prism3::contains(const R3& position) const
{
    double B = baseEdge();
    double H = height();

    double l = B * std::sin(pi / 3);
    double x_shift = B / 2 * std::tan(pi / 6);

    if (position.x() + x_shift < 0 || position.x() + x_shift > l || std::abs(position.y()) > B / 2
        || position.z() < 0 || position.z() > H)
        return false;

    double theta = 0; // angle between position & x-axis in position.z() plane
    if (position.x() + x_shift != 0 || position.y() != 0)
        theta =
            std::asin(std::abs(position.y())
                      / std::sqrt(std::pow(position.x() + x_shift, 2) + std::pow(position.y(), 2)));

    double k = l / (std::sin(theta) / std::tan(pi / 6) + std::cos(theta));

    if (std::pow(position.x() + x_shift, 2) + std::pow(position.y(), 2) <= std::pow(k, 2))
        return true;

    return false;
}

Prism3::Prism3(double base_edge, double height)
    : Prism3(std::vector<double>{base_edge, height})
{
}


Prism6::Prism6(const std::vector<double> P)
    : IFormfactorPrism(P)
    , m_base_edge(m_P[0])
    , m_height(m_P[1])
{
    pimpl.reset(ff::make::Prism6(m_base_edge, m_height));
    m_validated = true;
}

bool Prism6::contains(const R3& position) const
{
    double B = baseEdge();
    double H = height();

    if (std::abs(position.x()) > B || std::abs(position.y()) > B || position.z() < 0
        || position.z() > H)
        return false;

    double theta_prime = 0; // angle between position & x-axis in position.z() plane
    if (position.x() != 0 || position.y() != 0)
        theta_prime = Units::rad2deg(
            std::asin(std::abs(position.y())
                      / std::sqrt(std::pow(position.x(), 2) + std::pow(position.y(), 2))));
    int c = static_cast<int>(theta_prime / 60); // multiplicative constant
    double theta = Units::deg2rad(theta_prime - c * 60);
    double k_z = B / (std::cos(theta) + std::sin(theta) / std::tan(pi / 3));

    if (std::pow(position.x(), 2) + std::pow(position.y(), 2) <= std::pow(k_z, 2))
        return true;

    return false;
}

Prism6::Prism6(double base_edge, double height)
    : Prism6(std::vector<double>{base_edge, height})
{
}

//  ************************************************************************************************
//  Platonic solids
//  ************************************************************************************************

PlatonicTetrahedron::PlatonicTetrahedron(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_edge(m_P[0])
{
    pimpl.reset(ff::make::Tetrahedron(m_edge));
    m_validated = true;
}

bool PlatonicTetrahedron::contains(const R3&) const
{
    // TODO: Implement Platonic tetrahedron
    std::ostringstream ostr;
    ostr << "Outer shape Platonic tetrahedron not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

PlatonicTetrahedron::PlatonicTetrahedron(double edge)
    : PlatonicTetrahedron(std::vector<double>{edge})
{
}


PlatonicOctahedron::PlatonicOctahedron(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_edge(m_P[0])
{
    pimpl.reset(ff::make::Octahedron(m_edge));
    m_validated = true;
}

bool PlatonicOctahedron::contains(const R3&) const
{
    // TODO: Implement Platonic octahedron
    std::ostringstream ostr;
    ostr << "Outer shape Platonic octahedron not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

PlatonicOctahedron::PlatonicOctahedron(double edge)
    : PlatonicOctahedron(std::vector<double>{edge})
{
}


Dodecahedron::Dodecahedron(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_edge(m_P[0])
{
    pimpl.reset(ff::make::Dodecahedron(m_edge));
    m_validated = true;
}

bool Dodecahedron::contains(const R3&) const
{
    // TODO: Implement Dodecahedron
    std::ostringstream ostr;
    ostr << "Outer shape Dodecahedron not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

Dodecahedron::Dodecahedron(double edge)
    : Dodecahedron(std::vector<double>{edge})
{
}


Icosahedron::Icosahedron(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_edge(m_P[0])
{
    pimpl.reset(ff::make::Icosahedron(m_edge));
    m_validated = true;
}

bool Icosahedron::contains(const R3&) const
{
    // TODO: Implement Icosahedron
    std::ostringstream ostr;
    ostr << "Outer shape Icosahedron not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

Icosahedron::Icosahedron(double edge)
    : Icosahedron(std::vector<double>{edge})
{
}

//  ************************************************************************************************
//  Pyramids
//  ************************************************************************************************

Pyramid2::Pyramid2(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_length(m_P[0])
    , m_width(m_P[1])
    , m_height(m_P[2])
    , m_alpha(m_P[3])
{
    pimpl.reset(ff::make::Pyramid2(m_length, m_width, m_height, m_alpha));
    m_validated = true;
}

bool Pyramid2::contains(const R3& position) const
{
    double l_z = length() / 2
                 - position.z() / std::tan(alpha()); // half-length of rectangle at a given height
    double w_z =
        width() / 2 - position.z() / std::tan(alpha()); // half-width of rectangle at a given height
    if (std::abs(position.x()) <= l_z && std::abs(position.y()) <= w_z
        && (position.z() >= 0 && position.z() <= height()))
        return true;

    return false;
}

Pyramid2::Pyramid2(double length, double width, double height, double alpha)
    : Pyramid2(std::vector<double>{length, width, height, alpha})
{
}


Pyramid3::Pyramid3(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_base_edge(m_P[0])
    , m_height(m_P[1])
    , m_alpha(m_P[2])
{
    pimpl.reset(ff::make::Pyramid3(m_base_edge, m_height, m_alpha));
    m_validated = true;
}

bool Pyramid3::contains(const R3& position) const
{
    double B = baseEdge();
    double H = height();

    double B_z = B - position.z() * 2 / std::tan(alpha()); // edge of triangle at a given height

    double l = B_z * std::sin(pi / 3);
    double x_shift = B_z / 2 * std::tan(pi / 6);

    if (position.x() + x_shift < 0 || position.x() + x_shift > l || std::abs(position.y()) > B_z / 2
        || position.z() < 0 || position.z() > H)
        return false;

    double theta = 0; // angle between position & x-axis in position.z() plane
    if (position.x() + x_shift != 0 || position.y() != 0)
        theta =
            std::asin(std::abs(position.y())
                      / std::sqrt(std::pow(position.x() + x_shift, 2) + std::pow(position.y(), 2)));

    double k = l / (std::sin(theta) / std::tan(pi / 6) + std::cos(theta));

    if (std::pow(position.x() + x_shift, 2) + std::pow(position.y(), 2) <= std::pow(k, 2))
        return true;

    return false;
}

Pyramid3::Pyramid3(double base_edge, double height, double alpha)
    : Pyramid3(std::vector<double>{base_edge, height, alpha})
{
}


Pyramid4::Pyramid4(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_base_edge(m_P[0])
    , m_height(m_P[1])
    , m_alpha(m_P[2])
{
    pimpl.reset(ff::make::Pyramid4(m_base_edge, m_height, m_alpha));
    m_validated = true;
}

bool Pyramid4::contains(const R3& position) const
{
    double B = baseEdge();
    double H = height();

    double l_z =
        B / 2 - position.z() / std::tan(alpha()); // half-length of square at a given height
    if (std::abs(position.x()) <= l_z && std::abs(position.y()) <= l_z
        && (position.z() >= 0 && position.z() <= H))
        return true;

    return false;
}

Pyramid4::Pyramid4(double base_edge, double height, double alpha)
    : Pyramid4(std::vector<double>{base_edge, height, alpha})
{
}


Pyramid6::Pyramid6(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_base_edge(m_P[0])
    , m_height(m_P[1])
    , m_alpha(m_P[2])
{
    pimpl.reset(ff::make::Pyramid6(m_base_edge, m_height, m_alpha));
    m_validated = true;
}

bool Pyramid6::contains(const R3& position) const
{
    double B = baseEdge();

    if (std::abs(position.x()) > B || std::abs(position.y()) > B || position.z() < 0
        || position.z() > height())
        return false;

    double l_z = B - position.z() / std::tan(alpha()); // edge of hexagon at a given height
    double theta_prime = 0; // angle between position & x-axis in position.z() plane
    if (position.x() != 0 || position.y() != 0)
        theta_prime = Units::rad2deg(
            std::asin(std::abs(position.y())
                      / std::sqrt(std::pow(position.x(), 2) + std::pow(position.y(), 2))));
    int c = static_cast<int>(theta_prime / 60); // multiplication constant
    double theta = Units::deg2rad(theta_prime - c * 60);
    double k_z = l_z / (std::cos(theta) + std::sin(theta) / std::tan(pi / 3));

    if (std::pow(position.x(), 2) + std::pow(position.y(), 2) <= std::pow(k_z, 2))
        return true;

    return false;
}

Pyramid6::Pyramid6(double base_edge, double height, double alpha)
    : Pyramid6(std::vector<double>{base_edge, height, alpha})
{
}


Bipyramid4::Bipyramid4(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_length(m_P[0])
    , m_base_height(m_P[1])
    , m_height_ratio(m_P[2])
    , m_alpha(m_P[3])
{
    pimpl.reset(ff::make::Bipyramid4(m_length, m_base_height, m_height_ratio, m_alpha));
    m_validated = true;
}

bool Bipyramid4::contains(const R3& position) const
{
    double L = length();
    double H = base_height();
    double rH = heightRatio();

    double total_Height = H + rH * H;

    if (std::abs(position.x()) > L / 2 || std::abs(position.y()) > L / 2 || position.z() < 0
        || position.z() > total_Height)
        return false;

    // half-length of square (i.e. horizontal cross-section of Bipyramid4) at a given height
    double l_z = L / 2 - std::abs(H - position.z()) / std::tan(alpha());
    if (std::abs(position.x()) <= l_z && std::abs(position.y()) <= l_z)
        return true;

    return false;
}

Bipyramid4::Bipyramid4(double length, double base_height, double height_ratio, double alpha)
    : Bipyramid4(std::vector<double>{length, base_height, height_ratio, alpha})
{
}

//  ************************************************************************************************
//  Others
//  ************************************************************************************************

CantellatedCube::CantellatedCube(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_length(m_P[0])
    , m_removed_length(m_P[1])
{
    pimpl.reset(ff::make::CantellatedCube(m_length, m_removed_length));
    m_validated = true;
}

bool CantellatedCube::contains(const R3&) const
{
    // TODO: Implement Cantellated cube
    std::ostringstream ostr;
    ostr << "Outer shape Cantellated cube not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

CantellatedCube::CantellatedCube(double length, double removed_length)
    : CantellatedCube(std::vector<double>{length, removed_length})
{
}


TruncatedCube::TruncatedCube(const std::vector<double> P)
    : IFormfactorPolyhedron(P)
    , m_length(m_P[0])
    , m_removed_length(m_P[1])
{
    pimpl.reset(ff::make::TruncatedCube(m_length, m_removed_length));
    m_validated = true;
}

bool TruncatedCube::contains(const R3&) const
{
    // TODO: Implement Truncated cube
    std::ostringstream ostr;
    ostr << "Outer shape Truncated cube not yet implemented for Mesocrystal";
    ostr << "\n\nStay tuned!";
    throw std::runtime_error(ostr.str());
}

TruncatedCube::TruncatedCube(double length, double removed_length)
    : TruncatedCube(std::vector<double>{length, removed_length})
{
}