PLYImporter: - optimize memory and speed on ply importer / change parser to use a file stream - manage texture path in ply import - manage texture coords on faces in ply import - correction on point cloud faces generation

IFC :
 - update poly2tri to avoid crash on some IFC files

Collada :
 - manage missing texture reference in collada import
pull/1293/head
Arkeon 2017-06-02 13:24:56 +02:00
parent dcc5887817
commit f84851e893
19 changed files with 2237 additions and 2062 deletions

View File

@ -1726,6 +1726,8 @@ void ColladaLoader::BuildMaterials( ColladaParser& pParser, aiScene* /*pScene*/)
aiString ColladaLoader::FindFilenameForEffectTexture( const ColladaParser& pParser, aiString ColladaLoader::FindFilenameForEffectTexture( const ColladaParser& pParser,
const Collada::Effect& pEffect, const std::string& pName) const Collada::Effect& pEffect, const std::string& pName)
{ {
aiString result;
// recurse through the param references until we end up at an image // recurse through the param references until we end up at an image
std::string name = pName; std::string name = pName;
while( 1) while( 1)
@ -1744,11 +1746,17 @@ aiString ColladaLoader::FindFilenameForEffectTexture( const ColladaParser& pPars
ColladaParser::ImageLibrary::const_iterator imIt = pParser.mImageLibrary.find( name); ColladaParser::ImageLibrary::const_iterator imIt = pParser.mImageLibrary.find( name);
if( imIt == pParser.mImageLibrary.end()) if( imIt == pParser.mImageLibrary.end())
{ {
throw DeadlyImportError( format() << //missing texture should not stop the conversion
"Collada: Unable to resolve effect texture entry \"" << pName << "\", ended up at ID \"" << name << "\"." ); //throw DeadlyImportError( format() <<
} // "Collada: Unable to resolve effect texture entry \"" << pName << "\", ended up at ID \"" << name << "\"." );
aiString result; DefaultLogger::get()->warn("Collada: Unable to resolve effect texture entry \"" + pName + "\", ended up at ID \"" + name + "\".");
//set default texture file name
result.Set(name + ".jpg");
ConvertPath(result);
return result;
}
// if this is an embedded texture image setup an aiTexture for it // if this is an embedded texture image setup an aiTexture for it
if (imIt->second.mFileName.empty()) if (imIt->second.mFileName.empty())

View File

@ -100,6 +100,11 @@ public:
/// @return true if successful. /// @return true if successful.
bool getNextDataLine( std::vector<T> &buffer, T continuationToken ); bool getNextDataLine( std::vector<T> &buffer, T continuationToken );
/// @brief Will read the next block.
/// @param buffer The buffer for the next block.
/// @return true if successful.
bool getNextBlock( std::vector<T> &buffer );
private: private:
IOStream *m_stream; IOStream *m_stream;
size_t m_filesize; size_t m_filesize;
@ -274,4 +279,21 @@ bool IOStreamBuffer<T>::getNextDataLine( std::vector<T> &buffer, T continuationT
return true; return true;
} }
template<class T>
inline
bool IOStreamBuffer<T>::getNextBlock( std::vector<T> &buffer) {
//just return the last blockvalue if getNextLine was used before
if ( m_cachePos != 0) {
buffer = std::vector<T>(m_cache.begin() + m_cachePos, m_cache.end());
m_cachePos = 0;
}
else {
if ( !readNextBlock() )
return false;
buffer = std::vector<T>(m_cache.begin(), m_cache.end());
}
return true;
}
} // !ns Assimp } // !ns Assimp

File diff suppressed because it is too large Load Diff

View File

@ -68,7 +68,6 @@ public:
PLYImporter(); PLYImporter();
~PLYImporter(); ~PLYImporter();
public: public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
@ -78,6 +77,16 @@ public:
bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
bool checkSig) const; bool checkSig) const;
// -------------------------------------------------------------------
/** Extract a vertex from the DOM
*/
void LoadVertex(const PLY::Element* pcElement, const PLY::ElementInstance* instElement, unsigned int pos);
// -------------------------------------------------------------------
/** Extract a face from the DOM
*/
void LoadFace(const PLY::Element* pcElement, const PLY::ElementInstance* instElement, unsigned int pos);
protected: protected:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
@ -94,53 +103,10 @@ protected:
IOSystem* pIOHandler); IOSystem* pIOHandler);
protected: protected:
// -------------------------------------------------------------------
/** Extract vertices from the DOM
*/
void LoadVertices(std::vector<aiVector3D>* pvOut,
bool p_bNormals = false);
// -------------------------------------------------------------------
/** Extract vertex color channels from the DOM
*/
void LoadVertexColor(std::vector<aiColor4D>* pvOut);
// -------------------------------------------------------------------
/** Extract texture coordinate channels from the DOM
*/
void LoadTextureCoordinates(std::vector<aiVector2D>* pvOut);
// -------------------------------------------------------------------
/** Extract a face list from the DOM
*/
void LoadFaces(std::vector<PLY::Face>* pvOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Extract a material list from the DOM /** Extract a material list from the DOM
*/ */
void LoadMaterial(std::vector<aiMaterial*>* pvOut); void LoadMaterial(std::vector<aiMaterial*>* pvOut, std::string &defaultTexture, const bool pointsOnly);
// -------------------------------------------------------------------
/** Validate material indices, replace default material identifiers
*/
void ReplaceDefaultMaterial(std::vector<PLY::Face>* avFaces,
std::vector<aiMaterial*>* avMaterials);
// -------------------------------------------------------------------
/** Convert all meshes into our ourer representation
*/
void ConvertMeshes(std::vector<PLY::Face>* avFaces,
const std::vector<aiVector3D>* avPositions,
const std::vector<aiVector3D>* avNormals,
const std::vector<aiColor4D>* avColors,
const std::vector<aiVector2D>* avTexCoords,
const std::vector<aiMaterial*>* avMaterials,
std::vector<aiMesh*>* avOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Static helper to parse a color from four single channels in /** Static helper to parse a color from four single channels in
@ -151,7 +117,6 @@ protected:
PLY::EDataType aiTypes[4], PLY::EDataType aiTypes[4],
aiColor4D* clrOut); aiColor4D* clrOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** Static helper to parse a color channel value. The input value /** Static helper to parse a color channel value. The input value
* is normalized to 0-1. * is normalized to 0-1.
@ -160,12 +125,14 @@ protected:
PLY::PropertyInstance::ValueUnion val, PLY::PropertyInstance::ValueUnion val,
PLY::EDataType eType); PLY::EDataType eType);
/** Buffer to hold the loaded file */ /** Buffer to hold the loaded file */
unsigned char* mBuffer; unsigned char* mBuffer;
/** Document object model representation extracted from the file */ /** Document object model representation extracted from the file */
PLY::DOM* pcDOM; PLY::DOM* pcDOM;
/** Mesh generated by loader */
aiMesh* mGeneratedMesh;
}; };
} // end of namespace Assimp } // end of namespace Assimp

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,6 @@ Open Asset Import Library (assimp)
---------------------------------------------------------------------- ----------------------------------------------------------------------
Copyright (c) 2006-2017, assimp team Copyright (c) 2006-2017, assimp team
All rights reserved. All rights reserved.
Redistribution and use of this software in source and binary forms, Redistribution and use of this software in source and binary forms,
@ -46,19 +45,21 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "ParsingUtils.h" #include "ParsingUtils.h"
#include "IOStreamBuffer.h"
#include <vector> #include <vector>
namespace Assimp namespace Assimp
{ {
//pre-declaration
class PLYImporter;
// http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/ // http://local.wasp.uwa.edu.au/~pbourke/dataformats/ply/
// http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf // http://w3.impa.br/~lvelho/outgoing/sossai/old/ViHAP_D4.4.2_PLY_format_v1.1.pdf
// http://www.okino.com/conv/exp_ply.htm // http://www.okino.com/conv/exp_ply.htm
namespace PLY namespace PLY
{ {
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
/* /*
name type number of bytes name type number of bytes
@ -197,6 +198,9 @@ enum EElementSemantic
//! The element is a material description //! The element is a material description
EEST_Material, EEST_Material,
//! texture path
EEST_TextureFile,
//! Marks invalid entries //! Marks invalid entries
EEST_INVALID EEST_INVALID
}; };
@ -238,16 +242,15 @@ public:
//! string is either '\n', '\r' or '\0'. Return value is false //! string is either '\n', '\r' or '\0'. Return value is false
//! if the input string is NOT a valid property (E.g. does //! if the input string is NOT a valid property (E.g. does
//! not start with the "property" keyword) //! not start with the "property" keyword)
static bool ParseProperty (const char* pCur, const char** pCurOut, static bool ParseProperty(std::vector<char> &buffer, Property* pOut);
Property* pOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a data type from a string //! Parse a data type from a string
static EDataType ParseDataType(const char* pCur,const char** pCurOut); static EDataType ParseDataType(std::vector<char> &buffer);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a semantic from a string //! Parse a semantic from a string
static ESemantic ParseSemantic(const char* pCur,const char** pCurOut); static ESemantic ParseSemantic(std::vector<char> &buffer);
}; };
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
@ -285,13 +288,11 @@ public:
//! Parse an element from a string. //! Parse an element from a string.
//! The function will parse all properties contained in the //! The function will parse all properties contained in the
//! element, too. //! element, too.
static bool ParseElement (const char* pCur, const char** pCurOut, static bool ParseElement(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, Element* pOut);
Element* pOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a semantic from a string //! Parse a semantic from a string
static EElementSemantic ParseSemantic(const char* pCur, static EElementSemantic ParseSemantic(std::vector<char> &buffer);
const char** pCurOut);
}; };
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
@ -331,13 +332,13 @@ public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a property instance //! Parse a property instance
static bool ParseInstance (const char* pCur,const char** pCurOut, static bool ParseInstance(const char* &pCur,
const Property* prop, PropertyInstance* p_pcOut); const Property* prop, PropertyInstance* p_pcOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a property instance in binary format //! Parse a property instance in binary format
static bool ParseInstanceBinary (const char* pCur,const char** pCurOut, static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
const Property* prop, PropertyInstance* p_pcOut,bool p_bBE); const char* &pCur, unsigned int &bufferSize, const Property* prop, PropertyInstance* p_pcOut, bool p_bBE);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Get the default value for a given data type //! Get the default value for a given data type
@ -345,13 +346,12 @@ public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a value //! Parse a value
static bool ParseValue(const char* pCur,const char** pCurOut, static bool ParseValue(const char* &pCur, EDataType eType, ValueUnion* out);
EDataType eType,ValueUnion* out);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a binary value //! Parse a binary value
static bool ParseValueBinary(const char* pCur,const char** pCurOut, static bool ParseValueBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
EDataType eType,ValueUnion* out,bool p_bBE); const char* &pCur, unsigned int &bufferSize, EDataType eType, ValueUnion* out, bool p_bBE);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Convert a property value to a given type TYPE //! Convert a property value to a given type TYPE
@ -375,13 +375,13 @@ public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse an element instance //! Parse an element instance
static bool ParseInstance (const char* pCur,const char** pCurOut, static bool ParseInstance(const char* &pCur,
const Element* pcElement, ElementInstance* p_pcOut); const Element* pcElement, ElementInstance* p_pcOut);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a binary element instance //! Parse a binary element instance
static bool ParseInstanceBinary (const char* pCur,const char** pCurOut, static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
const Element* pcElement, ElementInstance* p_pcOut,bool p_bBE); const char* &pCur, unsigned int &bufferSize, const Element* pcElement, ElementInstance* p_pcOut, bool p_bBE);
}; };
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
@ -400,13 +400,13 @@ public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse an element instance list //! Parse an element instance list
static bool ParseInstanceList (const char* pCur,const char** pCurOut, static bool ParseInstanceList(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
const Element* pcElement, ElementInstanceList* p_pcOut); const Element* pcElement, ElementInstanceList* p_pcOut, PLYImporter* loader);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Parse a binary element instance list //! Parse a binary element instance list
static bool ParseInstanceListBinary (const char* pCur,const char** pCurOut, static bool ParseInstanceListBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer,
const Element* pcElement, ElementInstanceList* p_pcOut,bool p_bBE); const char* &pCur, unsigned int &bufferSize, const Element* pcElement, ElementInstanceList* p_pcOut, PLYImporter* loader, bool p_bBE);
}; };
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
/** \brief Class to represent the document object model of an ASCII or binary /** \brief Class to represent the document object model of an ASCII or binary
@ -428,50 +428,33 @@ public:
//! Parse the DOM for a PLY file. The input string is assumed //! Parse the DOM for a PLY file. The input string is assumed
//! to be terminated with zero //! to be terminated with zero
static bool ParseInstance (const char* pCur,DOM* p_pcOut); static bool ParseInstance(IOStreamBuffer<char> &streamBuffer, DOM* p_pcOut, PLYImporter* loader);
static bool ParseInstanceBinary (const char* pCur, static bool ParseInstanceBinary(IOStreamBuffer<char> &streamBuffer, DOM* p_pcOut, PLYImporter* loader, bool p_bBE);
DOM* p_pcOut,bool p_bBE);
//! Skip all comment lines after this //! Skip all comment lines after this
static bool SkipComments (const char* pCur,const char** pCurOut); static bool SkipComments(std::vector<char> &buffer);
static bool SkipSpaces(std::vector<char> &buffer);
static bool SkipLine(std::vector<char> &buffer);
static bool TokenMatch(std::vector<char> &buffer, const char* token, unsigned int len);
static bool SkipSpacesAndLineEnd(std::vector<char> &buffer);
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Handle the file header and read all element descriptions //! Handle the file header and read all element descriptions
bool ParseHeader (const char* pCur,const char** pCurOut, bool p_bBE); bool ParseHeader(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, bool p_bBE);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Read in all element instance lists //! Read in all element instance lists
bool ParseElementInstanceLists (const char* pCur,const char** pCurOut); bool ParseElementInstanceLists(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, PLYImporter* loader);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
//! Read in all element instance lists for a binary file format //! Read in all element instance lists for a binary file format
bool ParseElementInstanceListsBinary (const char* pCur, bool ParseElementInstanceListsBinary(IOStreamBuffer<char> &streamBuffer, std::vector<char> &buffer, const char* &pCur, unsigned int &bufferSize, PLYImporter* loader, bool p_bBE);
const char** pCurOut,bool p_bBE);
};
// ---------------------------------------------------------------------------------
/** \brief Helper class to represent a loaded PLY face
*/
class Face
{
public:
Face()
: iMaterialIndex(0xFFFFFFFF)
{
// set all indices to zero by default
mIndices.resize(3,0);
}
public:
//! List of vertex indices
std::vector<unsigned int> mIndices;
//! Material index
unsigned int iMaterialIndex;
}; };
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------

View File

@ -211,20 +211,20 @@ void STLImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS
for (unsigned int i = 0; i < pScene->mNumMeshes; i++) for (unsigned int i = 0; i < pScene->mNumMeshes; i++)
pScene->mRootNode->mMeshes[i] = i; pScene->mRootNode->mMeshes[i] = i;
// create a single default material, using a light gray diffuse color for consistency with // create a single default material, using a white diffuse color for consistency with
// other geometric types (e.g., PLY). // other geometric types (e.g., PLY).
aiMaterial* pcMat = new aiMaterial(); aiMaterial* pcMat = new aiMaterial();
aiString s; aiString s;
s.Set(AI_DEFAULT_MATERIAL_NAME); s.Set(AI_DEFAULT_MATERIAL_NAME);
pcMat->AddProperty(&s, AI_MATKEY_NAME); pcMat->AddProperty(&s, AI_MATKEY_NAME);
aiColor4D clrDiffuse(ai_real(0.6),ai_real(0.6),ai_real(0.6),ai_real(1.0)); aiColor4D clrDiffuse(ai_real(1.0),ai_real(1.0),ai_real(1.0),ai_real(1.0));
if (bMatClr) { if (bMatClr) {
clrDiffuse = clrColorDefault; clrDiffuse = clrColorDefault;
} }
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE); pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR); pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
clrDiffuse = aiColor4D( ai_real( 0.05), ai_real( 0.05), ai_real( 0.05), ai_real( 1.0)); clrDiffuse = aiColor4D( ai_real(1.0), ai_real(1.0), ai_real(1.0), ai_real(1.0));
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT); pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
pScene->mNumMaterials = 1; pScene->mNumMaterials = 1;

