.. _two-triangles-tutorial:

#############################################
UnstructGrid: Simple Model with Two Triangles
#############################################

.. image:: ../images/tut_twotriangles.png
    :height: 300
    :align: center

This tutorial shows how to create your own parts and use in a model. 
The geometry is a very simple structure containing two triangles only. 

.. note::
    This example expect the application to have a correctly configured :class:`cee::vis::View`
    in place. See demo applications on how to set up a :class:`cee::vis::View` in your application.


**Create model and data source**

Create a model and set a data source. 

Since you will be building our own geometry and not read from a file, we use a 
:class:`cee::ug::DataSourceMemory` object.

Upon creation of the :class:`cee::ug::DataSourceMemory`, you must specify a unique id for this data
source and how many geometries it will contain. Number of geometries cannot be 
changed once the data source has been created. In this example you only have one geometry.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 45-47

**Create state**

Each data source can have many data states. This simple model will only have one state.
In the construction of a state, you need to give the state an unique id and set how many
geometries it contains. 

The number of geometries for a state must always be identical to number of geometries in the data source the state is 
added to!

Create the state with an id and number of geometries and add it to the data source.

The :class:`cee::ug::ModelSpec` object is the model specification. Among many other settings, the model
specification tells which state(s) are current. Tell the model specification to use the first (and only)
state.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 50-53

**Create geometry**

Create a geometry object and add the geometry to the state at index 0 (since you only
have one state).

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 56-58

**Create nodes collection**

Create a nodes object. :class:`cee::ug::DataNodes` is a collection of nodes used for building the part. 
In this example you will use 5 nodes to create the two triangles. 
Important! The nodes object must be set to the correct size before setting the actual 
node coordinates!

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 64-70
    
**Create elements**

Create the connectivities array for the nodes in the elements. These are specified as
one std::vector<unsigned int> for each element. In this example that means two arrays,
one for each triangle.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 74-76

Create the elements object. :class:`cee::ug::DataElements` is a collection of elements used to define
a part. Each element has a element type (here TRIANGLE) and a connectivities array 
towards the matching :class:`cee::ug::DataNodes` object.

Add the two triangle elements. 

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 70-81
    

**Create the part**

Create the DataPart object.

A part consists of:

-   Node coordinates (:class:`cee::ug::DataNodes`)
-   Element connectivities (:class:`cee::ug::DataElements`)

Set the nodes and elements created above into the part.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 84-87

Add the part to the geometry

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 90


**Set up the created model**

When you're done creating the new data source (or have modified it), you need to 
populate the directory with the latest changes.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 94-100

The model is ready to use and can be added to the view. Exactly where the view exists
depends on the platform and solution. These examples uses Qt and the view is set up 
in a :class:`cee::qt::ViewerWidget`.


.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/UnstructGridModel.cpp
    :language: cpp
    :lines: 103,105-107

**See the complete source code here:**

:ref:`two-triangles-example`

