WTuringTextureCreator.cpp 9.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

Alexander Wiebel's avatar
Alexander Wiebel committed
25
#include <algorithm>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#include <iostream>
#include <vector>

#include <boost/random.hpp>

#include <core/common/exceptions/WPreconditionNotMet.h>

#include "WTuringTextureCreator.h"

WTuringTextureCreator::WTuringTextureCreator( std::size_t numThreads )
    : m_numIterations( 100 ),
      m_spotIrregularity( 0.1 ),
      m_spotSize( 0.1 )
{
    WPrecond( numThreads > 0, "" );

    for( std::size_t k = 0; k < numThreads; ++k )
    {
        m_threads.push_back( boost::shared_ptr< TextureThread >( new TextureThread( k, numThreads ) ) );
    }
}

WTuringTextureCreator::~WTuringTextureCreator()
{
}

void WTuringTextureCreator::setNumIterations( std::size_t iter )
{
    WPrecond( iter > 0, "Invalid number of iterations!" );

    m_numIterations = iter;
}

void WTuringTextureCreator::setSpotIrregularity( float irr )
{
    WPrecond( irr >= 0.0f && irr <= 1.0f, "Spot irregularity must be in [0,1]!" );

    m_spotIrregularity = irr;
}

void WTuringTextureCreator::setSpotSize( float size )
{
    WPrecond( size >= 0.0f && size <= 1.0f, "Spot size must be in [0,1]!" );

    m_spotSize = size;
}

osg::ref_ptr< WGETexture3D > WTuringTextureCreator::create( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ )
{
    // some constants, maybe change to parameters
    float const spotFactor = ( 0.02f + 0.58f * ( 1.0f - m_spotSize ) ) / 15.0f;
    float const d1 = 0.125f;
    float const d2 = 0.03125f;
    float const speed = 1.0f;

    std::vector< float > concentration1( sizeX * sizeY * sizeZ, 4.0f );
    std::vector< float > concentration2( sizeX * sizeY * sizeZ, 4.0f );
    std::vector< float > delta1( sizeX * sizeY * sizeZ, 0.0f );
    std::vector< float > delta2( sizeX * sizeY * sizeZ, 0.0f );
    std::vector< float > noise( sizeX * sizeY * sizeZ );

    boost::mt19937 generator( std::time( 0 ) );

    float noiseRange = 0.1f + 4.9f * m_spotIrregularity;

    boost::uniform_real< float > dist( 12.0f - noiseRange, 12.0f + noiseRange );
    boost::variate_generator< boost::mt19937&, boost::uniform_real< float > > rand( generator, dist );

    // initialize noise
    for( std::size_t k = 0; k < sizeX * sizeY * sizeZ; ++k )
    {
        noise[ k ] = rand();
    }

    // iterate
    for( std::size_t iter = 0; iter < m_numIterations; ++iter )
    {
        std::cout << "iteration: " << iter << std::endl;

        // step one: calculate change in concentration across the volume
        for( std::size_t k = 0; k < m_threads.size(); ++k )
        {
            m_threads[ k ]->setTextureSize( sizeX, sizeY, sizeZ );
            m_threads[ k ]->setSpotFactor( spotFactor );
            m_threads[ k ]->setDiffusionConstants( d1, d2 );
            m_threads[ k ]->setBufferPointers( &concentration1, &concentration2, &noise, &delta1, &delta2 );
            m_threads[ k ]->run();
        }

        for( std::size_t k = 0; k < m_threads.size(); ++k )
        {
            m_threads[ k ]->wait();
        }

        // applying the change in concentration is not too costly
        for( std::size_t k = 0; k < sizeX * sizeY * sizeZ; ++k )
        {
            concentration1[ k ] += speed * delta1[ k ];
            concentration2[ k ] += speed * delta2[ k ];
        }
    }

    // allocate new osg::Image for the texture data
    osg::ref_ptr< osg::Image > img = new osg::Image;
    img->allocateImage( sizeX, sizeY, sizeZ, GL_LUMINANCE, GL_UNSIGNED_BYTE );

    // find min and max
    float c1min = *std::min_element( concentration1.begin(), concentration1.end() );
    float c1max = *std::max_element( concentration1.begin(), concentration1.end() );

    // copy to image
    for( std::size_t k = 0; k < sizeX * sizeY * sizeZ; ++k )
    {
        img->data()[ k ] = 255.0f * ( concentration1[ k ] - c1min ) / ( c1max - c1min );
    }

    return osg::ref_ptr< WGETexture< osg::Texture3D > >( new WGETexture< osg::Texture3D >( img ) );
}

WTuringTextureCreator::TextureThread::TextureThread( std::size_t id, std::size_t max )
    : WThreadedRunner(),
      m_id( id ),
      m_maxThreads( max ),
      m_sizeX( 2 ),
      m_sizeY( 2 ),
      m_sizeZ( 2 ),
      m_spotFactor( 0.5 ),
      m_diffusionConstant1( 0.5 ),
      m_diffusionConstant2( 0.5 )
{
}

WTuringTextureCreator::TextureThread::~TextureThread()
{
}

void WTuringTextureCreator::TextureThread::setDiffusionConstants( float d1, float d2 )
{
    WPrecond( d1 >= 0.0 && d1 <= 1.0, "" );
    WPrecond( d2 >= 0.0 && d2 <= 1.0, "" );

    m_diffusionConstant1 = d1;
    m_diffusionConstant2 = d2;
}

