Implemented a bunch of core types. Image needs love

pull/7/head
Karroffel 2017-03-03 21:04:23 +01:00
parent 3b6da9ca76
commit 09332ee609
11 changed files with 3073 additions and 1 deletions

View File

@ -0,0 +1,23 @@
#ifndef CORETYPES_H
#define CORETYPES_H
#include "Defs.h"
#include "Basis.h"
#include "Color.h"
#include "Image.h"
#include "InputEvent.h"
#include "NodePath.h"
#include "Plane.h"
#include "Quat.h"
#include "Rect2.h"
#include "Rect3.h"
#include "RID.h"
#include "String.h"
#include "Transform.h"
#include "Transform2D.h"
#include "Vector2.h"
#include "Vector3.h"
#endif // CORETYPES_H

72
include/godot/core/Defs.h Normal file
View File

@ -0,0 +1,72 @@
#ifndef DEFS_H
#define DEFS_H
namespace godot {
enum Error {
OK,
FAILED, ///< Generic fail error
ERR_UNAVAILABLE, ///< What is requested is unsupported/unavailable
ERR_UNCONFIGURED, ///< The object being used hasnt been properly set up yet
ERR_UNAUTHORIZED, ///< Missing credentials for requested resource
ERR_PARAMETER_RANGE_ERROR, ///< Parameter given out of range (5)
ERR_OUT_OF_MEMORY, ///< Out of memory
ERR_FILE_NOT_FOUND,
ERR_FILE_BAD_DRIVE,
ERR_FILE_BAD_PATH,
ERR_FILE_NO_PERMISSION, // (10)
ERR_FILE_ALREADY_IN_USE,
ERR_FILE_CANT_OPEN,
ERR_FILE_CANT_WRITE,
ERR_FILE_CANT_READ,
ERR_FILE_UNRECOGNIZED, // (15)
ERR_FILE_CORRUPT,
ERR_FILE_MISSING_DEPENDENCIES,
ERR_FILE_EOF,
ERR_CANT_OPEN, ///< Can't open a resource/socket/file
ERR_CANT_CREATE, // (20)
ERR_QUERY_FAILED,
ERR_ALREADY_IN_USE,
ERR_LOCKED, ///< resource is locked
ERR_TIMEOUT,
ERR_CANT_CONNECT, // (25)
ERR_CANT_RESOLVE,
ERR_CONNECTION_ERROR,
ERR_CANT_AQUIRE_RESOURCE,
ERR_CANT_FORK,
ERR_INVALID_DATA, ///< Data passed is invalid (30)
ERR_INVALID_PARAMETER, ///< Parameter passed is invalid
ERR_ALREADY_EXISTS, ///< When adding, item already exists
ERR_DOES_NOT_EXIST, ///< When retrieving/erasing, it item does not exist
ERR_DATABASE_CANT_READ, ///< database is full
ERR_DATABASE_CANT_WRITE, ///< database is full (35)
ERR_COMPILATION_FAILED,
ERR_METHOD_NOT_FOUND,
ERR_LINK_FAILED,
ERR_SCRIPT_FAILED,
ERR_CYCLIC_LINK, // (40)
ERR_INVALID_DECLARATION,
ERR_DUPLICATE_SYMBOL,
ERR_PARSE_ERROR,
ERR_BUSY,
ERR_SKIP, // (45)
ERR_HELP, ///< user requested help!!
ERR_BUG, ///< a bug in the software certainly happened, due to a double check failing or unexpected behavior.
ERR_PRINTER_ON_FIRE, /// the parallel port printer is engulfed in flames
ERR_OMFG_THIS_IS_VERY_VERY_BAD, ///< shit happens, has never been used, though
ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above
};
}
// @Todo error handling stuff here plz
// @Todo as well as real_t
#ifndef ERR_PRINT
#define ERR_PRINT(msg)
#endif
#endif // DEFS_H

194
include/godot/core/Image.h Normal file
View File

@ -0,0 +1,194 @@
#ifndef IMAGE_H
#define IMAGE_H
#include "Defs.h"
#include "Vector2.h"
#include "Rect2.h"
#include "Color.h"
#include "String.h"
#include <godot/godot_image.h>
namespace godot {
class Image {
godot_image _godot_image;
public:
enum Format {
FORMAT_L8, //luminance
FORMAT_LA8, //luminance-alpha
FORMAT_R8,
FORMAT_RG8,
FORMAT_RGB8,
FORMAT_RGBA8,
FORMAT_RGB565, //16 bit
FORMAT_RGBA4444,
FORMAT_RGBA5551,
FORMAT_RF, //float
FORMAT_RGF,
FORMAT_RGBF,
FORMAT_RGBAF,
FORMAT_RH, //half float
FORMAT_RGH,
FORMAT_RGBH,
FORMAT_RGBAH,
FORMAT_DXT1, //s3tc bc1
FORMAT_DXT3, //bc2
FORMAT_DXT5, //bc3
FORMAT_ATI1, //bc4
FORMAT_ATI2, //bc5
FORMAT_BPTC_RGBA, //btpc bc6h
FORMAT_BPTC_RGBF, //float /
FORMAT_BPTC_RGBFU, //unsigned float
FORMAT_PVRTC2, //pvrtc
FORMAT_PVRTC2A,
FORMAT_PVRTC4,
FORMAT_PVRTC4A,
FORMAT_ETC, //etc1
FORMAT_ETC2_R11, //etc2
FORMAT_ETC2_R11S, //signed, NOT srgb.
FORMAT_ETC2_RG11,
FORMAT_ETC2_RG11S,
FORMAT_ETC2_RGB8,
FORMAT_ETC2_RGBA8,
FORMAT_ETC2_RGB8A1,
FORMAT_MAX
};
enum Interpolation {
INTERPOLATE_NEAREST,
INTERPOLATE_BILINEAR,
INTERPOLATE_CUBIC,
/* INTERPOLATE GAUSS */
};
enum CompressMode {
COMPRESS_16BIT,
COMPRESS_S3TC,
COMPRESS_PVRTC2,
COMPRESS_PVRTC4,
COMPRESS_ETC,
COMPRESS_ETC2
};
Image()
{
godot_image_new(&_godot_image);
}
Image(const int width, const int height, const bool mipmaps, const Format format)
{
godot_image_new_with_size_format(&_godot_image, width, height, mipmaps, (godot_image_format) format);
}
void blit_rect(const Image& src, const Rect2& src_rect, const Vector2& dest = Vector2(0, 0))
{
// @DLScript @Todo
}
void brush_transfer(const Image& src, const Image& brush, const Vector2& pos = Vector2(0, 0))
{
// @DLScript @Todo
}
Image brushed(const Image& src, const Image& brush, const Vector2& pos = Vector2(0, 0))
{
return *this; // @DLScript @Todo
}
Image compressed(const Format format)
{
return *this; // @DLScript @Todo
}
Image converted(const Format format)
{
return *this; // @DLScript @Todo
}
Image decompressed()
{
return *this; // @DLScript @Todo
}
bool empty() const
{
return true; // @DLScript @Todo
}
void fix_alpha_edges()
{
// @DLScript @Todo
}
/*
PoolByteArray get_data()
{
// @Todo
}
*/
Format get_format() const
{
return Format::FORMAT_RGBAH; // @DLScript @Todo
}
int get_height() const
{
return godot_image_get_height(&_godot_image);
}
Color get_pixel(const int x, const int y, const int mipmap_level = 0)
{
return Color(); // @DLScript @Todo
}
Image get_rect(const Rect2& area = Rect2())
{
return *this; // @DLScript @Todo
}
Rect2 get_used_rect() const
{
return Rect2(); // @DLScript @Todo
}
int get_width() const
{
return godot_image_get_width(&_godot_image);
}
Error load(const String& path)
{
return (Error) godot_image_load(&_godot_image, (godot_string *) &path);
}
void put_pixel(const int x, const int y, const Color& color, int mipmap_level = 0)
{
// @DLScript @Todo
}
Image resized(const int x, const int y, const Interpolation interpolation = INTERPOLATE_NEAREST)
{
return *this; // @DLScript @Todo
}
Error save_png(const String& path)
{
return (Error) godot_image_save_png(&_godot_image, (godot_string *) &path); // @Todo Error enum
}
~Image()
{
godot_image_destroy(&_godot_image);
}
};
}
#endif // IMAGE_H

View File

