<commit_before>/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 <NAME>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version.  The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* 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
* OpenSceneGraph Public License for more details.
*/

#include <osgVolume/Layer>

#include <osg/ImageUtils>
#include <osg/ImageStream>
#include <osg/Endian>
#include <osg/Notify>
#include <osg/io_utils>

using namespace osgVolume;

ImageDetails::ImageDetails():
    _texelOffset(0.0,0.0,0.0,0.0),
    _texelScale(1.0,1.0,1.0,1.0)
{
}

ImageDetails::ImageDetails(const ImageDetails& rhs,const osg::CopyOp& copyop):
    _texelOffset(rhs._texelOffset),
    _texelScale(rhs._texelScale),
    _matrix(rhs._matrix)
{
}

Layer::Layer():
    _minFilter(osg::Texture::LINEAR),
    _magFilter(osg::Texture::LINEAR)
{
}

Layer::Layer(const Layer& layer,const osg::CopyOp& copyop):
    osg::Object(layer,copyop),
    _filename(layer._filename),
    _minFilter(layer._minFilter),
    _magFilter(layer._magFilter)
{
}

Layer::~Layer()
{
}

osg::BoundingSphere Layer::computeBound() const
{
    if (!getLocator()) return osg::BoundingSphere();

    osg::Vec3d left, right;
    getLocator()->computeLocalBounds(left, right);

    //osg::notify(osg::NOTICE)<<"left = "<<left<<std::endl;
    //osg::notify(osg::NOTICE)<<"right = "<<right<<std::endl;

    return osg::BoundingSphere((left+right)*0.5, (right-left).length()*0.5);
}


void Layer::addProperty(Property* property)
{
    if (!property) return;

    if (!_property)
    {
        _property = property;
        return;
    }

    CompositeProperty* cp = dynamic_cast<CompositeProperty*>(_property.get());
    if (cp)
    {
        cp->addProperty(property);
    }
    else
    {
        cp = new CompositeProperty;
        cp->addProperty(property);
        cp->addProperty(_property.get());
        _property = cp;
    }
}

/////////////////////////////////////////////////////////////////////////////
//
// ImageLayer
//
ImageLayer::ImageLayer(osg::Image* image):
    _texelOffset(0.0,0.0,0.0,0.0),
    _texelScale(1.0,1.0,1.0,1.0),
    _image(image)
{
}

ImageLayer::ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop):
    Layer(imageLayer, copyop),
    _texelOffset(imageLayer._texelOffset),
    _texelScale(imageLayer._texelScale),
    _image(imageLayer._image)
{
}

void ImageLayer::setImage(osg::Image* image)
{
    _image = image;
}

void ImageLayer::dirty()
{
    if (_image.valid()) _image->dirty();
}

void ImageLayer::setModifiedCount(unsigned int value)
{
    if (!_image) return;
    else _image->setModifiedCount(value);
}

unsigned int ImageLayer::getModifiedCount() const
{
    if (!_image) return 0;
    else return _image->getModifiedCount();
}

bool ImageLayer::computeMinMax(osg::Vec4& minValue, osg::Vec4& maxValue)
{
    if (_image.valid()) return osg::computeMinMax(_image.get(), minValue, maxValue);
    else return false;
}

