API Reference¶
Geometry Generation¶
cdl_string_to_geometry¶
Generate geometry directly from a CDL string.
from crystal_geometry import cdl_string_to_geometry
geom = cdl_string_to_geometry("cubic[m3m]:{111}")
crystal_geometry.cdl_string_to_geometry(cdl, c_ratio=1.0)
¶
Convenience function to convert CDL string directly to geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cdl
|
str
|
CDL string like "cubic[m3m]:{111}@1.0 + {100}@1.3" |
required |
c_ratio
|
float
|
c/a ratio for non-cubic systems |
1.0
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry |
Source code in src/crystal_geometry/geometry.py
cdl_to_geometry¶
Generate geometry from a parsed CrystalDescription.
from cdl_parser import parse_cdl
from crystal_geometry import cdl_to_geometry
desc = parse_cdl("cubic[m3m]:{111}@1.0 + {100}@1.3")
geom = cdl_to_geometry(desc, c_ratio=1.0)
crystal_geometry.cdl_to_geometry(desc, c_ratio=1.0)
¶
Convert CDL description to 3D geometry.
Supports crystalline descriptions, amorphous descriptions, nested growth, and aggregates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desc
|
CrystalDescription | AmorphousDescription
|
Parsed CDL description (CrystalDescription or AmorphousDescription) |
required |
c_ratio
|
float
|
c/a ratio for non-cubic systems |
1.0
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry with vertices and faces |
Source code in src/crystal_geometry/geometry.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | |
Convenience Constructors¶
create_octahedron¶
crystal_geometry.create_octahedron(scale=1.0)
¶
Create a regular octahedron.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale
|
float
|
Distance from origin to vertices |
1.0
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry for octahedron |
Source code in src/crystal_geometry/geometry.py
create_cube¶
crystal_geometry.create_cube(scale=1.0)
¶
Create a cube.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale
|
float
|
Distance from origin to face centers |
1.0
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry for cube |
Source code in src/crystal_geometry/geometry.py
create_dodecahedron¶
crystal_geometry.create_dodecahedron(scale=1.0)
¶
Create a rhombic dodecahedron.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale
|
float
|
Distance from origin to face centers |
1.0
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry for dodecahedron |
Source code in src/crystal_geometry/geometry.py
create_truncated_octahedron¶
from crystal_geometry import create_truncated_octahedron
truncated = create_truncated_octahedron(octahedron_scale=1.0, cube_scale=1.3)
crystal_geometry.create_truncated_octahedron(octahedron_scale=1.0, cube_scale=1.3)
¶
Create a truncated octahedron (cuboctahedron-like).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
octahedron_scale
|
float
|
Scale for octahedron faces |
1.0
|
cube_scale
|
float
|
Scale for cube truncation |
1.3
|
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
CrystalGeometry |
Source code in src/crystal_geometry/geometry.py
CrystalGeometry Class¶
The main geometry container class.
from crystal_geometry import CrystalGeometry
geom = cdl_string_to_geometry("cubic[m3m]:{111}")
# Properties
geom.vertices # Nx3 numpy array of vertex positions
geom.faces # List of face vertex indices
geom.face_normals # List of unit normal vectors
geom.face_forms # Form index for each face
geom.face_millers # Miller indices for each face
# Methods
geom.get_edges() # Get unique edges as vertex pairs
geom.center() # Compute centroid
geom.scale_to_unit() # Scale to fit unit sphere
geom.translate(offset) # Translate by vector
geom.euler_characteristic() # V - E + F (should be 2)
geom.is_valid() # Verify geometry integrity
geom.to_dict() # Export to dictionary
crystal_geometry.CrystalGeometry
dataclass
¶
3D crystal geometry with vertices and faces.
Attributes:
| Name | Type | Description |
|---|---|---|
vertices |
ndarray
|
Nx3 array of vertex positions |
faces |
list[list[int]]
|
List of faces, each face is list of vertex indices (counter-clockwise) |
face_normals |
list[ndarray]
|
Normal vector for each face |
face_forms |
list[int]
|
Which form each face belongs to (index into forms list) |
face_millers |
list[tuple[int, int, int]]
|
Miller index (h, k, l) for each face |
forms |
list[CrystalForm]
|
Original form definitions from CDL |
component_ids |
list[int] | None
|
Optional array of component IDs for each face (for twins) |
twin_metadata |
TwinMetadata | None
|
Optional metadata for twinned crystals |
is_amorphous |
bool
|
Whether this is an amorphous (non-crystalline) geometry |
aggregate_metadata |
AggregateMetadata | None
|
Optional metadata for aggregate geometries |
Source code in src/crystal_geometry/models.py
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 | |
center()
¶
euler_characteristic()
¶
Compute Euler characteristic V - E + F.
Returns:
| Type | Description |
|---|---|
int
|
Euler characteristic (should be 2 for convex polyhedra) |
Source code in src/crystal_geometry/models.py
get_edges()
¶
Get all unique edges as vertex index pairs.
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
List of (v1, v2) tuples where v1 < v2 |
Source code in src/crystal_geometry/models.py
is_valid()
¶
Check if geometry is valid (Euler's formula holds).
Returns:
| Type | Description |
|---|---|
bool
|
True if V - E + F = 2 |
rotate(matrix)
¶
Rotate geometry by rotation matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
3x3 rotation matrix |
required |
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
New rotated CrystalGeometry |
Source code in src/crystal_geometry/models.py
scale_to_unit()
¶
Scale geometry to fit in unit sphere.
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
New CrystalGeometry with vertices scaled to max distance of 1.0 |
Source code in src/crystal_geometry/models.py
to_dict()
¶
Convert to dictionary representation.
Source code in src/crystal_geometry/models.py
translate(offset)
¶
Translate geometry by offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
ndarray
|
3D translation vector |
required |
Returns:
| Type | Description |
|---|---|
CrystalGeometry
|
New translated CrystalGeometry |
Source code in src/crystal_geometry/models.py
Symmetry Operations¶
get_point_group_operations¶
Get symmetry operations for a crystallographic point group.
from crystal_geometry import get_point_group_operations
ops = get_point_group_operations('m3m') # 48 operations
ops = get_point_group_operations('6/mmm') # 24 operations
crystal_geometry.get_point_group_operations(point_group)
¶
Get symmetry operations for a point group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_group
|
str
|
Hermann-Mauguin symbol (e.g., 'm3m', '6/mmm') |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of 3x3 rotation/reflection matrices |
Source code in src/crystal_geometry/symmetry.py
generate_equivalent_faces¶
Generate equivalent faces from one Miller index using point group symmetry.
from crystal_geometry import generate_equivalent_faces
faces = generate_equivalent_faces(1, 1, 1, 'm3m') # 8 faces for {111}
faces = generate_equivalent_faces(1, 0, 0, 'm3m') # 6 faces for {100}
crystal_geometry.generate_equivalent_faces(h, k, l, point_group, lattice=DEFAULT_LATTICE)
¶
Generate all symmetry-equivalent Miller indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h, k, l
|
Miller indices |
required | |
point_group
|
str
|
Hermann-Mauguin point group symbol |
required |
lattice
|
LatticeParams
|
Lattice parameters |
DEFAULT_LATTICE
|
Returns:
| Type | Description |
|---|---|
list[tuple[int, int, int]]
|
List of unique (h, k, l) tuples for equivalent faces |
Source code in src/crystal_geometry/symmetry.py
miller_to_normal¶
Convert Miller indices to a unit normal vector.
from crystal_geometry import miller_to_normal, LatticeParams
lattice = LatticeParams.cubic()
normal = miller_to_normal(1, 1, 1, lattice)
crystal_geometry.miller_to_normal(h, k, l, lattice=DEFAULT_LATTICE)
¶
Convert Miller index to face normal vector.
For non-cubic systems, uses the reciprocal lattice (cached for performance).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h, k, l
|
Miller indices |
required | |
lattice
|
LatticeParams
|
Lattice parameters |
DEFAULT_LATTICE
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Unit normal vector |
Source code in src/crystal_geometry/symmetry.py
Lattice Parameters¶
LatticeParams¶
Class for defining crystal lattice parameters.
from crystal_geometry import LatticeParams
# Factory methods for different crystal systems
cubic = LatticeParams.cubic()
tetragonal = LatticeParams.tetragonal(c_ratio=1.5)
hexagonal = LatticeParams.hexagonal(c_ratio=1.2)
orthorhombic = LatticeParams.orthorhombic(a=1.0, b=1.2, c=1.5)
monoclinic = LatticeParams.monoclinic(a=1.0, b=1.2, c=1.5, beta=100.0)
triclinic = LatticeParams.triclinic(a=1.0, b=1.2, c=1.5, alpha=80.0, beta=100.0, gamma=110.0)
crystal_geometry.LatticeParams
dataclass
¶
Crystal lattice parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
a, |
(b, c)
|
Lattice vector lengths |
alpha, |
(beta, gamma)
|
Angles between lattice vectors (in radians) |
Source code in src/crystal_geometry/models.py
get_lattice_for_system¶
Get default lattice parameters for a crystal system.
from crystal_geometry import get_lattice_for_system
lattice = get_lattice_for_system('cubic')
lattice = get_lattice_for_system('hexagonal', c_ratio=1.2)
crystal_geometry.get_lattice_for_system(system, c_ratio=1.0)
¶
Get appropriate lattice parameters for a crystal system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
str
|
Crystal system name |
required |
c_ratio
|
float
|
c/a ratio for non-cubic systems |
1.0
|
Returns:
| Type | Description |
|---|---|
LatticeParams
|
LatticeParams for the system |
Source code in src/crystal_geometry/symmetry.py
Low-Level Functions¶
halfspace_intersection_3d¶
Compute the convex polyhedron from half-space intersection.
from crystal_geometry import halfspace_intersection_3d
import numpy as np
# Half-spaces as Ax <= b
A = np.array([...]) # Normals
b = np.array([...]) # Distances
vertices, faces = halfspace_intersection_3d(A, b)
crystal_geometry.halfspace_intersection_3d(normals, distances, interior_point=None)
¶
Compute intersection of half-spaces in 3D.
Each half-space is defined by: normal . x <= distance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normals
|
list[ndarray] | ndarray
|
List or array of unit normal vectors pointing outward |
required |
distances
|
list[float] | ndarray
|
List or array of distances from origin to each plane |
required |
interior_point
|
ndarray | None
|
A point known to be inside the intersection |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Array of vertices, or None if intersection is empty/unbounded |
Source code in src/crystal_geometry/geometry.py
compute_face_vertices¶
Compute ordered vertices for each face of a polyhedron.
from crystal_geometry import compute_face_vertices
face_vertices = compute_face_vertices(vertices, faces)
crystal_geometry.compute_face_vertices(vertices, normal, distance, tolerance=1e-06)
¶
Find vertices that lie on a face plane.
Uses vectorized operations for improved performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
All vertices (Nx3 array) |
required |
normal
|
ndarray
|
Face normal (3-element array) |
required |
distance
|
float
|
Distance from origin to face plane |
required |
tolerance
|
float
|
Numerical tolerance |
1e-06
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of vertex indices on this face, ordered counter-clockwise |