Skip to content

Commit

Permalink
Element (str) --> Element (#10)
Browse files Browse the repository at this point in the history
* str->Element

* refactor to use Element rather than string

* remove bitflags

* cargo-machete

* gitignore

* avoid running tests twice
  • Loading branch information
zachcp authored Oct 27, 2024
1 parent 3120d7f commit bdc2feb
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 77 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: Tests
on:
push:
branches:
- "**"
pull_request:
push:
branches:
- "**"
- "main"

jobs:
test:
name: Run Tests
Expand Down
21 changes: 5 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions ferritin-bevy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ description.workspace = true
[dependencies]
bevy = "0.14"
bon = "2.3.0"
serde.workspace = true
ferritin-core = { path = "../ferritin-core" }
itertools.workspace = true
pdbtbx.workspace = true
124 changes: 124 additions & 0 deletions ferritin-bevy/examples/basic_ballandstick.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//! Example allowing custom colors and rendering options
use bevy::prelude::*;
use ferritin_bevy::{ColorScheme, RenderOptions, StructurePlugin, StructureSettings};

fn main() {
let chalky = StandardMaterial {
base_color: Color::srgb(0.9, 0.9, 0.9), // Light gray color
perceptual_roughness: 1.0, // Maximum roughness for a matte look
metallic: 0.0, // No metallic properties
reflectance: 0.1, // Low reflectance
specular_transmission: 0.0, // No specular transmission
thickness: 0.0, // No thickness (for transparency)
ior: 1.5, // Index of refraction (standard for most materials)
alpha_mode: AlphaMode::Opaque, // Fully opaque
cull_mode: None, // Don't cull any faces
..default() // Use defaults for other properties
};

let _metallic = StandardMaterial {
base_color: Color::srgb(0.8, 0.8, 0.9), // Slight blue tint for a steel-like appearance
metallic: 1.0, // Fully metallic
perceptual_roughness: 0.1, // Very smooth surface
reflectance: 0.5, // Medium reflectance
//emissive: Color::BLACK, // No emission
alpha_mode: AlphaMode::Opaque, // Fully opaque
ior: 2.5, // Higher index of refraction for metals
specular_transmission: 0.0, // No light transmission
thickness: 0.0, // No thickness (for transparency)
//cull_mode: Some(Face::Back), // Cull back faces for better performance
..default() // Use defaults for other properties
};

App::new()
.add_plugins(DefaultPlugins)
.add_plugins(StructurePlugin::new().with_file(
"examples/1fap.cif",
Some(StructureSettings {
render_type: RenderOptions::BallAndStick,
color_scheme: ColorScheme::ByAtomType,
material: chalky,
}),
))
.add_systems(Startup, setup)
.run();
}

#[derive(Component)]
struct MainCamera;

fn setup(mut commands: Commands) {
// Add a camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 50.0, 100.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
MainCamera,
));

// Key Light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::srgb(1.0, 0.9, 0.9),
illuminance: 10000.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_rotation(Quat::from_euler(EulerRot::XYZ, -0.5, 0.5, 0.0)),
..default()
});

// Fill Light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::srgb(0.8, 0.8, 1.0),
illuminance: 5000.0,
shadows_enabled: false,
..default()
},
transform: Transform::from_rotation(Quat::from_euler(EulerRot::XYZ, 0.5, -0.5, 0.0)),
..default()
});

// Back Light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::srgb(0.9, 0.9, 1.0),
illuminance: 3000.0,
shadows_enabled: false,
..default()
},
transform: Transform::from_rotation(Quat::from_euler(
EulerRot::XYZ,
0.0,
std::f32::consts::PI,
0.0,
)),
..default()
});

// Add a light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});

