API Reference¶
High-Level Visualization¶
generate_cdl_svg¶
Generate SVG visualization from a CDL string.
from crystal_renderer import generate_cdl_svg
generate_cdl_svg(
cdl_string="cubic[m3m]:{111}@1.0",
output_path="crystal.svg",
show_axes=True,
elev=30,
azim=-45,
color_by_form=False,
show_grid=True,
face_labels=False
)
crystal_renderer.generate_cdl_svg(cdl_string, output_path, show_axes=True, elev=30, azim=-45, color_by_form=False, show_grid=True, face_labels=False, info_properties=None, info_position='top-right', info_style='compact', info_fontsize=10, figsize=(10, 10), dpi=150, c_ratio=None)
¶
Generate SVG from Crystal Description Language notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cdl_string
|
str
|
CDL notation string (e.g., "cubic[m3m]:{111}@1.0 + {100}@1.3") |
required |
output_path
|
str | Path
|
Output SVG file path |
required |
show_axes
|
bool
|
Whether to show crystallographic axes |
True
|
elev
|
float
|
Elevation angle for view |
30
|
azim
|
float
|
Azimuth angle for view |
-45
|
color_by_form
|
bool
|
If True, color faces by which form they belong to |
False
|
show_grid
|
bool
|
If False, hide background grid and panes |
True
|
face_labels
|
bool
|
If True, show Miller indices on visible faces |
False
|
info_properties
|
dict[str, Any] | None
|
Dictionary of properties to display in info panel |
None
|
info_position
|
str
|
Panel position |
'top-right'
|
info_style
|
str
|
Panel style ('compact', 'detailed', 'minimal') |
'compact'
|
info_fontsize
|
int
|
Font size for info panel |
10
|
figsize
|
tuple[int, int]
|
Figure size in inches |
(10, 10)
|
dpi
|
int
|
Output resolution |
150
|
c_ratio
|
float | None
|
c/a ratio for non-cubic systems (default: 1.1 for trigonal/hexagonal, 1.0 for others). Use higher values for more elongated crystals. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Source code in src/crystal_renderer/visualization.py
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 | |
generate_geometry_svg¶
Generate SVG from raw geometry data.
from crystal_renderer import generate_geometry_svg
generate_geometry_svg(
vertices=geom.vertices,
faces=geom.faces,
output_path="geometry.svg",
face_color='#81D4FA',
edge_color='#0277BD'
)
crystal_renderer.generate_geometry_svg(vertices, faces, output_path, face_normals=None, show_axes=True, elev=30, azim=-45, show_grid=True, face_color='#81D4FA', edge_color='#0277BD', title=None, info_properties=None, figsize=(10, 10), dpi=150)
¶
Generate SVG from raw geometry data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Nx3 array of vertex positions |
required |
faces
|
list[list[int]]
|
List of faces (each face is list of vertex indices) |
required |
output_path
|
str | Path
|
Output SVG file path |
required |
face_normals
|
list[ndarray] | None
|
Optional list of face normal vectors |
None
|
show_axes
|
bool
|
Whether to show crystallographic axes |
True
|
elev
|
float
|
Elevation angle for view |
30
|
azim
|
float
|
Azimuth angle for view |
-45
|
show_grid
|
bool
|
If False, hide background grid and panes |
True
|
face_color
|
str
|
Face fill color |
'#81D4FA'
|
edge_color
|
str
|
Edge line color |
'#0277BD'
|
title
|
str | None
|
Optional title |
None
|
info_properties
|
dict[str, Any] | None
|
Properties for info panel |
None
|
figsize
|
tuple[int, int]
|
Figure size in inches |
(10, 10)
|
dpi
|
int
|
Output resolution |
150
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Source code in src/crystal_renderer/visualization.py
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 | |
3D Export¶
export_stl¶
Export geometry to STL format for 3D printing.
crystal_renderer.export_stl(vertices, faces, output_path, binary=True)
¶
Export crystal geometry to an STL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Nx3 array of vertex positions |
required |
faces
|
list[list[int]]
|
List of faces (each face is a list of vertex indices) |
required |
output_path
|
str | Path
|
Output file path |
required |
binary
|
bool
|
If True, output binary STL; if False, output ASCII STL |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Source code in src/crystal_renderer/formats/stl.py
export_gltf¶
Export geometry to glTF format for web/AR.
from crystal_renderer import export_gltf
export_gltf(
vertices, faces, "model.gltf",
color=(0.5, 0.7, 0.9, 0.8), # RGBA
name="crystal"
)
crystal_renderer.export_gltf(vertices, faces, output_path, color=None, name='crystal')
¶
Export crystal geometry to a glTF file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Nx3 array of vertex positions |
required |
faces
|
list[list[int]]
|
List of faces (each face is a list of vertex indices) |
required |
output_path
|
str | Path
|
Output file path |
required |
color
|
tuple[float, float, float, float] | None
|
Optional RGBA color tuple (0-1 range) |
None
|
name
|
str
|
Mesh name |
'crystal'
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Source code in src/crystal_renderer/formats/gltf.py
Format Conversion¶
convert_svg_to_raster¶
Convert existing SVG to raster format.
from crystal_renderer import convert_svg_to_raster
convert_svg_to_raster("input.svg", "output.png", scale=2.0)
crystal_renderer.convert_svg_to_raster(svg_path, output_path, output_format='png', scale=2.0, quality=95)
¶
Convert SVG to raster format (PNG, JPG, BMP).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
svg_path
|
str | Path
|
Path to input SVG file |
required |
output_path
|
str | Path
|
Path for output raster file |
required |
output_format
|
str
|
'png', 'jpg', 'jpeg', or 'bmp' |
'png'
|
scale
|
float
|
Scale factor for higher resolution (default 2x) |
2.0
|
quality
|
int
|
JPEG quality (1-100, default 95) |
95
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Raises:
| Type | Description |
|---|---|
ImportError
|
If required libraries not available |
ValueError
|
If format not supported |
Source code in src/crystal_renderer/conversion.py
generate_with_format¶
Generate visualization directly to any supported format.
from crystal_renderer import generate_with_format, generate_cdl_svg
generate_with_format(
generator_func=generate_cdl_svg,
output_path="crystal.png",
output_format="png",
cdl_string="cubic[m3m]:{111}"
)
crystal_renderer.generate_with_format(generator_func, output_path, output_format='svg', scale=2.0, quality=95, **kwargs)
¶
Generate crystal visualization in specified format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator_func
|
Callable
|
Function that generates SVG |
required |
output_path
|
str | Path
|
Output file path |
required |
output_format
|
str
|
'svg', 'png', 'jpg', or 'bmp' |
'svg'
|
scale
|
float
|
Scale factor for raster output |
2.0
|
quality
|
int
|
JPEG quality |
95
|
**kwargs
|
Additional arguments for generator_func |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to output file |
Raises:
| Type | Description |
|---|---|
ValueError
|
If format not supported |
Source code in src/crystal_renderer/conversion.py
Info Panels¶
render_info_panel¶
Render an info panel on a matplotlib axes.
from crystal_renderer import render_info_panel
properties = {
'name': 'Ruby',
'chemistry': 'Al2O3',
'hardness': '9',
'ri': '1.762-1.770'
}
render_info_panel(ax, properties, position='top-right', style='compact')
crystal_renderer.render_info_panel(ax, properties, position='top-right', style='compact', fontsize=10, get_label=None, format_value=None)
¶
Render gemstone information panel on the visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Any
|
Matplotlib axes object |
required |
properties
|
dict[str, Any]
|
Dictionary of property key -> value |
required |
position
|
str
|
Panel position ('top-left', 'top-right', 'bottom-left', 'bottom-right') |
'top-right'
|
style
|
str
|
Panel style ('compact', 'detailed', 'minimal') |
'compact'
|
fontsize
|
int
|
Base font size in points |
10
|
get_label
|
Callable[[str], str] | None
|
Optional custom function to get property labels |
None
|
format_value
|
Callable[[str, Any], str] | None
|
Optional custom function to format property values |
None
|
Source code in src/crystal_renderer/info_panel.py
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 | |
create_fga_info_panel¶
Create FGA-style property panel from mineral data.
crystal_renderer.create_fga_info_panel(mineral_data, include_keys=None)
¶
Create a standardized FGA-style info panel from mineral data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mineral_data
|
dict[str, Any]
|
Dictionary of mineral properties |
required |
include_keys
|
list | None
|
Optional list of keys to include (default: standard FGA set) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary ready for render_info_panel |
Source code in src/crystal_renderer/info_panel.py
Color Constants¶
AXIS_COLOURS¶
Colors for crystallographic axes (a, b, c).
from crystal_renderer import AXIS_COLOURS
print(AXIS_COLOURS['a']) # Red
print(AXIS_COLOURS['b']) # Green
print(AXIS_COLOURS['c']) # Blue
ELEMENT_COLOURS¶
Colors for chemical elements.
from crystal_renderer import ELEMENT_COLOURS, get_element_colour
color = get_element_colour('Si') # '#F0C8A0'
color = get_element_colour('O') # '#FF0000'
crystal_renderer.get_element_colour(symbol)
¶
Get colour for an element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Chemical element symbol |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hex colour string |
Source code in src/crystal_renderer/rendering.py
HABIT_COLOURS¶
Colors for crystal systems/habits.
from crystal_renderer import HABIT_COLOURS
print(HABIT_COLOURS['cubic']) # Color for cubic system
print(HABIT_COLOURS['trigonal']) # Color for trigonal system
FORM_COLORS¶
Colors for multi-form rendering.
from crystal_renderer import FORM_COLORS
# Colors for distinguishing different crystal forms
# e.g., {111} in one color, {100} in another
Projection Utilities¶
calculate_view_direction¶
Calculate the view direction vector from elevation and azimuth.
from crystal_renderer import calculate_view_direction
view = calculate_view_direction(elev=30, azim=-45)
crystal_renderer.calculate_view_direction(elev, azim)
¶
Calculate view direction vector from elevation and azimuth angles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elev
|
float
|
Elevation angle in degrees |
required |
azim
|
float
|
Azimuth angle in degrees |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Unit vector pointing towards viewer |
Source code in src/crystal_renderer/projection.py
calculate_axis_origin¶
Calculate the origin point for axis rendering.
from crystal_renderer import calculate_axis_origin
origin = calculate_axis_origin(vertices, padding=0.1)
crystal_renderer.calculate_axis_origin(vertices, elev=30, azim=-45, offset_factor=0.02)
¶
Calculate axis placement for maximum crystal fill efficiency.
Places axes at crystal corner with minimal clearance and adaptive length. The axes stay attached to the crystal's geometry in world coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Array of crystal vertex positions (N x 3) |
required |
elev
|
float
|
Elevation angle (unused, kept for API compatibility) |
30
|
azim
|
float
|
Azimuth angle (unused, kept for API compatibility) |
-45
|
offset_factor
|
float
|
Clearance from bounding box corner (0.02 = 2% typical) |
0.02
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, float]
|
Tuple of (axis_origin, axis_length) |
Source code in src/crystal_renderer/projection.py
calculate_vertex_visibility¶
Calculate which vertices are front-facing.
from crystal_renderer import calculate_vertex_visibility
visibility = calculate_vertex_visibility(vertices, faces, elev=30, azim=-45)
crystal_renderer.calculate_vertex_visibility(vertices, faces, elev, azim, threshold=0.1)
¶
Determine which vertices are on front-facing vs back-facing surfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Array of vertex positions (N x 3) |
required |
faces
|
list[list[int]]
|
List of face vertex index arrays (each face is a list of indices) |
required |
elev
|
float
|
Elevation angle in degrees |
required |
azim
|
float
|
Azimuth angle in degrees |
required |
threshold
|
float
|
Dot product threshold for front-facing (default 0.1) |
0.1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean array, True for front-facing vertices |
Source code in src/crystal_renderer/projection.py
is_face_visible¶
Check if a face is visible from the current view direction.
crystal_renderer.is_face_visible(vertices, face, elev, azim, threshold=0.1)
¶
Check if a face is visible from the given view angle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
ndarray
|
Array of all vertex positions |
required |
face
|
list[int]
|
List of vertex indices forming the face |
required |
elev
|
float
|
Elevation angle in degrees |
required |
azim
|
float
|
Azimuth angle in degrees |
required |
threshold
|
float
|
Dot product threshold for visibility |
0.1
|
Returns:
| Type | Description |
|---|---|
bool
|
True if face is front-facing |