8000 Merge pull request #4444 from ianthomas23/4437_qhull_triangle_orienta… · matplotlib/matplotlib@124472d · GitHub
[go: up one dir, main page]

Skip to content

Commit 124472d

Browse files
committed
Merge pull request #4444 from ianthomas23/4437_qhull_triangle_orientation
FIX: Do not correct orientation of triangles returned by Qhull
2 parents 2e445c2 + 44a0206 commit 124472d

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

lib/matplotlib/tests/test_triangulation.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,29 @@ def test_trirefiner_fortran_contiguous_triangles():
985985
assert_array_equal(fine_triang1.triangles, fine_triang2.triangles)
986986

987987

988+
def test_qhull_triangle_orientation():
989+
# github issue 4437.
990+
xi = np.linspace(-2, 2, 100)
991+
x, y = map(np.ravel, np.meshgrid(xi, xi))
992+
w = np.logical_and(x > y - 1, np.logical_and(x < -1.95, y > -1.2))
993+
x, y = x[w], y[w]
994+
theta = np.radians(25)
995+
x1 = x*np.cos(theta) - y*np.sin(theta)
996+
y1 = x*np.sin(theta) + y*np.cos(theta)
997+
998+
# Calculate Delaunay triangulation using Qhull.
999+
triang = mtri.Triangulation(x1, y1)
1000+
1001+
# Neighbors returned by Qhull.
1002+ qhull_neighbors = triang.neighbors
1003+
1004+
# Obtain neighbors using own C++ calculation.
1005+
triang._neighbors = None
1006+
own_neighbors = triang.neighbors
1007+
1008+
assert_array_equal(qhull_neighbors, own_neighbors)
1009+
1010+
9881011
if __name__ == '__main__':
9891012
import nose
9901013
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tri/_tri.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ Triangulation::Triangulation(PyArrayObject* x,
224224
PyArrayObject* triangles,
225225
PyArrayObject* mask,
226226
PyArrayObject* edges,
227-
PyArrayObject* neighbors)
227+
PyArrayObject* neighbors,
228+
int correct_triangle_orientations)
228229
: _npoints(PyArray_DIM(x,0)),
229230
_ntri(PyArray_DIM(triangles,0)),
230231
_x(x),
@@ -235,7 +236,8 @@ Triangulation::Triangulation(PyArrayObject* x,
235236
_neighbors(neighbors)
236237
{
237238
_VERBOSE("Triangulation::Triangulation");
238-
correct_triangles();
239+
if (correct_triangle_orientations)
240+
correct_triangles();
239241
}
240242

241243
Triangulation::~Triangulation()
@@ -2230,7 +2232,7 @@ TriModule::TriModule()
22302232
Py::Object TriModule::new_triangulation(const Py::Tuple &args)
22312233
{
22322234
_VERBOSE("TriModule::new_triangulation");
2233-
args.verify_length(6);
2235+
args.verify_length(7);
22342236

22352237
// x and y.
22362238
PyArrayObject* x = (PyArrayObject*)PyArray_ContiguousFromObject(
@@ -2305,7 +2307 8000 ,10 @@ Py::Object TriModule::new_triangulation(const Py::Tuple &args)
23052307
}
23062308
}
23072309

2308-
return Py::asObject(new Triangulation(x, y, triangles, mask, edges, neighbors));
2310+
int correct_triangle_orientations = Py::Int(args[6]);
2311+
2312+
return Py::asObject(new Triangulation(x, y, triangles, mask, edges, neighbors,
2313+
correct_triangle_orientations));
23092314
}
23102315

23112316
Py::Object TriModule::new_tricontourgenerator(const Py::Tuple &args)

lib/matplotlib/tri/_tri.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,17 @@ class Triangulation : public Py::PythonExtension<Triangulation>
180180
* once.
181181
* neighbors: Optional int array of shape (ntri,3) indicating which
182182
* triangles are the neighbors of which TriEdges, or -1 if
183-
* there is no such neighbor. */
183+
* there is no such neighbor.
184+
* correct_triangle_orientations: Whether or not should correct triangle
185+
* orientations so that vertices are
186+
* ordered anticlockwise. */
184187
Triangulation(PyArrayObject* x,
185188
PyArrayObject* y,
186189
PyArrayObject* triangles,
187190
PyArrayObject* mask,
188191
PyArrayObject* edges,
189-
PyArrayObject* neighbors);
192+
PyArrayObject* neighbors,
193+
int correct_triangle_orientations);
190194

191195
virtual ~Triangulation();
192196

lib/matplotlib/tri/triangulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get_cpp_triangulation(self):
107107
if self._cpp_triangulation is None:
108108
self._cpp_triangulation = _tri.Triangulation(
109109
self.x, self.y, self.triangles, self.mask, self._edges,
110-
self._neighbors)
110+
self._neighbors, not self.is_delaunay)
111111
return self._cpp_triangulation
112112

113113
def get_masked_triangles(self):

0 commit comments

Comments
 (0)
0