bgc-c/basic-geometry/affine2.h

175 lines
6.1 KiB
C

#ifndef _BGC_AFFINE2_H_INCLUDED_
#define _BGC_AFFINE2_H_INCLUDED_
#include "vector2.h"
#include "matrixes.h"
#include "matrix2x2.h"
// ==================== Types ==================== //
typedef struct {
BgcMatrix2x2FP32 distortion;
BgcVector2FP32 shift;
} BgcAffine2FP32;
typedef struct {
BgcMatrix2x2FP64 distortion;
BgcVector2FP64 shift;
} BgcAffine2FP64;
// ==================== Reset ==================== //
inline void bgc_affine2_reset_fp32(BgcAffine2FP32 * affine)
{
bgc_matrix2x2_set_to_identity_fp32(&affine->distortion);
bgc_vector2_reset_fp32(&affine->shift);
}
inline void bgc_affine2_reset_fp64(BgcAffine2FP64 * affine)
{
bgc_matrix2x2_set_to_identity_fp64(&affine->distortion);
bgc_vector2_reset_fp64(&affine->shift);
}
// ==================== Make ===================== //
inline void bgc_affine2_make_fp32(const BgcMatrix2x2FP32 * distortion, const BgcVector2FP32 * shift, BgcAffine2FP32 * affine)
{
bgc_matrix2x2_copy_fp32(distortion, &affine->distortion);
bgc_vector2_copy_fp32(shift, &affine->shift);
}
inline void bgc_affine2_make_fp64(const BgcMatrix2x2FP64 * distortion, const BgcVector2FP64 * shift, BgcAffine2FP64 * affine)
{
bgc_matrix2x2_copy_fp64(distortion, &affine->distortion);
bgc_vector2_copy_fp64(shift, &affine->shift);
}
// ==================== Copy ===================== //
inline void bgc_affine2_copy_fp32(const BgcAffine2FP32 * source, BgcAffine2FP32 * destination)
{
bgc_matrix2x2_copy_fp32(&source->distortion, &destination->distortion);
bgc_vector2_copy_fp32(&source->shift, &destination->shift);
}
inline void bgc_affine2_copy_fp64(const BgcAffine2FP64 * source, BgcAffine2FP64 * destination)
{
bgc_matrix2x2_copy_fp64(&source->distortion, &destination->distortion);
bgc_vector2_copy_fp64(&source->shift, &destination->shift);
}
// =================== Convert =================== //
inline void bgc_affine2_convert_fp64_to_fp32(const BgcAffine2FP64 * source, BgcAffine2FP32 * destination)
{
bgc_matrix2x2_convert_fp64_to_fp32(&source->distortion, &destination->distortion);
bgc_vector2_convert_fp64_to_fp32(&source->shift, &destination->shift);
}
inline void bgc_affine2_convert_fp32_to_fp64(const BgcAffine2FP32 * source, BgcAffine2FP64 * destination)
{
bgc_matrix2x2_convert_fp32_to_fp64(&source->distortion, &destination->distortion);
bgc_vector2_convert_fp32_to_fp64(&source->shift, &destination->shift);
}
// =================== Invert ==================== //
inline int bgc_affine2_invert_fp32(BgcAffine2FP32 * affine)
{
if (!bgc_matrix2x2_invert_fp32(&affine->distortion, &affine->distortion)) {
return 0;
}
bgc_matrix2x2_get_right_product_fp32(&affine->distortion, &affine->shift, &affine->shift);
bgc_vector2_make_opposite_fp32(&affine->shift);
return 1;
}
inline int bgc_affine2_invert_fp64(BgcAffine2FP64 * affine)
{
if (!bgc_matrix2x2_invert_fp64(&affine->distortion, &affine->distortion)) {
return 0;
}
bgc_matrix2x2_get_right_product_fp64(&affine->distortion, &affine->shift, &affine->shift);
bgc_vector2_make_opposite_fp64(&affine->shift);
return 1;
}
// ================= Get Inverse ================= //
inline int bgc_affine2_get_inverse_fp32(const BgcAffine2FP32 * source, BgcAffine2FP32 * destination)
{
if (!bgc_matrix2x2_invert_fp32(&source->distortion, &destination->distortion)) {
return 0;
}
bgc_matrix2x2_get_right_product_fp32(&destination->distortion, &source->shift, &destination->shift);
bgc_vector2_make_opposite_fp32(&destination->shift);
return 1;
}
inline int bgc_affine2_get_inverse_fp64(const BgcAffine2FP64 * source, BgcAffine2FP64 * destination)
{
if (!bgc_matrix2x2_invert_fp64(&source->distortion, &destination->distortion)) {
return 0;
}
bgc_matrix2x2_get_right_product_fp64(&destination->distortion, &source->shift, &destination->shift);
bgc_vector2_make_opposite_fp64(&destination->shift);
return 1;
}
// =================== Combine =================== //
inline void bgc_affine2_combine_fp32(const BgcAffine2FP32 * first, const BgcAffine2FP32 * second, BgcAffine2FP32 * combination)
{
BgcVector2FP32 first_shift;
bgc_matrix2x2_get_right_product_fp32(&second->distortion, &first->shift, &first_shift);
bgc_matrix_product_2x2_at_2x2_fp32(&second->distortion, &first->distortion, &combination->distortion);
bgc_vector2_add_fp32(&first_shift, &second->shift, &combination->shift);
}
inline void bgc_affine2_combine_fp64(const BgcAffine2FP64 * first, const BgcAffine2FP64 * second, BgcAffine2FP64 * combination)
{
BgcVector2FP64 first_shift;
bgc_matrix2x2_get_right_product_fp64(&second->distortion, &first->shift, &first_shift);
bgc_matrix_product_2x2_at_2x2_fp64(&second->distortion, &first->distortion, &combination->distortion);
bgc_vector2_add_fp64(&first_shift, &second->shift, &combination->shift);
}
// =============== Transform Point =============== //
inline void bgc_affine2_transform_point_fp32(const BgcAffine2FP32 * affine, const BgcVector2FP32 * initial_point, BgcVector2FP32 * transformed_point)
{
BgcVector2FP32 distorted;
bgc_matrix2x2_get_right_product_fp32(&affine->distortion, initial_point, &distorted);
bgc_vector2_add_fp32(&affine->shift, &distorted, transformed_point);
}
inline void bgc_affine2_transform_point_fp64(const BgcAffine2FP64 * affine, const BgcVector2FP64 * initial_point, BgcVector2FP64 * transformed_point)
{
BgcVector2FP64 distorted;
bgc_matrix2x2_get_right_product_fp64(&affine->distortion, initial_point, &distorted);
bgc_vector2_add_fp64(&affine->shift, &distorted, transformed_point);
}
// ============== Transform Vector =============== //
inline void bgc_affine2_transform_vector_fp32(const BgcAffine2FP32 * affine, const BgcVector2FP32 * initial_vector, BgcVector2FP32 * transformed_vector)
{
bgc_matrix2x2_get_right_product_fp32(&affine->distortion, initial_vector, transformed_vector);
}
inline void bgc_affine2_transform_vector_fp64(const BgcAffine2FP64 * affine, const BgcVector2FP64 * initial_vector, BgcVector2FP64 * transformed_vector)
{
bgc_matrix2x2_get_right_product_fp64(&affine->distortion, initial_vector, transformed_vector);
}
#endif