void ImageLayer::offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale)
{
    if (!_image) return;

#if 0
    osg::Vec4 minValue, maxValue;
    if (computeMinMax(minValue, maxValue))
    {
        osg::notify(osg::NOTICE)<<"ImageLayer::offsetAndScaleImage("<<offset<<" and "<<scale<<")"<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    _texelOffset "<<_texelOffset<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    _texelScale "<<_texelScale<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    minValue "<<minValue<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    maxValue "<<maxValue<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    minValue transformed "<<minValue[0]*_texelScale[0]+_texelOffset[0]<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    maxValue transformed "<<maxValue[0]*_texelScale[0]+_texelOffset[0]<<std::endl;
    }
#endif

    osg::offsetAndScaleImage(_image.get(), offset, scale);

    _texelScale[0] /= scale[0];
    _texelScale[1] /= scale[1];
    _texelScale[2] /= scale[2];
    _texelScale[3] /= scale[3];

    _texelOffset[0] -= offset[0]*_texelScale[0];
    _texelOffset[1] -= offset[1]*_texelScale[1];
    _texelOffset[2] -= offset[2]*_texelScale[2];
    _texelOffset[3] -= offset[3]*_texelScale[3];

#if 0<commit_msg>Added reset of the ImageDetails values of TexelOffset and TexelScale.<commit_after>/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 <NAME>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version.  The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* 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
* OpenSceneGraph Public License for more details.
*/

#include <osgVolume/Layer>

#include <osg/ImageUtils>
#include <osg/ImageStream>
#include <osg/Endian>
#include <osg/Notify>
#include <osg/io_utils>

using namespace osgVolume;

ImageDetails::ImageDetails():
    _texelOffset(0.0,0.0,0.0,0.0),
    _texelScale(1.0,1.0,1.0,1.0)
{
}

ImageDetails::ImageDetails(const ImageDetails& rhs,const osg::CopyOp& copyop):
    _texelOffset(rhs._texelOffset),
    _texelScale(rhs._texelScale),
    _matrix(rhs._matrix)
{
}

Layer::Layer():
    _minFilter(osg::Texture::LINEAR),
    _magFilter(osg::Texture::LINEAR)
{
}

Layer::Layer(const Layer& layer,const osg::CopyOp& copyop):
    osg::Object(layer,copyop),
    _filename(layer._filename),
    _minFilter(layer._minFilter),
    _magFilter(layer._magFilter)
{
}

Layer::~Layer()
{
}

osg::BoundingSphere Layer::computeBound() const
{
    if (!getLocator()) return osg::BoundingSphere();

    osg::Vec3d left, right;
    getLocator()->computeLocalBounds(left, right);

    //osg::notify(osg::NOTICE)<<"left = "<<left<<std::endl;
    //osg::notify(osg::NOTICE)<<"right = "<<right<<std::endl;

    return osg::BoundingSphere((left+right)*0.5, (right-left).length()*0.5);
}


void Layer::addProperty(Property* property)
{
    if (!property) return;

    if (!_property)
    {
        _property = property;
        return;
    }

    CompositeProperty* cp = dynamic_cast<CompositeProperty*>(_property.get());
    if (cp)
    {
        cp->addProperty(property);
    }
    else
    {
        cp = new CompositeProperty;
        cp->addProperty(property);
        cp->addProperty(_property.get());
        _property = cp;
    }
}

/////////////////////////////////////////////////////////////////////////////
//
// ImageLayer
//
ImageLayer::ImageLayer(osg::Image* image):
    _texelOffset(0.0,0.0,0.0,0.0),
    _texelScale(1.0,1.0,1.0,1.0),
    _image(image)
{
}

ImageLayer::ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop):
    Layer(imageLayer, copyop),
    _texelOffset(imageLayer._texelOffset),
    _texelScale(imageLayer._texelScale),
    _image(imageLayer._image)
{
}

void ImageLayer::setImage(osg::Image* image)
{
    _image = image;
}

void ImageLayer::dirty()
{
    if (_image.valid()) _image->dirty();
}

void ImageLayer::setModifiedCount(unsigned int value)
{
    if (!_image) return;
    else _image->setModifiedCount(value);
}

unsigned int ImageLayer::getModifiedCount() const
{
    if (!_image) return 0;
    else return _image->getModifiedCount();
}

bool ImageLayer::computeMinMax(osg::Vec4& minValue, osg::Vec4& maxValue)
{
    if (_image.valid()) return osg::computeMinMax(_image.get(), minValue, maxValue);
    else return false;
}

void ImageLayer::offsetAndScaleImage(const osg::Vec4& offset, const osg::Vec4& scale)
{
    if (!_image) return;

#if 0
    osg::Vec4 minValue, maxValue;
    if (computeMinMax(minValue, maxValue))
    {
        osg::notify(osg::NOTICE)<<"ImageLayer::offsetAndScaleImage("<<offset<<" and "<<scale<<")"<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    _texelOffset "<<_texelOffset<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    _texelScale "<<_texelScale<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    minValue "<<minValue<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    maxValue "<<maxValue<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    minValue transformed "<<minValue[0]*_texelScale[0]+_texelOffset[0]<<std::endl;
        osg::notify(osg::NOTICE)<<"     before    maxValue transformed "<<maxValue[0]*_texelScale[0]+_texelOffset[0]<<std::endl;
    }
#endif

    osg::offsetAndScaleImage(_image.get(), offset, scale);

    _texelScale[0] /= scale[0];
    _texelScale[1] /= scale[1];
    _texelScale[2] /= scale[2];
    _texelScale[3] /= scale[3];

    _texelOffset[0] -= offset[0]*_texelScale[0];
    _texelOffset[1] -= offset[1]*_texelScale[1];
    _texelOffset[2] -= offset[2]*_texelScale[2];
    _texelOffset[3] -= offset[3]*_texelScale[3];

    ImageDetails* details = dynamic_cast<ImageDetails*>(_image->getUserData());
    if (details)
    {
        details->setTexelOffset(_texelOffset);
        details->setTexelScale(_texelScale);
    }

#if 0
{'fraction_non_alphanumeric': 0.17295531197301856, 'fraction_numerical': 0.017074198988195614, 'mean_word_length': 3.903875968992248, 'pattern_counts': {'":': 0, '<': 140, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324136', 'n_tokens_mistral': 3329, 'n_tokens_neox': 3287, 'n_words': 617}