/' Copyright (c) 2007 Scott Lembcke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 '/

'' Axis structure used by cpPolyShape.
type cpPolyShapeAxis
	'' normal
	as cpVect	n
	'' distance from origin
	as cpFloat	d
end type

' Convex polygon shape structure.
type cpPolyShape
	as cpShape	shape
	
	'' Vertex and axis lists.
	as integer	numVerts
	as cpVect ptr	verts
	as cpPolyShapeAxis ptr	axes

	'' Transformed vertex and axis lists.
	as cpVect ptr	tVert
	as cpPolyShapeAxis ptr	tAxes
end type

' Basic allocation functions.
declare function cpPolyShapeAlloc cdecl alias "cpPolyShapeAlloc" ( ) as cpPolyShape ptr
declare function cpPolyShapeInit cdecl alias "cpPolyShapeInit" ( byval as cpPolyShape ptr, byval as cpBody ptr, byval as integer, byval as cpVect ptr, byval as cpVect ) as cpPolyShape ptr
declare function cpPolyShapeNew cdecl alias "cpPolyShapeNew" ( byval as cpBody ptr, byval as integer, byval as cpVect ptr, byval as cpVect ) as cpShape ptr

declare function cpBoxShapeInit cdecl alias "cpBoxShapeInit" ( byval as cpPolyShape ptr, byval as cpBody ptr, byval as cpFloat, byval as cpFloat ) as cpPolyShape ptr
declare function cpBoxShapeNew cdecl alias "cpBoxShapeNew" ( byval as cpBody ptr, byval as cpFloat, byval as cpFloat ) as cpShape ptr

'' Check that a set of vertexes has a correct winding and that they are convex
declare function cpPolyValidate cdecl alias "cpPolyValidate" ( byval as cpVect ptr, byval as integer ) as cpBool

declare function cpPolyShapeGetNumVerts cdecl alias "cpPolyShapeGetNumVerts" ( byval as cpShape ptr ) as integer
declare function cpPolyShapeGetVert cdecl alias "cpPolyShapeGetVert" ( byval as cpShape ptr, byval as integer ) as cpVect


' Returns the minimum distance of the polygon to the axis.
function cpPolyShapeValueOnAxis ( byval poly as cpPolyShape ptr, byval n as cpVect, byval d as cpFloat ) as cpFloat
	dim as cpVect ptr	verts = poly->tVert
	dim as cpFloat		min = cpvdot( n, verts[0] )
	
	dim i as integer
	for i = 0 to poly->numVerts - 1
		min = cpfmin( min, cpvdot( n, verts[i] ) )
	next i
	return min - d
end function

'' Returns true if the polygon contains the vertex.
function cpPolyShapeContainsVerts ( byval poly as const cpPolyShape ptr, byval v as const cpVect ) as cpBool

    dim as cpPolyShapeAxis ptr axes = poly->tAxes
    dim as integer i
    
    for i=0 to poly->numVerts-1
		dim as cpFloat dist = cpvdot(axes[i].n, v) - axes[i].d
		if dist > 0.0 then return cpFalse
	next i

    return cpTrue

end function

'' Same as cpPolyShapeContainsVert() but ignores faces pointing away from the normal.
function cpPolyShapeContainsVertPartial( byval poly as const cpPolyShape ptr, byval v as const cpVect, byval n as const cpVect ) as cpBool
	dim as cpPolyShapeAxis ptr	axes = poly->tAxes

	dim i as integer	
	for i = 0 to poly->numVerts - 1
		if cpvdot( axes[i].n, n ) >= 0.0 then
			var dist = cpvdot( axes[i].n, v ) - axes[i].d
			if dist > 0.0 then return cpFalse
		end if
	next i
	
	return cpTrue
end function

