예제 #1
0
def get_alpha_beta_gamma_from_p(proj_mat, rgt=0):
    """
    Method to get primary (rao/lao), secondary (cran/caud) and tertiary (img_rot) of projection
    matrix, as presented in the thesis.
    :param proj_mat:
    :param rgt: optinalPrameter for debugging
    :return:
    """
    H_xy = np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.float64)
    e_x = np.matrix([1, 0, 0], dtype=np.float64).T
    e_y = np.matrix([0, 1, 0], dtype=np.float64).T
    e_z = np.matrix([0, 0, 1], dtype=np.float64).T
    m_3 = proj_mat[2, 0:3].T / np.linalg.norm(proj_mat[2, 0:3])
    p1 = pg.plane_p3(proj_mat[0, :])
    p3 = pg.plane_p3(proj_mat[2, :])
    v = np.matrix(p1.meet(p3).dir()).T / np.linalg.norm(p1.meet(p3).dir())
    ###alpha
    sign_alpha = np.sign((e_x.T * m_3)[0, 0])
    if sign_alpha == 0: sign_alpha = 1

    alpha_abs = angulation(-e_y, H_xy * m_3)
    alpha = alpha_abs * sign_alpha

    ####beta ###not sure about sign of beta...need to verify this
    ##...the sign is right, but make sure to map it in the correct anatomical angle
    sign_beta = np.sign((-e_z.T * m_3)[0, 0])
    if sign_beta == 0: sign_beta = 1
    beta_abs = angulation(-e_y, rz(alpha).T * m_3)
    # beta_abs = angulation(Rz(alpha) * -e_y, m_3)
    beta = sign_beta * beta_abs
    ####gamma
    gamma_abs = angulation(-e_z, rx(beta).T * v)
    sign_gamma = np.sign(((rx(beta).T * v).T * -e_y)[0, 0])
    if sign_gamma == 0: sign_gamma = 1
    gamma = gamma_abs * sign_gamma
    return (math.degrees(alpha), math.degrees(beta), math.degrees(gamma))
예제 #2
0
def get_detector_edge_points(p, sid, size_u, size_v):
    p_inv = np.linalg.pinv(p)
    plane1 = pg.plane_p3(p[0, :])
    plane2 = pg.plane_p3(p[1, :])
    plane3 = pg.plane_p3(p[2, :])
    source_pos = (plane1.meet(plane2)).meet(plane3)
    plane_dtor = plane3.get_plane_at_distance(sid)
    #### Calculate the corner Points of Detector
    bp00 = pg.point_p2(0, 0).backproject(p_inv)
    bp01 = pg.point_p2(0, size_v).backproject(p_inv)
    bp10 = pg.point_p2(size_u, 0).backproject(p_inv)
    bp11 = pg.point_p2(size_u, size_v).backproject(p_inv)
    p00 = (bp00.join(source_pos)).meet(plane_dtor)
    p01 = (bp01.join(source_pos)).meet(plane_dtor)
    p10 = (bp10.join(source_pos)).meet(plane_dtor)
    p11 = (bp11.join(source_pos)).meet(plane_dtor)
    return (p00, p01, p10, p11)
