File: Ripple.cpp

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 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 (80 lines) | stat: -rw-r--r-- 2,879 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Img3D/Mesh/Ripple.cpp
//! @brief     Implements utility functions in ba3d namespace.
//!
//! @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 "Base/Util/Assert.h"
#include "Img3D/Model/Geometry.h"
#include <numbers>

using std::numbers::pi;

namespace Img3D {

Geometry::Mesh Geometry::meshRipple(float numSides, float ratio_asymmetry_W)
{
    int const sides = qRound(numSides);
    bool const smooth = (0 == sides); // sides = 0 implies smooth -> e.g. cosine ripple
    int const slices = smooth ? 4 * SLICES : sides;

    // Values are chosen such that length, width and height are 1
    float const R = .5f;
    float const H = 2 * R;                             // L = W = H = 2*R
    float const asymmetry = ratio_asymmetry_W * 2 * R; // for CosineRipple asymmetry is inherently 0

    Vertices vfront(slices), vback(slices);

    if (numSides == 3) {
        // SawtoothRipple: saw-tooth (3 rectangular sides and 2 triangular front and back)
        vfront[0] = F3(-R, -R, 0);
        vfront[1] = F3(-R, asymmetry, H);
        vfront[2] = F3(-R, R, 0);

        vback[0] = F3(R, -R, 0);
        vback[1] = F3(R, asymmetry, H);
        vback[2] = F3(R, R, 0);
    } else if (numSides == 0) {
        // CosineRipple: cosine ripple
        for (int s = 0; s < slices; ++s) {
            auto th = static_cast<float>(pi * s / (slices + 1));
            float y = -R * cosf(th);
            float z = R * (1.0f + cosf(2 * static_cast<float>(pi) * y / (2 * R)));
            vfront[s] = F3(-R, y, z);
            vback[s] = F3(R, y, z);
        }
    }

    int const nv = (3 + 3 + 6) * slices; // 3 for part of front face, 3 for part of back face
                                         // 6 for side face

    Vertices vs;
    vs.reserve(nv);

    for (int s = 0; s < slices; ++s) {
        int s1 = s, s2 = (s + 1) % slices;

        // clockwise ordering of vertices
        vs.addTriangle(vfront.at(s1), vfront.at(s2), F3(-R, asymmetry, H / 2)); // front

        // counter-clockwise ordering of vertices
        vs.addTriangle(vback.at(s1), F3(R, asymmetry, H / 2), vback.at(s2)); // back

        // counter-clockwise ordering of vertices
        vs.addQuad(vfront.at(s2), vfront.at(s1), vback.at(s1), vback.at(s2)); // side
    }

    ASSERT(vs.count() == nv);

    return makeMesh(vs, nullptr); // normals not calculated here and left for makeMesh to calculate
}

} // namespace Img3D