def generate_calibration(
    n_cameras=5,
    return_full_info=False,
    radial_distortion=False,
):
    pi = numpy.pi

    sccs = []

    # 1. extrinsic parameters:
    if 1:
        # method 1:
        #  arrange cameras in circle around common point
        common_point = numpy.array((0, 0, 0), dtype=numpy.float64)
        r = 10.0

        theta = numpy.linspace(0, 2 * pi, n_cameras, endpoint=False)
        x = numpy.cos(theta)
        y = numpy.sin(theta)
        z = numpy.zeros(y.shape)

        cc = numpy.c_[x, y, z]
        #cam_up = numpy.array((0,0,1))

        #cam_ups = numpy.resize(cam_up,cc.shape)
        #cam_forwads = -cc
        cam_centers = r * cc + common_point

        # Convert up/forward into rotation matrix.
        if 1:
            Rs = []
            for i, th in enumerate(theta):
                pos = cam_centers[i]
                target = common_point
                up = (0, 0, 1)
                if 0:
                    print 'pos', pos
                    print 'target', target
                    print 'up', up
                R = cgtypes.mat4().lookAt(pos, target, up)
                #print 'R4',R
                R = R.getMat3()
                #print 'R3',R
                R = numpy.asarray(R).T
                #print 'R',R
                #print
                Rs.append(R)

        else:
            # (Camera coords: looking forward -z, up +y, right +x)
            R = cgtypes.mat3().identity()

            if 1:
                # (looking forward -z, up +x, right -y)
                R = R.rotation(-pi / 2, (0, 0, 1))

                # (looking forward +x, up +z, right -y)
                R = R.rotation(-pi / 2, (0, 1, 0))

                # rotate to point -theta (with up +z)
                Rs = [R.rotation(float(th) + pi, (0, 0, 1)) for th in theta]
                #Rs = [ R for th in theta ]
            else:
                Rs = [R.rotation(pi / 2.0, (1, 0, 0)) for th in theta]
                #Rs = [ R for th in theta ]
            Rs = [numpy.asarray(R).T for R in Rs]
            print 'Rs', Rs

    # 2. intrinsic parameters

    resolutions = {}
    for cam_no in range(n_cameras):
        cam_id = 'fake_%d' % (cam_no + 1)

        # resolution of image
        res = (1600, 1200)
        resolutions[cam_id] = res

        # principal point
        cc1 = res[0] / 2.0
        cc2 = res[1] / 2.0

        # focal length
        fc1 = 1.0
        fc2 = 1.0
        alpha_c = 0.0
        #        R = numpy.asarray(Rs[cam_no]).T # conversion between cgkit and numpy
        R = Rs[cam_no]
        C = cam_centers[cam_no][:, numpy.newaxis]

        K = numpy.array(((fc1, alpha_c * fc1, cc1), (0, fc2, cc2), (0, 0, 1)))
        t = numpy.dot(-R, C)
        Rt = numpy.concatenate((R, t), axis=1)
        P = numpy.dot(K, Rt)
        if 0:
            print 'cam_id', cam_id
            print 'P'
            print P
            print 'K'
            print K
            print 'Rt'
            print Rt
            print
            KR = numpy.dot(K, R)
            print 'KR', KR
            K3, R3 = reconstruct.my_rq(KR)
            print 'K3'
            print K3
            print 'R3'
            print R3
            K3R3 = numpy.dot(K3, R3)
            print 'K3R3', K3R3

            print '*' * 60

        if radial_distortion:
            f = 1000.0
            r1 = 0.8
            r2 = -0.2

            helper = reconstruct_utils.ReconstructHelper(
                f,
                f,  # focal length
                cc1,
                cc2,  # image center
                r1,
                r2,  # radial distortion
                0,
                0)  # tangential distortion

        scc = reconstruct.SingleCameraCalibration_from_basic_pmat(
            P,
            cam_id=cam_id,
            res=res,
        )
        sccs.append(scc)
        if 1:
            # XXX test
            K2, R2 = scc.get_KR()
            if 0:
                print 'C', C
                print 't', t
                print 'K', K
                print 'K2', K2
                print 'R', R
                print 'R2', R2
                print 'P', P
                print 'KR|t', numpy.dot(K, Rt)
                t2 = scc.get_t()
                print 't2', t2
                Rt2 = numpy.concatenate((R2, t2), axis=1)
                print 'KR2|t', numpy.dot(K2, Rt2)
                print
            KR2 = numpy.dot(K2, R2)
            KR = numpy.dot(K, R)
            if not numpy.allclose(KR2, KR):
                if not numpy.allclose(KR2, -KR):
                    raise ValueError('expected KR2 and KR to be identical')
                else:
                    print 'WARNING: weird sign error in calibration math FIXME!'
    recon = reconstruct.Reconstructor(sccs)

    full_info = {
        'reconstructor': recon,
        'center': common_point,  # where all the cameras are looking
        'camera_dist_from_center': r,
        'resolutions': resolutions,
    }
    if return_full_info:
        return full_info
    return recon
