Skip to content

Commit

Permalink
Lens parameters - Reduce boiler-plate code.
Browse files Browse the repository at this point in the history
We use macro_rules, traits and slices to reduce the number of characters
and changes needed to add a new lens model.

This change was intended to refactor and reduce complexity of the
overall code, and so reducing repetion.
  • Loading branch information
david-cattermole committed Dec 30, 2024
1 parent 99763e7 commit 27b4860
Show file tree
Hide file tree
Showing 12 changed files with 945 additions and 1,137 deletions.
92 changes: 70 additions & 22 deletions lib/cppbind/mmlens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ lib/cppbind/mmlens/src/distortion_layers.rs
lib/cppbind/mmlens/src/distortion_process.cpp
lib/cppbind/mmlens/src/distortion_process.rs
lib/cppbind/mmlens/src/distortion_structs.h
lib/cppbind/mmlens/src/lens_parameters/mod.rs
lib/cppbind/mmlens/src/option_lens_parameters.rs
lib/mmsolverlibs/src/CMakeLists.txt
```
Expand All @@ -67,6 +68,7 @@ Add files:
```
lib/cppbind/mmlens/include/mmlens/lens_model_EXAMPLE.h
lib/cppbind/mmlens/src/lens_model_EXAMPLE.cpp
lib/cppbind/mmlens/src/lens_parameters/EXAMPLE.rs
```

### Important Implementation Considerations
Expand Down Expand Up @@ -111,17 +113,16 @@ Documentation:



### 0) option_lens_parameters.rs and cxxbridge.rs

#### Add Option Parameter Type
### 0) option_lens_parameters.rs and cxxbridge.rs - Add Option Parameter Type

In `option_lens_parameters.rs` add:

```rust
struct OptionParametersExample {
exists: bool,
value: ParametersExample,
}
impl_option_parameters_trait!(
BindOptionParametersExample,
BindParametersExample
);

```

Define parameter structures in `cxxbridge.rs`:
Expand All @@ -134,22 +135,60 @@ struct OptionParametersExample {
}
```

### 1) lens_parameters/mod.rs and lens_parameters/example.rs - Create Main Parameter Struct

### 1) cxxbridge.rs

#### Add New Type Enum

Add a new variant to `LensModelType` enum in `cxxbridge.rs`:

Create a new file `lens_parameters/example.rs`, and add default values
and functions to set each value via a slice:
```rust
#[repr(u8)]
pub(crate) enum LensModelType {
// ... existing types ...
TdeExample = <next_number>, // Add new type
impl Default for BindParametersExample {
fn default() -> Self {
Self {
distortion: 0.0,
anamorphic_squeeze: 1.0,
curvature_x: 0.0,
curvature_y: 0.0,
quartic_distortion: 0.0,
}
}
}

impl LensParameters for BindParametersExample {
impl_LensParameters_hash_parameters_method!(
distortion,
anamorphic_squeeze,
curvature_x,
curvature_y,
quartic_distortion
);

fn from_slice(data: &[f64]) -> Self {
Self {
distortion: data[0],
anamorphic_squeeze: data[1],
curvature_x: data[2],
curvature_y: data[3],
quartic_distortion: data[4],
}
}

fn into_args(self) -> Vec<f64> {
vec![
self.distortion,
self.anamorphic_squeeze,
self.curvature_x,
self.curvature_y,
self.quartic_distortion,
]
}
}
```

#### Create Main Parameter Struct

Then add the newly created file to `lens_parameters/mod.rs`:
```rust
mod example;
```

Define parameter structures in `cxxbridge.rs`:

Expand All @@ -163,7 +202,19 @@ pub(crate) struct ParametersExample {
}
```

### 2) lens_model_example.cpp/.h - Create Lens Model Class
### 2) cxxbridge.rs - Add New Type Enum

Add a new variant to `LensModelType` enum in `cxxbridge.rs`:

```rust
#[repr(u8)]
pub(crate) enum LensModelType {
// ... existing types ...
TdeExample = <next_number>, // Add new type
}
```

### 3) lens_model_example.cpp/.h - Create Lens Model Class

Add `lens_model_example.cpp` and `lens_model_example.h`, define the
'Example' lens model class inheriting from LensModel, and implement
Expand All @@ -189,9 +240,7 @@ private:
};
```

### 3) distortion_structs.h

#### Implement Distortion Operations
### 4) distortion_structs.h - Implement Distortion Operations

Create distortion class implementing the Distortion interface in `distortion_structs.h`.

Expand All @@ -216,7 +265,6 @@ void apply_f64_to_f64(...);
void apply_f64_to_f32(...);
```


### 5) constants.rs

Add parameter count constant in `constants.rs`:
Expand Down
12 changes: 6 additions & 6 deletions lib/cppbind/mmlens/src/cxxbridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub mod ffi {
lens_center_offset_y_cm: f64,
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deClassic {
distortion: f64, // "Distortion"
anamorphic_squeeze: f64, // "Anamorphic Squeeze"
Expand All @@ -169,7 +169,7 @@ pub mod ffi {
quartic_distortion: f64, // "Quartic Distortion"
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deRadialStdDeg4 {
degree2_distortion: f64, // "Distortion - Degree 2"
degree2_u: f64, // "U - Degree 2"
Expand All @@ -183,7 +183,7 @@ pub mod ffi {
cylindric_bending: f64, // "B - Cylindric Bending"
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deAnamorphicStdDeg4 {
degree2_cx02: f64, // "Cx02 - Degree 2"
degree2_cy02: f64, // "Cy02 - Degree 2"
Expand All @@ -202,7 +202,7 @@ pub mod ffi {
squeeze_y: f64, // "Squeeze-Y"
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deAnamorphicStdDeg4Rescaled {
degree2_cx02: f64, // "Cx02 - Degree 2"
degree2_cy02: f64, // "Cy02 - Degree 2"
Expand All @@ -222,7 +222,7 @@ pub mod ffi {
rescale: f64, // "Rescale"
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deAnamorphicStdDeg6 {
degree2_cx02: f64, // "Cx02 - Degree 2"
degree2_cy02: f64, // "Cy02 - Degree 2"
Expand Down Expand Up @@ -250,7 +250,7 @@ pub mod ffi {
squeeze_y: f64, // "Squeeze-Y"
}

#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct Parameters3deAnamorphicStdDeg6Rescaled {
degree2_cx02: f64, // "Cx02 - Degree 2"
degree2_cy02: f64, // "Cy02 - Degree 2"
Expand Down
Loading

0 comments on commit 27b4860

Please sign in to comment.