def test_dict_to_object_sensor_grid(): """Test the dict_to_object method with SensorGrid objects.""" sensors = [Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1))] sg_obj = SensorGrid('sg_1', sensors) sg_dict = sg_obj.to_dict() new_sg = dict_to_object(sg_dict) assert isinstance(new_sg, SensorGrid)
def test_move(): sensor = Sensor((0, 0, 10), (0, 0, -1)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.move(pv.Vector3D(10, 20, 30)) assert sensor.pos == (10, 20, 40) assert sensor.dir == (0, 0, -1)
def test_scale(): sensor = Sensor((1, 0, 2), (1, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.scale(2) assert round(sensor.pos[0]) == 2 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 4 assert round(sensor.dir[0]) == 1 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_reflect(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.reflect(Plane(pv.Vector3D(1, 0, 0), pv.Point3D(0, 0, 0))) assert round(sensor.pos[0]) == -1 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == -2 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_rotate_xy(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.rotate_xy(90, pv.Point3D(0, 0, 0)) assert round(sensor.pos[0]) == 0 assert round(sensor.pos[1]) == 1 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == 0 assert round(sensor.dir[1]) == 2 assert round(sensor.dir[2]) == 0
def test_rotate(): sensor = Sensor((0, 0, 0), (1, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.rotate(90, pv.Vector3D(0, 1, 1), pv.Point3D(0, 0, 20)) assert round(sensor.pos[0], 3) == -14.142 assert round(sensor.pos[1]) == -10 assert round(sensor.pos[2]) == 10 assert round(sensor.dir[0], 1) == 0.0 assert round(sensor.dir[1], 3) == 0.707 assert round(sensor.dir[2], 3) == -0.707
def test_to_and_from_dict(): sensor = Sensor() sensor_dict = sensor.to_dict() assert sensor_dict == {'pos': (0, 0, 0), 'dir': (0, 0, 1)} sensor_from = Sensor.from_dict(sensor_dict) assert sensor_from == sensor
def test_rotate(): sensor = Sensor((0, 0, 0), (1, 0, 0)) sensor.rotate(90, pv.Vector3D(0, 1, 1), pv.Point3D(0, 0, 20)) assert round(sensor.pos[0], 3) == -14.142 assert round(sensor.pos[1]) == -10 assert round(sensor.pos[2]) == 10 assert round(sensor.dir[0], 1) == 0.0 assert round(sensor.dir[1], 3) == 0.707 assert round(sensor.dir[2], 3) == -0.707
def test_scale(): sensor = Sensor((1, 0, 2), (1, 0, 0)) sensor.scale(2) assert round(sensor.pos[0]) == 2 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 4 assert round(sensor.dir[0]) == 1 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_reflect(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensor.reflect(Plane(pv.Vector3D(1, 0, 0), pv.Point3D(0, 0, 0))) assert round(sensor.pos[0]) == -1 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == -2 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_rotate_xy(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensor.rotate_xy(90, pv.Point3D(0, 0, 0)) assert round(sensor.pos[0]) == 0 assert round(sensor.pos[1]) == 1 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == 0 assert round(sensor.dir[1]) == 2 assert round(sensor.dir[2]) == 0
def test_assign_values(): sensor = Sensor((0, 0, 10), (0, 0, -1)) assert sensor.pos == (0, 0, 10) assert sensor.dir == (0, 0, -1)
def test_default_values(): sensor = Sensor() str(sensor) # test the string representation hash(sensor) # test the hash-ability assert sensor.pos == (0, 0, 0) assert sensor.dir == (0, 0, 1)
def test_updating_values(): sg = SensorGrid('sg_1', sensors) # sensor is immutable - hence no assignment with pytest.raises(TypeError): sg.sensors[0] = Sensor((0, 0, 100), (0, 0, -10))
def test_move(): sensor = Sensor((0, 0, 10), (0, 0, -1)) sensor.move(pv.Vector3D(10, 20, 30)) assert sensor.pos == (10, 20, 40) assert sensor.dir == (0, 0, -1)
def test_converting_string(): sensor = Sensor(('0', '0', '10'), ('0', '0', '-1')) assert sensor.pos == (0, 0, 10) assert sensor.dir == (0, 0, -1)
"""Test SensorGrid class.""" from honeybee_radiance.sensor import Sensor from honeybee_radiance.sensorgrid import SensorGrid import ladybug_geometry.geometry3d.pointvector as pv from ladybug_geometry.geometry3d.plane import Plane from ladybug_geometry.geometry2d.pointvector import Point2D from ladybug_geometry.geometry2d.mesh import Mesh2D from ladybug_geometry.geometry3d.mesh import Mesh3D import pytest sensors = [Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1))] def test_creation(): sg = SensorGrid('sg_1', sensors) str(sg) # test string representation hash(sg) # test hashability assert sg.identifier == 'sg_1' assert len(sg) == 2 assert sg[0] == sensors[0] assert sg[1] == sensors[1] def test_updating_values(): sg = SensorGrid('sg_1', sensors) # sensor is immutable - hence no assignment with pytest.raises(TypeError): sg.sensors[0] = Sensor((0, 0, 100), (0, 0, -10))
def test_invalid_input(): with pytest.raises(AssertionError): Sensor((0, 0))
def sensor_grid_simple(directory): sensors = [Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1))] sg = SensorGrid('sg_1', sensors) dest_file = os.path.join(directory, 'sensor_grid_simple.json') with open(dest_file, 'w') as fp: json.dump(sg.to_dict(), fp, indent=4)