Last active
August 29, 2015 14:24
-
-
Save eayoungs/e9f528d64b3f4425edc9 to your computer and use it in GitHub Desktop.
eppy_failed_test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from math import acos as arccos | |
from math import sqrt as sqrt | |
from tinynumpy import array as array | |
from pytest_helpers import almostequal | |
import math | |
def angle2vecs(vec1,vec2): | |
# vector a * vector b = |a|*|b|* cos(angle between vector a and vector b) | |
dot = np.dot(vec1,vec2) | |
vec1_modulus = sqrt((vec1*vec1).sum()) | |
vec2_modulus = sqrt((vec2*vec2).sum()) | |
if (vec1_modulus * vec2_modulus) == 0: | |
cos_angle = 1 | |
else: cos_angle = dot / (vec1_modulus * vec2_modulus) | |
return math.degrees(arccos(cos_angle)) | |
#unit normal vector of plane defined by points a, b, and c | |
def unit_normal(a, b, c): | |
x = np.linalg.det([[1,a[1],a[2]],[1,b[1],b[2]],[1,c[1],c[2]]]) | |
y = np.linalg.det([[a[0],1,a[2]],[b[0],1,b[2]],[c[0],1,c[2]]]) | |
z = np.linalg.det([[a[0],a[1],1],[b[0],b[1],1],[c[0],c[1],1]]) | |
magnitude = (x**2 + y**2 + z**2)**.5 | |
mag = (x/magnitude, y/magnitude, z/magnitude) | |
if magnitude < 0.00000001: mag = (0,0,0) | |
return mag | |
## end of http://code.activestate.com/recipes/578276/ }}} | |
def tilt(poly): | |
"""Tilt of a polygon poly""" | |
N = len(poly) - 1 | |
vec = unit_normal(poly[0], poly[1], poly[N]) | |
vec_alt = array([vec[0], vec[1], vec[2]]) | |
vec_z = array([0,0,1]) | |
# return (90 - angle2vecs(vec_alt, vec_z)) # update by Santosh | |
return (angle2vecs(vec_alt, vec_z)) | |
def test_tilt(): | |
"""test the tilt of a polygon poly""" | |
data = (([(0,0,0), (1,0,0), (1,1,0), (0,1,0)],0),# polygon, answer, | |
#([(0,0,0), (5,0,0), (5,0,8), (0,0,8)],90), | |
#([(0,0,0), (1,0,0), (1,1,1), (0,1,1)],45), | |
#([(3.571913,-9.390334,1.487381), (10.905826,-6.194443,1.487381), | |
#(8.998819,-1.818255,0.0), (1.664906, -5.014146, 0.0)],90-72.693912), | |
) | |
for poly,answer in data: | |
result = tilt(poly) | |
assert almostequal(answer, result, places=3) == True | |
test_tilt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment