Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert Counterclockwise Orientation of Read Gmsh File #115

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions meshes/square4el.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
5
1 -5 -5 0
2 -5 5 0
3 5 5 0
4 5 -5 0
5 0 0 0
$EndNodes
$Elements
12
1 15 2 0 1 1
2 15 2 0 2 2
3 15 2 0 3 3
4 15 2 0 4 4
5 1 2 0 1 2 3
6 1 2 0 2 3 4
7 1 2 0 3 4 1
8 1 2 0 4 1 2
9 2 2 0 1 1 2 5
10 2 2 0 1 4 1 5
11 2 2 0 1 2 3 5
12 2 2 0 1 3 4 5
$EndElements
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ if(BUILD_TESTING)
osh_add_exe(coarsen_test)
test_func(run_coarsen_test 1 ./coarsen_test)

if(Omega_h_USE_Gmsh)
Fuad-HH marked this conversation as resolved.
Show resolved Hide resolved
osh_add_exe(test_tri_vert_orientation)
test_func(run_tri_orientation 1 ./test_tri_vert_orientation ${CMAKE_SOURCE_DIR}/meshes/square4el.msh)
set(TEST_EXES ${TEST_EXES} test_tri_vert_orientation)
endif()

osh_add_exe(2d_conserve_test)
test_func(serial_2d_conserve 1 ./2d_conserve_test)
Fuad-HH marked this conversation as resolved.
Show resolved Hide resolved
if(Omega_h_USE_MPI)
Expand Down Expand Up @@ -602,6 +608,7 @@ if(BUILD_TESTING)
${CMAKE_SOURCE_DIR}/meshes/box_3d_2p_reduce.vtk
${CMAKE_SOURCE_DIR}/meshes/box3d_2p_adapt.vtk)
endif()

osh_add_exe(shape_test)
test_basefunc(run_shape_test 1 ./shape_test)

Expand Down
57 changes: 57 additions & 0 deletions src/test_tri_vert_orientation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <Omega_h_macros.h>

#include <Omega_h_adj.hpp>
#include <Omega_h_for.hpp>
#include <Omega_h_mesh.hpp>
#include <cstdio>
#include <Omega_h_file.hpp>

bool is_ccw_oriented(Omega_h::Mesh& mesh) {
OMEGA_H_CHECK(mesh.dim() == 2);
bool ccw;
// find mesh orientation: node number orientation of faces
// clockwise or counter clockwise: 1 for clockwise, -1 for counter clockwise
const auto& face2nodes = mesh.ask_down(Omega_h::FACE, Omega_h::VERT).ab2b;
const auto& coords = mesh.coords();
Omega_h::Write<Omega_h::LO> ccw_face(mesh.nfaces(), 0, "ccw_face");

auto find_ccw_face = OMEGA_H_LAMBDA(const Omega_h::LO face) {
const auto faceVerts = Omega_h::gather_verts<3>(face2nodes, face);
const auto faceCoords = Omega_h::gather_vectors<3, 2>(coords, faceVerts);
// calculate using (y2 - y1) * (x3 - x2) - (y3 - y2) * (x2 - x1)
cwsmith marked this conversation as resolved.
Show resolved Hide resolved
const bool cw = (faceCoords[1][1] - faceCoords[0][1]) *
(faceCoords[2][0] - faceCoords[1][0]) -
(faceCoords[2][1] - faceCoords[1][1]) *
(faceCoords[1][0] - faceCoords[0][0]) >
0;
ccw_face[face] = (!cw) ? 1 : -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you use 1 and -1? If I ask the question is_this_counter_clockwise and you tell me -1 it seems less obvious to me than 0 which corresponds to false.

};
Omega_h::parallel_for(mesh.nfaces(), find_ccw_face, "find_ccw_face");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention is to simply answer the question "are all faces ccw?" then this can be written as a max reduction and we can avoid storing ccw_face[face].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this check is going into the Omega_h constructor I agree.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PUMI has a very useful 'verify' function that checks for mesh and model classification issues (I'm not sure of the full set of checks offhand). This function seems like a good first step towards an Omega_h version of that.

auto ccw_check = OMEGA_H_LAMBDA(const Omega_h::LO face) {
// check all the faces are either clockwise or counter clockwise but same
// orientation
OMEGA_H_CHECK((ccw_face[face] == ccw_face[0]) && (ccw_face[face] != 0));
};
Omega_h::parallel_for(mesh.nfaces(), ccw_check, "ccw_check");

auto ccw_check_host = Omega_h::HostRead<Omega_h::LO>(ccw_face);
ccw = ccw_check_host[0] == 1;
printf(
"**** ORIENTATION: %s ****\n", (ccw) ? "COUNTER CLOCKWISE" : "CLOCKWISE");

return ccw;
}

int main(int argc, char** argv) {
Omega_h::Library library(&argc, &argv);
Omega_h::Mesh mesh(&library);
mesh = Omega_h::binary::read(argv[1], library.self());
bool ccw = is_ccw_oriented(mesh);

if (!ccw) {
std::fprintf(stderr, "ERROR: Mesh is not counter clockwise oriented\n");
return 1;
}

return 0;
}