• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scala-Robotics-Simulator / PPS-22-srs / #396

27 Aug 2025 11:04AM UTC coverage: 45.466% (-0.2%) from 45.671%
#396

push

github

srs-mate
chore: add exhaustive match with sensors in pretty print

1113 of 2448 relevant lines covered (45.47%)

6.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.61
/src/main/scala/io/github/srs/model/entity/dynamicentity/sensor/Sensor.scala
1
package io.github.srs.model.entity.dynamicentity.sensor
2

3
import cats.syntax.all.*
4
import io.github.srs.model.PositiveDouble
5
import io.github.srs.model.entity.dynamicentity.{ DynamicEntity, Robot }
6
import io.github.srs.model.entity.{ Orientation, Point2D }
7
import io.github.srs.model.environment.Environment
8
import io.github.srs.model.validation.Validation
9
import io.github.srs.utils.Ray.intersectRay
10
import io.github.srs.utils.SimulationDefaults.DynamicEntity.Sensor.ProximitySensor as ProximitySensorDefaults
11
import io.github.srs.model.illumination.model.ScaleFactor
12
import cats.Monad
13
import io.github.srs.model.entity.ShapeType
14

15
/**
16
 * Represents the range of a sensor.
17
 */
18
type Range = Double
19

×
20
/**
21
 * Represents a sensor that can sense the environment for a dynamic entity.
22
 * @tparam Entity
23
 *   the type of dynamic entity that the sensor can act upon.
24
 * @tparam Env
25
 *   the type of environment in which the sensor operates.
26
 */
27
trait Sensor[-Entity <: DynamicEntity, -Env <: Environment]:
28
  /**
8✔
29
   * The type of data that the sensor returns. This type can vary based on the specific sensor implementation.
30
   */
31
  type Data
32

33
  /**
34
   * The offset orientation of the sensor relative to the entity's orientation.
35
   * @return
36
   *   the orientation offset of the sensor.
37
   */
38
  def offset: Orientation
39

40
  /**
41
   * Senses the environment for the given entity and returns the data collected by the sensor.
42
   * @param entity
43
   *   the dynamic entity that the sensor is attached to.
44
   * @param env
45
   *   the environment in which the sensor operates.
46
   * @tparam F
47
   *   the effect type in which the sensing operation is performed.
48
   * @return
49
   *   a monadic effect containing the data sensed by the sensor.
50
   */
51
  def sense[F[_]: Monad](entity: Entity, env: Env): F[Data]
52

53
  private[sensor] def direction(entity: Entity): Point2D =
54
    val globalOrientation = entity.orientation.toRadians + offset.toRadians
4✔
55
    (math.cos(globalOrientation), -math.sin(globalOrientation))
24✔
56

20✔
57
  private[sensor] def origin(entity: Entity): Point2D =
58
    import Point2D.*
4✔
59
    val distance = entity.shape match
60
      case ShapeType.Circle(radius) => radius
7✔
61
      case ShapeType.Rectangle(width, height) => math.min(width, height)
19✔
62
    val origin = entity.position + direction(entity) * distance
1✔
63
    origin
33✔
64
end Sensor
2✔
65

66
/**
67
 * Represents a reading from a sensor. This case class encapsulates the sensor and the value it has sensed.
68
 * @param sensor
69
 *   the sensor that has taken the reading.
70
 * @param value
71
 *   the value sensed by the sensor.
72
 * @tparam S
73
 *   the type of sensor, which is a subtype of [[Sensor]].
74
 * @tparam A
75
 *   the type of data sensed by the sensor.
76
 */
77
final case class SensorReading[S <: Sensor[?, ?], A](sensor: S, value: A)
78

60✔
79
/**
80
 * A collection of sensor readings. This type is used to represent multiple sensor readings from a dynamic entity. It is
81
 * a vector of [[SensorReading]] instances, allowing for efficient access and manipulation of sensor data.
82
 */
83
type SensorReadings = Vector[SensorReading[? <: Sensor[?, ?], ?]]
84

85
object SensorReadings:
86

×
87
  extension (readings: SensorReadings)
88

×
89
    def prettyPrint: Vector[String] =
90
      readings
×
91
        .map(r =>
×
92
          r.sensor match
×
93
            case ProximitySensor(offset, range) =>
94
              s"Proximity (offset: ${offset.degrees}°, range: $range m) -> ${r.value}"
95
            case LightSensor(offset) => s"Light (offset: ${offset.degrees}°) -> ${r.value}"
96
            case other => s"${other.getClass.getSimpleName} -> ${r.value}",
97
        )
