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
|
|
|
|
|
|
|
2023-08-31 11:03:37 -07:00
|
|
|
|
#include <Eigen/Core>
|
2022-05-06 08:41:23 -07:00
|
|
|
|
#include <wpi/SymbolExports.h>
|
2023-09-22 16:01:27 -04:00
|
|
|
|
#include <wpi/json_fwd.h>
|
2022-11-04 12:56:22 -04:00
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
namespace frc {
|
|
|
|
|
|
|
2024-01-04 08:38:06 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* Represents a quaternion.
|
|
|
|
|
|
*/
|
2022-05-06 08:41:23 -07:00
|
|
|
|
class WPILIB_DLLEXPORT Quaternion {
|
|
|
|
|
|
public:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a quaternion with a default angle of 0 degrees.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion() = default;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a quaternion with the given components.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param w W component of the quaternion.
|
|
|
|
|
|
* @param x X component of the quaternion.
|
|
|
|
|
|
* @param y Y component of the quaternion.
|
|
|
|
|
|
* @param z Z component of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion(double w, double x, double y, double z);
|
|
|
|
|
|
|
2023-10-08 19:42:53 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Adds with another quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other the other quaternion
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion operator+(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Subtracts another quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other the other quaternion
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion operator-(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Multiples with a scalar value.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other the scalar value
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion operator*(const double other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Divides by a scalar value.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other the scalar value
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion operator/(const double other) const;
|
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Multiply with another quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The other quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion operator*(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Checks equality between this Quaternion and another object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The other object.
|
|
|
|
|
|
* @return Whether the two objects are equal.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool operator==(const Quaternion& other) const;
|
|
|
|
|
|
|
2023-10-08 19:42:53 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the elementwise product of two quaternions.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double Dot(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the conjugate of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Conjugate() const;
|
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the inverse of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Inverse() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Normalizes the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Normalize() const;
|
|
|
|
|
|
|
2023-10-08 19:42:53 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Calculates the L2 norm of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double Norm() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Calculates this quaternion raised to a power.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param t the power to raise this quaternion to.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Pow(const double t) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Matrix exponential of a quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other the "Twist" that will be applied to this quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Exp(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Matrix exponential of a quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* source: wpimath/algorithms.md
|
|
|
|
|
|
*
|
|
|
|
|
|
* If this quaternion is in 𝖘𝖔(3) and you are looking for an element of
|
|
|
|
|
|
* SO(3), use FromRotationVector
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Exp() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Log operator of a quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The quaternion to map this quaternion onto
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Log(const Quaternion& other) const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Log operator of a quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* source: wpimath/algorithms.md
|
|
|
|
|
|
*
|
|
|
|
|
|
* If this quaternion is in SO(3) and you are looking for an element of 𝖘𝖔(3),
|
|
|
|
|
|
* use ToRotationVector
|
|
|
|
|
|
*/
|
|
|
|
|
|
Quaternion Log() const;
|
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns W component of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double W() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns X component of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double X() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns Y component of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double Y() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns Z component of the quaternion.
|
|
|
|
|
|
*/
|
|
|
|
|
|
double Z() const;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the rotation vector representation of this quaternion.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This is also the log operator of SO(3).
|
|
|
|
|
|
*/
|
|
|
|
|
|
Eigen::Vector3d ToRotationVector() const;
|
|
|
|
|
|
|
2023-10-08 19:42:53 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the quaternion representation of this rotation vector.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This is also the exp operator of 𝖘𝖔(3).
|
|
|
|
|
|
*
|
|
|
|
|
|
* source: wpimath/algorithms.md
|
|
|
|
|
|
*/
|
|
|
|
|
|
static Quaternion FromRotationVector(const Eigen::Vector3d& rvec);
|
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
private:
|
2023-08-11 23:27:29 -07:00
|
|
|
|
// Scalar r in versor form
|
2022-05-06 08:41:23 -07:00
|
|
|
|
double m_r = 1.0;
|
2023-08-11 23:27:29 -07:00
|
|
|
|
|
|
|
|
|
|
// Vector v in versor form
|
2022-05-06 08:41:23 -07:00
|
|
|
|
Eigen::Vector3d m_v{0.0, 0.0, 0.0};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-04 12:56:22 -04:00
|
|
|
|
WPILIB_DLLEXPORT
|
|
|
|
|
|
void to_json(wpi::json& json, const Quaternion& quaternion);
|
|
|
|
|
|
|
|
|
|
|
|
WPILIB_DLLEXPORT
|
|
|
|
|
|
void from_json(const wpi::json& json, Quaternion& quaternion);
|
|
|
|
|
|
|
2022-05-06 08:41:23 -07:00
|
|
|
|
} // namespace frc
|
2023-10-19 21:41:47 -07:00
|
|
|
|
|
2024-09-12 23:44:19 -07:00
|
|
|
|
#ifndef NO_PROTOBUF
|
2023-11-21 13:11:57 -05:00
|
|
|
|
#include "frc/geometry/proto/QuaternionProto.h"
|
2024-09-12 23:44:19 -07:00
|
|
|
|
#endif
|
2023-11-21 13:11:57 -05:00
|
|
|
|
#include "frc/geometry/struct/QuaternionStruct.h"
|