From 4bc7ecf566cb1416a3ad174c62ff8a9377ced711 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Sun, 29 May 2022 18:24:04 +0200
Subject: [PATCH] as for 2D

---
 Device/Histo/Histogram1D.cpp          | 23 ++++-----
 Device/Histo/Histogram1D.h            |  2 +
 auto/Wrap/doxygenDevice.i             |  3 ++
 auto/Wrap/libBornAgainDevice.py       |  9 ++--
 auto/Wrap/libBornAgainDevice_wrap.cpp | 71 +++++++++++++++++++++++----
 5 files changed, 83 insertions(+), 25 deletions(-)

diff --git a/Device/Histo/Histogram1D.cpp b/Device/Histo/Histogram1D.cpp
index 0b2bc61eb4e..bee83747326 100644
--- a/Device/Histo/Histogram1D.cpp
+++ b/Device/Histo/Histogram1D.cpp
@@ -24,6 +24,11 @@ Histogram1D::Histogram1D(const Powerfield<double>& data)
     init_from_data(data);
 }
 
+Histogram1D::Histogram1D(Powerfield<CumulativeValue>* data)
+    : IHistogram(data)
+{
+}
+
 Histogram1D* Histogram1D::clone() const
 {
     return new Histogram1D(*this);
@@ -67,16 +72,12 @@ Histogram1D* Histogram1D::crop(double xmin, double xmax)
 {
     const std::unique_ptr<IAxis> xaxis{xAxis().clone()};
     xaxis->clip(xmin, xmax);
-    auto* result = new Histogram1D(*xaxis);
-    Powerfield<CumulativeValue>::const_iterator it_origin = m_data->begin();
-    Powerfield<CumulativeValue>::iterator it_result = result->m_data->begin();
-    while (it_origin != m_data->end()) {
-        const double x = m_data->projectedCoord(it_origin.getIndex(), 0);
-        if (result->xAxis().contains(x)) {
-            *it_result = *it_origin;
-            ++it_result;
-        }
-        ++it_origin;
+    auto out = new Powerfield<CumulativeValue>(*xaxis);
+    size_t iout = 0;
+    for (size_t i = 0; i < m_data->allocatedSize(); ++i) {
+        const double x = m_data->projectedCoord(i, 0);
+        if (xaxis->contains(x))
+            (*out)[iout++] = (*m_data)[i];
     }
-    return result;
+    return new Histogram1D(out);
 }
diff --git a/Device/Histo/Histogram1D.h b/Device/Histo/Histogram1D.h
index abd6de04858..8013ed284b5 100644
--- a/Device/Histo/Histogram1D.h
+++ b/Device/Histo/Histogram1D.h
@@ -25,6 +25,8 @@ public:
     //! Constructor for 1D histograms from basic Powerfield object
     Histogram1D(const Powerfield<double>& data);
 
+    Histogram1D(Powerfield<CumulativeValue>* data);
+
     //! Returns clone of histogram
     Histogram1D* clone() const override;
 
diff --git a/auto/Wrap/doxygenDevice.i b/auto/Wrap/doxygenDevice.i
index 4d16a0b80b4..d52013701dc 100644
--- a/auto/Wrap/doxygenDevice.i
+++ b/auto/Wrap/doxygenDevice.i
@@ -571,6 +571,9 @@ C++ includes: Histogram1D.h
 Constructor for 1D histograms from basic  Powerfield object. 
 ";
 
+%feature("docstring")  Histogram1D::Histogram1D "Histogram1D::Histogram1D(Powerfield< CumulativeValue > *data)
+";
+
 %feature("docstring")  Histogram1D::clone "Histogram1D * Histogram1D::clone() const override
 
 Returns clone of histogram. 
diff --git a/auto/Wrap/libBornAgainDevice.py b/auto/Wrap/libBornAgainDevice.py
index 2c53fdf5291..087fcb27b10 100644
--- a/auto/Wrap/libBornAgainDevice.py
+++ b/auto/Wrap/libBornAgainDevice.py
@@ -4631,15 +4631,14 @@ class Histogram1D(IHistogram):
     thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
     __repr__ = _swig_repr
 
-    def __init__(self, data):
+    def __init__(self, *args):
         r"""
         __init__(Histogram1D self, IntensityData data) -> Histogram1D
-        Histogram1D::Histogram1D(const Powerfield< double > &data)
-
-        Constructor for 1D histograms from basic  Powerfield object. 
+        __init__(Histogram1D self, Powerfield< CumulativeValue > * data) -> Histogram1D
+        Histogram1D::Histogram1D(Powerfield< CumulativeValue > *data)
 
         """
-        _libBornAgainDevice.Histogram1D_swiginit(self, _libBornAgainDevice.new_Histogram1D(data))
+        _libBornAgainDevice.Histogram1D_swiginit(self, _libBornAgainDevice.new_Histogram1D(*args))
 
     def clone(self):
         r"""
diff --git a/auto/Wrap/libBornAgainDevice_wrap.cpp b/auto/Wrap/libBornAgainDevice_wrap.cpp
index 4fd98781d6e..f4faa11b15b 100644
--- a/auto/Wrap/libBornAgainDevice_wrap.cpp
+++ b/auto/Wrap/libBornAgainDevice_wrap.cpp
@@ -37558,16 +37558,14 @@ SWIGINTERN PyObject *IHistogram_swigregister(PyObject *SWIGUNUSEDPARM(self), PyO
   return SWIG_Py_Void();
 }
 
-SWIGINTERN PyObject *_wrap_new_Histogram1D(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
+SWIGINTERN PyObject *_wrap_new_Histogram1D__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
   PyObject *resultobj = 0;
   Powerfield< double > *arg1 = 0 ;
   void *argp1 = 0 ;
   int res1 = 0 ;
-  PyObject *swig_obj[1] ;
   Histogram1D *result = 0 ;
   
-  if (!args) SWIG_fail;
-  swig_obj[0] = args;
+  if ((nobjs < 1) || (nobjs > 1)) SWIG_fail;
   res1 = SWIG_ConvertPtr(swig_obj[0], &argp1, SWIGTYPE_p_PowerfieldT_double_t,  0  | 0);
   if (!SWIG_IsOK(res1)) {
     SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Histogram1D" "', argument " "1"" of type '" "Powerfield< double > const &""'"); 
@@ -37584,6 +37582,62 @@ fail:
 }
 
 
+SWIGINTERN PyObject *_wrap_new_Histogram1D__SWIG_1(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) {
+  PyObject *resultobj = 0;
+  Powerfield< CumulativeValue > *arg1 = (Powerfield< CumulativeValue > *) 0 ;
+  void *argp1 = 0 ;
+  int res1 = 0 ;
+  Histogram1D *result = 0 ;
+  
+  if ((nobjs < 1) || (nobjs > 1)) SWIG_fail;
+  res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_PowerfieldT_CumulativeValue_t, 0 |  0 );
+  if (!SWIG_IsOK(res1)) {
+    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Histogram1D" "', argument " "1"" of type '" "Powerfield< CumulativeValue > *""'"); 
+  }
+  arg1 = reinterpret_cast< Powerfield< CumulativeValue > * >(argp1);
+  result = (Histogram1D *)new Histogram1D(arg1);
+  resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Histogram1D, SWIG_POINTER_NEW |  0 );
+  return resultobj;
+fail:
+  return NULL;
+}
+
+
+SWIGINTERN PyObject *_wrap_new_Histogram1D(PyObject *self, PyObject *args) {
+  Py_ssize_t argc;
+  PyObject *argv[2] = {
+    0
+  };
+  
+  if (!(argc = SWIG_Python_UnpackTuple(args, "new_Histogram1D", 0, 1, argv))) SWIG_fail;
+  --argc;
+  if (argc == 1) {
+    int _v;
+    int res = SWIG_ConvertPtr(argv[0], 0, SWIGTYPE_p_PowerfieldT_double_t, SWIG_POINTER_NO_NULL | 0);
+    _v = SWIG_CheckState(res);
+    if (_v) {
+      return _wrap_new_Histogram1D__SWIG_0(self, argc, argv);
+    }
+  }
+  if (argc == 1) {
+    int _v;
+    void *vptr = 0;
+    int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_PowerfieldT_CumulativeValue_t, 0);
+    _v = SWIG_CheckState(res);
+    if (_v) {
+      return _wrap_new_Histogram1D__SWIG_1(self, argc, argv);
+    }
+  }
+  
+fail:
+  SWIG_Python_RaiseOrModifyTypeError("Wrong number or type of arguments for overloaded function 'new_Histogram1D'.\n"
+    "  Possible C/C++ prototypes are:\n"
+    "    Histogram1D::Histogram1D(Powerfield< double > const &)\n"
+    "    Histogram1D::Histogram1D(Powerfield< CumulativeValue > *)\n");
+  return 0;
+}
+
+
 SWIGINTERN PyObject *_wrap_Histogram1D_clone(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
   PyObject *resultobj = 0;
   Histogram1D *arg1 = (Histogram1D *) 0 ;
@@ -42078,11 +42132,10 @@ static PyMethodDef SwigMethods[] = {
 		"\n"
 		""},
 	 { "IHistogram_swigregister", IHistogram_swigregister, METH_O, NULL},
-	 { "new_Histogram1D", _wrap_new_Histogram1D, METH_O, "\n"
-		"new_Histogram1D(IntensityData data) -> Histogram1D\n"
-		"Histogram1D::Histogram1D(const Powerfield< double > &data)\n"
-		"\n"
-		"Constructor for 1D histograms from basic  Powerfield object. \n"
+	 { "new_Histogram1D", _wrap_new_Histogram1D, METH_VARARGS, "\n"
+		"Histogram1D(IntensityData data)\n"
+		"new_Histogram1D(Powerfield< CumulativeValue > * data) -> Histogram1D\n"
+		"Histogram1D::Histogram1D(Powerfield< CumulativeValue > *data)\n"
 		"\n"
 		""},
 	 { "Histogram1D_clone", _wrap_Histogram1D_clone, METH_O, "\n"
-- 
GitLab