2017-03-02 09:42:05 +00:00
|
|
|
/*************************************************************************/
|
2017-09-01 14:07:55 +00:00
|
|
|
/* math_fieldwise.cpp */
|
2017-03-02 09:42:05 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2017-03-02 09:42:05 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-03-02 09:42:05 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "math_fieldwise.h"
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
#define SETUP_TYPE(m_type) \
|
|
|
|
m_type source = p_source; \
|
|
|
|
m_type target = p_target;
|
|
|
|
#define TRY_TRANSFER_FIELD(m_name, m_member) \
|
|
|
|
if (p_field == m_name) { \
|
|
|
|
target.m_member = source.m_member; \
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const String &p_field) {
|
|
|
|
ERR_FAIL_COND_V(p_target.get_type() != p_source.get_type(), p_target);
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
/* clang-format makes a mess of this macro usage */
|
|
|
|
/* clang-format off */
|
2017-03-02 09:42:05 +00:00
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
switch (p_source.get_type()) {
|
2017-03-02 09:42:05 +00:00
|
|
|
case Variant::VECTOR2: {
|
|
|
|
SETUP_TYPE(Vector2)
|
|
|
|
|
2017-03-05 14:56:52 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:02:23 +00:00
|
|
|
case Variant::VECTOR2I: {
|
|
|
|
SETUP_TYPE(Vector2i)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:42:05 +00:00
|
|
|
case Variant::RECT2: {
|
|
|
|
SETUP_TYPE(Rect2)
|
|
|
|
|
2017-06-03 22:25:13 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("x", position.x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", position.y)
|
2017-03-05 14:56:52 +00:00
|
|
|
else TRY_TRANSFER_FIELD("w", size.x)
|
|
|
|
else TRY_TRANSFER_FIELD("h", size.y)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:02:23 +00:00
|
|
|
case Variant::RECT2I: {
|
|
|
|
SETUP_TYPE(Rect2i)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("x", position.x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", position.y)
|
|
|
|
else TRY_TRANSFER_FIELD("w", size.x)
|
|
|
|
else TRY_TRANSFER_FIELD("h", size.y)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:42:05 +00:00
|
|
|
case Variant::VECTOR3: {
|
|
|
|
SETUP_TYPE(Vector3)
|
|
|
|
|
2017-03-05 14:56:52 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", z)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
2022-09-21 10:48:48 +00:00
|
|
|
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
case Variant::VECTOR3I: {
|
|
|
|
SETUP_TYPE(Vector3i)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", z)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
2022-09-21 10:48:48 +00:00
|
|
|
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
case Variant::VECTOR4: {
|
|
|
|
SETUP_TYPE(Vector4)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", z)
|
|
|
|
else TRY_TRANSFER_FIELD("w", w)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
2022-09-21 10:48:48 +00:00
|
|
|
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
case Variant::VECTOR4I: {
|
|
|
|
SETUP_TYPE(Vector4i)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", z)
|
|
|
|
else TRY_TRANSFER_FIELD("w", w)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:42:05 +00:00
|
|
|
case Variant::PLANE: {
|
|
|
|
SETUP_TYPE(Plane)
|
|
|
|
|
2017-03-05 14:56:52 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("x", normal.x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", normal.y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", normal.z)
|
2020-05-10 14:47:11 +00:00
|
|
|
else TRY_TRANSFER_FIELD("d", d)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2021-01-20 07:02:02 +00:00
|
|
|
case Variant::QUATERNION: {
|
|
|
|
SETUP_TYPE(Quaternion)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
2017-03-05 14:56:52 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("x", x)
|
|
|
|
else TRY_TRANSFER_FIELD("y", y)
|
|
|
|
else TRY_TRANSFER_FIELD("z", z)
|
|
|
|
else TRY_TRANSFER_FIELD("w", w)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
case Variant::AABB: {
|
|
|
|
SETUP_TYPE(AABB)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("px", position.x)
|
|
|
|
else TRY_TRANSFER_FIELD("py", position.y)
|
|
|
|
else TRY_TRANSFER_FIELD("pz", position.z)
|
2017-03-05 14:56:52 +00:00
|
|
|
else TRY_TRANSFER_FIELD("sx", size.x)
|
|
|
|
else TRY_TRANSFER_FIELD("sy", size.y)
|
|
|
|
else TRY_TRANSFER_FIELD("sz", size.z)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Variant::TRANSFORM2D: {
|
|
|
|
SETUP_TYPE(Transform2D)
|
|
|
|
|
2022-04-24 21:59:24 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("xx", columns[0][0])
|
|
|
|
else TRY_TRANSFER_FIELD("xy", columns[0][1])
|
|
|
|
else TRY_TRANSFER_FIELD("yx", columns[1][0])
|
|
|
|
else TRY_TRANSFER_FIELD("yy", columns[1][1])
|
|
|
|
else TRY_TRANSFER_FIELD("ox", columns[2][0])
|
|
|
|
else TRY_TRANSFER_FIELD("oy", columns[2][1])
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Variant::BASIS: {
|
|
|
|
SETUP_TYPE(Basis)
|
|
|
|
|
2022-04-24 22:07:35 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("xx", rows[0][0])
|
|
|
|
else TRY_TRANSFER_FIELD("xy", rows[0][1])
|
|
|
|
else TRY_TRANSFER_FIELD("xz", rows[0][2])
|
|
|
|
else TRY_TRANSFER_FIELD("yx", rows[1][0])
|
|
|
|
else TRY_TRANSFER_FIELD("yy", rows[1][1])
|
|
|
|
else TRY_TRANSFER_FIELD("yz", rows[1][2])
|
|
|
|
else TRY_TRANSFER_FIELD("zx", rows[2][0])
|
|
|
|
else TRY_TRANSFER_FIELD("zy", rows[2][1])
|
|
|
|
else TRY_TRANSFER_FIELD("zz", rows[2][2])
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2021-04-28 07:36:08 +00:00
|
|
|
case Variant::TRANSFORM3D: {
|
2020-10-17 05:08:21 +00:00
|
|
|
SETUP_TYPE(Transform3D)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
2022-04-24 22:07:35 +00:00
|
|
|
/**/ TRY_TRANSFER_FIELD("xx", basis.rows[0][0])
|
|
|
|
else TRY_TRANSFER_FIELD("xy", basis.rows[0][1])
|
|
|
|
else TRY_TRANSFER_FIELD("xz", basis.rows[0][2])
|
|
|
|
else TRY_TRANSFER_FIELD("yx", basis.rows[1][0])
|
|
|
|
else TRY_TRANSFER_FIELD("yy", basis.rows[1][1])
|
|
|
|
else TRY_TRANSFER_FIELD("yz", basis.rows[1][2])
|
|
|
|
else TRY_TRANSFER_FIELD("zx", basis.rows[2][0])
|
|
|
|
else TRY_TRANSFER_FIELD("zy", basis.rows[2][1])
|
|
|
|
else TRY_TRANSFER_FIELD("zz", basis.rows[2][2])
|
2017-03-05 14:56:52 +00:00
|
|
|
else TRY_TRANSFER_FIELD("xo", origin.x)
|
|
|
|
else TRY_TRANSFER_FIELD("yo", origin.y)
|
|
|
|
else TRY_TRANSFER_FIELD("zo", origin.z)
|
2017-03-02 09:42:05 +00:00
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2022-09-26 10:02:23 +00:00
|
|
|
case Variant::PROJECTION: {
|
|
|
|
SETUP_TYPE(Projection)
|
|
|
|
|
|
|
|
/**/ TRY_TRANSFER_FIELD("xx", matrix[0].x)
|
|
|
|
else TRY_TRANSFER_FIELD("xy", matrix[0].y)
|
|
|
|
else TRY_TRANSFER_FIELD("xz", matrix[0].z)
|
|
|
|
else TRY_TRANSFER_FIELD("xw", matrix[0].w)
|
|
|
|
else TRY_TRANSFER_FIELD("yx", matrix[1].x)
|
|
|
|
else TRY_TRANSFER_FIELD("yy", matrix[1].y)
|
|
|
|
else TRY_TRANSFER_FIELD("yz", matrix[1].z)
|
|
|
|
else TRY_TRANSFER_FIELD("yw", matrix[1].w)
|
|
|
|
else TRY_TRANSFER_FIELD("zx", matrix[2].x)
|
|
|
|
else TRY_TRANSFER_FIELD("zy", matrix[2].y)
|
|
|
|
else TRY_TRANSFER_FIELD("zz", matrix[2].z)
|
|
|
|
else TRY_TRANSFER_FIELD("zw", matrix[2].w)
|
|
|
|
else TRY_TRANSFER_FIELD("xo", matrix[3].x)
|
|
|
|
else TRY_TRANSFER_FIELD("yo", matrix[3].y)
|
|
|
|
else TRY_TRANSFER_FIELD("zo", matrix[3].z)
|
|
|
|
else TRY_TRANSFER_FIELD("wo", matrix[3].w)
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2017-03-02 09:42:05 +00:00
|
|
|
default: {
|
|
|
|
ERR_FAIL_V(p_target);
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 20:36:34 +00:00
|
|
|
/* clang-format on */
|
2017-03-02 09:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TOOLS_ENABLED
|