Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: surface interpolation with unequal number of control points #100

Merged
merged 1 commit into from
Feb 9, 2023

Conversation

Roxana-P
Copy link
Contributor

@Roxana-P Roxana-P commented Feb 8, 2023

Overview

Description
As pointed out in issue #99, the current routine splinepy.BSpline.interpolate_surface does not allow an unequal number of sampling points. This is due to a wrong ordering of the sampling points in l.85.
This code fixes this issue and transposes the control points, as they are otherwise incorrectly ordered. I am happy on feedback to improve the efficiency of l.108-114.

Addressed issues

Showcase

The example illustrated in #99.

import numpy as np
import splinepy


def interpolate_function(f, samples_per_direction):
    """Interpolate the function f with a given number of samples 
    in each direction on the interval [-1, 1] x [-1, 1]

    Parameters
    ----------
    f : function
        Function to interpolate
    samples_per_direction : list
        Number of samples per direction

    Returns
    -------
    np.ndarray, splinepy.BSpline
        Target points and interpolating BSpline
    """

    # Create dataset
    x = [np.linspace(-1, 1, samples_per_direction[0]), 
        np.linspace(-1, 1, samples_per_direction[1])]
    xx, yy = np.meshgrid(x[0], x[1])

    target_points = np.vstack(
        (xx.flatten(), yy.flatten(), h(xx, yy).flatten())
    ).T

    interpolated_surface = splinepy.BSpline.interpolate_surface(
        target_points,
        size_u=samples_per_direction[0],
        size_v=samples_per_direction[1],
        degree_u=2,
        degree_v=2,
    )
    return target_points, interpolated_surface
    
def h(x, y):
    return x + y

if __name__ == "__main__":
    try:
        import gustaf as gus

        gustaf_available = True
    except ImportError:
        gustaf_available = False


    for sample_sizes in [[3, 3], [3, 4], [4, 3], [20, 20], [25, 20]]:
        target_points, interpolated_surface = interpolate_function(h, sample_sizes)
        
        if gustaf_available:
            bspline = gus.BSpline(**interpolated_surface.todict())
            gus.show.show_vedo([bspline])

Results:
Number of control points (3,3)
cps_3_3
Number of control points (3,4)
cps_3_4
Number of control points (4,3)
cps_4_3
Number of control points (20, 20)
cps_20_20
Number of control points (5, 20)
cps_5_20

Checklists

  • Documentations are up-to-date.
  • Added example(s)
  • Added test(s)

Copy link
Member

@j042 j042 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you! This has been bugging me for a long time. I will remove reorganize param in next pr

@j042 j042 merged commit c7af783 into tataratat:main Feb 9, 2023
@jzwar jzwar mentioned this pull request Feb 10, 2023
@Roxana-P Roxana-P deleted the bug-interpolate-surface branch May 24, 2023 08:57
j042 added a commit that referenced this pull request Jul 24, 2023
this is unnecessary since #100
j042 added a commit that referenced this pull request Jul 24, 2023
this is unnecessary since #100
j042 added a commit that referenced this pull request Jul 24, 2023
this is unnecessary since #100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants