Skip to content

Commit

Permalink
Move implementation details to src
Browse files Browse the repository at this point in the history
  • Loading branch information
csparker247 committed Aug 19, 2024
1 parent 77b62a4 commit f1e3fda
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 480 deletions.
2 changes: 1 addition & 1 deletion segmentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(srcs
src/StructureTensorParticleSim.cpp
src/ThinnedFloodFillSegmentation.cpp
src/ComputeVolumetricMask.cpp
src/CubicMultithreadedSpline.cpp
src/CubicSplineMT.cpp
)

add_library(vc_segmentation ${srcs})
Expand Down
157 changes: 0 additions & 157 deletions segmentation/include/vc/segmentation/lrps/CubicMultithreadedSpline.hpp

This file was deleted.

73 changes: 73 additions & 0 deletions segmentation/include/vc/segmentation/lrps/CubicSplineMT.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* File: CubicMultithreadedSpline.cpp
* Author: Julian Schilliger
*
* Created on: September 2023
*
* Description: CubicMultithreadedSpline class implementation for scalar spline
* interpolation.
*
* License: MIT License
*
* Copyright (c) 2023 Julian Schilliger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

/** @file */

#include <mutex>
#include <vector>
#include <cstddef>

#include <Eigen/Dense>

#include "vc/segmentation/lrps/Common.hpp"

/** @brief Thread-safe cubic spline */
class CubicSplineMT
{
public:
CubicSplineMT() = default;
CubicSplineMT(const Eigen::VectorXd& x, const Eigen::VectorXd& y);
explicit CubicSplineMT(const std::vector<Voxel>& vs);
~CubicSplineMT() = default;

/** Copy constructor */
CubicSplineMT(const CubicSplineMT& other);

/** Custom copy assignment operator */
auto operator=(const CubicSplineMT& other) -> CubicSplineMT&;

/**
* @brief %Spline evaluation at t-space value t in [0, 1]
*/
auto operator()(double t) const -> Pixel;

private:
Eigen::VectorXd a_x_, b_x_, c_x_, d_x_;
Eigen::VectorXd a_y_, b_y_, c_y_, d_y_;
Eigen::VectorXd range_xy_;
Eigen::VectorXd subsegment_lengths_;
Eigen::VectorXd cumulative_lengths_;
std::mutex mtx_;
std::size_t npoints_{0};
};
4 changes: 2 additions & 2 deletions segmentation/include/vc/segmentation/lrps/FittedCurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>

#include "vc/segmentation/lrps/Common.hpp"
#include "vc/segmentation/lrps/CubicMultithreadedSpline.hpp"
#include "vc/segmentation/lrps/CubicSplineMT.hpp"

namespace volcart::segmentation
{
Expand All @@ -28,7 +28,7 @@ class FittedCurve
/** List of sampled points */
std::vector<Voxel> points_;
/** Spline representation of curve */
CubicMultithreadedSpline spline_;
CubicSplineMT spline_;

public:
/** @name Constructors */
Expand Down
37 changes: 19 additions & 18 deletions segmentation/include/vc/segmentation/lrps/Spline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@

namespace volcart::segmentation
{

/**
* @brief Combine X and Y values into an npoints x 2 matrix
*/
template <class Vector>
auto MakeWideMtx(const Vector& xs, const Vector& ys) -> Eigen::MatrixXd
{
Eigen::MatrixXd mat{2, xs.size()};
mat.row(0) = Eigen::VectorXd::Map(xs.data(), xs.size());
mat.row(1) = Eigen::VectorXd::Map(ys.data(), ys.size());
return mat;
}

/**
* @class Spline
* @brief Simple spline wrapper around Eigen::Spline
Expand All @@ -22,8 +35,9 @@ template <typename Scalar = double, int Degree = 3>
class Spline
{
public:
using ScalarVector = std::vector<Scalar>;
using Vector = std::vector<Scalar>;
using SplineType = Eigen::Spline<Scalar, 2>;
using Fitting = Eigen::SplineFitting<SplineType>;

Spline() = default;

Expand All @@ -33,11 +47,10 @@ class Spline
* @param xs Vector of X values
* @param ys Vector of Y values
*/
Spline(const ScalarVector& xs, const ScalarVector& ys) : npoints_{xs.size()}
Spline(const Vector& xs, const Vector& ys) : npoints_{xs.size()}
{
assert(xs.size() == ys.size() && "xs and ys must be same length");
auto points = make_wide_matrix_(xs, ys);
spline_ = Eigen::SplineFitting<SplineType>::Interpolate(points, Degree);
spline_ = Fitting::Interpolate(MakeWideMtx(xs, ys), Degree);
}

/**
Expand All @@ -52,21 +65,9 @@ class Spline

private:
/** Number of points on the spline */
std::size_t npoints_;

std::size_t npoints_{0};
/** Eigen spline */
SplineType spline_;

/**
* @brief Combine X and Y values into an npoints_ x 2 matrix
*/
Eigen::MatrixXd make_wide_matrix_(
const ScalarVector& xs, const ScalarVector& ys)
{
Eigen::MatrixXd mat{2, xs.size()};
mat.row(0) = Eigen::VectorXd::Map(xs.data(), xs.size());
mat.row(1) = Eigen::VectorXd::Map(ys.data(), ys.size());
return mat;
}
};

/**
Expand Down
Loading

0 comments on commit f1e3fda

Please sign in to comment.