<commit_before>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#ifndef SOFA_IMAGE_IMAGEVIEWER_H
#define SOFA_IMAGE_IMAGEVIEWER_H


#include "initImage.h"
#include "ImageTypes.h"
#include "VectorVis.h"

#include <sofa/component/component.h>
#include <sofa/helper/io/Image.h>
#include <sofa/helper/gl/Texture.h>
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/objectmodel/Event.h>
#include <sofa/component/visualmodel/VisualModelImpl.h>
#include <sofa/core/objectmodel/BaseObject.h>

#include <sofa/defaulttype/Vec.h>
#include <sofa/core/CollisionModel.h>

namespace sofa
{

namespace component
{

namespace misc
{

using namespace defaulttype;

/**
   * \brief This component is responsible for displaying images in SOFA
   *
   *  ImageViewer scene options:
   *
   *  <b>image</b> - a link to the imge in the ImageContainer component
   *
   *  <b>transform</b> - a link to the transformation in the ImageContainer component
   *
   *  <b>subsample</b> - two integers representing the default subsample values for the XY plane and the Z plane respectively. Default values are (12 12).
   *
   *  <b>shape</b> - If true, an image that contains vector information will display the vectors using a shape (arrows or ellipsoids). Default value is false.
   *
   *  <b>scale</b> - A float value of the scale (size) of the shape. Default value is 11.75.
   *
   *  <b>histogramValues</b> - Alias include: <b>defaultHistogram</b>, <b>defaultHisto</b>, <b>histoValues</b>.
   *    Two floats representing the minimum and maximum windowing (or clamping) values. Default gives no windowing.
   *
   *  <b>defaultSlices</b> - Three integers describing the x, y and z slices to be displayed initially. Default displays the middle slice in each plane.
   *
   *  <b>defaultRgb</b> - If true, an image that contains vector information will be displayed as an RGB image. Default value is false.
   *
   */
template<class _ImageTypes>
class SOFA_IMAGE_API ImageViewer : public sofa::core::objectmodel::BaseObject
{
public:
    typedef sofa::core::objectmodel::BaseObject Inherited;
    SOFA_CLASS(SOFA_TEMPLATE(ImageViewer, _ImageTypes), Inherited);

    // image data
    typedef _ImageTypes ImageTypes;
    typedef typename ImageTypes::T T;
    typedef typename ImageTypes::imCoord imCoord;
    typedef helper::ReadAccessor<Data< ImageTypes > > raImage;
    Data< ImageTypes > image;

    // @name Histogram
    /**@{*/
    typedef Histogram<T> HistogramType;
    typedef helper::WriteAccessor<Data< HistogramType > > waHisto;
    typedef helper::ReadAccessor<Data< HistogramType > > raHisto;
    Data< HistogramType > histo;
    /**@}*/

    //@name Transform data
    /**@{*/
    typedef SReal Real;
    typedef ImageLPTransform<Real> TransformType;
    typedef typename TransformType::Coord Coord;
    typedef helper::ReadAccessor<Data< TransformType > > raTransform;
    Data< TransformType > transform;
    /**@}*/

    // @name Plane selection
    /**@{*/
    typedef ImagePlane<T> ImagePlaneType;
    typedef helper::ReadAccessor<Data< ImagePlaneType > > raPlane;
    typedef helper::WriteAccessor<Data< ImagePlaneType > > waPlane;
    Data< ImagePlaneType > plane;
    /**@}*/

    // @name Vector visualization
    /**@{*/
    typedef helper::ReadAccessor<Data< VectorVis > > raVis;
    typedef helper::WriteAccessor<Data< VectorVis > > waVis;
    Data<VectorVis> vectorVisualization;
    /**@}*/

    typedef component::visualmodel::VisualModelImpl VisuModelType;

    std::string getTemplateName() const  {	return templateName(this);	}
    static std::string templateName(const ImageViewer<ImageTypes>* = NULL)	{ return ImageTypes::Name(); }

    ImageViewer() : Inherited()
        , image(initData(&image,ImageTypes(),"image","input image"))
        , histo(initData(&histo, HistogramType(256,256,false),"histo",""))
        , transform(initData(&transform, TransformType(), "transform" , ""))
        , plane ( initData ( &plane, ImagePlaneType(), "plane" , "" ) )
        , vectorVisualization ( initData (&vectorVisualization, VectorVis(), "vectorvis", ""))
    {
        this->addAlias(&image, "outputImage");
        this->addAlias(&transform, "outputTransform");

        image.setGroup("Image");
        image.setReadOnly(true);

        histo.setGroup("Histogram");
        histo.setWidget("imagehistogram");

        transform.setGroup("Transform");
        transform.setReadOnly(true);

        plane.setGroup("Image");
        plane.setWidget("imageplane");

        vectorVisualization.setWidget("vectorvis");

        for(unsigned int i=0; i<3; i++)	cutplane_tex[i]=NULL;
    }


