From 9ac4f202f6527a11a12186ddad7a0f277f2e493f Mon Sep 17 00:00:00 2001 From: Tom Schoonjans Date: Sun, 27 Dec 2020 11:08:46 +0000 Subject: [PATCH] C++: add Crystal::Struct constructor --- cplusplus/tests/test-crystal_diffraction.cpp | 8 +++++ cplusplus/xraylib++.h | 35 ++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cplusplus/tests/test-crystal_diffraction.cpp b/cplusplus/tests/test-crystal_diffraction.cpp index 3faa2d03..48a29a9e 100644 --- a/cplusplus/tests/test-crystal_diffraction.cpp +++ b/cplusplus/tests/test-crystal_diffraction.cpp @@ -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); @@ -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); diff --git a/cplusplus/xraylib++.h b/cplusplus/xraylib++.h index 2a253b3b..4f44ead3 100644 --- a/cplusplus/xraylib++.h +++ b/cplusplus/xraylib++.h @@ -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 @@ -8,6 +19,7 @@ #include #include #include +#include using _compoundDataPod = struct compoundData; using _radioNuclideDataPod = struct radioNuclideData; @@ -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 &atoms) : + Struct(const std::string &name, double a, double b, double c, double alpha, double beta, double gamma, double volume, const std::vector &atoms) : name(name), a(a), b(b), @@ -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) :