View File

@ -88,7 +88,7 @@ void Triangle::Clear()
points_[0]=points_[1]=points_[2] = NULL; points_[0]=points_[1]=points_[2] = NULL;
} }
void Triangle::ClearNeighbor(Triangle *triangle ) void Triangle::ClearNeighbor(const Triangle *triangle )
{ {
if( neighbors_[0] == triangle ) if( neighbors_[0] == triangle )
{ {
@ -116,13 +116,9 @@ void Triangle::ClearDelunayEdges()
delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false; delaunay_edge[0] = delaunay_edge[1] = delaunay_edge[2] = false;
} }
Point* Triangle::OppositePoint(Triangle& t, Point& p) Point* Triangle::OppositePoint(Triangle& t, const Point& p)
{ {
Point *cw = t.PointCW(p); Point *cw = t.PointCW(p);
//double x = cw->x;
//double y = cw->y;
//x = p.x;
//y = p.y;
return PointCW(*cw); return PointCW(*cw);
} }
@ -164,8 +160,7 @@ int Triangle::Index(const Point* p)
return 2; return 2;
} }
assert(0); assert(0);
return -1;
return 0;
} }
int Triangle::EdgeIndex(const Point* p1, const Point* p2) int Triangle::EdgeIndex(const Point* p1, const Point* p2)
@ -192,7 +187,7 @@ int Triangle::EdgeIndex(const Point* p1, const Point* p2)
return -1; return -1;
} }
void Triangle::MarkConstrainedEdge(const int index) void Triangle::MarkConstrainedEdge(int index)
{ {
constrained_edge[index] = true; constrained_edge[index] = true;
} }
@ -215,7 +210,7 @@ void Triangle::MarkConstrainedEdge(Point* p, Point* q)
} }
// The point counter-clockwise to given point // The point counter-clockwise to given point
Point* Triangle::PointCW(Point& point) Point* Triangle::PointCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return points_[2]; return points_[2];
@ -225,12 +220,11 @@ Point* Triangle::PointCW(Point& point)
return points_[1]; return points_[1];
} }
assert(0); assert(0);
return NULL;
return 0;
} }
// The point counter-clockwise to given point // The point counter-clockwise to given point
Point* Triangle::PointCCW(Point& point) Point* Triangle::PointCCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return points_[1]; return points_[1];
@ -240,12 +234,11 @@ Point* Triangle::PointCCW(Point& point)
return points_[0]; return points_[0];
} }
assert(0); assert(0);
return NULL;
return 0;
} }
// The neighbor clockwise to given point // The neighbor clockwise to given point
Triangle* Triangle::NeighborCW(Point& point) Triangle* Triangle::NeighborCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return neighbors_[1]; return neighbors_[1];
@ -256,7 +249,7 @@ Triangle* Triangle::NeighborCW(Point& point)
} }
// The neighbor counter-clockwise to given point // The neighbor counter-clockwise to given point
Triangle* Triangle::NeighborCCW(Point& point) Triangle* Triangle::NeighborCCW(const Point& point)
{ {
if (&point == points_[0]) { if (&point == points_[0]) {
return neighbors_[2]; return neighbors_[2];
@ -266,7 +259,7 @@ Triangle* Triangle::NeighborCCW(Point& point)
return neighbors_[1]; return neighbors_[1];
} }
bool Triangle::GetConstrainedEdgeCCW(Point& p) bool Triangle::GetConstrainedEdgeCCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return constrained_edge[2]; return constrained_edge[2];
@ -276,7 +269,7 @@ bool Triangle::GetConstrainedEdgeCCW(Point& p)
return constrained_edge[1]; return constrained_edge[1];
} }
bool Triangle::GetConstrainedEdgeCW(Point& p) bool Triangle::GetConstrainedEdgeCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return constrained_edge[1]; return constrained_edge[1];
@ -286,7 +279,7 @@ bool Triangle::GetConstrainedEdgeCW(Point& p)
return constrained_edge[0]; return constrained_edge[0];
} }
void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce) void Triangle::SetConstrainedEdgeCCW(const Point& p, bool ce)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
constrained_edge[2] = ce; constrained_edge[2] = ce;
@ -297,7 +290,7 @@ void Triangle::SetConstrainedEdgeCCW(Point& p, bool ce)
} }
} }
void Triangle::SetConstrainedEdgeCW(Point& p, bool ce) void Triangle::SetConstrainedEdgeCW(const Point& p, bool ce)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
constrained_edge[1] = ce; constrained_edge[1] = ce;
@ -308,7 +301,7 @@ void Triangle::SetConstrainedEdgeCW(Point& p, bool ce)
} }
} }
bool Triangle::GetDelunayEdgeCCW(Point& p) bool Triangle::GetDelunayEdgeCCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return delaunay_edge[2]; return delaunay_edge[2];
@ -318,7 +311,7 @@ bool Triangle::GetDelunayEdgeCCW(Point& p)
return delaunay_edge[1]; return delaunay_edge[1];
} }
bool Triangle::GetDelunayEdgeCW(Point& p) bool Triangle::GetDelunayEdgeCW(const Point& p)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
return delaunay_edge[1]; return delaunay_edge[1];
@ -328,7 +321,7 @@ bool Triangle::GetDelunayEdgeCW(Point& p)
return delaunay_edge[0]; return delaunay_edge[0];
} }
void Triangle::SetDelunayEdgeCCW(Point& p, bool e) void Triangle::SetDelunayEdgeCCW(const Point& p, bool e)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
delaunay_edge[2] = e; delaunay_edge[2] = e;
@ -339,7 +332,7 @@ void Triangle::SetDelunayEdgeCCW(Point& p, bool e)
} }
} }
void Triangle::SetDelunayEdgeCW(Point& p, bool e) void Triangle::SetDelunayEdgeCW(const Point& p, bool e)
{ {
if (&p == points_[0]) { if (&p == points_[0]) {
delaunay_edge[1] = e; delaunay_edge[1] = e;
@ -351,7 +344,7 @@ void Triangle::SetDelunayEdgeCW(Point& p, bool e)
} }
// The neighbor across to given point // The neighbor across to given point
Triangle& Triangle::NeighborAcross(Point& opoint) Triangle& Triangle::NeighborAcross(const Point& opoint)
{ {
if (&opoint == points_[0]) { if (&opoint == points_[0]) {
return *neighbors_[0]; return *neighbors_[0];
@ -370,4 +363,3 @@ void Triangle::DebugPrint()
} }
} }

View File

@ -113,7 +113,7 @@ struct Point {
/// Convert this point into a unit point. Returns the Length. /// Convert this point into a unit point. Returns the Length.
double Normalize() double Normalize()
{ {
double len = Length(); const double len = Length();
x /= len; x /= len;
y /= len; y /= len;
return len; return len;
@ -162,50 +162,50 @@ bool constrained_edge[3];
/// Flags to determine if an edge is a Delauney edge /// Flags to determine if an edge is a Delauney edge
bool delaunay_edge[3]; bool delaunay_edge[3];
Point* GetPoint(const int& index); Point* GetPoint(int index);
Point* PointCW(Point& point); Point* PointCW(const Point& point);
Point* PointCCW(Point& point); Point* PointCCW(const Point& point);
Point* OppositePoint(Triangle& t, Point& p); Point* OppositePoint(Triangle& t, const Point& p);
Triangle* GetNeighbor(const int& index); Triangle* GetNeighbor(int index);
void MarkNeighbor(Point* p1, Point* p2, Triangle* t); void MarkNeighbor(Point* p1, Point* p2, Triangle* t);
void MarkNeighbor(Triangle& t); void MarkNeighbor(Triangle& t);
void MarkConstrainedEdge(const int index); void MarkConstrainedEdge(int index);
void MarkConstrainedEdge(Edge& edge); void MarkConstrainedEdge(Edge& edge);
void MarkConstrainedEdge(Point* p, Point* q); void MarkConstrainedEdge(Point* p, Point* q);
int Index(const Point* p); int Index(const Point* p);
int EdgeIndex(const Point* p1, const Point* p2); int EdgeIndex(const Point* p1, const Point* p2);
Triangle* NeighborCW(Point& point); Triangle* NeighborCW(const Point& point);
Triangle* NeighborCCW(Point& point); Triangle* NeighborCCW(const Point& point);
bool GetConstrainedEdgeCCW(Point& p); bool GetConstrainedEdgeCCW(const Point& p);
bool GetConstrainedEdgeCW(Point& p); bool GetConstrainedEdgeCW(const Point& p);
void SetConstrainedEdgeCCW(Point& p, bool ce); void SetConstrainedEdgeCCW(const Point& p, bool ce);
void SetConstrainedEdgeCW(Point& p, bool ce); void SetConstrainedEdgeCW(const Point& p, bool ce);
bool GetDelunayEdgeCCW(Point& p); bool GetDelunayEdgeCCW(const Point& p);
bool GetDelunayEdgeCW(Point& p); bool GetDelunayEdgeCW(const Point& p);
void SetDelunayEdgeCCW(Point& p, bool e); void SetDelunayEdgeCCW(const Point& p, bool e);
void SetDelunayEdgeCW(Point& p, bool e); void SetDelunayEdgeCW(const Point& p, bool e);
bool Contains(Point* p); bool Contains(const Point* p);
bool Contains(const Edge& e); bool Contains(const Edge& e);
bool Contains(Point* p, Point* q); bool Contains(const Point* p, const Point* q);
void Legalize(Point& point); void Legalize(Point& point);
void Legalize(Point& opoint, Point& npoint); void Legalize(Point& opoint, Point& npoint);
/** /**
* Clears all references to all other triangles and points * Clears all references to all other triangles and points
*/ */
void Clear(); void Clear();
void ClearNeighbor(Triangle *triangle ); void ClearNeighbor(const Triangle *triangle);
void ClearNeighbors(); void ClearNeighbors();
void ClearDelunayEdges(); void ClearDelunayEdges();
inline bool IsInterior(); inline bool IsInterior();
inline void IsInterior(bool b); inline void IsInterior(bool b);
Triangle& NeighborAcross(Point& opoint); Triangle& NeighborAcross(const Point& opoint);
void DebugPrint(); void DebugPrint();
@ -258,7 +258,7 @@ inline bool operator ==(const Point& a, const Point& b)
inline bool operator !=(const Point& a, const Point& b) inline bool operator !=(const Point& a, const Point& b)
{ {
return a.x != b.x || a.y != b.y; return !(a.x == b.x) && !(a.y == b.y);
} }
/// Peform the dot product on two vectors. /// Peform the dot product on two vectors.
@ -282,22 +282,22 @@ inline Point Cross(const Point& a, double s)
/// Perform the cross product on a scalar and a point. In 2D this produces /// Perform the cross product on a scalar and a point. In 2D this produces
/// a point. /// a point.
inline Point Cross(const double s, const Point& a) inline Point Cross(double s, const Point& a)
{ {
return Point(-s * a.y, s * a.x); return Point(-s * a.y, s * a.x);
} }
inline Point* Triangle::GetPoint(const int& index) inline Point* Triangle::GetPoint(int index)
{ {
return points_[index]; return points_[index];
} }
inline Triangle* Triangle::GetNeighbor(const int& index) inline Triangle* Triangle::GetNeighbor(int index)
{ {
return neighbors_[index]; return neighbors_[index];
} }
inline bool Triangle::Contains(Point* p) inline bool Triangle::Contains(const Point* p)
{ {
return p == points_[0] || p == points_[1] || p == points_[2]; return p == points_[0] || p == points_[1] || p == points_[2];
} }
@ -307,7 +307,7 @@ inline bool Triangle::Contains(const Edge& e)
return Contains(e.p) && Contains(e.q); return Contains(e.p) && Contains(e.q);
} }
inline bool Triangle::Contains(Point* p, Point* q) inline bool Triangle::Contains(const Point* p, const Point* q)
{ {
return Contains(p) && Contains(q); return Contains(p) && Contains(q);
} }
@ -325,5 +325,3 @@ inline void Triangle::IsInterior(bool b)
} }
#endif #endif

View File

@ -32,14 +32,22 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
// Otherwise #defines like M_PI are undeclared under Visual Studio
#define _USE_MATH_DEFINES
#include <exception> #include <exception>
#include <math.h>
// C99 removes M_PI from math.h
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif
namespace p2t { namespace p2t {
const double PI = 3.1415926535897932384626433832795029; const double PI_3div4 = 3 * M_PI / 4;
const double PI_2 = 2 * PI; const double PI_div2 = 1.57079632679489661923;
const double PI_3div4 = 3 * PI / 4; const double EPSILON = 1e-12;
const double EPSILON = 1e-15;
enum Orientation { CW, CCW, COLLINEAR }; enum Orientation { CW, CCW, COLLINEAR };
@ -53,7 +61,7 @@ enum Orientation { CW, CCW, COLLINEAR };
* = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3) * = (x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)
* </pre> * </pre>
*/ */
Orientation Orient2d(Point& pa, Point& pb, Point& pc) Orientation Orient2d(const Point& pa, const Point& pb, const Point& pc)
{ {
double detleft = (pa.x - pc.x) * (pb.y - pc.y); double detleft = (pa.x - pc.x) * (pb.y - pc.y);
double detright = (pa.y - pc.y) * (pb.x - pc.x); double detright = (pa.y - pc.y) * (pb.x - pc.x);
@ -66,6 +74,7 @@ Orientation Orient2d(Point& pa, Point& pb, Point& pc)
return CW; return CW;
} }
/*
bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd) bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd)
{ {
double pdx = pd.x; double pdx = pd.x;
@ -97,7 +106,22 @@ bool InScanArea(Point& pa, Point& pb, Point& pc, Point& pd)
return true; return true;
} }
*/
bool InScanArea(const Point& pa, const Point& pb, const Point& pc, const Point& pd)
{
double oadb = (pa.x - pb.x)*(pd.y - pb.y) - (pd.x - pb.x)*(pa.y - pb.y);
if (oadb >= -EPSILON) {
return false;
}
double oadc = (pa.x - pc.x)*(pd.y - pc.y) - (pd.x - pc.x)*(pa.y - pc.y);
if (oadc <= EPSILON) {
return false;
}
return true;
}
} }
#endif #endif

