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-20 06:07:50 +08:00
|
|
|
|
#ifndef COCOS_2D_AUTOPOLYGON_H__
|
|
|
|
|
#define COCOS_2D_AUTOPOLYGON_H__
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2015-05-18 15:10:41 +08:00
|
|
|
|
#include "cocos2d.h"
|
2015-04-23 00:51:47 +08:00
|
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
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);
|
|
|
|
|
~AutoPolygon();
|
2015-05-18 15:10:41 +08:00
|
|
|
|
|
2015-04-23 00:51:47 +08:00
|
|
|
|
void setThreshold(unsigned int threshold){_threshold = threshold;};
|
2015-05-18 15:10:41 +08:00
|
|
|
|
const unsigned int getThreshold(){return _threshold;};
|
|
|
|
|
|
2015-05-15 17:18:19 +08:00
|
|
|
|
ssize_t getVecCount(){return _points.size();};
|
2015-05-19 11:25:53 +08:00
|
|
|
|
const std::vector<cocos2d::Vec2>& getPoints(){return _points;};
|
2015-05-18 15:10:41 +08:00
|
|
|
|
|
2015-05-20 06:07:50 +08:00
|
|
|
|
const TrianglesCommand::Triangles& getTriangles(){return _triangles;};
|
2015-04-23 00:51:47 +08:00
|
|
|
|
void printPoints();
|
2015-05-18 15:10:41 +08:00
|
|
|
|
|
2015-04-23 00:51:47 +08:00
|
|
|
|
//using Ramer–Douglas–Peucker algorithm
|
2015-05-20 06:07:50 +08:00
|
|
|
|
void trace(const cocos2d::Rect& rect, const unsigned int threshold = 0);
|
2015-05-19 11:25:53 +08:00
|
|
|
|
void optimize(const float& optimization);
|
|
|
|
|
void expand(const cocos2d::Rect& rect, const float& optimization);
|
2015-05-20 06:07:50 +08:00
|
|
|
|
void triangulate();
|
|
|
|
|
void calculateUV();
|
|
|
|
|
|
|
|
|
|
void generateTriangles(const cocos2d::Rect& rect, const float& optimization, const unsigned int threshold = 0);
|
2015-05-19 11:25:53 +08:00
|
|
|
|
|
2015-04-23 00:51:47 +08:00
|
|
|
|
protected:
|
|
|
|
|
unsigned int findFirstNoneTransparentPixel();
|
2015-05-18 15:10:41 +08:00
|
|
|
|
void marchSquare(const unsigned int& startx, const unsigned int& starty);
|
|
|
|
|
unsigned int getSquareValue(const unsigned int& x, const unsigned int& y);
|
2015-05-15 17:18:19 +08:00
|
|
|
|
|
2015-05-18 15:10:41 +08:00
|
|
|
|
unsigned char getAlphaByIndex(const unsigned int& i);
|
|
|
|
|
unsigned char getAlphaByPos(const unsigned int& x, const unsigned int& y);
|
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);
|
|
|
|
|
|
|
|
|
|
cocos2d::Image* _image;
|
|
|
|
|
unsigned char * _data;
|
|
|
|
|
std::string _filename;
|
|
|
|
|
unsigned int _width;
|
|
|
|
|
unsigned int _height;
|
2015-05-20 06:07:50 +08:00
|
|
|
|
float _scaleFactor;
|
|
|
|
|
cocos2d::Rect _rect;
|
2015-05-15 17:18:19 +08:00
|
|
|
|
unsigned int _threshold;
|
|
|
|
|
std::vector<cocos2d::Vec2> _points;
|
2015-05-20 06:07:50 +08:00
|
|
|
|
TrianglesCommand::Triangles _triangles;
|
2015-04-23 00:51:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
2015-05-20 06:07:50 +08:00
|
|
|
|
#endif // #ifndef COCOS_2D_AUTOPOLYGON_H__
|