Generate random valid positions
In the Treble SDK, you can generate random positions for sources or receivers according to certain rules. For example, you can generate positions that have a minimum distance from all surfaces in the model, from other sources or receivers, or from other points that are generated in the current generation process. The positions can either be drawn randomly from inside the model, or from within a specified -, -, or -coordinate range.
This page walks you through all necessary steps to generate random positions for your sources or receivers.
Setting up rules
The following example sets up the following rules:
- The generated points must have a minimum distance of m from all surfaces in the model.
- The generated points must have a minimum distance of m from each other.
- The generated points must have a minimum distance of m from other sources.
- The generated points must have a minimum distance of m from other receivers.
receivers_ruleset = treble.PointRuleset(
min_dist_from_surface=0.25,
min_dist_from_other_points=0.5,
min_dist_from_sources=0.5,
min_dist_from_receivers=0.5,
)
Generating points
In this example, we will work with a polygonal room that already includes two sound sources.
# Set up a polygonal room
polygon_points = [
[3.5, 0],
[2.5, 0],
[2.5, 2.5],
[0, 2.5],
[0, 3.5],
[2.5, 3.5],
[3, 6],
[3.5, 3.5],
[6, 3.5],
[6, 2.5],
[4.5, 1.5],
]
poly_room = treble.GeometryDefinitionGenerator.create_polygon_room(
points_xy=polygon_points,
height_z=2.2,
join_wall_layers=False,
)
poly_model = project.add_model("Polygon room", poly_room)
# Place two sound sources in the room
source_height = 1.1
source_pos = [treble.Point3d(3, 3, source_height), treble.Point3d(3, 1, source_height)]
source = [treble.Source.make_omni(pos, label=f"Src{idx}") for idx, pos in enumerate(source_pos)]
We use the previously defined rules to generate 15 random receiver positions around sound sources. In addition to the previously defined rules, we want to constrain the height of the generated receivers to coincide with the source height. To generate the receiver positions, we use the PointsGenerator
and its function generate_valid_points()
. This function returns coordinates that match the specified rules.
from treble_tsdk.geometry.generator import PointsGenerator
# Generate 15 random valid points inside the room
pg = treble.PointsGenerator()
n_new_points = 15
receiver_pos = pg.generate_valid_points(
poly_model,
n_new_points,
receivers_ruleset,
existing_source_points=source_pos,
z_range=(source_height, source_height),
)
Note: generate_valid_points()
aims to generate the desired number of points. However, if the specified rules are too strict, the function may not succeed and will return fewer points instead.
In a last step, we initialize Treble receivers from the generated coordinates, so they can be added to a simulation definition.
# Set up receivers
receivers = [treble.Receiver.make_mono(pos, label=f"Rcv{idx}") for idx, pos in enumerate(receiver_pos)]
Finally, let us have a look at the result. Below, you can see the two sound sources in yellow and the generated receivers in blue.