Exemplo n.º 1
0
    def _load_box(node: dict, material_adjustments: list, transform: Matrix, parent: MeshObject) -> List[MeshObject]:
        """ Creates a cube inside blender which follows the specifications of the given node.

        :param node: The node dict which contains information from house.json..
        :param material_adjustments: Adjustments to the materials which were specified inside house.json.
        :param transform: The transformation that should be applied to the loaded objects.
        :param parent: The parent object to which the ground should be linked
        :return: The list of loaded mesh objects.
        """
        box = MeshObject.create_primitive("CUBE")
        box.set_name("Box#" + node["id"])
        # Scale the cube to the required dimensions
        box.set_local2world_mat(Matrix.Scale(node["dimensions"][0] / 2, 4, (1.0, 0.0, 0.0)) @ Matrix.Scale(node["dimensions"][1] / 2, 4, (0.0, 1.0, 0.0)) @ Matrix.Scale(node["dimensions"][2] / 2, 4, (0.0, 0.0, 1.0)))

        # Create UV mapping (beforehand we apply the scaling from the previous step, such that the resulting uv mapping has the correct aspect)
        bpy.ops.object.transform_apply(scale=True)
        bpy.ops.object.editmode_toggle()
        bpy.ops.uv.cube_project()
        bpy.ops.object.editmode_toggle()

        # Create an empty material which is filled in the next step
        mat = bpy.data.materials.new(name="material_0")
        mat.use_nodes = True
        box.add_material(mat)

        SuncgLoader._transform_and_colorize_object(box, material_adjustments, transform, parent)
        # set class to void
        box.set_cp("category_id", LabelIdMapping.label_id_map["void"])
        # Rotate cube to match objects loaded from .obj, has to be done after transformations have been applied
        box.set_local2world_mat(Matrix.Rotation(math.radians(90), 4, "X") @ box.get_local2world_mat())

        return [box]
