Scicos blocks: use the new MVC graphic for scopes
[scilab.git] / scilab / modules / scicos_blocks / src / c / cscope.c
index 1eba2b8..05551fc 100644 (file)
-/*  Scicos
+/*
+ *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ *  Copyright (C) 2011 - Scilab Enterprises - ClĂ©ment DAVID
  *
- *  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
+ *  This file must be used under the terms of the CeCILL.
+ *  This source file is licensed as described in the file COPYING, which
+ *  you should have received as part of this distribution.  The terms
+ *  are also available at
+ *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ */
+
+#include "dynlib_scicos_blocks.h"
+#include "scoUtils.h"
+
+#include "MALLOC.h"
+#include "elementary_functions.h"
+
+#include "getGraphicObjectProperty.h"
+#include "setGraphicObjectProperty.h"
+#include "graphicObjectProperties.h"
+#include "createGraphicObject.h"
+
+#include "CurrentFigure.h"
+
+#include "scicos_block4.h"
+#include "scicos.h"
+
+#include "localization.h"
+
+#include "FigureList.h"
+#include "BuildObjects.h"
+#include "AxesModel.h"
+
+/*****************************************************************************
+ * Internal container structure
+ ****************************************************************************/
+
+/**
+ * Container structure
+ */
+typedef struct
+{
+    struct
+    {
+        int numberOfPoints;
+        int maxNumberOfPoints;
+        double *time;
+        double ***data;
+    } internal;
+
+    struct
+    {
+        int periodCounter;
+
+        char *cachedFigureUID;
+        char *cachedAxeUID;
+        char **cachedPolylinesUIDs;
+    } scope;
+} sco_data;
+
+/**
+ * Get (and allocate on demand) the internal data used on this scope
+ * \param block the block
+ * \return the scope data
+ */
+static sco_data *getScoData(scicos_block * block);
+
+/**
+ * Release any internal data
+ *
+ * \param block the block
+ */
+static void freeScoData(scicos_block * block);
+
+/**
+ * Append the data to the current data
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * \param block the block
+ * \param input the input (0-indexed)
+ * \param t the current time
+ * \param data the data to append
+ */
+static void appendData(scicos_block * block, int input, double t, double *data);
+
+/**
+ * Push the block data to the polyline
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * \param block the block
+ * \param input the selected input
+ * \param row the selected row
+ * \param pPolylineUID the polyline uid
  *
- * See the file ./license.txt
  */
-/*--------------------------------------------------------------------------*/
+static BOOL pushData(scicos_block * block, int input, int row);
+
+/*****************************************************************************
+ * Graphics utils
+ ****************************************************************************/
+
 /**
-   \file cscope.c
-   \author Benoit Bayol
-   \version 1.0
-   \date September 2006 - January 2007
-   \brief CSCOPE is a typical scope which links its input to the simulation time but there is only one input instead of CMSCOPE
-   \see CSCOPE.sci in macros/scicos_blocks/Sinks/
-*/
-/*--------------------------------------------------------------------------*/
-#include <stdio.h>
-#include "scicos.h"
-#include "scoMemoryScope.h"
-#include "scoWindowScope.h"
-#include "scoMisc.h"
-#include "scoGetProperty.h"
-#include "scoSetProperty.h"
-#include "scicos_block4.h"
-#include "SetJavaProperty.h"
-#include "scicos_malloc.h"
-#include "scicos_free.h"
-#include "MALLOC.h"
-#include "dynlib_scicos_blocks.h"
-/*--------------------------------------------------------------------------*/
-/** \fn cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
-    \brief Function to draw or redraw the window
+ * Get (and allocate on demand) the figure associated with the block
+ * \param block the block
+ * \return a valid figure UID or NULL on error
+ */
+static char *getFigure(scicos_block * block);
+
+/**
+ * Get (and allocate on demand) the axe associated with the input
+ *
+ * \param pFigureUID the parent figure UID
+ * \param block the block
+ * \param input the current input index (0-indexed)
+ * \return a valid axe UID or NULL on error
+ */
+static char *getAxe(char *pFigureUID, scicos_block * block, int input);
+
+/**
+ * Get (and allocate on demand) the polyline associated with the row
+ *
+ * \param pAxeUID the parent axe UID
+ * \param block the block
+ * \param row the current row index (0-indexed)
+ * \return a valid polyline UID or NULL on error
+ */
+static char *getPolyline(char *pAxeUID, scicos_block * block, int row);
+
+/**
+ * Set the polylines buffer size
+ *
+ * \param block the block
+ * \param input the input port index
+ * \param maxNumberOfPoints the size of the buffer
+ */
+static BOOL setPolylinesBuffers(scicos_block * block, int input, int maxNumberOfPoints);
+
+/**
+ * Set the polylines bounds
+ *
+ * \param block the block
+ * \param input the input port index
+ * \param periodCounter number of past periods since startup
+ */
+static BOOL setPolylinesBounds(scicos_block * block, int input, int periodCounter);
+
+/*****************************************************************************
+ * Simulation function
+ ****************************************************************************/
+
+/** \fn void cscope(scicos_block * block,int flag)
+    \brief the computational function
+    \param block A pointer to a scicos_block
+    \param flag An int which indicates the state of the block (init, update, ending)
 */