@ -0,0 +1,535 @@
#ifndef INPUTEVENT_H
#define INPUTEVENT_H
#include <cstdint>
#include "String.h"
namespace godot {
enum {
BUTTON_LEFT=1,
BUTTON_RIGHT=2,
BUTTON_MIDDLE=3,
BUTTON_WHEEL_UP=4,
BUTTON_WHEEL_DOWN=5,
BUTTON_WHEEL_LEFT=6,
BUTTON_WHEEL_RIGHT=7,
BUTTON_MASK_LEFT=(1<<(BUTTON_LEFT-1)),
BUTTON_MASK_RIGHT=(1<<(BUTTON_RIGHT-1)),
BUTTON_MASK_MIDDLE=(1<<(BUTTON_MIDDLE-1)),
};
enum {
JOY_BUTTON_0 = 0,
JOY_BUTTON_1 = 1,
JOY_BUTTON_2 = 2,
JOY_BUTTON_3 = 3,
JOY_BUTTON_4 = 4,
JOY_BUTTON_5 = 5,
JOY_BUTTON_6 = 6,
JOY_BUTTON_7 = 7,
JOY_BUTTON_8 = 8,
JOY_BUTTON_9 = 9,
JOY_BUTTON_10 = 10,
JOY_BUTTON_11 = 11,
JOY_BUTTON_12 = 12,
JOY_BUTTON_13 = 13,
JOY_BUTTON_14 = 14,
JOY_BUTTON_15 = 15,
JOY_BUTTON_MAX = 16,
JOY_L = JOY_BUTTON_4,
JOY_R = JOY_BUTTON_5,
JOY_L2 = JOY_BUTTON_6,
JOY_R2 = JOY_BUTTON_7,
JOY_L3 = JOY_BUTTON_8,
JOY_R3 = JOY_BUTTON_9,
JOY_SELECT = JOY_BUTTON_10,
JOY_START = JOY_BUTTON_11,
JOY_DPAD_UP = JOY_BUTTON_12,
JOY_DPAD_DOWN = JOY_BUTTON_13,
JOY_DPAD_LEFT = JOY_BUTTON_14,
JOY_DPAD_RIGHT = JOY_BUTTON_15,
// a little history about game controllers (who copied who)
JOY_SNES_B = JOY_BUTTON_0,
JOY_SNES_A = JOY_BUTTON_1,
JOY_SNES_Y = JOY_BUTTON_2,
JOY_SNES_X = JOY_BUTTON_3,
JOY_SONY_CIRCLE=JOY_SNES_A,
JOY_SONY_X=JOY_SNES_B,
JOY_SONY_SQUARE=JOY_SNES_Y,
JOY_SONY_TRIANGLE=JOY_SNES_X,
JOY_SEGA_B=JOY_SNES_A,
JOY_SEGA_A=JOY_SNES_B,
JOY_SEGA_X=JOY_SNES_Y,
JOY_SEGA_Y=JOY_SNES_X,
JOY_XBOX_B=JOY_SEGA_B,
JOY_XBOX_A=JOY_SEGA_A,
JOY_XBOX_X=JOY_SEGA_X,
JOY_XBOX_Y=JOY_SEGA_Y,
JOY_DS_A = JOY_SNES_A,
JOY_DS_B = JOY_SNES_B,
JOY_DS_X = JOY_SNES_X,
JOY_DS_Y = JOY_SNES_Y,
JOY_WII_C = JOY_BUTTON_5,
JOY_WII_Z = JOY_BUTTON_6,
JOY_WII_MINUS = JOY_BUTTON_9,
JOY_WII_PLUS = JOY_BUTTON_10,
// end of history
JOY_AXIS_0=0,
JOY_AXIS_1=1,
JOY_AXIS_2=2,
JOY_AXIS_3=3,
JOY_AXIS_4=4,
JOY_AXIS_5=5,
JOY_AXIS_6=6,
JOY_AXIS_7=7,
JOY_AXIS_MAX=8,
JOY_ANALOG_0_X = JOY_AXIS_0,
JOY_ANALOG_0_Y = JOY_AXIS_1,
JOY_ANALOG_1_X = JOY_AXIS_2,
JOY_ANALOG_1_Y = JOY_AXIS_3,
JOY_ANALOG_2_X = JOY_AXIS_4,
JOY_ANALOG_2_Y = JOY_AXIS_5,
JOY_ANALOG_L2 = JOY_AXIS_6,
JOY_ANALOG_R2 = JOY_AXIS_7,
};
/**
* Input Modifier Status
* for keyboard/mouse events.
*/
struct InputModifierState {
bool shift;
bool alt;
#ifdef APPLE_STYLE_KEYS
union {
bool command;
bool meta; //< windows/mac key
};
bool control;
#else
union {
bool command; //< windows/mac key
bool control;
};
bool meta; //< windows/mac key
#endif
bool operator==(const InputModifierState& rvalue) const {
return ( (shift==rvalue.shift) && (alt==rvalue.alt) && (control==rvalue.control) && (meta==rvalue.meta));
}
};
struct InputEventKey {
InputModifierState mod;
bool pressed; /// otherwise release
uint32_t scancode; ///< check keyboard.h , KeyCode enum, without modifier masks
uint32_t unicode; ///unicode
bool echo; /// true if this is an echo key
};
struct InputEventMouse {
InputModifierState mod;
int button_mask;
float x,y;
float global_x,global_y;
int pointer_index;
};
struct InputEventMouseButton : public InputEventMouse {
int button_index;
bool pressed; //otherwise released
bool doubleclick; //last even less than doubleclick time
};
struct InputEventMouseMotion : public InputEventMouse {
float relative_x,relative_y;
float speed_x,speed_y;
};
struct InputEventJoypadMotion {
int axis; ///< Joypad axis
float axis_value; ///< -1 to 1
};
struct InputEventJoypadButton {
int button_index;
bool pressed;
float pressure; //0 to 1
};
struct InputEventScreenTouch {
int index;
float x,y;
bool pressed;
};
struct InputEventScreenDrag {
int index;
float x,y;
float relative_x,relative_y;
float speed_x,speed_y;
};
struct InputEventAction {
int action;
bool pressed;
};
struct InputEvent {
enum Type {
NONE,
KEY,
MOUSE_MOTION,
MOUSE_BUTTON,
JOYPAD_MOTION,
JOYPAD_BUTTON,
SCREEN_TOUCH,
SCREEN_DRAG,
ACTION,
TYPE_MAX
};
uint32_t ID;
int type;
int device;
union {
InputEventMouseMotion mouse_motion;
InputEventMouseButton mouse_button;
InputEventJoypadMotion joy_motion;
InputEventJoypadButton joy_button;
InputEventKey key;
InputEventScreenTouch screen_touch;
InputEventScreenDrag screen_drag;
InputEventAction action;
};
bool is_pressed() const;
bool is_action(const String& p_action) const;
bool is_action_pressed(const String& p_action) const;
bool is_action_released(const String& p_action) const;
bool is_echo() const;
void set_as_action(const String& p_action, bool p_pressed);
InputEvent xform_by(const Transform2D& p_xform) const;
bool operator==(const InputEvent &p_event) const;
operator String() const;
InputEvent() { memset(this,0,sizeof(InputEvent)); }
};
bool InputEvent::operator==(const InputEvent &p_event) const {
if (type != p_event.type){
return false;
}
switch(type) {
/** Current clang-format style doesn't play well with the aligned return values of that switch. */
/* clang-format off */
case NONE:
return true;
case KEY:
return key.unicode == p_event.key.unicode
&& key.scancode == p_event.key.scancode
&& key.echo == p_event.key.echo
&& key.pressed == p_event.key.pressed
&& key.mod == p_event.key.mod;
case MOUSE_MOTION:
return mouse_motion.x == p_event.mouse_motion.x
&& mouse_motion.y == p_event.mouse_motion.y
&& mouse_motion.relative_x == p_event.mouse_motion.relative_x
&& mouse_motion.relative_y == p_event.mouse_motion.relative_y
&& mouse_motion.button_mask == p_event.mouse_motion.button_mask
&& key.mod == p_event.key.mod;
case MOUSE_BUTTON:
return mouse_button.pressed == p_event.mouse_button.pressed
&& mouse_button.x == p_event.mouse_button.x
&& mouse_button.y == p_event.mouse_button.y
&& mouse_button.button_index == p_event.mouse_button.button_index
&& mouse_button.button_mask == p_event.mouse_button.button_mask
&& key.mod == p_event.key.mod;
case JOYPAD_MOTION:
return joy_motion.axis == p_event.joy_motion.axis
&& joy_motion.axis_value == p_event.joy_motion.axis_value;
case JOYPAD_BUTTON:
return joy_button.pressed == p_event.joy_button.pressed
&& joy_button.button_index == p_event.joy_button.button_index
&& joy_button.pressure == p_event.joy_button.pressure;
case SCREEN_TOUCH:
return screen_touch.pressed == p_event.screen_touch.pressed
&& screen_touch.index == p_event.screen_touch.index
&& screen_touch.x == p_event.screen_touch.x
&& screen_touch.y == p_event.screen_touch.y;
case SCREEN_DRAG:
return screen_drag.index == p_event.screen_drag.index
&& screen_drag.x == p_event.screen_drag.x
&& screen_drag.y == p_event.screen_drag.y;
case ACTION:
return action.action == p_event.action.action
&& action.pressed == p_event.action.pressed;
/* clang-format on */
default:
ERR_PRINT("No logic to compare InputEvents of this type, this shouldn't happen.");
}
return false;
}
InputEvent::operator String() const {
/*
String str ="Device "+itos(device)+" ID "+itos(ID)+" ";
switch(type) {
case NONE: {
return "Event: None";
} break;
case KEY: {
str+= "Event: Key ";
str=str+"Unicode: "+String::chr(key.unicode)+" Scan: "+itos( key.scancode )+" Echo: "+String(key.echo?"True":"False")+" Pressed"+String(key.pressed?"True":"False")+" Mod: ";
if (key.mod.shift)
str+="S";
if (key.mod.control)
str+="C";
if (key.mod.alt)
str+="A";
if (key.mod.meta)
str+="M";
return str;
} break;
case MOUSE_MOTION: {
str+= "Event: Motion ";
str=str+" Pos: " +itos(mouse_motion.x)+","+itos(mouse_motion.y)+" Rel: "+itos(mouse_motion.relative_x)+","+itos(mouse_motion.relative_y)+" Mask: ";
for (int i=0;i<8;i++) {
if ((1<<i)&mouse_motion.button_mask)
str+=itos(i+1);
}
str+=" Mod: ";
if (key.mod.shift)
str+="S";
if (key.mod.control)
str+="C";
if (key.mod.alt)
str+="A";
if (key.mod.meta)
str+="M";
return str;
} break;
case MOUSE_BUTTON: {
str+= "Event: Button ";
str=str+"Pressed: "+itos(mouse_button.pressed)+" Pos: " +itos(mouse_button.x)+","+itos(mouse_button.y)+" Button: "+itos(mouse_button.button_index)+" Mask: ";
for (int i=0;i<8;i++) {
if ((1<<i)&mouse_button.button_mask)
str+=itos(i+1);
}
str+=" Mod: ";
if (key.mod.shift)
str+="S";
if (key.mod.control)
str+="C";
if (key.mod.alt)
str+="A";
if (key.mod.meta)
str+="M";
str+=String(" DoubleClick: ")+(mouse_button.doubleclick?"Yes":"No");
return str;
} break;
case JOYPAD_MOTION: {
str+= "Event: JoypadMotion ";
str=str+"Axis: "+itos(joy_motion.axis)+" Value: " +rtos(joy_motion.axis_value);
return str;
} break;
case JOYPAD_BUTTON: {
str+= "Event: JoypadButton ";
str=str+"Pressed: "+itos(joy_button.pressed)+" Index: " +itos(joy_button.button_index)+" pressure "+rtos(joy_button.pressure);
return str;
} break;
case SCREEN_TOUCH: {
str+= "Event: ScreenTouch ";
str=str+"Pressed: "+itos(screen_touch.pressed)+" Index: " +itos(screen_touch.index)+" pos "+rtos(screen_touch.x)+","+rtos(screen_touch.y);
return str;
} break;
case SCREEN_DRAG: {
str+= "Event: ScreenDrag ";
str=str+" Index: " +itos(screen_drag.index)+" pos "+rtos(screen_drag.x)+","+rtos(screen_drag.y);
return str;
} break;
case ACTION: {
str+= "Event: Action: "+InputMap::get_singleton()->get_action_from_id(action.action)+" Pressed: "+itos(action.pressed);
return str;
} break;
}
*/
return "";
}
void InputEvent::set_as_action(const String& p_action, bool p_pressed) {
godot_input_event_set_as_action((godot_input_event *) this, (godot_string*) &p_action, p_pressed);
}
bool InputEvent::is_pressed() const {
switch(type) {
case KEY: return key.pressed;
case MOUSE_BUTTON: return mouse_button.pressed;
case JOYPAD_BUTTON: return joy_button.pressed;
case SCREEN_TOUCH: return screen_touch.pressed;
case JOYPAD_MOTION: return ::fabs(joy_motion.axis_value) > 0.5;
case ACTION: return action.pressed;
default: {}
}
return false;
}
bool InputEvent::is_echo() const {
return (type==KEY && key.echo);
}
bool InputEvent::is_action(const String& p_action) const {
return godot_input_event_is_action((godot_input_event *) this, (godot_string *) &p_action);
}
bool InputEvent::is_action_pressed(const String& p_action) const {
return is_action(p_action) && is_pressed() && !is_echo();
}
bool InputEvent::is_action_released(const String& p_action) const {
return is_action(p_action) && !is_pressed();
}
InputEvent InputEvent::xform_by(const Transform2D& p_xform) const {
InputEvent ev=*this;
switch(ev.type) {
case InputEvent::MOUSE_BUTTON: {
Vector2 g = p_xform.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
Vector2 l = p_xform.xform(Vector2(ev.mouse_button.x,ev.mouse_button.y));
ev.mouse_button.x=l.x;
ev.mouse_button.y=l.y;
ev.mouse_button.global_x=g.x;
ev.mouse_button.global_y=g.y;
} break;
case InputEvent::MOUSE_MOTION: {
Vector2 g = p_xform.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
Vector2 l = p_xform.xform(Vector2(ev.mouse_motion.x,ev.mouse_motion.y));
Vector2 r = p_xform.basis_xform(Vector2(ev.mouse_motion.relative_x,ev.mouse_motion.relative_y));
Vector2 s = p_xform.basis_xform(Vector2(ev.mouse_motion.speed_x,ev.mouse_motion.speed_y));
ev.mouse_motion.x=l.x;
ev.mouse_motion.y=l.y;
ev.mouse_motion.global_x=g.x;
ev.mouse_motion.global_y=g.y;
ev.mouse_motion.relative_x=r.x;
ev.mouse_motion.relative_y=r.y;
ev.mouse_motion.speed_x=s.x;
ev.mouse_motion.speed_y=s.y;
} break;
case InputEvent::SCREEN_TOUCH: {
Vector2 t = p_xform.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y));
ev.screen_touch.x=t.x;
ev.screen_touch.y=t.y;
} break;
case InputEvent::SCREEN_DRAG: {
Vector2 t = p_xform.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y));
Vector2 r = p_xform.basis_xform(Vector2(ev.screen_drag.relative_x,ev.screen_drag.relative_y));
Vector2 s = p_xform.basis_xform(Vector2(ev.screen_drag.speed_x,ev.screen_drag.speed_y));
ev.screen_drag.x=t.x;
ev.screen_drag.y=t.y;
ev.screen_drag.relative_x=r.x;
ev.screen_drag.relative_y=r.y;
ev.screen_drag.speed_x=s.x;
ev.screen_drag.speed_y=s.y;
} break;
}
return ev;
}
}
#endif // INPUTEVENT_H

