Numpy divide doesn't return floats - list

When I run:
np.divide(np.array([0, 1, 2, 3, 4]),np.array([2, 2, 4, 4, 4]))
OR
np.array([0, 1, 2, 3, 4])/np.array([2, 2, 4, 4, 4])
OR
np.true_divide(np.array([0, 1, 2, 3, 4]),np.array([2, 2, 4, 4, 4]))
The output I get:
array([0., 0., 0., 1., 1.])
Even when the numbers are specified as floats like [0.0, 1.0, 2.0, 3.0, 4.0], the result is the same.
Expected output:
array([0., 0.5, 0.5, 0.75, 1.0])
I am unable to understand why the result is the way it is.

The print precision was set to precision=0.
Took me a while to figure that out!
Got fixed when I did:
np.set_printoptions(precision=2)

Related

why does pyrr.Matrix44 translation appear to be column-major, and rotation row-major?

consider the following:
>>>Matrix44.from_translation( np.array([1,2,3]))
Matrix44([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[1, 2, 3, 1]])
>>> Matrix44.from_x_rotation(0.5 * np.pi)
Matrix44([[ 1.0, 0.0, 0.0, 0.0],
[ 0.0, 0.0, -1.0, 0.0],
[ 0.0, 1.0, 0.0, 0.0],
[ 0.0, 0.0, 0.0, 1.0]])
The translation matrix shows that the layout of the matrix is column-major, but the rotation matrix, confusingly, suggests that it is row-major, if you consider that the standard right-hand 3x3 rotation matrix around X in row-major notation reads:
0.0 0.0 0.0
0.0 cos(a) -sin(a)
0.0 sin(a) cos(a)
As seems to be the result returned by from_x_rotation.
Does anyone know if this is a bug, or am I misinterpreting something?

Python process increasing memory RAM usage

I have been using python some months and this problem just appear two or three days ago.
At this moment I am running this code in the IDLE 3.4.3 python GUI :
x = [1,2,3,4,5]
for i in x:
x.append((i * (i + 1))/2)
print(x)
But this hasnt output or error(is not the only one), the console just is opened and its waiting(like thinking)
and then i check the process in task admin and i see that the process start in 30-35 mb
and after One or Two minutes the process is consuming:
I have not installed new software or something with the OS, this happen in two different laptops(W7 and W10), could be this code, I know(its works if I create a new empty list) but what about other simple instructions like 1+1
I have tried with diferent IDEs, and python versions including architecture.
First I was using iPython notebook and Spyder because I need to plot and Anaconda comes with everything ready, but the kernel always said Busy and without output; I restart, interrupt, new kernel but doesnt work and this happen just this week, because i was working perfectly so I had to remove it because this is happening.
Someone has idea what is happening?
To see what is happening, let me modify your code slightly
x = [1,2,3,4,5]
for i in x:
x.append((i * (i + 1))/2)
print(x)
if len(x) > 20:
break
print(x)
The output looks like this
[1, 2, 3, 4, 5, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0, 7260.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0, 7260.0, 1.0]
append simply adds a new value to the end of a list, so what you've done is created an infinite loop where x is just going to grow bigger and bigger. Basically every time the iterator draws a number from x a new number is added to the end of x, so the iterator will always have 5 more numbers to draw before it completes.
As the iterator keeps running, x keeps getting bigger and bigger and consuming more and more memory.
To fix this infinite loop you can either store the results in a different list or have a break condition like I used above if you really want the results to be appended to x. Also, if you are meaning to replace the values in x you can do something like this
x = [1,2,3,4,5]
for i, v in enumerate(x):
x[i] = (v * (v + 1))/2
print(x)
which outputs
[1.0, 3.0, 6.0, 10.0, 15.0]
Hope that helps.

Incorrectly rendering Isocahedron in OpenGL

I'm trying to draw an Isocahedron in OpenGL with c++. I keep getting close but having some missing faces. I have found 3 different sets of vertex/index data on multiple sites, most often the data listed below
float X = 0.525731112119133606f;
float Z = 0.850650808352039932f;
float temppts[12][3] = { { -X, 0.0f, Z }, { X, 0.0f, Z }, { -X, 0.0f, -Z }, { X, 0.0f, -Z },
{ 0.0f, Z, X }, { 0.0f, Z, -X }, { 0.0f, -Z, X }, { 0.0f, -Z, -X },
{ Z, X, 0.0f }, { -Z, X, 0.0f }, { Z, -X, 0.0f }, { -Z, -X, 0.0f } };
GLushort tempindicies[60] =
{ 1, 4, 0, 4, 9, 0, 4, 5, 9, 8, 5, 4, 1, 8, 4,
1, 10, 8, 10, 3, 8, 8, 3, 5, 3, 2, 5, 3, 7, 2,
3, 10, 7, 10, 6, 7, 6, 11, 7, 6, 0, 11, 6, 1, 0,
10, 1, 6, 11, 0, 9, 2, 11, 9, 5, 2, 9, 11, 2, 7};
This code is adapted from a book and multiple sites display it working, though they are drawing immediate and I'm using vbo/ibo. Can anyone point me to some working vertex/index data or tell me what is going wrong transferring this to buffer objects? The three different data all have differently incorrect icosahedrons, each with different faces missing.
I have checked over my bufferData calls many times and tried several drawing modes ( TRIANGLES, TRIANGLE_STRIP ... ) and am convinced the index data is wrong somehow
I used the mesh coordinates (vertices) and the triangle connectivity from Platonic Solids (scroll down to icosahedron). I've pasted a screen shot from that file below. When calling glDrawElements I used GL_TRIANGLES.
Icosahedron
Another thing to watch out for is back face culling. Initially switch off backface culling.
glDisable(GL_CULL_FACE);

OpenGL calculating Normal of a custom shape

i have a shape with the following vertexes and faces:
static Vec3f cubeVerts[24] = {
{ -0.5, 0.5, -0.5 }, /* backside */
{ -0.5, -0.5, -0.5 },
{ -0.3, 4.0, -0.5 },
{ -0.3, 3.0, -0.5 },
{ -0.1, 5.5, -0.5 },
{ -0.1, 4.5, -0.5 },
{ 0.1, 5.5, -0.5 },
{ 0.1, 4.5, -0.5 },
{ 0.3, 4.0, -0.5 },
{ 0.3, 3.0, -0.5 },
{ 0.5, 0.5, -0.5 },
{ 0.5, -0.5, -0.5 },
{ -0.5, 0.5, 0.5 }, /* frontside */
{ -0.5, -0.5, 0.5 },
{ -0.3, 4.0, 0.5 },
{ -0.3, 3.0, 0.5 },
{ -0.1, 5.5, 0.5 },
{ -0.1, 4.5, 0.5 },
{ 0.1, 5.5, 0.5 },
{ 0.1, 4.5, 0.5 },
{ 0.3, 4.0, 0.5 },
{ 0.3, 3.0, 0.5 },
{ 0.5, 0.5, 0.5 },
{ 0.5, -0.5, 0.5 }
};
static GLuint cubeFaces[] = {
0, 1, 3, 2, /*backfaces*/
2, 3, 5, 4,
4, 5, 7, 6,
6, 7, 9, 8,
8, 9, 11, 10,
12, 13, 15, 14, /*frontfaces*/
14, 15, 17, 16,
16, 17, 19, 18,
18, 19, 21, 20,
20, 21, 23, 22,
0, 2, 14, 12, /*topfaces*/
2, 4, 16, 14,
4, 6, 18, 16,
6, 8, 20, 18,
8, 10, 22, 20,
1, 3, 15, 13, /*bottomfaces*/
3, 5, 17, 15,
5, 7, 19, 17,
7, 9, 21, 19,
9, 11, 23, 21,
0, 1, 13, 12, /*sidefaces*/
10, 11, 23, 22
};
and i want to get its normal like this:
static Vec3f cubeNorms[] = {
{ 0, 1, 0 },
{ 0, 1, 0 },
{ 0, 1, 0 },
{ 0, 1, 0 }
};
Can someone tell me how to calculate its normal and putting it inside an array so i can use all these together like this, i know something is wrong with my normal, because lighting on my shape is not right and i am also not sure if its the right way of setting up the normal, just one example is fine, ive been reading heaps of normal calculations and still can't figure out how to do it.
static void drawCube()
{
//vertexes
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, cubeVerts);
//norms
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, cubeNorms);
//faces
glDrawElements(GL_QUADS, 22 * 4, GL_UNSIGNED_INT, cubeFaces);
}
I'm going to assume your faces are counter-clockwise front-facing - I don't know if that's the case - and the quads are, of course, convex and planar.
For a face, take vertices {0, 1, 2}. I don't know the Vec3f specification (or if it's a class or C struct), but we can find the normal for all vertices in the quad with:
Vec3f va = v0 - v1; // quad vertex 1 -> 0
Vec3f vb = v2 - v1; // quad vertex 1 -> 2
Vec3f norm = cross(vb, va); // cross product.
float norm_len = sqrt(dot(norm, norm));
norm /= norm_len; // divide each component of norm by norm_len.
That gives you a unit normal for that face. If vertices are shared, and you want to give the model the perception of curvature using lighting, you'll have to decide what value of the normal should be 'agreed' upon. Perhaps the best starting point is to simply take an average of the face normals at that vertex - and rescale the result to unit length as required.

Tesselation in OpenGL

I'm not getting the expected filling when tessellating some self-intersecting polygons.
For example using some simple sample code demonstrating OpenGL tessellation that can be downloaded from here
and making a couple of very simple modifications:
firstly, at line 272 change the x & y coords of the self-intersection polygon to ...
GLdouble star[5][6] = { {1.0, 2.0, 0, 1, 0, 0}, // 0: x,y,z,r,g,b
{0.0, 2.0, 0, 0, 1, 0}, // 1:
{3.0, 0.0, 0, 1, 0, 1}, // 2:
{0.0, 4.0, 0, 1, 1, 0}, // 3:
{-1.0, 0.0, 0, 0, 0, 1} }; // 4:
and at line 296, change the filling rule to EvenOdd...
gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
and compile and run and I get the following image which shows what I don't believe is the correct filling. (I'm not seeing the triangular hole in the third object.)
Here's what the polygon without filling looks like ...