Ideas on filling each shape in triangle mesh with values - python

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.

Related

Cutting a matrix for a certain complex shape - Python

I ran into a problem that I don't know how to address, if anyone has any ideas I would be very grateful.
So I have a NxM matrix and number of points (minimum 4) that represent a shape resting on the matrix as show in the figure (Each point is represented by a number, ignore the black/white points):
I know how to match each point to the x,y coordinate inside the matrix.
But let's say I want to calculate the average of the values inside the polygon, how can I do it?
Many thanks to all the helpers
I tried to parametrize the polygon to a new square, but without success...
You need to use a polygon filling algorithm (also called scan conversion). In a nutshell, you intersect the polygon with all horizontals through the matrix rows and find intervals covered by the inside of the polygon. Accumulate the matrix values spanned by these intervals.

How to determine the first triangle (out of a set of triangles) passed through by a 3-D ray?

I am trying to solve the following problem in Python. The problem comes from an image processing problem when i use the Finite Element Method.
In my problem, I have a set of triangles and a ray. Each triangle consists of three 3-D points, and the ray is in the form of a 3-D point and a 3-D vector. How can I determine the first triangle that is passed through by the ray? Now I do not even have an algorithm for this. Any inputs will be appreciated.
The first thing I would do, is translate the whole data set, subtracting the 3D ray origin. Then rotate the data set so that the ray's 3D vector aligns with the X-axis. See How to find the orthonormal transformation that will rotate a vector to the x axis?.
Now the problem has been converted to filter for triangles that cross the X-axis with a non-negative X-coordinate, and among those find the one whose crossing point has the minimal X-coordinate. So
For each triangle check where its plane crosses the X-axis. See Determine point of interesction of plane with axis given points of plane
Then throw away the triangles where that crossing point (on the X-axis) is not within the bounds of the triangle (check for each of the three edges that this point is at the "inner" side of it). See Check whether a point is within a 3D Triangle
Throw away the triangles whose crossing point has a negative X-coordinate.
Among the remaining triangles (that really cross the X-axis on the positive side) find the one with the minimum crossing point in terms of X-coordinate.

Python library for rotation and translation on a seesaw-like object

I'd like to do calculations on the 3D positions on both end's of a rigid object (see spot where the children are usually sitting in image below). The geometrical situation of the rigid object corresponds to a seesaw. Rotation has to be possible on three axes and can be represented by a ball bearing, which initially is located at the middle of the rod.
The input to the desired function should consist of three rotations performed at the position of the ball bearing, three translations along the bearing and the initial 3D positions of both ends of the object.
The output needs to be the calculated new 3D positions of both ends.
Does anyone know a python library that does provide functionalities regarding this issue?
I've just found out that Open3D has implemented exactly what I was looking for. As it is working with point clouds, all that needs to be done is to create two points in 3D space, define a rotation matrix and the center (= ball bearing in this case). The function "rotate" then calculates the altered positions of the rotated points. This also works for translation.
# Rotation
pcd.rotate(r, center = (0,0,0))
# Translation
pcd.translate(t)
With r = rotation matrix (3x3) and t = translation matrix (3x1).

Python - Method to convert random points to line?

When I have some points its position is random as drawn below.
I want to dynamically draw lines with some restrictions.
1) No points in selected region.
2) The triangles are at acute angle.
3) Points are in X/Y (2D) plane.
So points are processed & divided therefore...
Can I find advice about any appropriate math solutions or even libraries?
You will want to look up Delaunay triangulation & Voronoi diagram; you can find implementation of these objects in scipy.interpolate; I think these constructs are what you are looking for.

How can I antialias circle contained in a numpy array?

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?

Categories