Example #1
0
 def __init__(self, uid):
     self.uid = uid  # unique id
     self.jid = ''  # object id
     self.aid = []
     self.type = ''  # Floor or Window or ...
     self.bounding_box = BoundingBox()  # bbox
     self.mesh = 0  # is Mesh object, juran-json 'mesh' info
     self.ref = 0  # reference count
Example #2
0
 def __init__(self, uid, scene):
     self.scene = scene  # parent
     self.id = uid  # id
     self.type = ''  # Bedroom
     self.position = np.zeros(3)  # [0,0,0]
     self.rotation = np.array([0.0, 0.0, 0.0, 1.0])  # [0,0,0,1]
     self.scale = np.array([1.0, 1.0, 1.0])  # [1,1,1]
     self.children_for_mesh = []  # furniture
     self.children_for_furniture = []  # floor, window ...
     self.bounding_box = BoundingBox()  # box
Example #3
0
class Instance:
    def __init__(self, uid):
        self.uid = uid  # unique id
        self.jid = ''  # object id
        self.aid = []
        self.type = ''  # Floor or Window or ...
        self.bounding_box = BoundingBox()  # bbox
        self.mesh = 0  # is Mesh object, juran-json 'mesh' info
        self.ref = 0  # reference count

    def __repr__(self):
        return 'Instance %s, {jid:%s}' % (self.uid, self.jid)

    def clone(self):
        """
        clone
        :return: copy instance
        """

        instance = Instance(self.uid)
        instance.jid = self.jid
        instance.aid = self.aid[:]
        instance.type = self.type

        instance.bounding_box = self.bounding_box.clone()
        instance.mesh = self.mesh
        instance.ref = self.ref
        return instance
Example #4
0
 def __init__(self,
              refer_position=np.array([0, 0, 0]),
              direction_2d=np.array([1, 0]),
              bbox=BoundingBox(),
              position_str=['center', 'center', 'center']):
     self.position_str = position_str
     self.direction_3d = np.array([direction_2d[0], 0, direction_2d[1]])
     self.vt_3d = np.array([-direction_2d[1], 0, direction_2d[0]])
     self.z_axis = np.array([0, 1, 0])
     self.bbox = bbox
     self.camera_position = copy.deepcopy(refer_position)
Example #5
0
    def __init__(self, instance):
        self.instance = instance  # instance object
        self.type = ''  # data type
        self.bounding_box = BoundingBox()  # box
        self.rotate = np.array([0.0, 0.0, 0.0, 1.0])  # [0,0,0,1]
        self.position = np.array([0.0, 0.0, 0.0])  # [0,0,0
        self.scale = np.array([1.0, 1.0, 1.0])  # [1,1,1]
        self.used = True  # is used
        self.clamp_wall = False  # clamp wall
        self.clamp_rotate_list = []  # clamp direction
        self.children = []  # child object
        self.parent = None  # parent object
        self.modify = False  # is transform
        self.instance_id = 'id'  # reflect instanceid
        self.instance_ref = ''  # reflect uid
        self.use_gmm = False  # is gmm
        self.special_children = []  # bed and bedstand
        # self.align_neighbours = []

        # 规则摆放
        self.mainObject = False
        self.valid = True

        self.is_sale = False  # 是否为在售商品