Exemplo n.º 2
0
    def construct_random_room(
            used_floor_area: float, amount_of_extrusions: int,
            fac_from_square_room: float, corridor_width: float,
            wall_height: float, amount_of_floor_cuts: int,
            only_use_big_edges: bool,
            create_ceiling: bool) -> Tuple[MeshObject, MeshObject, MeshObject]:
        """
        This function constructs the floor plan and builds up the wall. This can be more than just a rectangular shape.

        If `amount_of_extrusions` is bigger than zero, the basic rectangular shape is extended, by first performing
        random cuts in this base rectangular shape along the axis. Then one of the edges is randomly selected and
        from there it is extruded outwards to get to the desired `floor_area`. This process is repeated
        `amount_of_extrusions` times. It might be that a room has less than the desired `amount_of_extrusions` if
        the random splitting reaches the `floor_area` beforehand.
        """
        floor_obj = None
        wall_obj = None
        ceiling_obj = None

        # if there is more than one extrusions, the used floor area must be split over all sections
        # the first section should be at least 50% - 80% big, after that the size depends on the amount of left
        # floor values
        if amount_of_extrusions > 1:
            size_sequence = []
            running_sum = 0.0
            start_minimum = 0.0
            for i in range(amount_of_extrusions - 1):
                if i == 0:
                    size_sequence.append(random.uniform(0.4, 0.8))
                    start_minimum = (1.0 -
                                     size_sequence[-1]) / amount_of_extrusions
                else:
                    if start_minimum < 1.0 - running_sum:
                        size_sequence.append(
                            random.uniform(start_minimum, 1.0 - running_sum))
                    else:
                        break
                running_sum += size_sequence[-1]
            if 1.0 - running_sum > 1e-7:
                size_sequence.append(1.0 - running_sum)
            if amount_of_extrusions != len(size_sequence):
                print(
                    "Amount of extrusions was reduced to: {}. To avoid rooms, which are smaller "
                    "than 1e-7".format(len(size_sequence)))
                amount_of_extrusions = len(size_sequence)
        else:
            size_sequence = [1.0]
        # this list of areas is then used to calculate the extrusions
        # if there is only one element in there, it will create a rectangle
        used_floor_areas = [size * used_floor_area for size in size_sequence]

        # calculate the squared room length for the base room
        squared_room_length = np.sqrt(used_floor_areas[0])
        # create a new plane and rename it to Wall
        wall_obj = MeshObject.create_primitive("PLANE")
        wall_obj.set_name("Wall")

        # calculate the side length of the base room, for that the `fac_from_square_room` is used
        room_length_x = fac_from_square_room * random.uniform(
            -1, 1) * squared_room_length + squared_room_length
        # make sure that the floor area is still used
        room_length_y = used_floor_areas[0] / room_length_x
        # change the plane to this size
        wall_obj.edit_mode()
        bpy.ops.transform.resize(value=(room_length_x * 0.5,
                                        room_length_y * 0.5, 1))
        wall_obj.object_mode()

        def cut_plane(plane: MeshObject):
            """
            Cuts the floor plane in several pieces randomly. This is used for selecting random edges for the extrusions
            later on. This function assumes the current `plane` object is already selected and no other object is
            selected.

            :param plane: The object, which should be split in edit mode.
            """

            # save the size of the plane to determine a best split value
            x_size = plane.get_scale()[0]
            y_size = plane.get_scale()[1]

            # switch to edit mode and select all faces
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.object.mode_set(mode='OBJECT')

            # convert plane to BMesh object
            bm = plane.mesh_as_bmesh(True)
            bm.faces.ensure_lookup_table()
            # find all selected edges
            edges = [e for e in bm.edges if e.select]

            biggest_face_id = np.argmax([f.calc_area() for f in bm.faces])
            biggest_face = bm.faces[biggest_face_id]
            # find the biggest face
            faces = [f for f in bm.faces if f == biggest_face]
            geom = []
            geom.extend(edges)
            geom.extend(faces)

            # calculate cutting point
            cutting_point = [
                x_size * random.uniform(-1, 1), y_size * random.uniform(-1, 1),
                0
            ]
            # select a random axis to specify in which direction to cut
            direction_axis = [1, 0, 0
                              ] if random.uniform(0, 1) < 0.5 else [0, 1, 0]

            # cut the plane and update the final mesh
            bmesh.ops.bisect_plane(bm,
                                   dist=0.01,
                                   geom=geom,
                                   plane_co=cutting_point,
                                   plane_no=direction_axis)
            plane.update_from_bmesh(bm)

        # for each floor cut perform one cut_plane
        for i in range(amount_of_floor_cuts):
            cut_plane(wall_obj)

        # do several extrusions of the basic floor plan, the first one is always the basic one
        for i in range(1, amount_of_extrusions):
            # Change to edit mode of the selected floor
            wall_obj.edit_mode()
            bpy.ops.mesh.select_all(action='DESELECT')
            bm = wall_obj.mesh_as_bmesh()
            bm.faces.ensure_lookup_table()
            bm.edges.ensure_lookup_table()
            # calculate the size of all edges and find all edges, which are wider than the minimum corridor_width
            # to avoid that super small, super long pieces are created
            boundary_edges = [e for e in bm.edges if e.is_boundary]
            boundary_sizes = [(e, e.calc_length()) for e in boundary_edges]
            boundary_sizes = [(e, s) for e, s in boundary_sizes
                              if s > corridor_width]

            if len(boundary_sizes) > 0:
                # sort the boundaries to focus only on the big ones
                boundary_sizes.sort(key=lambda e: e[1])
                if only_use_big_edges:
                    # only select the bigger half of the selected boundaries
                    half_size = len(boundary_sizes) // 2
                else:
                    # use any of the selected boundaries
                    half_size = 0
                used_edges = [e for e, s in boundary_sizes[half_size:]]

                random_edge = None
                shift_vec = None
                edge_counter = 0
                random_index = random.randrange(len(used_edges))
                while edge_counter < len(used_edges):
                    # select a random edge from the choose edges
                    random_edge = used_edges[random_index]
                    # get the direction of the current edge
                    direction = np.abs(random_edge.verts[0].co -
                                       random_edge.verts[1].co)
                    # the shift value depends on the used_floor_area size
                    shift_value = used_floor_areas[
                        i] / random_edge.calc_length()

                    # depending if the random edge is aligned with the x-axis or the y-axis,
                    # the shift is the opposite direction
                    if direction[0] == 0:
                        x_shift, y_shift = shift_value, 0
                    else:
                        x_shift, y_shift = 0, shift_value
                    # calculate the vertices for the new face
                    shift_vec = mathutils.Vector([x_shift, y_shift, 0])
                    dir_found = False
                    for tested_dir in [1, -1]:
                        shift_vec *= tested_dir
                        new_verts = [e.co for e in random_edge.verts]
                        new_verts.extend([e + shift_vec for e in new_verts])
                        new_verts = np.array(new_verts)

                        # check if the newly constructed face is colliding with one of the others
                        # if so generate a new face
                        collision_face_found = False
                        for existing_face in bm.faces:
                            existing_verts = np.array(
                                [v.co for v in existing_face.verts])
                            if check_bb_intersection_on_values(
                                    np.min(existing_verts, axis=0)[:2],
                                    np.max(existing_verts, axis=0)[:2],
                                    np.min(new_verts, axis=0)[:2],
                                    np.max(new_verts, axis=0)[:2],
                                    # by using this check an edge collision is ignored
                                    used_check=lambda a, b: a > b):
                                collision_face_found = True
                                break
                        if not collision_face_found:
                            dir_found = True
                            break
                    if dir_found:
                        break
                    random_index = (random_index + 1) % len(used_edges)
                    edge_counter += 1
                    random_edge = None

                if random_edge is None:
                    for e in used_edges:
                        e.select = True
                    raise Exception(
                        "No edge found to extrude up on! The reason might be that there are to many cuts"
                        "in the basic room or that the corridor width is too high."
                    )
                # extrude this edge with the calculated shift
                random_edge.select = True
                bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={
                    "use_normal_flip": False,
                    "use_dissolve_ortho_edges": False,
                    "mirror": False
                },
                                                 TRANSFORM_OT_translate={
                                                     "value": shift_vec,
                                                     "orient_type": 'GLOBAL'
                                                 })
            else:
                raise Exception(
                    "The corridor width is so big that no edge could be selected, "
                    "reduce the corridor width or reduce the amount of floor cuts."
                )
            # remove all doubles vertices, which might occur
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.remove_doubles()
            bpy.ops.mesh.select_all(action='DESELECT')
            wall_obj.update_from_bmesh(bm)
            wall_obj.object_mode()

        # create walls based on the outer shell
        wall_obj.edit_mode()
        bpy.ops.mesh.normals_make_consistent(inside=False)
        bm = wall_obj.mesh_as_bmesh()
        bm.edges.ensure_lookup_table()

        # select all edges
        boundary_edges = [e for e in bm.edges if e.is_boundary]
        for e in boundary_edges:
            e.select = True
        # extrude all boundary edges to create the walls
        bpy.ops.mesh.extrude_region_move(
            TRANSFORM_OT_translate={"value": (0, 0, wall_height)})
        wall_obj.update_from_bmesh(bm)
        wall_obj.object_mode()

        def extract_plane_from_room(obj: MeshObject, used_split_height: float,
                                    up_vec: mathutils.Vector,
                                    new_name_for_obj: str):
            """
            Extract a plane from the current room object. This uses the FloorExtractor Module functions

            :param obj: The current room object
            :param used_split_height: The height at which the split should be performed. Usually 0 or wall_height
            :param up_vec: The up_vec corresponds to the face.normal of the selected faces
            :param new_name_for_obj: This will be the new name of the created object
            :return: (bool, bpy.types.Object): Returns True if the object was split and also returns the object. \
                                               Else it returns (False, None).
            """
            compare_height = 0.15
            compare_angle = math.radians(7.5)
            obj.edit_mode()
            bpy.ops.mesh.select_all(action='DESELECT')
            bm = obj.mesh_as_bmesh()
            bm.faces.ensure_lookup_table()
            # Select faces at given height that should be separate from the mesh
            counter = FloorExtractor.select_at_height_value(
                bm, used_split_height, compare_height,
                mathutils.Vector(up_vec), compare_angle,
                obj.get_local2world_mat())
            # if any faces are selected split them up
            if counter:
                bpy.ops.mesh.separate(type='SELECTED')
                obj.update_from_bmesh(bm)
                obj.object_mode()
                cur_selected_objects = bpy.context.selected_objects
                if cur_selected_objects:
                    if len(cur_selected_objects) == 2:
                        cur_selected_objects = [
                            o for o in cur_selected_objects
                            if o != bpy.context.view_layer.objects.active
                        ]
                        cur_selected_objects[0].name = new_name_for_obj
                        cur_created_obj = MeshObject(cur_selected_objects[0])
                    else:
                        raise Exception(
                            "There is more than one selection after splitting, this should not happen!"
                        )
                else:
                    raise Exception("No floor object was constructed!")
                bpy.ops.object.select_all(action='DESELECT')
                return True, cur_created_obj
            else:
                obj.object_mode()
                bpy.ops.object.select_all(action='DESELECT')
                return False, None

        # if only one rectangle was created, the wall extrusion creates a full room with ceiling and floor, if not
        # only the floor gets created and the ceiling is missing
        only_rectangle_mode = False
        for used_split_height in [(0, "Floor", [0, 0, 1]),
                                  (wall_height, "Ceiling", [0, 0, -1])]:
            created, created_obj = extract_plane_from_room(
                wall_obj, used_split_height[0], used_split_height[2],
                used_split_height[1])
            if not created and used_split_height[1] == "Floor":
                only_rectangle_mode = True
                break
            elif created and created_obj is not None:
                if "Floor" == used_split_height[1]:
                    floor_obj = created_obj
                elif "Ceiling" == used_split_height[1]:
                    ceiling_obj = created_obj

        if only_rectangle_mode:
            # in this case the floor and ceiling are pointing outwards, so that normals have to be flipped
            for used_split_height in [(0, "Floor", [0, 0, -1]),
                                      (wall_height, "Ceiling", [0, 0, 1])]:
                created, created_obj = extract_plane_from_room(
                    wall_obj, used_split_height[0], used_split_height[2],
                    used_split_height[1])
                # save the result accordingly
                if created and created_obj is not None:
                    if "Floor" == used_split_height[1]:
                        floor_obj = created_obj
                    elif "Ceiling" == used_split_height[1]:
                        ceiling_obj = created_obj
        elif create_ceiling:
            # there is no ceiling -> create one
            wall_obj.edit_mode()
            bpy.ops.mesh.select_all(action='DESELECT')
            bm = wall_obj.mesh_as_bmesh()
            bm.edges.ensure_lookup_table()
            # select all upper edges and create a ceiling
            for e in bm.edges:
                if ((e.verts[0].co + e.verts[1].co) *
                        0.5)[2] >= wall_height - 1e-4:
                    e.select = True
            bpy.ops.mesh.edge_face_add()
            # split the ceiling away
            bpy.ops.mesh.separate(type='SELECTED')
            wall_obj.update_from_bmesh(bm)
            wall_obj.object_mode()
            selected_objects = bpy.context.selected_objects
            if selected_objects:
                if len(selected_objects) == 2:
                    selected_objects = [
                        o for o in selected_objects
                        if o != bpy.context.view_layer.objects.active
                    ]
                    selected_objects[0].name = "Ceiling"
                    ceiling_obj = MeshObject(selected_objects[0])
                else:
                    raise Exception(
                        "There is more than one selection after splitting, this should not happen!"
                    )
            else:
                raise Exception("No floor object was constructed!")
            bpy.ops.object.select_all(action='DESELECT')

        return floor_obj, wall_obj, ceiling_obj