Skip to main content

ballistics_engine/
wind.rs

1use nalgebra::Vector3;
2use std::cmp::Ordering;
3use std::f64::consts::PI;
4use thiserror::Error;
5
6/// Conversion constant from KMH to MPS
7const KMH_TO_MPS: f64 = 1000.0 / 3600.0;
8
9/// THE wind-vector builder (McCoy frame: x downrange, y up, z right).
10/// Horizontal wind uses the wind-FROM convention (0 = headwind, PI/2 = from
11/// the right); `vertical_mps` is positive-updraft and lands on y UNSCALED —
12/// boundary-layer shear models scale horizontal flow only (MBA-728 decision).
13pub fn wind_vector(speed_mps: f64, direction_rad: f64, vertical_mps: f64) -> Vector3<f64> {
14    Vector3::new(
15        -speed_mps * direction_rad.cos(),
16        vertical_mps,
17        -speed_mps * direction_rad.sin(),
18    )
19}
20
21/// One downrange wind segment. `vertical_mps` (m/s, positive = updraft) feeds
22/// straight into the segment's wind vector via [`wind_vector`] (MBA-728);
23/// boundary-layer shear scales horizontal wind only, so vertical passes
24/// through unscaled wherever shear is applied on top of a segment.
25///
26/// This matches the Python WindSock interface.
27#[derive(Debug, Clone, Copy, PartialEq, Default)]
28pub struct WindSegment {
29    pub speed_kmh: f64,
30    pub angle_deg: f64,
31    pub until_m: f64,
32    pub vertical_mps: f64,
33}
34
35impl WindSegment {
36    /// The historical 3-field constructor (vertical 0.0) — used by every
37    /// pre-existing call site.
38    pub fn new(speed_kmh: f64, angle_deg: f64, until_m: f64) -> Self {
39        Self {
40            speed_kmh,
41            angle_deg,
42            until_m,
43            vertical_mps: 0.0,
44        }
45    }
46}
47
48/// Sort wind segments by their `until_distance_m` threshold.
49///
50/// Shared by [`WindSock`] and the low-level trajectory integrator so every segmented-wind path
51/// applies the same interval ordering.
52pub(crate) fn sort_wind_segments_by_distance(segments: &mut [WindSegment]) {
53    segments.sort_by(|a, b| match (a.until_m.is_nan(), b.until_m.is_nan()) {
54        (true, true) => Ordering::Equal,
55        (true, false) => Ordering::Greater,
56        (false, true) => Ordering::Less,
57        (false, false) => {
58            a.until_m
59                .partial_cmp(&b.until_m)
60                .expect("non-NaN distances are ordered")
61        }
62    });
63}
64
65/// Which [`WindSegment`] field failed validation (MBA-1338).
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum WindSegmentField {
68    SpeedKmh,
69    AngleDeg,
70    UntilM,
71    VerticalMps,
72}
73
74impl WindSegmentField {
75    /// The struct field name, as it appears in error messages and the public API.
76    pub fn name(self) -> &'static str {
77        match self {
78            WindSegmentField::SpeedKmh => "speed_kmh",
79            WindSegmentField::AngleDeg => "angle_deg",
80            WindSegmentField::UntilM => "until_m",
81            WindSegmentField::VerticalMps => "vertical_mps",
82        }
83    }
84}
85
86impl std::fmt::Display for WindSegmentField {
87    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88        f.write_str(self.name())
89    }
90}
91
92/// The validation rule a [`WindSegment`] field violated (MBA-1338).
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub enum WindSegmentRule {
95    /// The value must be finite (`angle_deg`, `vertical_mps`).
96    Finite,
97    /// The value must be finite and `>= 0` (`speed_kmh`).
98    FiniteAndNonNegative,
99    /// The value must be finite and `> 0` (`until_m`).
100    FiniteAndPositive,
101}
102
103impl std::fmt::Display for WindSegmentRule {
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        // Phrasing matches the historical String errors exactly so consumers that surface
106        // Display output (FastSolution, solver boundaries, bindings) see unchanged messages.
107        f.write_str(match self {
108            WindSegmentRule::Finite => "finite",
109            WindSegmentRule::FiniteAndNonNegative => "finite and non-negative",
110            WindSegmentRule::FiniteAndPositive => "finite and greater than zero",
111        })
112    }
113}
114
115/// A malformed [`WindSegment`] rejected at a checked construction/solve boundary (MBA-1338).
116///
117/// `index` is the segment's position in the vector **as supplied by the caller** —
118/// validation runs before any sorting or normalization, so the index always refers to
119/// the caller's own ordering.
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
121#[error("wind.segments[{index}].{field} must be {rule}")]
122pub struct WindSegmentError {
123    pub index: usize,
124    pub field: WindSegmentField,
125    pub rule: WindSegmentRule,
126}
127
128/// Validate every segment, reporting the first violation as a typed error carrying the
129/// caller's segment index, the offending field, and the violated rule (MBA-1338).
130///
131/// Checked APIs ([`WindSock::try_new`], `try_integrate_trajectory`,
132/// `try_solve_trajectory_rust`) call this before sorting, vector precomputation, or
133/// producing any trajectory points.
134pub fn validate_wind_segments(segments: &[WindSegment]) -> Result<(), WindSegmentError> {
135    for (index, segment) in segments.iter().enumerate() {
136        let err = |field, rule| Err(WindSegmentError { index, field, rule });
137
138        if !segment.speed_kmh.is_finite() || segment.speed_kmh < 0.0 {
139            return err(
140                WindSegmentField::SpeedKmh,
141                WindSegmentRule::FiniteAndNonNegative,
142            );
143        }
144        if !segment.angle_deg.is_finite() {
145            return err(WindSegmentField::AngleDeg, WindSegmentRule::Finite);
146        }
147        if !segment.until_m.is_finite() || segment.until_m <= 0.0 {
148            return err(WindSegmentField::UntilM, WindSegmentRule::FiniteAndPositive);
149        }
150        if !segment.vertical_mps.is_finite() {
151            return err(WindSegmentField::VerticalMps, WindSegmentRule::Finite);
152        }
153    }
154
155    Ok(())
156}
157
158/// Wind condition handler for trajectory calculations
159#[derive(Debug, Clone)]
160pub struct WindSock {
161    /// Sorted wind segments by distance
162    winds: Vec<WindSegment>,
163    /// Precomputed wind vector for each segment (parallel to `winds`). The Monte-Carlo RK4
164    /// kernel queries wind 4x per step, so caching avoids recomputing sin/cos every call.
165    wind_vecs: Vec<Vector3<f64>>,
166    /// Current segment index
167    current: usize,
168    /// Distance where next segment starts
169    next_range: f64,
170    /// Current wind vector
171    current_vec: Vector3<f64>,
172    /// Validation result captured before sorting, preserving the caller's segment index.
173    validation_error: Option<WindSegmentError>,
174}
175
176impl WindSock {
177    /// Create a new WindSock from wind segments.
178    ///
179    /// **Legacy/unchecked entry point** (MBA-1338): this constructor is infallible for API
180    /// compatibility — a malformed segment is captured as a deferred error that the solver
181    /// consumes at its pre-integration validation boundary, but a direct library consumer
182    /// gets a constructed sock with no structured signal. Prefer [`WindSock::try_new`]
183    /// (or `TryFrom<Vec<WindSegment>>`), which rejects malformed segments up front.
184    ///
185    /// Args:
186    ///     segments: List of (speed_kmh, angle_deg, until_distance_m) tuples
187    pub fn new(segments: Vec<WindSegment>) -> Self {
188        // Validate before sorting so an error can identify the segment index supplied by
189        // the caller; defer it rather than fail (see doc above).
190        let validation_error = validate_wind_segments(&segments).err();
191        Self::build_unchecked(segments, validation_error)
192    }
193
194    /// Create a new WindSock, rejecting malformed segments with a typed error (MBA-1338).
195    ///
196    /// Validation runs **before** sorting and wind-vector precomputation, and the error's
197    /// `index` refers to the caller's own segment ordering. Rejects non-finite or negative
198    /// `speed_kmh`, non-finite `angle_deg`, non-finite or non-positive `until_m`, and
199    /// non-finite `vertical_mps`.
200    pub fn try_new(segments: Vec<WindSegment>) -> Result<Self, WindSegmentError> {
201        validate_wind_segments(&segments)?;
202        Ok(Self::build_unchecked(segments, None))
203    }
204
205    /// Shared constructor body: sort, precompute vectors, seed the cursor.
206    fn build_unchecked(
207        mut segments: Vec<WindSegment>,
208        validation_error: Option<WindSegmentError>,
209    ) -> Self {
210        // Sort segments by distance, handling NaN safely by treating it as greater than any value
211        sort_wind_segments_by_distance(&mut segments);
212
213        // Precompute each segment's wind vector once (depends only on its speed/angle).
214        let wind_vecs: Vec<Vector3<f64>> = segments.iter().map(Self::calc_vec).collect();
215
216        let (current, next_range, current_vec) = if segments.is_empty() {
217            (0, f64::INFINITY, Vector3::zeros())
218        } else {
219            (0, segments[0].until_m, wind_vecs[0])
220        };
221
222        WindSock {
223            winds: segments,
224            wind_vecs,
225            current,
226            next_range,
227            current_vec,
228            validation_error,
229        }
230    }
231
232    /// Return any malformed-segment error captured before normalization.
233    pub(crate) fn validate_segments(&self) -> Result<(), String> {
234        self.validation_error
235            .map_or(Ok(()), |err| Err(err.to_string()))
236    }
237
238    /// Calculate wind vector from wind segment
239    fn calc_vec(seg: &WindSegment) -> Vector3<f64> {
240        // Convert kmh to m/s
241        let speed_mps = seg.speed_kmh * KMH_TO_MPS;
242        // Preserve the historical multiply-then-divide result for ordinary angles, while
243        // avoiding intermediate overflow for otherwise-valid finite values near f64::MAX.
244        let angle_rad = if seg.angle_deg.abs() <= f64::MAX / PI {
245            seg.angle_deg * PI / 180.0
246        } else {
247            seg.angle_deg / 180.0 * PI
248        };
249
250        // Wind convention (matching trajectory coordinates):
251        // 0° = headwind (from front, affects -x downrange)
252        // 90° = wind from right (affects -z lateral)
253        // 180° = tailwind (from back, affects +x downrange)
254        // 270° = wind from left (affects +z lateral)
255        //
256        // McCoy convention: x=downrange, y=vertical, z=lateral. Vertical (MBA-728) passes
257        // straight through per-segment; it is not derived from speed_kmh/angle_deg.
258        wind_vector(speed_mps, angle_rad, seg.vertical_mps)
259    }
260
261    /// Upper bound (m/s) on the wind speed any segment can contribute, horizontal
262    /// plus vertical. Feeds the solver's integration divergence guard (MBA-1293).
263    pub fn max_speed_mps(&self) -> f64 {
264        self.winds
265            .iter()
266            .map(|seg| seg.speed_kmh.abs() * KMH_TO_MPS + seg.vertical_mps.abs())
267            .fold(0.0, f64::max)
268    }
269
270    /// Crosswind at the muzzle in the aerodynamic-jump convention: positive means wind from
271    /// the right. `None` means there are no segmented winds, while a real zero-crosswind
272    /// segment returns `Some(0.0)` so callers do not incorrectly fall back to scalar wind.
273    pub(crate) fn muzzle_crosswind_from_right_mps(&self) -> Option<f64> {
274        (!self.winds.is_empty()).then(|| -self.vector_for_range_stateless(0.0)[2])
275    }
276
277    /// Get wind vector for a given range
278    ///
279    /// Note: This modifies internal state and expects monotonically increasing ranges
280    /// For trajectory integration, we need a stateless version
281    pub fn vector_for_range(&mut self, range_m: f64) -> Vector3<f64> {
282        // Handle NaN
283        if range_m.is_nan() {
284            return Vector3::zeros();
285        }
286
287        // Advance the cursor across however many segments the query skipped (a single `if`
288        // returned a stale vector when a monotonic query jumped past a whole short segment).
289        while range_m >= self.next_range && self.current < self.winds.len() {
290            self.current += 1;
291            if self.current >= self.winds.len() {
292                self.current_vec = Vector3::zeros();
293                self.next_range = f64::INFINITY;
294            } else {
295                self.current_vec = self.wind_vecs[self.current];
296                self.next_range = self.winds[self.current].until_m;
297            }
298        }
299
300        self.current_vec
301    }
302
303    /// Get wind vector for a given range (stateless version)
304    ///
305    /// This version doesn't modify internal state and is safe for numerical integration
306    /// where the same range might be queried multiple times or out of order
307    pub fn vector_for_range_stateless(&self, range_m: f64) -> Vector3<f64> {
308        // Handle NaN
309        if range_m.is_nan() {
310            return Vector3::zeros();
311        }
312
313        // Find the appropriate segment (precomputed vector — no per-call trig).
314        for (i, segment) in self.winds.iter().enumerate() {
315            if range_m < segment.until_m {
316                return self.wind_vecs[i];
317            }
318        }
319
320        // Beyond all segments
321        Vector3::zeros()
322    }
323}
324
325/// Checked conversion mirroring [`WindSock::try_new`] (MBA-1338).
326impl TryFrom<Vec<WindSegment>> for WindSock {
327    type Error = WindSegmentError;
328
329    fn try_from(segments: Vec<WindSegment>) -> Result<Self, Self::Error> {
330        WindSock::try_new(segments)
331    }
332}
333
334/// Parse a `"SPEED:ANGLE:UNTIL_DISTANCE[:VERTICAL]"` string into a [`WindSegment`]
335/// `(speed_kmh, angle_deg, until_distance_m, vertical_mps)`.
336///
337/// `imperial`: when true, SPEED is mph and UNTIL_DISTANCE is yards; otherwise
338/// SPEED is m/s and UNTIL_DISTANCE is meters. ANGLE is always degrees in the
339/// wind-FROM convention (0 = headwind, 90 = from the right). The optional 4th
340/// field, VERTICAL, is ALWAYS m/s (positive = updraft, raises POI) regardless of
341/// `imperial` — it does not follow --units, matching how [`WindSegment::vertical_mps`]
342/// stores it. This speed-in-display-units-but-vertical-always-m/s asymmetry is
343/// unit-honest (it mirrors the struct field name) even though it reads oddly next
344/// to SPEED. Omitting the 4th field keeps the historical 3-field behavior
345/// (vertical wind 0.0). Shared by the CLI (`--wind-segment`) and the WASM
346/// front-ends so they parse identically.
347pub fn parse_wind_segment_str(s: &str, imperial: bool) -> Result<WindSegment, String> {
348    parse_wind_segment_str_detailed(s, imperial).map(|(segment, _)| segment)
349}
350
351/// [`parse_wind_segment_str`] plus clock-form provenance (MBA-1367/MBA-1368): the
352/// second tuple element is true when the ANGLE token used a marked clock form
353/// (`3oc`, `10h30`) rather than plain degrees — earth-fixed compass mode must reject
354/// clock positions, which are shooter-relative by definition. Error strings stay in
355/// the legacy `String` family (pre-MBA-1338 boundary): the typed
356/// [`WindDirectionParseError`] converts to a plain message here.
357pub fn parse_wind_segment_str_detailed(
358    s: &str,
359    imperial: bool,
360) -> Result<(WindSegment, bool), String> {
361    let parts: Vec<&str> = s.split(':').collect();
362    if parts.len() != 3 && parts.len() != 4 {
363        return Err(format!(
364            "invalid wind segment '{s}': expected SPEED:ANGLE:UNTIL_DISTANCE[:VERTICAL] \
365             (three or four colon-separated numbers; the optional 4th field VERTICAL is always \
366             m/s, positive = updraft, regardless of --units)"
367        ));
368    }
369    let num = |i: usize, name: &str| -> Result<f64, String> {
370        parts[i].trim().parse::<f64>().map_err(|_| {
371            format!("invalid wind segment '{s}': {name} '{}' is not a number", parts[i])
372        })
373    };
374    let speed = num(0, "speed")?;
375    // ANGLE additionally accepts the colon-FREE marked clock forms (`3oc`, `10h30`) —
376    // the colon form is impossible here because this grammar is colon-delimited
377    // (`10:30` inside a segment stays SPEED=10, ANGLE=30, exactly as it always has).
378    let (angle, angle_was_clock) = match parse_wind_direction(parts[1]) {
379        Ok(parsed) => (parsed.degrees, parsed.was_clock),
380        Err(WindDirectionParseError::Unrecognized(_)) => {
381            return Err(format!(
382                "invalid wind segment '{s}': angle '{}' is not a number or a clock \
383                 position like 3oc or 10h30",
384                parts[1]
385            ))
386        }
387        Err(e) => return Err(format!("invalid wind segment '{s}': {e}")),
388    };
389    let until = num(2, "until-distance")?;
390    let vertical = if parts.len() == 4 {
391        num(3, "vertical")?
392    } else {
393        0.0
394    };
395    if !speed.is_finite() || !angle.is_finite() || !until.is_finite() || !vertical.is_finite() {
396        return Err(format!(
397            "invalid wind segment '{s}': speed, angle, until-distance, and vertical (m/s, \
398             positive = updraft) must be finite numbers"
399        ));
400    }
401    if speed < 0.0 {
402        return Err(format!("invalid wind segment '{s}': speed must be >= 0"));
403    }
404    if until <= 0.0 {
405        return Err(format!("invalid wind segment '{s}': until-distance must be > 0"));
406    }
407    let (speed_kmh, until_m) = if imperial {
408        (speed * 1.609344, until * 0.9144) // mph -> km/h, yards -> meters
409    } else {
410        (speed * 3.6, until) // m/s -> km/h, meters -> meters
411    };
412    let mut segment = WindSegment::new(speed_kmh, angle, until_m);
413    segment.vertical_mps = vertical;
414    Ok((segment, angle_was_clock))
415}
416
417/// Which frame wind directions are ENTERED in (MBA-1368). `Shooter` (the default) is
418/// today's behavior: wind-FROM angles relative to the line of fire (0 = headwind).
419/// `Compass` treats every entered wind direction — the single direction AND every
420/// segment — as an absolute earth-fixed bearing (0 = north), derived shooter-relative
421/// once at the input boundary as `bearing - shot_azimuth` (wind-FROM both sides), so
422/// the physics downstream of [`wind_vector`] never sees a bearing.
423#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
424pub enum WindReference {
425    #[default]
426    Shooter,
427    Compass,
428}
429
430impl WindReference {
431    /// Parses the CLI/WASM token (`shooter` | `compass`, case-insensitive).
432    pub fn parse(token: &str) -> Result<Self, String> {
433        match token.trim().to_ascii_lowercase().as_str() {
434            "shooter" => Ok(Self::Shooter),
435            "compass" => Ok(Self::Compass),
436            other => Err(format!(
437                "invalid wind reference '{other}': expected 'shooter' or 'compass'"
438            )),
439        }
440    }
441}
442
443/// Earth-fixed compass bearing (degrees, 0 = north) -> shooter-relative wind-FROM
444/// degrees against the shot azimuth (MBA-1368), normalized to [0, 360). A wind FROM
445/// north (bearing 0) with the shot fired due north (azimuth 0) is a pure headwind
446/// (relative 0 — the 0.19.0 sign convention); a bearing 90 (from east) on a northbound
447/// shot is wind from the shooter's RIGHT (relative 90).
448pub fn compass_bearing_to_shooter_relative_deg(bearing_deg: f64, shot_azimuth_deg: f64) -> f64 {
449    (bearing_deg - shot_azimuth_deg).rem_euclid(360.0)
450}
451
452/// [`compass_bearing_to_shooter_relative_deg`] in radians (the solve-json v1 surface
453/// is SI/radians end to end).
454pub fn compass_bearing_to_shooter_relative_rad(bearing_rad: f64, shot_azimuth_rad: f64) -> f64 {
455    (bearing_rad - shot_azimuth_rad).rem_euclid(2.0 * PI)
456}
457
458/// A wind direction as entered at an input boundary (MBA-1367): the resolved degrees
459/// in the wind-FROM convention (0 = headwind, 90 = from the right — post-0.19.0), plus
460/// whether it was entered as a clock position. Clock positions are shooter-relative
461/// BY DEFINITION (12 o'clock = dead ahead), so `was_clock` is what lets the
462/// earth-fixed compass mode (MBA-1368) reject them.
463#[derive(Debug, Clone, Copy, PartialEq)]
464pub struct ParsedWindDirection {
465    pub degrees: f64,
466    pub was_clock: bool,
467}
468
469/// Typed parse errors for [`parse_wind_direction`] (internal family; legacy `String`
470/// boundaries like [`parse_wind_segment_str`] convert at the boundary per the
471/// MBA-1338 layering).
472#[derive(Debug, Error, PartialEq, Eq)]
473pub enum WindDirectionParseError {
474    #[error(
475        "invalid wind direction '{0}': expected degrees (e.g. 90) or a clock position \
476         like 3oc or 10h30 (12oc = headwind)"
477    )]
478    Unrecognized(String),
479    #[error("invalid clock position '{0}': hour must be 1-12")]
480    HourOutOfRange(String),
481    #[error("invalid clock position '{0}': minutes must be 00-59")]
482    MinutesOutOfRange(String),
483}
484
485/// Parses one wind-direction token (MBA-1367): a bare number is DEGREES exactly as it
486/// always was; marked clock forms are `<H>oc` (e.g. `3oc`) and `<H>h<MM>` (e.g.
487/// `10h30`), case-insensitive, mapping to `(H % 12) * 30 + MM * 0.5` degrees in the
488/// wind-FROM convention (12 o'clock = headwind = 0°, post-0.19.0). H must be 1-12 and
489/// MM 0-59. The colon form `<H>:<MM>` is deliberately NOT accepted here — it is legal
490/// only where a token cannot be colon-delimited grammar, i.e. standalone flags
491/// ([`parse_wind_direction_standalone`]); inside `--wind-segment`, `10:30` keeps its
492/// historical SPEED:ANGLE meaning.
493pub fn parse_wind_direction(token: &str) -> Result<ParsedWindDirection, WindDirectionParseError> {
494    let raw = token.trim();
495
496    // Bare number = degrees, byte-for-byte the pre-MBA-1367 acceptance (f64::from_str,
497    // exactly what the clap f64 args and the segment parser used).
498    if let Ok(degrees) = raw.parse::<f64>() {
499        return Ok(ParsedWindDirection {
500            degrees,
501            was_clock: false,
502        });
503    }
504
505    let lower = raw.to_ascii_lowercase();
506    let clock = |hour_str: &str, minute_str: Option<&str>| -> Result<ParsedWindDirection, WindDirectionParseError> {
507        let hour: u32 = hour_str
508            .parse()
509            .map_err(|_| WindDirectionParseError::Unrecognized(raw.to_string()))?;
510        if !(1..=12).contains(&hour) {
511            return Err(WindDirectionParseError::HourOutOfRange(raw.to_string()));
512        }
513        let minutes: u32 = match minute_str {
514            Some(m) if !m.is_empty() && m.len() <= 2 && m.bytes().all(|b| b.is_ascii_digit()) => {
515                m.parse()
516                    .map_err(|_| WindDirectionParseError::Unrecognized(raw.to_string()))?
517            }
518            Some(_) => return Err(WindDirectionParseError::Unrecognized(raw.to_string())),
519            None => 0,
520        };
521        if minutes > 59 {
522            return Err(WindDirectionParseError::MinutesOutOfRange(raw.to_string()));
523        }
524        Ok(ParsedWindDirection {
525            degrees: f64::from(hour % 12) * 30.0 + f64::from(minutes) * 0.5,
526            was_clock: true,
527        })
528    };
529
530    if let Some(hour_str) = lower.strip_suffix("oc") {
531        // Reject a nested separator ("10h30oc" is nonsense, not hour "10h30").
532        if !hour_str.is_empty() && hour_str.bytes().all(|b| b.is_ascii_digit()) {
533            return clock(hour_str, None);
534        }
535        return Err(WindDirectionParseError::Unrecognized(raw.to_string()));
536    }
537    if let Some((hour_str, minute_str)) = lower.split_once('h') {
538        if !hour_str.is_empty() && hour_str.bytes().all(|b| b.is_ascii_digit()) {
539            return clock(hour_str, Some(minute_str));
540        }
541    }
542
543    Err(WindDirectionParseError::Unrecognized(raw.to_string()))
544}
545
546/// [`parse_wind_direction`] for STANDALONE flag contexts (`--wind-direction`), which
547/// additionally accept the `<H>:<MM>` colon clock form (`10:30` = 315°) — legal there
548/// because a flag value is never colon-delimited grammar (MBA-1367).
549pub fn parse_wind_direction_standalone(
550    token: &str,
551) -> Result<ParsedWindDirection, WindDirectionParseError> {
552    let raw = token.trim();
553    if let Some((hour_str, minute_str)) = raw.split_once(':') {
554        if !hour_str.is_empty()
555            && hour_str.bytes().all(|b| b.is_ascii_digit())
556            && !minute_str.is_empty()
557            && minute_str.len() <= 2
558            && minute_str.bytes().all(|b| b.is_ascii_digit())
559        {
560            let with_h = format!("{hour_str}h{minute_str}");
561            return match parse_wind_direction(&with_h) {
562                Ok(parsed) => Ok(parsed),
563                // Re-report range errors against the token as typed, not the rewrite.
564                Err(WindDirectionParseError::HourOutOfRange(_)) => {
565                    Err(WindDirectionParseError::HourOutOfRange(raw.to_string()))
566                }
567                Err(WindDirectionParseError::MinutesOutOfRange(_)) => {
568                    Err(WindDirectionParseError::MinutesOutOfRange(raw.to_string()))
569                }
570                Err(WindDirectionParseError::Unrecognized(_)) => {
571                    Err(WindDirectionParseError::Unrecognized(raw.to_string()))
572                }
573            };
574        }
575        return Err(WindDirectionParseError::Unrecognized(raw.to_string()));
576    }
577    parse_wind_direction(raw)
578}
579
580#[cfg(test)]
581mod tests {
582    use super::*;
583
584    #[test]
585    fn segment_sort_is_stable_and_places_nan_endpoints_last() {
586        let mut segments = vec![
587            WindSegment::new(10.0, 0.0, f64::NAN),
588            WindSegment::new(20.0, 0.0, 100.0),
589            WindSegment::new(30.0, 0.0, 100.0),
590            WindSegment::new(40.0, 0.0, f64::INFINITY),
591            WindSegment::new(50.0, 0.0, f64::NEG_INFINITY),
592            WindSegment::new(60.0, 0.0, f64::NAN),
593        ];
594
595        sort_wind_segments_by_distance(&mut segments);
596
597        assert_eq!(segments[0].speed_kmh, 50.0); // -inf first
598        assert_eq!(segments[1].speed_kmh, 20.0); // equal endpoints retain input order
599        assert_eq!(segments[2].speed_kmh, 30.0);
600        assert_eq!(segments[3].speed_kmh, 40.0); // +inf after finite endpoints
601        assert_eq!(segments[4].speed_kmh, 10.0); // NaNs last and stable
602        assert_eq!(segments[5].speed_kmh, 60.0);
603        assert!(segments[4].until_m.is_nan() && segments[5].until_m.is_nan());
604    }
605
606    #[test]
607    fn test_wind_sock_empty() {
608        let sock = WindSock::new(vec![]);
609        assert_eq!(sock.vector_for_range_stateless(50.0), Vector3::zeros());
610        assert_eq!(sock.muzzle_crosswind_from_right_mps(), None);
611    }
612
613    #[test]
614    fn try_new_accepts_valid_and_empty_segments() {
615        assert!(WindSock::try_new(vec![]).is_ok());
616        let sock = WindSock::try_new(vec![WindSegment::new(16.0934, 90.0, 100.0)]).unwrap();
617        assert!(sock.vector_for_range_stateless(50.0).norm() > 0.0);
618        // TryFrom mirrors try_new.
619        assert!(WindSock::try_from(vec![WindSegment::new(10.0, 0.0, 50.0)]).is_ok());
620        assert!(WindSock::try_from(vec![WindSegment::new(f64::NAN, 0.0, 50.0)]).is_err());
621    }
622
623    #[test]
624    fn try_new_rejects_each_field_with_typed_index_field_rule() {
625        // MBA-1338 acceptance criteria: non-finite or negative speed_kmh, non-finite
626        // angle_deg, non-finite or non-positive until_m, non-finite vertical_mps.
627        let ok = WindSegment::new(10.0, 0.0, 100.0);
628        let cases: Vec<(WindSegment, WindSegmentField, WindSegmentRule)> = vec![
629            (
630                WindSegment::new(f64::NAN, 0.0, 100.0),
631                WindSegmentField::SpeedKmh,
632                WindSegmentRule::FiniteAndNonNegative,
633            ),
634            (
635                WindSegment::new(-1.0, 0.0, 100.0),
636                WindSegmentField::SpeedKmh,
637                WindSegmentRule::FiniteAndNonNegative,
638            ),
639            (
640                WindSegment::new(10.0, f64::INFINITY, 100.0),
641                WindSegmentField::AngleDeg,
642                WindSegmentRule::Finite,
643            ),
644            (
645                WindSegment::new(10.0, 0.0, 0.0),
646                WindSegmentField::UntilM,
647                WindSegmentRule::FiniteAndPositive,
648            ),
649            (
650                WindSegment::new(10.0, 0.0, f64::NEG_INFINITY),
651                WindSegmentField::UntilM,
652                WindSegmentRule::FiniteAndPositive,
653            ),
654            (
655                WindSegment {
656                    vertical_mps: f64::NAN,
657                    ..ok
658                },
659                WindSegmentField::VerticalMps,
660                WindSegmentRule::Finite,
661            ),
662        ];
663        for (bad, field, rule) in cases {
664            // Put the bad segment at index 1 behind a valid one so the reported index is
665            // meaningful (and would be perturbed if validation ran after sorting).
666            let err = WindSock::try_new(vec![ok, bad]).unwrap_err();
667            assert_eq!(err.index, 1, "index for {field}");
668            assert_eq!(err.field, field);
669            assert_eq!(err.rule, rule);
670        }
671    }
672
673    #[test]
674    fn try_new_error_index_is_the_callers_presort_index() {
675        // The bad segment sorts FIRST by until_m; the reported index must still be the
676        // caller's (2), proving validation runs before sorting.
677        let err = WindSock::try_new(vec![
678            WindSegment::new(10.0, 0.0, 500.0),
679            WindSegment::new(20.0, 0.0, 300.0),
680            WindSegment::new(f64::NAN, 0.0, 1.0),
681        ])
682        .unwrap_err();
683        assert_eq!(err.index, 2);
684        assert_eq!(err.field, WindSegmentField::SpeedKmh);
685    }
686
687    #[test]
688    fn wind_segment_error_display_matches_legacy_strings() {
689        // The deferred WindSock::new path and FastSolution surface this Display output;
690        // it must stay byte-identical to the historical String errors.
691        let err = WindSock::try_new(vec![WindSegment::new(f64::NAN, 0.0, 100.0)]).unwrap_err();
692        assert_eq!(
693            err.to_string(),
694            "wind.segments[0].speed_kmh must be finite and non-negative"
695        );
696        let err = WindSock::try_new(vec![WindSegment::new(10.0, 0.0, -5.0)]).unwrap_err();
697        assert_eq!(
698            err.to_string(),
699            "wind.segments[0].until_m must be finite and greater than zero"
700        );
701        let err = WindSock::try_new(vec![WindSegment {
702            vertical_mps: f64::INFINITY,
703            ..WindSegment::new(10.0, 0.0, 100.0)
704        }])
705        .unwrap_err();
706        assert_eq!(
707            err.to_string(),
708            "wind.segments[0].vertical_mps must be finite"
709        );
710    }
711
712    #[test]
713    fn infallible_new_still_defers_the_same_error() {
714        // Legacy behavior preserved: WindSock::new constructs, and the solver-boundary
715        // validate_segments() reports the identical message try_new would have raised.
716        let sock = WindSock::new(vec![WindSegment::new(10.0, f64::NAN, 100.0)]);
717        assert_eq!(
718            sock.validate_segments().unwrap_err(),
719            "wind.segments[0].angle_deg must be finite"
720        );
721    }
722
723    #[test]
724    fn muzzle_crosswind_distinguishes_an_explicit_zero_segment() {
725        let sock = WindSock::new(vec![WindSegment::new(0.0, 90.0, 100.0)]);
726        assert_eq!(sock.muzzle_crosswind_from_right_mps(), Some(0.0));
727    }
728
729    #[test]
730    fn muzzle_crosswind_uses_the_sorted_muzzle_segment_and_wind_from_sign() {
731        let from_right = WindSock::new(vec![
732            WindSegment::new(32.18688, 270.0, 5000.0),
733            WindSegment::new(16.09344, 90.0, 100.0),
734        ]);
735        let right_mps = from_right.muzzle_crosswind_from_right_mps().unwrap();
736        assert!((right_mps - 4.4704).abs() < 1e-12);
737
738        let from_left = WindSock::new(vec![WindSegment::new(16.09344, 270.0, 100.0)]);
739        let left_mps = from_left.muzzle_crosswind_from_right_mps().unwrap();
740        assert!((left_mps + 4.4704).abs() < 1e-12);
741    }
742
743    #[test]
744    fn test_wind_sock_single_segment() {
745        // 16.0934 kmh (10 mph) @ 90° until 100m
746        let sock = WindSock::new(vec![WindSegment::new(16.0934, 90.0, 100.0)]);
747
748        // Should have wind before 100m
749        let vec_50 = sock.vector_for_range_stateless(50.0);
750        println!("vec_50 = [{}, {}, {}]", vec_50[0], vec_50[1], vec_50[2]);
751        assert!(vec_50.norm() > 0.0);
752        // 90° wind from right (crosswind, McCoy): negative Z (lateral), zero Y, near-zero X (downrange)
753        assert!(
754            vec_50[2] < 0.0,
755            "Z (lateral) should be negative for 90° wind, got {}",
756            vec_50[2]
757        );
758        assert_eq!(vec_50[1], 0.0); // Zero Y component (vertical_mps defaults to 0.0, MBA-728)
759        assert!(
760            vec_50[0].abs() < 0.01,
761            "X (downrange) should be nearly zero for 90° wind, got {}",
762            vec_50[0]
763        );
764
765        // No wind after 100m
766        let vec_150 = sock.vector_for_range_stateless(150.0);
767        assert_eq!(vec_150, Vector3::zeros());
768    }
769
770    #[test]
771    fn test_wind_sock_multiple_segments() {
772        // Multiple wind segments (in kmh)
773        let sock = WindSock::new(vec![
774            WindSegment::new(16.0934, 90.0, 50.0),  // 10 mph @ 90° until 50m
775            WindSegment::new(24.1401, 45.0, 100.0), // 15 mph @ 45° until 100m
776            WindSegment::new(8.0467, 180.0, 200.0), // 5 mph @ 180° until 200m
777        ]);
778
779        // Test each segment
780        let vec_25 = sock.vector_for_range_stateless(25.0);
781        println!("vec_25 = [{}, {}, {}]", vec_25[0], vec_25[1], vec_25[2]);
782        assert!(vec_25.norm() > 0.0);
783        assert!(vec_25[2] < 0.0, "90° wind should have negative Z (lateral)"); // 90° wind from right
784
785        let vec_75 = sock.vector_for_range_stateless(75.0);
786        println!("vec_75 = [{}, {}, {}]", vec_75[0], vec_75[1], vec_75[2]);
787        assert!(vec_75.norm() > vec_25.norm()); // 15 mph > 10 mph
788        assert!(vec_75[0] < 0.0); // 45° wind has negative X component
789        assert!(vec_75[2] < 0.0); // 45° wind has negative Z component
790
791        let vec_150 = sock.vector_for_range_stateless(150.0);
792        println!("vec_150 = [{}, {}, {}]", vec_150[0], vec_150[1], vec_150[2]);
793        assert!(vec_150.norm() < vec_75.norm()); // 5 mph < 15 mph
794        assert!(
795            vec_150[2].abs() < 0.01,
796            "180° wind should have near-zero Z (lateral), got {}",
797            vec_150[2]
798        ); // 180° wind (from behind)
799        assert!(
800            vec_150[0] > 0.0,
801            "180° wind should have positive X (tailwind, downrange), got {}",
802            vec_150[0]
803        ); // Tailwind
804
805        let vec_250 = sock.vector_for_range_stateless(250.0);
806        assert_eq!(vec_250, Vector3::zeros()); // Beyond all segments
807    }
808
809    #[test]
810    fn test_wind_conversion() {
811        // Test conversion: 16.0934 km/h = 4.47 m/s
812        let sock = WindSock::new(vec![WindSegment::new(16.0934, 0.0, 100.0)]);
813        let vec = sock.vector_for_range_stateless(50.0);
814
815        let expected_speed = 16.0934 * KMH_TO_MPS;
816        assert!((vec.norm() - expected_speed).abs() < 0.01);
817    }
818
819    #[test]
820    fn test_wind_sock_boundary_is_upper_exclusive() {
821        // A segment's `until_distance_m` is exclusive: a query exactly at the
822        // boundary rolls to the next segment.
823        let sock = WindSock::new(vec![
824            WindSegment::new(16.0934, 90.0, 100.0),
825            WindSegment::new(32.1868, 270.0, 200.0),
826        ]);
827        // Just below 100 m -> first segment (90deg, negative Z).
828        assert!(sock.vector_for_range_stateless(99.999)[2] < 0.0);
829        // Exactly 100 m -> second segment (270deg, positive Z).
830        assert!(sock.vector_for_range_stateless(100.0)[2] > 0.0);
831        // Beyond the last boundary -> zero.
832        assert_eq!(sock.vector_for_range_stateless(200.0), Vector3::zeros());
833    }
834
835    #[test]
836    fn calc_vec_passes_through_segment_vertical_unscaled() {
837        // MBA-728: a segment's vertical_mps must land unchanged on the wind vector's Y
838        // component, independent of speed/angle.
839        let seg = WindSegment {
840            speed_kmh: 16.0934,
841            angle_deg: 90.0,
842            until_m: 100.0,
843            vertical_mps: 3.0,
844        };
845        let vec = WindSock::calc_vec(&seg);
846        assert_eq!(vec[1], 3.0);
847    }
848
849    #[test]
850    fn calc_vec_zero_vertical_segment_keeps_zero_y() {
851        // Upgrades (does not replace) the zero-Y assertions above: the historical
852        // 3-field constructor still yields vertical_mps == 0.0 -> Y == 0.0.
853        let seg = WindSegment::new(16.0934, 90.0, 100.0);
854        assert_eq!(seg.vertical_mps, 0.0);
855        let vec = WindSock::calc_vec(&seg);
856        assert_eq!(vec[1], 0.0);
857    }
858
859    #[test]
860    fn test_parse_wind_segment_str_units() {
861        // Imperial: 10 mph -> 16.0934 km/h, 100 yd -> 91.44 m.
862        let seg = parse_wind_segment_str("10:90:100", true).unwrap();
863        assert!((seg.speed_kmh - 16.09344).abs() < 1e-4);
864        assert_eq!(seg.angle_deg, 90.0);
865        assert!((seg.until_m - 91.44).abs() < 1e-4);
866
867        // Metric: 5 m/s -> 18 km/h, 200 m stays 200 m.
868        let seg = parse_wind_segment_str("5:270:200", false).unwrap();
869        assert!((seg.speed_kmh - 18.0).abs() < 1e-9);
870        assert_eq!(seg.angle_deg, 270.0);
871        assert!((seg.until_m - 200.0).abs() < 1e-9);
872
873        // Malformed inputs are rejected.
874        assert!(parse_wind_segment_str("10:90", true).is_err()); // too few fields
875        assert!(parse_wind_segment_str("10:bad:100", true).is_err()); // non-numeric
876        assert!(parse_wind_segment_str("10:90:0", true).is_err()); // zero until-distance
877        assert!(parse_wind_segment_str("-3:90:100", true).is_err()); // negative speed
878        // Non-finite values must be rejected (NaN comparisons would slip past < / <=).
879        assert!(parse_wind_segment_str("10:nan:5000", true).is_err());
880        assert!(parse_wind_segment_str("10:90:nan", true).is_err());
881        assert!(parse_wind_segment_str("inf:90:100", true).is_err());
882    }
883
884    #[test]
885    fn test_parse_wind_segment_str_vertical_field() {
886        // MBA-728: 3-field input is unchanged (backward compat) -> vertical_mps == 0.0.
887        let seg = parse_wind_segment_str("10:90:100", true).unwrap();
888        assert_eq!(seg.vertical_mps, 0.0);
889        let seg = parse_wind_segment_str("5:270:200", false).unwrap();
890        assert_eq!(seg.vertical_mps, 0.0);
891
892        // 4-field input parses the vertical component, m/s, regardless of --units.
893        let seg = parse_wind_segment_str("10:90:100:5", true).unwrap();
894        assert_eq!(seg.vertical_mps, 5.0);
895        // The rest of the fields still go through the imperial conversion.
896        assert!((seg.speed_kmh - 16.09344).abs() < 1e-4);
897        assert!((seg.until_m - 91.44).abs() < 1e-4);
898
899        // Vertical is NOT converted by --units (always m/s): metric and imperial parses of the
900        // same "...:5" 4th field land on the identical vertical_mps.
901        let seg_metric = parse_wind_segment_str("5:270:200:5", false).unwrap();
902        assert_eq!(seg_metric.vertical_mps, 5.0);
903
904        // Negative vertical (downdraft) is valid.
905        let seg_neg = parse_wind_segment_str("10:90:100:-3.5", true).unwrap();
906        assert_eq!(seg_neg.vertical_mps, -3.5);
907
908        // A non-numeric or non-finite 4th field is rejected.
909        assert!(parse_wind_segment_str("10:90:100:bad", true).is_err());
910        assert!(parse_wind_segment_str("10:90:100:nan", true).is_err());
911        assert!(parse_wind_segment_str("10:90:100:inf", true).is_err());
912
913        // Too many fields is still rejected.
914        assert!(parse_wind_segment_str("10:90:100:5:1", true).is_err());
915    }
916
917    /// MBA-1367: clock-position wind direction entry — the shared helper both the CLI
918    /// value parsers and the WASM hand-rolled arg loops call, so these host-run pins
919    /// ARE the WASM parity pins.
920    #[test]
921    fn clock_positions_map_to_wind_from_degrees() {
922        let deg = |t: &str| parse_wind_direction(t).unwrap();
923        assert_eq!(deg("3oc"), ParsedWindDirection { degrees: 90.0, was_clock: true });
924        assert_eq!(deg("6oc").degrees, 180.0);
925        assert_eq!(deg("9oc").degrees, 270.0);
926        assert_eq!(deg("12oc").degrees, 0.0); // 12 o'clock = headwind = 0° (post-0.19.0)
927        assert_eq!(deg("10h30").degrees, 315.0);
928        assert_eq!(deg("1h00").degrees, 30.0);
929        assert_eq!(deg("12h30").degrees, 15.0);
930        assert_eq!(deg("7h05").degrees, 212.5);
931        // Case-insensitive markers.
932        assert_eq!(deg("3OC").degrees, 90.0);
933        assert_eq!(deg("10H30").degrees, 315.0);
934
935        // Bare numbers stay degrees, byte-for-byte the old f64 acceptance.
936        assert_eq!(deg("90"), ParsedWindDirection { degrees: 90.0, was_clock: false });
937        assert_eq!(deg("-45").degrees, -45.0);
938        assert_eq!(deg("370.5").degrees, 370.5);
939
940        // Standalone flags additionally accept the colon form.
941        let sa = parse_wind_direction_standalone("10:30").unwrap();
942        assert_eq!(sa, ParsedWindDirection { degrees: 315.0, was_clock: true });
943        assert_eq!(parse_wind_direction_standalone("3oc").unwrap().degrees, 90.0);
944        assert!(!parse_wind_direction_standalone("90").unwrap().was_clock);
945        // ...but the non-standalone parser rejects the colon form (segment grammar).
946        assert!(matches!(
947            parse_wind_direction("10:30"),
948            Err(WindDirectionParseError::Unrecognized(_))
949        ));
950    }
951
952    #[test]
953    fn clock_positions_reject_bad_hours_and_minutes_with_helpful_messages() {
954        assert!(matches!(
955            parse_wind_direction("13oc"),
956            Err(WindDirectionParseError::HourOutOfRange(_))
957        ));
958        assert!(matches!(
959            parse_wind_direction("0oc"),
960            Err(WindDirectionParseError::HourOutOfRange(_))
961        ));
962        assert!(matches!(
963            parse_wind_direction("3h60"),
964            Err(WindDirectionParseError::MinutesOutOfRange(_))
965        ));
966        assert!(matches!(
967            parse_wind_direction_standalone("13:00"),
968            Err(WindDirectionParseError::HourOutOfRange(_))
969        ));
970        assert!(matches!(
971            parse_wind_direction_standalone("3:60"),
972            Err(WindDirectionParseError::MinutesOutOfRange(_))
973        ));
974        // Malformed shapes are Unrecognized, with a message naming the accepted forms.
975        for bad in ["oc", "hoc", "3.5oc", "3h", "3h123", "10h3x", "3:", ":30", "10:301", "x"] {
976            let err = parse_wind_direction_standalone(bad).unwrap_err();
977            assert!(
978                matches!(err, WindDirectionParseError::Unrecognized(_)),
979                "{bad}: {err}"
980            );
981        }
982        let msg = parse_wind_direction("bogus").unwrap_err().to_string();
983        assert!(msg.contains("3oc"), "{msg}");
984        assert!(msg.contains("degrees"), "{msg}");
985        let msg = parse_wind_direction("13oc").unwrap_err().to_string();
986        assert!(msg.contains("hour must be 1-12"), "{msg}");
987    }
988
989    /// MBA-1367 in the segment grammar: colon-free marked forms only; `10:30:400`
990    /// keeps its historical SPEED=10, ANGLE=30, DIST=400 meaning.
991    #[test]
992    fn segment_angle_accepts_colon_free_clock_forms_only() {
993        let clock = parse_wind_segment_str("10:3oc:400", true).unwrap();
994        let plain = parse_wind_segment_str("10:90:400", true).unwrap();
995        assert_eq!(clock, plain);
996
997        let (seg, was_clock) = parse_wind_segment_str_detailed("10:10h30:400", true).unwrap();
998        assert_eq!(seg.angle_deg, 315.0);
999        assert!(was_clock);
1000        let (_, was_clock) = parse_wind_segment_str_detailed("10:90:400", true).unwrap();
1001        assert!(!was_clock);
1002
1003        // The historical 3-numeric form is untouched (10:30 stays SPEED:ANGLE).
1004        let legacy = parse_wind_segment_str("10:30:400", true).unwrap();
1005        assert_eq!(legacy.angle_deg, 30.0);
1006        assert!((legacy.speed_kmh - 10.0 * 1.609344).abs() < 1e-12);
1007        assert!((legacy.until_m - 400.0 * 0.9144).abs() < 1e-9);
1008
1009        // Clock range errors surface through the legacy String boundary, typed text
1010        // preserved; unrecognized angles get the extended legacy message.
1011        let err = parse_wind_segment_str("10:13oc:400", true).unwrap_err();
1012        assert!(err.contains("hour must be 1-12"), "{err}");
1013        let err = parse_wind_segment_str("10:abc:400", true).unwrap_err();
1014        assert!(
1015            err.contains("angle 'abc' is not a number or a clock position"),
1016            "{err}"
1017        );
1018        // A 4-field segment with a clock angle still parses (vertical rides along).
1019        let (seg, was_clock) = parse_wind_segment_str_detailed("10:9oc:400:1.5", true).unwrap();
1020        assert_eq!(seg.angle_deg, 270.0);
1021        assert_eq!(seg.vertical_mps, 1.5);
1022        assert!(was_clock);
1023    }
1024}