Example #6
0
class Room:
    """
    Room info
    """
    def __init__(self, uid, scene):
        self.scene = scene  # parent
        self.id = uid  # id
        self.type = ''  # Bedroom
        self.position = np.zeros(3)  # [0,0,0]
        self.rotation = np.array([0.0, 0.0, 0.0, 1.0])  # [0,0,0,1]
        self.scale = np.array([1.0, 1.0, 1.0])  # [1,1,1]
        self.children_for_mesh = []  # furniture
        self.children_for_furniture = []  # floor, window ...
        self.bounding_box = BoundingBox()  # box

    def __repr__(self):
        return 'Room %s, {type:%s}' % (self.id, self.type)

    def calculate_bbox(self, scene):
        """
        :param scene:
        :return:
        """
        for instance_info in self.children_for_furniture:
            instanceId = instance_info['id']
            instance = scene.find_instance_for_furniture(instanceId)
            if (instance == 0 or instance.mesh == 0):
                continue

            bbox = instance.mesh.bounding_box.clone()
            bbox.transform(instance_info['pos'], instance_info['rot'],
                           instance_info['scale'])
            self.bounding_box.merge(bbox)
        return self.bounding_box

    def delete_all_furniture(self):
        """
        :return:
        """
        for instance_info in self.children_for_furniture:
            instance_id = instance_info['id']
            instance = self.scene.find_instance_for_furniture(instance_id)
            if instance == 0:
                continue

            self.scene.delete_furniture(instance)

            self.delete_child_to_content(instance_info['instanceid'])

        self.children_for_furniture = []

    def delete_furniture_by_index(self, index):
        """
        :param index:
        :return:
        """
        instance_info = self.children_for_furniture[index]
        instance_id = instance_info['id']
        instance = self.scene.find_instance_for_furniture(instance_id)
        if instance != 0:
            self.scene.delete_furniture(instance)

        del self.children_for_furniture[index]
        self.delete_child_to_content(instance_info['instanceid'])

    def delete_furniture_by_id(self, id):
        """
        :param item:
        :return:
        """
        for i in range(len(self.children_for_furniture) - 1, -1, -1):
            instance_info = self.children_for_furniture[i]

            # if not self.equal_furniture(instance_info, item):
            if instance_info['instanceid'] != id:
                continue

            self.delete_furniture_by_index(i)
            break

    def delete_child_to_content(self, child_id):
        """
        :param child_id:
        :return:
        """
        if len(self.scene.content) == 0:
            return

        room_content = self.scene.content['scene']['room']
        for room_info in room_content:
            if room_info['instanceid'] != self.id:
                continue

            room_child = room_info['children']
            for i, child in enumerate(room_child):
                if child['instanceid'] != child_id:
                    continue

                del room_child[i]
                return

    def replace_room(self, group_list):
        """
        :param group_list:
        :return:
        """
        for group_info in group_list:
            group = group_info['group']
            if len(group) == 0:
                continue

            for entity in group:
                if not entity.used:
                    continue

                self.add_entity(entity)

    def delete_entity(self, entity):
        """
        :param entity:
        :return:
        """
        self.delete_furniture_by_id(entity.instance_id)

    def add_entity(self, entity):
        """
        :param entity
        :return:
        """
        instance = self.scene.find_instance_for_furniture(entity.instance_ref)
        if instance == 0:
            entity.instance.ref += 1
            self.scene.add_instance_for_furniture(entity.instance_ref,
                                                  entity.instance)
            self.add_furniture_to_content(entity)
        else:
            instance.ref += 1

        info = {}
        info['id'] = entity.instance_ref
        info['pos'] = entity.position
        info['rot'] = entity.rotate
        info['scale'] = entity.scale
        info['instanceid'] = entity.instance_id
        self.children_for_furniture.append(info)

        self.add_child_to_content(entity)

        for child in entity.children:
            if not child.used:
                continue

            self.add_entity(child)

    def add_furniture_to_content(self, entity):
        """
        :param entity:
        :return:
        """
        if len(self.scene.content) == 0:
            return

        info = {}
        info['uid'] = entity.instance_ref
        info['jid'] = entity.instance.jid
        info['aid'] = entity.instance.aid
        self.scene.content['furniture'].append(info)

    def add_child_to_content(self, entity):
        """
        :param entity:
        :return:
        """
        if len(self.scene.content) == 0:
            return

        room_content = self.scene.content['scene']['room']
        for room_info in room_content:
            if room_info['instanceid'] != self.id:
                continue

            room_child = room_info['children']

            child_info = {}
            child_info['ref'] = entity.instance_ref
            child_info['instanceid'] = entity.instance_id
            child_info['pos'] = entity.position.tolist()
            child_info['rot'] = entity.rotate.tolist()
            child_info['scale'] = entity.scale.tolist()
            room_child.append(child_info)
            break