Пример #2
0
def Mat4Slot(value=mat4(), flags=0):
    return _core.Mat4Slot(value, flags)
Пример #3
0
def convertdefault(paramtuple):
    """Converts the default value of a shader parameter into a Python type.

    *paramtuple* must be a 7-tuple (or parameter object) as returned by 
    :func:`slparams()`.
    The function returns a Python object that corresponds to the default
    value of the parameter. If the default value can't be converted
    then ``None`` is returned. Only the functions that are present in the
    :mod:`sl<cgkit.sl>` module are evaluated. If a default value calls a user defined
    function then ``None`` is returned.

    The SL types will be converted into the following Python types:

        +------------+-------------+
        | SL type    | Python type |
        +============+=============+
        | ``float``  | ``float``   |
        +------------+-------------+
        | ``string`` | ``string``  |
        +------------+-------------+
        | ``color``  | ``vec3``    |
        +------------+-------------+
        | ``point``  | ``vec3``    |
        +------------+-------------+
        | ``vector`` | ``vec3``    |
        +------------+-------------+
        | ``normal`` | ``vec3``    |
        +------------+-------------+
        | ``matrix`` | ``mat4``    |
        +------------+-------------+

    Arrays will be converted into lists of the corresponding type.
    """
    global _local_namespace

    typ = paramtuple[2]
    arraylen = paramtuple[3]
    defstr = paramtuple[6]

    # The default value is not a string? Then it already contains the
    # converted default value (this is the case when the value was
    # extracted from a compiled shader).
    if not isinstance(defstr, basestring):
        # Make sure that point-like types are returned as vec3 and matrix types
        # are returned as mat4.
        if typ in ["color", "point", "vector", "normal"]:
            retType = cgtypes.vec3
        elif typ == "matrix":
            retType = cgtypes.mat4
        else:
            # No vec3/mat4 type, then just return the value
            return defstr
        # Cast the value...
        if arraylen is None:
            return retType(defstr)
        else:
            return map(lambda v: retType(v), defstr)

    # Replace {} with [] so that SL arrays look like Python lists
    if arraylen is not None:
        defstr = defstr.replace("{", "[").replace("}", "]")
    # If the parameter is not an array, then create an array with one
    # element (to unify further processing). It will be unwrapped in the end
    if arraylen is None:
        defstr = "[%s]" % defstr
    # Evaluate the string to create "raw" Python types (lists and tuples)
    try:
        rawres = eval(defstr, globals(), _local_namespace)
    except:
        return None

    # Convert into the appropriate type...
    if typ == "float":
        try:
            res = map(lambda x: float(x), rawres)
        except:
            return None
    elif typ == "color" or typ == "point" or typ == "vector" or typ == "normal":
        try:
            res = map(lambda x: cgtypes.vec3(x), rawres)
        except:
            return None
    elif typ == "matrix":
        try:
            res = map(lambda x: cgtypes.mat4(x), rawres)
        except:
            return None
    elif typ == "string":
        try:
            res = map(lambda x: str(x), rawres)
        except:
            return None

    if arraylen is None:
        if len(res) == 0:
            return None
        else:
            res = res[0]

    return res
Пример #4
0
 def __init__(self, proc):
     _core.Mat4Slot.__init__(self, mat4(1), 2) # 2 = NO_INPUT_CONNECTIONS
     self._proc = proc
