def virtualHelixMesh(pos1,
                     pos2,
                     radius: float = 1.0,
                     color: str = 'red') -> Mesh:
    pos1 = np.array(pos1, dtype=float)
    pos2 = np.array(pos2, dtype=float)
    delta = pos2 - pos1
    dist = np.linalg.norm(delta)
    c_geometry = CylinderBufferGeometry(radiusTop=radius,
                                        radiusBottom=radius,
                                        height=dist,
                                        radiusSegments=16)
    # print(c_geometry.height)
    v2 = normalize(delta)

    # NOTE default direction in Three.js is the Y direction for geometry
    v1 = np.array((0, 1, 0))

    # Calculate angle between Vectors
    angle = math.atan2(np.dot(np.cross(v1, v2), v1), np.dot(v1, v2))

    normal_vec = normalize(np.cross(v1, v2)).tolist()

    mid_point = (pos2 + pos1) / 2
    xAxis = [mid_point[0], 0, 0]
    yAxis = [0, mid_point[1], 0]
    zAxis = [0, 0, mid_point[2]]

    mesh = Mesh(geometry=c_geometry, material=MeshLambertMaterial(color=color))

    rotm = rotationMatrix(v2)
    mesh.setRotationFromMatrix(threeMatrix(rotm))
    mesh.position = mid_point.tolist()
    return mesh, mid_point
Beispiel #2
0
def create_face_geom(
        coord_vals: SoCoordValsListType,
        face_indices: SoIndicesListType,
        face_color: SoVectorType,
        transparency: float,
        translation: SoVectorType = None,
        quaternion: SoQuaternionType = None
) -> ThreeJSSceneGraphObjectListType:
    """
    Returns a pythreejs `Mesh` object that consists of the faces given by
    face_indices and the coord_vals.
    
    Additionally the attributes `Mesh.default_material` and `Mesh.geometry.default_color`
    will be set before returning the Mesh. Those attributes contain references to the
    default colors and materials that are set in this function to restore later changes.
    """
    vertices = np.asarray(coord_vals, dtype='float32')
    faces = np.asarray(face_indices, dtype='uint16')

    normals = compute_normals(faces, vertices)

    faces = faces.ravel()
    vertexcolors = np.asarray([face_color] * len(coord_vals), dtype='float32')

    face_geometry = BufferGeometry(
        attributes=dict(position=BufferAttribute(vertices, normalized=False),
                        index=BufferAttribute(faces, normalized=False),
                        normal=BufferAttribute(normals, normalized=False),
                        color=BufferAttribute(vertexcolors, normalized=False)))
    # this is used for returning to original state after highlighting
    face_geometry.default_color = vertexcolors

    # BUG: This is a bug in pythreejs and currently does not work
    #faceGeometry.exec_three_obj_method('computeFaceNormals')
    #faceGeometry.exec_three_obj_method('computeVertexNormals')
    col = so_col_to_hex(face_color)
    material = MeshPhongMaterial(color=col,
                                 transparency=transparency,
                                 depthTest=True,
                                 depthWrite=True,
                                 metalness=0)
    object_mesh = Mesh(
        geometry=face_geometry,
        material=material,
        position=[0, 0, 0]  # Center the cube
    )
    object_mesh.default_material = material

    if quaternion:
        object_mesh.quaternion = quaternion
    if translation:
        object_mesh.position = translation
    return [object_mesh]
Beispiel #3
0
def virtualHelixMesh(   pos1,
                        pos2,
                        radius: float = 1.0,
                        color: str = 'red') -> Mesh:
    pos1 = np.array(pos1, dtype=float)
    pos2 = np.array(pos2, dtype=float)
    delta = pos2 - pos1
    dist = np.linalg.norm(delta)
    c_geometry = CylinderBufferGeometry(  radiusTop=radius,
                                            radiusBottom=radius,
                                            height=dist,
                                            radiusSegments=16
                                    )
    # print(c_geometry.height)
    v2 = normalize(delta)

    # NOTE default direction in Three.js is the Y direction for geometry
    v1 = np.array((0, 1, 0))

    # Calculate angle between Vectors
    angle = math.atan2(
                np.dot(np.cross(v1, v2), v1),
                        np.dot(v1, v2)
            )

    normal_vec = normalize(np.cross(v1, v2)).tolist()

    mid_point = (pos2 + pos1) / 2
    xAxis = [mid_point[0], 0, 0]
    yAxis = [0, mid_point[1], 0]
    zAxis = [0, 0, mid_point[2]]

    mesh = Mesh(geometry=c_geometry,
                material=MeshLambertMaterial(color=color))

    rotm = rotationMatrix(v2)
    mesh.setRotationFromMatrix(threeMatrix(rotm))
    mesh.position = mid_point.tolist()
    return mesh, mid_point