xtalforge-core 1.5.1

Core crystal-structure types for xtalforge: elements, lattices, sites and structures.
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#![allow(clippy::indexing_slicing, clippy::needless_range_loop)] // fixed-size numeric kernels

//! Convex polyhedra as intersections of half-spaces (Task 9.3.1, 9.4.3).
//!
//! The periodic Voronoi cell and the Wulff construction are both the bounded
//! intersection of a set of half-spaces `n . x <= d`. This module builds that
//! polyhedron by clipping a large cube face by face, and reports its volume,
//! area, centroid and per-face geometry. The algorithm is exact for convex
//! input and deterministic: clipping order is the plane order.

use std::f64::consts::PI;

use serde::{Deserialize, Serialize};

use crate::Error;

/// One half-space `normal . x <= offset`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HalfSpace {
    /// Outward normal, not necessarily normalized.
    pub normal: [f64; 3],
    /// Plane offset along the normal.
    pub offset: f64,
}

impl HalfSpace {
    /// Creates a half-space.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidInput`] for a non-finite or zero normal and a
    /// non-finite offset.
    pub fn new(normal: [f64; 3], offset: f64) -> Result<Self, Error> {
        if !normal.iter().all(|value| value.is_finite()) {
            return Err(Error::InvalidInput(
                "half-space normal must be finite".to_owned(),
            ));
        }
        let norm = (normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]).sqrt();
        if norm < 1e-12 {
            return Err(Error::InvalidInput(
                "half-space normal must not be zero".to_owned(),
            ));
        }
        if !offset.is_finite() {
            return Err(Error::InvalidInput(
                "half-space offset must be finite".to_owned(),
            ));
        }
        Ok(Self {
            normal: [normal[0] / norm, normal[1] / norm, normal[2] / norm],
            offset: offset / norm,
        })
    }

    /// Signed distance of a point from the boundary, positive inside.
    #[must_use]
    pub fn signed_distance(&self, point: [f64; 3]) -> f64 {
        self.offset - dot(self.normal, point)
    }
}

/// A convex polyhedron with ordered faces.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Polyhedron {
    /// Vertex coordinates.
    pub vertices: Vec<[f64; 3]>,
    /// Faces as ordered vertex indices with an outward orientation.
    pub faces: Vec<Vec<usize>>,
}

impl Polyhedron {
    /// Intersects the half-spaces, starting from a box large enough to contain
    /// every finite intersection.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidInput`] for an empty plane list or a
    /// degenerate intersection.
    pub fn from_half_spaces(planes: &[HalfSpace]) -> Result<Self, Error> {
        if planes.is_empty() {
            return Err(Error::InvalidInput(
                "a polyhedron needs at least one half-space".to_owned(),
            ));
        }
        let extent = planes
            .iter()
            .map(|plane| plane.offset.abs())
            .fold(1.0_f64, f64::max)
            * 4.0
            + 10.0;
        let mut polyhedron = Self::cube(extent);
        for plane in planes {
            polyhedron = polyhedron.clip(plane, 1e-10 * extent);
            if polyhedron.faces.is_empty() {
                return Err(Error::InvalidInput(
                    "the half-spaces have an empty intersection".to_owned(),
                ));
            }
        }
        Ok(polyhedron)
    }

    /// Axis-aligned cube centred at the origin.
    #[must_use]
    pub fn cube(extent: f64) -> Self {
        let e = extent.abs().max(1e-3);
        let vertices = vec![
            [-e, -e, -e],
            [e, -e, -e],
            [e, e, -e],
            [-e, e, -e],
            [-e, -e, e],
            [e, -e, e],
            [e, e, e],
            [-e, e, e],
        ];
        let faces = vec![
            vec![0, 3, 2, 1],
            vec![4, 5, 6, 7],
            vec![0, 1, 5, 4],
            vec![1, 2, 6, 5],
            vec![2, 3, 7, 6],
            vec![3, 0, 4, 7],
        ];
        Self { vertices, faces }
    }

    /// Clips the polyhedron by one half-space.
    #[must_use]
    pub fn clip(&self, plane: &HalfSpace, epsilon: f64) -> Self {
        let distances: Vec<f64> = self
            .vertices
            .iter()
            .map(|vertex| plane.signed_distance(*vertex))
            .collect();
        let mut vertices = Vec::new();
        let mut faces = Vec::new();
        let mut cap_points: Vec<[f64; 3]> = Vec::new();
        for face in &self.faces {
            let mut polygon: Vec<usize> = Vec::new();
            for (position, &current) in face.iter().enumerate() {
                let Some(&next) = face.get((position + 1) % face.len()) else {
                    continue;
                };
                let (Some(&d_current), Some(&d_next)) =
                    (distances.get(current), distances.get(next))
                else {
                    continue;
                };
                let inside_current = d_current >= -epsilon;
                let inside_next = d_next >= -epsilon;
                if inside_current {
                    push_vertex(&mut vertices, &mut polygon, self.vertices[current]);
                }
                if inside_current != inside_next {
                    let denominator = d_current - d_next;
                    if denominator.abs() > 1e-30 {
                        let t = (d_current / denominator).clamp(0.0, 1.0);
                        let Some(&a) = self.vertices.get(current) else {
                            continue;
                        };
                        let Some(&b) = self.vertices.get(next) else {
                            continue;
                        };
                        let point = [
                            a[0] + (b[0] - a[0]) * t,
                            a[1] + (b[1] - a[1]) * t,
                            a[2] + (b[2] - a[2]) * t,
                        ];
                        push_vertex(&mut vertices, &mut polygon, point);
                        cap_points.push(point);
                    }
                }
            }
            if polygon.len() >= 3 {
                faces.push(polygon);
            }
        }
        if let Some(cap) = cap_face(vertices.as_slice(), &cap_points, plane.normal) {
            faces.push(cap);
        }
        Self { vertices, faces }
    }