Пример #5
0
def convertdefault(paramtuple):
    """Converts the default value of a shader parameter into a Python type.

    *paramtuple* must be a 7-tuple (or parameter object) as returned by 
    :func:`slparams()`.
    The function returns a Python object that corresponds to the default
    value of the parameter. If the default value can't be converted
    then ``None`` is returned. Only the functions that are present in the
    :mod:`sl<cgkit.sl>` module are evaluated. If a default value calls a user defined
    function then ``None`` is returned.

    The SL types will be converted into the following Python types:

        +------------+-------------+
        | SL type    | Python type |
        +============+=============+
        | ``float``  | ``float``   |
        +------------+-------------+
        | ``string`` | ``string``  |
        +------------+-------------+
        | ``color``  | ``vec3``    |
        +------------+-------------+
        | ``point``  | ``vec3``    |
        +------------+-------------+
        | ``vector`` | ``vec3``    |
        +------------+-------------+
        | ``normal`` | ``vec3``    |
        +------------+-------------+
        | ``matrix`` | ``mat4``    |
        +------------+-------------+

    Arrays will be converted into lists of the corresponding type.
    """
    global _local_namespace

    typ = paramtuple[2]
    arraylen = paramtuple[3]
    defstr = paramtuple[6]

    # The default value is not a string? Then it already contains the
    # converted default value (this is the case when the value was
    # extracted from a compiled shader).
    if not isinstance(defstr, basestring):
        # Make sure that point-like types are returned as vec3 and matrix types
        # are returned as mat4.
        if typ in ["color", "point", "vector", "normal"]:
            retType = cgtypes.vec3
        elif typ == "matrix":
            retType = cgtypes.mat4
        else:
            # No vec3/mat4 type, then just return the value
            return defstr
        # Cast the value...
        if arraylen is None:
            return retType(defstr)
        else:
            return map(lambda v: retType(v), defstr)

    # Replace {} with [] so that SL arrays look like Python lists
    if arraylen is not None:
        defstr = defstr.replace("{", "[").replace("}", "]")
    # If the parameter is not an array, then create an array with one
    # element (to unify further processing). It will be unwrapped in the end
    if arraylen is None:
        defstr = "[%s]" % defstr
    # Evaluate the string to create "raw" Python types (lists and tuples)
    try:
        rawres = eval(defstr, globals(), _local_namespace)
    except:
        return None

    # Convert into the appropriate type...
    if typ == "float":
        try:
            res = map(lambda x: float(x), rawres)
        except:
            return None
    elif typ == "color" or typ == "point" or typ == "vector" or typ == "normal":
        try:
            res = map(lambda x: cgtypes.vec3(x), rawres)
        except:
            return None
    elif typ == "matrix":
        try:
            res = map(lambda x: cgtypes.mat4(x), rawres)
        except:
            return None
    elif typ == "string":
        try:
            res = map(lambda x: str(x), rawres)
        except:
            return None

    if arraylen is None:
        if len(res) == 0:
            return None
        else:
            res = res[0]

    return res
Пример #6
0
import fsee.Observer

# these numbers specify body position and wing position (currently only body is used)

nums = """-63.944819191780439 0.95360065664418736 -1.5677184054558335
0.97592747002588576 0.00013135157421694402 0.21809381132492375
0.00080340363848455045 -64.511912229419792 2.4958009855663787
-0.91416847614060104 0.83348670845615658 -0.079986858311146714
0.50516881885673259 0.2090609331733887 -64.507322639730816
-0.59116643445214989 -0.91599543302231212 0.20749684980883201
0.50542162231080134 -0.079722030902706131 0.83374962597225588"""

nums = map(float, nums.split())

pos_vec3, ori_quat = cgtypes.vec3(nums[0:3]), cgtypes.quat(nums[3:7])
M = cgtypes.mat4().identity().translate(pos_vec3)
M = M * ori_quat.toMat4()
# grr, I hate cgtypes to numpy conversions!
M = np.array(
    (
        M[0, 0],
        M[1, 0],
        M[2, 0],
        M[3, 0],
        M[0, 1],
        M[1, 1],
        M[2, 1],
        M[3, 1],
        M[0, 2],
        M[1, 2],
        M[2, 2],
Пример #7
0
def Mat4Slot(value=mat4(), flags=0):
    return _core.Mat4Slot(value, flags)
Пример #8
0
 def __init__(self, proc):
     _core.Mat4Slot.__init__(self, mat4(1), 2)  # 2 = NO_INPUT_CONNECTIONS
     self._proc = proc