PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/openclnoise/worley.py

https://github.com/freethenation/OpenCLNoise
Python | 73 lines | 65 code | 7 blank | 1 comment | 0 complexity | 1de6964901c4140e32fbf72ce9fdeeca MD5 | raw file
Possible License(s): MIT
  1. from basefilter import *
  2. DISTANCES = {
  3. 'euclidian': 0,
  4. 'manhattan': 1,
  5. 'chebyshev': 2,
  6. 'chessboard': 2,
  7. }
  8. import re
  9. #def Property(func): return property(**func())
  10. class Worley(BaseFilter):
  11. _filename = 'worley.cl'
  12. def __init__(self,function='F1',distance='euclidian',seed=0):
  13. super(type(self),self).__init__()
  14. self.__function = ''
  15. self.__distance = ''
  16. self.__seed = 0
  17. self.__defines = {}
  18. self.distance = distance
  19. self.function = function
  20. self.seed = seed
  21. def get_number_of_inputs(self):
  22. return 1
  23. @property
  24. def function(self):
  25. return self.__function
  26. @function.setter
  27. def function(self,value):
  28. self.__function = value
  29. self.__defines['NUMVALUES'] = max([int(m.group(1)) for m in re.finditer(r'F(\d*)',self.function)]) # Calculate how many values we must find
  30. self.__defines['FUNCTION'] = self.__function
  31. for i in xrange(self.__defines['NUMVALUES']):
  32. self.__defines['FUNCTION'] = self.__defines['FUNCTION'].replace('F{0}'.format(i+1),'F[{0}]'.format(i))
  33. self.on_code_dirty(self)
  34. @property
  35. def distance(self):
  36. return self.__distance
  37. @distance.setter
  38. def distance(self,value):
  39. if not value in DISTANCES and value not in DISTANCES.values(): raise ValueError("Invalid distance. Valid options are: {0}".format(DISTANCES.keys()))
  40. self.__distance = value
  41. self.__defines['DISTANCE'] = DISTANCES[value]
  42. self.on_code_dirty(self)
  43. @filter_argument(ArgumentTypes.INT, 0)
  44. def seed():
  45. def fget(self):
  46. return self._seed
  47. def fset(self, value):
  48. self._seed = int(value)
  49. return fget, fset, None
  50. def generate_code(self):
  51. code = ''
  52. for k,v in self.__defines.iteritems():
  53. code += '#define /*id*/{0} {1}\n'.format(k,v)
  54. code += super(type(self),self).generate_code()
  55. return code
  56. def get_name(self):
  57. return 'worley'
  58. def __repr__(self):
  59. return "Worley(function={0},distance='{1}',seed={2})".format(self.function,self.distance,self.seed)