2015-09-22 17:40:01 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
|
|
|
// 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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2015-09-22 17:40:01 +00:00
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
|
|
|
// Copyright 2014 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2015-09-22 17:40:01 +00:00
|
|
|
// the U.S. Government retains certain rights in this software.
|
|
|
|
//
|
|
|
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
|
|
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
|
|
|
// this software.
|
|
|
|
//============================================================================
|
|
|
|
|
2015-11-02 21:22:35 +00:00
|
|
|
#ifndef VTKM_DEVICE_ADAPTER
|
|
|
|
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
|
|
|
|
#endif
|
|
|
|
|
2015-09-22 17:40:01 +00:00
|
|
|
#include <vtkm/Math.h>
|
|
|
|
#include <vtkm/cont/DataSet.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/filter/Triangulate.h>
|
|
|
|
#include <vtkm/worklet/DispatcherMapField.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
|
|
|
|
|
2017-08-10 17:46:17 +00:00
|
|
|
using DeviceAdapter = VTKM_DEFAULT_DEVICE_ADAPTER_TAG;
|
2015-09-22 17:40:01 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Point location of vertices from a CastAndCall but needs a static cast eventually
|
2017-05-18 14:29:41 +00:00
|
|
|
static vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float64, 3>> vertexArray;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-02 21:22:35 +00:00
|
|
|
//
|
2015-09-22 17:40:01 +00:00
|
|
|
// Functor to retrieve vertex locations from the CoordinateSystem
|
|
|
|
// Actually need a static cast to ArrayHandle from DynamicArrayHandleCoordinateSystem
|
|
|
|
// but haven't been able to figure out what that is
|
|
|
|
//
|
|
|
|
struct GetVertexArray
|
|
|
|
{
|
|
|
|
template <typename ArrayHandleType>
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_CONT void operator()(ArrayHandleType array) const
|
2015-09-22 17:40:01 +00:00
|
|
|
{
|
|
|
|
this->GetVertexPortal(array.GetPortalConstControl());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename PortalType>
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_CONT void GetVertexPortal(const PortalType& portal) const
|
2015-09-22 17:40:01 +00:00
|
|
|
{
|
|
|
|
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
|
|
|
|
{
|
|
|
|
vertexArray.GetPortalControl().Set(index, portal.Get(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
// Need the actual vertex points from a static cast of the dynamic array but can't get it right
|
|
|
|
// So use cast and call on a functor that stores that dynamic array into static array we created
|
2017-03-27 23:36:33 +00:00
|
|
|
vertexArray.Allocate(cellSet.GetNumberOfPoints());
|
|
|
|
vtkm::cont::CastAndCall(triDataSet.GetCoordinateSystem(), GetVertexArray());
|
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[])
|
|
|
|
{
|
|
|
|
std::cout << "TrianguleUniformGrid Example" << std::endl;
|
|
|
|
std::cout << "Parameters are [xdim ydim [# of cellsToDisplay]]" << std::endl << std::endl;
|
2015-11-02 21:22:35 +00:00
|
|
|
|
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;
|
2017-08-10 17:56:19 +00:00
|
|
|
vtkm::filter::Result result = triangulate.Execute(inDataSet);
|
2015-09-22 17:40:01 +00:00
|
|
|
|
2017-03-27 23:36:33 +00:00
|
|
|
triDataSet = result.GetDataSet();
|
|
|
|
|
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();
|
2017-02-14 22:37:58 +00:00
|
|
|
vertexArray.ReleaseResources();
|
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
|