File: CoreAndShellItem.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 (147 lines) | stat: -rw-r--r-- 4,796 bytes parent folder | download | duplicates (2)
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Sample/CoreAndShellItem.cpp
//! @brief     Implements class CoreAndShellItem.
//!
//! @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/Model/Sample/CoreAndShellItem.h"
#include "Base/Util/Assert.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Sample/ParticleItem.h"
#include "Sample/Particle/CoreAndShell.h"
#include "Sample/Particle/Particle.h"
#include "Sample/Scattering/Rotations.h"

namespace {
namespace Tag {

const QString Core("Core");
const QString Shell("Shell");
const QString BaseData("BaseData");
const QString ExpandMainGroupbox("ExpandMainGroupbox");
const QString ExpandCoreGroupbox("ExpandCoreGroupbox");
const QString ExpandShellGroupbox("ExpandShellGroupbox");

} // namespace Tag

const QString abundance_tooltip = "Proportion of this type of particles normalized to the \n"
                                  "total number of particles in the layout";

const QString position_tooltip = "Relative position of the particle's reference point \n"
                                 "in the coordinate system of the parent (nm)";

} // namespace

CoreAndShellItem::CoreAndShellItem(const MaterialsSet* materials)
    : ItemWithParticles(abundance_tooltip, position_tooltip)
    , m_materials(materials)
{
}

void CoreAndShellItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeBaseElement<ItemWithParticles>(w, XML::Tag::BaseData, this);

    // core
    if (m_core) {
        XML::writeTaggedElement(w, Tag::Core, *m_core);
    }

    // shell
    if (m_shell) {
        XML::writeTaggedElement(w, Tag::Shell, *m_shell);
    }
    XML::writeTaggedValue(w, Tag::ExpandMainGroupbox, expandMain);
    XML::writeTaggedValue(w, Tag::ExpandCoreGroupbox, expandCore);
    XML::writeTaggedValue(w, Tag::ExpandShellGroupbox, expandShell);
}

void CoreAndShellItem::readFrom(QXmlStreamReader* r)
{
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();
        if (tag == Tag::BaseData)
            XML::readBaseElement<ItemWithParticles>(r, tag, this);
        else if (tag == Tag::Core) {
            createCoreItem(m_materials)->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);
        } else if (tag == Tag::Shell) {
            createShellItem(m_materials)->readFrom(r);
            XML::gotoEndElementOfTag(r, tag);
        } else if (tag == Tag::ExpandMainGroupbox)
            expandMain = XML::readTaggedBool(r, tag);
        else if (tag == Tag::ExpandCoreGroupbox)
            expandCore = XML::readTaggedBool(r, tag);
        else if (tag == Tag::ExpandShellGroupbox)
            expandShell = XML::readTaggedBool(r, tag);
        else
            r->skipCurrentElement();
    }
}

std::unique_ptr<CoreAndShell> CoreAndShellItem::createCoreAndShell() const
{
    ASSERT(m_core);
    std::unique_ptr<Particle> core = m_core->createParticle();
    ASSERT(core);

    ASSERT(m_shell);
    std::unique_ptr<Particle> shell = m_shell->createParticle();
    ASSERT(shell);

    auto coreshell = std::make_unique<CoreAndShell>(*core, *shell);
    coreshell->setAbundance(abundance().dVal());
    if (auto r = createRotation(); r && !r->isIdentity())
        coreshell->rotate(*r);
    coreshell->translate(position());

    return coreshell;
}

ParticleItem* CoreAndShellItem::coreItem() const
{
    return m_core.get();
}

ParticleItem* CoreAndShellItem::createCoreItem(const MaterialsSet* materials)
{
    m_core = std::make_unique<ParticleItem>(materials);
    m_core->setMaterial(materials->defaultCoreMaterialItem());
    return m_core.get();
}

ParticleItem* CoreAndShellItem::shellItem() const
{
    return m_shell.get();
}

ParticleItem* CoreAndShellItem::createShellItem(const MaterialsSet* materials)
{
    m_shell = std::make_unique<ParticleItem>(materials);
    m_shell->setMaterial(materials->defaultParticleMaterialItem());
    return m_shell.get();
}

std::vector<ItemWithParticles*> CoreAndShellItem::containedItemsWithParticles() const
{
    std::vector<ItemWithParticles*> result;
    if (coreItem()) {
        result.push_back(coreItem());
        Vec::concat(result, coreItem()->containedItemsWithParticles());
    }
    if (shellItem()) {
        result.push_back(shellItem());
        Vec::concat(result, shellItem()->containedItemsWithParticles());
    }

    return result;
}