Skip to Content.
Sympa Menu

cgal-discuss - Re: [cgal-discuss] Plotting a mesh: Geom_traits with Triangulation_data_structure_2

Subject: CGAL users discussion list

List archive

Re: [cgal-discuss] Plotting a mesh: Geom_traits with Triangulation_data_structure_2


Chronological Thread 
  • From: Chris Marsh <>
  • To: <>
  • Subject: Re: [cgal-discuss] Plotting a mesh: Geom_traits with Triangulation_data_structure_2
  • Date: Wed, 14 Nov 2018 22:08:44 -0600
  • Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None ; spf=Pass ; spf=Pass
  • Ironport-phdr: 9a23:11TbWBGeH8aoLD4p8nJypp1GYnF86YWxBRYc798ds5kLTJ7yocWwAkXT6L1XgUPTWs2DsrQY07WQ6/iocFdDyK7JiGoFfp1IWk1NouQttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZrKeTpAI7SiNm82/yv95HJbAhEmDiwbaluIBmqsA7cqtQYjYx+J6gr1xDHuGFIe+NYxWNpIVKcgRPx7dqu8ZBg7ipdpesv+9ZPXqvmcas4S6dYDCk9PGAu+MLrrxjDQhCR6XYaT24bjwBHAwnB7BH9Q5fxri73vfdz1SWGIcH7S60/VDK/5KlpVRDokj8KODE38G7VisJ+gqFVrg+/qRNj2IPUZZ2ZOeBkc6/BYd8XR2xMVdtRWSxbBYO8apMCAvQBPeZCron9vEYFoxykCgm2H+PvyiJDi3jq0q06yeQhEBrG0BYlH90UqnTUt9X1ObwPUeCu1qbI1jbDY+lK1jjn8YXFdA0qr/+LXbJ1a8XRyE8vGhvDjlqKsoPqJDeV2foXv2eH6OpgUPqji28hqwFrozig3N0giofTho4NzlDL6zx1z50vKt2iU053e9ikH4VMty2CL4t5WNkiTHt2uCY71LIHt4S3fC8QyJQo3xLfZP2Hc5SI4hL+T+mRPCx4i2x/eL2jgBay91SgxfP6VsmoylpFsi1FktzUun8R2Bzc8MyHRuF6/ke71jaDzxzc6uFeIUA1jqrbLoQuwqQrlpoTq0jMAjX5lUPrh6GVbkUp4vWk5/rkb7n8uJOROYF5hhvgPqgzmsGzG/o0PwYMUmSD5+iwyLnu8Vf6TbhKlPE6j6jUvZHAKckbuKK0BRJe3Jw55BalFTim1cwVnXkZI1JBfxKKl43pNEvPIPD8F/u/nk+sny1wx/DFJLHuHo/CImDCkLfnY7l991ZRxBctwd1c/Z5YFL8MLfzpVkPvqdDVDgU1PxK6zuvlENl905kRWWOLAq+XKqPStlqI6/ozLOmMfo8VuS39J+Ig5/H0l385gl8dcLK03ZsMcn+4BfdmL1+FbnX2mtsBC3sFvhIiTOz2j12PSSJcZ3moUKI4/z00FYOmDZzfSYCwm7yBxzy2HoZWZ2BDElCDC23kd4SCW/cWaSKdONVtkjIeVevpdok6yBv7tBPm06E1aa3P6ygAvNTi0sJ07qvdj1Yp5DlsBoOc1W+KCGp7l2dNSz4t171kuh9BzQLX2qdxh7lUFMdY+uhSegY8L5/VieJgXYPcQAXEK/yJUle9CvCvGyoxBoY6w8UPeAB4Es+5jjjexCOxAPkenurYV9QP7qvA0i2pdI5GwHHc2fx51gh0co50LWSjw5VH2U3WDo/NnV+ekv/yJ7kH1TXOsmyKnzPX4BNoFTVoWKCAZkgxI1PMpI2htF7ZRqStT78uYFMYlJyyb5BSY9istm1oAffuPNOHPjC0kma0QwuTz6iPKoHjKTwQ