View File

@ -36,4 +36,3 @@
#include "sweep/cdt.h" #include "sweep/cdt.h"
#endif #endif

View File

@ -39,7 +39,7 @@ AdvancingFront::AdvancingFront(Node& head, Node& tail)
search_node_ = &head; search_node_ = &head;
} }
Node* AdvancingFront::LocateNode(const double& x) Node* AdvancingFront::LocateNode(double x)
{ {
Node* node = search_node_; Node* node = search_node_;
@ -61,7 +61,7 @@ Node* AdvancingFront::LocateNode(const double& x)
return NULL; return NULL;
} }
Node* AdvancingFront::FindSearchNode(const double& x) Node* AdvancingFront::FindSearchNode(double x)
{ {
(void)x; // suppress compiler warnings "unused parameter 'x'" (void)x; // suppress compiler warnings "unused parameter 'x'"
// TODO: implement BST index // TODO: implement BST index
@ -106,4 +106,3 @@ AdvancingFront::~AdvancingFront()
} }
} }

View File

@ -74,7 +74,7 @@ Node* search();
void set_search(Node* node); void set_search(Node* node);
/// Locate insertion point along advancing front /// Locate insertion point along advancing front
Node* LocateNode(const double& x); Node* LocateNode(double x);
Node* LocatePoint(const Point* point); Node* LocatePoint(const Point* point);
@ -82,7 +82,7 @@ private:
Node* head_, *tail_, *search_node_; Node* head_, *tail_, *search_node_;
Node* FindSearchNode(const double& x); Node* FindSearchNode(double x);
}; };
inline Node* AdvancingFront::head() inline Node* AdvancingFront::head()

