Debian Codegraph @

scene/resources/3d/convex_polygon_shape_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* convex_polygon_shape_3d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "convex_polygon_shape_3d.h"
32 #include "core/math/convex_hull.h"
33 #include "scene/resources/mesh.h"
34 #include "servers/physics_3d/physics_server_3d.h"
35
36 Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const {
37 Vector<Vector3> poly_points = get_points();
38
39 if (poly_points.size() > 1) { // Need at least 2 points for a line.
40 Vector<Vector3> varr = Variant(poly_points);
41 Geometry3D::MeshData md;
42 Error err = ConvexHullComputer::convex_hull(varr, md);
43 if (err == OK) {
44 Vector<Vector3> lines;
45 lines.resize(md.edges.size() * 2);
46 for (uint32_t i = 0; i < md.edges.size(); i++) {
47 lines.write[i * 2 + 0] = md.vertices[md.edges[i].vertex_a];
48 lines.write[i * 2 + 1] = md.vertices[md.edges[i].vertex_b];
49 }
50 return lines;
51 }
52 }
53
54 return Vector<Vector3>();
55 }
56
57 Ref<ArrayMesh> ConvexPolygonShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {
58 const Vector<Vector3> hull_points = get_points();
59
60 Vector<Vector3> verts;
61 Vector<Color> colors;
62 Vector<int> indices;
63
64 if (hull_points.size() >= 3) {
65 Geometry3D::MeshData md;
66 Error err = ConvexHullComputer::convex_hull(hull_points, md);
67 if (err == OK) {
68 verts = Vector<Vector3>(md.vertices);
69 for (int i = 0; i < verts.size(); i++) {
70 colors.push_back(p_modulate);
71 }
72 for (const Geometry3D::MeshData::Face &face : md.faces) {
73 const int first_point = face.indices[0];
74 const int indices_count = face.indices.size();
75 for (int i = 1; i < indices_count - 1; i++) {
76 indices.push_back(first_point);
77 indices.push_back(face.indices[i]);
78 indices.push_back(face.indices[i + 1]);
79 }
80 }
81 }
82 }
83
84 Ref<ArrayMesh> mesh = memnew(ArrayMesh);
85 Array a;
86 a.resize(Mesh::ARRAY_MAX);
87 a[RS::ARRAY_VERTEX] = verts;
88 a[RS::ARRAY_COLOR] = colors;
89 a[RS::ARRAY_INDEX] = indices;
90 mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
91
92 return mesh;
93 }
94
95 real_t ConvexPolygonShape3D::get_enclosing_radius() const {
96 Vector<Vector3> data = get_points();
97 const Vector3 *read = data.ptr();
98 real_t r = 0.0;
99 for (int i(0); i < data.size(); i++) {
100 r = MAX(read[i].length_squared(), r);
101 }
102 return Math::sqrt(r);
103 }
104
105 void ConvexPolygonShape3D::_update_shape() {
106 PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), points);
107 Shape3D::_update_shape();
108 }
109
110 void ConvexPolygonShape3D::set_points(const Vector<Vector3> &p_points) {
111 points = p_points;
112 _update_shape();
113 emit_changed();
114 }
115
116 Vector<Vector3> ConvexPolygonShape3D::get_points() const {
117 return points;
118 }
119
120 void ConvexPolygonShape3D::_bind_methods() {
121 ClassDB::bind_method(D_METHOD("set_points", "points"), &ConvexPolygonShape3D::set_points);
122 ClassDB::bind_method(D_METHOD("get_points"), &ConvexPolygonShape3D::get_points);
123
124 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "points"), "set_points", "get_points");
125 }
126
127 ConvexPolygonShape3D::ConvexPolygonShape3D() :
128 Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON)) {
129 }