2022-05-06 08:41:23 -07:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <wpi/SymbolExports.h>
|
2023-10-19 21:41:47 -07:00
|
|
|
#include <wpi/protobuf/Protobuf.h>
|
|
|
|
|
#include <wpi/struct/Struct.h>
|
2022-05-06 08:41:23 -07:00
|
|
|
|
2023-07-31 19:17:02 -07:00
|
|
|
#include "frc/geometry/Translation3d.h"
|
2022-05-06 08:41:23 -07:00
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
|
|
|
|
|
class WPILIB_DLLEXPORT Pose3d;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-06 19:59:42 -07:00
|
|
|
* Represents a transformation for a Pose3d in the pose's frame.
|
2022-05-06 08:41:23 -07:00
|
|
|
*/
|
|
|
|
|
class WPILIB_DLLEXPORT Transform3d {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs the transform that maps the initial pose to the final pose.
|
|
|
|
|
*
|
|
|
|
|
* @param initial The initial pose for the transformation.
|
|
|
|
|
* @param final The final pose for the transformation.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d(Pose3d initial, Pose3d final);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a transform with the given translation and rotation components.
|
|
|
|
|
*
|
|
|
|
|
* @param translation Translational component of the transform.
|
|
|
|
|
* @param rotation Rotational component of the transform.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d(Translation3d translation, Rotation3d rotation);
|
|
|
|
|
|
2023-10-13 11:51:39 +05:30
|
|
|
/**
|
|
|
|
|
* Constructs a transform with x, y, and z translations instead of a separate
|
|
|
|
|
* Translation3d.
|
|
|
|
|
*
|
|
|
|
|
* @param x The x component of the translational component of the transform.
|
|
|
|
|
* @param y The y component of the translational component of the transform.
|
|
|
|
|
* @param z The z component of the translational component of the transform.
|
|
|
|
|
* @param rotation The rotational component of the transform.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d(units::meter_t x, units::meter_t y, units::meter_t z,
|
|
|
|
|
Rotation3d rotation);
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs the identity transform -- maps an initial pose to itself.
|
|
|
|
|
*/
|
|
|
|
|
constexpr Transform3d() = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the translation component of the transformation.
|
|
|
|
|
*
|
|
|
|
|
* @return Reference to the translational component of the transform.
|
|
|
|
|
*/
|
|
|
|
|
const Translation3d& Translation() const { return m_translation; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the X component of the transformation's translation.
|
|
|
|
|
*
|
|
|
|
|
* @return The x component of the transformation's translation.
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t X() const { return m_translation.X(); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the Y component of the transformation's translation.
|
|
|
|
|
*
|
|
|
|
|
* @return The y component of the transformation's translation.
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t Y() const { return m_translation.Y(); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the Z component of the transformation's translation.
|
|
|
|
|
*
|
|
|
|
|
* @return The z component of the transformation's translation.
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t Z() const { return m_translation.Z(); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the rotational component of the transformation.
|
|
|
|
|
*
|
|
|
|
|
* @return Reference to the rotational component of the transform.
|
|
|
|
|
*/
|
|
|
|
|
const Rotation3d& Rotation() const { return m_rotation; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invert the transformation. This is useful for undoing a transformation.
|
|
|
|
|
*
|
|
|
|
|
* @return The inverted transformation.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d Inverse() const;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-28 21:34:29 -07:00
|
|
|
* Multiplies the transform by the scalar.
|
2022-05-06 08:41:23 -07:00
|
|
|
*
|
|
|
|
|
* @param scalar The scalar.
|
|
|
|
|
* @return The scaled Transform3d.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d operator*(double scalar) const {
|
|
|
|
|
return Transform3d(m_translation * scalar, m_rotation * scalar);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 21:34:29 -07:00
|
|
|
/**
|
|
|
|
|
* Divides the transform by the scalar.
|
|
|
|
|
*
|
|
|
|
|
* @param scalar The scalar.
|
|
|
|
|
* @return The scaled Transform3d.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d operator/(double scalar) const { return *this * (1.0 / scalar); }
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
/**
|
2023-08-15 13:12:09 -07:00
|
|
|
* Composes two transformations. The second transform is applied relative to
|
|
|
|
|
* the orientation of the first.
|
2022-05-06 08:41:23 -07:00
|
|
|
*
|
|
|
|
|
* @param other The transform to compose with this one.
|
|
|
|
|
* @return The composition of the two transformations.
|
|
|
|
|
*/
|
|
|
|
|
Transform3d operator+(const Transform3d& other) const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks equality between this Transform3d and another object.
|
|
|
|
|
*/
|
2022-11-27 21:01:01 -08:00
|
|
|
bool operator==(const Transform3d&) const = default;
|
2022-05-06 08:41:23 -07:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Translation3d m_translation;
|
|
|
|
|
Rotation3d m_rotation;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc
|
2023-10-19 21:41:47 -07:00
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct wpi::Struct<frc::Transform3d> {
|
|
|
|
|
static constexpr std::string_view kTypeString = "struct:Transform3d";
|
|
|
|
|
static constexpr size_t kSize = wpi::Struct<frc::Translation3d>::kSize +
|
|
|
|
|
wpi::Struct<frc::Rotation3d>::kSize;
|
|
|
|
|
static constexpr std::string_view kSchema =
|
|
|
|
|
"Translation3d translation;Rotation3d rotation";
|
|
|
|
|
static frc::Transform3d Unpack(std::span<const uint8_t, kSize> data) {
|
|
|
|
|
return {wpi::UnpackStruct<frc::Translation3d, 0>(data),
|
|
|
|
|
wpi::UnpackStruct<frc::Rotation3d, kRotationOff>(data)};
|
|
|
|
|
}
|
|
|
|
|
static void Pack(std::span<uint8_t, kSize> data,
|
|
|
|
|
const frc::Transform3d& value) {
|
|
|
|
|
wpi::PackStruct<0>(data, value.Translation());
|
|
|
|
|
wpi::PackStruct<kRotationOff>(data, value.Rotation());
|
|
|
|
|
}
|
|
|
|
|
static void ForEachNested(
|
|
|
|
|
std::invocable<std::string_view, std::string_view> auto fn) {
|
|
|
|
|
wpi::ForEachStructSchema<frc::Translation3d>(fn);
|
|
|
|
|
wpi::ForEachStructSchema<frc::Rotation3d>(fn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static constexpr size_t kRotationOff = wpi::Struct<frc::Translation3d>::kSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static_assert(wpi::HasNestedStruct<frc::Transform3d>);
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::Transform3d> {
|
|
|
|
|
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
|
|
|
|
static frc::Transform3d Unpack(const google::protobuf::Message& msg);
|
|
|
|
|
static void Pack(google::protobuf::Message* msg,
|
|
|
|
|
const frc::Transform3d& value);
|
|
|
|
|
};
|