    /// Unit outward normal of a face, from its vertex order.
    #[must_use]
    pub fn face_normal(&self, face: usize) -> Option<[f64; 3]> {
        let indices = self.faces.get(face)?;
        let first = *self.vertices.get(*indices.first()?)?;
        let mut normal = [0.0; 3];
        for (position, &index) in indices.iter().enumerate() {
            let next = *indices.get((position + 1) % indices.len())?;
            let a = self.vertices.get(index)?;
            let b = self.vertices.get(next)?;
            normal[0] += (a[1] - b[1]) * (a[2] + b[2]);
            normal[1] += (a[2] - b[2]) * (a[0] + b[0]);
            normal[2] += (a[0] - b[0]) * (a[1] + b[1]);
        }
        let norm = norm(normal);
        if norm < 1e-12 {
            return None;
        }
        let outward = dot(normal, subtract(first, self.centroid())) > 0.0;
        let sign = if outward { 1.0 } else { -1.0 };
        Some([
            normal[0] * sign / norm,
            normal[1] * sign / norm,
            normal[2] * sign / norm,
        ])
    }

    /// Area of one face.
    #[must_use]
    pub fn face_area(&self, face: usize) -> f64 {
        let Some(indices) = self.faces.get(face) else {
            return 0.0;
        };
        let Some(&first) = indices.first() else {
            return 0.0;
        };
        let Some(&origin) = self.vertices.get(first) else {
            return 0.0;
        };
        let mut area = 0.0;
        for position in 1..indices.len().saturating_sub(1) {
            let (Some(&i), Some(&j)) = (indices.get(position), indices.get(position + 1)) else {
                continue;
            };
            let (Some(&a), Some(&b)) = (self.vertices.get(i), self.vertices.get(j)) else {
                continue;
            };
            area += 0.5 * norm(cross(subtract(a, origin), subtract(b, origin)));
        }
        area
    }

    /// Total surface area.
    #[must_use]
    pub fn surface_area(&self) -> f64 {
        (0..self.faces.len()).map(|face| self.face_area(face)).sum()
    }

    /// Volume by the signed tetrahedra of each face around the origin.
    #[must_use]
    pub fn volume(&self) -> f64 {
        let mut volume = 0.0;
        for indices in &self.faces {
            let Some(&first) = indices.first() else {
                continue;
            };
            let Some(&origin) = self.vertices.get(first) else {
                continue;
            };
            for position in 1..indices.len().saturating_sub(1) {
                let (Some(&i), Some(&j)) = (indices.get(position), indices.get(position + 1))
                else {
                    continue;
                };
                let (Some(&a), Some(&b)) = (self.vertices.get(i), self.vertices.get(j)) else {
                    continue;
                };
                volume += dot(origin, cross(a, b)) / 6.0;
            }
        }
        volume.abs()
    }

    /// Centroid of the vertices; equal to the volume centroid for a simplex,
    /// and a stable centre for clipping.
    #[must_use]
    pub fn centroid(&self) -> [f64; 3] {
        let mut total = [0.0; 3];
        for vertex in &self.vertices {
            total = add(total, *vertex);
        }
        let count = self.vertices.len().max(1) as f64;
        [total[0] / count, total[1] / count, total[2] / count]
    }

    /// Face centroid.
    #[must_use]
    pub fn face_centroid(&self, face: usize) -> [f64; 3] {
        let Some(indices) = self.faces.get(face) else {
            return [0.0; 3];
        };
        let mut total = [0.0; 3];
        let mut count = 0.0;
        for &index in indices {
            if let Some(&vertex) = self.vertices.get(index) {
                total = add(total, vertex);
                count += 1.0;
            }
        }
        if count == 0.0 {
            return [0.0; 3];
        }
        [total[0] / count, total[1] / count, total[2] / count]
    }

    /// Solid angle of one face as seen from `origin`, in steradians.
    #[must_use]
    pub fn face_solid_angle(&self, face: usize, origin: [f64; 3]) -> f64 {
        let area = self.face_area(face);
        if area <= 0.0 {
            return 0.0;
        }
        let centre = subtract(self.face_centroid(face), origin);
        let distance = norm(centre);
        if distance < 1e-12 {
            return 0.0;
        }
        let Some(normal) = self.face_normal(face) else {
            return 0.0;
        };
        let cosine = dot(normal, centre) / distance;
        if cosine <= 0.0 {
            0.0
        } else {
            area * cosine / (distance * distance)
        }
    }

