~freecad-community/freecad-extras/assembly2

« back to all changes in this revision

Viewing changes to lib3D.py

  • Committer: looooo
  • Date: 2017-04-29 20:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: git-v1:b1dec62143dc055898bad6a73fb53e87cb9f68e2
py3: exceptions + dict.has_key
except Error msg: --> except Error as msg:
dict.has_key(value) --> value in dict

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
    elif abs(v) -1 < allowableNumericalError:
15
15
        return pi/2 if v > 0 else -pi/2
16
16
    else:
17
 
        raise ValueError,"arcsin2 called with invalid input of %s" % v
 
17
        raise ValueError("arcsin2 called with invalid input of %s" % v)
18
18
 
19
19
def arccos2( v, allowableNumericalError=10**-1 ):
20
20
    if -1 <= v and v <= 1:
22
22
    elif abs(v) -1 < allowableNumericalError:
23
23
        return 0 if v > 0 else pi
24
24
    else:
25
 
        raise ValueError,"arccos2 called with invalid input of %s" % v
 
25
        raise ValueError("arccos2 called with invalid input of %s" % v)
26
26
 
27
27
def normalize( v ):
28
28
    return v / norm(v)
216
216
        print('rotation_matrix_to_euler_ZYX_check_answer:')
217
217
        print('    norm(R - euler_ZYX_rotation_matrix( angle1, angle2, angle3)) %e' % error)
218
218
    if error > tol:
219
 
         raise RuntimeError,'rotation_matrix_to_euler_ZYX check failed!. locals %s' % locals()
 
219
         raise RuntimeError('rotation_matrix_to_euler_ZYX check failed!. locals %s' % locals())
220
220
def rotation_matrix_to_euler_ZYX_2(R, debug=False):
221
221
    axis, angle = rotation_matrix_axis_and_angle_2(R)
222
222
    q_1, q_2, q_3, q_0 = quaternion(angle, *axis)
243
243
        msg = 'abs(a % pi) > angle_pi_tol and abs(pi - (a % pi)) > angle_pi_tol'
244
244
        axis, angle = rotation_matrix_axis_and_angle_2( R, errorThreshold=errorThreshold, debug=debug, msg=msg)
245
245
    if numpy.isnan( angle ):
246
 
        raise RuntimeError,'locals %s' % locals() 
 
246
        raise RuntimeError('locals %s' % locals() )
247
247
    return axis, angle
248
248
def rotation_matrix_axis_and_angle_2(R, debug=False, errorThreshold=10**-7, msg=None):
249
249
    w, v = numpy.linalg.eig(R) #this method is not used at the primary method as numpy.linalg.eig does not return answers in high enough precision
264
264
            R_pickle_str = pickle.dumps(R)
265
265
            #R_abs_minus_identity = abs(R) - numpy.eye(3)
266
266
            print(R*R.transpose())
267
 
            raise ValueError, 'rotation_matrix_axis_and_angle_2: no solution found! locals %s' % str(locals())
 
267
            raise ValueError( 'rotation_matrix_axis_and_angle_2: no solution found! locals %s' % str(locals()))
268
268
    return axis, angle
269
269
 
270
270
 
288
288
        print(P)
289
289
        print(' error norm from eye(3) : %e' % error)
290
290
    if error > tol:
291
 
        raise RuntimeError,'plane_degrees_of_freedom check failed!. locals %s' % locals()
 
291
        raise RuntimeError('plane_degrees_of_freedom check failed!. locals %s' % locals())
292
292
 
293
293
def planeIntersection( normalVector1, normalVector2, debug=False, checkAnswer=False ):
294
294
    return normalize ( crossProduct(normalVector1, normalVector2) )
304
304
        error2 = abs(dotProduct( normalVector2, d*t ))
305
305
        if disp:print('    d*(%1.1f) -> error1 %e, error2 %e' % (t, error1, error2) )
306
306
        if error1 > tol or error2 > tol:
307
 
            raise RuntimeError,' planeIntersection check failed!. locals %s' % locals()
 
307
            raise RuntimeError(' planeIntersection check failed!. locals %s' % locals())
308
308
 
309
309
 
310
310
 
587
587
            angle_out = -angle_out
588
588
            axis_out = -axis_out
589
589
        if norm(axis - axis_out) > 10**-12 or norm(angle - angle_out) > 10**-9:
590
 
            raise ValueError, "norm(axis - axis_out) > 10**-12 or norm(angle - angle_out) > 10**-9. \n  in:  axis %s, angle %s\n  out: axis %s, angle %s" % (axis,angle,axis_out,angle_out) 
 
590
            raise ValueError("norm(axis - axis_out) > 10**-12 or norm(angle - angle_out) > 10**-9. \n  in:  axis %s, angle %s\n  out: axis %s, angle %s" % (axis,angle,axis_out,angle_out))
591
591
    print('testing axis_to_azimuth_and_elevation_angles & azimuth_and_elevation_angles_to_axis')
592
592
    for i,testcase in enumerate(testcases):
593
593
        axis, angle = testcase
594
594
        a,e = axis_to_azimuth_and_elevation_angles(*axis)
595
595
        axis_out = azimuth_and_elevation_angles_to_axis( a, e)
596
596
        if norm(axis - axis_out) > 10**-12:
597
 
            raise ValueError, "norm(axis - axis_out) > 10**-12. \n  in:  axis %s \n  azimuth %f, elavation %f \n  out: axis %s" % (axis,a,e,axis_out)
 
597
            raise ValueError("norm(axis - axis_out) > 10**-12. \n  in:  axis %s \n  azimuth %f, elavation %f \n  out: axis %s" % (axis,a,e,axis_out))
598
598
 
599
599
 
600
600
    print('\nchecking distance_between_axes function')
744
744
        print('  quatrian  v_ref %s, v %s, error %1.3e' % ( v, v_ref, error ) )
745
745
 
746
746
        if error > 10**-9 :
747
 
            raise ValueError, 'Failure for v_ref %s, v %s, error %1.3e' % ( v, v_ref, error )
 
747
            raise ValueError('Failure for v_ref %s, v %s, error %1.3e' % ( v, v_ref, error ))
748
748
    print('all test case passed')
749
749
 
750
750
    print('distance between axis and point')
787
787
            prettyPrintArray(U, '  ', '%1.2f')
788
788
            print('U U^T:')
789
789
            prettyPrintArray( W, '  ', '%1.2f' )
790
 
            raise ValueError, 'gram_schmidt_orthonormalization test failed, error %e > 10**-9' % error
 
790
            raise ValueError('gram_schmidt_orthonormalization test failed, error %e > 10**-9' % error)
791
791
    print('..passed')