diff --git a/Modules/Filtering/PolarTransform/CMakeLists.txt b/Modules/Filtering/PolarTransform/CMakeLists.txt new file mode 100644 index 000000000000..a00b4028b496 --- /dev/null +++ b/Modules/Filtering/PolarTransform/CMakeLists.txt @@ -0,0 +1,3 @@ +project(PolarTransform) + +itk_module_impl() diff --git a/Modules/Filtering/PolarTransform/LICENSE b/Modules/Filtering/PolarTransform/LICENSE new file mode 100644 index 000000000000..62589edd12a3 --- /dev/null +++ b/Modules/Filtering/PolarTransform/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Modules/Filtering/PolarTransform/README.md b/Modules/Filtering/PolarTransform/README.md new file mode 100644 index 000000000000..106c3572debf --- /dev/null +++ b/Modules/Filtering/PolarTransform/README.md @@ -0,0 +1,40 @@ +# PolarTransform + +In-tree ITK module providing forward and inverse Cartesian↔polar +coordinate transforms (`itk::CartesianToPolarTransform` and +`itk::PolarToCartesianTransform`) for use in registration, resampling, +and geometric reasoning over polar / cylindrical images. + +## Origin + +Ingested from the standalone remote module +[**InsightSoftwareConsortium/ITKPolarTransform**](https://github.com/InsightSoftwareConsortium/ITKPolarTransform) +on 2026-05-05, at upstream commit +[`65b3c950`](https://github.com/InsightSoftwareConsortium/ITKPolarTransform/commit/65b3c9509a8c8570a262e02ca283e412f77bafaa). +The upstream repository will be archived read-only after this PR +merges; it remains reachable at the URL above. + +## What lives here + +Per the v4 ingestion strategy (see +`Utilities/Maintenance/RemoteModuleIngest/INGESTION_STRATEGY_v4.md`), +only paths matching the narrow whitelist +(`Utilities/Maintenance/RemoteModuleIngest/whitelists/PolarTransform.list`) +crossed the merge boundary: + +- `include/` — public C++ headers (header-only module; no `src/`). +- `test/` — CTest drivers and content-link stubs. +- `wrapping/` — Python wrapping descriptors. +- `CMakeLists.txt`, `itk-module.cmake` — module metadata. +- `LICENSE` — original Apache-2.0 license text. + +Upstream-only artifacts (CI dashboards, packaging, +`.github/`, Docker scaffolding, `pyproject.toml`, etc.) were +explicitly excluded; the upstream commit graph and authorship history +are otherwise preserved through a `--no-ff --allow-unrelated-histories` +Mode-A merge. + +## Building + +PolarTransform is enabled by default once `Module_PolarTransform=ON` +is set; in CI it is enabled via `pixi.toml`'s `configure-ci` task. diff --git a/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.h b/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.h new file mode 100644 index 000000000000..1be4b3289427 --- /dev/null +++ b/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.h @@ -0,0 +1,201 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#ifndef itkCartesianToPolarTransform_h +#define itkCartesianToPolarTransform_h + +#include +#include "itkTransform.h" +#include "itkMacro.h" +#include "itkMatrix.h" + +namespace itk +{ + +/** \class CartesianToPolarTransform + * + * \brief Polar transformation of a vector space (e.g. space coordinates). + * + * Transforms first two coordinates form cartesian coordinates to polar + * coordinates . Other dimensions are left unchanges. In fact + * this is generalized cylindric transform: + * \f[ r = \sqrt{ x_0^2 + x_1^2 } \f] + * \f[ \alpha = \left\{ \begin{array}{ll} + * arccos( \frac{x_0}{r} ) & \mbox{$x_1 >= 0$} \\ + * \mbox{2 \pi} - arccos( \frac{x_0}{r} ) & \mbox{$x_1 < 0$} + * \end{array}\right. \f] + * \f[ x_n = x_n, \mbox{n >= 2} \f] + * + * + * \par + * Center of the polar transform is can be specified with SetCenter(). + * The default is center of coordinate system < 0, 0 >. + * + * Dimension must be at least 2. + * + * \author Jakub Bican, Department of Image Processing, Institute of Information Theory and Automation, Academy of + * Sciences of the Czech Republic. + * + * \ingroup Transforms + * \ingroup PolarTransform + */ +template // Number of dimensions +class ITK_TEMPLATE_EXPORT CartesianToPolarTransform : public Transform +{ +public: + ITK_DISALLOW_COPY_AND_MOVE(CartesianToPolarTransform); + + /** Standard class type alias. */ + using Self = CartesianToPolarTransform; + using Superclass = Transform; + using Pointer = SmartPointer; + using ConstPointer = SmartPointer; + + /** New macro for creation of through the object factory.*/ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkOverrideGetNameOfClassMacro(CartesianToPolarTransform); + + /** Dimension of the domain space. */ + static constexpr unsigned int SpaceDimension = NDimensions; + static constexpr unsigned int ParametersDimension = 0; + + /** Standard scalar type for this class. */ + using ScalarType = typename Superclass::ScalarType; + + /** Standard Jacobian container. */ + using JacobianType = typename Superclass::JacobianType; + + /** Standard parameters container. */ + using ParametersType = typename Superclass::ParametersType; + + /** Standard vector type for this class. */ + using InputVectorType = Vector; + using OutputVectorType = Vector; + + /** Standard covariant vector type for this class. */ + using InputCovariantVectorType = CovariantVector; + using OutputCovariantVectorType = CovariantVector; + + /** Standard vnl_vector type for this class. */ + using InputVnlVectorType = vnl_vector_fixed; + using OutputVnlVectorType = vnl_vector_fixed; + + /** Standard coordinate point type for this class. */ + using InputPointType = Point; + using OutputPointType = Point; + + /** Method to transform a point. + * This method transforms first two dimensions of a point from cartesian + * coordinates to polar coordinates . + */ + OutputPointType + TransformPoint(const InputPointType & point) const override; + + /** Method to transform a vector - not applicable for this type of transform. */ + OutputVectorType + TransformVector(const InputVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputVectorType(); + } + + /** Method to transform a vnl_vector - not applicable for this type of transform. */ + OutputVnlVectorType + TransformVector(const InputVnlVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputVnlVectorType(); + } + + /** Method to transform a vector - not applicable for this type of transform. */ + typename Superclass::OutputVectorPixelType + TransformVector(const typename Superclass::InputVectorPixelType &, const InputPointType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return typename Superclass::OutputVectorPixelType(); + } + + using Superclass::TransformVector; + + /** Method to transform a CovariantVector - not applicable for this type of transform. */ + OutputCovariantVectorType + TransformCovariantVector(const InputCovariantVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputCovariantVectorType(); + } + + using Superclass::TransformCovariantVector; + + void + ComputeJacobianWithRespectToParameters(const InputPointType &, JacobianType &) const override + { + itkExceptionMacro(<< "Method not implemented yet."); + } + + void + SetParameters(const ParametersType &) override + {} + + void + SetFixedParameters(const ParametersType &) override + {} + + /** Set the location of the center of the polar coordinate system. */ + itkSetMacro(Center, InputPointType); + itkGetConstReferenceMacro(Center, InputPointType); + + /** Set an angular offset for the polar coordinate transform. + * + * Defaults to 0.0 + */ + itkSetMacro(AngleOffset, typename OutputPointType::ValueType); + itkGetConstReferenceMacro(AngleOffset, typename OutputPointType::ValueType); + + /** Enable/Disable to use constant arc increment instead of constant angular increment. + * + * Defaults to Off + */ + itkSetMacro(ConstArcIncr, bool); + itkGetMacro(ConstArcIncr, bool); + itkBooleanMacro(ConstArcIncr); + +protected: + CartesianToPolarTransform(); + ~CartesianToPolarTransform() override; + + /** Print contents of an CartesianToPolarTransform. */ + void + PrintSelf(std::ostream & os, Indent indent) const override; + +private: + InputPointType m_Center; + typename OutputPointType::ValueType m_AngleOffset = 0; + bool m_ConstArcIncr = false; +}; // class CartesianToPolarTransform + +} // namespace itk + + +#ifndef ITK_MANUAL_INSTANTIATION +# include "itkCartesianToPolarTransform.hxx" +#endif + +#endif diff --git a/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.hxx b/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.hxx new file mode 100644 index 000000000000..2cc907e4aa6a --- /dev/null +++ b/Modules/Filtering/PolarTransform/include/itkCartesianToPolarTransform.hxx @@ -0,0 +1,81 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#ifndef itkCartesianToPolarTransform_hxx +#define itkCartesianToPolarTransform_hxx + +#include "itkMath.h" + +namespace itk +{ + +template +CartesianToPolarTransform::CartesianToPolarTransform() + : Superclass(ParametersDimension) +{ + this->m_Center.Fill(0.0); +} + + +template +CartesianToPolarTransform::~CartesianToPolarTransform() = default; + + +template +void +CartesianToPolarTransform::PrintSelf(std::ostream & os, Indent indent) const +{ + Superclass::PrintSelf(os, indent); + os << indent << "Center: " << m_Center << std::endl; +} + + +template +typename CartesianToPolarTransform::OutputPointType +CartesianToPolarTransform::TransformPoint(const InputPointType & inputPoint) const +{ + OutputPointType outputPoint(inputPoint); + + const InputVectorType vector = inputPoint - this->m_Center; + + outputPoint[1] = std::sqrt(vector[0] * vector[0] + vector[1] * vector[1]); // r= sqrt(x^2 + y^2) + if (outputPoint[1] == 0.0) + { + // alpha is undefined at r=0; choose 0 to avoid 0/0 NaN propagating through std::acos. + outputPoint[0] = 0.0; + } + else + { + outputPoint[0] = std::acos(vector[0] / outputPoint[1]); // alpha = acos(x/r) + outputPoint[0] += m_AngleOffset; // add offset before 2*pi adjustment to keep values within [-pi,pi] + if (vector[1] < 0.0) + { + outputPoint[0] = Math::twopi - outputPoint[0]; + } + + if (m_ConstArcIncr) + { + outputPoint[0] *= outputPoint[1]; // arc= r*alpha + } + } + + return outputPoint; +} + +} // namespace itk + +#endif diff --git a/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.h b/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.h new file mode 100644 index 000000000000..d841bc35548f --- /dev/null +++ b/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.h @@ -0,0 +1,208 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#ifndef itkPolarToCartesianTransform_h +#define itkPolarToCartesianTransform_h + +#include +#include "itkTransform.h" +#include "itkMacro.h" +#include "itkMatrix.h" + +namespace itk +{ + +/** \class PolarToCartesianTransform + * + * \brief Polar transformation of a vector space (e.g. space coordinates). + * + * Transforms first two coordinates form polar space to cartesian + * coordinates. Other dimensions are left unchanges. In fact this is generalized + * cylindric transform: + * \f[ x_1 = r cos( \alpha ) \f] + * \f[ x_2 = r sin( \alpha ) \f] + * \f[ x_n = x_n, \mbox{ n>=2 } \f] + * + * \par + * Center of the polar transform is can be specified with SetCenter(). + * The default is center of coordinate system < 0, 0 >. + * + * Dimension must be at least 2. + * + * Extent of input in first dimension (alpha) should be only < 0, 2*pi ). + * + * \author Jakub Bican, Department of Image Processing, Institute of Information Theory and Automation, Academy of + * Sciences of the Czech Republic. + * + * \ingroup Transforms + * \ingroup PolarTransform + */ +template // Number of dimensions +class ITK_TEMPLATE_EXPORT PolarToCartesianTransform : public Transform +{ +public: + ITK_DISALLOW_COPY_AND_MOVE(PolarToCartesianTransform); + + /** Standard class type alias. */ + using Self = PolarToCartesianTransform; + using Superclass = Transform; + using Pointer = SmartPointer; + using ConstPointer = SmartPointer; + + /** New macro for creation of through the object factory.*/ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkOverrideGetNameOfClassMacro(PolarToCartesianTransform); + + /** Dimension of the domain space. */ + static constexpr unsigned int SpaceDimension = NDimensions; + static constexpr unsigned int ParametersDimension = 0; + + /** Standard scalar type for this class. */ + using ScalarType = typename Superclass::ScalarType; + + /** Standard Jacobian container. */ + using JacobianType = typename Superclass::JacobianType; + + /** Standard parameters container. */ + using ParametersType = typename Superclass::ParametersType; + + /** Standard vector type for this class. */ + using InputVectorType = Vector; + using OutputVectorType = Vector; + + /** Standard covariant vector type for this class. */ + using InputCovariantVectorType = CovariantVector; + using OutputCovariantVectorType = CovariantVector; + + /** Standard vnl_vector type for this class. */ + using InputVnlVectorType = vnl_vector_fixed; + using OutputVnlVectorType = vnl_vector_fixed; + + /** Standard coordinate point type for this class. */ + using InputPointType = Point; + using OutputPointType = Point; + + /** Method to transform a point. + * This method transforms first two dimensions of a point from polar + * coordinates to cartesian coordinates. + */ + OutputPointType + TransformPoint(const InputPointType & point) const override; + + /** Method to transform a vector - not applicable for this type of transform. */ + OutputVectorType + TransformVector(const InputVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputVectorType(); + } + + /** Method to transform a vnl_vector - not applicable for this type of transform. */ + OutputVnlVectorType + TransformVector(const InputVnlVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputVnlVectorType(); + } + + /** Method to transform a vector - not applicable for this type of transform. */ + typename Superclass::OutputVectorPixelType + TransformVector(const typename Superclass::InputVectorPixelType &, const InputPointType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return typename Superclass::OutputVectorPixelType(); + } + + using Superclass::TransformVector; + + /** Method to transform a CovariantVector - not applicable for this type of transform */ + OutputCovariantVectorType + TransformCovariantVector(const InputCovariantVectorType &) const override + { + itkExceptionMacro(<< "Method not applicable for this type of transform."); + return OutputCovariantVectorType(); + } + + using Superclass::TransformCovariantVector; + + void + ComputeJacobianWithRespectToParameters(const InputPointType &, JacobianType &) const override + { + itkExceptionMacro(<< "Method not implemented yet."); + } + + void + SetParameters(const ParametersType &) override + {} + + void + SetFixedParameters(const ParametersType &) override + {} + + /** Set the location of the center of the polar coordinate system. */ + itkSetMacro(Center, OutputPointType); + itkGetConstReferenceMacro(Center, OutputPointType); + + /** Set an angular offset for the polar coordinate transform. + * + * Defaults to 0.0 + */ + itkSetMacro(AngleOffset, typename InputPointType::ValueType); + itkGetConstReferenceMacro(AngleOffset, typename InputPointType::ValueType); + + /** Enable/Disable to use constant arc increment instead of constant angular increment. + * + * Defaults to Off + */ + itkSetMacro(ConstArcIncr, bool); + itkGetMacro(ConstArcIncr, bool); + itkBooleanMacro(ConstArcIncr); + + /** Enable/Disable to return NaN in case alpha is outside [-pi,pi]. + * + * Defaults to Off + */ + itkSetMacro(ReturnNaN, bool); + itkGetMacro(ReturnNaN, bool); + itkBooleanMacro(ReturnNaN); + +protected: + PolarToCartesianTransform(); + ~PolarToCartesianTransform() override; + + /** Print contents of an PolarToCartesianTransform. */ + void + PrintSelf(std::ostream & os, Indent indent) const override; + +private: + OutputPointType m_Center; + typename InputPointType::ValueType m_AngleOffset = 0; + bool m_ConstArcIncr = false; + bool m_ReturnNaN = false; +}; // class PolarToCartesianTransform + +} // namespace itk + + +#ifndef ITK_MANUAL_INSTANTIATION +# include "itkPolarToCartesianTransform.hxx" +#endif + +#endif diff --git a/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.hxx b/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.hxx new file mode 100644 index 000000000000..57ce37cd33ec --- /dev/null +++ b/Modules/Filtering/PolarTransform/include/itkPolarToCartesianTransform.hxx @@ -0,0 +1,85 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#ifndef itkPolarToCartesianTransform_hxx +#define itkPolarToCartesianTransform_hxx + + +namespace itk +{ + +template +PolarToCartesianTransform::PolarToCartesianTransform() + : Superclass(ParametersDimension) +{ + this->m_Center.Fill(0.0); +} + + +template +PolarToCartesianTransform::~PolarToCartesianTransform() = default; + + +template +void +PolarToCartesianTransform::PrintSelf(std::ostream & os, Indent indent) const +{ + Superclass::PrintSelf(os, indent); + os << indent << "Center: " << m_Center << std::endl; +} + + +template +typename PolarToCartesianTransform::OutputPointType +PolarToCartesianTransform::TransformPoint(const InputPointType & inputPoint) const +{ + OutputPointType outputPoint(inputPoint); + + double alpha = inputPoint[0]; + if (m_ConstArcIncr) + { + if (inputPoint[1] == 0.0) + { + // r=0: output collapses to the center; alpha is undefined, avoid arc/r = 0/0 or arc/0. + alpha = 0.0; + } + else + { + alpha /= inputPoint[1]; // alpha = arc/r + } + } + + if (m_ReturnNaN && (alpha < -Math::pi || Math::pi < alpha)) + { + using PointNumericTraits = NumericTraits; + outputPoint[0] = PointNumericTraits::quiet_NaN(); + outputPoint[1] = PointNumericTraits::quiet_NaN(); + return outputPoint; + } + + alpha += m_AngleOffset; // add offset after NaN return to keep values within [-pi,pi] + + // Pass-through dims (ii >= 2) keep inputPoint values to match CartesianToPolar's round-trip. + outputPoint[0] = this->m_Center[0] + inputPoint[1] * std::cos(alpha); // m_Center[0] + r*cos(alpha) + outputPoint[1] = this->m_Center[1] + inputPoint[1] * std::sin(alpha); // m_Center[1] + r*sin(alpha) + + return outputPoint; +} + +} // namespace itk + +#endif diff --git a/Modules/Filtering/PolarTransform/itk-module.cmake b/Modules/Filtering/PolarTransform/itk-module.cmake new file mode 100644 index 000000000000..3385c1af928f --- /dev/null +++ b/Modules/Filtering/PolarTransform/itk-module.cmake @@ -0,0 +1,23 @@ +# the top-level README is used for describing this module, just +# re-used it for documentation here +get_filename_component(MY_CURRENT_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +file(READ "${MY_CURRENT_DIR}/README.md" DOCUMENTATION) + +# itk_module() defines the module dependencies in PolarTransform +# PolarTransform depends on ITKCommon +# The testing module in PolarTransform depends on ITKTestKernel +# and ITKMetaIO(besides PolarTransform and ITKCore) +# By convention those modules outside of ITK are not prefixed with +# ITK. + +# define the dependencies of the include module and the tests +itk_module( + PolarTransform + COMPILE_DEPENDS + ITKTransform + TEST_DEPENDS + ITKTestKernel + DESCRIPTION "${DOCUMENTATION}" + EXCLUDE_FROM_DEFAULT + ENABLE_SHARED +) diff --git a/Modules/Filtering/PolarTransform/test/CMakeLists.txt b/Modules/Filtering/PolarTransform/test/CMakeLists.txt new file mode 100644 index 000000000000..0b5ab93f4739 --- /dev/null +++ b/Modules/Filtering/PolarTransform/test/CMakeLists.txt @@ -0,0 +1,23 @@ +itk_module_test() + +set(PolarTransformTests itkPolarTransformTest.cxx) + +createtestdriver(PolarTransform "${PolarTransform-Test_LIBRARIES}" "${PolarTransformTests}") + +itk_add_test( + NAME itkPolarTransformTest + COMMAND + PolarTransformTestDriver + itkPolarTransformTest +) + +if(ITK_WRAP_PYTHON) + itk_python_expression_add_test( + NAME itkPolarToCartesianTransformPythonTest + EXPRESSION "instance = itk.PolarToCartesianTransform.New()" + ) + itk_python_expression_add_test( + NAME itkCartesianToPolarTransformPythonTest + EXPRESSION "instance = itk.CartesianToPolarTransform.New()" + ) +endif() diff --git a/Modules/Filtering/PolarTransform/test/itkPolarTransformTest.cxx b/Modules/Filtering/PolarTransform/test/itkPolarTransformTest.cxx new file mode 100644 index 000000000000..13f3f47c72d9 --- /dev/null +++ b/Modules/Filtering/PolarTransform/test/itkPolarTransformTest.cxx @@ -0,0 +1,159 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#include "itkPolarToCartesianTransform.h" +#include "itkCartesianToPolarTransform.h" +#include "itkMath.h" +#include + +int +itkPolarTransformTest(int, char *[]) +{ + constexpr unsigned int Dimension = 4; + const double epsilon = 1e-10; + + /* Create 3D polar transforms and print them. */ + using P2CTransformType = itk::PolarToCartesianTransform; + using C2PTransformType = itk::CartesianToPolarTransform; + P2CTransformType::Pointer p2c = P2CTransformType::New(); + C2PTransformType::Pointer c2p = C2PTransformType::New(); + + P2CTransformType::InputPointType center; + center.Fill(0.0); + center[0] = -1.0; + + p2c->SetCenter(center); + c2p->SetCenter(center); + + std::cout << "Polar to cartesian transform: " << p2c << std::endl; + std::cout << "Cartesian to polar transform: " << c2p << std::endl; + + + /* Create testing points in cartesian and polar space. */ + itk::Point c, p, tmp; + c[0] = 0.0; + c[1] = std::sqrt(3.0); + p[0] = itk::Math::pi / 3.0; + p[1] = 2.0; + for (unsigned int i = 2; i < Dimension; ++i) + { + c[i] = 3.0; + p[i] = c[i]; + } + + /* Transform point from polar to cartesian space. */ + tmp = p2c->TransformPoint(p); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (itk::Math::abs(tmp[i] - c[i]) > epsilon) + { + return EXIT_FAILURE; + } + } + + /* Transform point from cartesian to polar space. */ + tmp = c2p->TransformPoint(c); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (itk::Math::abs(tmp[i] - p[i]) > epsilon) + { + std::cout << "Invalid cartesian to polar computed." << std::endl; + return EXIT_FAILURE; + } + } + + /* Transform point from polar to cartesian space and back. */ + tmp = c2p->TransformPoint(p2c->TransformPoint(p)); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (itk::Math::abs(tmp[i] - p[i]) > epsilon) + { + std::cout << "Invalid polar to cartesian and back computed." << std::endl; + return EXIT_FAILURE; + } + } + + /* Transform point from cartesian to polar space and back. */ + tmp = p2c->TransformPoint(c2p->TransformPoint(c)); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (itk::Math::abs(tmp[i] - c[i]) > epsilon) + { + std::cout << "Invalid cartesian to polar and back computed." << std::endl; + return EXIT_FAILURE; + } + } + + /* Regression: r=0 (input at center) must not produce NaN in CartesianToPolar. */ + { + itk::Point atCenter(center); + tmp = c2p->TransformPoint(atCenter); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (std::isnan(tmp[i])) + { + std::cout << "NaN produced when input equals center (CartesianToPolar)." << std::endl; + return EXIT_FAILURE; + } + } + if (tmp[1] != 0.0) + { + std::cout << "r should be 0 when input equals center." << std::endl; + return EXIT_FAILURE; + } + } + + /* Regression: ConstArcIncr=true with r=0 must not divide by zero. */ + { + P2CTransformType::Pointer p2cArc = P2CTransformType::New(); + p2cArc->SetConstArcIncr(true); + itk::Point rZero; + rZero.Fill(0.0); + tmp = p2cArc->TransformPoint(rZero); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (std::isnan(tmp[i]) || std::isinf(tmp[i])) + { + std::cout << "NaN/Inf produced for r=0 with ConstArcIncr (PolarToCartesian)." << std::endl; + return EXIT_FAILURE; + } + } + } + + /* Regression: round-trip through pass-through dims with non-zero center in dim >= 2. */ + { + P2CTransformType::InputPointType offsetCenter; + offsetCenter.Fill(0.0); + offsetCenter[2] = 5.0; + P2CTransformType::Pointer p2cOff = P2CTransformType::New(); + C2PTransformType::Pointer c2pOff = C2PTransformType::New(); + p2cOff->SetCenter(offsetCenter); + c2pOff->SetCenter(offsetCenter); + + tmp = p2cOff->TransformPoint(c2pOff->TransformPoint(c)); + for (unsigned int i = 0; i < Dimension; ++i) + { + if (itk::Math::abs(tmp[i] - c[i]) > epsilon) + { + std::cout << "Round-trip failed with non-zero center in pass-through dim." << std::endl; + return EXIT_FAILURE; + } + } + } + + return EXIT_SUCCESS; +} diff --git a/Modules/Filtering/PolarTransform/wrapping/CMakeLists.txt b/Modules/Filtering/PolarTransform/wrapping/CMakeLists.txt new file mode 100644 index 000000000000..fbd29bfcc775 --- /dev/null +++ b/Modules/Filtering/PolarTransform/wrapping/CMakeLists.txt @@ -0,0 +1,3 @@ +itk_wrap_module(PolarTransform) +itk_auto_load_submodules() +itk_end_wrap_module() diff --git a/Modules/Filtering/PolarTransform/wrapping/itkCartesianToPolarTransform.wrap b/Modules/Filtering/PolarTransform/wrapping/itkCartesianToPolarTransform.wrap new file mode 100644 index 000000000000..2f6ffd22efd3 --- /dev/null +++ b/Modules/Filtering/PolarTransform/wrapping/itkCartesianToPolarTransform.wrap @@ -0,0 +1,5 @@ +itk_wrap_class("itk::CartesianToPolarTransform" POINTER) +foreach(d ${ITK_WRAP_IMAGE_DIMS}) + itk_wrap_template("${ITKM_D}${d}" "${ITKT_D},${d}") +endforeach() +itk_end_wrap_class() diff --git a/Modules/Filtering/PolarTransform/wrapping/itkPolarToCartesianTransform.wrap b/Modules/Filtering/PolarTransform/wrapping/itkPolarToCartesianTransform.wrap new file mode 100644 index 000000000000..4a338d885249 --- /dev/null +++ b/Modules/Filtering/PolarTransform/wrapping/itkPolarToCartesianTransform.wrap @@ -0,0 +1,5 @@ +itk_wrap_class("itk::PolarToCartesianTransform" POINTER) +foreach(d ${ITK_WRAP_IMAGE_DIMS}) + itk_wrap_template("${ITKM_D}${d}" "${ITKT_D},${d}") +endforeach() +itk_end_wrap_class() diff --git a/Modules/Remote/PolarTransform.remote.cmake b/Modules/Remote/PolarTransform.remote.cmake deleted file mode 100644 index dd03dfd5aba3..000000000000 --- a/Modules/Remote/PolarTransform.remote.cmake +++ /dev/null @@ -1,59 +0,0 @@ -#-- # Grading Level Criteria Report -#-- EVALUATION DATE: 2020-03-01 -#-- EVALUATORS: [<>,<>] -#-- -#-- ## Compliance level 5 star (AKA ITK main modules, or remote modules that could become core modules) -#-- - [ ] Widespread community dependance -#-- - [ ] Above 90% code coverage -#-- - [ ] CI dashboards and testing monitored rigorously -#-- - [ ] Key API features are exposed in wrapping interface -#-- - [ ] All requirements of Levels 4,3,2,1 -#-- -#-- ## Compliance Level 4 star (Very high-quality code, perhaps small community dependance) -#-- - [ ] Meets all ITK code style standards -#-- - [ ] No external requirements beyond those needed by ITK proper -#-- - [ ] Builds and passes tests on all supported platforms within 1 month of each core tagged release -#-- - [ ] Windows Shared Library Build with Visual Studio -#-- - [ ] Mac with clang compiller -#-- - [ ] Linux with gcc compiler -#-- - [ ] Active developer community dedicated to maintaining code-base -#-- - [ ] 75% code coverage demonstrated for testing suite -#-- - [ ] Continuous integration testing performed -#-- - [ ] All requirements of Levels 3,2,1 -#-- -#-- ## Compliance Level 3 star (Quality beta code) -#-- - [ ] API | executable interface is considered mostly stable and feature complete -#-- - [ ] 10% C0-code coverage demonstrated for testing suite -#-- - [ ] Some tests exist and pass on at least some platform -#-- - [X] All requirements of Levels 2,1 -#-- -#-- ## Compliance Level 2 star (Alpha code feature API development or niche community/execution environment dependance ) -#-- - [X] Compiles for at least 1 niche set of execution envirionments, and perhaps others -#-- (may depend on specific external tools like a java environment, or specific external libraries to work ) -#-- - [X] All requirements of Levels 1 -#-- -#-- ## Compliance Level 1 star (Pre-alpha features under development and code of unknown quality) -#-- - [X] Code complies on at least 1 platform -#-- -#-- ## Compliance Level 0 star ( Code/Feature of known poor-quality or deprecated status ) -#-- - [ ] Code reviewed and explicitly identified as not recommended for use -#-- -#-- ### Please document here any justification for the criteria above -# Code style enforced by clang-format on 2020-02-19, and clang-tidy modernizations completed - -# Contact: Matt McCormick -itk_fetch_module( - PolarTransform - "This is a module to transform from cartesian to polar coordinate systems and back. - -For more information, see the Insight Journal article: - - Bican J. - Polar Transforms for ITK - The Insight Journal. July-December. 2006. - https://doi.org/10.54294/t8t3j2 - " - MODULE_COMPLIANCE_LEVEL 2 - GIT_REPOSITORY https://github.com/InsightSoftwareConsortium/ITKPolarTransform.git - GIT_TAG 6967f9bebbc28af8c8983f9130b8663e0b3aac60 - ) diff --git a/pyproject.toml b/pyproject.toml index 9f1dd62bf5ba..5cf81f247832 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,7 @@ cmd = '''cmake -DModule_LabelErodeDilate:BOOL=ON -DModule_MeshNoise:BOOL=ON -DModule_MGHIO:BOOL=ON + -DModule_PolarTransform:BOOL=ON -DModule_SplitComponents:BOOL=ON -DModule_IOMeshMZ3:BOOL=ON -DModule_IOFDF:BOOL=ON