axmol/cocos/2d/CCAutoPolygon.h

99 lines
3.6 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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.
****************************************************************************/
2015-05-29 15:03:36 +08:00
#ifndef COCOS_2D_CCAUTOPOLYGON_H__
#define COCOS_2D_CCAUTOPOLYGON_H__
#include <string>
#include <vector>
#include "platform/CCImage.h"
#include "renderer/CCTrianglesCommand.h"
NS_CC_BEGIN
class CC_DLL PolygonInfo
{
public:
PolygonInfo():
triangles(),
isVertsOwner(true)
{
};
PolygonInfo(const PolygonInfo& other);
PolygonInfo(V3F_C4B_T2F_Quad *quad);
~PolygonInfo();
std::string filename;
TrianglesCommand::Triangles triangles;
protected:
bool isVertsOwner;
};
2015-05-20 06:07:50 +08:00
class CC_DLL AutoPolygon
{
public:
2015-05-20 06:07:50 +08:00
AutoPolygon(const std::string &filename);
AutoPolygon();
2015-05-20 06:07:50 +08:00
~AutoPolygon();
//using RamerDouglasPeucker algorithm
std::vector<Vec2> trace(const cocos2d::Rect& rect, const float& threshold = 0.0);
std::vector<Vec2> reduce(const std::vector<Vec2>& points, const float& epsilon = 2.0);
std::vector<Vec2> expand(const std::vector<Vec2>& points, const cocos2d::Rect& rect, const float& epsilon);
TrianglesCommand::Triangles triangulate(const std::vector<Vec2>& points);
void calculateUV(V3F_C4B_T2F* verts, const unsigned int& count);
2015-05-20 06:07:50 +08:00
PolygonInfo generateTriangles(const Rect& rect, const float& epsilon, const float& threshold = 0);
protected:
Vec2 findFirstNoneTransparentPixel(const Rect& rect);
std::vector<cocos2d::Vec2> marchSquare(const Rect& rect, const Vec2& first, const float& threshold);
unsigned int getSquareValue(const unsigned int& x, const unsigned int& y, const Rect& rect, const float& threshold);
unsigned char getAlphaByIndex(const unsigned int& i);
unsigned char getAlphaByPos(const Vec2& i);
int getIndexFromPos(const unsigned int& x, const unsigned int& y){return y*_width+x;};
cocos2d::Vec2 getPosFromIndex(const unsigned int& i){return cocos2d::Vec2(i%_width, i/_width);};
2015-05-20 06:07:50 +08:00
std::vector<cocos2d::Vec2> rdp(std::vector<cocos2d::Vec2> v, const float& optimization);
float perpendicularDistance(const cocos2d::Vec2& i, const cocos2d::Vec2& start, const cocos2d::Vec2& end);
2015-04-27 16:33:54 +08:00
bool isAConvexPoint(const cocos2d::Vec2& p1, const cocos2d::Vec2& p2);
Image* _image;
unsigned char * _data;
std::string _filename;
unsigned int _width;
unsigned int _height;
2015-05-20 06:07:50 +08:00
float _scaleFactor;
unsigned int _threshold;
};
NS_CC_END
2015-05-29 15:03:36 +08:00
#endif // #ifndef COCOS_2D_CCAUTOPOLYGON_H__