[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletCollision / Gimpact / btClipPolygon.h
1 #ifndef BT_CLIP_POLYGON_H_INCLUDED
2 #define BT_CLIP_POLYGON_H_INCLUDED
3
4 /*! \file btClipPolygon.h
5 \author Francisco Leon Najera
6 */
7 /*
8 This source file is part of GIMPACT Library.
9
10 For the latest info, see http://gimpact.sourceforge.net/
11
12 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
14
15
16 This software is provided 'as-is', without any express or implied warranty.
17 In no event will the authors be held liable for any damages arising from the use of this software.
18 Permission is granted to anyone to use this software for any purpose,
19 including commercial applications, and to alter it and redistribute it freely,
20 subject to the following restrictions:
21
22 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25 */
26
27 #include "LinearMath/btTransform.h"
28 #include "LinearMath/btGeometryUtil.h"
29
30 SIMD_FORCE_INLINE btScalar bt_distance_point_plane(const btVector4 &plane, const btVector3 &point)
31 {
32         return point.dot(plane) - plane[3];
33 }
34
35 /*! Vector blending
36 Takes two vectors a, b, blends them together*/
37 SIMD_FORCE_INLINE void bt_vec_blend(btVector3 &vr, const btVector3 &va, const btVector3 &vb, btScalar blend_factor)
38 {
39         vr = (1 - blend_factor) * va + blend_factor * vb;
40 }
41
42 //! This function calcs the distance from a 3D plane
43 SIMD_FORCE_INLINE void bt_plane_clip_polygon_collect(
44         const btVector3 &point0,
45         const btVector3 &point1,
46         btScalar dist0,
47         btScalar dist1,
48         btVector3 *clipped,
49         int &clipped_count)
50 {
51         bool _prevclassif = (dist0 > SIMD_EPSILON);
52         bool _classif = (dist1 > SIMD_EPSILON);
53         if (_classif != _prevclassif)
54         {
55                 btScalar blendfactor = -dist0 / (dist1 - dist0);
56                 bt_vec_blend(clipped[clipped_count], point0, point1, blendfactor);
57                 clipped_count++;
58         }
59         if (!_classif)
60         {
61                 clipped[clipped_count] = point1;
62                 clipped_count++;
63         }
64 }
65
66 //! Clips a polygon by a plane
67 /*!
68 *\return The count of the clipped counts
69 */
70 SIMD_FORCE_INLINE int bt_plane_clip_polygon(
71         const btVector4 &plane,
72         const btVector3 *polygon_points,
73         int polygon_point_count,
74         btVector3 *clipped)
75 {
76         int clipped_count = 0;
77
78         //clip first point
79         btScalar firstdist = bt_distance_point_plane(plane, polygon_points[0]);
80         ;
81         if (!(firstdist > SIMD_EPSILON))
82         {
83                 clipped[clipped_count] = polygon_points[0];
84                 clipped_count++;
85         }
86
87         btScalar olddist = firstdist;
88         for (int i = 1; i < polygon_point_count; i++)
89         {
90                 btScalar dist = bt_distance_point_plane(plane, polygon_points[i]);
91
92                 bt_plane_clip_polygon_collect(
93                         polygon_points[i - 1], polygon_points[i],
94                         olddist,
95                         dist,
96                         clipped,
97                         clipped_count);
98
99                 olddist = dist;
100         }
101
102         //RETURN TO FIRST  point
103
104         bt_plane_clip_polygon_collect(
105                 polygon_points[polygon_point_count - 1], polygon_points[0],
106                 olddist,
107                 firstdist,
108                 clipped,
109                 clipped_count);
110
111         return clipped_count;
112 }
113
114 //! Clips a polygon by a plane
115 /*!
116 *\param clipped must be an array of 16 points.
117 *\return The count of the clipped counts
118 */
119 SIMD_FORCE_INLINE int bt_plane_clip_triangle(
120         const btVector4 &plane,
121         const btVector3 &point0,
122         const btVector3 &point1,
123         const btVector3 &point2,
124         btVector3 *clipped  // an allocated array of 16 points at least
125 )
126 {
127         int clipped_count = 0;
128
129         //clip first point0
130         btScalar firstdist = bt_distance_point_plane(plane, point0);
131         ;
132         if (!(firstdist > SIMD_EPSILON))
133         {
134                 clipped[clipped_count] = point0;
135                 clipped_count++;
136         }
137
138         // point 1
139         btScalar olddist = firstdist;
140         btScalar dist = bt_distance_point_plane(plane, point1);
141
142         bt_plane_clip_polygon_collect(
143                 point0, point1,
144                 olddist,
145                 dist,
146                 clipped,
147                 clipped_count);
148
149         olddist = dist;
150
151         // point 2
152         dist = bt_distance_point_plane(plane, point2);
153
154         bt_plane_clip_polygon_collect(
155                 point1, point2,
156                 olddist,
157                 dist,
158                 clipped,
159                 clipped_count);
160         olddist = dist;
161
162         //RETURN TO FIRST  point0
163         bt_plane_clip_polygon_collect(
164                 point2, point0,
165                 olddist,
166                 firstdist,
167                 clipped,
168                 clipped_count);
169
170         return clipped_count;
171 }
172
173 #endif  // GIM_TRI_COLLISION_H_INCLUDED