273
include/godot/core/Plane.h Normal file
View File

@ -0,0 +1,273 @@
#ifndef PLANE_H
#define PLANE_H
#include "Vector3.h"
#include <cmath>
typedef float real_t; // @Todo move this to a global Godot.h
#define CMP_EPSILON 0.00001
namespace godot {
#define _PLANE_EQ_DOT_EPSILON 0.999
#define _PLANE_EQ_D_EPSILON 0.0001
enum ClockDirection {
CLOCKWISE,
COUNTERCLOCKWISE
};
class Plane {
public:
Vector3 normal;
real_t d;
void set_normal(const Vector3& p_normal)
{
this->normal = p_normal;
}
Vector3 get_normal() const { return normal; } ///Point is coplanar, CMP_EPSILON for precision
void normalize(); // down below
Plane normalized() const; // down below
/* Plane-Point operations */
Vector3 center() const { return normal*d; }
Vector3 get_any_point() const;
Vector3 get_any_perpendicular_normal() const;
bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
real_t distance_to(const Vector3 &p_point) const;
bool has_point(const Vector3 &p_point,real_t _epsilon=CMP_EPSILON) const;
/* intersections */
bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result=0) const;
bool intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3* p_intersection) const;
bool intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_intersection) const;
Vector3 project(const Vector3& p_point) const {
return p_point - normal * distance_to(p_point);
}
/* misc */
Plane operator-() const { return Plane(-normal,-d); }
bool is_almost_like(const Plane& p_plane) const;
bool operator==(const Plane& p_plane) const;
bool operator!=(const Plane& p_plane) const;
operator String() const;
Plane() { d=0; }
Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) : normal(p_a,p_b,p_c), d(p_d) { }
Plane(const Vector3 &p_normal, real_t p_d);
Plane(const Vector3 &p_point, const Vector3& p_normal);
Plane(const Vector3 &p_point1, const Vector3 &p_point2,const Vector3 &p_point3,ClockDirection p_dir = CLOCKWISE);
};
void Plane::normalize() {
real_t l = normal.length();
if (l==0) {
*this=Plane(0,0,0,0);
return;
}
normal/=l;
d/=l;
}
Plane Plane::normalized() const {
Plane p = *this;
p.normalize();
return p;
}
Vector3 Plane::get_any_point() const {
return get_normal()*d;
}
Vector3 Plane::get_any_perpendicular_normal() const {
static const Vector3 p1 = Vector3(1,0,0);
static const Vector3 p2 = Vector3(0,1,0);
Vector3 p;
if (::fabs(normal.dot(p1)) > 0.99) // if too similar to p1
p=p2; // use p2
else
p=p1; // use p1
p-=normal * normal.dot(p);
p.normalize();
return p;
}
/* intersections */
bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
const Plane &p_plane0=*this;
Vector3 normal0=p_plane0.normal;
Vector3 normal1=p_plane1.normal;
Vector3 normal2=p_plane2.normal;
real_t denom=vec3_cross(normal0,normal1).dot(normal2);
if (::fabs(denom)<=CMP_EPSILON)
return false;
if (r_result) {
*r_result = ( (vec3_cross(normal1, normal2) * p_plane0.d) +
(vec3_cross(normal2, normal0) * p_plane1.d) +
(vec3_cross(normal0, normal1) * p_plane2.d) )/denom;
}
return true;
}
bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3* p_intersection) const {
Vector3 segment=p_dir;
real_t den=normal.dot( segment );
//printf("den is %i\n",den);
if (::fabs(den)<=CMP_EPSILON) {
return false;
}
real_t dist=(normal.dot( p_from ) - d) / den;
//printf("dist is %i\n",dist);
if (dist>CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
return false;
}
dist=-dist;
*p_intersection = p_from + segment * dist;
return true;
}
bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3* p_intersection) const {
Vector3 segment= p_begin - p_end;
real_t den=normal.dot( segment );
//printf("den is %i\n",den);
if (::fabs(den)<=CMP_EPSILON) {
return false;
}
real_t dist=(normal.dot( p_begin ) - d) / den;
//printf("dist is %i\n",dist);
if (dist<-CMP_EPSILON || dist > (1.0 +CMP_EPSILON)) {
return false;
}
dist=-dist;
*p_intersection = p_begin + segment * dist;
return true;
}
/* misc */
bool Plane::is_almost_like(const Plane& p_plane) const {
return (normal.dot( p_plane.normal ) > _PLANE_EQ_DOT_EPSILON && ::fabs(d-p_plane.d) < _PLANE_EQ_D_EPSILON);
}
Plane::operator String() const {
// return normal.operator String() + ", " + rtos(d);
return String(); // @Todo
}
bool Plane::is_point_over(const Vector3 &p_point) const {
return (normal.dot(p_point) > d);
}
real_t Plane::distance_to(const Vector3 &p_point) const {
return (normal.dot(p_point)-d);
}
bool Plane::has_point(const Vector3 &p_point,real_t _epsilon) const {
real_t dist=normal.dot(p_point) - d;
dist=::fabs(dist);
return ( dist <= _epsilon);
}
Plane::Plane(const Vector3 &p_normal, real_t p_d) {
normal=p_normal;
d=p_d;
}
Plane::Plane(const Vector3 &p_point, const Vector3& p_normal) {
normal=p_normal;
d=p_normal.dot(p_point);
}
Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3,ClockDirection p_dir) {
if (p_dir == CLOCKWISE)
normal=(p_point1-p_point3).cross(p_point1-p_point2);
else
normal=(p_point1-p_point2).cross(p_point1-p_point3);
normal.normalize();
d = normal.dot(p_point1);
}
bool Plane::operator==(const Plane& p_plane) const {
return normal==p_plane.normal && d == p_plane.d;
}
bool Plane::operator!=(const Plane& p_plane) const {
return normal!=p_plane.normal || d != p_plane.d;
}
}
#endif // PLANE_H