View File

@ -32,13 +32,13 @@
namespace p2t { namespace p2t {
CDT::CDT(std::vector<Point*> polyline) CDT::CDT(const std::vector<Point*>& polyline)
{ {
sweep_context_ = new SweepContext(polyline); sweep_context_ = new SweepContext(polyline);
sweep_ = new Sweep; sweep_ = new Sweep;
} }
void CDT::AddHole(std::vector<Point*> polyline) void CDT::AddHole(const std::vector<Point*>& polyline)
{ {
sweep_context_->AddHole(polyline); sweep_context_->AddHole(polyline);
} }
@ -69,4 +69,3 @@ CDT::~CDT()
} }
} }

View File

@ -53,7 +53,7 @@ public:
* *
* @param polyline * @param polyline
*/ */
CDT(std::vector<Point*> polyline); CDT(const std::vector<Point*>& polyline);
/** /**
* Destructor - clean up memory * Destructor - clean up memory
@ -65,7 +65,7 @@ public:
* *
* @param polyline * @param polyline
*/ */
void AddHole(std::vector<Point*> polyline); void AddHole(const std::vector<Point*>& polyline);
/** /**
* Add a steiner point * Add a steiner point

View File

@ -49,7 +49,7 @@ void Sweep::Triangulate(SweepContext& tcx)
void Sweep::SweepPoints(SweepContext& tcx) void Sweep::SweepPoints(SweepContext& tcx)
{ {
for (int i = 1; i < tcx.point_count(); i++) { for (size_t i = 1; i < tcx.point_count(); i++) {
Point& point = *tcx.GetPoint(i); Point& point = *tcx.GetPoint(i);
Node* node = &PointEvent(tcx, point); Node* node = &PointEvent(tcx, point);
for (unsigned int i = 0; i < point.edge_list.size(); i++) { for (unsigned int i = 0; i < point.edge_list.size(); i++) {
@ -166,7 +166,7 @@ void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangl
bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq) bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq)
{ {
int index = triangle.EdgeIndex(&ep, &eq); const int index = triangle.EdgeIndex(&ep, &eq);
if (index != -1) { if (index != -1) {
triangle.MarkConstrainedEdge(index); triangle.MarkConstrainedEdge(index);
@ -230,8 +230,8 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
Node* node = n.next; Node* node = n.next;
while (node->next) { while (node->next) {
double angle = HoleAngle(*node); // if HoleAngle exceeds 90 degrees then break.
if (angle > PI_2 || angle < -PI_2) break; if (LargeHole_DontFill(node)) break;
Fill(tcx, *node); Fill(tcx, *node);
node = node->next; node = node->next;
} }
@ -240,29 +240,81 @@ void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
node = n.prev; node = n.prev;
while (node->prev) { while (node->prev) {
double angle = HoleAngle(*node); // if HoleAngle exceeds 90 degrees then break.
if (angle > PI_2 || angle < -PI_2) break; if (LargeHole_DontFill(node)) break;
Fill(tcx, *node); Fill(tcx, *node);
node = node->prev; node = node->prev;
} }
// Fill right basins // Fill right basins
if (n.next && n.next->next) { if (n.next && n.next->next) {
double angle = BasinAngle(n); const double angle = BasinAngle(n);
if (angle < PI_3div4) { if (angle < PI_3div4) {
FillBasin(tcx, n); FillBasin(tcx, n);
} }
} }
} }
double Sweep::BasinAngle(Node& node) // True if HoleAngle exceeds 90 degrees.
bool Sweep::LargeHole_DontFill(const Node* node) const {
const Node* nextNode = node->next;
const Node* prevNode = node->prev;
if (!AngleExceeds90Degrees(node->point, nextNode->point, prevNode->point))
return false;
// Check additional points on front.
const Node* next2Node = nextNode->next;
// "..Plus.." because only want angles on same side as point being added.
if ((next2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, next2Node->point, prevNode->point))
return false;
const Node* prev2Node = prevNode->prev;
// "..Plus.." because only want angles on same side as point being added.
if ((prev2Node != NULL) && !AngleExceedsPlus90DegreesOrIsNegative(node->point, nextNode->point, prev2Node->point))
return false;
return true;
}
bool Sweep::AngleExceeds90Degrees(const Point* origin, const Point* pa, const Point* pb) const {
const double angle = Angle(origin, pa, pb);
return ((angle > PI_div2) || (angle < -PI_div2));
}
bool Sweep::AngleExceedsPlus90DegreesOrIsNegative(const Point* origin, const Point* pa, const Point* pb) const {
const double angle = Angle(origin, pa, pb);
return (angle > PI_div2) || (angle < 0);
}
double Sweep::Angle(const Point* origin, const Point* pa, const Point* pb) const {
/* Complex plane
* ab = cosA +i*sinA
* ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)
* atan2(y,x) computes the principal value of the argument function
* applied to the complex number x+iy
* Where x = ax*bx + ay*by
* y = ax*by - ay*bx
*/
const double px = origin->x;
const double py = origin->y;
const double ax = pa->x- px;
const double ay = pa->y - py;
const double bx = pb->x - px;
const double by = pb->y - py;
const double x = ax * by - ay * bx;
const double y = ax * bx + ay * by;
return atan2(x, y);
}
double Sweep::BasinAngle(const Node& node) const
{ {
double ax = node.point->x - node.next->next->point->x; const double ax = node.point->x - node.next->next->point->x;
double ay = node.point->y - node.next->next->point->y; const double ay = node.point->y - node.next->next->point->y;
return atan2(ay, ax); return atan2(ay, ax);
} }
double Sweep::HoleAngle(Node& node) double Sweep::HoleAngle(const Node& node) const
{ {
/* Complex plane /* Complex plane
* ab = cosA +i*sinA * ab = cosA +i*sinA
@ -272,10 +324,10 @@ double Sweep::HoleAngle(Node& node)
* Where x = ax*bx + ay*by * Where x = ax*bx + ay*by
* y = ax*by - ay*bx * y = ax*by - ay*bx
*/ */
double ax = node.next->point->x - node.point->x; const double ax = node.next->point->x - node.point->x;
double ay = node.next->point->y - node.point->y; const double ay = node.next->point->y - node.point->y;
double bx = node.prev->point->x - node.point->x; const double bx = node.prev->point->x - node.point->x;
double by = node.prev->point->y - node.point->y; const double by = node.prev->point->y - node.point->y;
return atan2(ax * by - ay * bx, ax * bx + ay * by); return atan2(ax * by - ay * bx, ax * bx + ay * by);
} }
@ -340,43 +392,43 @@ bool Sweep::Legalize(SweepContext& tcx, Triangle& t)
return false; return false;
} }
bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd) bool Sweep::Incircle(const Point& pa, const Point& pb, const Point& pc, const Point& pd) const
{ {
double adx = pa.x - pd.x; const double adx = pa.x - pd.x;
double ady = pa.y - pd.y; const double ady = pa.y - pd.y;
double bdx = pb.x - pd.x; const double bdx = pb.x - pd.x;
double bdy = pb.y - pd.y; const double bdy = pb.y - pd.y;
double adxbdy = adx * bdy; const double adxbdy = adx * bdy;
double bdxady = bdx * ady; const double bdxady = bdx * ady;
double oabd = adxbdy - bdxady; const double oabd = adxbdy - bdxady;
if (oabd <= 0) if (oabd <= 0)
return false; return false;
double cdx = pc.x - pd.x; const double cdx = pc.x - pd.x;
double cdy = pc.y - pd.y; const double cdy = pc.y - pd.y;
double cdxady = cdx * ady; const double cdxady = cdx * ady;
double adxcdy = adx * cdy; const double adxcdy = adx * cdy;
double ocad = cdxady - adxcdy; const double ocad = cdxady - adxcdy;
if (ocad <= 0) if (ocad <= 0)
return false; return false;
double bdxcdy = bdx * cdy; const double bdxcdy = bdx * cdy;
double cdxbdy = cdx * bdy; const double cdxbdy = cdx * bdy;
double alift = adx * adx + ady * ady; const double alift = adx * adx + ady * ady;
double blift = bdx * bdx + bdy * bdy; const double blift = bdx * bdx + bdy * bdy;
double clift = cdx * cdx + cdy * cdy; const double clift = cdx * cdx + cdy * cdy;
double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd; const double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd;
return det > 0; return det > 0;
} }
void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) const
{ {
Triangle* n1, *n2, *n3, *n4; Triangle* n1, *n2, *n3, *n4;
n1 = t.NeighborCCW(p); n1 = t.NeighborCCW(p);
@ -708,11 +760,8 @@ Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)
} else if (o2d == CCW) { } else if (o2d == CCW) {
// Left // Left
return *ot.PointCW(op); return *ot.PointCW(op);
} else{
//throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
// ASSIMP_CHANGE (aramis_acg)
throw std::runtime_error("[Unsupported] Opposing point on constrained edge");
} }
throw std::runtime_error("[Unsupported] Opposing point on constrained edge");
} }
void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle, void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,
@ -740,7 +789,7 @@ void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle&
Sweep::~Sweep() { Sweep::~Sweep() {
// Clean up memory // Clean up memory
for(unsigned int i = 0; i < nodes_.size(); i++) { for(size_t i = 0; i < nodes_.size(); i++) {
delete nodes_[i]; delete nodes_[i];
} }

View File

@ -33,7 +33,7 @@
* Zalik, B.(2008)'Sweep-line algorithm for constrained Delaunay triangulation', * Zalik, B.(2008)'Sweep-line algorithm for constrained Delaunay triangulation',
* International Journal of Geographical Information Science * International Journal of Geographical Information Science
* *
* "FlipScan" Constrained Edge Algorithm invented by Thomas Åhlén, thahlen@gmail.com * "FlipScan" Constrained Edge Algorithm invented by Thomas ?hl?n, thahlen@gmail.com
*/ */
#ifndef SWEEP_H #ifndef SWEEP_H
@ -142,7 +142,7 @@ private:
* @param d - point opposite a * @param d - point opposite a
* @return true if d is inside circle, false if on circle edge * @return true if d is inside circle, false if on circle edge
*/ */
bool Incircle(Point& pa, Point& pb, Point& pc, Point& pd); bool Incircle(const Point& pa, const Point& pb, const Point& pc, const Point& pd) const;
/** /**
* Rotates a triangle pair one vertex CW * Rotates a triangle pair one vertex CW
@ -158,7 +158,7 @@ private:
* n4 n4 * n4 n4
* </pre> * </pre>
*/ */
void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op); void RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op) const;
/** /**
* Fills holes in the Advancing Front * Fills holes in the Advancing Front
@ -169,17 +169,24 @@ private:
*/ */
void FillAdvancingFront(SweepContext& tcx, Node& n); void FillAdvancingFront(SweepContext& tcx, Node& n);
// Decision-making about when to Fill hole.
// Contributed by ToolmakerSteve2
bool LargeHole_DontFill(const Node* node) const;
bool AngleExceeds90Degrees(const Point* origin, const Point* pa, const Point* pb) const;
bool AngleExceedsPlus90DegreesOrIsNegative(const Point* origin, const Point* pa, const Point* pb) const;
double Angle(const Point* origin, const Point* pa, const Point* pb) const;
/** /**
* *
* @param node - middle node * @param node - middle node
* @return the angle between 3 front nodes * @return the angle between 3 front nodes
*/ */
double HoleAngle(Node& node); double HoleAngle(const Node& node) const;
/** /**
* The basin angle is decided against the horizontal line [1,0] * The basin angle is decided against the horizontal line [1,0]
*/ */
double BasinAngle(Node& node); double BasinAngle(const Node& node) const;
/** /**
* Fills a basin that has formed on the Advancing Front to the right * Fills a basin that has formed on the Advancing Front to the right

View File

@ -34,17 +34,18 @@
namespace p2t { namespace p2t {
SweepContext::SweepContext(std::vector<Point*> polyline) SweepContext::SweepContext(const std::vector<Point*>& polyline) : points_(polyline),
front_(0),
head_(0),
tail_(0),
af_head_(0),
af_middle_(0),
af_tail_(0)
{ {
basin = Basin();
edge_event = EdgeEvent();
points_ = polyline;
InitEdges(points_); InitEdges(points_);
} }
void SweepContext::AddHole(std::vector<Point*> polyline) void SweepContext::AddHole(const std::vector<Point*>& polyline)
{ {
InitEdges(polyline); InitEdges(polyline);
for(unsigned int i = 0; i < polyline.size(); i++) { for(unsigned int i = 0; i < polyline.size(); i++) {
@ -56,12 +57,12 @@ void SweepContext::AddPoint(Point* point) {
points_.push_back(point); points_.push_back(point);
} }
std::vector<Triangle*> SweepContext::GetTriangles() std::vector<Triangle*> &SweepContext::GetTriangles()
{ {
return triangles_; return triangles_;
} }
std::list<Triangle*> SweepContext::GetMap() std::list<Triangle*> &SweepContext::GetMap()
{ {
return map_; return map_;
} }
@ -94,16 +95,16 @@ void SweepContext::InitTriangulation()
} }
void SweepContext::InitEdges(std::vector<Point*> polyline) void SweepContext::InitEdges(const std::vector<Point*>& polyline)
{ {
int num_points = static_cast<int>(polyline.size()); size_t num_points = polyline.size();
for (int i = 0; i < num_points; i++) { for (size_t i = 0; i < num_points; i++) {
int j = i < num_points - 1 ? i + 1 : 0; size_t j = i < num_points - 1 ? i + 1 : 0;
edge_list.push_back(new Edge(*polyline[i], *polyline[j])); edge_list.push_back(new Edge(*polyline[i], *polyline[j]));
} }
} }
Point* SweepContext::GetPoint(const int& index) Point* SweepContext::GetPoint(size_t index)
{ {
return points_[index]; return points_[index];
} }
@ -113,13 +114,13 @@ void SweepContext::AddToMap(Triangle* triangle)
map_.push_back(triangle); map_.push_back(triangle);
} }
Node& SweepContext::LocateNode(Point& point) Node& SweepContext::LocateNode(const Point& point)
{ {
// TODO implement search tree // TODO implement search tree
return *front_->LocateNode(point.x); return *front_->LocateNode(point.x);
} }
void SweepContext::CreateAdvancingFront(std::vector<Node*> nodes) void SweepContext::CreateAdvancingFront(const std::vector<Node*>& nodes)
{ {
(void) nodes; (void) nodes;
@ -164,12 +165,20 @@ void SweepContext::RemoveFromMap(Triangle* triangle)
void SweepContext::MeshClean(Triangle& triangle) void SweepContext::MeshClean(Triangle& triangle)
{ {
if (!triangle.IsInterior()) { std::vector<Triangle *> triangles;
triangle.IsInterior(true); triangles.push_back(&triangle);
triangles_.push_back(&triangle);
for (int i = 0; i < 3; i++) { while(!triangles.empty()){
if (!triangle.constrained_edge[i]) Triangle *t = triangles.back();
MeshClean(*triangle.GetNeighbor(i)); triangles.pop_back();
if (t != NULL && !t->IsInterior()) {
t->IsInterior(true);
triangles_.push_back(t);
for (int i = 0; i < 3; i++) {
if (!t->constrained_edge[i])
triangles.push_back(t->GetNeighbor(i));
}
} }
} }
} }

View File

@ -52,47 +52,47 @@ class SweepContext {
public: public:
/// Constructor /// Constructor
SweepContext(std::vector<Point*> polyline); SweepContext(const std::vector<Point*>& polyline);
/// Destructor /// Destructor
~SweepContext(); ~SweepContext();
void set_head(Point* p1); void set_head(Point* p1);
Point* head(); Point* head() const;
void set_tail(Point* p1); void set_tail(Point* p1);
Point* tail(); Point* tail() const;
int point_count(); size_t point_count() const;
Node& LocateNode(Point& point); Node& LocateNode(const Point& point);
void RemoveNode(Node* node); void RemoveNode(Node* node);
void CreateAdvancingFront(std::vector<Node*> nodes); void CreateAdvancingFront(const std::vector<Node*>& nodes);
/// Try to map a node to all sides of this triangle that don't have a neighbor /// Try to map a node to all sides of this triangle that don't have a neighbor
void MapTriangleToNodes(Triangle& t); void MapTriangleToNodes(Triangle& t);
void AddToMap(Triangle* triangle); void AddToMap(Triangle* triangle);
Point* GetPoint(const int& index); Point* GetPoint(size_t index);
Point* GetPoints(); Point* GetPoints();
void RemoveFromMap(Triangle* triangle); void RemoveFromMap(Triangle* triangle);
void AddHole(std::vector<Point*> polyline); void AddHole(const std::vector<Point*>& polyline);
void AddPoint(Point* point); void AddPoint(Point* point);
AdvancingFront* front(); AdvancingFront* front() const;
void MeshClean(Triangle& triangle); void MeshClean(Triangle& triangle);
std::vector<Triangle*> GetTriangles(); std::vector<Triangle*> &GetTriangles();
std::list<Triangle*> GetMap(); std::list<Triangle*> &GetMap();
std::vector<Edge*> edge_list; std::vector<Edge*> edge_list;
@ -147,18 +147,18 @@ Point* tail_;
Node *af_head_, *af_middle_, *af_tail_; Node *af_head_, *af_middle_, *af_tail_;
void InitTriangulation(); void InitTriangulation();
void InitEdges(std::vector<Point*> polyline); void InitEdges(const std::vector<Point*>& polyline);
}; };
inline AdvancingFront* SweepContext::front() inline AdvancingFront* SweepContext::front() const
{ {
return front_; return front_;
} }
inline int SweepContext::point_count() inline size_t SweepContext::point_count() const
{ {
return static_cast<int>(points_.size()); return points_.size();
} }
inline void SweepContext::set_head(Point* p1) inline void SweepContext::set_head(Point* p1)
@ -166,7 +166,7 @@ inline void SweepContext::set_head(Point* p1)
head_ = p1; head_ = p1;
} }
inline Point* SweepContext::head() inline Point* SweepContext::head() const
{ {
return head_; return head_;
} }
@ -176,7 +176,7 @@ inline void SweepContext::set_tail(Point* p1)
tail_ = p1; tail_ = p1;
} }
inline Point* SweepContext::tail() inline Point* SweepContext::tail() const
{ {
return tail_; return tail_;
} }