Volume Cartographer 2.27.0
Transforms.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cstddef>
6#include <iostream>
7#include <list>
8#include <memory>
9#include <string>
10#include <vector>
11
12#include <nlohmann/json.hpp>
13#include <opencv2/core.hpp>
14
18
19namespace volcart
20{
21
50{
51public:
53 static constexpr std::string_view TYPE{"Transform3D"};
54
56 using Pointer = std::shared_ptr<Transform3D>;
57
59 using Identifier = std::string;
60
62 virtual ~Transform3D() = default;
66 auto operator=(Transform3D&& other) -> Transform3D& = delete;
67
69 [[nodiscard]] virtual auto type() const -> std::string_view = 0;
71 [[nodiscard]] virtual auto clone() const -> Pointer = 0;
72
74 [[nodiscard]] virtual auto invertible() const -> bool;
76 [[nodiscard]] virtual auto invert() const -> Pointer;
78 [[nodiscard]] virtual auto composable() const -> bool;
79
88 virtual void reset() = 0;
90 void clear();
91
98 void source(const std::string& src);
104 [[nodiscard]] auto source() const -> std::string;
110 void target(const std::string& tgt);
116 [[nodiscard]] auto target() const -> std::string;
117
119 [[nodiscard]] virtual auto applyPoint(const cv::Vec3d& point) const
120 -> cv::Vec3d = 0;
122 [[nodiscard]] virtual auto applyVector(const cv::Vec3d& vector) const
123 -> cv::Vec3d = 0;
130 [[nodiscard]] auto applyUnitVector(const cv::Vec3d& vector) const
131 -> cv::Vec3d;
142 [[nodiscard]] auto applyPointAndNormal(
143 const cv::Vec6d& ptN, bool normalize = true) const -> cv::Vec6d;
144
169 static auto Compose(const Pointer& lhs, const Pointer& rhs)
170 -> std::pair<Pointer, Pointer>;
171
173 static void Save(const filesystem::path& path, const Pointer& transform);
175 static auto Load(const filesystem::path& path) -> Pointer;
176
177protected:
179 Transform3D() = default;
181 Transform3D(const Transform3D&) = default;
183 auto operator=(const Transform3D& other) -> Transform3D& = default;
184
190 [[nodiscard]] virtual auto compose_(const Pointer& rhs) const -> Pointer;
191
193 using Metadata = nlohmann::ordered_json;
195 static auto Serialize(const Pointer& transform) -> Metadata;
197 static auto Deserialize(const Metadata& meta) -> Pointer;
199 virtual void to_meta_(Metadata& meta) = 0;
201 virtual void from_meta_(const Metadata& meta) = 0;
202
203private:
205 std::string src_;
207 std::string tgt_;
208};
209
219auto operator*(const Transform3D::Pointer& lhs, const Transform3D::Pointer& rhs)
221
243{
244public:
246 static constexpr std::string_view TYPE{"AffineTransform"};
247
249 using Parameters = cv::Matx<double, 4, 4>;
250
252 using Pointer = std::shared_ptr<AffineTransform>;
253
255 static auto New() -> Pointer;
256
258 [[nodiscard]] auto type() const -> std::string_view final;
260 [[nodiscard]] auto clone() const -> Transform3D::Pointer final;
262 [[nodiscard]] auto invertible() const -> bool final;
264 [[nodiscard]] auto invert() const -> Transform3D::Pointer final;
266 [[nodiscard]] auto composable() const -> bool final;
268 void reset() final;
269
271 [[nodiscard]] auto applyPoint(const cv::Vec3d& point) const
272 -> cv::Vec3d final;
274 [[nodiscard]] auto applyVector(const cv::Vec3d& vector) const
275 -> cv::Vec3d final;
276
278 [[nodiscard]] auto params() const -> Parameters;
280 void params(const Parameters& params);
281
287 void translate(double x, double y, double z);
288
294 template <typename Tp, int Cn>
295 void translate(const cv::Vec<Tp, Cn>& translation)
296 {
297 static_assert(Cn >= 3, "cv::Vec must have >= 3 values");
298 translate(translation[0], translation[1], translation[2]);
299 }
300
306 void rotate(double angle, double ax, double ay, double az);
307
315 template <typename Tp, int Cn>
316 void rotate(const double angle, cv::Vec<Tp, Cn> axis)
317 {
318 static_assert(Cn >= 3, "cv::Vec must have >= 3 values");
319 rotate(angle, axis[0], axis[1], axis[2]);
320 }
321
323 void scale(double sx, double sy, double sz);
324
326 void scale(double s);
327
328private:
330 AffineTransform() = default;
332 Parameters params_{Parameters::eye()};
334 [[nodiscard]] auto compose_(const Transform3D::Pointer& rhs) const
335 -> Transform3D::Pointer final;
337 void to_meta_(Metadata& meta) final;
339 void from_meta_(const Metadata& meta) final;
340};
341
355{
356public:
358 static constexpr std::string_view TYPE{"IdentityTransform"};
359
361 using Pointer = std::shared_ptr<IdentityTransform>;
362
364 static auto New() -> Pointer;
365
367 [[nodiscard]] auto type() const -> std::string_view final;
369 [[nodiscard]] auto clone() const -> Transform3D::Pointer final;
371 [[nodiscard]] auto invertible() const -> bool final;
373 [[nodiscard]] auto invert() const -> Transform3D::Pointer final;
375 [[nodiscard]] auto composable() const -> bool final;
377 void reset() final;
378
380 [[nodiscard]] auto applyPoint(const cv::Vec3d& point) const
381 -> cv::Vec3d final;
383 [[nodiscard]] auto applyVector(const cv::Vec3d& vector) const
384 -> cv::Vec3d final;
385
386private:
388 IdentityTransform() = default;
390 [[nodiscard]] auto compose_(const Transform3D::Pointer& rhs) const
391 -> Transform3D::Pointer final;
393 void to_meta_(Metadata& meta) final;
395 void from_meta_(const Metadata& meta) final;
396};
397
437{
438public:
440 static constexpr std::string_view TYPE{"CompositeTransform"};
441
443 using Pointer = std::shared_ptr<CompositeTransform>;
444
446 static auto New() -> Pointer;
447
449 [[nodiscard]] auto type() const -> std::string_view final;
451 [[nodiscard]] auto clone() const -> Transform3D::Pointer final;
453 void reset() final;
454
456 [[nodiscard]] auto applyPoint(const cv::Vec3d& point) const
457 -> cv::Vec3d final;
459 [[nodiscard]] auto applyVector(const cv::Vec3d& vector) const
460 -> cv::Vec3d final;
461
469 void push_front(const Transform3D::Pointer& t);
470
478 void push_back(const Transform3D::Pointer& t);
479
481 [[nodiscard]] auto size() const noexcept -> std::size_t;
482
490 void simplify();
491
493 [[nodiscard]] auto transforms() const -> std::vector<Transform3D::Pointer>;
494
495private:
499 std::list<Transform3D::Pointer> tfms_;
501 void to_meta_(Metadata& meta) final;
503 void from_meta_(const Metadata& meta) final;
504};
505
508 const ITKMesh::Pointer& mesh,
509 const Transform3D::Pointer& transform,
510 bool normalize = true) -> ITKMesh::Pointer;
511
514 const PerPixelMap& ppm,
515 const Transform3D::Pointer& transform,
516 bool normalize = true) -> PerPixelMap;
517
520 const PerPixelMap::Pointer& ppm,
521 const Transform3D::Pointer& transform,
522 bool normalize = true) -> PerPixelMap::Pointer;
523
525template <class PointSetT>
526auto ApplyTransform(const PointSetT& ps, const Transform3D::Pointer& transform)
527 -> PointSetT;
528
529} // namespace volcart
530
531#include "vc/core/types/TransformsImpl.hpp"
3D affine transform
Definition: Transforms.hpp:243
void rotate(const double angle, cv::Vec< Tp, Cn > axis)
Add rotation in degrees.
Definition: Transforms.hpp:316
void rotate(double angle, double ax, double ay, double az)
Add rotation in degrees.
cv::Matx< double, 4, 4 > Parameters
Definition: Transforms.hpp:249
void from_meta_(const Metadata &meta) final
static auto New() -> Pointer
Create a new AffineTransform.
void to_meta_(Metadata &meta) final
void scale(double s)
Add isotropic scale.
auto compose_(const Transform3D::Pointer &rhs) const -> Transform3D::Pointer final
auto type() const -> std::string_view final
Return a string representation of the transform type.
void scale(double sx, double sy, double sz)
Add anisotropic scale.
Collection of transforms.
Definition: Transforms.hpp:437
static auto New() -> Pointer
Create a new CompositeTransform.
auto type() const -> std::string_view final
Return a string representation of the transform type.
Identity transform.
Definition: Transforms.hpp:355
auto type() const -> std::string_view final
Return a string representation of the transform type.
static auto New() -> Pointer
Create a new IdentityTransform.
A raster of a UVMap that provides a per-pixel mapping between a Volume and a Texture generated from t...
Definition: PerPixelMap.hpp:50
Base class for 3D transforms.
Definition: Transforms.hpp:50
auto target() const -> std::string
Set the identifier for the target space.
std::shared_ptr< Transform3D > Pointer
Definition: Transforms.hpp:56
static void Save(const filesystem::path &path, const Pointer &transform)
Save a transform to a JSON file.
static auto Compose(const Pointer &lhs, const Pointer &rhs) -> std::pair< Pointer, Pointer >
Compose two transforms into a single new transform.
virtual void to_meta_(Metadata &meta)=0
auto applyPointAndNormal(const cv::Vec6d &ptN, bool normalize=true) const -> cv::Vec6d
Transform a 3D point and surface normal stored in a cv::Vec6d.
virtual auto clone() const -> Pointer=0
Clone the transform.
static auto Deserialize(const Metadata &meta) -> Pointer
auto source() const -> std::string
Get the source space identifier.
virtual auto type() const -> std::string_view=0
Return a string representation of the transform type.
std::string Identifier
Definition: Transforms.hpp:59
virtual auto invert() const -> Pointer
Return the inverted transform.
auto applyUnitVector(const cv::Vec3d &vector) const -> cv::Vec3d
Transform a 3D direction unit vector.
static auto Load(const filesystem::path &path) -> Pointer
Load a transform from a JSON file.
virtual auto applyVector(const cv::Vec3d &vector) const -> cv::Vec3d=0
Transform a 3D direction vector.
virtual auto applyPoint(const cv::Vec3d &point) const -> cv::Vec3d=0
Transform a 3D point.
static auto Serialize(const Pointer &transform) -> Metadata
virtual auto composable() const -> bool
Return whether the underlying transform is composable.
virtual ~Transform3D()=default
virtual auto compose_(const Pointer &rhs) const -> Pointer
virtual void from_meta_(const Metadata &meta)=0
virtual void reset()=0
Reset the transform parameters.
auto operator=(Transform3D &&other) -> Transform3D &=delete
nlohmann::ordered_json Metadata
Definition: Transforms.hpp:193
Transform3D(Transform3D &&)=delete
virtual auto invertible() const -> bool
Return whether the underlying transform is invertible.
void clear()
Clears all parameters and properties of the transform.
static constexpr std::string_view TYPE
Transform type string constant.
Definition: Transforms.hpp:53
Volume Cartographer library
itk::Mesh< ITKPixel, 3, ITKMeshTraits > ITKMesh
Definition: ITKMesh.hpp:22
auto ApplyTransform(const ITKMesh::Pointer &mesh, const Transform3D::Pointer &transform, bool normalize=true) -> ITKMesh::Pointer
Apply a transform to an ITKMesh.