Example #7
0
 def __init__(self):
     self.bounding_box = BoundingBox()  # box
     self.vertex_array = []  # vertex
     self.index_array = []  # index
     self.normal = 0  # normal
     self.uv = 0  # texcoord
Example #8
0
class Mesh:
    """
    mesh info
    """
    def __init__(self):
        self.bounding_box = BoundingBox()  # box
        self.vertex_array = []  # vertex
        self.index_array = []  # index
        self.normal = 0  # normal
        self.uv = 0  # texcoord

    def __repr__(self):
        return 'Mesh '

    def set_data(self, vertex_array, index_array):
        """
        :param vertex_array
        :param index_array
        :return:
        """
        vertex_count = len(vertex_array)
        index_count = len(index_array)
        if (vertex_count < 3 or index_count < 3):
            return

        self.index_array = np.array(index_array)
        self.index_array = self.index_array.astype(int)

        self.vertex_array = np.array(vertex_array)
        self.vertex_array = self.vertex_array.astype(float)

        # # bbox
        # v = []
        # for i in self.index_array:
        #     v.append(self.vertex_array[nDim * int(i)])
        #     v.append(self.vertex_array[nDim * int(i) + 1])
        #     v.append(self.vertex_array[nDim * int(i) + 2])
        # self.bounding_box.setBox(v, 3)

    def set_normal_uv(self, normal, uv):
        """
        :param normal
        :param uv
        :return:
        """
        self.normal = np.array(normal)
        self.uv = np.array(uv)

    def cal_boundingbox(self):
        """
        :return:
        """
        if self.bounding_box.is_valid():
            return

        vertex_count = len(self.vertex_array)
        index_count = len(self.index_array)
        if (vertex_count < 3 or index_count < 3):
            return

        nDim = 3
        # bbox
        vertex_x = []
        vertex_y = []
        vertex_z = []
        for i in self.index_array:
            vertex_x.append(float(self.vertex_array[nDim * int(i)]))
            vertex_y.append(float(self.vertex_array[nDim * int(i) + 1]))
            vertex_z.append(float(self.vertex_array[nDim * int(i) + 2]))

        if (len(vertex_x) < 2):
            return

        self.bounding_box.min[0] = min(vertex_x)
        self.bounding_box.min[1] = min(vertex_y)
        self.bounding_box.min[2] = min(vertex_z)
        self.bounding_box.max[0] = max(vertex_x)
        self.bounding_box.max[1] = max(vertex_y)
        self.bounding_box.max[2] = max(vertex_z)