    virtual ~ImageViewer()
    {
        for(unsigned int i=0; i<3; i++)	if(cutplane_tex[i]) delete cutplane_tex[i];
    }

    virtual void init()
    {

        // getvisuals
        std::vector<VisuModelType*> visuals;
        sofa::core::objectmodel::BaseContext* context = this->getContext();
        context->get<VisuModelType>(&visuals,core::objectmodel::BaseContext::SearchRoot);

        // set histogram data
        waHisto whisto(this->histo);        whisto->setInput(image.getValue());

        // record user values of plane position
        typename ImagePlaneType::pCoord pc; bool usedefaultplanecoord=true;
        if(this->plane.isSet()) {  raPlane rplane(this->plane); pc=rplane->getPlane(); usedefaultplanecoord=false; }

        // set plane data
        waPlane wplane(this->plane);        wplane->setInput(image.getValue(),transform.getValue(),visuals);

        // set recorded plane pos
        if(!usedefaultplanecoord) wplane->setPlane(pc);

        // enable vecorvis ?
        if(wplane->getDimensions()[3]<2) vectorVisualization.setDisplayed(false);
        else         vectorVisualization.setGroup("Vectors");

        for(unsigned int i=0; i<3; i++)
        {
            cutplane_tex[i]= new helper::gl::Texture(new helper::io::Image,false);
            cutplane_tex[i]->getImage()->init(cutplane_res,cutplane_res,32);
        }

        updateTextures();

        for(unsigned int i=0; i<3; i++)	cutplane_tex[i]->init();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    }


