inline
po::options_description
makeOptions()
{
    po::options_description desc_options( "test_interp_twomesh options" );
    desc_options.add_options()
    ( "hsize1", po::value<double>()->default_value( 0.1 ), "mesh size1" ) //0.05
    ( "hsize2", po::value<double>()->default_value( 0.3 ), "mesh size2" ) //0.03
    ( "userelation", po::value<bool>()->default_value( false ), "mesh size2" ) //0.03
    ;
    return desc_options.add( Feel::feel_options() );
}

#if 1
#define BOOST_TEST_MODULE interp_twomesh tests
#include <feel/feelcore/testsuite.hpp>
#endif

#include <feel/options.hpp>
#include <feel/feeldiscr/pch.hpp>
#include <feel/feeldiscr/pchv.hpp>
#include <feel/feelvf/vf.hpp>
#include <feel/feelfilters/geotool.hpp>

#include <iostream>

using namespace Feel;
using namespace Feel::vf;

namespace test_interp_twomesh
{
  /*
typedef Application Application_type;
typedef std::shared_ptr<Application_type> Application_ptrtype;

enum type_champs
{
    ScalarTest = 0,
    VectorialTest
};
  */

  //Application_ptrtype test_app;
/*_________________________________________________*
 * Options
 *_________________________________________________*/

/*_________________________________________________*
 * About
 *_________________________________________________*/

inline
AboutData
makeAbout()
{
    AboutData about( "test_interp_twomesh" ,
                     "test_interp_twomesh" ,
                     "0.1",
                     "Test1 verify geomap such as phi*ph^{-1}=Id ; Test2: on gamma, ||..v(u1)-..v(u2)|| < epsilon",
                     Feel::AboutData::License_GPL,
                     "Copyright (c) 2010 Universite Joseph Fourier" );

    about.addAuthor( "Vincent Chabannes", "developer", "example@nnthink.com", "" );
    return about;

}

/*_________________________________________________*
 * run_test_geomap
 *_________________________________________________*/

template<uint32_type Dim,uint32_type OrderChamp,uint32_type OrderGeo>
void
run_test_geomap()
{

    LOG(INFO) << "[testGeoMap] start test geomap inverse\n";

    typedef Mesh<Simplex<Dim,OrderGeo,Dim> > mesh_type;
    typedef std::shared_ptr<  mesh_type > mesh_ptrtype;

    //Geometry
    double meshSize1 = doption(_name="hsize1");
    double meshSize2 = doption(_name="hsize2");
    GeoTool::Node x1( 0,0 );
    GeoTool::Node x2( 1,1 );
    GeoTool::Rectangle R1( meshSize1,"LEFT",x1,x2 );
    GeoTool::Node x_centre( 0.5,0.5 );
    GeoTool::Node x_bord( 0.8,0.5 );
    GeoTool::Circle C( meshSize2,"uncercle",x_centre,x_bord );
    //Mesh
    mesh_ptrtype mesh = ( R1-C ).createMesh(_mesh=new mesh_type,
                                            _name= "mesh_geomap_" + mesh_type::shape_type::name() );

    //geomap
    typedef typename mesh_type::gm_type gm_type;
    typedef typename std::shared_ptr<gm_type> gm_ptrtype;
    gm_ptrtype __gm = mesh->gm();

    //geomap context
    typedef typename mesh_type::element_type geoelement_type;
    typedef typename gm_type::template Context<vm::POINT, geoelement_type> gmc_type;
    typedef std::shared_ptr<gmc_type> gmc_ptrtype;

    //pts de Gauss
    typename _Q<OrderChamp>::template apply<mesh_type::nDim,typename mesh_type::value_type, Simplex >::type im;

    // iterators sur les elements du maillage
    typename mesh_type::element_iterator el_it;
    typename mesh_type::element_iterator el_en;
    boost::tie( boost::tuples::ignore, el_it, el_en ) = Feel::elements( *mesh );

    //init inverse geometric transformation phi^{-1}
    typename mesh_type::Inverse::gic_type gic( __gm, *el_it );

    typedef typename mesh_type::gm_type::precompute_type geopc_type;
    typedef typename mesh_type::gm_type::precompute_ptrtype geopc_ptrtype;
    geopc_ptrtype __geopc( new geopc_type( __gm, im.points() ) );

    for ( ; el_it!=el_en; ++el_it )
    {
        //init and apply the geometric transformation phi
        gmc_ptrtype __c( new gmc_type( __gm,
                                       *el_it,//mesh->element( *el_it ),
                                       __geopc ) );
#if 0
        //init inverse geometric transformation phi^{-1}
        typename mesh_type::Inverse::gic_type gic( __gm, *el_it );
#else
        gic.update( *el_it );
#endif
        for ( uint32_type i=0; i< __c->xReal().size2(); ++i )
        {

            // get phi for one point
            typename mesh_type::node_type n = Feel::ublas::column( __c->xReal(), i );

            gic.setXReal( n );

            //verify that : phi°phi^{-1}=Id
            for ( uint32_type j=0; j<n.size(); ++j )
            {
                double err = std::abs( Feel::ublas::column( im.points(),i )( j ) - gic.xRef()( j ) );
#if USE_BOOST_TEST
                BOOST_CHECK( err<1e-9 );
#endif
            }

        }

    }

    LOG(INFO) << "[testGeoMap] finish test geomap inverse\n";

} //end run

/*_________________________________________________*
 * run_interp_boundary
 *_________________________________________________*/

template<uint32_type Dim,uint32_type OrderChamp,uint32_type OrderGeo>
void
test_interp_boundary( boost::tuple<
                      std::shared_ptr< Mesh<Simplex<Dim,OrderGeo,Dim> > >,
                      std::shared_ptr< Mesh<Simplex<Dim,OrderGeo,Dim> > >,
                      bool> __mesh/*,
                                    boost::mpl::int_<ScalarTest>*/
                    )
{
    auto mesh1 = boost::get<0>( __mesh );
    auto mesh2 = boost::get<1>( __mesh );
    auto Xh1 = Pch<OrderChamp>( mesh1 );
    auto Xh2 = Pch<OrderChamp>( mesh2 );
    auto Yh1 = Pchv<OrderChamp>( mesh1 );
    auto Yh2 = Pchv<OrderChamp>( mesh2 );

    auto theexpr = Px()*Px()*Py()*Py();

    auto u1 = vf::project( _space=Xh1, _range=elements( mesh1 ), _expr=theexpr );
    auto u2 = vf::project( _space=Xh2, _range=elements( mesh2 ), _expr=theexpr );
    auto v1 = vf::project( _space=Yh1, _range=elements( mesh1 ), _expr=theexpr*one() );
    auto v2 = vf::project( _space=Yh2, _range=elements( mesh2 ), _expr=theexpr*one() );

    bool useConformalIntegration=boost::get<2>( __mesh );//true;//false;
    double  __errId = normL2( _range=markedfaces( mesh1, "Interface" ),
                              _expr=idv( u1 )-idv( u2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] idv(u1) error : " << __errId );
    BOOST_CHECK_SMALL( __errId,1e-12 );
#else
    LOG(INFO) << "[testBoundary] idv(u1) error : " << __errId << "\n";
    std::cout << "[testBoundary] idv(u1) error : " << __errId << "\n";
#endif
    double __errGrad = normL2( _range=markedfaces( mesh1, "Interface" ),
                               _expr= gradv( u1 )-gradv( u2 , useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] gradv(u1) error : " << __errGrad );
    BOOST_CHECK_SMALL( __errGrad,1e-12 );
#else
    LOG(INFO) << "[testBoundary] gradv(u1) error : " << __errGrad <<"\n";
    std::cout << "[testBoundary] gradv(u1) error : " << __errGrad <<"\n";
#endif

    /*
    std::cout << "\ndiv 1 :" << std::sqrt(integrate( markedfaces(mesh1, mesh1->markerName("Interface")),
                                            (divv(v1))*(divv(v1)) ).evaluate()(0,0));
    std::cout << "\ndiv 2" << std::sqrt(integrate( markedfaces(mesh1, mesh1->markerName("Interface")),
                                                   print((divv(v2))*(divv(v2)),"div2") ).evaluate()(0,0));
    */
    double  __errDx = normL2( _range=markedfaces( mesh1, "Interface" ),
                              _expr=dxv( u1 )-dxv( u2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] dxv(u1) error : " << __errDx );
    BOOST_CHECK_SMALL( __errDx,1e-12 );
#else
    LOG(INFO) << "[testBoundary] dxv(u1) error : " << __errDx << "\n";
    std::cout << "[testBoundary] dxv(u1) error : " << __errDx << "\n";
#endif
    double  __errDy = normL2( _range=markedfaces( mesh1, "Interface" ),
                              _expr=dyv( u1 )-dyv( u2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] dyv(u1) error : " << __errDy );
    BOOST_CHECK_SMALL( __errDy,1e-12 );
#else
    LOG(INFO) << "[testBoundary] dyv(u1) error : " << __errDy << "\n";
    std::cout << "[testBoundary] dyv(u1) error : " << __errDy << "\n";
#endif
    if ( OrderGeo==1 )
      {
        double __errHess = normL2( _range=markedfaces( mesh1, "Interface" ),
                            _expr= hessv( u1 )-hessv( u2, useConformalIntegration ) );
#if USE_BOOST_TEST
        BOOST_MESSAGE( "[testBoundary] hessv(u1) error : " << __errHess );
        BOOST_CHECK_SMALL( __errHess,1e-9 );
#else
        std::cout << "[testBoundary] hessv(u1) error : " << __errHess << "\n";
#endif
      }

    double  __errIdVec = normL2( _range=markedfaces( mesh1, "Interface" ),
                                 _expr=idv( v1 )-idv( v2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] idv vectorial error : " << __errIdVec );
    BOOST_CHECK_SMALL( __errIdVec,1e-12 );
#else
    std::cout << "[testBoundary] idv vectorial error : " << __errIdVec <<"\n";
#endif

    double __errGradVec = normL2( _range=markedfaces( mesh1, "Interface" ),
                                  _expr= gradv( v1 )-gradv( v2 , useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] gradv vectorial error : " << __errGradVec );
    BOOST_CHECK_SMALL( __errGradVec,1e-12 );
#else
    std::cout << "[testBoundary] gradv vectorial error : " << __errGradVec <<"\n";
#endif
    double  __errDiv = normL2( _range=markedfaces( mesh1, "Interface" ),
                               _expr=divv( v1 )-divv( v2 , useConformalIntegration) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] divv(u1) error : " << __errDiv );
    BOOST_CHECK_SMALL( __errDiv,1e-12 );
#else
    LOG(INFO) << "[testBoundary] divv(u1) error : " << __errDiv << "\n";
    std::cout << "[testBoundary] divv(u1) error : " << __errDiv << "\n";
#endif
    double  __errDivx = normL2( _range=markedfaces( mesh1, "Interface" ),
                                _expr=dxv( v1 )+dyv( v1 )-dxv( v2, useConformalIntegration )-dyv( v2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] dxv+dyv(v1) error : " << __errDivx );
    BOOST_CHECK_SMALL( __errDivx,1e-12 );
#else
    LOG(INFO) << "[testBoundary] dxv+dyv(v1) error : " << __errDivx << "\n";
    std::cout << "[testBoundary] dxv+dyv(v1) error : " << __errDivx << "\n";
#endif

    double  __errCurl = normL2( _range=markedfaces( mesh1, "Interface" ),
                                _expr=curlv( v1 )-curlv( v2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] curlv(v1) error : " << __errCurl );
    BOOST_CHECK_SMALL( __errCurl,1e-12 );
#else
    LOG(INFO) << "[testBoundary] curlv(u1) error : " << __errCurl << "\n";
    std::cout << "[testBoundary] curlv(u1) error : " << __errCurl << "\n";
#endif
    double  __errCurlx = normL2( _range=markedfaces( mesh1, "Interface" ),
                                 _expr=curlxv( v1 )-curlxv( v2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] curlxv(v1) error : " << __errCurlx );
    BOOST_CHECK_SMALL( __errCurlx,1e-12 );
#else
    LOG(INFO) << "[testBoundary] curlxv(u1) error : " << __errCurlx << "\n";
    std::cout << "[testBoundary] curlxv(u1) error : " << __errCurlx << "\n";
#endif
    double  __errCurly = normL2( _range=markedfaces( mesh1, "Interface" ),
                                 _expr=curlyv( v1 )-curlyv( v2, useConformalIntegration ) );
#if USE_BOOST_TEST
    BOOST_MESSAGE( "[testBoundary] curlyv(v1) error : " << __errCurly );
    BOOST_CHECK_SMALL( __errCurly,1e-12 );
#else
    LOG(INFO) << "[testBoundary] curlyv(u1) error : " << __errCurly << "\n";
    std::cout << "[testBoundary] curlyv(u1) error : " << __errCurly << "\n";
#endif

}

/*_________________________________________________*
 * run_test_interp
 *_________________________________________________*/

template<uint32_type Dim,uint32_type OrderChamp,uint32_type OrderGeo>
void
run_test_interp()
{
    typedef Mesh<Simplex<Dim,OrderGeo,Dim> > mesh_type;
    typedef std::shared_ptr<  mesh_type > mesh_ptrtype;

    double meshSize1 = doption(_name="hsize1");
    double meshSize2 = doption(_name="hsize2");
    bool userelation = boption(_name="userelation");

    //Geometry
    GeoTool::Node x1( -1,-1 );
    GeoTool::Node x2( 0,1 );
    GeoTool::Rectangle R1( meshSize1,"Omega1",x1,x2 );
    R1.setMarker( _type="line",_name="Interface",_marker2=true );
    R1.setMarker( _type="line",_name="Boundary",_marker1=true,_marker3=true,_marker4=true );
    R1.setMarker( _type="surface",_name="Omega1",_markerAll=true );

    GeoTool::Node x3( 0,-1 );
    GeoTool::Node x4( 1,1 );
    GeoTool::Rectangle R2( meshSize2,"Omega2",x3,x4 );
    R2.setMarker( _type="line",_name="Interface",_marker4=true );
    R2.setMarker( _type="line",_name="Boundary",_marker1=true,_marker2=true,_marker3=true );
    R2.setMarker( _type="surface",_name="Omega2",_markerAll=true );

    GeoTool::Node x21( 0,0 );
    GeoTool::Node x22( 1,1 );
    GeoTool::Rectangle R( meshSize1,"Omega3",x21,x22 );
    R.setMarker( _type="line",_name="Boundary",_markerAll=true );
    R.setMarker( _type="surface",_name="Omega3",_markerAll=true );

    GeoTool::Node x_centre( 0.5,0.5 );
    GeoTool::Node x_bord( 0.7,0.5 );
    GeoTool::Circle C( meshSize2,"Omega4",x_centre,x_bord );
    C.setMarker( _type="line",_name="Interface",_markerAll=true );
    C.setMarker(_type="surface",_name="Omega4",_markerAll=true);
    /*
    GeoTool::Node x2_centre( 0.5,0.5 );
    GeoTool::Node x2_bord( 0.7,0.5 );
    GeoTool::Circle C2( meshSize2,"DISQUE",x2_centre,x2_bord );
    C2.setMarker( _type="line",_name="Interface",_marker1=true );
    C2.setMarker( _type="surface",_name="Omega4",_marker1=true );*/

    //Mesh
    auto mesh1 = R1.createMesh(_mesh=new mesh_type,
                               _name= "mesh1_interp" + mesh_type::shape_type::name() );
    auto mesh2 = R2.createMesh(_mesh=new mesh_type,
                               _name="mesh2_interp" + mesh_type::shape_type::name() );
#if 0
    auto mesh3 = /*( R-C )*/C.createMesh(_mesh=new mesh_type,
                                    _name="mesh3_interp" + mesh_type::shape_type::name() );
#if 1
    auto mesh4 = /*( R-C )*/C.createMesh(_mesh=new mesh_type,
                                         _name="mesh4_interp" + mesh_type::shape_type::name() );
#else

    size_type ctx = userelation? EXTRACTION_KEEP_MESH_RELATION:0;
    auto mesh4 = createSubmesh(_mesh=mesh3,_range=elements(mesh3), _context=ctx);
#endif
#else
    auto meshBase = ((R-C)+C).createMesh(_mesh=new mesh_type,
                                         _name="meshBase_interp" + mesh_type::shape_type::name() );
    size_type ctx = userelation? EXTRACTION_KEEP_MESH_RELATION:0;
    auto mesh3 = createSubmesh(_mesh=meshBase,_range=markedelements(meshBase,"Omega3"), _context=ctx);
    auto mesh4 = createSubmesh(_mesh=meshBase,_range=markedelements(meshBase,"Omega4"), _context=ctx);
#endif
    std::list<boost::tuple<mesh_ptrtype,mesh_ptrtype,bool> > __listMesh;
    __listMesh.push_back( boost::make_tuple( mesh1,mesh2, meshSize1==meshSize2 ) );
    __listMesh.push_back( boost::make_tuple( mesh3,mesh4, true ) );

    auto itMesh = __listMesh.begin();
    auto const itMesh_end = __listMesh.end();
    for ( int i=1 ; itMesh!=itMesh_end ; ++itMesh, ++i )
    {
        LOG(INFO) << "[run] start test interpolation mesh pair" << i << "\n";
        test_interp_boundary<Dim,OrderChamp,OrderGeo>( *itMesh/*, boost::mpl::int_<ScalarTest>()*/ );
        LOG(INFO) << "[run] finish test interpolation mesh pair" << i << "\n";
    }

}

/*_________________________________________________*
 * runTestExport
 *_________________________________________________*/

}//end namespace test_interp_twomesh

/*_________________________________________________*
 *_________________________________________________*
 *_________________________________________________*
 * MAIN
 *_________________________________________________*
 *_________________________________________________*
 *_________________________________________________*/

#if USE_BOOST_TEST

FEELPP_ENVIRONMENT_WITH_OPTIONS( test_interp_twomesh::makeAbout(), test_interp_twomesh::makeOptions() )

BOOST_AUTO_TEST_SUITE( interp_twomesh_testsuite )

#if 0
BOOST_AUTO_TEST_CASE( interp_twomesh_geomap )
{
    using namespace test_interp_twomesh;

    BOOST_MESSAGE(   "\n[main] ----------TEST_GEOMAP_START----------\n" );
    BOOST_MESSAGE(   "[main] ----------------<2,6,1>---------------\n" );
    run_test_geomap<2,6,1>();
    BOOST_MESSAGE(   "[main] ----------------<2,6,2>---------------\n" );
    run_test_geomap<2,6,2>();
    BOOST_MESSAGE(   "[main] ----------------<2,6,3>---------------\n" );
    run_test_geomap<2,6,3>();
    BOOST_MESSAGE(   "[main] ----------------<2,6,4>---------------\n" );
    run_test_geomap<2,6,4>();
    BOOST_MESSAGE(   "[main] ----------TEST_GEOMAP_FINISH----------\n\n" );
}
#endif

// problem with this test case
BOOST_AUTO_TEST_CASE( interp_twomesh_interp_241 )
{
    using namespace test_interp_twomesh;
    BOOST_MESSAGE(   "[main] ----------------<2,4,1>---------------\n" );
    run_test_interp<2,4,1>();
}

BOOST_AUTO_TEST_CASE( interp_twomesh_interp_242 )
{
    using namespace test_interp_twomesh;
    BOOST_MESSAGE(   "[main] ----------------<2,4,2>---------------\n" );
    run_test_interp<2,4,2>();

}

BOOST_AUTO_TEST_SUITE_END()

#else
int main(int argc, char**argv )
{
    using namespace Feel;
	Environment env( _argc=argc, _argv=argv,
                     _desc=test_interp_twomesh::makeOptions(),
                     _about=test_interp_twomesh::makeAbout() );

    Environment::changeRepository( _directory=boost::format( "testsuite/feeldiscr/interp/%1%/" )
                                   % env.about().appName()
                                   );

    std::cout << "[main] ----------TEST_INTERP_START-----------\n";
    std::cout << "[main] ----------------<2,4,1>---------------\n";
    test_interp_twomesh::run_test_interp<2,4,1>();
    std::cout << "[main] ----------------<2,4,2>---------------\n";
    test_interp_twomesh::run_test_interp<2,4,2>();
    std::cout << "[main] ----------TEST_INTERP_FINISH----------\n";

    return 0;
}
#endif


void CSplinePatch::Init( int w, int h, int extraChannels )
{
	assert( extraChannels < MAX_CHANNELS );
	m_ChannelCount = extraChannels;
	m_Width = w;
	m_Height = h;
	m_LinearFactor = 1.0f;
}

// Purpose: A little helper class that computes a spline patch
#include "cbase.h"
#include "SplinePatch.h"

#include "mathlib/VMatrix.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// Catmull rom blend weights

static VMatrix s_CatmullRom( -0.5,  1.5, -1.5,  0.5,
							    1, -2.5,    2, -0.5,
							 -0.5,    0,  0.5,    0,
							    0,    1,    0,    0 );

// The last argument represents the number of float channels in addition to position

CSplinePatch::CSplinePatch( ) : m_ChannelCount(0),
	m_Width(0), m_Height(0), m_ppPositions(0), m_LinearFactor(1.0f)
{
}

CSplinePatch::~CSplinePatch()
{
}

// Initialize the spline patch

void CSplinePatch::SetLinearBlend( float factor )
{
	m_LinearFactor = factor;
}

// Hooks the patch up to externally controlled data...

void CSplinePatch::SetControlPositions( Vector const** pPositions )
{
	m_ppPositions = pPositions;
}

void CSplinePatch::SetChannelData( int channel, float* pChannel )
{
	m_pChannel[channel] = pChannel;
}

static inline void ComputeIndex( int i, int maxval, int* idx )
{
	if (i == 0)
	{
		idx[0] = 0; idx[1] = 0; idx[2] = 1;
		idx[3] = (maxval > 2) ? 2 : 1;
	}
	else
	{
		idx[0] = i-1; idx[1] = i;
		if (i >= maxval - 1)
		{
			idx[2] = i; idx[3] = i;
		}
		else
		{
			idx[2] = i+1;
			if (i >= maxval - 2)
				idx[3] = i+1;
			else
				idx[3]=  i+2;
		}
	}
}

// Computes indices of the samples to read for this interpolation

void CSplinePatch::ComputeIndices( )
{
	int s[4];
	int t[4];

	ComputeIndex( m_is, m_Width, s );
	ComputeIndex( m_it, m_Height, t );

	int base = t[0] * m_Width;
	m_SampleIndices[0][0] = base + s[0];
	m_SampleIndices[1][0] = base + s[1];
	m_SampleIndices[2][0] = base + s[2];
	m_SampleIndices[3][0] = base + s[3];

	base = t[1] * m_Width;
	m_SampleIndices[0][1] = base + s[0];
	m_SampleIndices[1][1] = base + s[1];
	m_SampleIndices[2][1] = base + s[2];
	m_SampleIndices[3][1] = base + s[3];

	base = t[2] * m_Width;
	m_SampleIndices[0][2] = base + s[0];
	m_SampleIndices[1][2] = base + s[1];
	m_SampleIndices[2][2] = base + s[2];
	m_SampleIndices[3][2] = base + s[3];

	base = t[3] * m_Width;
	m_SampleIndices[0][3] = base + s[0];
	m_SampleIndices[1][3] = base + s[1];
	m_SampleIndices[2][3] = base + s[2];
	m_SampleIndices[3][3] = base + s[3];
}

// Call this before querying the patch for data at (s,t)

void CSplinePatch::SetupPatchQuery( float s, float t )
{
	m_is = (int)s;
	m_it = (int)t;

	if( m_is >= m_Width )
	{
		m_is = m_Width - 1;
		m_fs = 1.0f;
	}
	else
	{
		m_fs = s - m_is;
	}

	if( m_it >= m_Height )
	{
		m_it = m_Height - 1;
		m_ft = 1.0f;
	}
	else
	{
		m_ft = t - m_it;
	}

	ComputeIndices( );

	// The patch equation is:
	// px = S * M * Gx * M^T * T^T
	// py = S * M * Gy * M^T * T^T
	// pz = S * M * Gz * M^T * T^T
	// M is the patch type matrix, in my case I'm using a catmull-rom
	// G is the array of control points. rows have constant t

	// We're gonna cache off S * M and M^T * T^T...
	Vector4D svec, tvec;
 	float fs2 = m_fs * m_fs;
	svec[0] = fs2 * m_fs; svec[1] = fs2; svec[2] = m_fs; svec[3] = 1.0f;
	float ft2 = m_ft * m_ft;
	tvec[0] = ft2 * m_ft; tvec[1] = ft2; tvec[2] = m_ft; tvec[3] = 1.0f;

	// This sets up the catmull rom matrix based on the blend factor!!
	// we can go from linear to curvy!
	s_CatmullRom.Init(  -0.5 * m_LinearFactor,  1.5 * m_LinearFactor, -1.5 * m_LinearFactor,  0.5 * m_LinearFactor,
						       m_LinearFactor, -2.5 * m_LinearFactor,    2 * m_LinearFactor, -0.5 * m_LinearFactor,
						-0.5 * m_LinearFactor,   -1 + m_LinearFactor,    1 - 0.5 * m_LinearFactor,    0,
											0,					   1,					  0,    0 );
	Vector4DMultiplyTranspose( s_CatmullRom, svec, m_SVec );
	Vector4DMultiplyTranspose( s_CatmullRom, tvec, m_TVec );
}

// Gets the point and normal at (i,j) specified above

void CSplinePatch::GetPointAndNormal( Vector& position, Vector& normal ) const
{
	// The patch equation is:
	// px = S * M * Gx * M^T * T^T
	// py = S * M * Gy * M^T * T^T
	// pz = S * M * Gz * M^T * T^T
	// M is the patch type matrix, in my case I'm using a catmull-rom
	// G is the array of control points. rows have constant t

	VMatrix controlPointsX, controlPointsY, controlPointsZ;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			int idx = m_SampleIndices[i][j];
			controlPointsX[i][j] = m_ppPositions[ idx ]->x;
			controlPointsY[i][j] = m_ppPositions[ idx ]->y;
			controlPointsZ[i][j] = m_ppPositions[ idx ]->z;
		}
	}

	Vector4D tmp;

	Vector4DMultiply( controlPointsX, m_TVec, tmp );
	position[0] = DotProduct4D( tmp, m_SVec );
	Vector4DMultiply( controlPointsY, m_TVec, tmp );
	position[1] = DotProduct4D( tmp, m_SVec );
	Vector4DMultiply( controlPointsZ, m_TVec, tmp );
	position[2] = DotProduct4D( tmp, m_SVec );

	// Normal computation

	float fs2 = m_fs * m_fs;
	float ft2 = m_ft * m_ft;
	Vector4D dsvec( 3.0f * fs2, 2.0f * m_fs, 1.0f, 0.0f );
	Vector4D dtvec( 3.0f * ft2, 2.0f * m_ft, 1.0f, 0.0f );

	Vector4DMultiplyTranspose( s_CatmullRom, dsvec, dsvec );
	Vector4DMultiplyTranspose( s_CatmullRom, dtvec, dtvec );

	Vector ds, dt;

	Vector4DMultiply( controlPointsX, m_TVec, tmp );
	ds[0] = DotProduct4D( tmp, dsvec );
	Vector4DMultiply( controlPointsY, m_TVec, tmp );
	ds[1] = DotProduct4D( tmp, dsvec );
	Vector4DMultiply( controlPointsZ, m_TVec, tmp );
	ds[2] = DotProduct4D( tmp, dsvec );

	Vector4DMultiply( controlPointsX, dtvec, tmp );
	dt[0] = DotProduct4D( tmp, m_SVec );
	Vector4DMultiply( controlPointsY, dtvec, tmp );
	dt[1] = DotProduct4D( tmp, m_SVec );
	Vector4DMultiply( controlPointsZ, dtvec, tmp );
	dt[2] = DotProduct4D( tmp, m_SVec );

	CrossProduct( ds, dt, normal );
	VectorNormalize( normal );
}

// Gets at other channels

float CSplinePatch::GetChannel( int channel ) const
{
	// The patch equation is:
	// px = S * M * Gx * M^T * T^T
	// py = S * M * Gy * M^T * T^T
	// pz = S * M * Gz * M^T * T^T
	// M is the patch type matrix, in my case I'm using a catmull-rom
	// G is the array of control points. rows have constant t

	assert( m_pChannel[channel] );

	VMatrix controlPoints;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			controlPoints[i][j] = m_pChannel[channel][ m_SampleIndices[i][j] ];
		}
	}

	Vector4D tmp;
	Vector4DMultiply( controlPoints, m_TVec, tmp );
	return DotProduct4D( tmp, m_SVec );
}


inline static double PointDist( double x1, double y1, double x2, double y2 )
{
    return sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) );
}

inline static double PointDist( double x1, double y1, double z1, double x2,
    double y2, double z2 )
{
    return sqrt( (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) +
        (z2 - z1) * (z2 - z1) );
}

/************************************************************************/
/*                          TranslateLEADER()                           */
/************************************************************************/

OGRDXFFeature *OGRDXFLayer::TranslateLEADER()

{
    char szLineBuf[257];
    int nCode;
    OGRDXFFeature *poFeature = new OGRDXFFeature( poFeatureDefn );

    OGRLineString *poLine = new OGRLineString();
    bool bHaveX = false;
    bool bHaveY = false;
    bool bHaveZ = false;
    double dfCurrentX = 0.0;
    double dfCurrentY = 0.0;
    double dfCurrentZ = 0.0;
    int nNumVertices = 0;

    bool bHorizontalDirectionFlip = true;
    double dfHorizontalDirectionX = 1.0;
    double dfHorizontalDirectionY = 0.0;
    double dfHorizontalDirectionZ = 0.0;
    bool bHasTextAnnotation = false;
    double dfTextAnnotationWidth = 0.0;
    bool bIsSpline = false;

    // spec is silent as to default, but AutoCAD assumes true
    bool bWantArrowhead = true;

    bool bReadyForDimstyleOverride = false;

    std::map<CPLString,CPLString> oDimStyleProperties;
    poDS->PopulateDefaultDimStyleProperties(oDimStyleProperties);

    while( (nCode = poDS->ReadValue(szLineBuf,sizeof(szLineBuf))) > 0 )
    {
        switch( nCode )
        {
          case 3:
            // 3 is the dimension style name. We don't need to store it,
            // let's just fetch the dimension style properties
            poDS->LookupDimStyle(szLineBuf, oDimStyleProperties);
            break;

          case 10:
            // add the previous point onto the linestring
            if( bHaveX && bHaveY && bHaveZ ) {
                poLine->setPoint( nNumVertices++,
                    dfCurrentX, dfCurrentY, dfCurrentZ );
                bHaveY = bHaveZ = false;
            }
            dfCurrentX = CPLAtof(szLineBuf);
            bHaveX = true;
            break;

          case 20:
            // add the previous point onto the linestring
            if( bHaveX && bHaveY && bHaveZ ) {
                poLine->setPoint( nNumVertices++,
                    dfCurrentX, dfCurrentY, dfCurrentZ );
                bHaveX = bHaveZ = false;
            }
            dfCurrentY = CPLAtof(szLineBuf);
            bHaveY = true;
            break;

          case 30:
            // add the previous point onto the linestring
            if( bHaveX && bHaveY && bHaveZ ) {
                poLine->setPoint( nNumVertices++,
                    dfCurrentX, dfCurrentY, dfCurrentZ );
                bHaveX = bHaveY = false;
            }
            dfCurrentZ = CPLAtof(szLineBuf);
            bHaveZ = true;
            break;

          case 41:
            dfTextAnnotationWidth = CPLAtof(szLineBuf);
            break;

          case 71:
            bWantArrowhead = atoi(szLineBuf) != 0;
            break;

          case 72:
            bIsSpline = atoi(szLineBuf) != 0;
            break;

          case 73:
            bHasTextAnnotation = atoi(szLineBuf) == 0;
            break;

          case 74:
            // DXF spec seems to have this backwards. A value of 0 actually
            // indicates no flipping occurs, and 1 (flip) is the default
            bHorizontalDirectionFlip = atoi(szLineBuf) != 0;
            break;

          case 211:
            dfHorizontalDirectionX = CPLAtof(szLineBuf);
            break;

          case 221:
            dfHorizontalDirectionY = CPLAtof(szLineBuf);
            break;

          case 231:
            dfHorizontalDirectionZ = CPLAtof(szLineBuf);
            break;

          case 1001:
            bReadyForDimstyleOverride = EQUAL(szLineBuf, "ACAD");
            break;

          case 1070:
            if( bReadyForDimstyleOverride )
            {
                // Store DIMSTYLE override values in the dimension
                // style property map. The nInnerCode values match the
                // group codes used in the DIMSTYLE table.
                const int nInnerCode = atoi(szLineBuf);
                const char* pszProperty = ACGetDimStylePropertyName(nInnerCode);
                if( pszProperty )
                {
                    nCode = poDS->ReadValue(szLineBuf,sizeof(szLineBuf));
                    if( nCode == 1005 || nCode == 1040 || nCode == 1070 )
                        oDimStyleProperties[pszProperty] = szLineBuf;
                }
            }
            break;

          default:
            TranslateGenericProperty( poFeature, nCode, szLineBuf );
            break;
        }
    }

    if( nCode == 0 )
        poDS->UnreadValue();

    if( bHaveX && bHaveY && bHaveZ )
        poLine->setPoint( nNumVertices++, dfCurrentX, dfCurrentY, dfCurrentZ );

    // Unpack the dimension style
    bool bWantExtension = atoi(oDimStyleProperties["DIMTAD"]) > 0;
    double dfTextOffset = CPLAtof(oDimStyleProperties["DIMGAP"]);
    double dfScale = CPLAtof(oDimStyleProperties["DIMSCALE"]);
    double dfArrowheadSize = CPLAtof(oDimStyleProperties["DIMASZ"]);
    int nLeaderColor = atoi(oDimStyleProperties["DIMCLRD"]);
    // DIMLDRBLK is the entity handle of the BLOCK_RECORD table entry that
    // corresponds to the arrowhead block.
    CPLString osArrowheadBlockHandle = oDimStyleProperties["DIMLDRBLK"];

    // Zero scale has a special meaning which we aren't interested in,
    // so we can change it to 1.0
    if( dfScale == 0.0 )
        dfScale = 1.0;

    // Use the color from the dimension style if it is not ByBlock
    if( nLeaderColor > 0 )
        poFeature->oStyleProperties["Color"] = oDimStyleProperties["DIMCLRD"];

/* -------------------------------------------------------------------- */
/*      Add an arrowhead to the start of the leader line.               */
/* -------------------------------------------------------------------- */

    if( bWantArrowhead && nNumVertices >= 2 )
    {
        InsertArrowhead( poFeature, osArrowheadBlockHandle, poLine,
            dfArrowheadSize * dfScale );
    }

    if( bHorizontalDirectionFlip )
    {
        dfHorizontalDirectionX *= -1;
        dfHorizontalDirectionX *= -1;
        dfHorizontalDirectionX *= -1;
    }

/* -------------------------------------------------------------------- */
/*      For a spline leader, determine the end tangent direction        */
/*      and interpolate the spline vertices.                            */
/* -------------------------------------------------------------------- */

    if( bIsSpline )
    {
        DXFTriple oEndTangent;
        if( bHasTextAnnotation )
        {
            oEndTangent = DXFTriple( dfHorizontalDirectionX,
                dfHorizontalDirectionY, dfHorizontalDirectionZ );
        }
        InterpolateSpline( poLine, oEndTangent );
    }

/* -------------------------------------------------------------------- */
/*      Add an extension to the end of the leader line. This is not     */
/*      properly documented in the DXF spec, but it is needed to        */
/*      replicate the way AutoCAD displays leader objects.              */
/*                                                                      */
/*      When $DIMTAD (77) is nonzero, the leader line is extended       */
/*      under the text annotation. This extension is not stored as an   */
/*      additional vertex, so we need to create it ourselves.           */
/* -------------------------------------------------------------------- */

    if( bWantExtension && bHasTextAnnotation && poLine->getNumPoints() >= 2 )
    {
        OGRPoint oLastVertex;
        poLine->getPoint( poLine->getNumPoints() - 1, &oLastVertex );

        double dfExtensionX = oLastVertex.getX();
        double dfExtensionY = oLastVertex.getY();
        double dfExtensionZ = oLastVertex.getZ();

        double dfExtensionLength = ( dfTextOffset * dfScale ) +
            dfTextAnnotationWidth;
        dfExtensionX += dfHorizontalDirectionX * dfExtensionLength;
        dfExtensionY += dfHorizontalDirectionY * dfExtensionLength;
        dfExtensionZ += dfHorizontalDirectionZ * dfExtensionLength;

        poLine->setPoint( poLine->getNumPoints(), dfExtensionX, dfExtensionY,
            dfExtensionZ );
    }

    poFeature->SetGeometryDirectly( poLine );

    PrepareLineStyle( poFeature );

    return poFeature;
}

/************************************************************************/
/*       DXFMLEADERVertex, DXFMLEADERLeaderLine, DXFMLEADERLeader       */
/************************************************************************/

struct DXFMLEADERVertex {
    DXFTriple                oCoords;
    std::vector<std::pair<DXFTriple, DXFTriple>> aoBreaks;

    DXFMLEADERVertex( double dfX, double dfY )
        : oCoords( DXFTriple( dfX, dfY, 0.0 ) ) {}
};

struct DXFMLEADERLeader {
    double                   dfLandingX;
    double                   dfLandingY;
    double                   dfDoglegVectorX;
    double                   dfDoglegVectorY;
    double                   dfDoglegLength;
    std::vector<std::pair<DXFTriple, DXFTriple>> aoDoglegBreaks;
    std::vector<std::vector<DXFMLEADERVertex>> aaoLeaderLines;
};

/************************************************************************/
/*                         TranslateMLEADER()                           */
/************************************************************************/

OGRDXFFeature *OGRDXFLayer::TranslateMLEADER()

{
    char szLineBuf[257];
    int nCode = 0;

    // This is a dummy feature object used to store style properties
    // and the like. We end up deleting it without returning it
    OGRDXFFeature *poOverallFeature = new OGRDXFFeature( poFeatureDefn );

    DXFMLEADERLeader oLeader;
    std::vector<DXFMLEADERLeader> aoLeaders;

    std::vector<DXFMLEADERVertex> oLeaderLine;
    double dfCurrentX = 0.0;
    double dfCurrentY = 0.0;
    double dfCurrentX2 = 0.0;
    double dfCurrentY2 = 0.0;
    size_t nCurrentVertex = 0;

    double dfScale = 1.0;
    bool bHasDogleg = true;
    CPLString osLeaderColor = "0";

    CPLString osText;
    CPLString osTextStyleHandle;
    double dfTextX = 0.0;
    double dfTextY = 0.0;
    int nTextAlignment = 1; // 1 = left, 2 = center, 3 = right
    double dfTextAngle = 0.0;
    double dfTextHeight = 4.0;

    CPLString osBlockHandle;
    OGRDXFInsertTransformer oBlockTransformer;
    CPLString osBlockAttributeHandle;
    // Map of ATTDEF handles to attribute text
    std::map<CPLString, CPLString> oBlockAttributes;

    CPLString osArrowheadBlockHandle;
    double dfArrowheadSize = 4.0;

    // The different leader line types
    const int MLT_NONE = 0;
    const int MLT_STRAIGHT = 1;
    const int MLT_SPLINE = 2;
    int nLeaderLineType = MLT_STRAIGHT;

    // Group codes mean different things in different sections of the
    // MLEADER entity. We need to keep track of the section we are in.
    const int MLS_COMMON = 0;
    const int MLS_CONTEXT_DATA = 1;
    const int MLS_LEADER = 2;
    const int MLS_LEADER_LINE = 3;
    int nSection = MLS_COMMON;

    // The way the 30x group codes work is missing from the DXF docs.
    // We assume that the sections are always nested as follows:

    // ... [this part is identified as MLS_COMMON]

    while( (nCode = poDS->ReadValue(szLineBuf,sizeof(szLineBuf))) > 0 )
    {
        switch( nSection )
        {
          case MLS_COMMON:
            switch( nCode )
            {
              case 300:
                nSection = MLS_CONTEXT_DATA;
                break;

              case 342:
                // 342 is the entity handle of the BLOCK_RECORD table entry that
                // corresponds to the arrowhead block.
                osArrowheadBlockHandle = szLineBuf;
                break;

              case 42:
                // TODO figure out difference between 42 and 140 for arrowheadsize
                dfArrowheadSize = CPLAtof( szLineBuf );
                break;

              case 330:
                osBlockAttributeHandle = szLineBuf;
                break;

              case 302:
                if( osBlockAttributeHandle != "" )
                {
                    oBlockAttributes[osBlockAttributeHandle] =
                        TextUnescape( szLineBuf, true );
                    osBlockAttributeHandle = "";
                }
                break;

              case 91:
                osLeaderColor = szLineBuf;
                break;

              case 170:
                nLeaderLineType = atoi(szLineBuf);
                break;

              case 291:
                bHasDogleg = atoi(szLineBuf) != 0;
                break;

              default:
                TranslateGenericProperty( poOverallFeature, nCode, szLineBuf );
                break;
            }
            break;

          case MLS_CONTEXT_DATA:
            switch( nCode )
            {
              case 301:
                nSection = MLS_COMMON;
                break;

              case 302:
                nSection = MLS_LEADER;
                break;

              case 304:
                osText = TextUnescape(szLineBuf, true);
                break;

              case 40:
                dfScale = CPLAtof( szLineBuf );
                break;

              case 340:
                // 340 is the entity handle of the STYLE table entry that
                // corresponds to the text style.
                osTextStyleHandle = szLineBuf;
                break;

              case 12:
                dfTextX = CPLAtof( szLineBuf );
                break;

              case 22:
                dfTextY = CPLAtof( szLineBuf );
                break;

              case 41:
                dfTextHeight = CPLAtof( szLineBuf );
                break;

              case 42:
                dfTextAngle = CPLAtof( szLineBuf ) * 180 / M_PI;
                break;

              case 171:
                nTextAlignment = atoi( szLineBuf );
                break;

              case 341:
                // 341 is the entity handle of the BLOCK_RECORD table entry that
                // corresponds to the block content of this MLEADER.
                osBlockHandle = szLineBuf;
                break;

              case 15:
                oBlockTransformer.dfXOffset = CPLAtof( szLineBuf );
                break;

              case 25:
                oBlockTransformer.dfYOffset = CPLAtof( szLineBuf );
                break;

              case 16:
                oBlockTransformer.dfXScale = CPLAtof( szLineBuf );
                break;

              case 26:
                oBlockTransformer.dfYScale = CPLAtof( szLineBuf );
                break;

              case 46:
                oBlockTransformer.dfAngle = CPLAtof( szLineBuf );
                break;
            }
            break;

          case MLS_LEADER:
            switch( nCode )
            {
              case 303:
                nSection = MLS_CONTEXT_DATA;
                aoLeaders.push_back( oLeader );
                oLeader = DXFMLEADERLeader();
                break;

              case 304:
                nSection = MLS_LEADER_LINE;
                break;

              case 10:
                oLeader.dfLandingX = CPLAtof(szLineBuf);
                break;

              case 20:
                oLeader.dfLandingY = CPLAtof(szLineBuf);
                break;

              case 11:
                oLeader.dfDoglegVectorX = CPLAtof(szLineBuf);
                break;

              case 21:
                oLeader.dfDoglegVectorY = CPLAtof(szLineBuf);
                break;

              case 12:
                dfCurrentX = CPLAtof(szLineBuf);
                break;

              case 22:
                dfCurrentY = CPLAtof(szLineBuf);
                break;

              case 13:
                dfCurrentX2 = CPLAtof(szLineBuf);
                break;

              case 23:
                dfCurrentY2 = CPLAtof(szLineBuf);
                oLeader.aoDoglegBreaks.push_back( std::make_pair(
                    DXFTriple( dfCurrentX, dfCurrentY, 0.0 ),
                    DXFTriple( dfCurrentX2, dfCurrentY2, 0.0 ) ) );
                break;

              case 40:
                oLeader.dfDoglegLength = CPLAtof(szLineBuf);
                break;
            }
            break;

          case MLS_LEADER_LINE:
            switch( nCode )
            {
              case 305:
                nSection = MLS_LEADER;
                oLeader.aaoLeaderLines.push_back( oLeaderLine );
                oLeaderLine.clear();
                break;

              case 10:
                dfCurrentX = CPLAtof(szLineBuf);
                break;

              case 20:
                dfCurrentY = CPLAtof(szLineBuf);
                oLeaderLine.push_back(
                    DXFMLEADERVertex( dfCurrentX, dfCurrentY ) );
                break;

              case 90:
                nCurrentVertex = atoi(szLineBuf);
                if( nCurrentVertex >= oLeaderLine.size() )
                {
                    CPLError( CE_Warning, CPLE_AppDefined,
                        "Wrong group code 90 in LEADER_LINE: %s", szLineBuf );
                    DXF_LAYER_READER_ERROR();
                    delete poOverallFeature;
                    return nullptr;
                }
                break;

              case 11:
                dfCurrentX = CPLAtof(szLineBuf);
                break;

              case 21:
                dfCurrentY = CPLAtof(szLineBuf);
                break;

              case 12:
                dfCurrentX2 = CPLAtof(szLineBuf);
                break;

              case 22:
                if( nCurrentVertex >= oLeaderLine.size() )
                {
                    CPLError( CE_Warning, CPLE_AppDefined,
                        "Misplaced group code 22 in LEADER_LINE" );
                    DXF_LAYER_READER_ERROR();
                    delete poOverallFeature;
                    return nullptr;
                }
                dfCurrentY2 = CPLAtof(szLineBuf);
                oLeaderLine[nCurrentVertex].aoBreaks.push_back( std::make_pair(
                    DXFTriple( dfCurrentX, dfCurrentY, 0.0 ),
                    DXFTriple( dfCurrentX2, dfCurrentY2, 0.0 ) ) );
                break;
            }
            break;
        }
    }

    if( nCode < 0 )
    {
        DXF_LAYER_READER_ERROR();
        delete poOverallFeature;
        return nullptr;
    }
    if( nCode == 0 )
        poDS->UnreadValue();

    // Convert the block handle to a block name. If there is no block,
    // osBlockName will remain empty.
    CPLString osBlockName;

    if( osBlockHandle != "" )
        osBlockName = poDS->GetBlockNameByRecordHandle( osBlockHandle );

/* -------------------------------------------------------------------- */
/*      Add the landing and arrowhead onto each leader line, and add    */
/*      the dogleg, if present, onto the leader.                        */
/* -------------------------------------------------------------------- */
    OGRDXFFeature* poLeaderFeature = poOverallFeature->CloneDXFFeature();
    poLeaderFeature->oStyleProperties["Color"] = osLeaderColor;

    OGRMultiLineString *poMLS = new OGRMultiLineString();

    // Arrowheads should be the same color as the leader line. If the leader
    // line is ByBlock or ByLayer then the arrowhead should be "owned" by the
    // overall feature for styling purposes.
    OGRDXFFeature* poArrowheadOwningFeature = poLeaderFeature;
    if( ( atoi(osLeaderColor) & 0xC2000000 ) == 0xC0000000 )
        poArrowheadOwningFeature = poOverallFeature;

    for( std::vector<DXFMLEADERLeader>::iterator oIt = aoLeaders.begin();
         nLeaderLineType != MLT_NONE && oIt != aoLeaders.end();
         ++oIt )
    {
        const bool bLeaderHasDogleg = bHasDogleg &&
            nLeaderLineType != MLT_SPLINE &&
            oIt->dfDoglegLength != 0.0 &&
            ( oIt->dfDoglegVectorX != 0.0 || oIt->dfDoglegVectorY != 0.0 );

        // We assume that the dogleg vector in the DXF is a unit vector.
        // Safe assumption? Who knows. The documentation is so bad.
        const double dfDoglegX = oIt->dfLandingX +
            oIt->dfDoglegVectorX * oIt->dfDoglegLength;
        const double dfDoglegY = oIt->dfLandingY +
            oIt->dfDoglegVectorY * oIt->dfDoglegLength;

        // When the dogleg is turned off or we are in spline mode, it seems
        // that the dogleg and landing data are still present in the DXF file,
        // but they are not supposed to be drawn.
        if( !bHasDogleg || nLeaderLineType == MLT_SPLINE )
        {
            oIt->dfLandingX = dfDoglegX;
            oIt->dfLandingY = dfDoglegY;
        }

        // Iterate through each leader line
        for( const auto& aoLineVertices : oIt->aaoLeaderLines )
        {
            if( aoLineVertices.empty() )
                continue;

            OGRLineString* poLeaderLine = new OGRLineString();

            // Get the first line segment for arrowhead purposes
            poLeaderLine->addPoint(
                aoLineVertices[0].oCoords.dfX,
                aoLineVertices[0].oCoords.dfY );

            if( aoLineVertices.size() > 1 )
            {
                poLeaderLine->addPoint(
                    aoLineVertices[1].oCoords.dfX,
                    aoLineVertices[1].oCoords.dfY );
            }
            else
            {
                poLeaderLine->addPoint( oIt->dfLandingX, oIt->dfLandingY );
            }

            // Add an arrowhead if required
            InsertArrowhead( poArrowheadOwningFeature,
                osArrowheadBlockHandle, poLeaderLine,
                dfArrowheadSize * dfScale );

            poLeaderLine->setNumPoints( 1 );

            // Go through the vertices of the leader line, adding them,
            // as well as break start and end points, to the linestring.
            for( size_t iVertex = 0; iVertex < aoLineVertices.size();
                 iVertex++ )
            {
                if( iVertex > 0 )
                {
                    poLeaderLine->addPoint(
                        aoLineVertices[iVertex].oCoords.dfX,
                        aoLineVertices[iVertex].oCoords.dfY );
                }

                // Breaks are ignored for spline leaders
                if( nLeaderLineType != MLT_SPLINE )
                {
                    for( const auto& oBreak :
                         aoLineVertices[iVertex].aoBreaks )
                    {
                        poLeaderLine->addPoint( oBreak.first.dfX,
                            oBreak.first.dfY );

                        poMLS->addGeometryDirectly( poLeaderLine );
                        poLeaderLine = new OGRLineString();

                        poLeaderLine->addPoint( oBreak.second.dfX,
                            oBreak.second.dfY );
                    }
                }
            }

            // Add the final vertex (the landing) to the end of the line
            poLeaderLine->addPoint( oIt->dfLandingX, oIt->dfLandingY );

            // Make the spline geometry for spline leaders
            if( nLeaderLineType == MLT_SPLINE )
            {
                DXFTriple oEndTangent;
                if( osBlockName.empty() )
                {
                    oEndTangent = DXFTriple( oIt->dfDoglegVectorX,
                        oIt->dfDoglegVectorY, 0 );
                }
                InterpolateSpline( poLeaderLine, oEndTangent );
            }

            poMLS->addGeometryDirectly( poLeaderLine );
        }

        // Add the dogleg as a separate line in the MLS
        if( bLeaderHasDogleg )
        {
            OGRLineString *poDoglegLine = new OGRLineString();
            poDoglegLine->addPoint( oIt->dfLandingX, oIt->dfLandingY );

            // Interrupt the dogleg line at breaks
            for( const auto& oBreak : oIt->aoDoglegBreaks )
            {
                poDoglegLine->addPoint( oBreak.first.dfX,
                    oBreak.first.dfY );

                poMLS->addGeometryDirectly( poDoglegLine );
                poDoglegLine = new OGRLineString();

                poDoglegLine->addPoint( oBreak.second.dfX,
                    oBreak.second.dfY );
            }

            poDoglegLine->addPoint( dfDoglegX, dfDoglegY );
            poMLS->addGeometryDirectly( poDoglegLine );
        }
    }

    poLeaderFeature->SetGeometryDirectly( poMLS );

    PrepareLineStyle( poLeaderFeature, poOverallFeature );

/* -------------------------------------------------------------------- */
/*      If we have block content, insert that block.                    */
/* -------------------------------------------------------------------- */

    if( osBlockName != "" )
    {
        oBlockTransformer.dfXScale *= dfScale;
        oBlockTransformer.dfYScale *= dfScale;

        DXFBlockDefinition *poBlock = poDS->LookupBlock( osBlockName );

        std::map<OGRDXFFeature *, CPLString> oBlockAttributeValues;

        // If we have block attributes and will need to output them,
        // go through all the features on this block, looking for
        // ATTDEFs whose handle is in our list of attribute handles
        if( poBlock && !oBlockAttributes.empty() &&
            ( poDS->InlineBlocks() ||
            poOverallFeature->GetFieldIndex( "BlockAttributes" ) != -1 ) )
        {
            for( std::vector<OGRDXFFeature *>::iterator oIt =
                poBlock->apoFeatures.begin();
                oIt != poBlock->apoFeatures.end();
                ++oIt )
            {
                const char* pszHandle =
                    (*oIt)->GetFieldAsString( "EntityHandle" );

                if( pszHandle && oBlockAttributes.count( pszHandle ) > 0 )
                    oBlockAttributeValues[*oIt] = oBlockAttributes[pszHandle];
            }
        }

        OGRDXFFeature *poBlockFeature = poOverallFeature->CloneDXFFeature();

        // If not inlining the block, insert a reference and add attributes
        if( !poDS->InlineBlocks() )
        {
            poBlockFeature = InsertBlockReference( osBlockName,
                oBlockTransformer, poBlockFeature );

            if( !oBlockAttributes.empty() &&
                poOverallFeature->GetFieldIndex( "BlockAttributes" ) != -1 )
            {
                std::vector<char *> apszAttribs;

                for( std::map<OGRDXFFeature *, CPLString>::iterator oIt =
                    oBlockAttributeValues.begin();
                    oIt != oBlockAttributeValues.end();
                    ++oIt )
                {
                    // Store the attribute tag and the text value as
                    // a space-separated entry in the BlockAttributes field
                    CPLString osAttribString = oIt->first->osAttributeTag;
                    osAttribString += " ";
                    osAttribString += oIt->second;

                    apszAttribs.push_back(
                        new char[osAttribString.length() + 1] );
                    CPLStrlcpy( apszAttribs.back(), osAttribString.c_str(),
                        osAttribString.length() + 1 );
                }

                apszAttribs.push_back( nullptr );

                poBlockFeature->SetField( "BlockAttributes", &apszAttribs[0] );
            }

            apoPendingFeatures.push( poBlockFeature );
        }
        else
        {
            // Insert the block inline.
            OGRDXFFeatureQueue apoExtraFeatures;
            try
            {
                poBlockFeature = InsertBlockInline(
                    CPLGetErrorCounter(), osBlockName,
                    oBlockTransformer, poBlockFeature, apoExtraFeatures,
                    true, poDS->ShouldMergeBlockGeometries() );
            }
            catch( const std::invalid_argument& )
            {
                // Block doesn't exist
                delete poBlockFeature;
                poBlockFeature = nullptr;
            }

            // Add the block geometries to the pending feature stack.
            if( poBlockFeature )
            {
                apoPendingFeatures.push( poBlockFeature );
            }
            while( !apoExtraFeatures.empty() )
            {
                apoPendingFeatures.push( apoExtraFeatures.front() );
                apoExtraFeatures.pop();
            }

            // Also add any attributes to the pending feature stack.
            for( std::map<OGRDXFFeature *, CPLString>::iterator oIt =
                     oBlockAttributeValues.begin();
                 oIt != oBlockAttributeValues.end();
                 ++oIt )
            {
                OGRDXFFeature *poAttribFeature = oIt->first->CloneDXFFeature();

                poAttribFeature->SetField( "Text", oIt->second );

                // Replace text in the style string
                const char* poStyleString = poAttribFeature->GetStyleString();
                if( poStyleString && STARTS_WITH(poStyleString, "LABEL(") )
                {
                    CPLString osNewStyle = poStyleString;
                    const size_t nTextStartPos = osNewStyle.find( ",t:\"" );
                    if( nTextStartPos != std::string::npos )
                    {
                        size_t nTextEndPos = nTextStartPos + 4;
                        while( nTextEndPos < osNewStyle.size() &&
                            osNewStyle[nTextEndPos] != '\"' )
                        {
                            nTextEndPos++;
                            if( osNewStyle[nTextEndPos] == '\\' )
                                nTextEndPos++;
                        }

                        if( nTextEndPos < osNewStyle.size() )
                        {
                            osNewStyle.replace( nTextStartPos + 4,
                                nTextEndPos - ( nTextStartPos + 4 ),
                                oIt->second );
                            poAttribFeature->SetStyleString( osNewStyle );
                        }
                    }
                }

                // The following bits are copied from
                if( poAttribFeature->GetGeometryRef() )
                {
                    poAttribFeature->GetGeometryRef()->transform(
                        &oBlockTransformer );
                }

                if( EQUAL( poAttribFeature->GetFieldAsString( "Layer" ), "0" ) &&
                    !EQUAL( poOverallFeature->GetFieldAsString( "Layer" ), "" ) )
                {
                    poAttribFeature->SetField( "Layer",
                        poOverallFeature->GetFieldAsString( "Layer" ) );
                }

                PrepareFeatureStyle( poAttribFeature, poOverallFeature );

                ACAdjustText( oBlockTransformer.dfAngle * 180 / M_PI,
                    oBlockTransformer.dfXScale, oBlockTransformer.dfYScale,
                    poAttribFeature );

                if ( !EQUAL( poOverallFeature->GetFieldAsString(
                    "EntityHandle" ), "" ) )
                {
                    poAttribFeature->SetField( "EntityHandle",
                        poOverallFeature->GetFieldAsString( "EntityHandle" ) );
                }

                apoPendingFeatures.push( poAttribFeature );
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Prepare a new feature to serve as the leader text label         */
/*      refeature.  We will push it onto the layer as a pending           */
/*      feature for the next feature read.                              */
/* -------------------------------------------------------------------- */

    if( osText.empty() || osText == " " )
    {
        delete poOverallFeature;
        return poLeaderFeature;
    }

    OGRDXFFeature *poLabelFeature = poOverallFeature->CloneDXFFeature();

    poLabelFeature->SetField( "Text", osText );
    poLabelFeature->SetGeometryDirectly( new OGRPoint( dfTextX, dfTextY ) );

    CPLString osStyle;
    char szBuffer[64];

    const CPLString osStyleName =
        poDS->GetTextStyleNameByHandle( osTextStyleHandle );

    // Font name
    osStyle.Printf("LABEL(f:\"");

    // Preserve legacy behaviour of specifying "Arial" as a default font name.
    osStyle += poDS->LookupTextStyleProperty( osStyleName, "Font", "Arial" );

    osStyle += "\"";

    // Bold, italic
    if( EQUAL( poDS->LookupTextStyleProperty( osStyleName,
        "Bold", "0" ), "1" ) )
    {
        osStyle += ",bo:1";
    }
    if( EQUAL( poDS->LookupTextStyleProperty( osStyleName,
        "Italic", "0" ), "1" ) )
    {
        osStyle += ",it:1";
    }

    osStyle += CPLString().Printf(",t:\"%s\",p:%d", osText.c_str(),
        nTextAlignment + 6); // 7,8,9: vertical align top

    if( dfTextAngle != 0.0 )
    {
        CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfTextAngle);
        osStyle += CPLString().Printf(",a:%s", szBuffer);
    }

    if( dfTextHeight != 0.0 )
    {
        CPLsnprintf(szBuffer, sizeof(szBuffer), "%.3g", dfTextHeight);
        osStyle += CPLString().Printf(",s:%sg", szBuffer);
    }

    const char *pszWidthFactor = poDS->LookupTextStyleProperty( osStyleName,
        "Width", "1" );
    if( pszWidthFactor && CPLAtof( pszWidthFactor ) != 1.0 )
    {
        CPLsnprintf(szBuffer, sizeof(szBuffer), "%.4g",
            CPLAtof( pszWidthFactor ) * 100.0);
        osStyle += CPLString().Printf(",w:%s", szBuffer);
    }

    // Color
    osStyle += ",c:";
    osStyle += poLabelFeature->GetColor( poDS );

    osStyle += ")";

    poLabelFeature->SetStyleString( osStyle );

    apoPendingFeatures.push( poLabelFeature );

    delete poOverallFeature;
    return poLeaderFeature;
}

/************************************************************************/
/*                     GenerateDefaultArrowhead()                       */
/*                                                                      */
/*      Generates the default DWG/DXF arrowhead (a filled triangle      */
/*      with a 3:1 aspect ratio) on the end of the line segment         */
/*      defined by the two points.                                      */
/************************************************************************/
static void GenerateDefaultArrowhead( OGRDXFFeature* const poArrowheadFeature,
    const OGRPoint& oPoint1, const OGRPoint& oPoint2,
    const double dfArrowheadScale )

{
    // calculate the baseline to be expanded out into arrowheads
    const double dfParallelPartX = dfArrowheadScale *
        (oPoint2.getX() - oPoint1.getX());
    const double dfParallelPartY = dfArrowheadScale *
        (oPoint2.getY() - oPoint1.getY());
    // ...and drop a perpendicular
    const double dfPerpPartX = dfParallelPartY;
    const double dfPerpPartY = -dfParallelPartX;

    OGRLinearRing *poLinearRing = new OGRLinearRing();
    poLinearRing->setPoint( 0,
        oPoint1.getX() + dfParallelPartX + dfPerpPartX/6,
        oPoint1.getY() + dfParallelPartY + dfPerpPartY/6,
        oPoint1.getZ() );
    poLinearRing->setPoint( 1,
        oPoint1.getX(),
        oPoint1.getY(),
        oPoint1.getZ() );
    poLinearRing->setPoint( 2,
        oPoint1.getX() + dfParallelPartX - dfPerpPartX/6,
        oPoint1.getY() + dfParallelPartY - dfPerpPartY/6,
        oPoint1.getZ() );
    poLinearRing->closeRings();

    OGRPolygon* poPoly = new OGRPolygon();
    poPoly->addRingDirectly( poLinearRing );

    poArrowheadFeature->SetGeometryDirectly( poPoly );
}

/************************************************************************/
/*                          InsertArrowhead()                           */
/*                                                                      */
/*      Inserts the specified arrowhead block at the start of the       */
/*      first segment of the given line string (or the end of the       */
/*      last segment if bReverse is false).  2D only.                   */
/*                                                                      */
/*      The first (last) point of the line string may be updated.       */
/************************************************************************/
void OGRDXFLayer::InsertArrowhead( OGRDXFFeature* const poFeature,
    const CPLString& osBlockHandle, OGRLineString* const poLine,
    const double dfArrowheadSize, const bool bReverse /* = false */ )
{
    OGRPoint oPoint1, oPoint2;
    poLine->getPoint( bReverse ? poLine->getNumPoints() - 1 : 0, &oPoint1 );
    poLine->getPoint( bReverse ? poLine->getNumPoints() - 2 : 1, &oPoint2 );

    const double dfFirstSegmentLength = PointDist( oPoint1.getX(),
        oPoint1.getY(), oPoint2.getX(), oPoint2.getY() );

    // AutoCAD only displays an arrowhead if the length of the arrowhead
    // is less than or equal to half the length of the line segment
    if( dfArrowheadSize == 0.0 || dfFirstSegmentLength == 0.0 ||
        dfArrowheadSize > 0.5 * dfFirstSegmentLength )
    {
        return;
    }

    OGRDXFFeature *poArrowheadFeature = poFeature->CloneDXFFeature();

    // Convert the block handle to a block name.
    CPLString osBlockName = "";

    if( osBlockHandle != "" )
        osBlockName = poDS->GetBlockNameByRecordHandle( osBlockHandle );

    OGRDXFFeatureQueue apoExtraFeatures;

    // If the block doesn't exist, we need to fall back to the
    if( osBlockName == "" )
    {
        GenerateDefaultArrowhead( poArrowheadFeature, oPoint1, oPoint2,
            dfArrowheadSize / dfFirstSegmentLength );

        PrepareBrushStyle( poArrowheadFeature );
    }
    else
    {
        // Build a transformer to insert the arrowhead block with the
        // required location, angle and scale.
        OGRDXFInsertTransformer oTransformer;
        oTransformer.dfXOffset = oPoint1.getX();
        oTransformer.dfYOffset = oPoint1.getY();
        oTransformer.dfZOffset = oPoint1.getZ();
        // Arrowhead blocks always point to the right (--->)
        oTransformer.dfAngle = atan2( oPoint2.getY() - oPoint1.getY(),
            oPoint2.getX() - oPoint1.getX() ) + M_PI;
        oTransformer.dfXScale = oTransformer.dfYScale =
            oTransformer.dfZScale = dfArrowheadSize;

        // Insert the block.
        try
        {
            poArrowheadFeature = InsertBlockInline(
                CPLGetErrorCounter(), osBlockName,
                oTransformer, poArrowheadFeature, apoExtraFeatures,
                true, false );
        }
        catch( const std::invalid_argument& )
        {
            // Supposedly the block doesn't exist. But what has probably
            // happened is that the block exists in the DXF, but it contains
            // no entities, so the data source didn't read it in.
            // In this case, no arrowhead is required.
            delete poArrowheadFeature;
            poArrowheadFeature = nullptr;
        }
    }

    // Add the arrowhead geometries to the pending feature stack.
    if( poArrowheadFeature )
    {
        apoPendingFeatures.push( poArrowheadFeature );
    }
    while( !apoExtraFeatures.empty() )
    {
        apoPendingFeatures.push( apoExtraFeatures.front() );
        apoExtraFeatures.pop();
    }

    // Move the endpoint of the line out of the way of the arrowhead.
    // We assume that arrowheads are 1 unit long, except for a list
    // of specific block names which are treated as having no length

    static const char* apszSpecialArrowheads[] = {
        "_ArchTick",
        "_DotSmall",
        "_Integral",
        "_None",
        "_Oblique",
        "_Small"
    };

    if( std::find( apszSpecialArrowheads, apszSpecialArrowheads + 6,
        osBlockName ) == ( apszSpecialArrowheads + 6 ) )
    {
        oPoint1.setX( oPoint1.getX() + dfArrowheadSize *
            ( oPoint2.getX() - oPoint1.getX() ) / dfFirstSegmentLength );
        oPoint1.setY( oPoint1.getY() + dfArrowheadSize *
            ( oPoint2.getY() - oPoint1.getY() ) / dfFirstSegmentLength );

        poLine->setPoint( bReverse ? poLine->getNumPoints() - 1 : 0,
            &oPoint1 );
    }
}

/************************************************************************/
/*                        basis(), rbspline2()                          */
/*                                                                      */
/*      Spline calculation functions defined in intronurbs.cpp.         */
/************************************************************************/
void basis( int c, double t, int npts, double x[], double N[] );
void rbspline2( int npts,int k,int p1,double b[],double h[],
    bool bCalculateKnots, double x[], double p[] );

/************************************************************************/
/*                      GetBSplineControlPoints()                       */
/*                                                                      */
/*      Evaluates the control points for the B-spline of given degree   */
/*      that interpolates the given data points, using the given        */
/*      parameters, start tangent and end tangent.  The parameters      */
/*      and knot vector must be increasing sequences with first         */
/*      element 0 and last element 1.  Given n data points, there       */
/*      must be n parameters and n + nDegree + 3 knots.                 */
/*                                                                      */
/*      It is recommended to match AutoCAD by generating a knot         */
/*      vector from the parameters as follows:                          */
/*              0 0 ... 0 adfParameters 1 1 ... 1                       */
/*        (nDegree zeros)               (nDegree ones)                  */
/*      To fully match AutoCAD's behaviour, a chord-length              */
/*      parameterisation should be used, and the start and end          */
/*      tangent vectors should be multiplied by the total chord         */
/*      length of all chords.                                           */
/*                                                                      */
/*      Reference: Piegl, L., Tiller, W. (1995), The NURBS Book,        */
/*      2nd ed. (Springer), sections 2.2 and 9.2.                       */
/*      Although this book contains implementations of algorithms,      */
/*      this function is an original implementation based on the        */
/*      concepts discussed in the book and was written without          */
/*      reference to Piegl and Tiller's implementations.                */
/************************************************************************/
static std::vector<DXFTriple> GetBSplineControlPoints(
    const std::vector<double>& adfParameters,
    const std::vector<double>& adfKnots,
    const std::vector<DXFTriple>& aoDataPoints, const int nDegree,
    DXFTriple oStartTangent, DXFTriple oEndTangent )
{
    CPLAssert( nDegree > 1 );

    // Count the number of data points
    // Note: The literature often sets n to one less than the number of data
    // points for some reason, but we don't do that here
    const int nPoints = static_cast<int>( aoDataPoints.size() );

    CPLAssert( nPoints > 0 );
    CPLAssert( nPoints == static_cast<int>( adfParameters.size() ) );

    // RAM consumption is quadratic in the number of control points.
    if( nPoints > atoi(CPLGetConfigOption(
                        "DXF_MAX_BSPLINE_CONTROL_POINTS", "2000")) )
    {
        CPLError(CE_Failure, CPLE_AppDefined,
                 "Too many control points (%d) for spline leader. "
                 "Set DXF_MAX_BSPLINE_CONTROL_POINTS configuration "
                 "option to a higher value to remove this limitation "
                 "(at the cost of significant RAM consumption)", nPoints);
        return std::vector<DXFTriple>();
    }

    // We want to solve the linear system NP=D for P, where N is a coefficient
    // matrix made up of values of the basis functions at each parameter
    // value, with two additional rows for the endpoint tangent information.
    // Each row relates to a different parameter.
    std::vector<double> adfN( (nPoints + 2) * (nPoints + 2), 0.0 );

#define ACCESS_adfN(row,col) adfN[(row) * (nPoints + 2) + (col)]

    // Set up D as a matrix consisting initially of the data points
    std::vector<double> adfD( (nPoints + 2) * 3, 0.0 );

    aoDataPoints[0].ToArray( &adfD[0] );
    for( int iIndex = 1; iIndex < nPoints - 1; iIndex++ )
        aoDataPoints[iIndex].ToArray( &adfD[(iIndex + 1) * 3] );
    aoDataPoints[nPoints - 1].ToArray( &adfD[(nPoints + 1) * 3] );

    const double dfStartMultiplier = adfKnots[nDegree + 1] / nDegree;
    oStartTangent *= dfStartMultiplier;
    oStartTangent.ToArray( &adfD[3] );

    const double dfEndMultiplier = ( 1.0 - adfKnots[nPoints + 1] ) / nDegree;
    oEndTangent *= dfEndMultiplier;
    oEndTangent.ToArray( &adfD[nPoints * 3] );

    // First control point will be the first data point
    ACCESS_adfN(0,0) = 1.0;

    // Start tangent determines the second control point
    ACCESS_adfN(1,0) = -1.0;
    ACCESS_adfN(1,1) = 1.0;

    // Fill the middle rows of the matrix with basis function values. We
    // have to use a temporary vector, because intronurbs' basis function
    // requires an additional nDegree entries for temporary storage.
    std::vector<double> adfTempRow( nPoints + 2 + nDegree, 0.0 );
    for( int iRow = 2; iRow < nPoints; iRow++ )
    {
        basis( nDegree + 1, adfParameters[iRow - 1], nPoints + 2,
            const_cast<double *>( &adfKnots[0] ) - 1, &adfTempRow[0] - 1 );
        std::copy_n( adfTempRow.begin(), nPoints + 2,
            adfN.begin() + iRow * (nPoints + 2) );
    }

    // End tangent determines the second-last control point
    ACCESS_adfN(nPoints,nPoints) = -1.0;
    ACCESS_adfN(nPoints,nPoints + 1) = 1.0;

    // Last control point will be the last data point
    ACCESS_adfN(nPoints + 1,nPoints + 1) = 1.0;

    // Solve the linear system
    std::vector<double> adfP( (nPoints + 2) * 3 );
    GDALLinearSystemSolve( nPoints + 2, 3, &adfN[0], &adfD[0], &adfP[0] );

    std::vector<DXFTriple> aoControlPoints( nPoints + 2 );
    for( int iRow = 0; iRow < nPoints + 2; iRow++ )
    {
        aoControlPoints[iRow].dfX = adfP[iRow * 3];
        aoControlPoints[iRow].dfY = adfP[iRow * 3 + 1];
        aoControlPoints[iRow].dfZ = adfP[iRow * 3 + 2];
    }

    return aoControlPoints;
}

/************************************************************************/
/*                         InterpolateSpline()                          */
/*                                                                      */
/*      Interpolates a cubic spline between the data points of the      */
/*      given line string. The line string is updated with the new      */
/*      spline geometry.                                                */
/*                                                                      */
/*      If an end tangent of (0,0,0) is given, the direction vector     */
/*      of the last chord (line segment) is used.                       */
/************************************************************************/
static void InterpolateSpline( OGRLineString* const poLine,
    const DXFTriple& oEndTangentDirection )
{
    int nDataPoints = static_cast<int>( poLine->getNumPoints() );
    if ( nDataPoints < 2 )
        return;

    // Transfer line vertices into DXFTriple objects
    std::vector<DXFTriple> aoDataPoints;
    OGRPoint oPrevPoint;
    for( int iIndex = 0; iIndex < nDataPoints; iIndex++ )
    {
        OGRPoint oPoint;
        poLine->getPoint( iIndex, &oPoint );

        // Remove sequential duplicate points
        if( iIndex > 0 && oPrevPoint.Equals( &oPoint ) )
            continue;

        aoDataPoints.push_back( DXFTriple( oPoint.getX(), oPoint.getY(),
            oPoint.getZ() ) );
        oPrevPoint = oPoint;
    }
    nDataPoints = static_cast<int>( aoDataPoints.size() );
    if( nDataPoints < 2 )
        return;

    // Work out the chord length parameterisation
    std::vector<double> adfParameters;
    adfParameters.push_back( 0.0 );
    for( int iIndex = 1; iIndex < nDataPoints; iIndex++ )
    {
        const double dfParameter = adfParameters[iIndex - 1] +
            PointDist( aoDataPoints[iIndex - 1].dfX,
                aoDataPoints[iIndex - 1].dfY,
                aoDataPoints[iIndex - 1].dfZ,
                aoDataPoints[iIndex].dfX,
                aoDataPoints[iIndex].dfY,
                aoDataPoints[iIndex].dfZ );

        // Bail out in pathological cases. This will happen when
        // some lengths are very large (above 10^16) and others are
        // very small (such as 1)
        if( dfParameter == adfParameters[iIndex - 1] )
            return;

        adfParameters.push_back( dfParameter );
    }

    const double dfTotalChordLength = adfParameters[adfParameters.size() - 1];

    // Start tangent can be worked out from the first chord
    DXFTriple oStartTangent( aoDataPoints[1].dfX - aoDataPoints[0].dfX,
        aoDataPoints[1].dfY - aoDataPoints[0].dfY,
        aoDataPoints[1].dfZ - aoDataPoints[0].dfZ );
    oStartTangent *= dfTotalChordLength / adfParameters[1];

    // If end tangent is zero, it is worked out from the last chord
    DXFTriple oEndTangent = oEndTangentDirection;
    if( oEndTangent.dfX == 0.0 && oEndTangent.dfY == 0.0 &&
        oEndTangent.dfZ == 0.0 )
    {
        oEndTangent = DXFTriple(
            aoDataPoints[nDataPoints - 1].dfX -
                aoDataPoints[nDataPoints - 2].dfX,
            aoDataPoints[nDataPoints - 1].dfY -
                aoDataPoints[nDataPoints - 2].dfY,
            aoDataPoints[nDataPoints - 1].dfZ -
                aoDataPoints[nDataPoints - 2].dfZ );
        oEndTangent /= dfTotalChordLength - adfParameters[nDataPoints - 2];
    }

    // End tangent direction is multiplied by total chord length
    oEndTangent *= dfTotalChordLength;

    // Normalise the parameter vector
    for( int iIndex = 1; iIndex < nDataPoints; iIndex++ )
        adfParameters[iIndex] /= dfTotalChordLength;

    // Generate a knot vector
    const int nDegree = 3;
    std::vector<double> adfKnots( aoDataPoints.size() + nDegree + 3, 0.0 );
    std::copy( adfParameters.begin(), adfParameters.end(),
        adfKnots.begin() + nDegree );
    std::fill( adfKnots.end() - nDegree, adfKnots.end(), 1.0 );

    // Calculate the spline control points
    std::vector<DXFTriple> aoControlPoints = GetBSplineControlPoints(
        adfParameters, adfKnots, aoDataPoints, nDegree,
        oStartTangent, oEndTangent );
    const int nControlPoints = static_cast<int>( aoControlPoints.size() );

    if( nControlPoints == 0 )
        return;

    // Interpolate the spline using the intronurbs code
    int nWantedPoints = nControlPoints * 8;
    std::vector<double> adfWeights( nControlPoints, 1.0 );
    std::vector<double> adfPoints( 3 * nWantedPoints, 0.0 );

    rbspline2( nControlPoints, nDegree + 1, nWantedPoints,
        reinterpret_cast<double*>( &aoControlPoints[0] ) - 1,
        &adfWeights[0] - 1, false, &adfKnots[0] - 1, &adfPoints[0] - 1 );

    // Preserve 2D/3D status as we add the interpolated points to the line
    const int bIs3D = poLine->Is3D();
    poLine->empty();
    for( int iIndex = 0; iIndex < nWantedPoints; iIndex++ )
    {
        poLine->addPoint( adfPoints[iIndex * 3], adfPoints[iIndex * 3 + 1],
            adfPoints[iIndex * 3 + 2] );
    }
    if( !bIs3D )
        poLine->flattenTo2D();
}