summaryrefslogtreecommitdiff
path: root/src/lib/evas/include/evas_3d_utils.h
diff options
context:
space:
mode:
authorperepelits.m <perepelits.m@samsung.com>2014-06-05 20:50:42 +0200
committerCedric BAIL <c.bail@partner.samsung.com>2014-06-05 20:50:49 +0200
commite5905f3f7485a6b731b75005950d18337e0e07f3 (patch)
tree4a6310c813dc668eaabfeaea923666640d912689 /src/lib/evas/include/evas_3d_utils.h
parent700f4396e18e473220a1777fb4d50401457becfd (diff)
evas: Evas_3D - add evas_box3_ray3_intersect and evas_box2_intersect_2d functions.
Summary: First function is used for intersection of ray and parallelepiped, the second one - for intersection of ray and rectangle in two-dimensional case. @feature Reviewers: Hermet, cedric, raster CC: cedric Differential Revision: https://phab.enlightenment.org/D956 Signed-off-by: Cedric BAIL <c.bail@partner.samsung.com>
Diffstat (limited to 'src/lib/evas/include/evas_3d_utils.h')
-rw-r--r--src/lib/evas/include/evas_3d_utils.h171
1 files changed, 169 insertions, 2 deletions
diff --git a/src/lib/evas/include/evas_3d_utils.h b/src/lib/evas/include/evas_3d_utils.h
index ee4aab2249..8e02a07223 100644
--- a/src/lib/evas/include/evas_3d_utils.h
+++ b/src/lib/evas/include/evas_3d_utils.h
@@ -3,6 +3,7 @@
3#endif 3#endif
4 4
5#include <math.h> 5#include <math.h>
6#include <float.h>
6 7
7#define DEGREE_TO_RADIAN(x) (((x) * M_PI) / 180.0) 8#define DEGREE_TO_RADIAN(x) (((x) * M_PI) / 180.0)
8#define EVAS_MATRIX_IS_IDENTITY 0x00000001 9#define EVAS_MATRIX_IS_IDENTITY 0x00000001
@@ -1537,12 +1538,178 @@ evas_ray3_init(Evas_Ray3 *ray, Evas_Real x, Evas_Real y, const Evas_Mat4 *mvp)
1537} 1538}
1538 1539
1539static inline Eina_Bool 1540static inline Eina_Bool
1540evas_box3_ray3_intersect(const Evas_Box3 *box EINA_UNUSED, const Evas_Ray3 *ray EINA_UNUSED) 1541evas_box2_intersect_2d(const Evas_Box2 *box, const Evas_Vec2 *org, const Evas_Vec2 *dir)
1541{ 1542{
1542 /* TODO: */ 1543 Evas_Real t1, t2, t_near = FLT_MIN, t_far = FLT_MAX;
1544 /* ray intersects box if its begins in */
1545 if ((org->x >= box->p0.x) && (org->x <= box->p1.x) &&
1546 (org->y >= box->p0.y) && (org->y <= box->p1.y))
1547 {
1548 return EINA_TRUE;
1549 }
1550 /* minmax algorithm of ray and box intersection */
1551 if ((dir->x != 0.0f) && (dir->y != 0.0f))
1552 {
1553 t1 = (box->p0.x - org->x) / dir->x;
1554 t2 = (box->p1.x - org->x) / dir->x;
1555
1556 if (t1 > t2)
1557 {
1558 Evas_Real tmp = t1;
1559 t1 = t2;
1560 t2 = tmp;
1561 }
1562
1563 if (t1 > t_near) t_near = t1;
1564 if (t2 < t_far) t_far = t2;
1565
1566 if (t_far < 0.0f)
1567 return EINA_FALSE;
1568
1569 t1 = (box->p0.y - org->y) / dir->y;
1570 t2 = (box->p1.y - org->y) / dir->y;
1571
1572 if (t1 > t2)
1573 {
1574 Evas_Real tmp = t1;
1575 t1 = t2;
1576 t2 = tmp;
1577 }
1578
1579 if (t1 > t_near) t_near = t1;
1580 if (t2 < t_far) t_far = t2;
1581
1582 if ((t_near > t_far) || (t_far < 0.0f))
1583 return EINA_FALSE;
1584 }
1585 /* case when ray is parallel to one of axes */
1586 else if (dir->x == 0.0f)
1587 {
1588 if ((org->x < box->p0.x) && (org->x > box->p1.x))
1589 return EINA_FALSE;
1590 }
1591 else if (org->y < box->p0.y && org->y > box->p1.y)
1592 return EINA_FALSE;
1593
1543 return EINA_TRUE; 1594 return EINA_TRUE;
1544} 1595}
1545 1596
1597static inline Eina_Bool
1598evas_box3_ray3_intersect(const Evas_Box3 *box, const Evas_Ray3 *ray)
1599{
1600 Evas_Real t1, t2, t_near = FLT_MIN, t_far = FLT_MAX;
1601 Evas_Box2 box2;
1602 Evas_Vec2 org2;
1603 Evas_Vec2 dir2;
1604 Eina_Bool intersect = EINA_FALSE;
1605
1606 /* ray intersects box if its begins in */
1607 if ((ray->org.x >= box->p0.x) && (ray->org.x <= box->p1.x) &&
1608 (ray->org.y >= box->p0.y) && (ray->org.y <= box->p1.y) &&
1609 (ray->org.z >= box->p0.z) && (ray->org.z <= box->p1.z))
1610 {
1611 return EINA_TRUE;
1612 }
1613 /* minmax algorithm of ray and box intersection */
1614 if ((ray->dir.x != 0.0f) && (ray->dir.y != 0.0f) && (ray->dir.z != 0.0f))
1615 {
1616 t1 = (box->p0.x - ray->org.x) / ray->dir.x;
1617 t2 = (box->p1.x - ray->org.x) / ray->dir.x;
1618
1619 if (t1 > t2)
1620 {
1621 Evas_Real tmp = t1;
1622 t1 = t2;
1623 t2 = tmp;
1624 }
1625
1626 if (t1 > t_near) t_near = t1;
1627 if (t2 < t_far) t_far = t2;
1628
1629 if (t_far < 0.0f)
1630 return EINA_FALSE;
1631
1632 t1 = (box->p0.y - ray->org.y) / ray->dir.y;
1633 t2 = (box->p1.y - ray->org.y) / ray->dir.y;
1634
1635 if (t1 > t2)
1636 {
1637 Evas_Real tmp = t1;
1638 t1 = t2;
1639 t2 = tmp;
1640 }
1641
1642 if (t1 > t_near) t_near = t1;
1643 if (t2 < t_far) t_far = t2;
1644
1645 if ((t_near > t_far) || (t_far < 0.0f))
1646 return EINA_FALSE;
1647
1648 t1 = (box->p0.z - ray->org.z) / ray->dir.z;
1649 t2 = (box->p1.z - ray->org.z) / ray->dir.z;
1650
1651 if (t1 > t2)
1652 {
1653 Evas_Real tmp = t1;
1654 t1 = t2;
1655 t2 = tmp;
1656 }
1657
1658 if (t1 > t_near) t_near = t1;
1659 if (t2 < t_far) t_far = t2;
1660
1661 if ((t_near > t_far) || (t_far < 0.0f))
1662 return EINA_FALSE;
1663
1664 intersect = EINA_TRUE;
1665 }
1666 /* case when ray is parallel to one of axes */
1667 else
1668 {
1669 /* use two-dimensional version here */
1670 if (ray->dir.x == 0.0f)
1671 {
1672 if ((ray->org.x < box->p0.x) || (ray->org.x > box->p1.x))
1673 return EINA_FALSE;
1674 else
1675 {
1676 evas_vec2_set(&org2, ray->org.y, ray->org.z);
1677 evas_vec2_set(&dir2, ray->dir.y, ray->dir.z);
1678 evas_box2_set(&box2, box->p0.y, box->p0.z, box->p1.y, box->p1.z);
1679 intersect = evas_box2_intersect_2d(&box2, &org2, &dir2);
1680 }
1681 }
1682
1683 if (ray->dir.y == 0.0f)
1684 {
1685 if ((ray->org.y < box->p0.y) || (ray->org.y > box->p1.y))
1686 return EINA_FALSE;
1687 else
1688 {
1689 evas_vec2_set(&org2, ray->org.x, ray->org.z);
1690 evas_vec2_set(&dir2, ray->dir.x, ray->dir.z);
1691 evas_box2_set(&box2, box->p0.x, box->p0.z, box->p1.x, box->p1.z);
1692 intersect = evas_box2_intersect_2d(&box2, &org2, &dir2);
1693 }
1694 }
1695
1696 if (ray->dir.z == 0.0f)
1697 {
1698 if (ray->org.z < box->p0.z || ray->org.z > box->p1.z)
1699 return EINA_FALSE;
1700 else
1701 {
1702 evas_vec2_set(&org2, ray->org.x, ray->org.y);
1703 evas_vec2_set(&dir2, ray->dir.x, ray->dir.y);
1704 evas_box2_set(&box2, box->p0.x, box->p0.y, box->p1.x, box->p1.y);
1705 intersect = evas_box2_intersect_2d(&box2, &org2, &dir2);
1706 }
1707 }
1708 }
1709
1710 return intersect;
1711}
1712
1546static inline Evas_Real 1713static inline Evas_Real
1547evas_reciprocal_sqrt(Evas_Real x) 1714evas_reciprocal_sqrt(Evas_Real x)
1548{ 1715{