UnstructGrid: Simple Model with Two Triangles

This example shows how to build a custom memory data source. The geometry is a simple structure containing two triangles only.

    // Set up the model, state and geometry.
    // This model contains two triangle elements

    // Create model and data source
    // The data source must be given a unique id and number of geometries upon construction
    cee::PtrRef<cee::ug::UnstructGridModel> ugModel = new cee::ug::UnstructGridModel();
    cee::PtrRef<cee::ug::DataSourceMemory> dataSource = new cee::ug::DataSourceMemory(1, 1);

    // Create new state with one geometry
    int stateId = 1;
    cee::PtrRef<cee::ug::DataState> state = new cee::ug::DataState(stateId, 1);

    // Create a geometry and set it in the state object
    int geometryIndex = 0;
    cee::PtrRef<cee::ug::DataGeometry> geo = new cee::ug::DataGeometry();
    state->setGeometry(geometryIndex, geo.get());

    // Add a part which consists of only two triangles

    // Define nodes
    // Add four nodes to node array. Remember to set the array size first.
    cee::PtrRef<cee::ug::DataNodes> nodes = new cee::ug::DataNodes(false);
    nodes->setNode(0, cee::Vec3d(0,1,0));
    nodes->setNode(1, cee::Vec3d(0,0,0));
    nodes->setNode(2, cee::Vec3d(1,0,0));
    nodes->setNode(3, cee::Vec3d(2,0,0));
    nodes->setNode(4, cee::Vec3d(2,1,0));

    // Define elements
    // Connectivity array for two triangles
    int c[] = {0, 1, 2, 2, 3, 4};
    std::vector<unsigned int> eltNodes1(c, c+3);    // First triangle
    std::vector<unsigned int> eltNodes2(c+3, c+6);  // Second triangle

    // Add a triangle to the elements array. 
    cee::PtrRef<cee::ug::DataElements> elements = new cee::ug::DataElements(false, 0);   
    elements->addElement(cee::ug::Element::TRIANGLES, eltNodes1);
    elements->addElement(cee::ug::Element::TRIANGLES, eltNodes2);

    // Define part. Set nodes and elements into the part object.
    int partId = 1;
    cee::PtrRef<cee::ug::DataPart> part = new cee::ug::DataPart(partId);             

    // Add the part to the geometry

    // When we're done creating the data source, we need to update the directory with the current
    // content.
    // Set the part info
    cee::ug::PartInfo partInfo(partId, "My part");
    dataSource->directory()->setPartInfo(geometryIndex, partInfo);

    // Set the state info
    cee::ug::StateInfo stateInfo(stateId, "My state", 0.0);

    // Add model to view. Ensure that old models are removed first
    cee::vis::View* gcView = getTutorialView();


    cee::BoundingBox bb = gcView->boundingBox();
    gcView->camera().fitView(bb, cee::Vec3d(0, 0, -1), cee::Vec3d(0, 1, 0));