# 3D Visualization

**URL:** https://discourse.paraview.org/t/3d-visualization/9356
**Category:** ParaView Support
**Tags:** vtk
**Created:** [April 5, 2022, 7:02am UTC](https://discourse.paraview.org/t/3d-visualization/9356 "2022-04-05T07:02:19Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![uog](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/u/c0e974/32.png) [@uog](https://discourse.paraview.org/u/uog)
#### Post date: [April 5, 2022, 7:02am UTC](https://discourse.paraview.org/t/3d-visualization/9356/1 "2022-04-05T07:02:19Z")

</div>

Hello everyone,  
I am new in paraview and working on CFD. I want to extract data from C++ and visualize in paraview. For this I need some code samples which extract data from C++ in the .vtk file.

Does anyone have some experience in this, please share or any other possible suggestions for how to extract data from C++ to paraview for visualization?

Thank you so much in advance!

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [April 5, 2022, 8:31am UTC](https://discourse.paraview.org/t/3d-visualization/9356/2 "2022-04-05T08:31:30Z")

</div>

Hi @uog ,

Use a vtkWriter ?

---

<div class="post-metadata">

### Author: ![uog](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/u/c0e974/32.png) [@uog](https://discourse.paraview.org/u/uog)
#### Post date: [April 5, 2022, 8:55am UTC](https://discourse.paraview.org/t/3d-visualization/9356/3 "2022-04-05T08:55:20Z")

</div>

Hi Mathieu Westphal,

Thank you so much. Can you share with me information about vtkWriter?

I am using the code which works for 2D but not 3D, can you check this?

```auto

Code is like this:
void Mesh::Write_ascii_paraview(std::string const &fname, std::vector<double> const &v) const
{
assert(Nnodes() == static_cast<int>(v.size())); // fits vector length to mesh information?

ofstream fout(fname); // open file ASCII mode
if ( !fout.is_open() )
{
cout << "\nFile " << fname << " has not been opened.\n\n" ;
assert( fout.is_open() && "File not opened." );
}

string const DELIMETER(" "); // define the same delimiter as in matlab/ascii_read*.m
//int const OFFSET(o); // C-indexing in output

fout << "# vtk DataFile Version 2.0" << endl;
fout << "HEAT EQUATION" << endl;
fout << "ASCII" << endl;
fout << "DATASET POLYDATA" << endl;
fout << "POINTS "<< v.size()<<" float"<<endl;

assert( Nnodes()*Ndims() == static_cast<int>(_xc.size()));
for (int k = 0, kj = 0; k < Nnodes(); ++k)
{
for (int j = 0; j < Ndims(); ++j, ++kj)
{
fout << _xc[kj] << DELIMETER;
}

fout << v[k] << endl;
}

fout << "POLYGONS "<< Nelems() << ' ' << Nelems()*4 << endl;

assert( Nelems()*NverticesElements() == static_cast<int>(_ia.size()));
for (int k = 0, kj = 0; k < Nelems(); ++k)
{
fout << 4 << DELIMETER; // triangular patches
for (int j = 0; j < NverticesElements()-1; ++j, ++kj)
{
fout << _ia[kj] << DELIMETER;
}

fout << _ia[kj];
kj=kj+1;

if(k<Nelems()-1)
{
fout << endl;
}
}

fout.close();
return;
}

void Mesh::Visualize_paraview(vector<double> const &v) const
{
//const string exec_m("open -a paraview"); // paraview
const string exec_m("paraview"); // paraview

const string fname("uv.vtk");
Write_ascii_paraview(fname, v);

int ierror = system(exec_m.c_str()); // call external command

if (ierror != 0)
{
cout << endl << "Check path to paraview on your system" << endl;
}
cout << endl;
return;
}

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [April 5, 2022, 9:09am UTC](https://discourse.paraview.org/t/3d-visualization/9356/4 "2022-04-05T09:09:05Z")

</div>

I’m suggesting to use vtk directly to write the file. You are writing the file manually, this is not the same thing.

---

<div class="post-metadata">

### Author: ![aron.helser](https://discourse.paraview.org/user_avatar/discourse.paraview.org/aron.helser/32/13_2.png) [@aron.helser](https://discourse.paraview.org/u/aron.helser)
#### Post date: [April 5, 2022, 12:00pm UTC](https://discourse.paraview.org/t/3d-visualization/9356/5 "2022-04-05T12:00:09Z")

</div>

I suggest looking at the vtk examples site: [https://kitware.github.io/vtk-examples/site/](https://kitware.github.io/vtk-examples/site/)  
In your case, you could combine the VTP writer example, [https://kitware.github.io/vtk-examples/site/Cxx/IO/WriteVTP/](https://kitware.github.io/vtk-examples/site/Cxx/IO/WriteVTP/)  
with one that sets point data on a polydata, like [https://kitware.github.io/vtk-examples/site/Cxx/VTKConcepts/Scalars/](https://kitware.github.io/vtk-examples/site/Cxx/VTKConcepts/Scalars/)  
If you look at the bottom of the example, it shows how to include and link vtk to your program using CMake.  
HTH,  
Aron
