Skip to content

Commit

Permalink
C++: add Crystal::Struct constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoonj committed Dec 27, 2020
1 parent eeaa2c7 commit 6c874f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cplusplus/tests/test-crystal_diffraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ int main(int argc, char *argv[]) {

auto cs = xrlpp::Crystal::GetCrystal("Diamond");
auto cs_copy(cs);
auto *cs_copy2 = new xrlpp::Crystal::Struct(cs_copy);
delete cs_copy2;

try {
xrlpp::Crystal::AddCrystal(cs);
Expand All @@ -54,6 +56,12 @@ int main(int argc, char *argv[]) {
catch (std::invalid_argument &e) {
}

auto *cs_new = new xrlpp::Crystal::Struct("Diamond Copy", cs.a, cs.b, cs.c, cs.alpha, cs.beta, cs.gamma, cs.volume, cs.atom);
xrlpp::Crystal::AddCrystal(*cs_new);
delete cs_new;
crystals_list = xrlpp::Crystal::GetCrystalsList();
assert(crystals_list.size() == 39);

double angle = cs.Bragg_angle(10.0, 1, 1, 1);
assert(fabs(angle - 0.3057795845795849) < 1E-6);

Expand Down
23 changes: 21 additions & 2 deletions cplusplus/xraylib++.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ namespace xrlpp {
}

// constructor -> this needs to generate the underlying cs pointer!
/*Struct(const std::string &name, double a, double b, double c, double alpha, double beta, double gamma, double volume, const std::vector<Atom> &atoms) :
Struct(const std::string &name, double a, double b, double c, double alpha, double beta, double gamma, double volume, const std::vector<Atom> &atoms) :
name(name),
a(a),
b(b),
Expand All @@ -325,7 +325,26 @@ namespace xrlpp {
volume(volume),
n_atom(atoms.size()),
atom(atoms)
{}*/
{
cs = (Crystal_Struct *) malloc(sizeof(Crystal_Struct));
cs->name = strdup(name.c_str());
cs->a = a;
cs->b = b;
cs->c = c;
cs->alpha = alpha;
cs->beta = beta;
cs->gamma = gamma;
cs->volume = volume;
cs->n_atom = n_atom;
cs->atom = (Crystal_Atom *) malloc(sizeof(Crystal_Atom) * n_atom);
for (int i = 0 ; i < n_atom ; i++) {
cs->atom[i].Zatom = atoms[i].Zatom;
cs->atom[i].fraction = atoms[i].fraction;
cs->atom[i].x = atoms[i].x;
cs->atom[i].y = atoms[i].y;
cs->atom[i].z = atoms[i].z;
}
}

// copy constructor
Struct(const Struct &_struct) :
Expand Down

0 comments on commit 6c874f4

Please sign in to comment.