.. _geometry-primitives-tutorial:

###########################################################
Geometry: Create a Geometry Model with Geometric Primitives
###########################################################


.. image:: ../images/tut_geometry_primitives.png
    :height: 300
    :align: center
    
This tutorial shows how to create a geometry with geometric primitives like spheres, boxes and cylinders.

A geometry model has a part data generator that can be used to tessellate various geometric primitives.

In this tutorial you will create your own geometry model parts and apply some part settings.

Create the following parts:

-   A box
-   A sphere
-   A cylinder

The box should be colored yellow.

.. note::
    This tutorial 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 the model**

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 44

**Add the parts**

The :class:`DataGenerator <cee::geo::DataGenerator>` can tessellate a box and return it as 
:class:`DataIndexedTriangles <cee::ug::DataIndexedTriangles>`. This is fed into the constructor of the 
:class:`Part <cee::geo::Part>`. 

Add a box part. The box is defined by the center point and the full extent of the box. Then add it to the geometry model.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 48-49

Add the sphere part. The sphere is defined the center position, the radius of the sphere and the number of subdivisions. 
Then add it to the geometry model.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 52-53

Add the cylinder part. The cylinder is defined by the bottom center position, outer and inner radius (no inner radius in 
this example so it is a full cylinder), the direction of the cylinder and the length (height) and finally the number of 
sub divisions, which controls the resolution of the tessellation. Then add it to the geometry model.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 56-57

**Part settings**

We use the effect system of the geometry model to control how the parts are visualized. To set the color of the part, 
use the :class:`PartEffectColor <cee::geo::PartEffectColor>`. Below we set the color of the box and cylinder part. The 
sphere part is not set and will have the default color as there are no color effects defined on that part.

.. literalinclude:: ../../../../EnvisionDesktop/Tutorials/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 60-61

**Set up the created model**

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/GeometryModelWithPrimitives.cpp
    :language: cpp
    :lines: 66


**See the complete source code here:**

:ref:`geometry-primitives-example`