예제 #3
0
def create_line_and_projection(line,
                               projMatrix,
                               sid,
                               size_u,
                               size_v,
                               p1,
                               p2,
                               ren,
                               createProjectionPlane=False):
    sourcePos = projection.get_source_position(projMatrix)
    det_plane = pg.plane_p3(projMatrix[2, :]).get_plane_at_distance(sid)
    det_Corner = get_detector_edge_points(projMatrix, sid, size_u, size_v)
    line_proj = line.project(projMatrix)
    ###########Draw line on Detector
    line_up = pg.point_p2(0, 0).join(pg.point_p2(0, size_v))
    line_right = pg.point_p2(0, size_v).join(pg.point_p2(size_u, size_v))
    line_left = pg.point_p2(0, 0).join(pg.point_p2(size_u, 0))
    line_down = pg.point_p2(size_u, 0).join(pg.point_p2(size_u, size_v))
    ###extract the two points that cross the detector
    delta = 0.1
    pt_list = [
        line_proj.meet(line_up),
        line_proj.meet(line_right),
        line_proj.meet(line_left),
        line_proj.meet(line_down)
    ]
    det_meet_points = []
    for point in pt_list:
        if point.get_euclidean_point(
        )[0] > 0 - delta and point.get_euclidean_point()[0] < size_u + delta:
            if point.get_euclidean_point(
            )[1] > 0 - delta and point.get_euclidean_point(
            )[1] < size_v + delta:
                det_meet_points.append(point)
    p1_euclid = p1.get_euclidean_point()
    p2_euclid = p2.get_euclidean_point()
    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetCenter(p1_euclid[0], p1_euclid[1], p1_euclid[2])
    sphereSource.SetRadius(10)
    sphere_mapper = vtk.vtkPolyDataMapper()
    sphere_mapper.SetInputConnection(sphereSource.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(sphere_mapper)
    actor.GetProperty().SetColor(1, 0.0, 0.0)
    ren.AddActor(actor)
    p1_euclid = p1.get_euclidean_point()
    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetCenter(p2_euclid[0], p2_euclid[1], p2_euclid[2])
    # sphereSource.SetCenter(0, 0, 0)
    sphereSource.SetRadius(10)
    sphere_mapper = vtk.vtkPolyDataMapper()
    sphere_mapper.SetInputConnection(sphereSource.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(sphere_mapper)
    actor.GetProperty().SetColor(1, 0.0, 0.0)
    ren.AddActor(actor)

    if (len(det_meet_points) < 2):
        print('object is outside of projector. choose a more centered line')
        return
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    bpray_1 = det_meet_points[0].backproject(
        np.linalg.pinv(projMatrix)).join(sourcePos)
    p3D_1 = bpray_1.meet(det_plane)
    points.SetPoint(0,
                    p3D_1.get_euclidean_point()[0],
                    p3D_1.get_euclidean_point()[1],
                    p3D_1.get_euclidean_point()[2])
    bpray_2 = det_meet_points[1].backproject(
        np.linalg.pinv(projMatrix)).join(sourcePos)
    p3D_2 = bpray_2.meet(det_plane)
    points.SetPoint(1,
                    p3D_2.get_euclidean_point()[0],
                    p3D_2.get_euclidean_point()[1],
                    p3D_2.get_euclidean_point()[2])
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(2)
    lines.InsertCellPoint(0)
    lines.InsertCellPoint(1)
    polygon = vtk.vtkPolyData()
    polygon.SetPoints(points)
    polygon.SetLines(lines)
    polygonMapper = vtk.vtkPolyDataMapper()
    polygonMapper.SetInputData(polygon)
    polygonMapper.Update()
    # create actor and add polydatamapper to actor
    det_line_actor = vtk.vtkActor()
    det_line_actor.SetMapper(polygonMapper)
    # det_line_actor.GetProperty().SetColor(0.3, 0.3, 0.3)
    det_line_actor.GetProperty().SetColor(1, 1, 1)
    det_line_actor.GetProperty().SetLineWidth(0.5)
    ren.AddActor(det_line_actor)
    #######Add Line in Space
    p1_euclid = p1.get_euclidean_point()
    p2_euclid = p2.get_euclidean_point()
    ###Now add the actual line
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    points.SetPoint(0, p1_euclid[0], p1_euclid[1], p1_euclid[2])
    points.SetPoint(1, p2_euclid[0], p2_euclid[1], p2_euclid[2])
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(2)
    lines.InsertCellPoint(0)
    lines.InsertCellPoint(1)
    polygon = vtk.vtkPolyData()
    polygon.SetPoints(points)
    polygon.SetLines(lines)
    polygonMapper = vtk.vtkPolyDataMapper()
    polygonMapper.SetInputData(polygon)
    polygonMapper.Update()
    # create actor and add polydatamapper to actor
    line_actor = vtk.vtkActor()
    line_actor.SetMapper(polygonMapper)
    line_actor.GetProperty().SetColor(0, 0, 0)
    line_actor.GetProperty().SetLineWidth(2.5)
    ren.AddActor(line_actor)

    #######Add Projected Line in Space
    p1_det = p1.project(projMatrix).backproject(
        np.linalg.pinv(projMatrix)).join(sourcePos).meet(det_plane)
    p2_det = p2.project(projMatrix).backproject(
        np.linalg.pinv(projMatrix)).join(sourcePos).meet(det_plane)
    ###Now add the actual line
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    points.SetPoint(0,
                    p1_det.get_euclidean_point()[0],
                    p1_det.get_euclidean_point()[1],
                    p1_det.get_euclidean_point()[2])
    points.SetPoint(1,
                    p2_det.get_euclidean_point()[0],
                    p2_det.get_euclidean_point()[1],
                    p2_det.get_euclidean_point()[2])
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(2)
    lines.InsertCellPoint(0)
    lines.InsertCellPoint(1)
    polygon = vtk.vtkPolyData()
    polygon.SetPoints(points)
    polygon.SetLines(lines)
    polygonMapper = vtk.vtkPolyDataMapper()
    polygonMapper.SetInputData(polygon)
    polygonMapper.Update()
    # create actor and add polydatamapper to actor
    line_actor = vtk.vtkActor()
    line_actor.SetMapper(polygonMapper)
    line_actor.GetProperty().SetColor(0, 0, 0)
    ren.AddActor(line_actor)

    ####draw plane
    plane_line_sourcePos = line.join(sourcePos)
    points = vtk.vtkPoints()
    triangles = vtk.vtkCellArray()
    points.SetNumberOfPoints(3)
    points.SetPoint(0,
                    p3D_1.get_euclidean_point()[0],
                    p3D_1.get_euclidean_point()[1],
                    p3D_1.get_euclidean_point()[2])
    points.SetPoint(1,
                    p3D_2.get_euclidean_point()[0],
                    p3D_2.get_euclidean_point()[1],
                    p3D_2.get_euclidean_point()[2])
    points.SetPoint(2,
                    sourcePos.get_euclidean_point()[0],
                    sourcePos.get_euclidean_point()[1],
                    sourcePos.get_euclidean_point()[2])
    triangle = vtk.vtkTriangle()
    triangle.GetPointIds().SetId(0, 0)
    triangle.GetPointIds().SetId(1, 1)
    triangle.GetPointIds().SetId(2, 2)
    triangles.InsertNextCell(triangle)

    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.SetPolys(triangles)
    # mapper
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(polydata)
    # actor
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(0, 0, 0)
    actor.GetProperty().SetOpacity(0.3)
    ren.AddActor(actor)
예제 #4
0
def vis_foreshortening(P,
                       line,
                       sid,
                       point_on_line,
                       size_v,
                       ren,
                       min_vis_degree=4):
    fs_ang = evaluation_methods.calculateForeshortening(0, P, line)
    if (fs_ang < min_vis_degree):
        return
    det_plane = pg.plane_p3(P[2, :]).get_plane_at_distance(sid)
    sp = projection.get_source_position(P)
    angle_origin = det_plane.meet(line)
    det_point = point_on_line.project(P).backproject(
        np.linalg.pinv(P)).join(sp).meet(det_plane)
    ###Draw line from projection to meet_point
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    points.SetPoint(0,
                    angle_origin.e()[0],
                    angle_origin.e()[1],
                    angle_origin.e()[2])
    points.SetPoint(1,
                    point_on_line.e()[0],
                    point_on_line.e()[1],
                    point_on_line.e()[2])

    lines = vtk.vtkCellArray()
    lines.InsertNextCell(2)
    lines.InsertCellPoint(0)
    lines.InsertCellPoint(1)
    polygon = vtk.vtkPolyData()
    polygon.SetPoints(points)
    polygon.SetLines(lines)
    # vtkPolyDataMapper is a class that maps polygonal data (i.e., vtkPolyData)
    # to graphics primitives
    polygonMapper = vtk.vtkPolyDataMapper()
    polygonMapper.SetInputData(polygon)
    polygonMapper.Update()
    polygonActor = vtk.vtkActor()
    polygonActor.SetMapper(polygonMapper)
    polygonActor.GetProperty().SetColor(0, 0, 0)
    polygonActor.GetProperty().SetOpacity(0.3)
    # polygonActor.GetProperty().SetLineWidth(5)
    ren.AddActor(polygonActor)
    ###Draw line from line to meet_point
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(2)
    points.SetPoint(0,
                    angle_origin.e()[0],
                    angle_origin.e()[1],
                    angle_origin.e()[2])
    points.SetPoint(1, det_point.e()[0], det_point.e()[1], det_point.e()[2])
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(2)
    lines.InsertCellPoint(0)
    lines.InsertCellPoint(1)
    polygon = vtk.vtkPolyData()
    polygon.SetPoints(points)
    polygon.SetLines(lines)
    # vtkPolyDataMapper is a class that maps polygonal data (i.e., vtkPolyData)
    # to graphics primitives
    polygonMapper = vtk.vtkPolyDataMapper()
    polygonMapper.SetInputData(polygon)
    polygonMapper.Update()
    polygonActor = vtk.vtkActor()
    polygonActor.SetMapper(polygonMapper)
    polygonActor.GetProperty().SetColor(0, 0, 0)
    # polygonActor.GetProperty().SetLineWidth(5)
    polygonActor.GetProperty().SetOpacity(0.5)
    ren.AddActor(polygonActor)
    dir_det_line = np.matrix(det_point.e()) - np.matrix(angle_origin.e())
    dir_det_line = dir_det_line / np.linalg.norm(dir_det_line)
    start_pos = np.matrix(angle_origin.e()) + 100 * dir_det_line
    if (angle_origin.project(P).e()[1] > size_v / 2):
        sign = -1
    else:
        sign = 1
    drawRadian(line.join(sp), angle_origin, pg.point_p3(start_pos),
               sign * math.radians(fs_ang), 30, ren)
def p3(p):
    return pg.plane_p3(p[2, :])
def p2(p):
    return pg.plane_p3(p[1, :])
def p1(p):
    return pg.plane_p3(p[0, :])