Example #9
0
class Entity:
    """
    layout object
    """
    def __init__(self, instance):
        self.instance = instance  # instance object
        self.type = ''  # data type
        self.bounding_box = BoundingBox()  # box
        self.rotate = np.array([0.0, 0.0, 0.0, 1.0])  # [0,0,0,1]
        self.position = np.array([0.0, 0.0, 0.0])  # [0,0,0
        self.scale = np.array([1.0, 1.0, 1.0])  # [1,1,1]
        self.used = True  # is used
        self.clamp_wall = False  # clamp wall
        self.clamp_rotate_list = []  # clamp direction
        self.children = []  # child object
        self.parent = None  # parent object
        self.modify = False  # is transform
        self.instance_id = 'id'  # reflect instanceid
        self.instance_ref = ''  # reflect uid
        self.use_gmm = False  # is gmm
        self.special_children = []  # bed and bedstand
        # self.align_neighbours = []

        # 规则摆放
        self.mainObject = False
        self.valid = True

        self.is_sale = False  # 是否为在售商品

    def __repr__(self):
        return 'Entity %s, {type: %s}' % (self.id, self.type)

    # def add_align_neighbour(self, obj):
    #     self.align_neighbours.append(obj)

    def add_child(self, node):
        """
        add child node
        :param node:
        :return:
        """
        self.children.append(node)
        node.parent = self

    def set_position(self, pos):
        """
        set position
        :param pos:
        :return:
        """
        offset = pos - self.position
        self.position = pos
        for child in self.children:
            child.transform(offset)

    def set_rotate(self, rot):
        """
        set rotate
        :param rot:
        :return:
        """
        invert_rot = quaternion_invert(self.rotate)
        for i, clp_rot in enumerate(self.clamp_rotate_list):
            cur_rot = quaternion_muli(invert_rot, clp_rot)
            cur_rot = quaternion_muli(rot, cur_rot)
            self.clamp_rotate_list[i] = cur_rot

        self.rotate = rot

        rel_rot = quaternion_muli(rot, invert_rot)
        rotMat = quaternion_to_matrix(rel_rot)
        for child in self.children:
            pos = child.position - self.position
            child.position = vector_dot_matrix3(pos, rotMat) + self.position

            child_rot = quaternion_muli(child.rotate, rel_rot)
            child.set_rotate(child_rot)

        # self.rotate = rot
        # for child in self.children:
        #     child.set_rotate(rot)

    def set_scale(self, scale, is_local=False):
        """
        set scale
        :param scale:
        :param is_local:
        :return:
        """
        t_scale = scale.copy()
        if not is_local and is_rot(self.rotate):
            t_scale[0] = scale[2]
            t_scale[2] = scale[0]

        cur_scale = t_scale / self.scale
        self.scaling(cur_scale, True)

    def transform(self, pos):
        """
        transform
        :param pos:
        :return:
        """
        self.position = self.position + pos
        for child in self.children:
            child.transform(pos)

    def rotation(self, rot):
        """
        rotation
        :param rot:
        :return:
        """
        for i, clp_rot in enumerate(self.clamp_rotate_list):
            self.clamp_rotate_list[i] = quaternion_muli(rot, clp_rot)

        rotMat = quaternion_to_matrix(rot)
        self.position = vector_dot_matrix3(self.position, rotMat)
        self.rotate = np.array(quaternion_muli(rot, self.rotate))
        for child in self.children:
            child.rotation(rot)

    def scaling(self, scale, is_local=False):
        """
        scaling
        :param scale:
        :param is_local:
        :return:
        """
        t_scale = scale.copy()
        if not is_local and is_rot(self.rotate):
            t_scale[0] = scale[2]
            t_scale[2] = scale[0]

        for child in self.children:
            child.transform(-self.position)

            if is_local:
                rot_matrix = quaternion_to_matrix(
                    quaternion_invert(self.rotate))
                pos = vector_dot_matrix3(child.position, rot_matrix)
                pos *= scale
                pos = vector_dot_matrix3(pos, quaternion_to_matrix(
                    self.rotate)) + self.position
            else:
                pos = child.position * scale + self.position

            child.set_position(pos)

            value = min(abs(scale[0]), abs(scale[2]))
            child.scaling(np.array([value, value, value]))

        self.scale = self.scale * t_scale

    def mirror(self, axis):
        """
        mirror
        :param axis: axis = 0, x; axis = 1, y
        :return:
        """
        if axis == 0:
            reflect_matrix = get_reflect_matrix([1, 0, 0])
            scale = np.array([-1, 1, 1])
        else:
            reflect_matrix = get_reflect_matrix([0, 0, 1])
            scale = np.array([-1, 1, 1])

        self.position = vector_dot_matrix3(self.position, reflect_matrix)

        dir = quaternion_to_dir(self.rotate)
        dir = vector_dot_matrix3(dir, reflect_matrix)
        self.rotate = dir_to_quaternion(dir)
        self.scale *= scale

        for child in self.children:
            child.mirror(axis)

        for i, clp_rot in enumerate(self.clamp_rotate_list):
            dir = quaternion_to_dir(clp_rot)
            dir = vector_dot_matrix3(dir, reflect_matrix)

            self.clamp_rotate_list[i] = dir_to_quaternion(dir)

    def set_used(self, value):
        """
        set_used
        :param value:
        :return:
        """
        self.used = value
        for child in self.children:
            child.set_used(value)

    def get_bounding_box(self):
        """
        get_bounding_box
        :return:
        """
        bbox = self.bounding_box.clone()
        bbox.transform(self.position, self.rotate, self.scale)
        return bbox