holoviz-topics / imagen

ImaGen: Generic Python library for 0D, 1D and 2D pattern distributions
https://imagen.holoviz.org/
BSD 3-Clause "New" or "Revised" License
31 stars 16 forks source link

Allow polar coordinates for pattern generators #14

Open sf-issues opened 11 years ago

sf-issues commented 11 years ago

Converted from SourceForge issue 3025117, submitted by jbednar Submit Date: 2010-07-04 17:03 GMT

Stuart Wilson found it useful to be able to place a PatternGenerator at specified (radius,angle) polar coordinates, and modified PatternGenerator to allow this:

 rotation = param\.Number\(default=0\.0,softbounds=\(0\.0,2\*pi\),precedence=0\.40,doc="""
     Angle of polar translation from x,y origin\."""\)

 radius = param\.Number\(default=0\.0,softbounds=\(0\.0,1\.0\),precedence=0\.50,doc="""
     Radius of polar translation from x,y origin\."""\)

 rotate\_around\_square = param\.Boolean\(default=False,doc="""
     Make polar rotation follow a square rather than a circle\."""\)

...

def __create_and_rotate_coordinate_arrays(self, x, y, orientation, rotation, radius, rotate_around_square): """ Create pattern matrices from x and y vectors, and rotate them to the specified orientation. """

     \# Polar Coordinate translation added by Stuart
     if rotate\_around\_square==True:
         x\-=round\(cos\(rotation\)\)\*radius
         y\-=round\(sin\(rotation\)\)\*radius
     else:
         x\-=cos\(rotation\)\*radius
         y\-=sin\(rotation\)\*radius

     \# Using this two\-liner requires that x increase from left to
     \# right and y decrease from left to right; I don't think it
     \# can be rewritten in so little code otherwise \- but please
     \# prove me wrong\.

     pattern\_y = subtract\.outer\(cos\(orientation\)\*y, sin \(orientation\)\*x\)
     pattern\_x = add\.outer\(sin\(orientation\)\*y, cos\(orientation\)\*x\)

     return pattern\_x, pattern\_y

Although I can see cases where using polar coordinates would be very useful, I'm not sure that we need to provide it at the PatternGenerator level. (But if anyone disagrees, please generalize the above, put it into PatternGenerator, and let me know that you've done so!

It seems like we could instead provide a simple wrapper PatternGenerator similar to Composite that would accept the polar coordinates, and then internally convert them to Cartesian coordinates to place the wrapped PatternGenerator. Seems like only a few lines to implement that, but because I don't currently have any use for it, I'm filing it as a feature request for someone to pick up if they need it.