How can I antialias circle contained in a numpy array? - python

I'm trying to produce an antialiased circle in in pygame 1.9.1release, so I do not have access to pygame.gfxdraw, nor do I really feel like using this submodule since it is clearly marked as being experimental.
This having been said, I would like to produce an antialiased circle, so I was wondering if there was a way to achieve this using pygame's ability to reference pixels in numpy arrays.
Below is an example of such a circle. The pickled numpy array containing the circle as a 3D array (with the 3rd dimension represeting RGB values) can be found here.
My question is as follows: How can I smooth the edges of the circle I have draw, preferably using numpy?

Related

Ideas on filling each shape in triangle mesh with values

I'm trying to write python code that creates a 2D NumPy array (e.g. 128x128) containing a mesh of raster triangles in which each triangle has a random value.
Does anyone know of any python package that could do this?
I tried using this triangle package (https://rufat.be/triangle/examples.html) but i can't seem figure out how to link that to my problem.
The ideal results would be something like this https://people.sc.fsu.edu/~jburkardt/data/triangle_files/double_hex2.2.ele.png)
but each triangle will show different color because it has different values.
Generate k random points inside the rectangle (0,0,128,128) and generate the Delaunay Triangulation of the set of the points plus the four corners of the rectangle.
Construct the NumPy array from the list of triangles by either
Implementing the filled triangle rasterization algorithm of your choice -- e.g. Bresenham's -- using a NumPy array as the target.
or more realistically, painting the triangles into a 128x128 pixel bitmap using a Python raster painting library of your choice and then converting the bitmap to a NumPy array.
Another idea would be to start with a tesselation of equilateral triangles with half triangles along two edges that cover a rectangle, stretch that to cover a square, randomly perturb the vertices, and then do (1) or (2) above ... but this will look less random in that all the internal vertices will have six triangles around them.

Numpy- how to sample a line in world coordinates to pixel coordinates

I have a line given as
x1,x2, y1,y2.
In WORLD coordinates.
Assuming we place the image plane, whose center (or corner) is at xc,yc, and whose resolution is w,h pixels.
I would like to get an efficient way of getting the list of pixels of the given line, sampled at the picture's pixels it intersects
I am aware of the bresenham algorithm, but a pythonic implementation is slow.
Is there a numpy way of doing this?
Is there another python library for doing this?
Is there a way to do this for many lines, all in some vectorized way?
Thanks!

How to create the vision of a 2D camera looking at a 2D world

In a 3D world, a camera looking out will create a 2D representation of what it sees, AKA a picture. Looking at a 3D cube would produce this 2D image:
However, inside a 2D world, a camera looking out will create a 1D line of pixels that will represent what it sees.
The camera in this scene:
Would produce this 1D "image":
Think of the camera looking out. It would be unable to see all of the pink shape, because most of it is obstructed be the red shape. It would only see the part that is unobstructed. Also, objects further away appear smaller.
How can I create the view of a 2D camera looking out at a 2D world, creating a 1D image?
I am looking for a method, preferably in Python, to accomplish this. I am trying to do this for some 2D creatures I am simulating. I want their input to be 1D array that represents their view of the world.
Pick a focal point at the creature. Draw a circle around it. Subdivide the circle for each pixel in your image resolution, and cast a ray from the focal point to the circle point and out into the world. Find the points of intersection between that ray and the world objects. Get the color of the closest one to set the color in the image at that pixel. Repeat for each pixel.
This gives a 360-degree perspective view, which might simplify things for a simulated creature. If you want a more directional view, just use an arc instead of a circle. 45 degrees seems reasonable.

How to flatten 3D object surface into 2D array?

I've got 3D objects which are represented as numpy arrays.
How can I unfold the "surface" of such objects to get a 2D map of values (I don't care about inner values)?
It's similar to unwrapping globe surface, but the shape is varied from case to case.
This is a vertices problem. Each triangle on the model is a flat surface that can be mapped to a 2D plane. So the most naive solution without any assumed structure would be to:
for triangle in mesh:
// project to plane defined by normal to avoid stretching
This solution is not ideal as it places all of the uv's on top of each other. The next step would be to spread out the triangles to fill a certain space. This is the layout stage that defines how the vertices are layed out in the 2D space.
Usually it is ideal to fit the UV's within a unit square. This allows for easy UV mapping from a single image.
Option: 2
You surround the object with a known 2D mapped shape and project each triangle onto the shape based on its normal. This provides a mechanism for unwrapping the uv's in a structured manor. An example object would be to project onto a cube.
Option: 3
consult academic papers and opensource libraries/tools like blender:
https://wiki.blender.org/index.php/Doc:2.4/Manual/Textures/Mapping/UV/Unwrapping
blender uses methods as described above to unwrap arbitrary geometry. There are other methods to accomplish this as described on the blender unwrap page. The nice thing about blender is that you can consult the source code for the implementation of the uv unwrap methods.
Hope this is helpful.

Calculating the fraction of each cell in a grid overlapped by a 2D object

I have an arbitrary rectangular Cartesian grid divided into potentially 10^6 or so rectangular cells. (Arbitrary means that the $x$ grid is along points $x_1,...x_n$ and the same goes for the $y$ grid.) I would like to draw an arbitrary object on top of it (say a rotated rectangle, or a circle), and efficiently calculate what fraction of each cell is overlapped by the object: if the cell is entirely inside the bounds of the object, 1.0; if the cell is entirely outside, 0.0; if half of the cell is covered by the object, 0.5. If you displayed this as an image and scaled it where 1 is black and 0 is white, the result would look like an antialiased drawing of the black object.
My application for this question is in Python, and it seems like this capability might be provided by some existing graphics library. Is there a Python module that will test for the fractional intersection of a rectangle and an arbitrary object? Is there a Python library that can at least efficiently test if a point is inside an arbitrary object like a rotated rectangle?
You could use PyCairo, which has fast native routines to do its drawing. It's antialiased by default.
Implementing the drawing algorithms in Python would be very slow.
To find the area of a trapezoid resulting from a polygon-square intersection, you can follow the process described by Sean Barrett at https://nothings.org/gamedev/rasterize/
The shapely Python library can find the area of a trapezoid and perform point-in-object tests. However, for best performance this sounds like something that you'd want to write in C/C++ and provide numpy bindings.

Categories