1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! Bevy-native `SystemParam`s for cross-frame state computation
//! ([Frame-Tree-ECS-Native § 13][1]).
//!
//! ECS-native cross-frame queries via Bevy's `ChildOf` / `Children`
//! relationship and the [`FrameTransC`] / [`FrameRotC`] /
//! [`FrameAngVelC`] components on frame entities. Mission code asks
//! for cross-frame state by passing entity handles; the
//! `SystemParam`s walk the hierarchy and dispatch through the
//! storage-agnostic [`astrodyn::FrameStorage`] algorithm shared with
//! `astrodyn_runner`.
//!
//! Mission code asks for cross-frame state by passing entity handles,
//! never `FrameId`s — the surface looks like any other Bevy
//! `SystemParam`. The shared algorithm lives in
//! [`astrodyn::frame_compute_relative_state_via_storage`] (the
//! `FrameStorage` trait abstraction described in [§ 7][2]); this
//! `SystemParam` only supplies the storage adapter.
//!
//! Two SystemParams are exposed (mirroring [§ 6][3]'s mission-code
//! catalog):
//!
//! - [`RelativeFrameState`] — general "state of `to` relative to
//! `from`" query. Returns raw `DVec3` (and the full
//! [`RefFrameState`] when the rotation/angular-velocity portion is
//! needed).
//! - [`FrameOrigin`] — specialized "origin of a frame in an ancestor
//! frame" query. Returns
//! `(Position<RootInertial>, Velocity<RootInertial>)` typed at the
//! root-inertial phantom when called against the root frame entity.
//! Sugar over `RelativeFrameState::position_velocity(root, frame)`
//! that makes the resulting frame phantom explicit in the
//! signature.
//!
//! Both `SystemParam`s walk the ECS hierarchy
//! (`Query<&ChildOf>`) over the
//! [`crate::components::FrameTransC`] / [`crate::components::FrameRotC`] /
//! [`crate::components::FrameAngVelC`] components on frame entities,
//! delegating the per-step compose to the storage-agnostic algorithm
//! shared with `astrodyn_runner`'s arena via the
//! [`astrodyn::FrameStorage`] trait.
//!
//! [1]: https://github.com/simnaut/astrodyn/wiki/Frame-Tree-ECS-Native#13-migration-sequencing
//! [2]: https://github.com/simnaut/astrodyn/wiki/Frame-Tree-ECS-Native#7-internal-algorithm-sharing-q1
//! [3]: https://github.com/simnaut/astrodyn/wiki/Frame-Tree-ECS-Native#6-mission-code-surface--systemparam-catalog-q7
use ;
use SystemParam;
use *;
use DVec3;
use crate;
/// Compute relative state between two frame entities by walking
/// Bevy's hierarchy and composing per-node states with the pure-state
/// math from `astrodyn_frames` (`incr_left`, `incr_right`, `negate`),
/// dispatched through the storage-agnostic
/// [`astrodyn::frame_compute_relative_state_via_storage`] algorithm.
///
/// Numerically identical to `astrodyn_runner::Simulation`'s arena-backed
/// `compute_relative_state` for the same scenario — both consumers
/// share the storage-agnostic algorithm via the
/// [`astrodyn::FrameStorage`] trait. Mission code reads an
/// `Entity`-keyed surface (no `FrameId`s, no frame-tree resource).
/// `FrameStorage` impl: lets the storage-agnostic algorithms in
/// `astrodyn_frames::frame_storage` operate over the ECS hierarchy + the
/// frame-state components.
/// Compute the origin (position + velocity) of a frame entity expressed
/// in a chosen ancestor frame's coordinates. ECS-native replacement for
/// `astrodyn::frame_origin` / `astrodyn::frame_origin_typed::<F>`
/// ([Frame-Tree-ECS-Native § 6][1]).
///
/// [1]: https://github.com/simnaut/astrodyn/wiki/Frame-Tree-ECS-Native#6-mission-code-surface--systemparam-catalog-q7
///
/// Internally backed by the same `Query<&ChildOf>` +
/// `Query<(&FrameTransC, &FrameRotC, &FrameAngVelC)>` walks as
/// [`RelativeFrameState`]; in fact `FrameOrigin` wraps a
/// [`RelativeFrameState`]. `FrameOrigin` is a specialized variant for
/// the common "origin of frame F in an ancestor frame" query — most
/// frequently "in the root frame," which is the form gravity,
/// integration, and `IntegOrigin`-shift sites need.
///
/// The three-method shape mirrors the design doc's catalog:
///
/// - [`FrameOrigin::origin_in_root`] —
/// `(Position<RootInertial>, Velocity<RootInertial>)`. The typed
/// surface that lifts the result into the root-inertial phantom
/// without a `from_raw_si` boundary at the call site.
/// Equivalent to
/// `frame_origin_typed::<RootInertial>(tree, root, frame)` /
/// `(rel.position_velocity(root, frame))`-then-wrap.
/// - [`FrameOrigin::origin_in`] — raw `DVec3` form for callers whose
/// ancestor isn't the root (e.g. an integration frame that's a
/// child of root, not root itself). Caller chooses the
/// ancestor entity, mirroring the arena helper's `(root, frame)`
/// parameter shape.
/// - [`FrameOrigin::origin_in_typed`] — generic-typed sibling of
/// `origin_in_root` for callers whose ancestor frame's marker is
/// some other `F: Frame` (e.g. a `PlanetInertial<P>` integration
/// frame). Caller asserts that `ancestor`'s marker is `F`; the
/// phantom-tag attachment is unchecked at runtime, mirroring
/// `astrodyn::frame_origin_typed`.
///
/// # Example
/// ```ignore
/// use bevy::prelude::*;
/// use astrodyn_bevy::prelude::*;
///
/// fn read_origin_in_root(
/// origin: FrameOrigin,
/// root: Res<RootFrameEntityR>,
/// bodies: Query<&FrameEntityC, With<MyBody>>,
/// ) -> Position<RootInertial> {
/// let body_e = bodies.single().unwrap().0;
/// let (pos, _vel) = origin.origin_in_root(root.0, body_e);
/// pos
/// }
/// ```