-SCICOS_BLOCKS_IMPEXP void cscope_draw(scicos_block * block, ScopeMemory ** pScopeMemory, int firstdraw)
+SCICOS_BLOCKS_IMPEXP void cscope(scicos_block * block, scicos_flag flag)
 {
-    int i = 0;
-    double *rpar = NULL;
-    int *ipar = NULL, nipar = 0;
-    double period = 0.;
-    int dimension = 0;
-    double ymin = 0., ymax = 0., xmin = 0., xmax = 0.;
-    int buffer_size = 0;
-    int win_pos[2];
-    int win_dim[2];
-    int win = 0;
-    int number_of_subwin = 0;
-    int number_of_curves_by_subwin[1];
-    int * colors = NULL;
-    char *label = NULL;
+    char *pFigureUID;
 
-    /*Retrieving Parameters*/
-    rpar = GetRparPtrs(block);
-    ipar = GetIparPtrs(block);
-    nipar = GetNipar(block);
+    double t;
+    double *u;
+    sco_data *sco;
 
-    buffer_size = ipar[2]; //** this is the "standard" buffer size
+    int i;
+    BOOL result;
 
-    win = ipar[0];
-    period  = rpar[3];
-    win_pos[0] = ipar[(nipar-1) - 3];
-    win_pos[1] = ipar[(nipar-1) - 2];
-    win_dim[0] = ipar[(nipar-1) - 1];
-    win_dim[1] = ipar[nipar-1];
-    dimension = 2;
-    number_of_curves_by_subwin[0] = GetInPortRows(block,1);
+    switch (flag)
+    {
 
-    number_of_subwin = 1;
-    ymin = rpar[1];
-    ymax = rpar[2];
-    label = GetLabelPtrs(block);
+    case Initialization:
+        sco = getScoData(block);
+        if (sco == NULL)
+        {
+            set_block_error(-5);
+        }
+        pFigureUID = getFigure(block);
+        if (pFigureUID == NULL)
+        {
+            // allocation error
+            set_block_error(-5);
+        }
+        break;
 
-    colors = (int*)scicos_malloc(number_of_curves_by_subwin[0]*sizeof(int));
-    for(i = 0 ; i < number_of_curves_by_subwin[0] ; i++)
-    {
-        colors[i] = ipar[3+i];
+    case StateUpdate:
+        pFigureUID = getFigure(block);
+
+        t = get_scicos_time();
+        u = GetRealInPortPtrs(block, 1);
+
+        appendData(block, 0, t, u);
+
+        for (i = 0; i < block->insz[0]; i++)
+        {
+            result = pushData(block, 0, i);
+            if (result == FALSE)
+            {
+                Coserror("%s: unable to push some data.", "cscope");
+                break;
+            }
+        }
+        break;
+
+    case Ending:
+        freeScoData(block);
+        break;
+
+    default:
+        break;
     }
+}
+
+/*-------------------------------------------------------------------------*/
 
-    /*Allocating memory*/
-    if(firstdraw == 1)
+/*****************************************************************************
+ *
+ * Container management
+ *
+ ****************************************************************************/
+
+static sco_data *getScoData(scicos_block * block)
+{
+    sco_data *sco = (sco_data *) * (block->work);
+    int i, j, k, l;
+    BOOL result;
+
+    if (sco == NULL)
     {
-        scoInitScopeMemory(block->work,pScopeMemory, number_of_subwin, number_of_curves_by_subwin);
-        /*Must be placed before adding polyline or other elements*/
-        scoSetLongDrawSize(*pScopeMemory, 0, 50);
+        /*
+         * Data allocation
+         */
+
+        sco = (sco_data *) MALLOC(sizeof(sco_data));
+        if (sco == NULL)
+            goto error_handler_sco;
+
+        sco->internal.numberOfPoints = 0;
+        sco->internal.maxNumberOfPoints = block->ipar[2];
+
+        sco->internal.data = (double ***)CALLOC(block->nin, sizeof(double **));
+        if (sco->internal.data == NULL)
+            goto error_handler_data;
 
-        scoSetShortDrawSize(*pScopeMemory,0,buffer_size); //** set the short draw size
+        for (i = 0; i < block->nin; i++)
+        {
+            sco->internal.data[i] = (double **)CALLOC(block->insz[i], sizeof(double *));
+            if (sco->internal.data[i] == NULL)
+                goto error_handler_data_i;
+        }
+        for (i = 0; i < block->nin; i++)
+        {
+            for (j = 0; j < block->insz[i]; j++)
+            {
+                sco->internal.data[i][j] = (double *)CALLOC(block->ipar[2], sizeof(double));
+
+                if (sco->internal.data[i][j] == NULL)
+                    goto error_handler_data_ij;
+            }
+        }
+
+        sco->internal.time = (double *)CALLOC(block->ipar[2], sizeof(double));
+        if (sco->internal.time == NULL)
+        {
+            goto error_handler_time;
+        }
 
-        scoSetPeriod(*pScopeMemory,0,period);
+        sco->scope.periodCounter = 0;
+        sco->scope.cachedFigureUID = NULL;
+        sco->scope.cachedAxeUID = NULL;
+        sco->scope.cachedPolylinesUIDs = (char **)CALLOC(block->insz[0], sizeof(char *));
+
+        *(block->work) = sco;
     }
 
-    xmin = period*scoGetPeriodCounter(*pScopeMemory,0);
-    xmax = period*(scoGetPeriodCounter(*pScopeMemory,0)+1);
+    return sco;
+
+    /*
+     * Error management (out of normal flow)
+     */
 
-    /*Creating the Scope*/
-    scoInitOfWindow(*pScopeMemory, dimension, win, win_pos, win_dim, &xmin, &xmax, &ymin, &ymax, NULL, NULL);
-    if(scoGetScopeActivation(*pScopeMemory) == 1)
+error_handler_time:
+error_handler_data_ij:
+    for (k = 0; k < i; k++)
+    {
+        for (l = 0; l < j; l++)
+        {
+            FREE(sco->internal.data[k][l]);
+        }
+    }
+    i = block->nin - 1;
+error_handler_data_i:
+    for (j = 0; j < i; j++)
     {
-        scoAddTitlesScope(*pScopeMemory,label,"t","y",NULL);
-        /*Add a couple of polyline : one for the shortdraw and one for the longdraw*/
-        scoAddCoupleOfPolylines(*pScopeMemory,colors);
-        /* scoAddPolylineLineStyle(*pScopeMemory,colors); */
+        FREE(sco->internal.data[i]);
     }
-    scicos_free(colors);
+    FREE(sco->internal.data);
+error_handler_data:
+    FREE(sco);
+error_handler_sco:
+    // allocation error
+    set_block_error(-5);
+    return NULL;
+}
+
+static void freeScoData(scicos_block * block)
+{
+    sco_data *sco = (sco_data *) * (block->work);
+    int i, j;
 
-    /* use only single buffering to be sure to draw on the screen */
-    if (scoGetPointerScopeWindow(*pScopeMemory) != NULL)
+    if (sco != NULL)
     {
-        sciSetJavaUseSingleBuffer(scoGetPointerScopeWindow(*pScopeMemory), TRUE);
+        for (i = 0; i < block->nin; i++)
+        {
+            for (j = 0; j < block->insz[i]; j++)
+            {
+                FREE(sco->internal.data[i][j]);
+            }
+            FREE(sco->internal.data[i]);
+        }
+
+        FREE(sco->internal.data);
+        FREE(sco->internal.time);
+
+//      Commented due to the C++ allocation
+//      see http://bugzilla.scilab.org/show_bug.cgi?id=9747
+//      FREE(sco->scope.cachedFigureUID);
+//      sco->scope.cachedFigureUID = NULL;
+//      for (i=0; i<block->nin; i++) {
+//          for (j=0; j<block->insz[i]; j++) {
+//              FREE(sco->scope.cachedPolylinesUIDs[i][j]);
+//              sco->scope.cachedPolylinesUIDs[i][j] = NULL;
+//          }
+//          FREE(sco->scope.cachedAxeUID[i]);
+//          sco->scope.cachedAxeUID[i] = NULL;
+//      }
+
+        FREE(sco);
     }
+}
+
+static sco_data *reallocScoData(scicos_block * block, int numberOfPoints)
+{
+    sco_data *sco = (sco_data *) * (block->work);
+    int i, j;
+    double *ptr;
+    int setLen;
+
+    for (i = 0; i < block->nin; i++)
+    {
+        for (j = 0; j < block->insz[i]; j++)
+        {
+            ptr = (double *)REALLOC(sco->internal.data[i][j], numberOfPoints * sizeof(double));
+            if (ptr == NULL)
+                goto error_handler;
 
+            for (setLen = sco->internal.maxNumberOfPoints - numberOfPoints; setLen >= 0; setLen--)
+                ptr[sco->internal.maxNumberOfPoints + setLen] = ptr[sco->internal.maxNumberOfPoints - 1];
+            sco->internal.data[i][j] = ptr;
+        }
+    }
+
+    ptr = (double *)REALLOC(sco->internal.time, numberOfPoints * sizeof(double));
+    if (ptr == NULL)
+        goto error_handler;
+
+    for (setLen = sco->internal.maxNumberOfPoints - numberOfPoints; setLen >= 0; setLen--)
+        ptr[sco->internal.maxNumberOfPoints + setLen] = ptr[sco->internal.maxNumberOfPoints - 1];
+    sco->internal.time = ptr;
+
+    sco->internal.maxNumberOfPoints = numberOfPoints;
+    return sco;
+
+error_handler:
+    freeScoData(block);
+    // allocation error
+    set_block_error(-5);
+    return NULL;
 }
-/*--------------------------------------------------------------------------*/
-/** \fn void cscope(scicos_block * block,int flag)
-    \brief the computational function
-    \param block A pointer to a scicos_block
-    \param flag An int which indicates the state of the block (init, update, ending)
-*/
-SCICOS_BLOCKS_IMPEXP void cscope(scicos_block * block,int flag)
+
+static void appendData(scicos_block * block, int input, double t, double *data)
 {
-    ScopeMemory * pScopeMemory = NULL;
-    int i = 0;
-    double t = 0.;
-    int NbrPtsShort = 0;
-    double * u1 = NULL;
-    scoGraphicalObject pShortDraw;
+    int i;
+    static const int i__1 = 1;
 
-    double d_current_real_time = 0.;
+    sco_data *sco = (sco_data *) * (block->work);
+    int maxNumberOfPoints = sco->internal.maxNumberOfPoints;
+    int numberOfPoints = sco->internal.numberOfPoints;
 
-    switch(flag)
+    /*
+     * Handle the case where the t is greater than the data_bounds
+     */
+    if (t > ((sco->scope.periodCounter + 1) * block->rpar[3]))
     {
+        sco->scope.periodCounter++;
 
-    case Initialization:
+        numberOfPoints = 0;
+        sco->internal.numberOfPoints = 0;
+        if (setPolylinesBounds(block, input, sco->scope.periodCounter) == FALSE)
+        {
+            set_block_error(-5);
+            freeScoData(block);
+            sco = NULL;
+        }
+    }
+
+    /*
+     * Handle the case where the scope has more points than maxNumberOfPoints
+     */
+    if (sco != NULL && numberOfPoints >= maxNumberOfPoints)
     {
-        cscope_draw(block,&pScopeMemory,1);
+        // on a full scope, re-alloc
+        maxNumberOfPoints = maxNumberOfPoints + block->ipar[2];
+        sco = reallocScoData(block, maxNumberOfPoints);
+
+        // reconfigure related graphic objects
+        if (setPolylinesBuffers(block, input, maxNumberOfPoints) == FALSE)
+        {
+            set_block_error(-5);
+            freeScoData(block);
+            sco = NULL;
+        }
+    }
 
-        //** Init the real time section
-        //** current real time as double [second]
-        d_current_real_time = scoGetRealTime();
-        pScopeMemory->d_last_scope_update_time = d_current_real_time ; //** update the ds for the next step
-        //** printf("Init:Start! \n\n"); //** DEBUG ONLY !
+    /*
+     * Update data
+     */
+    if (sco != NULL)
+    {
+        int setLen;
 
-        break;
+        for (i = 0; i < block->insz[input]; i++)
+        {
+            for (setLen = maxNumberOfPoints - numberOfPoints; setLen >= 0; setLen--)
+                sco->internal.data[input][i][numberOfPoints + setLen] = data[i];
+        }
+
+        for (setLen = maxNumberOfPoints - numberOfPoints; setLen >= 0; setLen--)
+            sco->internal.time[numberOfPoints + setLen] = t;
+
+        sco->internal.numberOfPoints++;
     }
+}
 
-    case StateUpdate:
+static BOOL pushData(scicos_block * block, int input, int row)
+{
+    char *pFigureUID;
+    char *pAxeUID;
+    char *pPolylineUID;
+
+    static const int i__0 = 0;
+
+    double *data;
+    sco_data *sco;
+
+    BOOL result = TRUE;
+
+    pFigureUID = getFigure(block);
+    pAxeUID = getAxe(pFigureUID, block, input);
+    pPolylineUID = getPolyline(pAxeUID, block, row);
+
+    sco = getScoData(block);
+    if (sco == NULL)
+        return FALSE;
+
+    // select the right input and row
+    data = sco->internal.data[input][row];
+
+    result &= setGraphicObjectProperty(pPolylineUID, __GO_DATA_MODEL_X__, sco->internal.time, jni_double_vector, sco->internal.maxNumberOfPoints);
+    result &= setGraphicObjectProperty(pPolylineUID, __GO_DATA_MODEL_Y__, data, jni_double_vector, sco->internal.maxNumberOfPoints);
+
+    return result;
+}
+
+/*****************************************************************************
+ *
+ * Graphic utils
+ *
+ ****************************************************************************/
+
+/**
+ * Set properties on the figure.
+ *
+ * \param pFigureUID the figure uid
+ * \param block the current block
+ */
+static void setFigureSettings(char *pFigureUID, scicos_block * block)
+{
+    int nipar = GetNipar(block);
+    int *ipar = GetIparPtrs(block);
+
+    int win_pos[2];
+    int win_dim[2];
+
+    win_pos[0] = ipar[(nipar - 1) - 3];
+    win_pos[1] = ipar[(nipar - 1) - 2];
+    win_dim[0] = ipar[(nipar - 1) - 1];
+    win_dim[1] = ipar[nipar - 1];
+
+    setGraphicObjectProperty(pFigureUID, __GO_POSITION__, &win_pos, jni_int_vector, 2);
+    setGraphicObjectProperty(pFigureUID, __GO_SIZE__, &win_dim, jni_int_vector, 2);
+};
+
+/*****************************************************************************
+ *
+ * Graphic
+ *
+ ****************************************************************************/
+
+static char *getFigure(scicos_block * block)
+{
+    signed int figNum;
+    char *pFigureUID = NULL;
+    char *pAxe = NULL;
+    static const int i__1 = 1;
+    sco_data *sco = (sco_data *) * (block->work);
+
+    int i, j;
+
+    // fast path for an existing object
+    if (sco->scope.cachedFigureUID != NULL)
+    {
+        return sco->scope.cachedFigureUID;
+    }
+
+    figNum = block->ipar[0];
+
+    // with a negative id, use the block number indexed from a constant.
+    if (figNum < 0)
+    {
+        figNum = 20000 + get_block_number();
+    }
+
+    pFigureUID = getFigureFromIndex(figNum);
+    // create on demand
+    if (pFigureUID == NULL)
     {
-        scoRetrieveScopeMemory(block->work,&pScopeMemory);
-        if(scoGetScopeActivation(pScopeMemory) == 1)
+        pFigureUID = createNewFigureWithAxes();
+        setGraphicObjectProperty(pFigureUID, __GO_ID__, &figNum, jni_int, 1);
+
+        // set configured parameters
+        setFigureSettings(pFigureUID, block);
+
+        // allocate the axes through the getter
+        for (i = 0; i < GetNin(block); i++)
         {
-            t = get_scicos_time();
-            /*Retreiving Scope in the block->work*/
+            pAxe = getAxe(pFigureUID, block, i);
 
-            /*If window has been destroyed we recreate it*/
-            if (scoGetPointerScopeWindow(pScopeMemory) == NULL)
-            {
-                cscope_draw(block,&pScopeMemory,0);
-            }
+            /*
+             * Setup according to block settings
+             */
+            setLabel(pAxe, __GO_X_AXIS_LABEL__, "t");
+            setLabel(pAxe, __GO_Y_AXIS_LABEL__, "y");
+
+            setGraphicObjectProperty(pAxe, __GO_X_AXIS_VISIBLE__, &i__1, jni_bool, 1);
+            setGraphicObjectProperty(pAxe, __GO_Y_AXIS_VISIBLE__, &i__1, jni_bool, 1);
+
+            setPolylinesBounds(block, i, 0);
+        }
+
+        sco->scope.cachedFigureUID = pFigureUID;
+    }
+
+    if (sco->scope.cachedFigureUID == NULL)
+    {
+        sco->scope.cachedFigureUID = pFigureUID;
+    }
+    return pFigureUID;
+}
+
+static char *getAxe(char *pFigureUID, scicos_block * block, int input)
+{
+    char *pAxe;
+    int i;
+    sco_data *sco = (sco_data *) * (block->work);
+
+    // fast path for an existing object
+    if (sco->scope.cachedAxeUID != NULL)
+    {
+        return sco->scope.cachedAxeUID;
+    }
 
-            /*Maybe we are in the end of axes so we have to draw new ones */
-            scoRefreshDataBoundsX(pScopeMemory,t);
+    pAxe = findChildWithKindAt(pFigureUID, __GO_AXES__, input);
+
+    /*
+     * Allocate if necessary
+     */
+    if (pAxe == NULL)
+    {
+        pAxe = cloneGraphicObject(getAxesModel());
+
+        if (pAxe != NULL)
+        {
+            setGraphicObjectRelationship(pFigureUID, pAxe);
 
-            // Cannot be factorized depends of the scope
-            u1 = GetRealInPortPtrs(block,1);
-            for (i = 0 ; i < scoGetNumberOfCurvesBySubwin(pScopeMemory,0) ; i++)
+            // allocate the polylines through the getter
+            for (i = 0; i < block->insz[input]; i++)
             {
-                pShortDraw  = scoGetPointerShortDraw(pScopeMemory,0,i);
-                if (pShortDraw)
-                {
-                    NbrPtsShort = pPOLYLINE_FEATURE(pShortDraw)->n1;
-                    pPOLYLINE_FEATURE(pShortDraw)->pvx[NbrPtsShort] = t;
-                    pPOLYLINE_FEATURE(pShortDraw)->pvy[NbrPtsShort] = u1[i];
-                    pPOLYLINE_FEATURE(pShortDraw)->n1++;
-                }
+                getPolyline(pAxe, block, i);
             }
-            // End of Cannot
 
-            // Draw the Scope
-            scoDrawScopeAmplitudeTimeStyle(pScopeMemory, t); //** the scope update condition
-                                                             //** is hidden here
+            sco->scope.cachedAxeUID = pAxe;
         }
+    }
 
-        break;
-    } //**-----------------------------------------------------------
+    if (sco->scope.cachedAxeUID == NULL)
+    {
+        sco->scope.cachedAxeUID = pAxe;
+    }
+    return pAxe;
+}
 
-    case Ending:
+static char *getPolyline(char *pAxeUID, scicos_block * block, int row)
+{
+    char *pPolyline;
+    static const double d__0 = 0.0;
+    static const int i__1 = 1;
+    static const BOOL b__true = TRUE;
+
+    int color;
+
+    sco_data *sco = (sco_data *) * (block->work);
+
+    // fast path for an existing object
+    if (sco->scope.cachedPolylinesUIDs != NULL && sco->scope.cachedPolylinesUIDs[row] != NULL)
+    {
+        return sco->scope.cachedPolylinesUIDs[row];
+    }
+
+    pPolyline = findChildWithKindAt(pAxeUID, __GO_POLYLINE__, row);
+
+    /*
+     * Allocate if necessary
+     */
+    if (pPolyline == NULL)
     {
-        scoRetrieveScopeMemory(block->work, &pScopeMemory);
-        if(scoGetScopeActivation(pScopeMemory) == 1)
+        pPolyline = createGraphicObject(__GO_POLYLINE__);
+
+        if (pPolyline != NULL)
         {
-            //  sciSetUsedWindow(scoGetWindowID(pScopeMemory));
-            //  pShortDraw = sciGetCurrentFigure();
-            //  pFIGURE_FEATURE(pShortDraw)->user_data = NULL;
-            //  pFIGURE_FEATURE(pShortDraw)->size_of_user_data = 0;
-            ///* restore double buffering */
-            //sciSetJavaUseSingleBuffer(pShortDraw, FALSE);
-            //  scoDelCoupleOfPolylines(pScopeMemory);
-
-            /* Check if figure is still opened, otherwise, don't try to destroy it again. */
-            scoGraphicalObject figure = scoGetPointerScopeWindow(pScopeMemory);
-            if (figure != NULL)
+            createDataObject(pPolyline, __GO_POLYLINE__);
+            setGraphicObjectRelationship(pAxeUID, pPolyline);
+
+            /*
+             * Default setup (will crash if removed)
+             */
             {
-                clearUserData(figure);
+                int polylineSize[2] = { 1, block->ipar[2] };
+                setGraphicObjectProperty(pPolyline, __GO_DATA_MODEL_NUM_ELEMENTS_ARRAY__, polylineSize, jni_int_vector, 2);
+            }
+
+            setGraphicObjectProperty(pPolyline, __GO_DATA_MODEL_X__, &d__0, jni_double_vector, 1);
+            setGraphicObjectProperty(pPolyline, __GO_DATA_MODEL_Y__, &d__0, jni_double_vector, 1);
 
-                /* restore double buffering */
-                if (figure) {
-                    sciSetJavaUseSingleBuffer(figure, FALSE);
-                }
-                scoDelCoupleOfPolylines(pScopeMemory);
+            color = block->ipar[3 + row];
+            if (color > 0)
+            {
+                setGraphicObjectProperty(pPolyline, __GO_LINE_MODE__, &b__true, jni_bool, 1);
+                setGraphicObjectProperty(pPolyline, __GO_LINE_COLOR__, &color, jni_int, 1);
+            }
+            else
+            {
+                color = -color;
+                setGraphicObjectProperty(pPolyline, __GO_MARK_MODE__, &b__true, jni_bool, 1);
+                setGraphicObjectProperty(pPolyline, __GO_MARK_STYLE__, &color, jni_int, 1);
             }
 
+            sco->scope.cachedPolylinesUIDs[row] = pPolyline;
         }
-        scoFreeScopeMemory(block->work, &pScopeMemory);
-        break;
     }
+
+    if (sco->scope.cachedPolylinesUIDs != NULL && sco->scope.cachedPolylinesUIDs[row] == NULL)
+    {
+        sco->scope.cachedPolylinesUIDs[row] = pPolyline;
+    }
+    return pPolyline;
+}
+
+static BOOL setPolylinesBuffers(scicos_block * block, int input, int maxNumberOfPoints)
+{
+    int i;
+
+    char *pFigureUID;
+    char *pAxeUID;
+    char *pPolylineUID;
+
+    BOOL result = TRUE;
+    int polylineSize[2] = { 1, maxNumberOfPoints };
+
+    pFigureUID = getFigure(block);
+    pAxeUID = getAxe(pFigureUID, block, input);
+
+    for (i = 0; i < block->insz[input]; i++)
+    {
+        pPolylineUID = getPolyline(pAxeUID, block, i);
+        result &= setGraphicObjectProperty(pPolylineUID, __GO_DATA_MODEL_NUM_ELEMENTS_ARRAY__, polylineSize, jni_int_vector, 2);
     }
+
+    return result;
+}
+
+static BOOL setPolylinesBounds(scicos_block * block, int input, int periodCounter)
+{
+    char *pFigureUID;
+    char *pAxeUID;
+
+    BOOL result;
+    double dataBounds[6];
+    double period = block->rpar[3];
+
+    dataBounds[0] = periodCounter * period; // xMin
+    dataBounds[1] = (periodCounter + 1) * period;   // xMax
+    dataBounds[2] = block->rpar[1]; // yMin
+    dataBounds[3] = block->rpar[2]; // yMax
+    dataBounds[4] = -1.0;       // zMin
+    dataBounds[5] = 1.0;        // zMax
+
+    pFigureUID = getFigure(block);
+    pAxeUID = getAxe(pFigureUID, block, input);
+    return setGraphicObjectProperty(pAxeUID, __GO_DATA_BOUNDS__, dataBounds, jni_double_vector, 6);
 }
-/*--------------------------------------------------------------------------*/