Python implementation of Perlin Noise: how to evaluate t?

grasshopper
python
ironpython

(Chris Hanley) #1

I have posted the original question over on the grasshopper fourm, http://www.grasshopper3d.com/forum/topics/python-version-on-perlin-noise-how-to-evaluate-t

I guess the short version is, I don’t know an approach, in python, to “evaluate t”. As in, I have a domain of values, (in this case “noise” values), and I want to cycle(?) through the values based on some input parameter t (representing a place in time)? It’s literally a python recreation/knock off of guilio’s perlin noise component.

I"m just not sure where to start. Here is the code I have so far:
NOTE: REQUIRES: https://pypi.python.org/pypi/noise/ open perlin.py in rhino python editor or set path to module

 import Rhino as r
 import perlin as p
 
 sn = p.SimplexNoise()
 
pVals = []
#remappedValues = []
 
 #def mapValues(values,srcMin,srcMax,targetMin,targetMax):  
 #    for v in values:
 #        if srcMax-srcMin > 0:
 #            rv = ((v-srcMin)/(srcMax-srcMin))*(targetMax-targetMin)+targetMin
#        else:
#            rv = (targetMin+targetMax)/2
#        remappedValues.append(rv)
      
for pt in pts: #trying to figure out how to implement t.....offset x/y/z scale somehow?
     if t < 0: 
         noise = sn.noise3(pt.X*scl, pt.Y*scl, pt.Z*scl)
    else:
        noise = sn.noise3(pt.X*scl, pt.Y*scl, pt.Z*scl)#what's the approach?
     pVals.append(noise)

#mapValues(pVals, min(pVals), max(pVals), -1, 1)
 
b = pVals

If anyone has any ideas to point me in the right direction, it would be much appreciated. desired outcome shown below. Guilio’s version (evaluating t in blue, my “static” version in pink).
Comparision

gh file with guilios component and my python attempt…
PerlinExercisePy.gh (21.1 KB)
required python module, (load in rhino python editor or save and set module path)
perlin.py (11.4 KB)

Thanks,
Chris


(Giulio Piacentino) #2

Hi Chris

I am not sure about the library you are using, but I used code that evaluates Perlin noise extended in 4D. If you write the code yourself, it’s easy to imagine how to do that. It’s been a long time, though, so I do not exactly remember all details. However, the Perlin noise and the Simplex noise components I wrote are published here:

You can have a look at how that code is implemented, it’s a C# port of this paper:
http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com


(Chris Hanley) #4

Guilio,

Thanks for the direction!

There are currently two python libraries that I found / referenced.
The first looks like a port from the paper you referenced, but doesn’t contain a 4D function. That’s the one that I referenced above. It’s here for anyone interested. (extract perlin.py)
https://pypi.python.org/pypi/noise/

The second one I found does have a 4D function and worked great!


I was able to get it working, but had to change one line in it: (line 81 in the library).
return c_long(x).value
to
return long(x)

The downside is that it’s a bit above my current python knowledge. Aside from getting it working in a GH python component… I didn’t really learn anything…which was the original point of this exercise!

With that said, I am going to use the paper you referenced, and try to write a 4D function in the original library I was using, as I’m pretty sure it was ported from the paper you referenced.

Thanks again for all of you help!
Chris


(Giulio Piacentino) #5

Hi @chanley

Exactly :slight_smile: I remember that I had to write the Perlin function myself but it wasn’t all that complicated. Good luck, it’s a fun exercise! :slight_smile:

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com