示例#1
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(ArrayTest, ExtractElementsFromIndexedArray)
{
    ///   source               = {2.0, 5.5, 100.0}
    ///   perItemSourceIndices = {  0,     2,   1,   0,     2}
    ///   -> output            = {2.0, 100.0, 5.5, 2.0, 100.0}
    FloatArray source;
    source.reserve(3);
    source.add(2.0f);
    source.add(5.5f);
    source.add(100.0f);

    UIntArray indices;
    indices.reserve(5);
    indices.add(0);
    indices.add(2);
    indices.add(1);
    indices.add(0);
    indices.add(2);

    ref<FloatArray> arr = source.extractElements(indices);
    ASSERT_EQ(5, arr->size());
    EXPECT_FLOAT_EQ(  2.0, arr->get(0));
    EXPECT_FLOAT_EQ(100.0, arr->get(1));
    EXPECT_FLOAT_EQ(  5.5, arr->get(2));
    EXPECT_FLOAT_EQ(  2.0, arr->get(3));
    EXPECT_FLOAT_EQ(100.0, arr->get(4));
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(MeshEdgeExtractorTest, ThreeQuadsFromUIntArray)
{
    // 3------4------5   9------8
    // |      |      |   |      | 
    // |      |      |   |      | 
    // 0------1------2   6------7

    MeshEdgeExtractor ee;

    // Two connected quads
    {
        UIntArray q;
        q.reserve(2*4);

        // Observe different winding
        q.add(0);   q.add(1);   q.add(4);   q.add(3);
        q.add(1);   q.add(4);   q.add(5);   q.add(2);

        ee.addPrimitives(4, q);
    }

    // Single loose quad
    {
        UIntArray q;
        q.reserve(4);
        q.add(6);   q.add(7);   q.add(8);   q.add(9);

        ee.addPrimitives(4, q);
    }

    ref<UIntArray> li = ee.lineIndices();
    ASSERT_EQ(2*11, li->size());

    EXPECT_EQ(0, li->get(0));    EXPECT_EQ(1, li->get(1));
    EXPECT_EQ(0, li->get(2));    EXPECT_EQ(3, li->get(3));
    EXPECT_EQ(1, li->get(4));    EXPECT_EQ(2, li->get(5));
    EXPECT_EQ(1, li->get(6));    EXPECT_EQ(4, li->get(7));
    EXPECT_EQ(2, li->get(8));    EXPECT_EQ(5, li->get(9));
    EXPECT_EQ(3, li->get(10));   EXPECT_EQ(4, li->get(11));
    EXPECT_EQ(4, li->get(12));   EXPECT_EQ(5, li->get(13));

    EXPECT_EQ(6, li->get(14));   EXPECT_EQ(7, li->get(15));
    EXPECT_EQ(6, li->get(16));   EXPECT_EQ(9, li->get(17));
    EXPECT_EQ(7, li->get(18));   EXPECT_EQ(8, li->get(19));
    EXPECT_EQ(8, li->get(20));   EXPECT_EQ(9, li->get(21));
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(MeshEdgeExtractorTest, PrimitiveMixFromFaceList)
{
    //           6----5
    //          /      \      *11
    //         /        \         *12
    // 8------7          4-----9   
    // |      |\        /      |
    // |      | \      /       |
    // 0------1--2----3       10   

    UIntArray fl;
    fl.reserve(26);

    fl.add(1);  fl.add(11);  
    fl.add(1);  fl.add(12);
    fl.add(2);  fl.add(4);  fl.add(9);  
    fl.add(2);  fl.add(10); fl.add(9);  
    fl.add(3);  fl.add(1);  fl.add(2);  fl.add(7); 
    fl.add(4);  fl.add(0);  fl.add(1);  fl.add(7);  fl.add(8); 
    fl.add(6);  fl.add(2);  fl.add(3);  fl.add(4);  fl.add(5);  fl.add(6);  fl.add(7); 
    ASSERT_EQ(26, fl.size());


    MeshEdgeExtractor ee;
    ee.addFaceList(fl);

    UIntArray li = *ee.lineIndices();
    ASSERT_EQ(2*13, li.size());

    EXPECT_EQ(0, li[ 0]);    EXPECT_EQ(1, li[ 1]);
    EXPECT_EQ(0, li[ 2]);    EXPECT_EQ(8, li[ 3]);
    EXPECT_EQ(1, li[ 4]);    EXPECT_EQ(2, li[ 5]);
    EXPECT_EQ(1, li[ 6]);    EXPECT_EQ(7, li[ 7]);
    EXPECT_EQ(2, li[ 8]);    EXPECT_EQ(3, li[ 9]);
    EXPECT_EQ(2, li[10]);    EXPECT_EQ(7, li[11]);
    EXPECT_EQ(3, li[12]);    EXPECT_EQ(4, li[13]);
    EXPECT_EQ(4, li[14]);    EXPECT_EQ(5, li[15]);
    EXPECT_EQ(4, li[16]);    EXPECT_EQ(9, li[17]);
    EXPECT_EQ(5, li[18]);    EXPECT_EQ(6, li[19]);
    EXPECT_EQ(6, li[20]);    EXPECT_EQ(7, li[21]);
    EXPECT_EQ(7, li[22]);    EXPECT_EQ(8, li[23]);
    EXPECT_EQ(9, li[24]);    EXPECT_EQ(10,li[25]);
}
示例#4
0
//--------------------------------------------------------------------------------------------------
/// Create a 2D patch
/// 
/// \param origin       The start point of the patch
/// \param uUnit        Direction vector u. First point 'to the right of' origin is origin + uUnit.
/// \param vUnit        Direction vector v. Coordinates of first point 'above' origin is origin + vunit. 
/// \param uCellCount   The number of cells/quads to generate along the uUnit dimension.
/// \param vCellCount   The number of cells/quads to generate along the vUnit dimension.
/// \param builder      Geometry builder to use when creating geometry
/// 
/// The figure below illustrates how the patch is constructed from the specified parameters.
/// 
/// <PRE>
///         v8-----v9----v10----v11             Parameters:            Resulting vertices:
///         |      |      |      |                origin = (10,20,0)     v0 = (10,20,0)
///  origin |      |      |      |                uUnit  = (2,0,0)       v1 = (12,20,0)
/// + vunit v4-----v5-----v6-----v7   |y          vUnit  = (0,1,0)       v2 = (14,20,0)
///         |      |      |      |    |           uCellCount = 3         v3 = (16,20,0)
///         |      |      |      |    |           vCellCount = 2         v4 = (10,21,0)
///         v0-----v1-----v2-----v3   *----x                             v5 = (12,21,0)
///     origin    origin                                                 :
///              + uUnit   </PRE>
/// 
/// The following quad connectivities will be produced:\n
/// <TT> &nbsp; &nbsp; (v4,v0,v1,v5)  (v5,v1,v2,v6)  (v6,v2,v3,v5) ... (v10,v6,v7,v11)</TT>
//--------------------------------------------------------------------------------------------------
void GeometryUtils::createPatch(const Vec3f& origin, const Vec3f& uUnit, const Vec3f& vUnit, uint uCellCount, uint vCellCount, GeometryBuilder* builder)
{
    CVF_ASSERT(uCellCount > 0);
    CVF_ASSERT(vCellCount > 0);

    uint numVertices = (uCellCount + 1)*(vCellCount + 1);
    uint numQuads = uCellCount*vCellCount;
    
    Vec3fArray vertices;
    vertices.reserve(numVertices);

    uint u, v;
    for (v = 0; v <= vCellCount; v++)
    {
        for (u = 0; u <= uCellCount; u++)
        {
            vertices.add(origin + static_cast<float>(u)*uUnit + static_cast<float>(v)*vUnit);
        }
    }

    uint baseNodeIdx = builder->addVertices(vertices);

    UIntArray conn;
    conn.reserve(4*numQuads);

    for (v = 0; v < vCellCount; v++)
    {
        for (u = 0; u < uCellCount; u++)
        {
            conn.add(baseNodeIdx + u     + (v + 1)*(uCellCount + 1));
            conn.add(baseNodeIdx + u     + v*(uCellCount + 1));
            conn.add(baseNodeIdx + u + 1 + v*(uCellCount + 1));
            conn.add(baseNodeIdx + u + 1 + (v + 1)*(uCellCount + 1));
        }
    }

    builder->addQuads(conn);
}
示例#5
0
Array* Array_readLocalData(Input& fr)
{
    if (fr[0].matchWord("Use"))
    {
        if (fr[1].isString())
        {
            Object* obj = fr.getObjectForUniqueID(fr[1].getStr());
            if (obj)
            {
                fr+=2;
                return dynamic_cast<Array*>(obj);
            }
        }

        osg::notify(osg::WARN)<<"Warning: invalid uniqueID found in file."<<std::endl;
        return NULL;
    }

    std::string uniqueID;
    if (fr[0].matchWord("UniqueID") && fr[1].isString())
    {
        uniqueID = fr[1].getStr();
        fr += 2;
    }


    int entry = fr[0].getNoNestedBrackets();

    const char* arrayName = fr[0].getStr();

    unsigned int capacity = 0;
    fr[1].getUInt(capacity);
    ++fr;

    fr += 2;


    Array* return_array = 0;

    if (strcmp(arrayName,"ByteArray")==0)
    {
        ByteArray* array = new ByteArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;

        return_array = array;
    }
    else if (strcmp(arrayName,"ShortArray")==0)
    {
        ShortArray* array = new ShortArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"IntArray")==0)
    {
        IntArray* array = new IntArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UByteArray")==0)
    {
        UByteArray* array = new UByteArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UShortArray")==0)
    {
        UShortArray* array = new UShortArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UIntArray")==0)
    {
        UIntArray* array = new UIntArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UVec4bArray")==0 || strcmp(arrayName,"Vec4ubArray")==0)
    {
        Vec4ubArray* array = new Vec4ubArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4ub(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"FloatArray")==0)
    {
        FloatArray* array = new FloatArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            float float_value;
            if (fr[0].getFloat(float_value))
            {
                ++fr;
                array->push_back(float_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"DoubleArray")==0)
    {
        DoubleArray* array = new DoubleArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            double double_value;
            if (fr[0].getFloat(double_value))
            {
                ++fr;
                array->push_back(double_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2Array")==0)
    {
        Vec2Array* array = new Vec2Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec2 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()))
            {
                fr += 2;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2dArray")==0)
    {
        Vec2dArray* array = new Vec2dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec2d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()))
            {
                fr += 2;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3Array")==0)
    {
        Vec3Array* array = new Vec3Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec3 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()))
            {
                fr += 3;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3dArray")==0)
    {
        Vec3dArray* array = new Vec3dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec3d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()))
            {
                fr += 3;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4Array")==0)
    {
        Vec4Array* array = new Vec4Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec4 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w()))
            {
                fr += 4;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4dArray")==0)
    {
        Vec4dArray* array = new Vec4dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec4d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w()))
            {
                fr += 4;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2bArray")==0)
    {
        Vec2bArray* array = new Vec2bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g))
            {
                fr+=2;
                array->push_back(osg::Vec2b(r,g));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3bArray")==0)
    {
        Vec3bArray* array = new Vec3bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b))
            {
                fr+=3;
                array->push_back(osg::Vec3b(r,g,b));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4bArray")==0)
    {
        Vec4bArray* array = new Vec4bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4b(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2sArray")==0)
    {
        Vec2sArray* array = new Vec2sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g))
            {
                fr+=2;
                array->push_back(osg::Vec2s(r,g));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3sArray")==0)
    {
        Vec3sArray* array = new Vec3sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b))
            {
                fr+=3;
                array->push_back(osg::Vec3s(r,g,b));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4sArray")==0)
    {
        Vec4sArray* array = new Vec4sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4s(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }

    if (return_array)
    {
        if (!uniqueID.empty()) fr.registerUniqueIDForObject(uniqueID.c_str(),return_array);
    }

    return return_array;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(OutlineEdgeExtractorTest, PrimitiveMixFromFaceList)
{
    ref<Vec3fArray> va = new Vec3fArray;
    va->resize(13);
    va->set( 0, Vec3f(0, 0, 0));
    va->set( 1, Vec3f(1, 0, 0));
    va->set( 2, Vec3f(2, 0, 0));
    va->set( 3, Vec3f(3, 0, 0));
    va->set( 4, Vec3f(4, 1, 0));
    va->set( 5, Vec3f(3, 2, 0));
    va->set( 6, Vec3f(2, 2, 0));
    va->set( 7, Vec3f(1, 1, 0));
    va->set( 8, Vec3f(0, 1, 0));
    va->set( 9, Vec3f(5, 1, 0));
    va->set(10, Vec3f(5, 0, 0));
    va->set(11, Vec3f(6, 6, 0));
    va->set(12, Vec3f(7, 7, 0));

    //                 
    //                        *11
    //                            *12
    // 8------7          4-----9   
    // |      |\               |
    // |      | \              |
    // 0------1--2            10   

    UIntArray fl;
    fl.reserve(26);

    fl.add(1);  fl.add(11);  
    fl.add(1);  fl.add(12);
    fl.add(2);  fl.add(4);  fl.add(9);  
    fl.add(2);  fl.add(10); fl.add(9);  
    fl.add(3);  fl.add(1);  fl.add(2);  fl.add(7); 
    fl.add(4);  fl.add(0);  fl.add(1);  fl.add(7);  fl.add(8); 
    ASSERT_EQ(19, fl.size());

    OutlineEdgeExtractor ee(0, *va);
    ee.addFaceList(fl);

    UIntArray li = *ee.lineIndices();
    ASSERT_EQ(2*7, li.size());

    EXPECT_TRUE( EdgeKey(0, 1) == EdgeKey(li[ 0], li[ 1]) );
    EXPECT_TRUE( EdgeKey(0, 8) == EdgeKey(li[ 2], li[ 3]) );
    EXPECT_TRUE( EdgeKey(1, 2) == EdgeKey(li[ 4], li[ 5]) );
    EXPECT_TRUE( EdgeKey(2, 7) == EdgeKey(li[ 6], li[ 7]) );
    EXPECT_TRUE( EdgeKey(4, 9) == EdgeKey(li[ 8], li[ 9]) );
    EXPECT_TRUE( EdgeKey(7, 8) == EdgeKey(li[10], li[11]) );
    EXPECT_TRUE( EdgeKey(9,10) == EdgeKey(li[12], li[13]) );

    /*
    //           6----5
    //          /      \      *11
    //         /        \         *12
    // 8------7          4-----9   
    // |      |\        /      |
    // |      | \      /       |
    // 0------1--2----3       10   

    fl.add(1);  fl.add(11);  
    fl.add(1);  fl.add(12);
    fl.add(2);  fl.add(4);  fl.add(9);  
    fl.add(2);  fl.add(10); fl.add(9);  
    fl.add(3);  fl.add(1);  fl.add(2);  fl.add(7); 
    fl.add(4);  fl.add(0);  fl.add(1);  fl.add(7);  fl.add(8); 
    fl.add(6);  fl.add(2);  fl.add(3);  fl.add(4);  fl.add(5);  fl.add(6);  fl.add(7); 
    ASSERT_EQ(26, fl.size());

    OutlineEdgeExtractor ee(0, *va);
    ee.addFaceList(fl);

    UIntArray li = *ee.lineIndices();
    ASSERT_EQ(2*11, li.size());

    EXPECT_TRUE( EdgeKey(0, 1) == EdgeKey(li[ 0], li[ 1]) );
    EXPECT_TRUE( EdgeKey(0, 8) == EdgeKey(li[ 2], li[ 3]) );
    EXPECT_TRUE( EdgeKey(1, 2) == EdgeKey(li[ 4], li[ 5]) );
    EXPECT_TRUE( EdgeKey(2, 3) == EdgeKey(li[ 6], li[ 7]) );
    EXPECT_TRUE( EdgeKey(3, 4) == EdgeKey(li[ 8], li[ 9]) );
    EXPECT_TRUE( EdgeKey(4, 5) == EdgeKey(li[10], li[11]) );
    EXPECT_TRUE( EdgeKey(4, 9) == EdgeKey(li[12], li[13]) );
    EXPECT_TRUE( EdgeKey(5, 6) == EdgeKey(li[14], li[15]) );
    EXPECT_TRUE( EdgeKey(6, 7) == EdgeKey(li[16], li[17]) );
    EXPECT_TRUE( EdgeKey(7, 8) == EdgeKey(li[18], li[19]) );
    EXPECT_TRUE( EdgeKey(9,10) == EdgeKey(li[20], li[21]) );
    */
}