Skip to content

Structure Viewer

The structure viewer is the largest module in CatGo, providing interactive 3D visualization of crystal structures, molecules, and surfaces.

Source: src/lib/structure/

Components

Core

ComponentDescription
Structure.svelteMain orchestrator — manages state, controls, and passes data to the 3D scene
StructureScene.svelteThree.js scene — renders atoms, bonds, lattice, labels, and handles raycasting
StructureControls.svelteControl panel with toggles for bonds, labels, lattice, camera, and more
StructureLegend.svelteColor legend for atom types
StructureInfoPane.svelteDisplays metadata (formula, space group, density, etc.)

Geometry Primitives

ComponentDescription
Bond.svelteRenders a bond as a cylinder between two atoms
Cylinder.svelteGeneric cylinder primitive used by bonds and arrows
Arrow.svelteArrow for force vectors (MD/optimization)
Lattice.svelteUnit cell wireframe edges and lattice vectors

Panes & Controls

ComponentDescription
LatticePane.svelteEdit lattice parameters (a, b, c, alpha, beta, gamma)
ExportPane.svelteExport structure in CIF, POSCAR, XYZ, EXTXYZ, JSON, GLB, OBJ
MillerSlabCutterPane.svelteGenerate surface slabs from Miller indices
CuttingPlaneVisualizer.svelteVisual preview of the cutting plane
OptimizationPane.svelteConnect to optimization server for relaxation
AdsorptionSitePane.svelteFind adsorption sites on surfaces
CubePanel.svelteIntegrate density visualization from CUBE files
CellSelect.svelteSupercell dimension selector (n x m x p)

Modals

ComponentDescription
OptimadeSearchModal.svelteSearch OPTIMADE structure databases
OptimadePreviewModal.sveltePreview structures before loading
PubchemSearchModal.svelteSearch molecules via PubChem
PasteContentModal.sveltePaste structure data directly
VacuumBoxModal.svelteAdd vacuum box around structure

Rendering Architecture

The viewer uses Three.js via the Threlte Svelte wrapper:

  • InstancedMesh for efficient rendering of many atoms (handles thousands of atoms)
  • BVH acceleration (three-mesh-bvh) for fast raycasting / atom picking
  • Spatial grid for bond detection in structures with >50 atoms
  • Level of Detail (LOD) — sphere segment count adjusts based on atom count
  • Depth cueing — fades distant atoms and bonds toward the background color (VESTA-style) for depth perception

Camera & Controls

  • TrackballControls — orbit, zoom, and pan
  • Perspective and Orthographic projection modes
  • Auto-rotate mode
  • Camera reset aligns to lattice after slab cuts / supercell operations
  • Gizmo orientation widget for spatial reference

Atom Interaction

Selection

  • Click to select individual atoms (raycasting)
  • Selected atoms show index, element, and coordinates
  • Bond selection and deletion supported

Manipulation

  • Add atom — insert new atom at position
  • Delete atoms — remove selected atoms
  • Replace atom — change element at site
  • Move atoms — reposition via arrow keys or drag
  • Add bonds — drag from one atom to another in bond editing mode to create a bond (or click-click as fallback)
  • Freeze atoms — mark atoms as fixed for optimization (ring/crosshatch/dimmed indicators)

Labels

  • Site labels with element symbol
  • Index display
  • Custom label colors, sizes, and offsets
  • Configurable via settings

Charge Labels

Per-atom charge labels display Bader charge values directly on atoms in the 3D scene.

Toggling labels:

  • Right-click an atom → Charge LabelShow/Hide charge label to toggle a single atom
  • Right-click → Charge LabelShow all charge labels / Hide all charge labels for bulk control
  • Labels are only visible when the atom coloring mode is set to Charge — switching to another mode (e.g., Element) hides them, and switching back restores them

Repositioning:

  • Click and drag any charge label to reposition it in screen space
  • Offsets persist across camera rotations and are stored per-atom
  • Labels default to a position slightly above the atom to avoid blocking interactions

Editing values:

  • Double-click a charge label to edit the value inline (Enter to confirm, Escape to cancel)
  • Right-click → Charge LabelSet charge value... to manually enter a charge for any atom (useful for atoms without Bader data)

Loading charges:

  • Import Bader charges from an ACF.dat file via right-click → ImportLoad charges
  • Charges are stored as site.properties.bader_charge on each atom

Display Modes

Atom Coloring

  • Element — CPK/Jmol standard colors
  • Coordination number — color by number of neighbors
  • Wyckoff position — color by symmetry site
  • Charge — color by Bader charge value (from ACF.dat); supports per-atom charge labels
  • Custom — user-defined color per element

Lattice Display

  • Unit cell edges (wireframe)
  • Lattice vectors with arrows
  • Adjustable opacity
  • Periodic boundary condition (PBC) image atoms

Key Functions

typescript
// Structure creation & manipulation
add_atom(structure, element, position)
delete_atoms(structure, indices)
replace_atom(structure, index, new_element)
move_atom(structure, index, new_position)
move_atoms_by_displacement(structure, indices, displacement)
concatenate_structures(struct_a, struct_b)

// Analysis
get_center_of_mass(structure)
get_density(structure)
calculate_inertia_tensor(structure)
get_principal_axes(structure)
align_to_principal_axes(structure)

Settings

Key settings that control the structure viewer (defined in settings.ts):

SettingTypeDefaultDescription
atom_radiusnumber0.4Atom sphere radius
bond_thicknessnumber0.15Bond cylinder thickness
sphere_segmentsnumber16Sphere quality (segments)
show_image_atomsbooleantrueShow PBC image atoms
show_bondsbooleantrueDisplay bonds
show_cellbooleantrueDisplay unit cell
camera_projectionstring"perspective"Camera mode
color_modestring"element"Atom coloring strategy
auto_rotatebooleanfalseAuto-rotation mode
depth_cueingnumber0Fog effect strength

Released under the MIT License.