98

×
99
/**
100
 * A proximity sensor that can sense the distance to other entities in the environment. It calculates the distance to
101
 * the nearest entity within its range and returns a normalized value. The value is normalized to a range between 0.0
102
 * (closest) and 1.0 (farthest).
103
 * @param offset
104
 *   the offset orientation of the sensor relative to the entity's orientation.
105
 * @param distance
106
 *   the distance from the center of the entity to the sensor.
107
 * @param range
108
 *   the range of the sensor, which defines how far it can sense.
109
 * @tparam Entity
110
 *   the type of dynamic entity that the sensor can act upon.
111
 * @tparam Env
112
 *   the type of environment in which the sensor operates.
113
 */
114
final case class ProximitySensor[Entity <: DynamicEntity, Env <: Environment](
115
    override val offset: Orientation = Orientation(ProximitySensorDefaults.defaultOffset),
72✔
116
    val range: Range = ProximitySensorDefaults.defaultRange,
3✔
117
) extends Sensor[Entity, Env]:
3✔
118

119
  override type Data = Double
120

121
  private def rayEnd(entity: Entity): Point2D =
122
    import Point2D.*
4✔
123
    origin(entity) + direction(entity) * range
124

35✔
125
  override def sense[F[_]: Monad](entity: Entity, env: Env): F[Data] =
126
    Monad[F].pure:
4✔
127
      val o = origin(entity)
10✔
128
      val end = rayEnd(entity)
12✔
129

8✔
130
      val distances = env.entities
131
        .filter(!_.equals(entity))
5✔
132
        .flatMap(intersectRay(_, o, end))
7✔
133
        .filter(_ <= range)
8✔
134

8✔
135
      distances.minOption.map(_ / range).getOrElse(1.0)
136

29✔
137
end ProximitySensor
138

139
/**
140
 * A light sensor that senses the light intensity in the environment.
141
 * @param offset
142
 *   the offset orientation of the sensor relative to the entity's orientation.
143
 * @param distance
144
 *   the distance from the center of the entity to the sensor.
145
 * @param range
146
 *   the range of the sensor, which defines how far it can sense.
147
 * @tparam Entity
148
 *   the type of dynamic entity that the sensor can act upon.
149
 * @tparam Env
150
 *   the type of environment in which the sensor operates.
151
 */
152
final case class LightSensor[Entity <: DynamicEntity, Env <: Environment](
153
    offset: Orientation = Orientation(ProximitySensorDefaults.defaultOffset),
58✔
154
) extends Sensor[Entity, Env]:
6✔
155

156
  override type Data = Double
157

158
  /**
159
   * Senses the light intensity at the position of the sensor in the environment. It uses a cached light map to compute
160
   * the field of light and samples it at the sensor's position.
161
   * @param entity
162
   *   the dynamic entity that the sensor is attached to.
163
   * @param env
164
   *   the environment in which the sensor operates.
165
   * @return
166
   *   a monadic effect containing the light intensity sensed by the sensor.
167
   */
168
  override def sense[F[_]: Monad](entity: Entity, env: Env): F[Data] =
169
    Monad[F].pure:
4✔
170
      val o = origin(entity)
10✔
171
      env.lightField.sampleAtWorld(o)(using ScaleFactor.default)
12✔
172

20✔
173
end LightSensor
174

175
object Sensor:
176

×
177
  extension [E <: DynamicEntity, Env <: Environment](s: ProximitySensor[E, Env])
178

5✔
179
    /**
180
     * Validates the properties of a sensor.
181
     */
182
    def validate: Validation[ProximitySensor[E, Env]] =
183
      for _ <- PositiveDouble(s.range).validate
4✔
184
      yield s
21✔
185

3✔
186
  extension (r: Robot)
187

188
    /**
189
     * Senses all sensors of the robot in the given environment.
190
     * @param env
191
     *   the environment in which to sense.
192
     * @return
193
     *   a vector of sensor readings.
194
     */
195
    def senseAll[F[_]: Monad](env: Environment): F[SensorReadings] =
196
      r.sensors.traverse: sensor =>
4✔
197
        sensor.sense(r, env).map(reading => SensorReading(sensor, reading))
17✔
198
end Sensor
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc