Python形状:.equals函数并不总是有效:

时间:2016-09-05 10:10:42

标签: python shapely

我对这个匀称的库有一些问题。 现在等于函数似乎并不总是有效:

glm::vec3 light_direction = glm::normalize(glm::vec3(1.f, 1.f, 3.f));
glm::vec3 light_position = glm::vec3(1.0f, 1.0f, 0.0f);
glm::vec3 light_intensity = glm::vec3(1.f, 1.f, 1.f);

glDepthMask(GL_FALSE);
glUseProgram(skyProgram);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureCube);
glUniform1i(glGetUniformLocation(skyProgram, "textureSampler"), 3);
glUniform3fv(glGetUniformLocation(skyProgram, "light_direction"), 1, &light_direction[0]);
glUniform3fv(glGetUniformLocation(skyProgram, "light_intensity"), 1, &light_intensity[0]);
glUniform3fv(glGetUniformLocation(skyProgram, "view_position"), 1, &camera_position[0]);
glUniform1f(glGetUniformLocation(skyProgram, "shininess"), 60); // shininess = 60, range is [0.0 - 128.0] 
glUniformMatrix3fv(glGetUniformLocation(skyProgram, "view_rot"), 1, GL_FALSE, &(glm::mat3(glm::inverse(view)))[0][0]);

结果有误。 知道为什么吗?

2 个答案:

答案 0 :(得分:2)

来自docs:

  

Polygon构造函数有两个位置参数。第一个是(x,y [,z])点元组的有序序列,其处理方式与LinearRing情况完全相同。

因此,在创建Polygon

之前,首先尝试对它们进行排序(元组)
>>> pol1_coords = ([0, 1, 2], [3, 4, 5], [6, 7, 8])
>>> pol2_coords = ([0, 1, 2], [6, 7, 8], [3, 4, 5])
>>> Polygon(sorted(pol1_coords)) == Polygon(sorted(pol2_coords))
True

相反,您仍然会遇到此问题:

>>> Polygon(pol1_coords) == Polygon(pol2_coords)
False

答案 1 :(得分:1)

正如@ewcz在评论中所说,这是因为Shapely只适用于XY平面中的2D几何体。这里忽略了Z坐标。当投影到XY平面时,这些是无效的多边形,因此Shapely不准备同意它们是相等的。如果删除(不必要的)x坐标,它可以正常工作:

from shapely.geometry import Polygon

poly1 =  Polygon(([220.0, 400, 500], [220.0, 20, 500], [220.0, 20, 0], [220.0, 400, 0], [220.0, 400, 500]))
poly2 =  Polygon(([220.0, 20, 500], [220.0, 400, 500], [220.0, 400, 0], [220.0, 20, 0], [220.0, 20, 500]))

print (poly1.equals(poly2)) # False

print poly1.is_valid  # False
print poly2.is_valid  # False

poly1 =  Polygon(([400, 500], [20, 500], [20, 0], [400, 0], [400, 500]))
poly2 =  Polygon(([20, 500], [400, 500], [400, 0], [20, 0], [20, 500]))

print (poly1.equals(poly2)) # True

print poly1.is_valid  # True
print poly2.is_valid  # True

poly1 =  Polygon(([220.0, 400], [220.0, 20], [220.0, 20], [220.0, 400], [220.0, 400]))
poly2 =  Polygon(([220.0, 20], [220.0, 400], [220.0, 400], [220.0, 20], [220.0, 20]))

print (poly1.equals(poly2)) # False

print poly1.is_valid  # False
print poly2.is_valid  # False