File: Column.cpp

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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,502 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/Column.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::meshColumn(float ratio_Rt_Rb, float numSides)
{
    int const sides = qRound(numSides);
    bool const smooth = (0 == sides); // sides = 0 implies smooth -> e.g. cylinder
    int const slices = smooth ? SLICES : sides;

    // Rt is the top radius, Rb is the bottom radius, H is the height
    // Values are chosen such that diameter and height are 1
    float const R = .5f;
    float Rb = R, Rt = Rb * ratio_Rt_Rb, H = 2 * R;

    // mesh of vertices (bottom and top) and normals
    Vertices vb(slices), vt(slices), nbt(slices);
    float const nz = (1 - Rt / Rb) * H;

    for (int s = 0; s < slices; ++s) {
        float th = float((2 * pi) * s / slices), st = sinf(th), ct = cosf(th);

        F3 vb_(Rb * ct, Rb * st, 0), vt_(Rt * ct, Rt * st, H);
        vb[s] = vb_;
        vt[s] = vt_;
        if (smooth)
            nbt[s] = F3(vb_.x(), vb_.y(), nz).normalized();
    }

    // make into triangles
    int const nv = 6 * 2 * slices;
    Vertices vs;
    vs.reserve(nv);
    Vertices ns;
    if (smooth)
        ns.reserve(nv);

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

        vs.addTriangle(vb.at(s1), F3(0, 0, 0), vb.at(s2)); // bottom
        if (smooth)
            ns.addVertex(F3(0, 0, -1), 3);

        vs.addTriangle(F3(0, 0, H), vt.at(s1), vt.at(s2)); // top
        if (smooth)
            ns.addVertex(F3(0, 0, 1), 3);

        vs.addQuad(vb.at(s1), vb.at(s2), vt.at(s2), vt.at(s1)); // side
        if (smooth) {
            auto &n1 = nbt.at(s1), &n2 = nbt.at(s2);
            ns.addQuad(n1, n2, n2, n1);
        }
    }

    ASSERT(vs.count() == nv);
    ASSERT(!smooth || ns.count() == nv);

    return makeMesh(vs, smooth ? &ns : nullptr);
}

} // namespace Img3D