NACA Airfoil script

 From:  Hamish Mead (HAIRYKIWI)
7265.1 
Hi everyone,

First off - many thanks Petr, Bemfarmer, Smirnov and Michael for all your really helpful javascript examples. Also to David Morrill for his MoI javascript reference.

I'm part way through making a script to generate NACA four- and five-digit series airfoils (wing sections). It could probably easily be extended at a later date to generate the six-digit series also.

Right now I'd like to try improving code efficiency and need some help please. Here's the problem:
Reverse the array of (x,y,z) points within one interpcurve GeometryFactory, then add to it the array of points from a second GeometryFactory - more efficiently than the method I've so far hacked together in the code below.

Where I'm at:
Two working versions -
- One generates two separate upper and lower curves - this code seems reasonably fast enough.
- The other (code snippet below) generates one continuous curve. It's significantly slower because it has to copy the x,y,z, values of points from two PointObjectLists (upper and lower) to a single 'interpcurve' factory.
The benefit of the second version is that it improves on the first by removing a subtle blip in curvature continuity at the airfoil Leading Edge in a 'two curve' solution.

MoI JavaScript code:
Update()
{
    var num_stations = moi.ui.commandUI.num_stations.numericValue;
    var cosine_spacing =  moi.ui.commandUI.cosine_spacing.value;
    var PointObjectListUpper = moi.geometryDatabase.createObjectList();
    var PointObjectListLower = moi.geometryDatabase.createObjectList();
    var FactoryAirfoil =  moi.command.createFactory( 'interpcurve' );
    var PointFactory = moi.command.createFactory( 'point' );

    for (i = 0; i < num_stations + 1; ++i)
    {
        if (cosine_spacing == 0)
        {
            x = i/num_stations;
        }
        else if (cosine_spacing == 1)
        {
            x = ( 1 - Math.cos( (i/num_stations) * Math.PI) )/2;
        }
        
        // ... airfoil upper and lower surface x y calcs happen here ...
        
        PointFactory.setInput( 0, moi.vectorMath.createPoint( x, y, z  ) );
        PointObjectListUpper.addObject( PointFactory.calculate().item(0) );
        PointFactory.reset();
        
        PointFactory.setInput( 0, moi.vectorMath.createPoint( x, y, z  ) );
        PointObjectListLower.addObject( PointFactory.calculate().item(0) );
        PointFactory.reset();
    }
    
    // Create one continuous airfoil curve using the values from the PointObjectLists calculated above.
    // Step 1: Copy points from PointObjectListUpper in REVERSE order into FactoryAirfoil
    // i.e. startpoint of airfoil curve is at the trailing edge and proceeds forward over the airfoil upper surface:
    for ( g = 0; g < num_stations + 1; ++g )
    {
        FactoryAirfoil.createInput( 'point' );
        FactoryAirfoil.setInput( g, moi.vectorMath.createPoint( PointObjectListUpper.item(num_stations - g).pt.x, \
        PointObjectListUpper.item(num_stations - g).pt.y, PointObjectListUpper.item(num_stations - g).pt.z ) );
    }
    
    // Continue along the airfoil lower surface:
    // Step 2: Copy points from PointObjectListLower directly to (end of) FactoryAirfoil
    for ( g = 1; g < num_stations + 1; ++g )
    {
        FactoryAirfoil.createInput( 'point' );
        FactoryAirfoil.setInput( num_stations + g, moi.vectorMath.createPoint( PointObjectListLower.item(g).pt.x, \
        PointObjectListLower.item(g).pt.y, PointObjectListLower.item(g).pt.z ) );
    }

    FactoryAirfoil.update();
    PointFactory.cancel();
    
    // return airfoil profile and points to main function to enable either or both to be committed
    return { 'FactoryAirfoil':FactoryAirfoil, 'PointObjectListLower': PointObjectListLower, \
    'PointObjectListUpper': PointObjectListUpper };
}


I'll post at least one working airfoil generating script when I've cleaned up the comments and aligned variable names with the 'standard' described in the pdf's below:
http://www.pdas.com/refs/tm4741.pdf
http://www.dept.aoe.vt.edu/~mason/Mason_f/CAtxtAppA.pdf
http://www.pdas.com/refs/aiaa5235.pdf
http://www.pdas.com/naca456.html
http://airfoiltools.com/airfoil/naca5digit

Cheers,
Hamish

EDITED: 13 Mar 2018 by HAIRYKIWI