void WTuringTextureCreator::TextureThread::setSpotFactor( float spotFactor )
{
    WPrecond( spotFactor > 0.0, "" );

    m_spotFactor = spotFactor;
}

void WTuringTextureCreator::TextureThread::setTextureSize( std::size_t sizeX, std::size_t sizeY, std::size_t sizeZ )
{
    WPrecond( sizeX > 0, "" );
    WPrecond( sizeY > 0, "" );
    WPrecond( sizeZ > 0, "" );

    m_sizeX = sizeX;
    m_sizeY = sizeY;
    m_sizeZ = sizeZ;
}

void WTuringTextureCreator::TextureThread::threadMain()
{
    WPrecond( m_concentration1 != 0, "Invalid pointer!" );
    WPrecond( m_concentration2 != 0, "Invalid pointer!" );
    WPrecond( m_noise != 0, "Invalid pointer!" );
    WPrecond( m_delta1 != 0, "Invalid pointer!" );
    WPrecond( m_delta2 != 0, "Invalid pointer!" );

    std::size_t const numVoxels = m_sizeX * m_sizeY * m_sizeZ;

    std::size_t start = m_id * ( numVoxels / m_maxThreads );
    std::size_t end = ( m_id + 1 ) * ( numVoxels / m_maxThreads );

    if( m_id == m_maxThreads - 1 )
    {
        end = numVoxels;
    }

    for( std::size_t idx = start; idx < end; ++idx )
    {
        std::size_t i = idx % m_sizeX;
        std::size_t j = ( idx / m_sizeX ) % m_sizeY;
        std::size_t k = ( idx / m_sizeX ) / m_sizeY;

        std::size_t iNext = ( i + 1 ) % m_sizeX;
        std::size_t iPrev = ( i + m_sizeX - 1 ) % m_sizeX;

        std::size_t jNext = ( j + 1 ) % m_sizeY;
        std::size_t jPrev = ( j + m_sizeY - 1 ) % m_sizeY;

        std::size_t kNext = ( k + 1 ) % m_sizeZ;
        std::size_t kPrev = ( k + m_sizeZ - 1 ) % m_sizeZ;

        // estimate change in concentrations using 3d laplace filter
        float dc1 = 0.0;
        dc1 += ( *m_concentration1 )[ iPrev + j * m_sizeX + k * m_sizeX * m_sizeY ];
        dc1 += ( *m_concentration1 )[ iNext + j * m_sizeX + k * m_sizeX * m_sizeY ];
        dc1 += ( *m_concentration1 )[ i + jPrev * m_sizeX + k * m_sizeX * m_sizeY ];
        dc1 += ( *m_concentration1 )[ i + jNext * m_sizeX + k * m_sizeX * m_sizeY ];
        dc1 += ( *m_concentration1 )[ i + j * m_sizeX + kPrev * m_sizeX * m_sizeY ];
        dc1 += ( *m_concentration1 )[ i + j * m_sizeX + kNext * m_sizeX * m_sizeY ];
        dc1 -= 6.0f * ( *m_concentration1 )[ idx ];

        float dc2 = 0.0;
        dc2 += ( *m_concentration2 )[ iPrev + j * m_sizeX + k * m_sizeX * m_sizeY ];
        dc2 += ( *m_concentration2 )[ iNext + j * m_sizeX + k * m_sizeX * m_sizeY ];
        dc2 += ( *m_concentration2 )[ i + jPrev * m_sizeX + k * m_sizeX * m_sizeY ];
        dc2 += ( *m_concentration2 )[ i + jNext * m_sizeX + k * m_sizeX * m_sizeY ];
        dc2 += ( *m_concentration2 )[ i + j * m_sizeX + kPrev * m_sizeX * m_sizeY ];
        dc2 += ( *m_concentration2 )[ i + j * m_sizeX + kNext * m_sizeX * m_sizeY ];
        dc2 -= 6.0f * ( *m_concentration2 )[ idx ];

        // diffusion
        ( *m_delta1 )[ idx ] = m_spotFactor * ( 16.0f - ( *m_concentration1 )[ idx ] * ( *m_concentration2 )[ idx ] ) + m_diffusionConstant1 * dc1;
Alexander Wiebel's avatar
Alexander Wiebel committed
243 244 245 246
        ( *m_delta2 )[ idx ] = m_spotFactor *
            ( ( *m_concentration1 )[ idx ] * ( *m_concentration2 )[ idx ]
              - ( *m_concentration2 )[ idx ] - ( *m_noise )[ idx ] )
            + m_diffusionConstant2 * dc2;
247 248 249
    }
}

Alexander Wiebel's avatar
Alexander Wiebel committed
250 251 252 253 254
void WTuringTextureCreator::TextureThread::setBufferPointers( std::vector< float > const* concentration1,
                                                              std::vector< float > const* concentration2,
                                                              std::vector< float > const* noise,
                                                              std::vector< float >* delta1,
                                                              std::vector< float >* delta2 )
255 256 257 258 259 260 261 262 263 264 265 266 267
{
    WPrecond( concentration1 != 0, "Invalid pointer!" );
    WPrecond( concentration2 != 0, "Invalid pointer!" );
    WPrecond( noise != 0, "Invalid pointer!" );
    WPrecond( delta1 != 0, "Invalid pointer!" );
    WPrecond( delta2 != 0, "Invalid pointer!" );

    m_concentration1 = concentration1;
    m_concentration2 = concentration2;
    m_noise = noise;
    m_delta1 = delta1;
    m_delta2 = delta2;
}