Last active
May 30, 2016 17:16
-
-
Save auxiliary/b0f928185f8f32e12b6ab3d57a196870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''========================================================================= | |
Program: Visualization Toolkit | |
Module: vtk1DGaussianTransferFunction.cxx (Python version) | |
Converted to Python by Mohammad A.Raji ([email protected]) | |
Copyright (c) Ken Martin, Schroeder, Lorensen | |
All rights reserved. | |
See Copyright.txt or http:#www.kitware.com/Copyright.htm for details. | |
This software is distributed WITHOUT ANY WARRANTY; without even | |
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
PURPOSE. See the above copyright notice for more information. | |
=========================================================================''' | |
# .NAME vtk1DGaussianTransferFunction | |
# .SECTION Thanks | |
# <verbatim> | |
# | |
# This file is part of the PointSprites plugin developed and contributed by | |
# | |
# Copyright (c) CSCS - Swiss National Supercomputing Centre | |
# EDF - Electricite de France | |
# | |
# John Biddiscombe, Varetto (CSCS) | |
# Stephane Ploix (EDF) | |
# | |
# </verbatim> | |
import math | |
class vtk1GaussianTransferFunction: | |
def __init__(self): | |
self.GaussianControlPoints = [] | |
def MapValue(self, value, _range): | |
opacity = 0.0 | |
delta = _range[1] - _range[0] | |
if delta == 0: | |
delta = 1.0 | |
ratio = (value - _range[0]) / delta | |
for p in range(self.GetNumberOfGaussianControlPoints()): | |
gp = self.GaussianControlPoints[p] | |
pos = gp[0] | |
height = gp[1] | |
width = gp[2] | |
xbias = gp[3] | |
ybias = gp[4] | |
# clamp non-zero values to pos +/- width | |
if ratio > pos + width or ratio < pos - width: | |
opacity = max(opacity, 0.0) | |
else: | |
# non-zero width | |
if width == 0: | |
width = .00001 | |
# translate the original x to a x based on the xbias | |
if xbias == 0 or ratio == pos + xbias: | |
x0 = ratio | |
elif ratio > pos + xbias: | |
if width == xbias: | |
x0 = pos | |
else: | |
x0 = pos + (ratio - pos - xbias) * (width / (width - xbias)) | |
else: # (x < pos+xbias) | |
if -width == xbias: | |
x0 = pos | |
else: | |
x0 = pos - (ratio - pos - xbias) * (width / (width + xbias)) | |
# center around 0 and normalize to -1,1 | |
x1 = (x0 - pos) / width | |
# do a linear interpolation between: | |
# a gaussian and a parabola if 0<ybias<1 | |
# a parabola and a step function if 1<ybias<2 | |
h0a = math.exp(-(4* x1 * x1)) | |
h0b = 1. - x1 * x1 | |
h0c = 1. | |
if ybias < 1: | |
h1 = ybias * h0b + (1 - ybias) * h0a | |
else: | |
h1 = (2 - ybias) * h0b + (ybias - 1) * h0c | |
h2 = height * h1 | |
# perform the MAX over different guassians, the sum | |
opacity = max(opacity, h2) | |
return opacity | |
def GetNumberOfGaussianControlPoints(self): | |
return len(self.GaussianControlPoints) | |
# Description: | |
# Set/Get a single Gaussian control point. | |
def SetGaussianControlPoint(self, index, pos, height, width, xbias, ybias): | |
values = (pos, height, width, xbias, ybias) | |
self._SetGaussianControlPoint(index, values) | |
def _SetGaussianControlPoint(self, index, values): | |
_tuple = [] | |
if index < 0: | |
return | |
if index < len(self.GaussianControlPoints): | |
self.GaussianControlPoints[index] = values | |
else: | |
self.GaussianControlPoints.append(values) | |
def GetGaussianControlPoint(self, index): | |
if index < 0 or index >= self.GetNumberOfGaussianControlPoints(): | |
return | |
_tuple = self.GaussianControlPoints[index] | |
return _tuple | |
# Description: | |
# add/remove a Gaussian control point. | |
def AddGaussianControlPoint(self, pos, height, width, xbiais, ybiais): | |
self.SetGaussianControlPoint(self.GetNumberOfGaussianControlPoints(), pos, height, width, xbiais, ybiais) | |
def _AddGaussianControlPoint(self, values): | |
self._SetGaussianControlPoint(self.GetNumberOfGaussianControlPoints(), values) | |
def RemoveGaussianControlPoint(self, index): | |
if index < 0 or index >= self.GetNumberOfGaussianControlPoints(): | |
return | |
if self.GetNumberOfGaussianControlPoints() == 1: | |
self.RemoveAllGaussianControlPoints() | |
return | |
newGCP = [] | |
ngcp = len(self.GaussianControlPoints) - 1 | |
for ii in range(index): | |
newGCP[ii] = self.GaussianControlPoints[ii] | |
for ii in range(index, ngcp): | |
newGCP[ii] = self.GaussianControlPoints[ii + 1] | |
self.GaussianControlPoints = newGCP | |
# Description: | |
# clean all control points. | |
def RemoveAllGaussianControlPoints(self): | |
self.GaussianControlPoints = [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment