From 4d2478b322c87f3d7621d42e820f3cff3d8e342f Mon Sep 17 00:00:00 2001 From: Patrick Meehan Date: Tue, 31 May 2016 18:40:10 -0700 Subject: [PATCH] wow! such hull! so convex! wow! --- samples/convex-hull-edit/main.lua | 70 +++++++++ samples/convex-hull/main.lua | 87 +++++++++++ src/moai-sim/MOAIRegion.cpp | 144 +++++++++++++++++- src/moai-sim/MOAIRegion.h | 11 +- src/zl-util/ZLCleanup.h | 45 ++++++ src/zl-util/ZLHull2D.cpp | 111 ++++++++++++++ src/zl-util/ZLHull2D.h | 37 +++++ src/zl-util/ZLLeanArray.h | 8 + src/zl-util/ZLPolygon2D.h | 7 +- src/zl-util/ZLResult.h | 22 +++ src/zl-util/ZLVec2D.h | 16 +- src/zl-util/headers.h | 2 + .../libmoai/libmoai.xcodeproj/project.pbxproj | 18 +++ 13 files changed, 571 insertions(+), 7 deletions(-) create mode 100644 samples/convex-hull-edit/main.lua create mode 100644 samples/convex-hull/main.lua create mode 100644 src/zl-util/ZLCleanup.h create mode 100644 src/zl-util/ZLHull2D.cpp create mode 100644 src/zl-util/ZLHull2D.h diff --git a/samples/convex-hull-edit/main.lua b/samples/convex-hull-edit/main.lua new file mode 100644 index 0000000000..b35bcde22c --- /dev/null +++ b/samples/convex-hull-edit/main.lua @@ -0,0 +1,70 @@ +---------------------------------------------------------------- +-- Copyright (c) 2010-2011 Zipline Games, Inc. +-- All Rights Reserved. +-- http://getmoai.com +---------------------------------------------------------------- + +MOAISim.openWindow ( "test", 512, 512 ) + +viewport = MOAIViewport.new () +viewport:setSize ( 512, 512 ) +viewport:setScale ( 512, 512 ) + +layer = MOAILayer2D.new () +layer:setViewport ( viewport ) +layer:setClearColor ( 1, 1, 1, 1 ) +MOAISim.pushRenderPass ( layer ) + +drawPoints = function ( stream ) + + local total = stream:getLength () / 8 + stream:seek ( 0 ) + + MOAIGfxMgr.setPenColor ( 0, 0, 1, 1 ) + + for i = 1, total do + + local x = stream:readFloat () + local y = stream:readFloat () + + MOAIDraw.fillCircle ( x, y, 2 ) + end +end + +points = MOAIMemStream.new () +region = MOAIRegion.new () + +onDraw = function () + + drawPoints ( points ) + region:drawDebug () +end + +onMouseLeftEvent = function ( down ) + + if down then + + local x, y = layer:wndToWorld ( MOAIInputMgr.device.pointer:getLoc ()) + + points:seek ( points:getLength ()) + points:writeFloat ( x ) + points:writeFloat ( y ) + points:seek ( 0 ) + + if points:getLength () / 8 >= 3 then + region:convexHull ( points ) + end + end +end + +onMouseRightEvent = function ( down ) + if down then + points:discardAll () + region:clear () + end +end + +MOAIInputMgr.device.mouseLeft:setCallback ( onMouseLeftEvent ) +MOAIInputMgr.device.mouseRight:setCallback ( onMouseRightEvent ) + +layer:setOverlayTable ({ onDraw }) diff --git a/samples/convex-hull/main.lua b/samples/convex-hull/main.lua new file mode 100644 index 0000000000..e1c7f08a03 --- /dev/null +++ b/samples/convex-hull/main.lua @@ -0,0 +1,87 @@ +---------------------------------------------------------------- +-- Copyright (c) 2010-2011 Zipline Games, Inc. +-- All Rights Reserved. +-- http://getmoai.com +---------------------------------------------------------------- + +MOAISim.openWindow ( "test", 512, 512 ) + +viewport = MOAIViewport.new () +viewport:setSize ( 512, 512 ) +viewport:setScale ( 512, 512 ) + +layer = MOAILayer2D.new () +layer:setViewport ( viewport ) +layer:setClearColor ( 1, 1, 1, 1 ) +MOAISim.pushRenderPass ( layer ) + +drawPoints = function ( stream ) + + local total = stream:getLength () / 8 + stream:seek ( 0 ) + + MOAIGfxMgr.setPenColor ( 0, 0, 1, 1 ) + + for i = 1, total do + + local x = stream:readFloat () + local y = stream:readFloat () + + MOAIDraw.fillCircle ( x, y, 2 ) + end +end + +generatePoints = function ( stream, total, xMin, yMin, xMax, yMax ) + + for i = 1, total do + + local r = math.pow ( math.random (), 2 ) + local a = math.random () * math.pi * 2 + + local hw = ( xMax - xMin ) / 2 + local hh = ( yMax - yMin ) / 2 + + local x = xMin + hw + ( hw * ( math.cos ( a ) * r )) + local y = yMin + hh + ( hh * ( math.sin ( a ) * r )) + + stream:writeFloat ( x ) + stream:writeFloat ( y ) + end + + stream:seek ( 0 ) +end + +points = MOAIMemStream.new () +region = MOAIRegion.new () + +generateHull = function () + + generatePoints ( points, 100, -172, -172, 172, 172 ) + region:convexHull ( points ) +end + +onDraw = function () + + drawPoints ( points ) + region:drawDebug () +end + +onMouseLeftEvent = function ( down ) + if down then + points = MOAIMemStream.new () + generateHull () + end +end + +onMouseRightEvent = function ( down ) + if down then + generateHull () + end +end + +MOAIInputMgr.device.mouseLeft:setCallback ( onMouseLeftEvent ) +MOAIInputMgr.device.mouseRight:setCallback ( onMouseRightEvent ) + +generateHull () + +layer:setOverlayTable ({ onDraw }) diff --git a/src/moai-sim/MOAIRegion.cpp b/src/moai-sim/MOAIRegion.cpp index 6cd8f9127d..87443ecb4c 100644 --- a/src/moai-sim/MOAIRegion.cpp +++ b/src/moai-sim/MOAIRegion.cpp @@ -54,6 +54,48 @@ int MOAIRegion::_boolean ( lua_State* L ) { return 0; } +//----------------------------------------------------------------// +// TODO: doxygen +int MOAIRegion::_clear ( lua_State* L ) { + MOAI_LUA_SETUP ( MOAIRegion, "U" ) + + self->Clear (); + return 0; +} + +//----------------------------------------------------------------// +// TODO: doxygen +int MOAIRegion::_convexHull ( lua_State* L ) { + MOAI_LUA_SETUP ( MOAIRegion, "U" ) + + MOAIStream* stream = state.GetLuaObject < MOAIStream >( 2, true ); + + if ( stream ) { + + size_t resetCursor = stream->GetCursor (); + + size_t nVerts = state.GetValue < u32 >( 3, 0 ); + + if ( nVerts == 0 ) { + nVerts = stream->GetLength () / ZLHull2D::VERTEX_SIZE; + stream->Seek ( 0, SEEK_SET ); + } + + if ( nVerts > 0 ) { + + ZLSizeResult result = self->ConvexHull ( *stream, nVerts ); + + if ( result.mCode == ZL_OK ) { + state.Push (( u32 )result.mValue ); + return 1; + } + } + + stream->Seek ( resetCursor, SEEK_SET ); + } + return 0; +} + //----------------------------------------------------------------// int MOAIRegion::_copy ( lua_State* L ) { MOAI_LUA_SETUP ( MOAIRegion, "UU" ) @@ -205,6 +247,25 @@ int MOAIRegion::_getTriangles ( lua_State* L ) { return 3; } +//----------------------------------------------------------------// +// TODO: doxygen +int MOAIRegion::_getVertices ( lua_State* L ) { + MOAI_LUA_SETUP ( MOAIRegion, "U" ) + + MOAIStream* stream = state.GetLuaObject < MOAIStream >( 2, true ); + + if ( stream ) { + + ZLSizeResult result = self->GetVertices ( *stream ); + + if ( result.mCode == ZL_OK ) { + state.Push (( u32 )result.mValue ); + return 1; + } + } + return 0; +} + //----------------------------------------------------------------// // TODO: doxygen int MOAIRegion::_pad ( lua_State* L ) { @@ -497,6 +558,12 @@ void MOAIRegion::BooleanXor ( const MOAIRegion& regionA, const MOAIRegion& regio this->CombineAndTesselate ( regionA, regionB, TESS_WINDING_ODD ); } +//----------------------------------------------------------------// +void MOAIRegion::Clear () { + + this->mPolygons.Clear (); +} + //----------------------------------------------------------------// int MOAIRegion::CombineAndTesselate ( const MOAIRegion& regionA, const MOAIRegion& regionB, int windingRule ) { @@ -515,6 +582,43 @@ int MOAIRegion::CombineAndTesselate ( const MOAIRegion& regionA, const MOAIRegio return error; } +//----------------------------------------------------------------// +ZLSizeResult MOAIRegion::ConvexHull ( ZLStream& vtxStream, size_t nVerts ) { + + ZLCleanup < MOAIRegion > cleanup ( this, &MOAIRegion::Clear ); + + this->Clear (); + + ZLMemStream hull; + ZLSizeResult hullSize = ZLHull2D::MonotoneChain ( hull, vtxStream, nVerts, ZLHull2D::SORT_CSTDLIB ); + + ZL_HANDLE_ERROR_CODE ( hullSize.mCode, ZL_RETURN_SIZE_RESULT ( 0, CODE )) + + nVerts = hullSize; + if ( nVerts == 0 ) ZL_RETURN_SIZE_RESULT ( 0, ZL_ERROR ) + + ZL_HANDLE_ERROR_CODE ( this->ReservePolygons ( 1 ), ZL_RETURN_SIZE_RESULT ( 0, CODE )) + ZL_HANDLE_ERROR_CODE ( this->ReserveVertices ( 0, nVerts ), ZL_RETURN_SIZE_RESULT ( 0, CODE )); + + hull.Seek ( 0, SEEK_SET ); + + for ( size_t i = 0; i < nVerts; ++i ) { + + ZLResult < float > x = hull.Read < float >( 0.0f ); + ZL_HANDLE_ERROR_CODE ( x.mCode, ZL_RETURN_SIZE_RESULT ( 0, CODE )); + + ZLResult < float > y = hull.Read < float >( 0.0f ); + ZL_HANDLE_ERROR_CODE ( y.mCode, ZL_RETURN_SIZE_RESULT ( 0, CODE )); + + this->mPolygons [ 0 ].SetVert ( i, ZLVec2D ( x, y )); + } + + this->Bless (); + + cleanup.Skip (); + ZL_RETURN_SIZE_RESULT ( nVerts, ZL_OK ) +} + //----------------------------------------------------------------// void MOAIRegion::Copy ( const MOAIRegion& region ) { @@ -788,6 +892,28 @@ u32 MOAIRegion::GetTriangles ( MOAIVertexFormat& format, MOAIVertexBuffer& vtxBu return 0; } +//----------------------------------------------------------------// +ZLSizeResult MOAIRegion::GetVertices ( ZLStream& vtxStream ) const { + + size_t count = 0; + + size_t nPolys = this->mPolygons.Size (); + for ( size_t i = 0; i < nPolys; ++i ) { + + const ZLPolygon2D& poly = this->mPolygons [ i ]; + + size_t nVerts = poly.GetSize (); + for ( size_t j = 0; j < nVerts; ++j ) { + + const ZLVec2D& v = poly.GetVertex ( j ); + vtxStream.Write < float >( v.mX ); + vtxStream.Write < float >( v.mY ); + count++; + } + } + ZL_RETURN_SIZE_RESULT ( count, ZL_OK ) +} + //----------------------------------------------------------------// MOAIRegion::MOAIRegion () { @@ -917,6 +1043,8 @@ void MOAIRegion::RegisterLuaFuncs ( MOAILuaState& state ) { { "append", _append }, { "bless", _bless }, { "boolean", _boolean }, + { "clear", _clear }, + { "convexHull", _convexHull }, { "copy", _copy }, { "countPolygons", _countPolygons }, { "cull", _cull }, @@ -925,6 +1053,7 @@ void MOAIRegion::RegisterLuaFuncs ( MOAILuaState& state ) { { "getDistance", _getDistance }, { "getPolygon", _getPolygon }, { "getTriangles", _getTriangles }, + { "getVertices", _getVertices }, { "pad", _pad }, { "pointInside", _pointInside }, { "print", _print }, @@ -944,9 +1073,20 @@ void MOAIRegion::RegisterLuaFuncs ( MOAILuaState& state ) { } //----------------------------------------------------------------// -void MOAIRegion::ReservePolygons ( size_t size ) { +ZLResultCode MOAIRegion::ReservePolygons ( size_t size ) { + + return this->mPolygons.Init ( size ); +} + +//----------------------------------------------------------------// +ZLResultCode MOAIRegion::ReserveVertices ( size_t idx, size_t size ) { - this->mPolygons.Init ( size ); + if ( idx >= this->mPolygons.Size ()) { + + ZLResultCode result = this->mPolygons.Grow ( idx ); + if ( result != ZL_OK ) return result; + } + return this->mPolygons [ idx ].ReserveVertices ( size ); } //----------------------------------------------------------------// diff --git a/src/moai-sim/MOAIRegion.h b/src/moai-sim/MOAIRegion.h index d56e31d108..dbe3165af2 100644 --- a/src/moai-sim/MOAIRegion.h +++ b/src/moai-sim/MOAIRegion.h @@ -23,15 +23,18 @@ class MOAIRegion : //----------------------------------------------------------------// static int _append ( lua_State* L ); static int _bless ( lua_State* L ); + static int _boolean ( lua_State* L ); + static int _clear ( lua_State* L ); + static int _convexHull ( lua_State* L ); static int _copy ( lua_State* L ); static int _countPolygons ( lua_State* L ); static int _cull ( lua_State* L ); - static int _boolean ( lua_State* L ); static int _drawDebug ( lua_State* L ); static int _edge ( lua_State* L ); static int _getDistance ( lua_State* L ); static int _getPolygon ( lua_State* L ); static int _getTriangles ( lua_State* L ); + static int _getVertices ( lua_State* L ); static int _pad ( lua_State* L ); static int _pointInside ( lua_State* L ); static int _print ( lua_State* L ); @@ -75,7 +78,9 @@ class MOAIRegion : void BooleanNot ( const MOAIRegion& regionA, const MOAIRegion& regionB ); void BooleanOr ( const MOAIRegion& regionA, const MOAIRegion& regionB ); void BooleanXor ( const MOAIRegion& regionA, const MOAIRegion& regionB ); + void Clear (); int CombineAndTesselate ( const MOAIRegion& regionA, const MOAIRegion& regionB, int windingRule ); + ZLSizeResult ConvexHull ( ZLStream& vtxStream, size_t nVerts ); void Copy ( const MOAIRegion& region ); void Copy ( const SafeTesselator& tess ); void Copy ( const SafeTesselator& tess, ZLAffine2D& transform ); @@ -89,6 +94,7 @@ class MOAIRegion : u32 GetTriangles ( SafeTesselator& tess ) const; u32 GetTriangles ( MOAIVertexFormat& format, ZLStream& vtxStream, ZLStream& idxStream ) const; u32 GetTriangles ( MOAIVertexFormat& format, MOAIVertexBuffer& vtxBuffer, MOAIIndexBuffer& idxBuffer, u32 idxSizeInBytex ) const; + ZLSizeResult GetVertices ( ZLStream& vtxStream ) const; MOAIRegion (); ~MOAIRegion (); void Pad ( const MOAIRegion& region, float pad ); @@ -96,7 +102,8 @@ class MOAIRegion : void Print () const; void RegisterLuaClass ( MOAILuaState& state ); void RegisterLuaFuncs ( MOAILuaState& state ); - void ReservePolygons ( size_t size ); + ZLResultCode ReservePolygons ( size_t size ); + ZLResultCode ReserveVertices ( size_t idx, size_t size ); void ReverseWinding ( const MOAIRegion& region ); void SerializeIn ( MOAILuaState& state, MOAIDeserializer& serializer ); void SerializeOut ( MOAILuaState& state, MOAISerializer& serializer ); diff --git a/src/zl-util/ZLCleanup.h b/src/zl-util/ZLCleanup.h new file mode 100644 index 0000000000..2ee1427918 --- /dev/null +++ b/src/zl-util/ZLCleanup.h @@ -0,0 +1,45 @@ +// Copyright (c) 2010-2011 Zipline Games, Inc. All Rights Reserved. +// http://getmoai.com + +#ifndef ZLCLEANUP_H +#define ZLCLEANUP_H + +//================================================================// +// ZLCleanup +//================================================================// +template < typename TYPE > +class ZLCleanup { +protected: + + typedef void ( TYPE::*SelectorType )(); + + TYPE* mTarget; + SelectorType mSelector; + +public: + + //----------------------------------------------------------------// + virtual void Skip () { + this->mTarget = 0; + this->mSelector = 0; + } + + //----------------------------------------------------------------// + ZLCleanup ( TYPE* target, SelectorType selector ) : + mTarget ( target ), + mSelector ( selector ) { + + assert ( target ); + assert ( selector ); + } + + //----------------------------------------------------------------// + ~ZLCleanup () { + + if ( this->mTarget && this->mSelector ) { + ( this->mTarget->*this->mSelector )(); + } + } +}; + +#endif diff --git a/src/zl-util/ZLHull2D.cpp b/src/zl-util/ZLHull2D.cpp new file mode 100644 index 0000000000..42ce68e20f --- /dev/null +++ b/src/zl-util/ZLHull2D.cpp @@ -0,0 +1,111 @@ +// Copyright (c) 2010-2011 Zipline Games, Inc. All Rights Reserved. +// http://getmoai.com + +#include "pch.h" +#include +#include +#include +#include +#include + +typedef ZLRadixKey32 < ZLVec2D > ZLRadixVec2D; + +//================================================================// +// ZLHull2D +//================================================================// + +//----------------------------------------------------------------// +ZLSizeResult ZLHull2D::MonotoneChain ( ZLVec2D* hull, ZLVec2D* points, size_t nPoints, u32 sort ) { + + size_t nHull = 0; + size_t size = nPoints * sizeof ( ZLVec2D ); + + switch ( sort ) { + + case SORT_RADIX: { + + ZL_DECLARE_SCOPED_BUFFER ( radixBuffer, ZLRadixVec2D, 2 * size, 1024 ) + if ( !radixBuffer ) ZL_RETURN_SIZE_RESULT ( 0, ZL_ALLOCATION_ERROR ); + + ZLRadixVec2D* swapBuffer = ( ZLRadixVec2D* )(( size_t )radixBuffer + size ); + + for ( size_t i = 0; i < nPoints; ++i ) { + const ZLVec2D& p = points [ i ]; + radixBuffer [ i ].mData = p; + radixBuffer [ i ].mKey = ZLFloat::FloatToIntKey (( p.mX * ( float )nPoints ) + p.mY ); // sort by x then y + } + + radixBuffer = RadixSort32 < ZLRadixVec2D >( radixBuffer, swapBuffer, nPoints ); + + for ( size_t i = 0; i < nPoints; ++i ) { + points [ i ] = radixBuffer [ i ].mData; + } + return ZLHull2D::MonotoneChain ( hull, points, nPoints, SORT_NONE ); + } + + case SORT_CSTDLIB: { + + qsort ( points, nPoints, sizeof ( ZLVec2D ), ZLHull2D::MonotoneChainComp ); + return ZLHull2D::MonotoneChain ( hull, points, nPoints, SORT_NONE ); + } + + case SORT_NONE: + default: { + + for ( size_t i = 0; i < nPoints; i++ ) { + while ( nHull >= 2 && hull [ nHull - 2 ].CrossJoint ( hull [ nHull - 1 ], points [ i ]) <= 0 ) nHull--; + hull [ nHull++ ] = points [ i ]; + } + + for ( size_t i = nPoints - 2, t = nHull + 1; i != ( size_t )-1; i-- ) { + while ( nHull >= t && hull [ nHull - 2 ].CrossJoint ( hull [ nHull - 1 ], points [ i ]) <= 0 ) nHull--; + hull [ nHull++ ] = points [ i ]; + } + } + } + + ZL_RETURN_SIZE_RESULT ( nHull - 1, ZL_OK ); +} + +//----------------------------------------------------------------// +ZLSizeResult ZLHull2D::MonotoneChain ( ZLStream& output, ZLStream& input, size_t nPoints, u32 sort ) { + + u32 resultCode = ZL_OK; + size_t nHull = 0; + + size_t size = nPoints * sizeof ( ZLVec2D ); + + ZL_DECLARE_SCOPED_BUFFER ( points, ZLVec2D, size, 512 ) + ZL_DECLARE_SCOPED_BUFFER ( hull, ZLVec2D, 2 * size, 512 ) + + if ( !( points && hull )) ZL_RETURN_SIZE_RESULT ( 0, ZL_ALLOCATION_ERROR ) + + ZLSizeResult readResult = input.ReadBytes ( points, size ); + ZL_RETURN_SIZE_RESULT_IF_NOT_EQUAL ( readResult, size, 0, ZL_RANGE_ERROR ) + + ZLSizeResult chainResult = ZLHull2D::MonotoneChain ( hull, points, nPoints, sort ); + + if ( chainResult.mCode == ZL_OK ) { + + size = chainResult.mValue * sizeof ( ZLVec2D ); + + ZLSizeResult writeResult = output.WriteBytes ( hull, size ); + ZL_RETURN_SIZE_RESULT_IF_NOT_EQUAL ( writeResult, size, 0, ZL_RANGE_ERROR ) + } + return chainResult; +} + +//----------------------------------------------------------------// +int ZLHull2D::MonotoneChainComp ( const void* p1, const void* p2 ) { + + const ZLVec2D* v0 = ( ZLVec2D* )p1; + const ZLVec2D* v1 = ( ZLVec2D* )p2; + + if ( v0->mX < v1->mX ) return -1; + if ( v0->mX > v1->mX ) return 1; + + if ( v0->mY < v1->mY ) return -1; + if ( v0->mY > v1->mY ) return 1; + + return 0; +} diff --git a/src/zl-util/ZLHull2D.h b/src/zl-util/ZLHull2D.h new file mode 100644 index 0000000000..494aba0962 --- /dev/null +++ b/src/zl-util/ZLHull2D.h @@ -0,0 +1,37 @@ +// Copyright (c) 2010-2011 Zipline Games, Inc. All Rights Reserved. +// http://getmoai.com + +#ifndef ZLHULL_H +#define ZLHULL_H + +#include +#include + +class ZLStream; + +//================================================================// +// ZLHull2D +//================================================================// +class ZLHull2D { +private: + + //----------------------------------------------------------------// + static int MonotoneChainComp ( const void* p1, const void* p2 ); + +public: + + typedef float VertexComponentType; + static const size_t VERTEX_SIZE = 2 * sizeof ( VertexComponentType ); + + enum { + SORT_NONE, + SORT_RADIX, + SORT_CSTDLIB, + }; + + //----------------------------------------------------------------// + static ZLSizeResult MonotoneChain ( ZLVec2D* hull, ZLVec2D* points, size_t nPoints, u32 sort = SORT_CSTDLIB ); // hull buffer *must* be at least 2x the size of points + static ZLSizeResult MonotoneChain ( ZLStream& output, ZLStream& input, size_t nPoints, u32 sort = SORT_CSTDLIB ); +}; + +#endif diff --git a/src/zl-util/ZLLeanArray.h b/src/zl-util/ZLLeanArray.h index 5068577075..4cce2351d5 100644 --- a/src/zl-util/ZLLeanArray.h +++ b/src/zl-util/ZLLeanArray.h @@ -6,6 +6,14 @@ #include +#define ZL_DECLARE_SCOPED_BUFFER(name,type,size,stackAllocMax) \ + ZLLeanArray < u8 > _##name##_array; \ + type* name = ( type* )( size <= stackAllocMax ? alloca ( size ) : 0 ); \ + if ( !name ) { \ + _##name##_array.Init ( size ); \ + name = ( type* )_##name##_array.Data (); \ + } + //================================================================// // ZLLeanArray //================================================================// diff --git a/src/zl-util/ZLPolygon2D.h b/src/zl-util/ZLPolygon2D.h index d4362931a5..d156055b51 100644 --- a/src/zl-util/ZLPolygon2D.h +++ b/src/zl-util/ZLPolygon2D.h @@ -107,6 +107,9 @@ class ZLMetaPolygon2D { else if ( z < 0.0f ) { countNegative++; } + else { + continue; + } // we're going to brute force this here for now // TODO: check number of edges and do something efficient (Shamos-Hoey, for example) @@ -394,10 +397,10 @@ class ZLMetaPolygon2D { } //----------------------------------------------------------------// - void ReserveVertices ( size_t total ) { + ZLResultCode ReserveVertices ( size_t total ) { - this->mVertices.Init ( total ); this->mInfo = POLY_UNKNOWN; + return this->mVertices.Init ( total ); } //----------------------------------------------------------------// diff --git a/src/zl-util/ZLResult.h b/src/zl-util/ZLResult.h index 8e33f637a7..baf6a8b18e 100644 --- a/src/zl-util/ZLResult.h +++ b/src/zl-util/ZLResult.h @@ -21,6 +21,28 @@ #define ZL_RETURN_SIZE_RESULT(value,code) ZL_RETURN_RESULT ( size_t, value, code ) #define ZL_RETURN_STRING_RESULT(value,code) ZL_RETURN_RESULT ( STLString, value, code ) +#define ZL_HANDLE_ERROR_CODE(code,handler) { \ + ZLResultCode CODE = code; \ + if ( CODE != ZL_OK ) { \ + handler; \ + } \ +} + +#define ZL_HANDLE_ERROR_RESULT(result,handler) ZL_HANDLE_ERROR_CODE ( result.mCode, handler ) + +#define ZL_RETURN_IF_NOT_EQUAL(result,expected,type,value,code) { \ + if ( result.mCode != ZL_OK ) ZL_RETURN_RESULT ( type, value, result.mCode ) \ + if ( result.mValue != expected ) ZL_RETURN_RESULT ( type, value, code ) \ +} + +#define ZL_RETURN_BOOL_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, bool, value, code ) +#define ZL_RETURN_CHAR_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, u8, value, code ) +#define ZL_RETURN_DOUBLE_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, double, value, code ) +#define ZL_RETURN_FLOAT_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, float, value, code ) +#define ZL_RETURN_INT_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, int, value, code ) +#define ZL_RETURN_SIZE_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, size_t, value, code ) +#define ZL_RETURN_STRING_RESULT_IF_NOT_EQUAL(result,expected,value,code) ZL_RETURN_IF_NOT_EQUAL ( result, expected, STLString, value, code ) + enum { ZL_OK, ZL_ERROR, diff --git a/src/zl-util/ZLVec2D.h b/src/zl-util/ZLVec2D.h index 12f1521375..82123d1c5f 100644 --- a/src/zl-util/ZLVec2D.h +++ b/src/zl-util/ZLVec2D.h @@ -93,7 +93,7 @@ class ZLMetaVec2D { } //----------------------------------------------------------------// - // V = V x vec + // V x vec TYPE Cross ( const ZLMetaVec2D < TYPE >& vec ) const { return ( mX * vec.mY ) - ( mY * vec.mX ); @@ -105,6 +105,20 @@ class ZLMetaVec2D { return ( v0.mX * v1.mY ) - ( v0.mY * v1.mX ); } + //----------------------------------------------------------------// + // ( v0 - V ) x ( v1 - V ) + TYPE CrossJoint ( const ZLMetaVec2D < TYPE >& v0, const ZLMetaVec2D < TYPE >& v1 ) { + + return (( v0.mX - mX ) * ( v1.mY - mY )) - (( v0.mY - mY ) * ( v1.mX - mX )); + } + + //----------------------------------------------------------------// + // ( v1 - V0 ) x ( v2 - V0 ) + static TYPE CrossJoint ( const ZLMetaVec2D < TYPE >& v0, const ZLMetaVec2D < TYPE >& v1, const ZLMetaVec2D < TYPE >& v2 ) { + + return (( v1.mX - v0.mX ) * ( v2.mY - v0.mY )) - (( v1.mY - v0.mY ) * ( v2.mX - v0.mX )); + } + //----------------------------------------------------------------// bool Compare ( const ZLMetaVec2D < TYPE >& point ) { diff --git a/src/zl-util/headers.h b/src/zl-util/headers.h index 629129f8ed..82c1af8d14 100644 --- a/src/zl-util/headers.h +++ b/src/zl-util/headers.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,7 @@ #include #include #include +#include #include #include #include diff --git a/xcode/libmoai/libmoai.xcodeproj/project.pbxproj b/xcode/libmoai/libmoai.xcodeproj/project.pbxproj index 49645440be..cea3464e2c 100644 --- a/xcode/libmoai/libmoai.xcodeproj/project.pbxproj +++ b/xcode/libmoai/libmoai.xcodeproj/project.pbxproj @@ -4873,6 +4873,12 @@ CDDAF4671C2FC5AA00F10946 /* ZLSample.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDAF4631C2F3D7C00F10946 /* ZLSample.h */; }; CDDAF4681C2FC5AD00F10946 /* ZLSample.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDDAF4621C2F3D7C00F10946 /* ZLSample.cpp */; }; CDDAF4691C2FC5AD00F10946 /* ZLSample.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDAF4631C2F3D7C00F10946 /* ZLSample.h */; }; + CDDB553A1CF7C57F009E82D8 /* ZLHull2D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDDB55381CF7C57F009E82D8 /* ZLHull2D.cpp */; }; + CDDB553B1CF7C57F009E82D8 /* ZLHull2D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDDB55381CF7C57F009E82D8 /* ZLHull2D.cpp */; }; + CDDB553C1CF7C57F009E82D8 /* ZLHull2D.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDB55391CF7C57F009E82D8 /* ZLHull2D.h */; }; + CDDB553D1CF7C57F009E82D8 /* ZLHull2D.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDB55391CF7C57F009E82D8 /* ZLHull2D.h */; }; + CDDB553F1CFC15E9009E82D8 /* ZLCleanup.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDB553E1CFC15E9009E82D8 /* ZLCleanup.h */; }; + CDDB55401CFC15E9009E82D8 /* ZLCleanup.h in Headers */ = {isa = PBXBuildFile; fileRef = CDDB553E1CFC15E9009E82D8 /* ZLCleanup.h */; }; CDE36E501A5F4E340095E863 /* MOAIImageFormatMgr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDE36E4E1A5F4E340095E863 /* MOAIImageFormatMgr.cpp */; }; CDE36E511A5F4E340095E863 /* MOAIImageFormatMgr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDE36E4E1A5F4E340095E863 /* MOAIImageFormatMgr.cpp */; }; CDE36E521A5F4E340095E863 /* MOAIImageFormatMgr.h in Headers */ = {isa = PBXBuildFile; fileRef = CDE36E4F1A5F4E340095E863 /* MOAIImageFormatMgr.h */; }; @@ -8021,6 +8027,9 @@ CDD781CF1A11635F00AD133E /* MOAIMotionMgrIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MOAIMotionMgrIOS.mm; path = "moai-ios-motion/MOAIMotionMgrIOS.mm"; sourceTree = ""; }; CDDAF4621C2F3D7C00F10946 /* ZLSample.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ZLSample.cpp; sourceTree = ""; }; CDDAF4631C2F3D7C00F10946 /* ZLSample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLSample.h; sourceTree = ""; }; + CDDB55381CF7C57F009E82D8 /* ZLHull2D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ZLHull2D.cpp; sourceTree = ""; }; + CDDB55391CF7C57F009E82D8 /* ZLHull2D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLHull2D.h; sourceTree = ""; }; + CDDB553E1CFC15E9009E82D8 /* ZLCleanup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZLCleanup.h; sourceTree = ""; }; CDDD30921574AE0000C410A0 /* libmoai-ios-fmod-designer.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libmoai-ios-fmod-designer.a"; sourceTree = BUILT_PRODUCTS_DIR; }; CDDD30A51574AE0F00C410A0 /* libmoai-osx-fmod-designer.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libmoai-osx-fmod-designer.a"; sourceTree = BUILT_PRODUCTS_DIR; }; CDE36E4E1A5F4E340095E863 /* MOAIImageFormatMgr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MOAIImageFormatMgr.cpp; sourceTree = ""; }; @@ -9200,6 +9209,7 @@ 89BE68A9173974FC00DFE837 /* ZLBinarySearch.h */, CD41F2FD1A20115E008E3420 /* ZLBitBuffer.cpp */, CD41F2FE1A20115E008E3420 /* ZLBitBuffer.h */, + CDDB553E1CFC15E9009E82D8 /* ZLCleanup.h */, 89BE68B0173974FC00DFE837 /* ZLColor.cpp */, 89BE68B1173974FC00DFE837 /* ZLColor.h */, CD2FB19A1C473AB4005F8D8D /* ZLCopyOnWrite.cpp */, @@ -9946,6 +9956,8 @@ CDE4400F1AC0D27600C745DB /* ZLFrustum.h */, CDE440101AC0D27600C745DB /* ZLFrustumFitter.cpp */, CDE440111AC0D27600C745DB /* ZLFrustumFitter.h */, + CDDB55381CF7C57F009E82D8 /* ZLHull2D.cpp */, + CDDB55391CF7C57F009E82D8 /* ZLHull2D.h */, 89BE68E6173974FC00DFE837 /* ZLInterpolate.cpp */, 89BE68E7173974FC00DFE837 /* ZLInterpolate.h */, 89BE68E8173974FC00DFE837 /* ZLIntersect.cpp */, @@ -14137,6 +14149,7 @@ 89BE6A09173974FD00DFE837 /* ZLLeanList.h in Headers */, 89BE6A0B173974FD00DFE837 /* ZLLeanPool.h in Headers */, 89BE6A0D173974FD00DFE837 /* ZLLeanStack.h in Headers */, + CDDB553C1CF7C57F009E82D8 /* ZLHull2D.h in Headers */, 89BE6A11173974FD00DFE837 /* ZLLexStream.h in Headers */, 89BE6A13173974FD00DFE837 /* ZLList.h in Headers */, 89BE6A17173974FD00DFE837 /* ZLLog.h in Headers */, @@ -14147,6 +14160,7 @@ 89BE6A23173974FD00DFE837 /* ZLMemStream.h in Headers */, 89BE6A27173974FD00DFE837 /* ZLMercator.h in Headers */, CDB718B21A44E7FE003DFFBA /* zl_usocket.h in Headers */, + CDDB553F1CFC15E9009E82D8 /* ZLCleanup.h in Headers */, CD1756471A199B24009C430A /* ZLBarycentric.h in Headers */, CD41F3011A20115E008E3420 /* ZLBitBuffer.h in Headers */, CDFB27511A3C27F800853B1D /* STLSort.h in Headers */, @@ -14203,12 +14217,14 @@ 89BE695E173974FD00DFE837 /* pch.h in Headers */, 89BE6960173974FD00DFE837 /* STLArray.h in Headers */, 89BE6962173974FD00DFE837 /* STLIteration.h in Headers */, + CDDB55401CFC15E9009E82D8 /* ZLCleanup.h in Headers */, CD2FB1F31C473B76005F8D8D /* ZLGfxLogger.h in Headers */, 89BE6964173974FD00DFE837 /* STLList.h in Headers */, 89BE6966173974FD00DFE837 /* STLMap.h in Headers */, 89BE6968173974FD00DFE837 /* STLSet.h in Headers */, CD2FB1F11C473B76005F8D8D /* ZLGfxImmediate.h in Headers */, 89BE696C173974FD00DFE837 /* STLString.h in Headers */, + CDDB553D1CF7C57F009E82D8 /* ZLHull2D.h in Headers */, 89BE696E173974FD00DFE837 /* ZLAccessors.h in Headers */, 89BE6970173974FD00DFE837 /* ZLAdapterInfo.h in Headers */, CD1756481A199B24009C430A /* ZLBarycentric.h in Headers */, @@ -17945,6 +17961,7 @@ 89BE698F173974FD00DFE837 /* ZLCgt.cpp in Sources */, 89BE6993173974FD00DFE837 /* ZLColor.cpp in Sources */, 89BE6997173974FD00DFE837 /* ZLCurve.cpp in Sources */, + CDDB553A1CF7C57F009E82D8 /* ZLHull2D.cpp in Sources */, CDE440121AC0D27600C745DB /* ZLFrustum.cpp in Sources */, 89BE699B173974FD00DFE837 /* ZLDeflateReader.cpp in Sources */, 89BE699F173974FD00DFE837 /* ZLDeflateWriter.cpp in Sources */, @@ -18023,6 +18040,7 @@ 89BE6990173974FD00DFE837 /* ZLCgt.cpp in Sources */, 89BE6994173974FD00DFE837 /* ZLColor.cpp in Sources */, 89BE6998173974FD00DFE837 /* ZLCurve.cpp in Sources */, + CDDB553B1CF7C57F009E82D8 /* ZLHull2D.cpp in Sources */, CDE440131AC0D27600C745DB /* ZLFrustum.cpp in Sources */, 89BE699C173974FD00DFE837 /* ZLDeflateReader.cpp in Sources */, 89BE69A0173974FD00DFE837 /* ZLDeflateWriter.cpp in Sources */,