Esempio n. 1
0
 def test_unit_vector_1d(self):
     v = 0.1
     self.assertEqual(unit_vector(v), 1.0)
     v = -0.1
     self.assertEqual(unit_vector(v), -1.0)
     v = [0.1]
     self.assertEqual(unit_vector(v), [1.0])
     v = [-0.1]
     self.assertEqual(unit_vector(v), [-1.0])
     v = np.array([0.1])
     self.assertEqual(unit_vector(v), [1.0])
Esempio n. 2
0
 def _set_basis(self, basis):
     """
     Sets basis for the cartesian coordinate system. Basis will be converted to 3x3 array.
     Each basis vector will be normed and placed in separate row like this:
       x  y  z
     1 x1 y1 z1
     2 x2 y2 z2
     3 x3 y3 z3
     :param basis: 3x3 numpy array.
     """
     if basis is None:
         basis = np.identity(3, dtype=np.float)
     if isinstance(basis, np.ndarray):
         try:
             basis = basis.astype(np.float)
         except:
             raise ValueError('only numeric basis coordinates are supported')
         if basis.shape == (3, 3) or basis.size == 9:
             basis = basis.reshape((3, 3))
             for i in range(3):
                 basis[i] = unit_vector(basis[i])
             if not np.allclose(np.dot(basis[0], basis[1]), [0]):
                 raise ValueError('only orthogonal vectors accepted')
             if not np.allclose(np.cross(basis[0], basis[1]), basis[2]):
                 raise ValueError('only right-hand basis accepted')
             self._rotation.rotation_matrix = basis.T
         else:
             raise ValueError('complete 3D basis is needed')
Esempio n. 3
0
def line_between_two_points(coordinate_system, point1, point2):
    direction = point2 - point1
    distance = np.sqrt(np.dot(direction, direction))
    v = unit_vector(direction)
    line_coordinate_system = Cartesian(basis=np.copy(coordinate_system.basis), origin=np.copy(coordinate_system.origin),
                                       name='Line path coordinate system')
    path = Line(name='Line Path', coordinate_system=line_coordinate_system,
                origin=point1, a=v[0], b=v[1], c=v[2],
                start=0, stop=distance)
    return path
Esempio n. 4
0
 def test_unit_vector_random_vector(self):
     max_dimensions = 100
     for dim in range(max_dimensions):
         v = np.random.random(dim+1) * 100
         np.testing.assert_allclose(unit_vector(v), v / np.sqrt(np.dot(v, v)))