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 9ac4f20
Show file tree
Hide file tree
Showing 2 changed files with 41 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
35 changes: 33 additions & 2 deletions cplusplus/xraylib++.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/* Copyright (c) 2020, Tom Schoonjans
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Tom Schoonjans ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Tom Schoonjans BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef XRAYLIB_PLUSPLUS_H
#define XRAYLIB_PLUSPLUS_H
Expand All @@ -8,6 +19,7 @@
#include <new>
#include <complex>
#include <vector>
#include <cstring>

using _compoundDataPod = struct compoundData;
using _radioNuclideDataPod = struct radioNuclideData;
Expand Down Expand Up @@ -314,7 +326,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 +337,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 9ac4f20

Please sign in to comment.