2021-08-02 16:34:47 +00:00
/*************************************************************************/
/* Transform2D.hpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2017-03-03 20:04:23 +00:00
# ifndef TRANSFORM2D_H
# define TRANSFORM2D_H
2017-03-06 07:49:24 +00:00
# include "Vector2.hpp"
2017-03-03 20:04:23 +00:00
namespace godot {
typedef Vector2 Size2 ;
2018-02-23 11:39:35 +00:00
struct Rect2 ;
2017-03-03 20:04:23 +00:00
2017-07-23 15:53:50 +00:00
struct Transform2D {
2020-12-08 23:00:34 +00:00
static const Transform2D IDENTITY ;
static const Transform2D FLIP_X ;
static const Transform2D FLIP_Y ;
2017-03-03 20:04:23 +00:00
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
// M = (elements[0][0] elements[1][0])
// (elements[0][1] elements[1][1])
// This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i].
// Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
// This requires additional care when working with explicit indices.
// See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
// Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
// and angle is measure from +X to +Y in a clockwise-fashion.
Vector2 elements [ 3 ] ;
2018-11-23 22:09:41 +00:00
inline real_t tdotx ( const Vector2 & v ) const { return elements [ 0 ] [ 0 ] * v . x + elements [ 1 ] [ 0 ] * v . y ; }
inline real_t tdoty ( const Vector2 & v ) const { return elements [ 0 ] [ 1 ] * v . x + elements [ 1 ] [ 1 ] * v . y ; }
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
inline const Vector2 & operator [ ] ( int p_idx ) const { return elements [ p_idx ] ; }
inline Vector2 & operator [ ] ( int p_idx ) { return elements [ p_idx ] ; }
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
inline Vector2 get_axis ( int p_axis ) const {
ERR_FAIL_INDEX_V ( p_axis , 3 , Vector2 ( ) ) ;
return elements [ p_axis ] ;
}
inline void set_axis ( int p_axis , const Vector2 & p_vec ) {
ERR_FAIL_INDEX ( p_axis , 3 ) ;
elements [ p_axis ] = p_vec ;
}
2017-03-03 20:04:23 +00:00
void invert ( ) ;
Transform2D inverse ( ) const ;
void affine_invert ( ) ;
Transform2D affine_inverse ( ) const ;
void set_rotation ( real_t p_phi ) ;
real_t get_rotation ( ) const ;
2018-11-23 22:09:41 +00:00
void set_rotation_and_scale ( real_t p_phi , const Size2 & p_scale ) ;
2017-03-03 20:04:23 +00:00
void rotate ( real_t p_phi ) ;
2018-11-23 22:09:41 +00:00
void scale ( const Size2 & p_scale ) ;
void scale_basis ( const Size2 & p_scale ) ;
void translate ( real_t p_tx , real_t p_ty ) ;
void translate ( const Vector2 & p_translation ) ;
2017-03-03 20:04:23 +00:00
real_t basis_determinant ( ) const ;
Size2 get_scale ( ) const ;
2018-11-23 22:09:41 +00:00
inline const Vector2 & get_origin ( ) const { return elements [ 2 ] ; }
inline void set_origin ( const Vector2 & p_origin ) { elements [ 2 ] = p_origin ; }
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
Transform2D scaled ( const Size2 & p_scale ) const ;
Transform2D basis_scaled ( const Size2 & p_scale ) const ;
Transform2D translated ( const Vector2 & p_offset ) const ;
2017-03-03 20:04:23 +00:00
Transform2D rotated ( real_t p_phi ) const ;
Transform2D untranslated ( ) const ;
void orthonormalize ( ) ;
Transform2D orthonormalized ( ) const ;
2018-11-23 22:09:41 +00:00
bool operator = = ( const Transform2D & p_transform ) const ;
bool operator ! = ( const Transform2D & p_transform ) const ;
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
void operator * = ( const Transform2D & p_transform ) ;
Transform2D operator * ( const Transform2D & p_transform ) const ;
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
Transform2D interpolate_with ( const Transform2D & p_transform , real_t p_c ) const ;
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
Vector2 basis_xform ( const Vector2 & p_vec ) const ;
Vector2 basis_xform_inv ( const Vector2 & p_vec ) const ;
Vector2 xform ( const Vector2 & p_vec ) const ;
Vector2 xform_inv ( const Vector2 & p_vec ) const ;
Rect2 xform ( const Rect2 & p_vec ) const ;
Rect2 xform_inv ( const Rect2 & p_vec ) const ;
2017-03-03 20:04:23 +00:00
operator String ( ) const ;
2017-03-06 02:30:46 +00:00
Transform2D ( real_t xx , real_t xy , real_t yx , real_t yy , real_t ox , real_t oy ) ;
2017-03-03 20:04:23 +00:00
2018-11-23 22:09:41 +00:00
Transform2D ( real_t p_rot , const Vector2 & p_pos ) ;
inline Transform2D ( ) {
elements [ 0 ] [ 0 ] = 1.0 ;
elements [ 1 ] [ 1 ] = 1.0 ;
}
2017-03-03 20:04:23 +00:00
} ;
2018-11-23 22:09:41 +00:00
} // namespace godot
2017-03-03 20:04:23 +00:00
# endif // TRANSFORM2D_H