# 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);
        ugModel->setDataSource(dataSource.get());

        // Create new state with one geometry
        int stateId = 1;
        cee::PtrRef<cee::ug::DataState> state = new cee::ug::DataState(stateId, 1);
        dataSource->addState(state.get());
        ugModel->modelSpec().setStateId(stateId);

        // 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->resize(5);
        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);
        part->setNodes(nodes.get());
        part->setElements(elements.get());

        // Add the part to the geometry
        geo->addPart(part.get());

        // 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);
        dataSource->directory()->setStateInfo(stateInfo);

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

        ugModel->updateVisualization();

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

        gcView->requestRedraw();

Conversation saved
HOOPSY BETA

### Hello! I'm HOOPSY

Your multilingual generative AI assistant for documentation. Ask me anything about HOOPS Envision for Desktop in your own language, or choose a sample question below to start a conversation:

What is HOOPS Envision for Desktop?
What are the latest release notes for HOOPS Envision for Desktop?
How do I start evaluating HOOPS Envision for Desktop?
How do I write my first sample application?
How can I get assistance?

HOOPSY may store conversations according to OpenAI's policy. Responses may not always be accurate.

HOOPSY is thinking...