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
//! Convex hull algorithm implementations (Pure Rust)
//!
//! This module provides different algorithms for computing convex hulls,
//! each with their own strengths and use cases. All implementations are
//! pure Rust with no external C library dependencies.
//!
//! # Available Algorithms
//!
//! ## Quickhull (Pure Rust)
//! - **Module**: [`quickhull`]
//! - **Dimensions**: Any (1D, 2D, 3D, nD)
//! - **Time Complexity**: O(n log n) for 2D/3D, O(n^⌊d/2⌋) for d dimensions
//! - **Use Case**: General purpose, robust for all dimensions
//! - **Features**: Pure Rust, handles degenerate cases, provides facet equations
//!
//! ## Graham Scan (Pure Rust)
//! - **Module**: [`graham_scan`]
//! - **Dimensions**: 2D only
//! - **Time Complexity**: O(n log n)
//! - **Use Case**: Educational, guaranteed output order
//! - **Features**: Simple to understand, produces vertices in counterclockwise order
//!
//! ## Jarvis March (Gift Wrapping) (Pure Rust)
//! - **Module**: [`jarvis_march`]
//! - **Dimensions**: 2D only
//! - **Time Complexity**: O(nh) where h is the number of hull vertices
//! - **Use Case**: When hull has few vertices (h << n)
//! - **Features**: Output-sensitive, good for small hulls
//!
//! ## Special Cases
//! - **Module**: [`special_cases`]
//! - **Purpose**: Handle degenerate cases (collinear points, identical points, etc.)
//! - **Features**: Robust handling of edge cases that might break standard algorithms
//!
//! # Examples
//!
//! ## Using Quickhull (Recommended)
//! ```rust
//! use scirs2_spatial::convex_hull::algorithms::quickhull::compute_quickhull;
//! use scirs2_core::ndarray::array;
//!
//! let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
//! let hull = compute_quickhull(&points.view()).expect("Operation failed");
//! println!("Hull has {} vertices", hull.vertex_indices().len());
//! ```
//!
//! ## Using Graham Scan for 2D
//! ```rust
//! use scirs2_spatial::convex_hull::algorithms::graham_scan::compute_graham_scan;
//! use scirs2_core::ndarray::array;
//!
//! let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
//! let hull = compute_graham_scan(&points.view()).expect("Operation failed");
//! println!("Hull vertices: {:?}", hull.vertex_indices());
//! ```
//!
//! ## Using Jarvis March for Small Hulls
//! ```rust
//! use scirs2_spatial::convex_hull::algorithms::jarvis_march::compute_jarvis_march;
//! use scirs2_core::ndarray::array;
//!
//! let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
//! let hull = compute_jarvis_march(&points.view()).expect("Operation failed");
//! println!("Hull computed with {} vertices", hull.vertex_indices().len());
//! ```
//!
//! ## Handling Special Cases
//! ```rust
//! use scirs2_spatial::convex_hull::algorithms::special_cases::handle_degenerate_case;
//! use scirs2_core::ndarray::array;
//!
//! // Collinear points
//! let collinear = array![[0.0, 0.0], [1.0, 0.0], [2.0, 0.0]];
//! if let Some(result) = handle_degenerate_case(&collinear.view()) {
//! let hull = result.expect("Operation failed");
//! println!("Handled degenerate case with {} vertices", hull.vertex_indices().len());
//! }
//! ```
//!
//! # Algorithm Selection Guidelines
//!
//! - **For general use**: Use Quickhull - it's robust and handles all dimensions
//! - **For 2D educational purposes**: Use Graham Scan for its simplicity
//! - **For 2D with small expected hulls**: Use Jarvis March for output-sensitive performance
//! - **For edge cases**: All algorithms automatically fall back to special case handlers
//!
//! # Performance Characteristics
//!
//! | Algorithm | 2D Time | 3D Time | nD Time | Space | Robustness |
//! |-----------|---------|---------|---------|-------|------------|
//! | Quickhull | O(n log n) | O(n log n) | O(n^⌊d/2⌋) | O(n) | High |
//! | Graham Scan | O(n log n) | N/A | N/A | O(n) | Medium |
//! | Jarvis March | O(nh) | N/A | N/A | O(n) | Medium |
//!
//! Where:
//! - n = number of input points
//! - h = number of hull vertices
//! - d = dimension
// Re-export the main computation functions for convenience
pub use compute_graham_scan;
pub use compute_jarvis_march;
pub use compute_quickhull;
pub use ;
/// Algorithm performance characteristics for selection guidance
/// Get performance characteristics for each algorithm
///
/// # Arguments
///
/// * `algorithm` - The algorithm to query
/// * `dimension` - The dimension of the problem
///
/// # Returns
///
/// * Tuple of (time_complexity, space_complexity, max_dimension)
///
/// # Examples
///
/// ```rust
/// use scirs2_spatial::convex_hull::algorithms::{get_algorithm_complexity, AlgorithmComplexity};
/// use scirs2_spatial::convex_hull::ConvexHullAlgorithm;
///
/// let (time, space, max_dim) = get_algorithm_complexity(ConvexHullAlgorithm::GrahamScan, 2);
/// assert_eq!(time, AlgorithmComplexity::NLogN);
/// assert_eq!(max_dim, Some(2));
/// ```
/// Recommend the best algorithm for given constraints
///
/// # Arguments
///
/// * `num_points` - Number of input points
/// * `dimension` - Dimension of the points
/// * `expected_hull_size` - Expected number of hull vertices (None if unknown)
///
/// # Returns
///
/// * Recommended algorithm
///
/// # Examples
///
/// ```rust
/// use scirs2_spatial::convex_hull::algorithms::recommend_algorithm;
/// use scirs2_spatial::convex_hull::ConvexHullAlgorithm;
///
/// // Large 2D dataset with small expected hull
/// let algo = recommend_algorithm(10000, 2, Some(8));
/// assert_eq!(algo, ConvexHullAlgorithm::JarvisMarch);
///
/// // 3D dataset
/// let algo = recommend_algorithm(1000, 3, None);
/// assert_eq!(algo, ConvexHullAlgorithm::Quickhull);
/// ```