408
include/godot/core/Rect2.h Normal file
View File

@ -0,0 +1,408 @@
#ifndef RECT2_H
#define RECT2_H
#include "Vector2.h"
#include <cmath>
namespace godot {
typedef Vector2 Size2;
typedef Vector2 Point2;
class Transform2D;
#ifndef MAX
#define MAX(a, b) (a > b ? a : b)
#endif
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
#endif
struct Rect2 {
Point2 pos;
Size2 size;
const Vector2& get_pos() const { return pos; }
void set_pos(const Vector2& p_pos) { pos=p_pos; }
const Vector2& get_size() const { return size; }
void set_size(const Vector2& p_size) { size=p_size; }
real_t get_area() const { return size.width*size.height; }
inline bool intersects(const Rect2& p_rect) const {
if ( pos.x >= (p_rect.pos.x + p_rect.size.width) )
return false;
if ( (pos.x+size.width) <= p_rect.pos.x )
return false;
if ( pos.y >= (p_rect.pos.y + p_rect.size.height) )
return false;
if ( (pos.y+size.height) <= p_rect.pos.y )
return false;
return true;
}
inline real_t distance_to(const Vector2& p_point) const {
real_t dist = 1e20;
if (p_point.x < pos.x) {
dist=MIN(dist,pos.x-p_point.x);
}
if (p_point.y < pos.y) {
dist=MIN(dist,pos.y-p_point.y);
}
if (p_point.x >= (pos.x+size.x) ) {
dist=MIN(p_point.x-(pos.x+size.x),dist);
}
if (p_point.y >= (pos.y+size.y) ) {
dist=MIN(p_point.y-(pos.y+size.y),dist);
}
if (dist==1e20)
return 0;
else
return dist;
}
bool intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const;
bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const;
inline bool encloses(const Rect2& p_rect) const {
return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y));
}
inline bool has_no_area() const {
return (size.x<=0 || size.y<=0);
}
inline Rect2 clip(const Rect2& p_rect) const { /// return a clipped rect
Rect2 new_rect=p_rect;
if (!intersects( new_rect ))
return Rect2();
new_rect.pos.x = MAX( p_rect.pos.x , pos.x );
new_rect.pos.y = MAX( p_rect.pos.y , pos.y );
Point2 p_rect_end=p_rect.pos+p_rect.size;
Point2 end=pos+size;
new_rect.size.x=MIN(p_rect_end.x,end.x) - new_rect.pos.x;
new_rect.size.y=MIN(p_rect_end.y,end.y) - new_rect.pos.y;
return new_rect;
}
inline Rect2 merge(const Rect2& p_rect) const { ///< return a merged rect
Rect2 new_rect;
new_rect.pos.x=MIN( p_rect.pos.x , pos.x );
new_rect.pos.y=MIN( p_rect.pos.y , pos.y );
new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x );
new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y );
new_rect.size = new_rect.size - new_rect.pos; //make relative again
return new_rect;
}
inline bool has_point(const Point2& p_point) const {
if (p_point.x < pos.x)
return false;
if (p_point.y < pos.y)
return false;
if (p_point.x >= (pos.x+size.x) )
return false;
if (p_point.y >= (pos.y+size.y) )
return false;
return true;
}
inline bool no_area() const { return (size.width<=0 || size.height<=0 ); }
bool operator==(const Rect2& p_rect) const { return pos==p_rect.pos && size==p_rect.size; }
bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
inline Rect2 grow(real_t p_by) const {
Rect2 g=*this;
g.pos.x-=p_by;
g.pos.y-=p_by;
g.size.width+=p_by*2;
g.size.height+=p_by*2;
return g;
}
inline Rect2 expand(const Vector2& p_vector) const {
Rect2 r = *this;
r.expand_to(p_vector);
return r;
}
inline void expand_to(const Vector2& p_vector) { //in place function for speed
Vector2 begin=pos;
Vector2 end=pos+size;
if (p_vector.x<begin.x)
begin.x=p_vector.x;
if (p_vector.y<begin.y)
begin.y=p_vector.y;
if (p_vector.x>end.x)
end.x=p_vector.x;
if (p_vector.y>end.y)
end.y=p_vector.y;
pos=begin;
size=end-begin;
}
operator String() const { return String(pos)+", "+String(size); }
Rect2() {}
Rect2( real_t p_x, real_t p_y, real_t p_width, real_t p_height) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
};
bool Rect2::intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos,Point2* r_normal) const {
real_t min=0,max=1;
int axis=0;
real_t sign=0;
for(int i=0;i<2;i++) {
real_t seg_from=p_from[i];
real_t seg_to=p_to[i];
real_t box_begin=pos[i];
real_t box_end=box_begin+size[i];
real_t cmin,cmax;
real_t csign;
if (seg_from < seg_to) {
if (seg_from > box_end || seg_to < box_begin)
return false;
real_t length=seg_to-seg_from;
cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
csign=-1.0;
} else {
if (seg_to > box_end || seg_from < box_begin)
return false;
real_t length=seg_to-seg_from;
cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
csign=1.0;
}
if (cmin > min) {
min = cmin;
axis=i;
sign=csign;
}
if (cmax < max)
max = cmax;
if (max < min)
return false;
}
Vector2 rel=p_to-p_from;
if (r_normal) {
Vector2 normal;
normal[axis]=sign;
*r_normal=normal;
}
if (r_pos)
*r_pos=p_from+rel*min;
return true;
}
}
#include "Transform2D.h"
namespace godot {
bool Rect2::intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const {
//SAT intersection between local and transformed rect2
Vector2 xf_points[4]={
p_xform.xform(p_rect.pos),
p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y)),
p_xform.xform(Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y)),
p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y)),
};
real_t low_limit;
//base rect2 first (faster)
if (xf_points[0].y>pos.y)
goto next1;
if (xf_points[1].y>pos.y)
goto next1;
if (xf_points[2].y>pos.y)
goto next1;
if (xf_points[3].y>pos.y)
goto next1;
return false;
next1:
low_limit=pos.y+size.y;
if (xf_points[0].y<low_limit)
goto next2;
if (xf_points[1].y<low_limit)
goto next2;
if (xf_points[2].y<low_limit)
goto next2;
if (xf_points[3].y<low_limit)
goto next2;
return false;
next2:
if (xf_points[0].x>pos.x)
goto next3;
if (xf_points[1].x>pos.x)
goto next3;
if (xf_points[2].x>pos.x)
goto next3;
if (xf_points[3].x>pos.x)
goto next3;
return false;
next3:
low_limit=pos.x+size.x;
if (xf_points[0].x<low_limit)
goto next4;
if (xf_points[1].x<low_limit)
goto next4;
if (xf_points[2].x<low_limit)
goto next4;
if (xf_points[3].x<low_limit)
goto next4;
return false;
next4:
Vector2 xf_points2[4]={
pos,
Vector2(pos.x+size.x,pos.y),
Vector2(pos.x,pos.y+size.y),
Vector2(pos.x+size.x,pos.y+size.y),
};
real_t maxa=p_xform.elements[0].dot(xf_points2[0]);
real_t mina=maxa;
real_t dp = p_xform.elements[0].dot(xf_points2[1]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
dp = p_xform.elements[0].dot(xf_points2[2]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
dp = p_xform.elements[0].dot(xf_points2[3]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
real_t maxb=p_xform.elements[0].dot(xf_points[0]);
real_t minb=maxb;
dp = p_xform.elements[0].dot(xf_points[1]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
dp = p_xform.elements[0].dot(xf_points[2]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
dp = p_xform.elements[0].dot(xf_points[3]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
if ( mina > maxb )
return false;
if ( minb > maxa )
return false;
maxa=p_xform.elements[1].dot(xf_points2[0]);
mina=maxa;
dp = p_xform.elements[1].dot(xf_points2[1]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
dp = p_xform.elements[1].dot(xf_points2[2]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
dp = p_xform.elements[1].dot(xf_points2[3]);
maxa=MAX(dp,maxa);
mina=MIN(dp,mina);
maxb=p_xform.elements[1].dot(xf_points[0]);
minb=maxb;
dp = p_xform.elements[1].dot(xf_points[1]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
dp = p_xform.elements[1].dot(xf_points[2]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
dp = p_xform.elements[1].dot(xf_points[3]);
maxb=MAX(dp,maxb);
minb=MIN(dp,minb);
if ( mina > maxb )
return false;
if ( minb > maxa )
return false;
return true;
}
}
#endif // RECT2_H

727
include/godot/core/Rect3.h Normal file
View File

@ -0,0 +1,727 @@
#ifndef RECT3_H
#define RECT3_H
#include "Vector3.h"
#include "Plane.h"
namespace godot {
// @Todo
// error handling...
#ifndef ERR_FAIL_V
#define ERR_FAIL_V(a) return a
#endif
#ifndef ERR_FAIL_INDEX
#define ERR_FAIL_INDEX(a, b)
#endif
class Rect3 {
public:
Vector3 pos;
Vector3 size;
real_t get_area() const; /// get area
bool has_no_area() const {
return (size.x<=CMP_EPSILON || size.y<=CMP_EPSILON || size.z<=CMP_EPSILON);
}
bool has_no_surface() const {
return (size.x<=CMP_EPSILON && size.y<=CMP_EPSILON && size.z<=CMP_EPSILON);
}
const Vector3& get_pos() const { return pos; }
void set_pos(const Vector3& p_pos) { pos=p_pos; }
const Vector3& get_size() const { return size; }
void set_size(const Vector3& p_size) { size=p_size; }
bool operator==(const Rect3& p_rval) const;
bool operator!=(const Rect3& p_rval) const;
bool intersects(const Rect3& p_aabb) const; /// Both AABBs overlap
bool intersects_inclusive(const Rect3& p_aabb) const; /// Both AABBs (or their faces) overlap
bool encloses(const Rect3 & p_aabb) const; /// p_aabb is completely inside this
Rect3 merge(const Rect3& p_with) const;
void merge_with(const Rect3& p_aabb); ///merge with another AABB
Rect3 intersection(const Rect3& p_aabb) const; ///get box where two intersect, empty if no intersection occurs
bool intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
bool intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip=NULL,Vector3* r_normal=NULL) const;
bool smits_intersect_ray(const Vector3 &from,const Vector3& p_dir, real_t t0, real_t t1) const;
bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
bool intersects_plane(const Plane &p_plane) const;
bool has_point(const Vector3& p_point) const;
Vector3 get_support(const Vector3& p_normal) const;
Vector3 get_longest_axis() const;
int get_longest_axis_index() const;
real_t get_longest_axis_size() const;
Vector3 get_shortest_axis() const;
int get_shortest_axis_index() const;
real_t get_shortest_axis_size() const;
Rect3 grow(real_t p_by) const;
void grow_by(real_t p_amount);
void get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const;
Vector3 get_endpoint(int p_point) const;
Rect3 expand(const Vector3& p_vector) const;
void project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const;
void expand_to(const Vector3& p_vector); /** expand to contain a point if necesary */
operator String() const;
Rect3() {}
inline Rect3(const Vector3 &p_pos,const Vector3& p_size) { pos=p_pos; size=p_size; }
};
inline bool Rect3::intersects(const Rect3& p_aabb) const {
if ( pos.x >= (p_aabb.pos.x + p_aabb.size.x) )
return false;
if ( (pos.x+size.x) <= p_aabb.pos.x )
return false;
if ( pos.y >= (p_aabb.pos.y + p_aabb.size.y) )
return false;
if ( (pos.y+size.y) <= p_aabb.pos.y )
return false;
if ( pos.z >= (p_aabb.pos.z + p_aabb.size.z) )
return false;
if ( (pos.z+size.z) <= p_aabb.pos.z )
return false;
return true;
}
inline bool Rect3::intersects_inclusive(const Rect3& p_aabb) const {
if ( pos.x > (p_aabb.pos.x + p_aabb.size.x) )
return false;
if ( (pos.x+size.x) < p_aabb.pos.x )
return false;
if ( pos.y > (p_aabb.pos.y + p_aabb.size.y) )
return false;
if ( (pos.y+size.y) < p_aabb.pos.y )
return false;
if ( pos.z > (p_aabb.pos.z + p_aabb.size.z) )
return false;
if ( (pos.z+size.z) < p_aabb.pos.z )
return false;
return true;
}
inline bool Rect3::encloses(const Rect3 & p_aabb) const {
Vector3 src_min=pos;
Vector3 src_max=pos+size;
Vector3 dst_min=p_aabb.pos;
Vector3 dst_max=p_aabb.pos+p_aabb.size;
return (
(src_min.x <= dst_min.x) &&
(src_max.x > dst_max.x) &&
(src_min.y <= dst_min.y) &&
(src_max.y > dst_max.y) &&
(src_min.z <= dst_min.z) &&
(src_max.z > dst_max.z) );
}
Vector3 Rect3::get_support(const Vector3& p_normal) const {
Vector3 half_extents = size * 0.5;
Vector3 ofs = pos + half_extents;
return Vector3(
(p_normal.x>0) ? -half_extents.x : half_extents.x,
(p_normal.y>0) ? -half_extents.y : half_extents.y,
(p_normal.z>0) ? -half_extents.z : half_extents.z
)+ofs;
}
Vector3 Rect3::get_endpoint(int p_point) const {
switch(p_point) {
case 0: return Vector3( pos.x , pos.y , pos.z );
case 1: return Vector3( pos.x , pos.y , pos.z+size.z );
case 2: return Vector3( pos.x , pos.y+size.y , pos.z );
case 3: return Vector3( pos.x , pos.y+size.y , pos.z+size.z );
case 4: return Vector3( pos.x+size.x , pos.y , pos.z );
case 5: return Vector3( pos.x+size.x , pos.y , pos.z+size.z );
case 6: return Vector3( pos.x+size.x , pos.y+size.y , pos.z );
case 7: return Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
};
ERR_FAIL_V(Vector3());
}
bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
Vector3 half_extents = size * 0.5;
Vector3 ofs = pos + half_extents;
for(int i=0;i<p_plane_count;i++) {
const Plane &p=p_planes[i];
Vector3 point(
(p.normal.x>0) ? -half_extents.x : half_extents.x,
(p.normal.y>0) ? -half_extents.y : half_extents.y,
(p.normal.z>0) ? -half_extents.z : half_extents.z
);
point+=ofs;
if (p.is_point_over(point))
return false;
}
return true;
}
bool Rect3::has_point(const Vector3& p_point) const {
if (p_point.x<pos.x)
return false;
if (p_point.y<pos.y)
return false;
if (p_point.z<pos.z)
return false;
if (p_point.x>pos.x+size.x)
return false;
if (p_point.y>pos.y+size.y)
return false;
if (p_point.z>pos.z+size.z)
return false;
return true;
}
inline void Rect3::expand_to(const Vector3& p_vector) {
Vector3 begin=pos;
Vector3 end=pos+size;
if (p_vector.x<begin.x)
begin.x=p_vector.x;
if (p_vector.y<begin.y)
begin.y=p_vector.y;
if (p_vector.z<begin.z)
begin.z=p_vector.z;
if (p_vector.x>end.x)
end.x=p_vector.x;
if (p_vector.y>end.y)
end.y=p_vector.y;
if (p_vector.z>end.z)
end.z=p_vector.z;
pos=begin;
size=end-begin;
}
void Rect3::project_range_in_plane(const Plane& p_plane,real_t &r_min,real_t& r_max) const {
Vector3 half_extents( size.x * 0.5, size.y * 0.5, size.z * 0.5 );
Vector3 center( pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z );
real_t length = p_plane.normal.abs().dot(half_extents);
real_t distance = p_plane.distance_to( center );
r_min = distance - length;
r_max = distance + length;
}
inline real_t Rect3::get_longest_axis_size() const {
real_t max_size=size.x;
if (size.y > max_size ) {
max_size=size.y;
}
if (size.z > max_size ) {
max_size=size.z;
}
return max_size;
}
inline real_t Rect3::get_shortest_axis_size() const {
real_t max_size=size.x;
if (size.y < max_size ) {
max_size=size.y;
}
if (size.z < max_size ) {
max_size=size.z;
}
return max_size;
}
bool Rect3::smits_intersect_ray(const Vector3 &from,const Vector3& dir, real_t t0, real_t t1) const {
real_t divx=1.0/dir.x;
real_t divy=1.0/dir.y;
real_t divz=1.0/dir.z;
Vector3 upbound=pos+size;
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
if (dir.x >= 0) {
tmin = (pos.x - from.x) * divx;
tmax = (upbound.x - from.x) * divx;
}
else {
tmin = (upbound.x - from.x) * divx;
tmax = (pos.x - from.x) * divx;
}
if (dir.y >= 0) {
tymin = (pos.y - from.y) * divy;
tymax = (upbound.y - from.y) * divy;
}
else {
tymin = (upbound.y - from.y) * divy;
tymax = (pos.y - from.y) * divy;
}
if ( (tmin > tymax) || (tymin > tmax) )
return false;
if (tymin > tmin)
tmin = tymin;
if (tymax < tmax)
tmax = tymax;
if (dir.z >= 0) {
tzmin = (pos.z - from.z) * divz;
tzmax = (upbound.z - from.z) * divz;
}
else {
tzmin = (upbound.z - from.z) * divz;
tzmax = (pos.z - from.z) * divz;
}
if ( (tmin > tzmax) || (tzmin > tmax) )
return false;
if (tzmin > tmin)
tmin = tzmin;
if (tzmax < tmax)
tmax = tzmax;
return ( (tmin < t1) && (tmax > t0) );
}
void Rect3::grow_by(real_t p_amount) {
pos.x-=p_amount;
pos.y-=p_amount;
pos.z-=p_amount;
size.x+=2.0*p_amount;
size.y+=2.0*p_amount;
size.z+=2.0*p_amount;
}
}
namespace godot {
real_t Rect3::get_area() const {
return size.x*size.y*size.z;
}
bool Rect3::operator==(const Rect3& p_rval) const {
return ((pos==p_rval.pos) && (size==p_rval.size));
}
bool Rect3::operator!=(const Rect3& p_rval) const {
return ((pos!=p_rval.pos) || (size!=p_rval.size));
}
void Rect3::merge_with(const Rect3& p_aabb) {
Vector3 beg_1,beg_2;
Vector3 end_1,end_2;
Vector3 min,max;
beg_1=pos;
beg_2=p_aabb.pos;
end_1=Vector3(size.x,size.y,size.z)+beg_1;
end_2=Vector3(p_aabb.size.x,p_aabb.size.y,p_aabb.size.z)+beg_2;
min.x=(beg_1.x<beg_2.x)?beg_1.x:beg_2.x;
min.y=(beg_1.y<beg_2.y)?beg_1.y:beg_2.y;
min.z=(beg_1.z<beg_2.z)?beg_1.z:beg_2.z;
max.x=(end_1.x>end_2.x)?end_1.x:end_2.x;
max.y=(end_1.y>end_2.y)?end_1.y:end_2.y;
max.z=(end_1.z>end_2.z)?end_1.z:end_2.z;
pos=min;
size=max-min;
}
Rect3 Rect3::intersection(const Rect3& p_aabb) const {
Vector3 src_min=pos;
Vector3 src_max=pos+size;
Vector3 dst_min=p_aabb.pos;
Vector3 dst_max=p_aabb.pos+p_aabb.size;
Vector3 min,max;
if (src_min.x > dst_max.x || src_max.x < dst_min.x )
return Rect3();
else {
min.x= ( src_min.x > dst_min.x ) ? src_min.x :dst_min.x;
max.x= ( src_max.x < dst_max.x ) ? src_max.x :dst_max.x;
}
if (src_min.y > dst_max.y || src_max.y < dst_min.y )
return Rect3();
else {
min.y= ( src_min.y > dst_min.y ) ? src_min.y :dst_min.y;
max.y= ( src_max.y < dst_max.y ) ? src_max.y :dst_max.y;
}
if (src_min.z > dst_max.z || src_max.z < dst_min.z )
return Rect3();
else {
min.z= ( src_min.z > dst_min.z ) ? src_min.z :dst_min.z;
max.z= ( src_max.z < dst_max.z ) ? src_max.z :dst_max.z;
}
return Rect3( min, max-min );
}
bool Rect3::intersects_ray(const Vector3& p_from, const Vector3& p_dir,Vector3* r_clip,Vector3* r_normal) const {
Vector3 c1, c2;
Vector3 end = pos+size;
real_t near=-1e20;
real_t far=1e20;
int axis=0;
for (int i=0;i<3;i++){
if (p_dir[i] == 0){
if ((p_from[i] < pos[i]) || (p_from[i] > end[i])) {
return false;
}
} else { // ray not parallel to planes in this direction
c1[i] = (pos[i] - p_from[i]) / p_dir[i];
c2[i] = (end[i] - p_from[i]) / p_dir[i];
if(c1[i] > c2[i]){
std::swap(c1,c2);
}
if (c1[i] > near){
near = c1[i];
axis=i;
}
if (c2[i] < far){
far = c2[i];
}
if( (near > far) || (far < 0) ){
return false;
}
}
}
if (r_clip)
*r_clip=c1;
if (r_normal) {
*r_normal=Vector3();
(*r_normal)[axis]=p_dir[axis]?-1:1;
}
return true;
}
bool Rect3::intersects_segment(const Vector3& p_from, const Vector3& p_to,Vector3* r_clip,Vector3* r_normal) const {
real_t min=0,max=1;
int axis=0;
real_t sign=0;
for(int i=0;i<3;i++) {
real_t seg_from=p_from[i];
real_t seg_to=p_to[i];
real_t box_begin=pos[i];
real_t box_end=box_begin+size[i];
real_t cmin,cmax;
real_t csign;
if (seg_from < seg_to) {
if (seg_from > box_end || seg_to < box_begin)
return false;
real_t length=seg_to-seg_from;
cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0;
cmax = (seg_to > box_end)?((box_end - seg_from)/length):1;
csign=-1.0;
} else {
if (seg_to > box_end || seg_from < box_begin)
return false;
real_t length=seg_to-seg_from;
cmin = (seg_from > box_end)?(box_end - seg_from)/length:0;
cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1;
csign=1.0;
}
if (cmin > min) {
min = cmin;
axis=i;
sign=csign;
}
if (cmax < max)
max = cmax;
if (max < min)
return false;
}
Vector3 rel=p_to-p_from;
if (r_normal) {
Vector3 normal;
normal[axis]=sign;
*r_normal=normal;
}
if (r_clip)
*r_clip=p_from+rel*min;
return true;
}
bool Rect3::intersects_plane(const Plane &p_plane) const {
Vector3 points[8] = {
Vector3( pos.x , pos.y , pos.z ),
Vector3( pos.x , pos.y , pos.z+size.z ),
Vector3( pos.x , pos.y+size.y , pos.z ),
Vector3( pos.x , pos.y+size.y , pos.z+size.z ),
Vector3( pos.x+size.x , pos.y , pos.z ),
Vector3( pos.x+size.x , pos.y , pos.z+size.z ),
Vector3( pos.x+size.x , pos.y+size.y , pos.z ),
Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z ),
};
bool over=false;
bool under=false;
for (int i=0;i<8;i++) {
if (p_plane.distance_to(points[i])>0)
over=true;
else
under=true;
}
return under && over;
}
Vector3 Rect3::get_longest_axis() const {
Vector3 axis(1,0,0);
real_t max_size=size.x;
if (size.y > max_size ) {
axis=Vector3(0,1,0);
max_size=size.y;
}
if (size.z > max_size ) {
axis=Vector3(0,0,1);
max_size=size.z;
}
return axis;
}
int Rect3::get_longest_axis_index() const {
int axis=0;
real_t max_size=size.x;
if (size.y > max_size ) {
axis=1;
max_size=size.y;
}
if (size.z > max_size ) {
axis=2;
max_size=size.z;
}
return axis;
}
Vector3 Rect3::get_shortest_axis() const {
Vector3 axis(1,0,0);
real_t max_size=size.x;
if (size.y < max_size ) {
axis=Vector3(0,1,0);
max_size=size.y;
}
if (size.z < max_size ) {
axis=Vector3(0,0,1);
max_size=size.z;
}
return axis;
}
int Rect3::get_shortest_axis_index() const {
int axis=0;
real_t max_size=size.x;
if (size.y < max_size ) {
axis=1;
max_size=size.y;
}
if (size.z < max_size ) {
axis=2;
max_size=size.z;
}
return axis;
}
Rect3 Rect3::merge(const Rect3& p_with) const {
Rect3 aabb=*this;
aabb.merge_with(p_with);
return aabb;
}
Rect3 Rect3::expand(const Vector3& p_vector) const {
Rect3 aabb=*this;
aabb.expand_to(p_vector);
return aabb;
}
Rect3 Rect3::grow(real_t p_by) const {
Rect3 aabb=*this;
aabb.grow_by(p_by);
return aabb;
}
void Rect3::get_edge(int p_edge,Vector3& r_from,Vector3& r_to) const {
ERR_FAIL_INDEX(p_edge,12);
switch(p_edge) {
case 0:{
r_from=Vector3( pos.x+size.x , pos.y , pos.z );
r_to=Vector3( pos.x , pos.y , pos.z );
} break;
case 1:{
r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
r_to=Vector3( pos.x+size.x , pos.y , pos.z );
} break;
case 2:{
r_from=Vector3( pos.x , pos.y , pos.z+size.z );
r_to=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
} break;
case 3:{
r_from=Vector3( pos.x , pos.y , pos.z );
r_to=Vector3( pos.x , pos.y , pos.z+size.z );
} break;
case 4:{
r_from=Vector3( pos.x , pos.y+size.y , pos.z );
r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
} break;
case 5:{
r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
} break;
case 6:{
r_from=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
} break;
case 7:{
r_from=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
r_to=Vector3( pos.x , pos.y+size.y , pos.z );
} break;
case 8:{
r_from=Vector3( pos.x , pos.y , pos.z+size.z );
r_to=Vector3( pos.x , pos.y+size.y , pos.z+size.z );
} break;
case 9:{
r_from=Vector3( pos.x , pos.y , pos.z );
r_to=Vector3( pos.x , pos.y+size.y , pos.z );
} break;
case 10:{
r_from=Vector3( pos.x+size.x , pos.y , pos.z );
r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z );
} break;
case 11:{
r_from=Vector3( pos.x+size.x , pos.y , pos.z+size.z );
r_to=Vector3( pos.x+size.x , pos.y+size.y , pos.z+size.z );
} break;
}
}
Rect3::operator String() const {
//return String()+pos +" - "+ size;
return String(); // @Todo
}
}
#endif // RECT3_H

View File

@ -91,7 +91,6 @@ public:
void operator +=(const String &s) void operator +=(const String &s)
{ {
// @Todo
godot_string_operator_plus(&_godot_string, &_godot_string, &s._godot_string); godot_string_operator_plus(&_godot_string, &_godot_string, &s._godot_string);
} }

View File

@ -0,0 +1,383 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include "Basis.h"
#include "Plane.h"
#include "Rect3.h"
namespace godot {
class Transform {
public:
Basis basis;
Vector3 origin;
void invert();
Transform inverse() const;
void affine_invert();
Transform affine_inverse() const;
Transform rotated(const Vector3& p_axis,real_t p_phi) const;
void rotate(const Vector3& p_axis,real_t p_phi);
void rotate_basis(const Vector3& p_axis,real_t p_phi);
void set_look_at( const Vector3& p_eye, const Vector3& p_target, const Vector3& p_up );
Transform looking_at( const Vector3& p_target, const Vector3& p_up ) const;
void scale(const Vector3& p_scale);
Transform scaled(const Vector3& p_scale) const;
void scale_basis(const Vector3& p_scale);
void translate( real_t p_tx, real_t p_ty, real_t p_tz );
void translate( const Vector3& p_translation );
Transform translated( const Vector3& p_translation ) const;
const Basis& get_basis() const { return basis; }
void set_basis(const Basis& p_basis) { basis=p_basis; }
const Vector3& get_origin() const { return origin; }
void set_origin(const Vector3& p_origin) { origin=p_origin; }
void orthonormalize();
Transform orthonormalized() const;
bool operator==(const Transform& p_transform) const;
bool operator!=(const Transform& p_transform) const;
Vector3 xform(const Vector3& p_vector) const;
Vector3 xform_inv(const Vector3& p_vector) const;
Plane xform(const Plane& p_plane) const;
Plane xform_inv(const Plane& p_plane) const;
Rect3 xform(const Rect3& p_aabb) const;
Rect3 xform_inv(const Rect3& p_aabb) const;
void operator*=(const Transform& p_transform);
Transform operator*(const Transform& p_transform) const;
Transform interpolate_with(const Transform& p_transform, real_t p_c) const;
Transform inverse_xform(const Transform& t) const {
Vector3 v = t.origin - origin;
return Transform(basis.transpose_xform(t.basis),
basis.xform(v));
}
void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz,real_t tx, real_t ty, real_t tz) {
basis.elements[0][0]=xx;
basis.elements[0][1]=xy;
basis.elements[0][2]=xz;
basis.elements[1][0]=yx;
basis.elements[1][1]=yy;
basis.elements[1][2]=yz;
basis.elements[2][0]=zx;
basis.elements[2][1]=zy;
basis.elements[2][2]=zz;
origin.x=tx;
origin.y=ty;
origin.z=tz;
}
operator String() const;
Transform(const Basis& p_basis, const Vector3& p_origin=Vector3());
Transform() {}
};
Vector3 Transform::xform(const Vector3& p_vector) const {
return Vector3(
basis[0].dot(p_vector)+origin.x,
basis[1].dot(p_vector)+origin.y,
basis[2].dot(p_vector)+origin.z
);
}
Vector3 Transform::xform_inv(const Vector3& p_vector) const {
Vector3 v = p_vector - origin;
return Vector3(
(basis.elements[0][0]*v.x ) + ( basis.elements[1][0]*v.y ) + ( basis.elements[2][0]*v.z ),
(basis.elements[0][1]*v.x ) + ( basis.elements[1][1]*v.y ) + ( basis.elements[2][1]*v.z ),
(basis.elements[0][2]*v.x ) + ( basis.elements[1][2]*v.y ) + ( basis.elements[2][2]*v.z )
);
}
Plane Transform::xform(const Plane& p_plane) const {
Vector3 point=p_plane.normal*p_plane.d;
Vector3 point_dir=point+p_plane.normal;
point=xform(point);
point_dir=xform(point_dir);
Vector3 normal=point_dir-point;
normal.normalize();
real_t d=normal.dot(point);
return Plane(normal,d);
}
Plane Transform::xform_inv(const Plane& p_plane) const {
Vector3 point=p_plane.normal*p_plane.d;
Vector3 point_dir=point+p_plane.normal;
xform_inv(point);
xform_inv(point_dir);
Vector3 normal=point_dir-point;
normal.normalize();
real_t d=normal.dot(point);
return Plane(normal,d);
}
Rect3 Transform::xform(const Rect3& p_aabb) const {
/* define vertices */
Vector3 x=basis.get_axis(0)*p_aabb.size.x;
Vector3 y=basis.get_axis(1)*p_aabb.size.y;
Vector3 z=basis.get_axis(2)*p_aabb.size.z;
Vector3 pos = xform( p_aabb.pos );
//could be even further optimized
Rect3 new_aabb;
new_aabb.pos=pos;
new_aabb.expand_to( pos+x );
new_aabb.expand_to( pos+y );
new_aabb.expand_to( pos+z );
new_aabb.expand_to( pos+x+y );
new_aabb.expand_to( pos+x+z );
new_aabb.expand_to( pos+y+z );
new_aabb.expand_to( pos+x+y+z );
return new_aabb;
}
Rect3 Transform::xform_inv(const Rect3& p_aabb) const {
/* define vertices */
Vector3 vertices[8]={
Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
Vector3(p_aabb.pos.x+p_aabb.size.x, p_aabb.pos.y, p_aabb.pos.z),
Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z+p_aabb.size.z),
Vector3(p_aabb.pos.x, p_aabb.pos.y+p_aabb.size.y, p_aabb.pos.z),
Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z+p_aabb.size.z),
Vector3(p_aabb.pos.x, p_aabb.pos.y, p_aabb.pos.z)
};
Rect3 ret;
ret.pos=xform_inv(vertices[0]);
for (int i=1;i<8;i++) {
ret.expand_to( xform_inv(vertices[i]) );
}
return ret;
}
}
namespace godot {
void Transform::affine_invert() {
basis.invert();
origin = basis.xform(-origin);
}
Transform Transform::affine_inverse() const {
Transform ret=*this;
ret.affine_invert();
return ret;
}
void Transform::invert() {
basis.transpose();
origin = basis.xform(-origin);
}
Transform Transform::inverse() const {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
Transform ret=*this;
ret.invert();
return ret;
}
void Transform::rotate(const Vector3& p_axis,real_t p_phi) {
*this = rotated(p_axis, p_phi);
}
Transform Transform::rotated(const Vector3& p_axis,real_t p_phi) const{
return Transform(Basis( p_axis, p_phi ), Vector3()) * (*this);
}
void Transform::rotate_basis(const Vector3& p_axis,real_t p_phi) {
basis.rotate(p_axis,p_phi);
}
Transform Transform::looking_at( const Vector3& p_target, const Vector3& p_up ) const {
Transform t = *this;
t.set_look_at(origin,p_target,p_up);
return t;
}
void Transform::set_look_at( const Vector3& p_eye, const Vector3& p_target, const Vector3& p_up ) {
// Reference: MESA source code
Vector3 v_x, v_y, v_z;
/* Make rotation matrix */
/* Z vector */
v_z = p_eye - p_target;
v_z.normalize();
v_y = p_up;
v_x=v_y.cross(v_z);
/* Recompute Y = Z cross X */
v_y=v_z.cross(v_x);
v_x.normalize();
v_y.normalize();
basis.set_axis(0,v_x);
basis.set_axis(1,v_y);
basis.set_axis(2,v_z);
origin=p_eye;
}
Transform Transform::interpolate_with(const Transform& p_transform, real_t p_c) const {
/* not sure if very "efficient" but good enough? */
Vector3 src_scale = basis.get_scale();
Quat src_rot = basis;
Vector3 src_loc = origin;
Vector3 dst_scale = p_transform.basis.get_scale();
Quat dst_rot = p_transform.basis;
Vector3 dst_loc = p_transform.origin;
Transform dst;
dst.basis=src_rot.slerp(dst_rot,p_c);
dst.basis.scale(src_scale.linear_interpolate(dst_scale,p_c));
dst.origin=src_loc.linear_interpolate(dst_loc,p_c);
return dst;
}
void Transform::scale(const Vector3& p_scale) {
basis.scale(p_scale);
origin*=p_scale;
}
Transform Transform::scaled(const Vector3& p_scale) const {
Transform t = *this;
t.scale(p_scale);
return t;
}
void Transform::scale_basis(const Vector3& p_scale) {
basis.scale(p_scale);
}
void Transform::translate( real_t p_tx, real_t p_ty, real_t p_tz) {
translate( Vector3(p_tx,p_ty,p_tz) );
}
void Transform::translate( const Vector3& p_translation ) {
for( int i = 0; i < 3; i++ ) {
origin[i] += basis[i].dot(p_translation);
}
}
Transform Transform::translated( const Vector3& p_translation ) const {
Transform t=*this;
t.translate(p_translation);
return t;
}
void Transform::orthonormalize() {
basis.orthonormalize();
}
Transform Transform::orthonormalized() const {
Transform _copy = *this;
_copy.orthonormalize();
return _copy;
}
bool Transform::operator==(const Transform& p_transform) const {
return (basis==p_transform.basis && origin==p_transform.origin);
}
bool Transform::operator!=(const Transform& p_transform) const {
return (basis!=p_transform.basis || origin!=p_transform.origin);
}
void Transform::operator*=(const Transform& p_transform) {
origin=xform(p_transform.origin);
basis*=p_transform.basis;
}
Transform Transform::operator*(const Transform& p_transform) const {
Transform t=*this;
t*=p_transform;
return t;
}
Transform::operator String() const {
return basis.operator String() + " - " + origin.operator String();
}
Transform::Transform(const Basis& p_basis, const Vector3& p_origin) {
basis=p_basis;
origin=p_origin;
}
}
#endif // TRANSFORM_H

View File

@ -0,0 +1,448 @@
#ifndef TRANSFORM2D_H
#define TRANSFORM2D_H
#include "Vector2.h"
// @Todo
// error handling plllls
#ifndef ERR_FAIL_INDEX_V
#define ERR_FAIL_INDEX_V(a, b, c)
#endif
#ifndef ERR_FAIL_INDEX
#define ERR_FAIL_INDEX(a, b)
#endif
#ifndef ERR_FAIL_COND
#define ERR_FAIL_COND(a)
#endif
namespace godot {
typedef Vector2 Size2;
class Rect2;
struct Transform2D {
// 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];
real_t tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
real_t tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
Vector2& operator[](int p_idx) { return elements[p_idx]; }
Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
void invert();
Transform2D inverse() const;
void affine_invert();
Transform2D affine_inverse() const;
void set_rotation(real_t p_phi);
real_t get_rotation() const;
void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
void rotate(real_t p_phi);
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 );
real_t basis_determinant() const;
Size2 get_scale() const;
const Vector2& get_origin() const { return elements[2]; }
void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
Transform2D scaled(const Size2& p_scale) const;
Transform2D basis_scaled(const Size2& p_scale) const;
Transform2D translated(const Vector2& p_offset) const;
Transform2D rotated(real_t p_phi) const;
Transform2D untranslated() const;
void orthonormalize();
Transform2D orthonormalized() const;
bool operator==(const Transform2D& p_transform) const;
bool operator!=(const Transform2D& p_transform) const;
void operator*=(const Transform2D& p_transform);
Transform2D operator*(const Transform2D& p_transform) const;
Transform2D interpolate_with(const Transform2D& p_transform, real_t p_c) const;
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;
operator String() const;
Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
elements[0][0] = xx;
elements[0][1] = xy;
elements[1][0] = yx;
elements[1][1] = yy;
elements[2][0] = ox;
elements[2][1] = oy;
}
Transform2D(real_t p_rot, const Vector2& p_pos);
Transform2D() { elements[0][0]=1.0; elements[1][1]=1.0; }
};
}
#include "Rect2.h"
namespace godot {
Vector2 Transform2D::basis_xform(const Vector2& v) const {
return Vector2(
tdotx(v),
tdoty(v)
);
}
Vector2 Transform2D::basis_xform_inv(const Vector2& v) const{
return Vector2(
elements[0].dot(v),
elements[1].dot(v)
);
}
Vector2 Transform2D::xform(const Vector2& v) const {
return Vector2(
tdotx(v),
tdoty(v)
) + elements[2];
}
Vector2 Transform2D::xform_inv(const Vector2& p_vec) const {
Vector2 v = p_vec - elements[2];
return Vector2(
elements[0].dot(v),
elements[1].dot(v)
);
}
Rect2 Transform2D::xform(const Rect2& p_rect) const {
Vector2 x=elements[0]*p_rect.size.x;
Vector2 y=elements[1]*p_rect.size.y;
Vector2 pos = xform( p_rect.pos );
Rect2 new_rect;
new_rect.pos=pos;
new_rect.expand_to( pos+x );
new_rect.expand_to( pos+y );
new_rect.expand_to( pos+x+y );
return new_rect;
}
void Transform2D::set_rotation_and_scale(real_t p_rot,const Size2& p_scale) {
elements[0][0]=::cos(p_rot)*p_scale.x;
elements[1][1]=::cos(p_rot)*p_scale.y;
elements[1][0]=-::sin(p_rot)*p_scale.y;
elements[0][1]=::sin(p_rot)*p_scale.x;
}
Rect2 Transform2D::xform_inv(const Rect2& p_rect) const {
Vector2 ends[4]={
xform_inv( p_rect.pos ),
xform_inv( Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y ) ),
xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y ) ),
xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y ) )
};
Rect2 new_rect;
new_rect.pos=ends[0];
new_rect.expand_to(ends[1]);
new_rect.expand_to(ends[2]);
new_rect.expand_to(ends[3]);
return new_rect;
}
void Transform2D::invert() {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
std::swap(elements[0][1],elements[1][0]);
elements[2] = basis_xform(-elements[2]);
}
Transform2D Transform2D::inverse() const {
Transform2D inv=*this;
inv.invert();
return inv;
}
void Transform2D::affine_invert() {
real_t det = basis_determinant();
ERR_FAIL_COND(det==0);
real_t idet = 1.0 / det;
std::swap( elements[0][0],elements[1][1] );
elements[0]*=Vector2(idet,-idet);
elements[1]*=Vector2(-idet,idet);
elements[2] = basis_xform(-elements[2]);
}
Transform2D Transform2D::affine_inverse() const {
Transform2D inv=*this;
inv.affine_invert();
return inv;
}
void Transform2D::rotate(real_t p_phi) {
*this = Transform2D(p_phi,Vector2()) * (*this);
}
real_t Transform2D::get_rotation() const {
real_t det = basis_determinant();
Transform2D m = orthonormalized();
if (det < 0) {
m.scale_basis(Size2(-1,-1));
}
return ::atan2(m[0].y,m[0].x);
}
void Transform2D::set_rotation(real_t p_rot) {
real_t cr = ::cos(p_rot);
real_t sr = ::sin(p_rot);
elements[0][0]=cr;
elements[0][1]=sr;
elements[1][0]=-sr;
elements[1][1]=cr;
}
Transform2D::Transform2D(real_t p_rot, const Vector2& p_pos) {
real_t cr = ::cos(p_rot);
real_t sr = ::sin(p_rot);
elements[0][0]=cr;
elements[0][1]=sr;
elements[1][0]=-sr;
elements[1][1]=cr;
elements[2]=p_pos;
}
Size2 Transform2D::get_scale() const {
real_t det_sign = basis_determinant() > 0 ? 1 : -1;
return det_sign * Size2( elements[0].length(), elements[1].length() );
}
void Transform2D::scale(const Size2& p_scale) {
scale_basis(p_scale);
elements[2]*=p_scale;
}
void Transform2D::scale_basis(const Size2& p_scale) {
elements[0][0]*=p_scale.x;
elements[0][1]*=p_scale.y;
elements[1][0]*=p_scale.x;
elements[1][1]*=p_scale.y;
}
void Transform2D::translate( real_t p_tx, real_t p_ty) {
translate(Vector2(p_tx,p_ty));
}
void Transform2D::translate( const Vector2& p_translation ) {
elements[2]+=basis_xform(p_translation);
}
void Transform2D::orthonormalize() {
// Gram-Schmidt Process
Vector2 x=elements[0];
Vector2 y=elements[1];
x.normalize();
y = (y-x*(x.dot(y)));
y.normalize();
elements[0]=x;
elements[1]=y;
}
Transform2D Transform2D::orthonormalized() const {
Transform2D on=*this;
on.orthonormalize();
return on;
}
bool Transform2D::operator==(const Transform2D& p_transform) const {
for(int i=0;i<3;i++) {
if (elements[i]!=p_transform.elements[i])
return false;
}
return true;
}
bool Transform2D::operator!=(const Transform2D& p_transform) const {
for(int i=0;i<3;i++) {
if (elements[i]!=p_transform.elements[i])
return true;
}
return false;
}
void Transform2D::operator*=(const Transform2D& p_transform) {
elements[2] = xform(p_transform.elements[2]);
real_t x0,x1,y0,y1;
x0 = tdotx(p_transform.elements[0]);
x1 = tdoty(p_transform.elements[0]);
y0 = tdotx(p_transform.elements[1]);
y1 = tdoty(p_transform.elements[1]);
elements[0][0]=x0;
elements[0][1]=x1;
elements[1][0]=y0;
elements[1][1]=y1;
}
Transform2D Transform2D::operator*(const Transform2D& p_transform) const {
Transform2D t = *this;
t*=p_transform;
return t;
}
Transform2D Transform2D::scaled(const Size2& p_scale) const {
Transform2D copy=*this;
copy.scale(p_scale);
return copy;
}
Transform2D Transform2D::basis_scaled(const Size2& p_scale) const {
Transform2D copy=*this;
copy.scale_basis(p_scale);
return copy;
}
Transform2D Transform2D::untranslated() const {
Transform2D copy=*this;
copy.elements[2]=Vector2();
return copy;
}
Transform2D Transform2D::translated(const Vector2& p_offset) const {
Transform2D copy=*this;
copy.translate(p_offset);
return copy;
}
Transform2D Transform2D::rotated(real_t p_phi) const {
Transform2D copy=*this;
copy.rotate(p_phi);
return copy;
}
real_t Transform2D::basis_determinant() const {
return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
}
Transform2D Transform2D::interpolate_with(const Transform2D& p_transform, real_t p_c) const {
//extract parameters
Vector2 p1 = get_origin();
Vector2 p2 = p_transform.get_origin();
real_t r1 = get_rotation();
real_t r2 = p_transform.get_rotation();
Size2 s1 = get_scale();
Size2 s2 = p_transform.get_scale();
//slerp rotation
Vector2 v1(::cos(r1), ::sin(r1));
Vector2 v2(::cos(r2), ::sin(r2));
real_t dot = v1.dot(v2);
dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1]
Vector2 v;
if (dot > 0.9995) {
v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
} else {
real_t angle = p_c*::acos(dot);
Vector2 v3 = (v2 - v1*dot).normalized();
v = v1*::cos(angle) + v3*::sin(angle);
}
//construct matrix
Transform2D res(::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c));
res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c));
return res;
}
Transform2D::operator String() const {
//return String(String()+elements[0]+", "+elements[1]+", "+elements[2]);
return String(); // @Todo
}
}
#endif // TRANSFORM2D_H

View File

@ -380,6 +380,11 @@ struct Vector3 {
v.snap(by); v.snap(by);
return v; return v;
} }
operator String() const
{
return String(); // @Todo
}
}; };
Vector3 operator*(real_t p_scalar, const Vector3& p_vec) Vector3 operator*(real_t p_scalar, const Vector3& p_vec)
@ -387,6 +392,11 @@ Vector3 operator*(real_t p_scalar, const Vector3& p_vec)
return p_vec * p_scalar; return p_vec * p_scalar;
} }
Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
return p_a.cross(p_b);
}
} }
#endif // VECTOR3_H #endif // VECTOR3_H