From b995cf95cc8eaae3317f85e18edec0f333a14dcd Mon Sep 17 00:00:00 2001 From: Michael Nilsson Date: Mon, 25 Apr 2016 17:31:37 -0400 Subject: [PATCH] cleanup of read data function --- modules/iswa/rendering/dataplane.cpp | 230 ++++++++++++--------------- modules/iswa/rendering/dataplane.h | 11 ++ 2 files changed, 115 insertions(+), 126 deletions(-) diff --git a/modules/iswa/rendering/dataplane.cpp b/modules/iswa/rendering/dataplane.cpp index 7fd907da9c..35da2f58fd 100644 --- a/modules/iswa/rendering/dataplane.cpp +++ b/modules/iswa/rendering/dataplane.cpp @@ -247,51 +247,16 @@ float* DataPlane::readData(){ std::string line; std::vector selectedOptions = _dataOptions.value(); + int numSelected = selectedOptions.size(); - std::vector min; - std::vector max; + std::vector min(numSelected, std::numeric_limits::max()); + std::vector max(numSelected, std::numeric_limits::min()); - std::vector logmean; - - std::vector sum; - std::vector mean; - std::vector standardDeviation; - - std::vector> optionValues; + std::vector logmean(numSelected, 0); + std::vector sum(numSelected, 0.0f); + std::vector> optionValues(numSelected, std::vector()); - // HISTOGRAM - // number of levels/bins/values - const int levels = 512; - // Normal Histogram where "levels" is the number of steps/bins - std::vector> histogram; - // Maps the old levels to new ones. - std::vector> newLevels; - - // maps the data values to the histogram bin/index/level - auto mapToHistogram = [levels](float val, float varMin, float varMax) { - float probability = (val-varMin)/(varMax-varMin); - float mappedValue = probability * levels; - return glm::clamp(mappedValue, 0.0f, static_cast(levels - 1)); - }; - - - for(int i=0; i < selectedOptions.size(); i++){ - min.push_back(std::numeric_limits::max()); - max.push_back(std::numeric_limits::min()); - - sum.push_back(0); - logmean.push_back(0); - - std::vector v; - optionValues.push_back(v); - - //initialize histogram for chosen values - histogram.push_back( std::vector(levels, 0) ); - //initialize the newLevels for chosen values - newLevels.push_back( std::vector(levels, 0.0f) ); - } - - float* combinedValues = new float[3*_dimensions.x*_dimensions.y]; + float* data = new float[3*_dimensions.x*_dimensions.y]{0.0f}; int numValues = 0; while(getline(memorystream, line)){ @@ -307,7 +272,7 @@ float* DataPlane::readData(){ } if(value.size()){ - for(int i=0; i inputData, float min, float max,float sum, int numOutputChannels, float logmean){ + + // HISTOGRAM + // number of levels/bins/values + const int levels = 512; + // Normal Histogram where "levels" is the number of steps/bins + std::vector histogram = std::vector(levels, 0); + // Maps the old levels to new ones. + std::vector newLevels = std::vector(levels, 0.0f); + + const int numValues = inputData.size(); + + // maps the data values to the histogram bin/index/level + auto mapToHistogram = [levels](float val, float varMin, float varMax) { + float probability = (val-varMin)/(varMax-varMin); + float mappedValue = probability * levels; + return glm::clamp(mappedValue, 0.0f, static_cast(levels - 1)); + }; + + //Calculate the mean + float mean = (1.0 / numValues) * sum; + //Calculate the Standard Deviation + float standardDeviation = sqrt (((pow(sum, 2.0)) - ((1.0/numValues) * (pow(sum,2.0)))) / (numValues - 1.0)); + //calulate log mean + logmean /= numValues; + + //HISTOGRAM FUNCTIONALITY + //====================== + if(_useHistogram.value()){ + for(int i = 0; i < numValues; i++){ + float v = inputData[i]; + float pixelVal = mapToHistogram(v, min, max); + histogram[(int)pixelVal]++; + inputData[i] = pixelVal; + } + + // Map mean and standard deviation to histogram levels + mean = mapToHistogram(mean , min, max); + logmean = mapToHistogram(logmean , min, max); + standardDeviation = mapToHistogram(standardDeviation, min, max); + min = 0.0f; + max = levels - 1.0f; + + //Calculate the cumulative distributtion function (CDF) + float previousCdf = 0.0f; + for(int i = 0; i < levels; i++){ + + float probability = histogram[i] / (float)numValues; + float cdf = previousCdf + probability; + cdf = glm::clamp(cdf, 0.0f, 1.0f); //just in case + newLevels[i] = cdf * (levels-1); + previousCdf = cdf; + } + } + //====================== + + for(int i=0; i< numValues; i++){ + + float v = inputData[i]; + + // if use histogram get the equalized values + if(_useHistogram.value()){ + v = newLevels[(int)v]; + + // Map mean and standard deviation to new histogram levels + mean = newLevels[(int) mean]; + logmean = newLevels[(int) logmean]; + standardDeviation = newLevels[(int) standardDeviation]; + } + + // Normalize values + if(_useLog.value()){ + v = normalizeWithLogarithm(v, logmean); + }else{ + v = normalizeWithStandardScore(v, mean, standardDeviation); + } + + if(numOutputChannels == 1 && inputChannel > 0){ + // take the average. + outputData[3*i+0] = ( outputData[3*i+0] * inputChannel + v ) / (inputChannel+1); + } else { + outputData[3*i+inputChannel] += v; + } + } +} + float DataPlane::normalizeWithStandardScore(float value, float mean, float sd){ float zScoreMin = _normValues.value().x; diff --git a/modules/iswa/rendering/dataplane.h b/modules/iswa/rendering/dataplane.h index 37b61c7e6f..9dc5e16803 100644 --- a/modules/iswa/rendering/dataplane.h +++ b/modules/iswa/rendering/dataplane.h @@ -48,6 +48,17 @@ class DataPlane : public CygnetPlane { virtual bool updateTexture() override; void readHeader(); float* readData(); + void processData( + float* outputData, // Where you want your processed data to go + int inputChannel, // index of the data channel + std::vector inputData, //data that needs processing + float min, // min value of the input data + float max, // max valye of the input data + float sum, // sum of the input data + int numOutputChannels, // number of data channels that you want in the output + float logmean // log mean value of the input data + ); + float normalizeWithStandardScore(float value, float mean, float sd); float normalizeWithLogarithm(float value, int logMean);