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
//! Alpha shapes implementation for spatial analysis
//!
//! Alpha shapes are a generalization of convex hulls that allow for
//! non-convex boundaries by controlling the "tightness" of the shape
//! around a set of points through the alpha parameter.
use crate::error::{SpatialError, SpatialResult};
use scirs2_core::ndarray::{Array2, ArrayView1, ArrayView2};
/// Alpha shape representation for point cloud analysis
///
/// Alpha shapes provide a way to compute non-convex boundaries around
/// a set of points, controlled by the alpha parameter.
#[derive(Debug, Clone)]
pub struct AlphaShape {
/// Points defining the alpha shape
pub points: Array2<f64>,
/// Alpha parameter controlling shape tightness
pub alpha: f64,
/// Boundary edges of the alpha shape
pub edges: Vec<(usize, usize)>,
/// Triangles in the alpha shape (if applicable)
pub triangles: Vec<(usize, usize, usize)>,
}
impl AlphaShape {
/// Create a new alpha shape from a set of points
///
/// # Arguments
///
/// * `points` - Input points as a 2D array
/// * `alpha` - Alpha parameter controlling shape tightness
///
/// # Returns
///
/// * Result containing the computed alpha shape
pub fn new(points: &ArrayView2<'_, f64>, alpha: f64) -> SpatialResult<Self> {
if points.is_empty() {
return Err(SpatialError::ValueError(
"Points array cannot be empty".to_string(),
));
}
if alpha <= 0.0 {
return Err(SpatialError::ValueError(
"Alpha parameter must be positive".to_string(),
));
}
// Basic implementation - create a minimal alpha shape
// In a full implementation, this would use Delaunay triangulation
// and alpha-complex computation
let points_owned = points.to_owned();
let n_points = points_owned.nrows();
// For now, create a simple convex hull-like boundary
let mut edges = Vec::new();
let mut triangles = Vec::new();
// Simple boundary for demonstration
if n_points >= 3 {
for i in 0..n_points {
edges.push((i, (i + 1) % n_points));
}
// Add a single triangle if we have exactly 3 points
if n_points == 3 {
triangles.push((0, 1, 2));
}
}
Ok(AlphaShape {
points: points_owned,
alpha,
edges,
triangles,
})
}
/// Get the number of points in the alpha shape
pub fn num_points(&self) -> usize {
self.points.nrows()
}
/// Get the number of edges in the alpha shape boundary
pub fn num_edges(&self) -> usize {
self.edges.len()
}
/// Get the number of triangles in the alpha shape
pub fn num_triangles(&self) -> usize {
self.triangles.len()
}
/// Check if a point is inside the alpha shape
///
/// # Arguments
///
/// * `point` - Point to test
///
/// # Returns
///
/// * True if the point is inside the alpha shape
pub fn contains_point(&self, point: &ArrayView1<f64>) -> bool {
// Simplified implementation - just check if point is close to any input point
if point.len() != self.points.ncols() {
return false;
}
let tolerance = self.alpha;
for i in 0..self.points.nrows() {
let dist: f64 = point
.iter()
.zip(self.points.row(i).iter())
.map(|(&a, &b)| (a - b).powi(2))
.sum::<f64>()
.sqrt();
if dist <= tolerance {
return true;
}
}
false
}
/// Compute the area/volume of the alpha shape
///
/// # Returns
///
/// * Area (2D) or volume (3D) of the alpha shape
pub fn area(&self) -> f64 {
// Simplified implementation
// In a full implementation, this would compute the actual area/volume
// based on the triangulation
if self.triangles.is_empty() {
return 0.0;
}
// Simple area calculation for triangles in 2D
if self.points.ncols() == 2 {
let mut total_area = 0.0;
for &(i, j, k) in &self.triangles {
let p1 = self.points.row(i);
let p2 = self.points.row(j);
let p3 = self.points.row(k);
// Triangle area using cross product
let area = 0.5
* ((p2[0] - p1[0]) * (p3[1] - p1[1]) - (p3[0] - p1[0]) * (p2[1] - p1[1])).abs();
total_area += area;
}
return total_area;
}
// For 3D and higher dimensions, return a placeholder
1.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::array;
#[test]
fn test_alpha_shape_creation() {
let points = array![[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]];
let alpha_shape = AlphaShape::new(&points.view(), 1.0);
assert!(alpha_shape.is_ok());
let shape = alpha_shape.expect("Operation failed");
assert_eq!(shape.num_points(), 3);
assert_eq!(shape.alpha, 1.0);
}
#[test]
fn test_alpha_shape_contains() {
let points = array![[0.0, 0.0], [1.0, 0.0], [0.5, 1.0]];
let alpha_shape = AlphaShape::new(&points.view(), 0.5).expect("Operation failed");
let test_point = array![0.1, 0.1];
let contains = alpha_shape.contains_point(&test_point.view());
assert!(contains);
let far_point = array![10.0, 10.0];
let contains_far = alpha_shape.contains_point(&far_point.view());
assert!(!contains_far);
}
#[test]
fn test_alpha_shape_area() {
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]];
let alpha_shape = AlphaShape::new(&points.view(), 1.0).expect("Operation failed");
let area = alpha_shape.area();
assert!(area >= 0.0);
}
#[test]
fn test_invalid_alpha() {
let points = array![[0.0, 0.0], [1.0, 0.0]];
let result = AlphaShape::new(&points.view(), -1.0);
assert!(result.is_err());
}
#[test]
fn test_empty_points() {
let points = Array2::<f64>::zeros((0, 2));
let result = AlphaShape::new(&points.view(), 1.0);
assert!(result.is_err());
}
}