BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ParticleComposition.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Particle/ParticleComposition.cpp
6 //! @brief Implements class ParticleComposition.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
18 
20 
22 
24 {
25  auto* result = new ParticleComposition();
26  result->setAbundance(m_abundance);
27  for (size_t index = 0; index < m_particles.size(); ++index)
28  result->addParticle(*m_particles[index]);
29  if (m_rotation)
30  result->setRotation(*m_rotation);
31  result->setParticlePosition(m_position);
32  return result;
33 }
34 
35 std::vector<const INode*> ParticleComposition::nodeChildren() const
36 {
37  std::vector<const INode*> result = IParticle::nodeChildren();
38  for (const IParticle* p : m_particles)
39  result.emplace_back(p);
40  return result;
41 }
42 
43 #ifndef SWIG
44 std::vector<std::unique_ptr<IParticle>> ParticleComposition::decompose() const
45 {
46  std::vector<std::unique_ptr<IParticle>> result;
47  const auto* rot = rotation();
48  const auto& translation = particlePosition();
49  for (const auto& particle : m_particles) {
50  const auto sublist = particle->decompose();
51  for (const auto& subparticle : sublist) {
52  IParticle* new_subparticle = subparticle->clone();
53  if (rot)
54  new_subparticle->rotate(*rot);
55  new_subparticle->translate(translation);
56  result.emplace_back(new_subparticle);
57  }
58  }
59  return result;
60 }
61 #endif
62 
64 {
65  m_particles.emplace_back(particle.clone());
66 }
67 
68 void ParticleComposition::addParticle(const IParticle& particle, R3 position)
69 {
70  m_particles.emplace_back(particle.clone()->translate(position));
71 }
72 
73 // Please note, that positions is not const reference here. This is intentional, to
74 // enable python lists to std::vector conversion
75 void ParticleComposition::addParticles(const IParticle& particle, std::vector<R3> positions)
76 {
77  for (size_t i = 0; i < positions.size(); ++i)
78  addParticle(particle, positions[i]);
79 }
80 
81 std::vector<const IParticle*> ParticleComposition::particles() const
82 {
83  std::vector<const IParticle*> result;
84  for (const IParticle* p : m_particles)
85  result.emplace_back(p);
86  return result;
87 }
Defines the macro ASSERT.
Defines class ParticleComposition.
Defines IRotation classes.
Abstract base class for Particle, ParticleComposition, ParticleCoreShell, MesoCrystal....
Definition: IParticle.h:31
IParticle * translate(R3 translation)
Translates the particle, and returns this.
Definition: IParticle.cpp:25
R3 m_position
Definition: IParticle.h:79
const IRotation * rotation() const
Returns rotation object.
Definition: IParticle.cpp:31
IParticle * rotate(const IRotation &rotation)
Rotates the particle, and returns this.
Definition: IParticle.cpp:41
double m_abundance
Definition: IParticle.h:78
std::vector< const INode * > nodeChildren() const override
Returns all children.
Definition: IParticle.cpp:20
IParticle * clone() const override=0
Returns a clone of this ISampleNode object.
std::unique_ptr< IRotation > m_rotation
Definition: IParticle.h:80
R3 particlePosition() const
Returns particle position.
Definition: IParticle.h:46
void emplace_back(T *e)
Definition: OwningVector.h:62
size_t size() const
Definition: OwningVector.h:70
A composition of particles at fixed positions.
void addParticle(const IParticle &particle)
void addParticles(const IParticle &particle, std::vector< R3 > positions)
std::vector< std::unique_ptr< IParticle > > decompose() const override
Decompose in constituent IParticle objects.
std::vector< const INode * > nodeChildren() const override
Returns all children.
std::vector< const IParticle * > particles() const
OwningVector< IParticle > m_particles
~ParticleComposition() override
ParticleComposition * clone() const override
Returns a clone of this ISampleNode object.