# vtkHyperTreeGrid Paraview example in 2D

**URL:** <https://discourse.paraview.org/t/vtkhypertreegrid-paraview-example-in-2d/4122>\
**Category:** ParaView Support\
**Created:** [April 20, 2020, 12:49pm UTC](https://discourse.paraview.org/t/vtkhypertreegrid-paraview-example-in-2d/4122 "2020-04-20T12:49:24Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![Yohann\_Bearzi](https://discourse.paraview.org/user_avatar/discourse.paraview.org/yohann_bearzi/32/1894_2.png) [@Yohann\_Bearzi](https://discourse.paraview.org/u/Yohann_Bearzi)\
**Post date:** [June 17, 2020, 2:51pm UTC](https://discourse.paraview.org/t/vtkhypertreegrid-paraview-example-in-2d/4122/12 "2020-06-17T14:51:39Z")

</div>

The paraview example is indeed out of date, that will be updated.

There are 2 main ways you could create your htg.

- You can do it “by hand”. Let me show an example in c++ (don’t forget to include relevant headers)

```auto
vtkNew<vtkHyperTreeGrid> htg;
/*
 * Initialize it like you did with x, y, and z coordinates
 */
vtkIdType treeOffset = 0;
for (vtkIdType i = 0; i < htg->GetCellDims()[0]; ++i)
{
  for (vtkIdType j = 0; j < htg->GetCellDims()[1]; ++j)
  {
     // We go through every trees in the grid. It is a 2D grid so there are 2 loop.
     // Let's initialize k = 0 for completeness.
    vtkIdType k = 0, treeId;
    // Get the tree index.
    htg->GetIndexFromLevelZeroCoordinates(treeId, i, j, k);
    // Create a cursor (there are many kinds, we can go through them later if you have questions)
    auto cursor = vtkSmartPointer<vtkHyperTreeGridNonOrientedCursor>::Take(
        htg->NewNonOrientedCursor(treeId, true /* create */);
    // Set tree indexing offset
    cursor->GetTree()->SetGlobalIndexStart(treeOffset);
    // You have to write your own procedure here, I assume you wrote the following
    // recursive function
    RecursivelySubdivideLeaves(cursor, paramsDrivingSubdivision);
    treeOffset += cursor->GetTree()->GetNumberOfVertices(); // we update the offset.
  }
}

```

Then, to subdivide a leave, you need to call `cursor->SubdivideLeaf()`. You have access of the global htg level index in the scalar fields with `cursor->GetGlobalIndex()`, and index relative to one tree with `cursor->GetVertexId()`. To navigate, you have `cursor->GetNumberOfChildren()` telling you how many children you have, `cursor->ToChild(iChild)` and `cursor->ToParent()` to walk inside the tree, and `cursor->IsLeaf()` to know when to stop.

- You can also use `vtkHyperTreeGridSource`, which will create the structure of a htg given a descriptor. You can use it to create a htg pretty fast. The descriptor works as follow: `.` means “leaf”, `R` means subdivide, `|` means `next level`. An htg of 4 quadtrees like you showed in your first comment would be `....`. If you want to subdivide the 4th tree, you would write `...R|....`. If you want to subdivie the 2nd and the 4th tree, you would write `.R.R|........`. Finally, if you want to additionally subdivide the first child of the 4th tree, you would write `.R.R|....R...|....`.

Now, concerning writers, there might be a bug that went through the release. To make sure this works, you should call `writer->SetDataSetMajorVersion(0)` before your write. You don’t need to specify this parameter for the reader.

Did I answer your question or are there still some dark corners? Don’t hesitate to ask for clarification if something isn’t clear.

Thanks,  
Yohann

---

_[View the full topic](https://discourse.paraview.org/t/vtkhypertreegrid-paraview-example-in-2d/4122)._