Hi,
My apologizes for the slow response. Please find attach a minimum working example to initialize a vtk unstructured mesh from CGAL. It then writes to a vtu you can load into paraview, but you can do whatever you want with this datastructue (eg use with Qt+VTK).
The 2 parameter on l 71-73 and 77 to 79 is the point id, which is really the only bookeeping. I store the vertex id (I start at 0 and increment as I read in vertices) using the vertex_with_info data vertex class. The idea is you just need a way to index into the points array to  say which vertices the triangle cell is using.
e.g., in my code
tri->GetPointIds()->SetId(0, face->vertex(0)->get_id());
tri->GetPointIds()->SetId(1, face->vertex(1)->get_id());
tri->GetPointIds()->SetId(2, face->vertex(2)->get_id());
but really any way of tracking which vertices are where will work. It's just book keeping.

You can then use CellData to embed further information onto the triangles in VTK, so you can display numerical results.
For example, this is a mesh I generate with CGAL to fit topography in a dynamic way, and then I display it in paraview (but that's really just vtk) using cell data to hold, in this case, elevations.
https://raw.githubusercontent.com/Chrismarsh/mesher/master/images/mesh.png

I find homebrew to consistently work. I've had more issues with macports, but I'll admit it's been probably 10 years since I used it. I think the Qt issues are just my inexperience with the library.

>As a consequence, “draw” is not a good option in my case.
You could always build and link against a new CGAL version. It's pretty easy to compile.

Cheers

Chris Marsh
PhD Candidate
chrismarsh.ca

On Tue, 13 Nov 2018 at 19:30, Yoann LE BARS <> wrote:

Hello everybody out there!

        Thank you for your answer, Chris, and my apologies to have sent this
message to you alone rather to the list. I repost it.

Le 13/11/2018 à 17:31, Chris Marsh a écrit :
> I've never used the Qt functionality of CGAL, but I think you need to
> use just CGAL::draw(tri), as per
> https://doc.cgal.org/latest/Triangulation_2/Triangulation_2_2draw_triangulation_2_8cpp-example.html

        Well, it turned out CGAL version on Ubuntu has not been compiled with
“draw”. As it is one of the systems I have to support, I cannot use it.
Anyway, I am working on an application with an interface made with Qt,
therefore the visualisation should be embedded in the interface and I
have to use Qt. As a consequence, “draw” is not a good option in my case.

> I'm on MacOS and my Qt library through brew causes me nothing but
> problems, so it might be related to that.

        This is not the question, but I do not really understand why mac people
tend to prefer Homebrew to Macport. My tests show you have better
control with Macport, it is more coherent with a Unix system and you can
solve things more easily and more efficiently when something goes wrong.
Also, there are much more applications available in Macports.

        Anyway …

> Everything I've read over the years suggests the viz in cgal is for
> quick and dirty visualization.
> e.g.,
> http://cgal-discuss.949826.n4.nabble.com/Tutorial-for-visualisation-in-CGAL-using-QT-td4656057.html

        Then, it turns out I should use CGAL only for mesh manipulation and
computation, then something else for visualisation.

> Depending on what your needs are, you might want to consider writing to
> a format that is easily visualized in another software. If you're
> interested in visualizing simulation output, might I suggest writing to
> a Paraview (paraview.org <http://paraview.org>) format (vtu)? This
> approach has treated me very well. It's pretty straightforward format to
> write to (VTK library does the hard part), and Paraview is immensely
> useful for visualizing simulation outputs  and exploring a mesh. I can
> send you a paraview file that is an exported CGAL mesh if you're interested.

        Using Paraview does not seem a good option to me, as I need to do
several treatments either on my meshes or using my meshes.

        Paraview uses Vtk library, which can be embedded in Qt applications. It
may be a good way to do visualisation I need to do.

        However, I am interested in the code you use for visualisation with
Paraview.

        Moreover, I am interested on any information to use CGAL with Vtk.

        Best regards.

--
Yoann LE BARS
http://le-bars.net/yoann/
Diaspora* :

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://sympa.inria.fr/sympa/info/cgal-discuss


cmake_minimum_required (VERSION 3.11)
project (tri)
find_package(CGAL REQUIRED)
include(${CGAL_USE_FILE})


find_package(VTK REQUIRED HINTS
"/home/chris/Documents/PhD/code/CHM/cmake-build-release/lib/VTK/lib/cmake/vtk-8.1")
include(${VTK_USE_FILE})

include_directories(
${CGAL_INCLUDE_DIRS}
${CGAL_3RD_PARTY_INCLUDE_DIRS})

add_executable(main
main.cpp)

target_link_libraries(main ${CGAL_3RD_PARTY_LIBRARIES} ${VTK_LIBRARIES})
#include <CGAL/Triangulation_2.h>
#include <CGAL/Projection_traits_xy_3.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_ds_face_base_2.h>

#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkFloatArray.h>

#include <iostream>

int main()
{
	typedef CGAL::Exact_predicates_inexact_constructions_kernel K;  
	typedef K::Point_3 Point_3;
	typedef CGAL::Projection_traits_xy_3<K> Gt; //allows for using 2D algorithms on the 3D points (good for terrain)
	typedef CGAL::Triangulation_vertex_base_2<Gt> Vb; 
	typedef CGAL::Constrained_triangulation_face_base_2<Gt> Fb; 
	typedef CGAL::Triangulation_data_structure_2<Vb, Fb> triangulation;

	triangulation tri;
	//read in x,y,z from a file here
	//create a new vertex and insert into a 2D triangulation

	//vertex 0
	double x,y,z;
	x=0;
    y=0, 
    z=0;
	Point_3 pt0( x,y,z);
	triangulation::Vertex_handle Vh0 = tri.create_vertex();
	Vh0->set_point(pt0);

	//vertex 1
	x = 1000;
	y = 0;
	z=0;
	Point_3 pt1( x,y,z);
	triangulation::Vertex_handle Vh1 = tri.create_vertex();
	Vh1->set_point(pt1);

	//vertex 2
	x = 1000;
	y = 1000;
	z=0;
	Point_3 pt2( x,y,z);
	triangulation::Vertex_handle Vh2= tri.create_vertex();
	Vh2->set_point(pt2);

	triangulation::Face_handle face = tri.create_face(Vh0,Vh1,Vh2);
	Vh0->set_face(face);
	Vh1->set_face(face);
	Vh2->set_face(face);

	std::cout << "#vetex=" << tri.number_of_vertices() << std::endl;
	std::cout << "#faces=" << tri.faces().size() << std::endl;

	vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->SetNumberOfPoints(3);

    vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
    triangles->Allocate(1);
	
	vtkSmartPointer<vtkTriangle> triface = vtkSmartPointer<vtkTriangle>::New();

	//create triangles
	triface->GetPointIds()->SetId(0, 0); // the second parameter is effectively an id 
	triface->GetPointIds()->SetId(1, 1); // needs to be the same as the the points id below
    triface->GetPointIds()->SetId(2, 2);

    // insert the points into the mesh
    // first parameter needs to be the same id as above
    points->SetPoint(0, face->vertex(0)->point().x(), face->vertex(0)->point().y(), face->vertex(0)->point().z());
    points->SetPoint(1, face->vertex(1)->point().x(), face->vertex(1)->point().y(), face->vertex(1)->point().z());
    points->SetPoint(2, face->vertex(2)->point().x(), face->vertex(2)->point().y(), face->vertex(2)->point().z());

    triangles->InsertNextCell(triface);

    vtkSmartPointer<vtkUnstructuredGrid> _vtk_unstructuredGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
    _vtk_unstructuredGrid->SetPoints(points);
    _vtk_unstructuredGrid->SetCells(VTK_TRIANGLE, triangles);

    vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer = vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
    writer->SetFileName("out.vtu");

    writer->SetInputData(_vtk_unstructuredGrid);
    writer->Write();

}



Attachment: out.vtu
Description: model/vnd.vtu




Archive powered by MHonArc 2.6.18.

Top of Page