    virtual void reinit()
    {
        waHisto whisto(this->histo);
        waPlane wplane(this->plane);
        raVis rvis(this->vectorVisualization);<commit_msg>r12041/sofa-dev : FIX: Would only display vectorial images as greyscale after a change was made in the ImageViewer widget.  <commit_after>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#ifndef SOFA_IMAGE_IMAGEVIEWER_H
#define SOFA_IMAGE_IMAGEVIEWER_H


#include "initImage.h"
#include "ImageTypes.h"
#include "VectorVis.h"

#include <sofa/component/component.h>
#include <sofa/helper/io/Image.h>
#include <sofa/helper/gl/Texture.h>
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/objectmodel/Event.h>
#include <sofa/component/visualmodel/VisualModelImpl.h>
#include <sofa/core/objectmodel/BaseObject.h>

#include <sofa/defaulttype/Vec.h>
#include <sofa/core/CollisionModel.h>

namespace sofa
{

namespace component
{

namespace misc
{

using namespace defaulttype;

/**
   * \brief This component is responsible for displaying images in SOFA
   *
   *  ImageViewer scene options:
   *
   *  <b>image</b> - a link to the imge in the ImageContainer component
   *
   *  <b>transform</b> - a link to the transformation in the ImageContainer component
   *
   *  <b>subsample</b> - two integers representing the default subsample values for the XY plane and the Z plane respectively. Default values are (12 12).
   *
   *  <b>shape</b> - If true, an image that contains vector information will display the vectors using a shape (arrows or ellipsoids). Default value is false.
   *
   *  <b>scale</b> - A float value of the scale (size) of the shape. Default value is 11.75.
   *
   *  <b>histogramValues</b> - Alias include: <b>defaultHistogram</b>, <b>defaultHisto</b>, <b>histoValues</b>.
   *    Two floats representing the minimum and maximum windowing (or clamping) values. Default gives no windowing.
   *
   *  <b>defaultSlices</b> - Three integers describing the x, y and z slices to be displayed initially. Default displays the middle slice in each plane.
   *
   *  <b>defaultRgb</b> - If true, an image that contains vector information will be displayed as an RGB image. Default value is false.
   *
   */
template<class _ImageTypes>
class SOFA_IMAGE_API ImageViewer : public sofa::core::objectmodel::BaseObject
{
public:
    typedef sofa::core::objectmodel::BaseObject Inherited;
    SOFA_CLASS(SOFA_TEMPLATE(ImageViewer, _ImageTypes), Inherited);

    // image data
    typedef _ImageTypes ImageTypes;
    typedef typename ImageTypes::T T;
    typedef typename ImageTypes::imCoord imCoord;
    typedef helper::ReadAccessor<Data< ImageTypes > > raImage;
    Data< ImageTypes > image;

    // @name Histogram
    /**@{*/
    typedef Histogram<T> HistogramType;
    typedef helper::WriteAccessor<Data< HistogramType > > waHisto;
    typedef helper::ReadAccessor<Data< HistogramType > > raHisto;
    Data< HistogramType > histo;
    /**@}*/

    //@name Transform data
    /**@{*/
    typedef SReal Real;
    typedef ImageLPTransform<Real> TransformType;
    typedef typename TransformType::Coord Coord;
    typedef helper::ReadAccessor<Data< TransformType > > raTransform;
    Data< TransformType > transform;
    /**@}*/

    // @name Plane selection
    /**@{*/
    typedef ImagePlane<T> ImagePlaneType;
    typedef helper::ReadAccessor<Data< ImagePlaneType > > raPlane;
    typedef helper::WriteAccessor<Data< ImagePlaneType > > waPlane;
    Data< ImagePlaneType > plane;
    /**@}*/

    // @name Vector visualization
    /**@{*/
    typedef helper::ReadAccessor<Data< VectorVis > > raVis;
    typedef helper::WriteAccessor<Data< VectorVis > > waVis;
    Data<VectorVis> vectorVisualization;
    /**@}*/

    typedef component::visualmodel::VisualModelImpl VisuModelType;

    std::string getTemplateName() const  {	return templateName(this);	}
    static std::string templateName(const ImageViewer<ImageTypes>* = NULL)	{ return ImageTypes::Name(); }

    ImageViewer() : Inherited()
        , image(initData(&image,ImageTypes(),"image","input image"))
        , histo(initData(&histo, HistogramType(256,256,false),"histo",""))
        , transform(initData(&transform, TransformType(), "transform" , ""))
        , plane ( initData ( &plane, ImagePlaneType(), "plane" , "" ) )
        , vectorVisualization ( initData (&vectorVisualization, VectorVis(), "vectorvis", ""))
    {
        this->addAlias(&image, "outputImage");
        this->addAlias(&transform, "outputTransform");

        image.setGroup("Image");
        image.setReadOnly(true);

        histo.setGroup("Histogram");
        histo.setWidget("imagehistogram");

        transform.setGroup("Transform");
        transform.setReadOnly(true);

        plane.setGroup("Image");
        plane.setWidget("imageplane");

        vectorVisualization.setWidget("vectorvis");

        for(unsigned int i=0; i<3; i++)	cutplane_tex[i]=NULL;
    }


    virtual ~ImageViewer()
    {
        for(unsigned int i=0; i<3; i++)	if(cutplane_tex[i]) delete cutplane_tex[i];
    }

    virtual void init()
    {

        // getvisuals
        std::vector<VisuModelType*> visuals;
        sofa::core::objectmodel::BaseContext* context = this->getContext();
        context->get<VisuModelType>(&visuals,core::objectmodel::BaseContext::SearchRoot);

        // set histogram data
        waHisto whisto(this->histo);        whisto->setInput(image.getValue());

        // record user values of plane position
        typename ImagePlaneType::pCoord pc; bool usedefaultplanecoord=true;
        if(this->plane.isSet()) {  raPlane rplane(this->plane); pc=rplane->getPlane(); usedefaultplanecoord=false; }

        // set plane data
        waPlane wplane(this->plane);        wplane->setInput(image.getValue(),transform.getValue(),visuals);

        // set recorded plane pos
        if(!usedefaultplanecoord) wplane->setPlane(pc);

        // enable vecorvis ?
        if(wplane->getDimensions()[3]<2) vectorVisualization.setDisplayed(false);
        else         vectorVisualization.setGroup("Vectors");

        for(unsigned int i=0; i<3; i++)
        {
            cutplane_tex[i]= new helper::gl::Texture(new helper::io::Image,false);
            cutplane_tex[i]->getImage()->init(cutplane_res,cutplane_res,32);
        }

        raVis rvis(this->vectorVisualization);

        whisto->setMergeChannels(!rvis->getRgb());
        wplane->setMergeChannels(!rvis->getRgb());
        wplane->setClamp(whisto->getClamp());

        updateTextures();

        for(unsigned int i=0; i<3; i++)	cutplane_tex[i]->init();

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    }


    virtual void reinit()
    {
        waHisto whisto(this->histo);
        waPlane wplane(this->plane);
        raVis rvis(this->vectorVisualization);
{'fraction_non_alphanumeric': 0.13808503036798855, 'fraction_numerical': 0.006490413242824818, 'mean_word_length': 2.6463308727746417, 'pattern_counts': {'":': 0, '<': 133, '<?xml version=': 0, '>': 168, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '237745', 'n_tokens_mistral': 4741, 'n_tokens_neox': 4522, 'n_words': 1190}