summaryrefslogtreecommitdiff
path: root/src/lib/evas/include/evas_3d_utils.h
diff options
context:
space:
mode:
authorIvan Furs <i.furs@samsung.com>2016-02-12 20:58:04 +0100
committerCedric BAIL <cedric@osg.samsung.com>2016-02-12 21:25:55 +0100
commit5af22ba27af5298a9a836c328677453689138f24 (patch)
treeaebcb67770285ad7c05b8923d1a5038fd8749caa /src/lib/evas/include/evas_3d_utils.h
parent61f7236a62db2c932c82bc68fb1ac8082514831f (diff)
evas: optimize Canvas3D calculation of bounding space
Summary: Bounding box and sphere will calculate if user need callback of collision or data of box or data of sphere. Reviewers: sri.hebbar, cedric Subscribers: cedric, jpeg Differential Revision: https://phab.enlightenment.org/D3643 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/lib/evas/include/evas_3d_utils.h')
-rw-r--r--src/lib/evas/include/evas_3d_utils.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lib/evas/include/evas_3d_utils.h b/src/lib/evas/include/evas_3d_utils.h
index b2eb08ec44..bb94a65283 100644
--- a/src/lib/evas/include/evas_3d_utils.h
+++ b/src/lib/evas/include/evas_3d_utils.h
@@ -2016,3 +2016,82 @@ evas_tangent_space_get(float *data, float *tex_data, float *normal_data, unsigne
2016 2016
2017 return; 2017 return;
2018} 2018}
2019
2020static inline void
2021calculate_box(Evas_Box3 *box3, int vertex_count, Evas_Vec3 *vertex_position)
2022{
2023 int i = 0;
2024 float vxmin, vymin, vzmin, vxmax, vymax, vzmax;
2025
2026 vxmax = vxmin = vertex_position[0].x;
2027 vymax = vymin = vertex_position[0].y;
2028 vzmax = vzmin = vertex_position[0].z;
2029
2030 for (i = 1; i < vertex_count; ++i)
2031 {
2032 if (vxmin > vertex_position[i].x) vxmin = vertex_position[i].x;
2033 if (vxmax < vertex_position[i].x) vxmax = vertex_position[i].x;
2034 if (vymin > vertex_position[i].y) vymin = vertex_position[i].y;
2035 if (vymax < vertex_position[i].y) vymax = vertex_position[i].y;
2036 if (vzmin > vertex_position[i].z) vzmin = vertex_position[i].z;
2037 if (vzmax < vertex_position[i].z) vzmax = vertex_position[i].z;
2038 }
2039 evas_box3_set(box3, vxmin, vymin, vzmin, vxmax, vymax, vzmax);
2040}
2041
2042static inline void
2043calculate_sphere(Evas_Sphere *sphere, int vertex_count, Evas_Vec3 *vertex_position)
2044{
2045 float radius = 0.0001f;
2046 Evas_Vec3 center, pos, diff;
2047 float len, alpha, alpha2;
2048 int i, k;
2049
2050 // shuffle array for averaging algorithms error
2051 for (i = 0; i < vertex_count; i++)
2052 {
2053 k = i + rand()%(vertex_count-i);
2054 pos = vertex_position[i];
2055 vertex_position[i] = vertex_position[k];
2056 vertex_position[k] = pos;
2057 }
2058
2059 center = vertex_position[0];
2060
2061 for (k = 0; k < 2; k++)
2062 {
2063 for (i = 0; i < vertex_count; ++i)
2064 {
2065 pos = vertex_position[i];
2066 evas_vec3_subtract(&diff, &pos, &center);
2067 len = evas_vec3_length_get(&diff);
2068 if (len > radius)
2069 {
2070 alpha = len / radius;
2071 alpha2 = alpha * alpha;
2072 radius = 0.5f * (alpha + 1 / alpha) * radius;
2073 evas_vec3_scale(&pos, &pos, 1 - 1 / alpha2);
2074 evas_vec3_scale(&center, &center, (1 + 1 / alpha2));
2075 evas_vec3_add(&center, &center, &pos);
2076 evas_vec3_scale(&center, &center, 0.5f);
2077 }
2078 }
2079 }
2080
2081 for (i = 0; i < vertex_count; ++i)
2082 {
2083 pos = vertex_position[i];
2084 evas_vec3_subtract(&diff, &pos, &center);
2085 len = evas_vec3_length_get(&diff);
2086
2087 if (len > radius)
2088 {
2089 radius = (radius + len) / 2.0f;
2090 evas_vec3_scale(&diff, &diff, (len - radius) / len);
2091 evas_vec3_add(&center, &center, &diff);
2092 }
2093 }
2094
2095 sphere->radius = radius;
2096 sphere->center = center;
2097}