def apply_relations_surfacesample(
    state: state_def.State,
    name: str,
):
    """
    Apply the relation constraints to the object. Place it in the scene according to the constraints.
    """

    obj_state = state.objs[name]
    obj_name = obj_state.obj.name

    def relation_sort_key(relation_state):
        return isinstance(relation_state.relation, cl.CoPlanar)

    obj_state.relations = sorted(obj_state.relations, key=relation_sort_key)

    parent_objs = []
    parent_planes = []
    obj_planes = []
    margins = []
    parent_tag_list = []
    relations = []
    rev_normals = []

    if len(obj_state.relations) == 0:
        raise ValueError(f"Object {name} has no relations")
    elif len(obj_state.relations) > 3:
        raise ValueError(
            f"Object {name} has more than 3 relations, not supported. {obj_state.relations=}"
        )

    for i, relation_state in enumerate(obj_state.relations):
        if isinstance(relation_state.relation, cl.AnyRelation):
            raise ValueError(
                f"Got {relation_state.relation} for {name=} {relation_state.target_name=}"
            )

        parent_obj = state.objs[relation_state.target_name].obj
        obj_plane, parent_plane = state.planes.get_rel_state_planes(
            state, name, relation_state
        )

        if obj_plane is None:
            continue
        if parent_plane is None:
            continue

        obj_planes.append(obj_plane)
        parent_planes.append(parent_plane)
        parent_objs.append(parent_obj)
        match relation_state.relation:
            case cl.StableAgainst(
                _child_tags, parent_tags, margin, _check_z, rev_normal
            ):
                margins.append(margin)
                parent_tag_list.append(parent_tags)
                relations.append(relation_state.relation)
                rev_normals.append(rev_normal)
            case cl.SupportedBy(_parent_tags, parent_tags):
                margins.append(0)
                parent_tag_list.append(parent_tags)
                relations.append(relation_state.relation)
                rev_normals.append(False)
            case cl.CoPlanar(_child_tags, parent_tags, margin, rev_normal):
                margins.append(margin)
                parent_tag_list.append(parent_tags)
                relations.append(relation_state.relation)
                rev_normals.append(rev_normal)
            case cl.Touching(_child_tags, parent_tags, margin):
                margins.append(margin)
                parent_tag_list.append(parent_tags)
                relations.append(relation_state.relation)
                rev_normals.append(False)
            case _:
                raise NotImplementedError

    valid, dof, T = check_init_valid(
        state, name, obj_planes, parent_planes, margins, rev_normals
    )
    if not valid:
        rels = [(rels.relation, rels.target_name) for rels in obj_state.relations]
        logger.warning(f"Init was invalid for {name=} {rels=}")
        return None

    if dof == 0:
        iu.translate(state.trimesh_scene, obj_name, T)
    elif dof == 1:
        assert len(parent_planes) == 2, (name, len(parent_planes))

        parent_obj1 = parent_objs[0]
        parent_obj2 = parent_objs[1]
        parent_plane1 = parent_planes[0]
        parent_plane2 = parent_planes[1]
        parent_tags1 = parent_tag_list[0]
        parent_tags2 = parent_tag_list[1]
        margin1 = margins[0]
        margin2 = margins[1]
        obj_plane1 = obj_planes[0]
        obj_plane2 = obj_planes[1]
        relation2 = relations[1]
        rev_normal1 = rev_normals[0]
        rev_normal2 = rev_normals[1]

        parent1_trimesh = state.planes.get_tagged_submesh(
            state.trimesh_scene, parent_obj1.name, parent_tags1, parent_plane1
        )
        parent2_trimesh = state.planes.get_tagged_submesh(
            state.trimesh_scene, parent_obj2.name, parent_tags2, parent_plane2
        )

        parent1_poly_index = parent_plane1[1]
        parent1_poly = parent_obj1.data.polygons[parent1_poly_index]
        plane_normal_1 = butil.global_polygon_normal(parent_obj1, parent1_poly)
        pts = parent2_trimesh.vertices
        projected = project(pts, plane_normal_1)
        p1_to_p1 = trimesh.path.polygons.projected(
            parent1_trimesh, plane_normal_1, (0, 0, 0)
        )

        if p1_to_p1 is None:
            raise ValueError(
                f"Failed to project {parent1_trimesh=} {plane_normal_1=} for {name=}"
            )

        if all(
            [p1_to_p1.buffer(1e-1).contains(Point(pt[0], pt[1])) for pt in projected]
        ) and (not isinstance(relation2, cl.CoPlanar)):
            face_mask = tagging.tagged_face_mask(parent_obj2, parent_tags2)
            stability.move_obj_random_pt(
                state, obj_name, parent_obj2.name, face_mask, parent_plane2
            )
            stability.snap_against(
                state.trimesh_scene,
                obj_name,
                parent_obj2.name,
                obj_plane2,
                parent_plane2,
                margin=margin2,
                rev_normal=rev_normal2,
            )
            stability.snap_against(
                state.trimesh_scene,
                obj_name,
                parent_obj1.name,
                obj_plane1,
                parent_plane1,
                margin=margin1,
                rev_normal=rev_normal1,
            )
        else:
            face_mask = tagging.tagged_face_mask(parent_obj1, parent_tags1)
            stability.move_obj_random_pt(
                state, obj_name, parent_obj1.name, face_mask, parent_plane1
            )
            stability.snap_against(
                state.trimesh_scene,
                obj_name,
                parent_obj1.name,
                obj_plane1,
                parent_plane1,
                margin=margin1,
                rev_normal=rev_normal1,
            )
            stability.snap_against(
                state.trimesh_scene,
                obj_name,
                parent_obj2.name,
                obj_plane2,
                parent_plane2,
                margin=margin2,
                rev_normal=rev_normal2,
            )

    elif dof == 2:
        # assert len(parent_planes) == 1, (name, len(parent_planes))
        for i, relation_state in enumerate(obj_state.relations):
            parent_obj = state.objs[relation_state.target_name].obj
            obj_plane, parent_plane = state.planes.get_rel_state_planes(
                state, name, relation_state
            )
            if obj_plane is None:
                continue
            if parent_plane is None:
                continue
            iu.set_rotation(
                state.trimesh_scene,
                obj_name,
                (0, 0, 2 * np.pi * np.random.randint(0, 4) / 4),
            )
            face_mask = tagging.tagged_face_mask(
                parent_obj, relation_state.relation.parent_tags
            )

            match relation_state.relation:
                case cl.CoPlanar:
                    stability.snap_against(
                        state.trimesh_scene,
                        obj_name,
                        parent_obj.name,
                        obj_plane,
                        parent_plane,
                        margin=margin,
                        rev_normal=relation_state.relation.rev_normal,
                    )

                case cl.StableAgainst(_, parent_tags, margin, _check_z, rev_normal):
                    stability.move_obj_random_pt(
                        state, obj_name, parent_obj.name, face_mask, parent_plane
                    )
                    stability.snap_against(
                        state.trimesh_scene,
                        obj_name,
                        parent_obj.name,
                        obj_plane,
                        parent_plane,
                        margin=margin,
                        rev_normal=rev_normal,
                    )

                case cl.SupportedBy(_, parent_tags):
                    stability.move_obj_random_pt(
                        state, obj_name, parent_obj.name, face_mask, parent_plane
                    )
                    stability.snap_against(
                        state.trimesh_scene,
                        obj_name,
                        parent_obj.name,
                        obj_plane,
                        parent_plane,
                        margin=0,
                        rev_normal=False,
                    )
                case _:
                    raise NotImplementedError
    return parent_planes

    Apply the relation constraints to the object. Place it in the scene according to the constraints.
    
infinigen/core/constraints/example_solver/geometry/dof.py