File: geometry.cpp

package info (click to toggle)
bornagain 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,800 kB
  • sloc: cpp: 469,684; python: 38,920; xml: 805; awk: 630; sh: 286; ansic: 37; makefile: 25
file content (174 lines) | stat: -rw-r--r-- 4,923 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
// ************************************************************************** //
//
//  BornAgain: simulate and fit scattering at grazing incidence
//
//! @file      GUI/ba3d/model/geometry.cpp
//! @brief     Implements Geometry class
//!
//! @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 "GUI/ba3d/model/geometry.h"
#include "Base/Utils/Assert.h"
#include "GUI/ba3d/model/model.h"

namespace RealSpace
{
//------------------------------------------------------------------------------

Geometry::Vert_Normal::Vert_Normal(const Vector3D& v_, const Vector3D& n_) : v(v_), n(n_) {}

void Geometry::Vertices::addVertex(const Vector3D& v, int n)
{
    for (int i = 0; i < n; ++i)
        append(v);
}

void Geometry::Vertices::addTriangle(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3)
{
    append(v1);
    append(v2);
    append(v3);
}

void Geometry::Vertices::addQuad(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3,
                                 const Vector3D& v4)
{
    addTriangle(v1, v2, v3);
    addTriangle(v3, v4, v1);
}

void Geometry::Vertices::addQuad(const Vertices& vs, unsigned i1, unsigned i2, unsigned i3,
                                 unsigned i4)
{
    addQuad(vs.at(i1), vs.at(i2), vs.at(i3), vs.at(i4));
}

void Geometry::Vertices::addStrip(const Vertices& vs, const Indices& is)
{
    ASSERT(is.size() >= 3); // at least one triangle
    for (unsigned i = 0; i + 2 < is.size(); ++i)
        if (i % 2)
            addTriangle(vs.at(is.at(i)), vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
        else
            addTriangle(vs.at(is.at(i)), vs.at(is.at(2 + i)), vs.at(is.at(1 + i)));
}

void Geometry::Vertices::addFan(const Vertices& vs, const Indices& is)
{
    ASSERT(is.size() >= 3); // at least one triangle
    auto& ctr = vs.at(is.at(0));
    for (unsigned i = 0; i + 2 < is.size(); ++i)
        addTriangle(ctr, vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
}

//------------------------------------------------------------------------------

Geometry::Geometry(GeometricID::Key key_) : m_key(key_)
{
    using namespace GeometricID;

    switch (m_key.id) {
    case BaseShape::Plane:
        m_mesh = meshPlane();
        break;
    case BaseShape::Box:
        m_mesh = meshBox();
        break;
    case BaseShape::Sphere:
        m_mesh = meshSphere(m_key.p1, m_key.p2, m_key.p3);
        break;
    case BaseShape::Column:
        m_mesh = meshColumn(m_key.p1, m_key.p2);
        break;
    case BaseShape::Icosahedron:
        m_mesh = meshIcosahedron();
        break;
    case BaseShape::Dodecahedron:
        m_mesh = meshDodecahedron();
        break;
    case BaseShape::TruncatedBox:
        m_mesh = meshTruncBox(m_key.p1);
        break;
    case BaseShape::Cuboctahedron:
        m_mesh = meshCuboctahedron(m_key.p1, m_key.p2, m_key.p3);
        break;
    case BaseShape::Ripple:
        m_mesh = meshRipple(m_key.p1, m_key.p2);
        break;
    }
}

Geometry::~Geometry()
{
    // remove self from the store
    geometryStore().geometryDeleted(*this);
}

Geometry::Mesh Geometry::makeMesh(const Vertices& vs, Vertices const* ns)
{
    int nv = vs.count();
    ASSERT(0 == nv % 3);
    ASSERT(!ns || nv == ns->count()); // if normals not given, will be computed

    Mesh mesh(nv);

    for (int i = 0; i < nv; i += 3) {
        const Vector3D &v0 = vs.at(0 + i), v1 = vs.at(1 + i), v2 = vs.at(2 + i);
        const Vector3D *n0, *n1, *n2;
        Vector3D nm;

        if (ns) {
            n0 = &ns->at(0 + i);
            n1 = &ns->at(1 + i);
            n2 = &ns->at(2 + i);
        } else {
            nm = cross((v1 - v0), (v2 - v0));
            n0 = n1 = n2 = &nm;
        }

        mesh.append(Vert_Normal(v0, *n0));
        mesh.append(Vert_Normal(v1, *n1));
        mesh.append(Vert_Normal(v2, *n2));
    }

    return mesh;
}

Geometry::Mesh Geometry::makeMesh(const Vertices& vs, const Vertices& ns)
{
    return makeMesh(vs, &ns);
}

//------------------------------------------------------------------------------

GeometryHandle GeometryStore::getGeometry(GeometricID::Key key)
{
    auto it = m_geometries.find(key);
    if (m_geometries.end() != it) {
        if (auto g = it->second.lock())
            return g;
    }
    GeometryHandle g = GeometryHandle(new Geometry(key));
    m_geometries[key] = GeometryRef(g);
    return g;
}

void GeometryStore::geometryDeleted(Geometry const& g)
{
    emit deletingGeometry(&g);
    m_geometries.erase(g.m_key);
}

GeometryStore& geometryStore()
{
    static GeometryStore gs;
    return gs;
}

//------------------------------------------------------------------------------
} // namespace RealSpace