File size: 12,534 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
function [Pc, out] = PcCircleWithConjData(r1,v1,cov1,r2,v2,cov2,HBR,params)
% PcCircleWithConjData - Computes a Pc using the PcCircle method and adds
%                        extra conjunction data used by CARA
%
% Syntax: [Pc,out] =  PcCircleWithConjData(r1,v1,cov1,r2,v2,cov2,HBR);
%         [Pc,out] =  PcCircleWithConjData(r1,v1,cov1,r2,v2,cov2,HBR,params);
%
% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
%   Computes the Pc using the PcCircle method (see PcCircle.m for more
%   documentation) and adds conjunction data used by CARA processing. This
%   data includes semimajor/semiminor axes, clock angle, miss distance,
%   and sigma values, among other values.
%
% =========================================================================
%
% Input:
%
%    r1 - Primary object's position vector in ECI coordinates         [nx3]
%
%    v1 - Primary object's velocity vector in ECI coordinates  [nx3 or 1x3]
%         Note: If r1 is [nx3] but v1 is [1x3], the same v1
%               will be used for every r1
%
%    cov1 - Primary object's covariance matrix in    [nx9, 3x3xn, or 6x6xn]
%           ECI coordinate frame.
%           Note: representations are as follows:
%
%             nx9 row vectors for n primary covariances represented as
%             [(1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3)])
%                               
%             3x3xn matrices for n primary covariances
%
%             6x6xn matrices for n primary covariances
%
%             1x9 row vector which will be repeated for each primary
%             position
%
%             3x3 matrix which will be repeated for each primary position
%
%             6x6 matrix which will be repeated for each primary position
%
%    r2 - Secondary object's position vector in ECI coordinates       [nx3]
%
%    v2 - Secondary object's velocity vector in ECI            [nx3 or 1x3]
%         coordinates
%         Note: If r2 is [nx3] but v2 is [1x3], the same v2
%               will be used for every r2
%
%    cov2 - Secondary object's covariance matrix in  [nx9, 3x3xn, or 6x6xn]
%           ECI coordinate frame
%           Note: representations are as follows:
%
%             nx9 row vectors for n secondary covariances represented as
%             [(1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3)])
%                               
%             3x3xn matrices for n secondary covariances
%
%             6x6xn matrices for n secondary covariances
%
%             1x9 row vector which will be repeated for each secondary
%             position
%
%             3x3 matrix which will be repeated for each secondary position
%
%             6x6 matrix which will be repeated for each secondary position
%
%    HBR - Hard body radius                                    [1x1 or nx1]
%          Note: If the positions are given as [nx3] but HBR
%                only [1x1], the same HBR will be used for all
%                n cases
%
%    params - (Optional) Auxilliary input parameter structure, see
%             PcCircle.m documentation for a full description of the
%             parameters.
%
% =========================================================================
%
% Output:
%
%   Pc - Probability of collision
%
%   out - An auxilliary output structure which contains a number of extra
%         quantities from the Pc calculation. See PcCircle.m for the
%         nominal list of values. This calculation adds the following
%         values.
%
%     SemiMajorAxis - Larger of the sigma values of the relative miss [nx1]
%                     distance PDF on the conjunction plane (i.e. sx)
%
%     SemiMinorAxis - Smaller of the sigma values of the relative     [nx1]
%                     miss distance PDF on the conjunction plane
%                     (i.e. sz)
%
%     ClockAngle - Angle between the +x-axis and the semi-major axis  [nx1]
%                  in the conjunction plane (deg)
%
%     MissDistance - Distance between the r1 and r2 vectors           [nx1]
%
%     x1Sigma - Mahalanobis distance when projected into the          [nx1]
%               conjunction plane
%
%     RadialSigma - Combined covariance uncertainty in the primary's  [nx1]
%                   radial direction
%
%     InTrackSigma - Combined covariance uncertainty in the primary's [nx1]
%                    in-track direction
%
%     CrossTrackSigma - Combined covariance uncertainty in the        [nx1]
%                       primary's cross-track direction
%
%     CondNumPrimary - Condition number of the primary covariance     [nx1]
%                      matrix
%
%     CondNumSecondary - Condition number of the secondary covariance [nx1]
%                        matrix
%
%     CondNumCombined - Condition number of the combined covariance   [nx1]
%                       matrix
%
%     CondNumProjected - Condition number of the combined covariance  [nx1]
%                        after it has been projected onto the relative
%                        encounter frame
%
%     RelativePhaseAngle - Angle between the velocity vectors         [nx1]
%
% =========================================================================
%
% Disclaimer:
%
%    No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY
%    WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY,
%    INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE
%    WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
%    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM
%    INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
%    FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO
%    THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER,
%    CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT
%    OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY
%    OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE.
%    FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES
%    REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE,
%    AND DISTRIBUTES IT "AS IS."
%
%    Waiver and Indemnity:  RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS
%    AGAINST THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND
%    SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT.  IF RECIPIENT'S USE OF
%    THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES,
%    EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM
%    PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT
%    SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE UNITED
%    STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY
%    PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW.  RECIPIENT'S SOLE
%    REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL
%    TERMINATION OF THIS AGREEMENT.
%
% =========================================================================
%
% Initial version: Dec 2023;  Latest update: Aug 2025
%
% ----------------- BEGIN CODE -----------------

    persistent pathsAdded
    if isempty(pathsAdded)
        [p,~,~] = fileparts(mfilename('fullpath'));
        s = what(fullfile(p, 'Utils')); addpath(s.path);
        pathsAdded = true;
    end
    
    % Number of input arguments
    Nargin = nargin;
    
    % Check for the parameters structure
    if Nargin == 7 || (Nargin == 8 && isempty(params))
        params = [];
    elseif Nargin ~= 8
        error('PcCircleWithConjData:InvalidNumberOfArguments','Invalid number of arguments passed in');
    end

    % Call PcCircle to get the base calculation out of the way
    [Pc, out] = PcCircle(r1,v1,cov1,r2,v2,cov2,HBR,params);
    
    % Get the adjusted input parameters from the PcCircle output structure
    r1 = out.r1;
    v1 = out.v1;
    cov1 = out.cov1;
    r2 = out.r2;
    v2 = out.v2;
    cov2 = out.cov2;
    
    % Miss distance
    r = r1 - r2;
    out.MissDistance = sqrt(r(:,1).^2 + r(:,2).^2 + r(:,3).^2);
    
    % Semimajor/semiminor axis values
    out.SemiMajorAxis = out.sx;
    out.SemiMinorAxis = out.sz;
    
    % Clock angle
    V1 = out.EigV1;
    ClockAngle = atan2(V1(:,2),V1(:,1)) .* 180/pi;
    adjDown = ClockAngle >=  90;
    adjUp   = ClockAngle <= -90;
    ClockAngle(adjDown) = ClockAngle(adjDown) - 180;
    ClockAngle(adjUp)   = ClockAngle(adjUp)   + 180;
    out.ClockAngle = ClockAngle;
    
    % x1 Sigma
    sx = out.sx;
    sz = out.sz;
    out.x1Sigma = (sx.*sz) ./ sqrt((sz.*cosd(ClockAngle)).^2 + (sx.*sind(ClockAngle)).^2);
    
    % Compute uncertainties in the primary RIC frame
    CombCov = cov1 + cov2;
    % Compute the angular momentum vector for the primary object
    h1 = cross(r1,v1,2);
    % Compute the primary object RIC frame
    Rhat = r1./sqrt(r1(:,1).^2 + r1(:,2).^2 + r1(:,3).^2);
    Chat = h1./sqrt(h1(:,1).^2 + h1(:,2).^2 + h1(:,3).^2);
    Ihat = cross(Chat,Rhat,2);
    eci2ricPrim = [Rhat Ihat Chat];
    % Project the combined covariance in the RIC primary frame
    CovCombRICPrim = Product3x3(eci2ricPrim,Product3x3(CombCov(:,1:1:9),eci2ricPrim(:,[1 4 7 2 5 8 3 6 9])));
    out.RadialSigma = sqrt(CovCombRICPrim(:,1)) .* 1000;
    out.InTrackSigma = sqrt(CovCombRICPrim(:,5)) .* 1000;
    out.CrossTrackSigma = sqrt(CovCombRICPrim(:,9)) .* 1000;
    
    % Relative phase angle (in degrees)
    norm_v1 = sqrt(v1(:,1).^2 + v1(:,2).^2 + v1(:,3).^2);
    norm_v2 = sqrt(v2(:,1).^2 + v2(:,2).^2 + v2(:,3).^2);
    v1dotv2 = v1(:,1).*v2(:,1) + v1(:,2).*v2(:,2) + v1(:,3).*v2(:,3);
    out.RelativePhaseAngle = acos(v1dotv2./(norm_v1.*norm_v2)) .* 180/pi;
    
    % Calculate the condition numbers
    numRows = size(r1,1);
    out.CondNumPrimary = nan(numRows,1);
    out.CondNumSecondary = nan(numRows,1);
    out.CondNumCombined = nan(numRows,1);
    out.CondNumProjected = nan(numRows,1);
    for i = 1:numRows
        C1 = [cov1(i,1:3); cov1(i,4:6); cov1(i,7:9)];
        C2 = [cov2(i,1:3); cov2(i,4:6); cov2(i,7:9)];
        Ccomb = C1 + C2;
        
        % Input vectors
        r = r1(i,:) - r2(i,:);
        v = v1(i,:) - v2(i,:);
        h = cross(r,v);
        
        % Relative encounter frame
        y = v / norm(v);
        z = h / norm(h);
        x = cross(y,z);
        
        % Transformation matrix from ECI to relative encounter plane
        eci2xyz = [x; y; z];
        
        % Transform combined ECI covariance into xyz
        covcombxyz = eci2xyz * Ccomb * eci2xyz';

        

        % Projection onto xz-plane in the relative encounter frame

        Cp = [1 0 0; 0 0 1] * covcombxyz * [1 0 0; 0 0 1]';
        
        % Condition numbers
        try
            out.CondNumPrimary(i) = cond(C1);
        catch
            out.CondNumPrimary(i) = NaN;
        end
        try
            out.CondNumSecondary(i) = cond(C2);
        catch
            out.CondNumSecondary(i) = NaN;
        end
        try
            out.CondNumCombined(i) = cond(Ccomb);
        catch
            out.CondNumCombined(i) = NaN;
        end
        try
            out.CondNumProjected(i) = cond(Cp);
        catch
            out.CondNumProjected(i) = NaN;
        end
    end
end

% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history 
% shown below:
%
% ----------------- CHANGE HISTORY ----------------------------------------
% Developer      |    Date    |     Description
% -------------------------------------------------------------------------
% L. Baars       | 12-12-2023 |  Initial version
% L. Baars       | 08-21-2025 |  Added disclaimer and updated documentation

% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================