// Spot light
commands.spawn(SpotLightBundle {
spot_light: SpotLight {
intensity: 10000.0,
color: Color::srgb(0.8, 1.0, 0.8),
shadows_enabled: true,
outer_angle: 0.6,
..default()
},
transform: Transform::from_xyz(-4.0, 5.0, -4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
33 changes: 23 additions & 10 deletions ferritin-bevy/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! This module defines the color mapping used for rendering.
use bevy::prelude::Color;
use pdbtbx::Element;

/// Represents different color schemes for rendering atoms.
#[derive(Clone)]
Expand All @@ -25,16 +26,16 @@ pub enum ColorScheme {
// ColorScheme::ByResidueType(func) => func(residue),
// ColorScheme::Custom(func) => func(atom, residue, chain),
impl ColorScheme {
pub fn get_color(&self, atom: &str) -> Color {
pub fn get_color(&self, atom: &Element) -> Color {
match &self {
ColorScheme::Solid(color) => *color,
ColorScheme::ByAtomType => {
match atom {
"C" => Color::srgb(0.5, 0.5, 0.5), // Carbon: Gray
"N" => Color::srgb(0.0, 0.0, 1.0), // Nitrogen: Blue
"O" => Color::srgb(1.0, 0.0, 0.0), // Oxygen: Red
"S" => Color::srgb(1.0, 1.0, 0.0), // Sulfur: Yellow
_ => Color::srgb(1.0, 1.0, 1.0), // Other: White
Element::C => Color::srgb(0.5, 0.5, 0.5), // Carbon: Gray
Element::N => Color::srgb(0.0, 0.0, 1.0), // Nitrogen: Blue
Element::O => Color::srgb(1.0, 0.0, 0.0), // Oxygen: Red
Element::S => Color::srgb(1.0, 1.0, 0.0), // Sulfur: Yellow
_ => Color::srgb(1.0, 1.0, 1.0), // Other: White
}
}
}
Expand All @@ -47,9 +48,21 @@ mod tests {
#[test]
fn test_get_color() {
let by_atom_scheme = ColorScheme::ByAtomType;
assert_eq!(by_atom_scheme.get_color("C"), Color::srgb(0.5, 0.5, 0.5));
assert_eq!(by_atom_scheme.get_color("N"), Color::srgb(0.0, 0.0, 1.0));
assert_eq!(by_atom_scheme.get_color("O"), Color::srgb(1.0, 0.0, 0.0));
assert_eq!(by_atom_scheme.get_color("S"), Color::srgb(1.0, 1.0, 0.0));
assert_eq!(
by_atom_scheme.get_color(&Element::C),
Color::srgb(0.5, 0.5, 0.5)
);
assert_eq!(
by_atom_scheme.get_color(&Element::N),
Color::srgb(0.0, 0.0, 1.0)
);
assert_eq!(
by_atom_scheme.get_color(&Element::O),
Color::srgb(1.0, 0.0, 0.0)
);
assert_eq!(
by_atom_scheme.get_color(&Element::S),
Color::srgb(1.0, 1.0, 0.0)
);
}
}
1 change: 0 additions & 1 deletion ferritin-bevy/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! Over time this would be a good candidate for factoring out
use super::{ColorScheme, RenderOptions, Structure};
use bevy::prelude::*;
// use pdbtbx::StrictnessLevel;
use ferritin_core::AtomCollection;
use std::path::Path;
use std::path::PathBuf;
Expand Down
13 changes: 5 additions & 8 deletions ferritin-bevy/src/structure.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Renderable
//! Structure.
//!
//! Trait and Implementations for Generating Representations.
//! Struct for rendering with Bevy
//!
//!
// use bevy::prelude::*;
use super::ColorScheme;
use bevy::asset::Assets;
use bevy::math::Vec4;
Expand All @@ -13,8 +12,7 @@ use bevy::prelude::{
StandardMaterial, Transform, Vec3,
};
use bon::Builder;
use ferritin_core::{AtomCollection, Bond};
use pdbtbx::Element;
use ferritin_core::AtomCollection;

/// Enum representing various rendering options.
///
Expand Down Expand Up @@ -147,16 +145,15 @@ impl Structure {
fn render_spheres(&self) -> Mesh {
self.pdb
.iter_coords_and_elements()
.map(|(coord, element_str)| {
.map(|(coord, element)| {
let center = Vec3::new(coord[0], coord[1], coord[2]);
let element = Element::from_symbol(element_str).expect("Element not recognized");
let radius = element
.atomic_radius()
.van_der_waals
.expect("Van der waals not defined") as f32;
let mut sphere_mesh = Sphere::new(radius).mesh().build();
let vertex_count = sphere_mesh.count_vertices();
let color = self.color_scheme.get_color(element_str).to_srgba();
let color = self.color_scheme.get_color(element).to_srgba();
let color_array =
vec![Vec4::new(color.red, color.green, color.blue, color.alpha); vertex_count];
sphere_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, color_array);
Expand Down
3 changes: 0 additions & 3 deletions ferritin-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ description.workspace = true

[dependencies]
ferritin-pymol = { path = "../ferritin-pymol" }
once_cell.workspace = true
bitflags.workspace = true
serde = { workspace = true }
pdbtbx.workspace = true
itertools.workspace = true
lazy_static = "1.5.0"
Loading

0 comments on commit bdc2feb

Please sign in to comment.