Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/bullet/BulletCollision/Gimpact/btClipPolygon.h @ 3825

Last change on this file since 3825 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 4.3 KB
RevLine 
[2431]1#ifndef BT_CLIP_POLYGON_H_INCLUDED
2#define BT_CLIP_POLYGON_H_INCLUDED
3
4/*! \file btClipPolygon.h
5\author Francisco Len Nßjera
6*/
7/*
8This source file is part of GIMPACT Library.
9
10For the latest info, see http://gimpact.sourceforge.net/
11
12Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13email: projectileman@yahoo.com
14
15
16This software is provided 'as-is', without any express or implied warranty.
17In no event will the authors be held liable for any damages arising from the use of this software.
18Permission is granted to anyone to use this software for any purpose,
19including commercial applications, and to alter it and redistribute it freely,
20subject to the following restrictions:
21
221. 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.
232. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
243. 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
31SIMD_FORCE_INLINE btScalar bt_distance_point_plane(const btVector4 & plane,const btVector3 &point)
32{
33        return point.dot(plane) - plane[3];
34}
35
36/*! Vector blending
37Takes two vectors a, b, blends them together*/
38SIMD_FORCE_INLINE void bt_vec_blend(btVector3 &vr, const btVector3 &va,const btVector3 &vb, btScalar blend_factor)
39{
40        vr = (1-blend_factor)*va + blend_factor*vb;
41}
42
43//! This function calcs the distance from a 3D plane
44SIMD_FORCE_INLINE void bt_plane_clip_polygon_collect(
45                                                const btVector3 & point0,
46                                                const btVector3 & point1,
47                                                btScalar dist0,
48                                                btScalar dist1,
49                                                btVector3 * clipped,
50                                                int & clipped_count)
51{
52        bool _prevclassif = (dist0>SIMD_EPSILON);
53        bool _classif = (dist1>SIMD_EPSILON);
54        if(_classif!=_prevclassif)
55        {
56                btScalar blendfactor = -dist0/(dist1-dist0);
57                bt_vec_blend(clipped[clipped_count],point0,point1,blendfactor);
58                clipped_count++;
59        }
60        if(!_classif)
61        {
62                clipped[clipped_count] = point1;
63                clipped_count++;
64        }
65}
66
67
68//! Clips a polygon by a plane
69/*!
70*\return The count of the clipped counts
71*/
72SIMD_FORCE_INLINE int bt_plane_clip_polygon(
73                                                const btVector4 & plane,
74                                                const btVector3 * polygon_points,
75                                                int polygon_point_count,
76                                                btVector3 * clipped)
77{
78    int clipped_count = 0;
79
80
81    //clip first point
82        btScalar firstdist = bt_distance_point_plane(plane,polygon_points[0]);;
83        if(!(firstdist>SIMD_EPSILON))
84        {
85                clipped[clipped_count] = polygon_points[0];
86                clipped_count++;
87        }
88
89        btScalar olddist = firstdist;
90        for(int i=1;i<polygon_point_count;i++)
91        {
92                btScalar dist = bt_distance_point_plane(plane,polygon_points[i]);
93
94                bt_plane_clip_polygon_collect(
95                                                polygon_points[i-1],polygon_points[i],
96                                                olddist,
97                                                dist,
98                                                clipped,
99                                                clipped_count);
100
101
102                olddist = dist;
103        }
104
105        //RETURN TO FIRST  point
106
107        bt_plane_clip_polygon_collect(
108                                        polygon_points[polygon_point_count-1],polygon_points[0],
109                                        olddist,
110                                        firstdist,
111                                        clipped,
112                                        clipped_count);
113
114        return clipped_count;
115}
116
117//! Clips a polygon by a plane
118/*!
119*\param clipped must be an array of 16 points.
120*\return The count of the clipped counts
121*/
122SIMD_FORCE_INLINE int bt_plane_clip_triangle(
123                                                const btVector4 & plane,
124                                                const btVector3 & point0,
125                                                const btVector3 & point1,
126                                                const btVector3& point2,
127                                                btVector3 * clipped // an allocated array of 16 points at least
128                                                )
129{
130    int clipped_count = 0;
131
132    //clip first point0
133        btScalar firstdist = bt_distance_point_plane(plane,point0);;
134        if(!(firstdist>SIMD_EPSILON))
135        {
136                clipped[clipped_count] = point0;
137                clipped_count++;
138        }
139
140        // point 1
141        btScalar olddist = firstdist;
142        btScalar dist = bt_distance_point_plane(plane,point1);
143
144        bt_plane_clip_polygon_collect(
145                                        point0,point1,
146                                        olddist,
147                                        dist,
148                                        clipped,
149                                        clipped_count);
150
151        olddist = dist;
152
153
154        // point 2
155        dist = bt_distance_point_plane(plane,point2);
156
157        bt_plane_clip_polygon_collect(
158                                        point1,point2,
159                                        olddist,
160                                        dist,
161                                        clipped,
162                                        clipped_count);
163        olddist = dist;
164
165
166
167        //RETURN TO FIRST  point0
168        bt_plane_clip_polygon_collect(
169                                        point2,point0,
170                                        olddist,
171                                        firstdist,
172                                        clipped,
173                                        clipped_count);
174
175        return clipped_count;
176}
177
178
179
180
181
182#endif // GIM_TRI_COLLISION_H_INCLUDED
Note: See TracBrowser for help on using the repository browser.