    /// Triangles of the surface with outward normals.
    #[must_use]
    pub fn triangles(&self) -> Vec<([f64; 3], [[f64; 3]; 3])> {
        let mut triangles = Vec::new();
        for (face, indices) in self.faces.iter().enumerate() {
            let Some(normal) = self.face_normal(face) else {
                continue;
            };
            let Some(&first) = indices.first() else {
                continue;
            };
            let Some(&origin) = self.vertices.get(first) else {
                continue;
            };
            for position in 1..indices.len().saturating_sub(1) {
                let (Some(&i), Some(&j)) = (indices.get(position), indices.get(position + 1))
                else {
                    continue;
                };
                let (Some(&a), Some(&b)) = (self.vertices.get(i), self.vertices.get(j)) else {
                    continue;
                };
                triangles.push((normal, [origin, a, b]));
            }
        }
        triangles
    }
}

/// Spherical area of a full sphere in steradians.
pub const FULL_SPHERE: f64 = 4.0 * PI;

fn cap_face(vertices: &[[f64; 3]], points: &[[f64; 3]], normal: [f64; 3]) -> Option<Vec<usize>> {
    if points.len() < 3 {
        return None;
    }
    let mut total = [0.0; 3];
    for point in points {
        total = add(total, *point);
    }
    let count = points.len() as f64;
    let centre = [total[0] / count, total[1] / count, total[2] / count];
    let (u, v) = plane_basis(normal);
    let mut ordered: Vec<(f64, [f64; 3])> = points
        .iter()
        .map(|point| {
            let delta = subtract(*point, centre);
            (dot(delta, v).atan2(dot(delta, u)), *point)
        })
        .collect();
    ordered.sort_by(|first, second| {
        first
            .0
            .partial_cmp(&second.0)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let mut face = Vec::with_capacity(ordered.len());
    for (_, point) in ordered {
        let mut index = None;
        for (candidate, vertex) in vertices.iter().enumerate() {
            if norm(subtract(*vertex, point)) < 1e-8 {
                index = Some(candidate);
                break;
            }
        }
        if let Some(index) = index {
            face.push(index);
        }
    }
    if face.len() < 3 {
        return None;
    }
    let area_normal = polygon_normal(vertices, &face);
    if dot(area_normal, normal) < 0.0 {
        face.reverse();
    }
    Some(face)
}

fn polygon_normal(vertices: &[[f64; 3]], face: &[usize]) -> [f64; 3] {
    let mut normal = [0.0; 3];
    for (position, &index) in face.iter().enumerate() {
        let next = face[(position + 1) % face.len()];
        let (Some(a), Some(b)) = (vertices.get(index), vertices.get(next)) else {
            continue;
        };
        normal[0] += (a[1] - b[1]) * (a[2] + b[2]);
        normal[1] += (a[2] - b[2]) * (a[0] + b[0]);
        normal[2] += (a[0] - b[0]) * (a[1] + b[1]);
    }
    normal
}

fn plane_basis(normal: [f64; 3]) -> ([f64; 3], [f64; 3]) {
    let reference = if normal[0].abs() < 0.9 {
        [1.0, 0.0, 0.0]
    } else {
        [0.0, 1.0, 0.0]
    };
    let u = normalize(cross(normal, reference));
    let v = normalize(cross(normal, u));
    (u, v)
}

fn push_vertex(vertices: &mut Vec<[f64; 3]>, polygon: &mut Vec<usize>, point: [f64; 3]) {
    for (index, vertex) in vertices.iter().enumerate() {
        if norm(subtract(*vertex, point)) < 1e-9 {
            polygon.push(index);
            return;
        }
    }
    let index = vertices.len();
    vertices.push(point);
    polygon.push(index);
}

fn add(first: [f64; 3], second: [f64; 3]) -> [f64; 3] {
    [
        first[0] + second[0],
        first[1] + second[1],
        first[2] + second[2],
    ]
}

fn subtract(first: [f64; 3], second: [f64; 3]) -> [f64; 3] {
    [
        first[0] - second[0],
        first[1] - second[1],
        first[2] - second[2],
    ]
}

fn cross(first: [f64; 3], second: [f64; 3]) -> [f64; 3] {
    [
        first[1] * second[2] - first[2] * second[1],
        first[2] * second[0] - first[0] * second[2],
        first[0] * second[1] - first[1] * second[0],
    ]
}

fn dot(first: [f64; 3], second: [f64; 3]) -> f64 {
    first[0] * second[0] + first[1] * second[1] + first[2] * second[2]
}

fn norm(vector: [f64; 3]) -> f64 {
    dot(vector, vector).sqrt()
}

fn normalize(vector: [f64; 3]) -> [f64; 3] {
    let length = norm(vector);
    if length < 1e-12 {
        [0.0; 3]
    } else {
        [vector[0] / length, vector[1] / length, vector[2] / length]
    }
}