2013-10-27 14:56:01 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2013, assimp team
|
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2013-10-27 14:56:01 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2013-10-27 14:56:01 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2013-10-27 14:56:01 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2013-10-27 14:56:01 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2013-10-27 14:56:01 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderTessellator.cpp
|
|
|
|
* @brief A simple tessellation wrapper
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
|
|
|
|
|
|
|
|
#include "BlenderDNA.h"
|
|
|
|
#include "BlenderScene.h"
|
|
|
|
#include "BlenderBMesh.h"
|
|
|
|
#include "BlenderTessellator.h"
|
|
|
|
|
2014-04-21 14:18:14 +00:00
|
|
|
static const unsigned int BLEND_TESS_MAGIC = 0x83ed9ac3;
|
2013-10-27 14:56:01 +00:00
|
|
|
|
|
|
|
#if ASSIMP_BLEND_WITH_GLU_TESSELLATE
|
|
|
|
|
2014-04-21 14:18:14 +00:00
|
|
|
namspace Assimp
|
2013-10-27 14:56:01 +00:00
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
template< > const std::string LogFunctions< BlenderTessellatorGL >::log_prefix = "BLEND_TESS_GL: ";
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Blender;
|
|
|
|
|
|
|
|
#ifndef CALLBACK
|
|
|
|
#define CALLBACK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
BlenderTessellatorGL::BlenderTessellatorGL( BlenderBMeshConverter& converter ):
|
2015-05-19 03:57:13 +00:00
|
|
|
converter( &converter )
|
2013-10-27 14:56:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
BlenderTessellatorGL::~BlenderTessellatorGL( )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::Tessellate( const MLoop* polyLoop, int vertexCount, const std::vector< MVert >& vertices )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
AssertVertexCount( vertexCount );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector< VertexGL > polyLoopGL;
|
|
|
|
GenerateLoopVerts( polyLoopGL, polyLoop, vertexCount, vertices );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
TessDataGL tessData;
|
|
|
|
Tesssellate( polyLoopGL, tessData );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
TriangulateDrawCalls( tessData );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::AssertVertexCount( int vertexCount )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
if ( vertexCount <= 4 )
|
|
|
|
{
|
|
|
|
ThrowException( "Expected more than 4 vertices for tessellation" );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::GenerateLoopVerts( std::vector< VertexGL >& polyLoopGL, const MLoop* polyLoop, int vertexCount, const std::vector< MVert >& vertices )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( int i = 0; i < vertexCount; ++i )
|
|
|
|
{
|
|
|
|
const MLoop& loopItem = polyLoop[ i ];
|
|
|
|
const MVert& vertex = vertices[ loopItem.v ];
|
|
|
|
polyLoopGL.push_back( VertexGL( vertex.co[ 0 ], vertex.co[ 1 ], vertex.co[ 2 ], loopItem.v, BLEND_TESS_MAGIC ) );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::Tesssellate( std::vector< VertexGL >& polyLoopGL, TessDataGL& tessData )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
GLUtesselator* tessellator = gluNewTess( );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_BEGIN_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateBegin ) );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_END_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateEnd ) );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_VERTEX_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateVertex ) );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_COMBINE_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateCombine ) );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_EDGE_FLAG_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateEdgeFlag ) );
|
|
|
|
gluTessCallback( tessellator, GLU_TESS_ERROR_DATA, reinterpret_cast< void ( CALLBACK * )( ) >( TessellateError ) );
|
|
|
|
gluTessProperty( tessellator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
gluTessBeginPolygon( tessellator, &tessData );
|
|
|
|
gluTessBeginContour( tessellator );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( unsigned int i = 0; i < polyLoopGL.size( ); ++i )
|
|
|
|
{
|
|
|
|
gluTessVertex( tessellator, reinterpret_cast< GLdouble* >( &polyLoopGL[ i ] ), &polyLoopGL[ i ] );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
gluTessEndContour( tessellator );
|
|
|
|
gluTessEndPolygon( tessellator );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TriangulateDrawCalls( const TessDataGL& tessData )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// NOTE - Because we are supplying a callback to GLU_TESS_EDGE_FLAG_DATA we don't technically
|
|
|
|
// need support for GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN but we'll keep it here in case
|
|
|
|
// GLU tessellate changes or tristrips and fans are wanted.
|
|
|
|
// See: http://www.opengl.org/sdk/docs/man2/xhtml/gluTessCallback.xml
|
|
|
|
for ( unsigned int i = 0; i < tessData.drawCalls.size( ); ++i )
|
|
|
|
{
|
|
|
|
const DrawCallGL& drawCallGL = tessData.drawCalls[ i ];
|
|
|
|
const VertexGL* vertices = &tessData.vertices[ drawCallGL.baseVertex ];
|
|
|
|
if ( drawCallGL.drawMode == GL_TRIANGLES )
|
|
|
|
{
|
|
|
|
MakeFacesFromTris( vertices, drawCallGL.vertexCount );
|
|
|
|
}
|
|
|
|
else if ( drawCallGL.drawMode == GL_TRIANGLE_STRIP )
|
|
|
|
{
|
|
|
|
MakeFacesFromTriStrip( vertices, drawCallGL.vertexCount );
|
|
|
|
}
|
|
|
|
else if ( drawCallGL.drawMode == GL_TRIANGLE_FAN )
|
|
|
|
{
|
|
|
|
MakeFacesFromTriFan( vertices, drawCallGL.vertexCount );
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::MakeFacesFromTris( const VertexGL* vertices, int vertexCount )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
int triangleCount = vertexCount / 3;
|
|
|
|
for ( int i = 0; i < triangleCount; ++i )
|
|
|
|
{
|
|
|
|
int vertexBase = i * 3;
|
|
|
|
converter->AddFace( vertices[ vertexBase + 0 ].index, vertices[ vertexBase + 1 ].index, vertices[ vertexBase + 2 ].index );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::MakeFacesFromTriStrip( const VertexGL* vertices, int vertexCount )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
int triangleCount = vertexCount - 2;
|
|
|
|
for ( int i = 0; i < triangleCount; ++i )
|
|
|
|
{
|
|
|
|
int vertexBase = i;
|
|
|
|
converter->AddFace( vertices[ vertexBase + 0 ].index, vertices[ vertexBase + 1 ].index, vertices[ vertexBase + 2 ].index );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::MakeFacesFromTriFan( const VertexGL* vertices, int vertexCount )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
int triangleCount = vertexCount - 2;
|
|
|
|
for ( int i = 0; i < triangleCount; ++i )
|
|
|
|
{
|
|
|
|
int vertexBase = i;
|
|
|
|
converter->AddFace( vertices[ 0 ].index, vertices[ vertexBase + 1 ].index, vertices[ vertexBase + 2 ].index );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateBegin( GLenum drawModeGL, void* userData )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
TessDataGL& tessData = *reinterpret_cast< TessDataGL* >( userData );
|
|
|
|
tessData.drawCalls.push_back( DrawCallGL( drawModeGL, tessData.vertices.size( ) ) );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateEnd( void* )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// Do nothing
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateVertex( const void* vtxData, void* userData )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
TessDataGL& tessData = *reinterpret_cast< TessDataGL* >( userData );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const VertexGL& vertex = *reinterpret_cast< const VertexGL* >( vtxData );
|
|
|
|
if ( vertex.magic != BLEND_TESS_MAGIC )
|
|
|
|
{
|
|
|
|
ThrowException( "Point returned by GLU Tessellate was probably not one of ours. This indicates we need a new way to store vertex information" );
|
|
|
|
}
|
|
|
|
tessData.vertices.push_back( vertex );
|
|
|
|
if ( tessData.drawCalls.size( ) == 0 )
|
|
|
|
{
|
|
|
|
ThrowException( "\"Vertex\" callback received before \"Begin\"" );
|
|
|
|
}
|
|
|
|
++( tessData.drawCalls.back( ).vertexCount );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateCombine( const GLdouble intersection[ 3 ], const GLdouble* [ 4 ], const GLfloat [ 4 ], GLdouble** out, void* userData )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException( "Intersected polygon loops are not yet supported" );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateEdgeFlag( GLboolean, void* )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
// Do nothing
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorGL::TessellateError( GLenum errorCode, void* )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException( reinterpret_cast< const char* >( gluErrorString( errorCode ) ) );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ASSIMP_BLEND_WITH_GLU_TESSELLATE
|
|
|
|
|
|
|
|
#if ASSIMP_BLEND_WITH_POLY_2_TRI
|
|
|
|
|
|
|
|
namespace Assimp
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
template< > const std::string LogFunctions< BlenderTessellatorP2T >::log_prefix = "BLEND_TESS_P2T: ";
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Blender;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
BlenderTessellatorP2T::BlenderTessellatorP2T( BlenderBMeshConverter& converter ):
|
2015-05-19 03:57:13 +00:00
|
|
|
converter( &converter )
|
2013-10-27 14:56:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
BlenderTessellatorP2T::~BlenderTessellatorP2T( )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::Tessellate( const MLoop* polyLoop, int vertexCount, const std::vector< MVert >& vertices )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
AssertVertexCount( vertexCount );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// NOTE - We have to hope that points in a Blender polygon are roughly on the same plane.
|
|
|
|
// There may be some triangulation artifacts if they are wildly different.
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector< PointP2T > points;
|
|
|
|
Copy3DVertices( polyLoop, vertexCount, vertices, points );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PlaneP2T plane = FindLLSQPlane( points );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix4x4 transform = GeneratePointTransformMatrix( plane );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
TransformAndFlattenVectices( transform, points );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector< p2t::Point* > pointRefs;
|
|
|
|
ReferencePoints( points, pointRefs );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
p2t::CDT cdt( pointRefs );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
cdt.Triangulate( );
|
|
|
|
std::vector< p2t::Triangle* > triangles = cdt.GetTriangles( );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
MakeFacesFromTriangles( triangles );
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::AssertVertexCount( int vertexCount )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
if ( vertexCount <= 4 )
|
|
|
|
{
|
|
|
|
ThrowException( "Expected more than 4 vertices for tessellation" );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::Copy3DVertices( const MLoop* polyLoop, int vertexCount, const std::vector< MVert >& vertices, std::vector< PointP2T >& points ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
points.resize( vertexCount );
|
|
|
|
for ( int i = 0; i < vertexCount; ++i )
|
|
|
|
{
|
|
|
|
const MLoop& loop = polyLoop[ i ];
|
|
|
|
const MVert& vert = vertices[ loop.v ];
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PointP2T& point = points[ i ];
|
|
|
|
point.point3D.Set( vert.co[ 0 ], vert.co[ 1 ], vert.co[ 2 ] );
|
|
|
|
point.index = loop.v;
|
|
|
|
point.magic = BLEND_TESS_MAGIC;
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiMatrix4x4 BlenderTessellatorP2T::GeneratePointTransformMatrix( const Blender::PlaneP2T& plane ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
aiVector3D sideA( 1.0f, 0.0f, 0.0f );
|
|
|
|
if ( std::fabs( plane.normal * sideA ) > 0.999f )
|
|
|
|
{
|
|
|
|
sideA = aiVector3D( 0.0f, 1.0f, 0.0f );
|
|
|
|
}
|
|
|
|
|
|
|
|
aiVector3D sideB( plane.normal ^ sideA );
|
|
|
|
sideB.Normalize( );
|
|
|
|
sideA = sideB ^ plane.normal;
|
|
|
|
|
|
|
|
aiMatrix4x4 result;
|
|
|
|
result.a1 = sideA.x;
|
|
|
|
result.a2 = sideA.y;
|
|
|
|
result.a3 = sideA.z;
|
|
|
|
result.b1 = sideB.x;
|
|
|
|
result.b2 = sideB.y;
|
|
|
|
result.b3 = sideB.z;
|
|
|
|
result.c1 = plane.normal.x;
|
|
|
|
result.c2 = plane.normal.y;
|
|
|
|
result.c3 = plane.normal.z;
|
|
|
|
result.a4 = plane.centre.x;
|
|
|
|
result.b4 = plane.centre.y;
|
|
|
|
result.c4 = plane.centre.z;
|
|
|
|
result.Inverse( );
|
|
|
|
|
|
|
|
return result;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::TransformAndFlattenVectices( const aiMatrix4x4& transform, std::vector< Blender::PointP2T >& vertices ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( unsigned int i = 0; i < vertices.size( ); ++i )
|
|
|
|
{
|
|
|
|
PointP2T& point = vertices[ i ];
|
|
|
|
point.point3D = transform * point.point3D;
|
|
|
|
point.point2D.set( point.point3D.y, point.point3D.z );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::ReferencePoints( std::vector< Blender::PointP2T >& points, std::vector< p2t::Point* >& pointRefs ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
pointRefs.resize( points.size( ) );
|
|
|
|
for ( unsigned int i = 0; i < points.size( ); ++i )
|
|
|
|
{
|
|
|
|
pointRefs[ i ] = &points[ i ].point2D;
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Yes this is filthy... but we have no choice
|
2013-11-22 18:54:47 +00:00
|
|
|
#define OffsetOf( Class, Member ) ( static_cast< unsigned int >( \
|
2015-05-19 03:57:13 +00:00
|
|
|
reinterpret_cast<uint8_t*>(&( reinterpret_cast< Class* >( NULL )->*( &Class::Member ) )) - \
|
|
|
|
static_cast<uint8_t*>(NULL) ) )
|
2013-11-22 18:54:47 +00:00
|
|
|
|
2013-10-27 14:56:01 +00:00
|
|
|
inline PointP2T& BlenderTessellatorP2T::GetActualPointStructure( p2t::Point& point ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int pointOffset = OffsetOf( PointP2T, point2D );
|
|
|
|
PointP2T& pointStruct = *reinterpret_cast< PointP2T* >( reinterpret_cast< char* >( &point ) - pointOffset );
|
|
|
|
if ( pointStruct.magic != static_cast<int>( BLEND_TESS_MAGIC ) )
|
|
|
|
{
|
|
|
|
ThrowException( "Point returned by poly2tri was probably not one of ours. This indicates we need a new way to store vertex information" );
|
|
|
|
}
|
|
|
|
return pointStruct;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BlenderTessellatorP2T::MakeFacesFromTriangles( std::vector< p2t::Triangle* >& triangles ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( unsigned int i = 0; i < triangles.size( ); ++i )
|
|
|
|
{
|
|
|
|
p2t::Triangle& Triangle = *triangles[ i ];
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
PointP2T& pointA = GetActualPointStructure( *Triangle.GetPoint( 0 ) );
|
|
|
|
PointP2T& pointB = GetActualPointStructure( *Triangle.GetPoint( 1 ) );
|
|
|
|
PointP2T& pointC = GetActualPointStructure( *Triangle.GetPoint( 2 ) );
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
converter->AddFace( pointA.index, pointB.index, pointC.index );
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
inline float p2tMax( float a, float b )
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
return a > b ? a : b;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Adapted from: http://missingbytes.blogspot.co.uk/2012/06/fitting-plane-to-point-cloud.html
|
|
|
|
float BlenderTessellatorP2T::FindLargestMatrixElem( const aiMatrix3x3& mtx ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
float result = 0.0f;
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( int x = 0; x < 3; ++x )
|
|
|
|
{
|
|
|
|
for ( int y = 0; y < 3; ++y )
|
|
|
|
{
|
|
|
|
result = p2tMax( std::fabs( mtx[ x ][ y ] ), result );
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return result;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Aparently Assimp doesn't have matrix scaling
|
|
|
|
aiMatrix3x3 BlenderTessellatorP2T::ScaleMatrix( const aiMatrix3x3& mtx, float scale ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMatrix3x3 result;
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
for ( int x = 0; x < 3; ++x )
|
|
|
|
{
|
|
|
|
for ( int y = 0; y < 3; ++y )
|
|
|
|
{
|
|
|
|
result[ x ][ y ] = mtx[ x ][ y ] * scale;
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return result;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Adapted from: http://missingbytes.blogspot.co.uk/2012/06/fitting-plane-to-point-cloud.html
|
|
|
|
aiVector3D BlenderTessellatorP2T::GetEigenVectorFromLargestEigenValue( const aiMatrix3x3& mtx ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
float scale = FindLargestMatrixElem( mtx );
|
|
|
|
aiMatrix3x3 mc = ScaleMatrix( mtx, 1.0f / scale );
|
|
|
|
mc = mc * mc * mc;
|
|
|
|
|
|
|
|
aiVector3D v( 1.0f );
|
|
|
|
aiVector3D lastV = v;
|
|
|
|
for ( int i = 0; i < 100; ++i )
|
|
|
|
{
|
|
|
|
v = mc * v;
|
|
|
|
v.Normalize( );
|
|
|
|
if ( ( v - lastV ).SquareLength( ) < 1e-16f )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastV = v;
|
|
|
|
}
|
|
|
|
return v;
|
2015-05-19 03:52:10 +00:00
|
|
|
}
|
2013-10-27 14:56:01 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Adapted from: http://missingbytes.blogspot.co.uk/2012/06/fitting-plane-to-point-cloud.html
|
|
|
|
PlaneP2T BlenderTessellatorP2T::FindLLSQPlane( const std::vector< PointP2T >& points ) const
|
|
|
|
{
|
2015-05-19 03:57:13 +00:00
|
|
|
PlaneP2T result;
|
|
|
|
|
|
|
|
aiVector3D sum( 0.0f );
|
|
|
|
for ( unsigned int i = 0; i < points.size( ); ++i )
|
|
|
|
{
|
|
|
|
sum += points[ i ].point3D;
|
|
|
|
}
|
|
|
|
result.centre = sum * ( 1.0f / points.size( ) );
|
|
|
|
|
|
|
|
float sumXX = 0.0f;
|
|
|
|
float sumXY = 0.0f;
|
|
|
|
float sumXZ = 0.0f;
|
|
|
|
float sumYY = 0.0f;
|
|
|
|
float sumYZ = 0.0f;
|
|
|
|
float sumZZ = 0.0f;
|
|
|
|
for ( unsigned int i = 0; i < points.size( ); ++i )
|
|
|
|
{
|
|
|
|
aiVector3D offset = points[ i ].point3D - result.centre;
|
|
|
|
sumXX += offset.x * offset.x;
|
|
|
|
sumXY += offset.x * offset.y;
|
|
|
|
sumXZ += offset.x * offset.z;
|
|
|
|
sumYY += offset.y * offset.y;
|
|
|
|
sumYZ += offset.y * offset.z;
|
|
|
|
sumZZ += offset.z * offset.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiMatrix3x3 mtx( sumXX, sumXY, sumXZ, sumXY, sumYY, sumYZ, sumXZ, sumYZ, sumZZ );
|
|
|
|
|
|
|
|
float det = mtx.Determinant( );
|
|
|
|
if ( det == 0.0f )
|
|
|
|
{
|
|
|
|
result.normal = aiVector3D( 0.0f );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aiMatrix3x3 invMtx = mtx;
|
|
|
|
invMtx.Inverse( );
|
|
|
|
result.normal = GetEigenVectorFromLargestEigenValue( invMtx );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-10-27 14:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ASSIMP_BLEND_WITH_POLY_2_TRI
|
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_BLEND_IMPORTER
|