2015-04-23 00:51:47 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
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__
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2015-06-02 10:10:09 +08:00
|
|
|
|
#include "platform/CCImage.h"
|
|
|
|
|
#include "renderer/CCTrianglesCommand.h"
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
2015-06-02 10:10:09 +08:00
|
|
|
|
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
|
2015-04-23 00:51:47 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2015-05-20 06:07:50 +08:00
|
|
|
|
AutoPolygon(const std::string &filename);
|
2015-06-02 10:10:09 +08:00
|
|
|
|
AutoPolygon();
|
2015-05-20 06:07:50 +08:00
|
|
|
|
~AutoPolygon();
|
2015-05-18 15:10:41 +08:00
|
|
|
|
|
2015-04-23 00:51:47 +08:00
|
|
|
|
//using Ramer–Douglas–Peucker algorithm
|
2015-06-02 10:10:09 +08:00
|
|
|
|
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
|
|
|
|
|
2015-06-02 10:10:09 +08:00
|
|
|
|
PolygonInfo generateTriangles(const Rect& rect, const float& epsilon, const float& threshold = 0);
|
2015-05-19 11:25:53 +08:00
|
|
|
|
|
2015-04-23 00:51:47 +08:00
|
|
|
|
protected:
|
2015-06-02 10:10:09 +08:00
|
|
|
|
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);
|
2015-05-15 17:18:19 +08:00
|
|
|
|
|
2015-05-18 15:10:41 +08:00
|
|
|
|
unsigned char getAlphaByIndex(const unsigned int& i);
|
2015-06-02 10:10:09 +08:00
|
|
|
|
unsigned char getAlphaByPos(const Vec2& i);
|
2015-05-15 17:18:19 +08:00
|
|
|
|
|
2015-05-18 15:10:41 +08:00
|
|
|
|
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-15 17:18:19 +08:00
|
|
|
|
|
2015-05-20 06:07:50 +08:00
|
|
|
|
std::vector<cocos2d::Vec2> rdp(std::vector<cocos2d::Vec2> v, const float& optimization);
|
2015-05-18 15:10:41 +08:00
|
|
|
|
float perpendicularDistance(const cocos2d::Vec2& i, const cocos2d::Vec2& start, const cocos2d::Vec2& end);
|
2015-04-27 16:33:54 +08:00
|
|
|
|
|
2015-05-15 17:18:19 +08:00
|
|
|
|
bool isAConvexPoint(const cocos2d::Vec2& p1, const cocos2d::Vec2& p2);
|
|
|
|
|
|
2015-06-02 10:10:09 +08:00
|
|
|
|
Image* _image;
|
2015-05-15 17:18:19 +08:00
|
|
|
|
unsigned char * _data;
|
|
|
|
|
std::string _filename;
|
|
|
|
|
unsigned int _width;
|
|
|
|
|
unsigned int _height;
|
2015-05-20 06:07:50 +08:00
|
|
|
|
float _scaleFactor;
|
2015-05-15 17:18:19 +08:00
|
|
|
|
unsigned int _threshold;
|
2015-04-23 00:51:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
2015-05-29 15:03:36 +08:00
|
|
|
|
#endif // #ifndef COCOS_2D_CCAUTOPOLYGON_H__
|