Xcos blocks: make scifunc_block_m work 28/16428/5
Paul Bignier [Thu, 30 Apr 2015 09:59:48 +0000 (11:59 +0200)]
 * "Inverted pendulum" demo contains that block

Change-Id: Ifea2c72b6de1a1068a41c387ae5a262aea677e3b

scilab/modules/scicos/macros/scicos_scicos/genmac.sci
scilab/modules/scicos/src/cpp/sciblk2.cpp

index d838c2a..dcaf84a 100644 (file)
@@ -86,5 +86,5 @@ function mac=genmac(tt,nin,nout)
     set_y
     "end";]
 
-    deff("[y,x,z,t_evo,xd]=mac(%_flag,n_evi,t,x,z,rpar,ipar,u)",mac_txt)
+    deff("[xd,t_evo,z,x,y]=mac(%_flag,n_evi,t,x,z,rpar,ipar,u)",mac_txt)
 endfunction
index 55e842e..f454628 100644 (file)
@@ -101,7 +101,7 @@ void sciblk2(int* flag, int* nevprt, double* t, double xd[], double x[], int* nx
 
     // Treating 'ipar' differently because it is an int tab, unlike the other double ones
     types::Double* Ipar = new types::Double(*nipar, 1);
-    std::transform(ipar, ipar + *nipar, Ipar, toDouble);
+    std::transform(ipar, ipar + *nipar, Ipar->get(), toDouble);
     in[6] = Ipar;
 
     types::List* Nin = new types::List();
@@ -171,35 +171,44 @@ void sciblk2(int* flag, int* nevprt, double* t, double xd[], double x[], int* nx
             types::Double* Xout = out[3]->getAs<types::Double>();
             memcpy(x, Xout->get(), *nx * sizeof(double));
 
-            if (*nout != 0)
+            if (*flag == 1 || *flag == 6)
             {
-                if (!out[4]->isList())
+                if (*nout != 0)
                 {
-                    setErrAndFree(-1, out);
-                    return;
-                }
-                types::List* Yout = out[4]->getAs<types::List>();
-                if (Yout->getSize() < *nout)
-                {
-                    // Consider that 'outptr' has not been defined in the macro: do not update the current 'outptr'
-                    break;
-                }
-                for (int k = *nout - 1; k >= 0; --k)
-                {
-                    if (!Yout->get(k)->isDouble())
+                    if (!out[4]->isList())
                     {
                         setErrAndFree(-1, out);
                         return;
                     }
-                    types::Double* KthElement = Yout->get(k)->getAs<types::Double>();
-                    double* y = (double*)outptr[k];
-                    int ny = outsz[k];
-                    int ny2 = 1;
-                    if (*flag == 1)
+                    types::List* Yout = out[4]->getAs<types::List>();
+                    if (Yout->getSize() < *nout)
+                    {
+                        // Consider that 'outptr' has not been defined in the macro: do not update the current 'outptr'
+                        break;
+                    }
+                    for (int k = *nout - 1; k >= 0; --k)
                     {
-                        ny2 = outsz[*nout + k];
+                        if (!Yout->get(k)->isDouble())
+                        {
+                            setErrAndFree(-1, out);
+                            return;
+                        }
+                        types::Double* KthElement = Yout->get(k)->getAs<types::Double>();
+                        double* y = (double*)outptr[k];
+                        int ny = outsz[k];
+                        int ny2 = 1;
+                        if (*flag == 1)
+                        {
+                            ny2 = outsz[*nout + k];
+                        }
+                        if (KthElement->getSize() != ny * ny2)
+                        {
+                            // At initialization (flag 6), the 'y' returned by the macro is not necessarily properly initialized.
+                            // In this case, do nothing to avoid copying corrupt data
+                            break;
+                        }
+                        memcpy(y, KthElement->get(), ny * ny2 * sizeof(double));
                     }
-                    memcpy(y, KthElement->get(), ny * ny2 * sizeof(double));
                 }
             }
             break;