> Date: Wed, 26 Oct 2011 19:50:09 +0200 > From: [hidden email] > To: [hidden email] > Subject: Re: [OpenSCAD] boolean logic in openscad > > On Wed, Oct 26, 2011 at 01:36:10PM +0000, William Adams wrote: > > I am wanting to implement a function that has the following pattern > > func(x) = (x<<13) & 0x7fffffff[...]? > > are you aware that this is just the same as func(x) = (x*8192) % 2147483648; > (that is, in more readable but non-openscad notation, x*(2^13) % 0x80000000)? > > (at least, if you expect x to behave like an integer -- i couldn't > correlate your sniplet to the source code i found when quickly looking > it up[1].) > > regards > chrysn William, I am not familair with Perlin functions but I think Chrysn here comes closest to a solution. Given his observation that the AND operation can be replaced by MOD in the case of a group of contiguious bits, the only missing operator in OpenSCAD is the shift operator. No big deal. Let me go just a bit further to find: func(x) = (x<<13) & 0x7fffffff func(x) = ((2^13)*x) mod 2^31 func(x)/2^13 = x mod 2^18 func(x) = (2^13)*(x mod 2^18) func(x) = 8192*(x mod 262144) which, I believe, can be implemented in many languages (including OpenSCAD) as func(x) = 8192*(x % 262144). Does that accomplish what you have in mind for Perlin functions? Yours, Dan |
Thanks for the suggestions.
I won't always be doing '&' with such a nice number, so having a generalized form that can deal with any bit pattern would be extra useful. The shifting, and general bit twiddling would be useful as well. Perlin Noise is a concept from 3D Graphics. It is a basic component in procedural texture mapping. Whenever you see marble, wood, or even landscapes that are generated algorithmically, there is probably usage of Perlin noise, or something like it. I've played around with some simple procedural texture mapping in OpenScad. Now I want to up the game so that I can see prettier pictures in the viewer. -- William =============================== - Shaping clay is easier than digging it out of the ground. http://blog.nanotechstyles.com http://www.thingiverse.com/WilliamAAdams https://github.com/Wiladams > To: [hidden email]; [hidden email]; [hidden email] > CC: [hidden email] > From: [hidden email] > Subject: A possible solution, was Re: [OpenSCAD] boolean logic in openscad > Date: Wed, 26 Oct 2011 14:50:09 -0700 > > > > > Date: Wed, 26 Oct 2011 19:50:09 +0200 > > From: [hidden email] > > To: [hidden email] > > Subject: Re: [OpenSCAD] boolean logic in openscad > > > > On Wed, Oct 26, 2011 at 01:36:10PM +0000, William Adams wrote: > > > I am wanting to implement a function that has the following pattern > > > func(x) = (x<<13) & 0x7fffffff[...]? > > > > are you aware that this is just the same as func(x) = (x*8192) % 2147483648; > > (that is, in more readable but non-openscad notation, x*(2^13) % 0x80000000)? > > > > (at least, if you expect x to behave like an integer -- i couldn't > > correlate your sniplet to the source code i found when quickly looking > > it up[1].) > > > > regards > > chrysn > > > William, > > I am not familair with Perlin functions but I > think Chrysn here comes closest to a solution. > > Given his observation that the AND operation > can be replaced by MOD in the case of a group > of contiguious bits, the only missing operator > in OpenSCAD is the shift operator. No big deal. > > Let me go just a bit further to find: > > func(x) = (x<<13) & 0x7fffffff > func(x) = ((2^13)*x) mod 2^31 > func(x)/2^13 = x mod 2^18 > func(x) = (2^13)*(x mod 2^18) > func(x) = 8192*(x mod 262144) > > which, I believe, can be implemented in many > languages (including OpenSCAD) as > > func(x) = 8192*(x % 262144). > > Does that accomplish what you have in mind for > Perlin functions? > > Yours, > > Dan |
> From: William Adams <[hidden email]>
> To: <[hidden email]>, <[hidden email]>, <[hidden email]> > Subject: RE: A possible solution, was Re: [OpenSCAD] boolean logic in > openscad > Date: Wed, 26 Oct 2011 22:51:33 +0000 > > > Thanks for the suggestions. I won't always be doing '&' with such a nice nu= > mber=2C so having a generalized form that can deal with any bit pattern wou= > ld be extra useful. The shifting=2C and general bit twiddling would be usef= > ul as well. Perlin Noise is a concept from 3D Graphics. It is a basic comp= > onent in procedural texture mapping. Whenever you see marble=2C wood=2C or= > even landscapes that are generated algorithmically=2C there is probably us= > age of Perlin noise=2C or something like it. I've played around with some s= > imple procedural texture mapping in OpenScad. Now I want to up the game so= > that I can see prettier pictures in the viewer. -- William > Again, I do not know of Perlin Noise. Even so, let me suggest a few possibilities. (1) If it is your purpose to use a linearly distributed deterministic psuedo random number generator using the operations available, there are many possibilities. Those of the form (a*x) mod M probably have the most history attached to them. The resulting formulae are simple & easily understandable. (2) If it is your purpose to implement functions of the form (2^a*x) & (2^M-1) you now have a generalization of the formula I gave you, namely (2^a)*(x mod 2^(M-a)) as an equivalent calculation. The resulting formulae are also simple & easily understandable. (3) If it is your purpose to implement some more general bit-twiddling, let me suggest that ANY arithmetic formula no matter how inelegant or obscure can be hidden in some formulae named bitAnd(x,y) or unsignedBitShift(x,s). (4) Finally, if it is your purpose to find some 'efficient' implementation of bit-twiddling in a language that does not have it as a primitive, let me suggest that this is a false optimization. Whatever it is you are using Perlin functions for, OpenSCAD spends orders of magnitude more time & space rendering the resulting solid than it will in even the most inelegant & inefficient bit-twiddling conceivable. We would help everyone far far more spending time improving THAT than diddling the integer arithmetic. You know your purpose. So you should know the best approach to achieving it. Yours, Dan |
Free forum by Nabble | Edit this page |