2015-09-22 17:40:01 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2015-09-22 17:40:01 +00:00
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/Math.h>
|
|
|
|
#include <vtkm/cont/DataSet.h>
|
2019-04-09 13:52:53 +00:00
|
|
|
#include <vtkm/cont/Initialize.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/filter/Triangulate.h>
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
2015-10-08 17:45:50 +00:00
|
|
|
//Suppress warnings about glut being deprecated on OSX
|
2016-01-12 14:00:07 +00:00
|
|
|
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
|
2017-05-18 14:29:41 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
2015-10-08 17:45:50 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <GLUT/glut.h>
|
2015-09-22 17:40:01 +00:00
|
|
|
#else
|
2017-05-18 14:29:41 +00:00
|
|
|
#include <GL/glut.h>
|
2015-09-22 17:40:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Default size of the example
|
2017-05-18 14:29:41 +00:00
|
|
|
static vtkm::Id2 dims(4, 4);
|
2017-02-14 22:37:58 +00:00
|
|
|
static vtkm::Id cellsToDisplay = 16;
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// Takes input uniform grid and outputs unstructured grid of triangles
|
2017-03-27 23:36:33 +00:00
|
|
|
static vtkm::cont::DataSet triDataSet;
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Construct an input data set with uniform grid of indicated dimensions, origin and spacing
|
|
|
|
//
|
2015-09-28 22:23:03 +00:00
|
|
|
vtkm::cont::DataSet MakeTriangulateTestDataSet(vtkm::Id2 dim)
|
2015-09-22 17:40:01 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::DataSet dataSet;
|
|
|
|
|
|
|
|
// Place uniform grid on a set physical space so OpenGL drawing is easier
|
2015-09-28 22:23:03 +00:00
|
|
|
const vtkm::Id3 vdims(dim[0] + 1, dim[1] + 1, 1);
|
2015-09-22 17:40:01 +00:00
|
|
|
const vtkm::Vec<vtkm::Float32, 3> origin = vtkm::make_Vec(0.0f, 0.0f, 0.0f);
|
2015-09-28 22:23:03 +00:00
|
|
|
const vtkm::Vec<vtkm::Float32, 3> spacing = vtkm::make_Vec(
|
2017-05-18 14:29:41 +00:00
|
|
|
1.0f / static_cast<vtkm::Float32>(dim[0]), 1.0f / static_cast<vtkm::Float32>(dim[1]), 0.0f);
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// Generate coordinate system
|
|
|
|
vtkm::cont::ArrayHandleUniformPointCoordinates coordinates(vdims, origin, spacing);
|
2017-05-18 14:29:41 +00:00
|
|
|
dataSet.AddCoordinateSystem(vtkm::cont::CoordinateSystem("coordinates", coordinates));
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// Generate cell set
|
2015-11-03 14:10:39 +00:00
|
|
|
vtkm::cont::CellSetStructured<2> cellSet("cells");
|
2015-09-28 22:23:03 +00:00
|
|
|
cellSet.SetPointDimensions(vtkm::make_Vec(dim[0] + 1, dim[1] + 1));
|
2015-09-22 17:40:01 +00:00
|
|
|
dataSet.AddCellSet(cellSet);
|
|
|
|
|
|
|
|
return dataSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Initialize the OpenGL state
|
|
|
|
//
|
|
|
|
void initializeGL()
|
|
|
|
{
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
glOrtho(-0.5f, 1.5f, -0.5f, 1.5f, -1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Render the output using simple OpenGL
|
|
|
|
//
|
|
|
|
void displayCall()
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glLineWidth(3.0f);
|
|
|
|
|
2015-09-28 22:23:03 +00:00
|
|
|
// Get the cellset, coordinate system and coordinate data
|
2016-01-14 19:01:05 +00:00
|
|
|
vtkm::cont::CellSetSingleType<> cellSet;
|
2017-03-27 23:36:33 +00:00
|
|
|
triDataSet.GetCellSet(0).CopyTo(cellSet);
|
2015-09-22 17:40:01 +00:00
|
|
|
|
2017-12-12 19:26:23 +00:00
|
|
|
auto vertexArray = triDataSet.GetCoordinateSystem().GetData();
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// Draw the two triangles belonging to each quad
|
|
|
|
vtkm::Id triangle = 0;
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Float32 color[4][3] = {
|
|
|
|
{ 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }
|
2015-09-28 22:23:03 +00:00
|
|
|
};
|
2015-09-22 17:40:01 +00:00
|
|
|
|
2015-09-28 22:23:03 +00:00
|
|
|
for (vtkm::Id quad = 0; quad < cellsToDisplay; quad++)
|
|
|
|
{
|
|
|
|
for (vtkm::Id j = 0; j < 2; j++)
|
|
|
|
{
|
2015-09-22 17:40:01 +00:00
|
|
|
vtkm::Id indx = triangle % 4;
|
|
|
|
glColor3f(color[indx][0], color[indx][1], color[indx][2]);
|
|
|
|
|
|
|
|
// Get the indices of the vertices that make up this triangle
|
|
|
|
vtkm::Vec<vtkm::Id, 3> triIndices;
|
|
|
|
cellSet.GetIndices(triangle, triIndices);
|
|
|
|
|
|
|
|
// Get the vertex points for this triangle
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Vec<vtkm::Float64, 3> pt0 = vertexArray.GetPortalConstControl().Get(triIndices[0]);
|
|
|
|
vtkm::Vec<vtkm::Float64, 3> pt1 = vertexArray.GetPortalConstControl().Get(triIndices[1]);
|
|
|
|
vtkm::Vec<vtkm::Float64, 3> pt2 = vertexArray.GetPortalConstControl().Get(triIndices[2]);
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// Draw the triangle filled with alternating colors
|
2017-05-18 14:29:41 +00:00
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
2015-09-22 17:40:01 +00:00
|
|
|
glBegin(GL_TRIANGLES);
|
2017-05-18 14:29:41 +00:00
|
|
|
glVertex3d(pt0[0], pt0[1], pt0[2]);
|
|
|
|
glVertex3d(pt1[0], pt1[1], pt1[2]);
|
|
|
|
glVertex3d(pt2[0], pt2[1], pt2[2]);
|
2015-09-22 17:40:01 +00:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
triangle++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glFlush();
|
|
|
|
}
|
|
|
|
|
2017-03-27 23:36:33 +00:00
|
|
|
// Triangulate and render uniform grid example
|
2015-09-22 17:40:01 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2019-04-09 13:52:53 +00:00
|
|
|
// Process vtk-m general args
|
|
|
|
auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice;
|
|
|
|
auto config = vtkm::cont::Initialize(argc, argv, opts);
|
2015-11-02 21:22:35 +00:00
|
|
|
|
2019-04-09 13:52:53 +00:00
|
|
|
std::cout << "TrianguleUniformGrid Example" << std::endl;
|
|
|
|
std::cout << "Parameters are [options] [xdim ydim [# of cellsToDisplay]]" << std::endl;
|
|
|
|
std::cout << config.Usage << std::endl << std::endl;
|
2015-09-22 17:40:01 +00:00
|
|
|
// Set the problem size and number of cells to display from command line
|
2015-09-28 22:23:03 +00:00
|
|
|
if (argc >= 3)
|
|
|
|
{
|
2015-09-22 17:40:01 +00:00
|
|
|
dims[0] = atoi(argv[1]);
|
|
|
|
dims[1] = atoi(argv[2]);
|
|
|
|
cellsToDisplay = dims[0] * dims[1];
|
|
|
|
}
|
2015-09-28 22:23:03 +00:00
|
|
|
if (argc == 4)
|
|
|
|
{
|
2015-09-22 17:40:01 +00:00
|
|
|
cellsToDisplay = atoi(argv[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the input uniform cell set
|
|
|
|
vtkm::cont::DataSet inDataSet = MakeTriangulateTestDataSet(dims);
|
|
|
|
|
2017-03-27 23:36:33 +00:00
|
|
|
// Convert uniform quad to triangle
|
2017-03-27 22:47:44 +00:00
|
|
|
vtkm::filter::Triangulate triangulate;
|
2018-03-13 15:31:24 +00:00
|
|
|
triDataSet = triangulate.Execute(inDataSet);
|
2017-03-27 23:36:33 +00:00
|
|
|
|
2015-09-22 17:40:01 +00:00
|
|
|
// Render the output dataset of tets
|
|
|
|
glutInit(&argc, argv);
|
|
|
|
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
|
|
|
|
glutInitWindowSize(1000, 1000);
|
|
|
|
glutInitWindowPosition(100, 100);
|
|
|
|
|
|
|
|
glutCreateWindow("VTK-m Uniform Triangulate");
|
|
|
|
|
|
|
|
initializeGL();
|
|
|
|
|
|
|
|
glutDisplayFunc(displayCall);
|
|
|
|
glutMainLoop();
|
|
|
|
|
2017-03-27 23:36:33 +00:00
|
|
|
triDataSet.Clear();
|
2015-09-22 17:40:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-08 17:45:50 +00:00
|
|
|
|
2016-01-12 14:00:07 +00:00
|
|
|
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
|
2017-05-18 14:29:41 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2015-10-08 17:45:50 +00:00
|
|
|
#endif
|