<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi Ian,</p>
    <p>Thank you for your reply.</p>
    <p>The modification you provided correctly finds the zero area
      element, and masks it from the triangulation. In the example from
      the previous post, masking the zero area element works.</p>
    <p>When I try and make a slightly different triangulation (see
      below), and try to mask the zero area elements, I still get an
      invalid triangulation. I am using v1.4.2. Do you have a sense as
      to what could be going on here?</p>
    <p>Thanks,<br>
    </p>
    <p>Pat</p>
    <p>import matplotlib.tri as mtri<br>
      import numpy as np<br>
      <br>
      # manually construct an invalid triangulation<br>
      x = np.array([0.0, 1.0, 1.0, 1.0, 2.0])<br>
      y = np.array([1.0, 0.0, 2.0, 1.0, 1.0])<br>
      z = np.zeros(5)<br>
      <br>
      # slightly modified from what I originally posted<br>
      triangles = np.array( [[0, 1, 4], [2, 3, 4], [0, 3, 2], [0, 4,
      3]])<br>
      <br>
      # create a Matplotlib Triangulation<br>
      triang = mtri.Triangulation(x,y,triangles)<br>
      <br>
      # ---------- start of new code ----------<br>
      xy = np.dstack((triang.x[triang.triangles],
      triang.y[triang.triangles])) #shape (ntri,3,2)<br>
      twice_area = np.cross(xy[:,1,:] - xy[:,0,:], xy[:,2,:] -
      xy[:,0,:])  # shape (ntri)<br>
      mask = twice_area < 1e-10  # shape (ntri)<br>
      <br>
      if np.any(mask):<br>
          triang.set_mask(mask)<br>
      # ---------- end of new code ----------<br>
      <br>
      # to perform the linear interpolation<br>
      interpolator = mtri.LinearTriInterpolator(triang, z)<br>
      m_z = interpolator(1.0, 1.0)<br>
    </p>
    <div class="moz-cite-prefix">On 11/21/2016 03:47 AM, Ian Thomas
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAH53=NYQnxNofi-tkJ7v6aHcZrQwyins3nE3=RkgYK2WNoQu-g@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>
          <div>
            <div>Hello Pat,<br>
              <br>
            </div>
            The solution is to use the function Triangulation.set_mask()
            to mask out the zero-area triangles.  The masked-out
            triangles will be ignored in subsequent calls to
            LinearTriInterpolator, tricontourf, etc.  For example:<br>
            <br>
            # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-<br>
            import matplotlib.tri as mtri<br>
            import numpy as np<br>
            <br>
            # manually construct an invalid triangulation having a zero
            area element<br>
            x = np.array([0.0, 1.0, 1.0, 1.0, 2.0])<br>
            y = np.array([1.0, 0.0, 2.0, 1.0, 1.0])<br>
            z = np.zeros(5)<br>
            <br>
            triangles = np.array( [[0, 1, 2], [1, 3, 2], [1, 4, 2], [0,
            4, 1]])<br>
            <br>
            # create a Matplotlib Triangulation<br>
            triang = mtri.Triangulation(x,y,triangles)<br>
            <br>
            # ---------- start of new code ----------<br>
            xy = np.dstack((triang.x[triang.triangles],
            triang.y[triang.triangles]))  # shape (ntri,3,2)<br>
            twice_area = np.cross(xy[:,1,:] - xy[:,0,:], xy[:,2,:] -
            xy[:,0,:])  # shape (ntri)<br>
            mask = twice_area < 1e-10  # shape (ntri)<br>
            <br>
            if np.any(mask):<br>
                triang.set_mask(mask)<br>
            # ---------- end of new code ----------<br>
            <br>
            # to perform the linear interpolation<br>
            interpolator = mtri.LinearTriInterpolator(triang, z)<br>
            m_z = interpolator(1.0, 1.0)<br>
            # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-<br>
            <br>
          </div>
          Note that I have used a small positive number to test the
          triangle areas against rather than zero.  This is to avoid
          problems with rounding errors.  You may need to alter this
          threshold.<br>
          <br>
        </div>
        Ian<br>
        <div>
          <div><br>
          </div>
        </div>
        <div class="gmail_extra"><br>
          <div class="gmail_quote">On 19 November 2016 at 12:24, Pat
            Prodanovic <span dir="ltr"><<a moz-do-not-send="true"
                href="mailto:pprodano@gmail.com" target="_blank">pprodano@gmail.com</a>></span>
            wrote:<br>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
              <br>
              I am using JR Shewchuk's Triangle to create triangulations
              for use in floodplain modeling. I am using a city's
              digital terrain model with hundreds of thousands of
              breaklines that constrain where triangles can form in the
              triangulations (streets, rivers, etc). Triangle does this
              very efficiently.<br>
              <br>
              Sometimes the input topology I am using has bad inputs
              which makes Triangle create zero area elements. When I
              import these triangulations to Matplotlib I get the error
              that such triangulations are invalid (when using the
              LinearTriInterpolator() method). I understand the
              trapezoid map algorithm implemented requires only valid
              triangulations. So far, so good.<br>
              <br>
              The option of cleaning the input topology before using
              Matplotlib exists, but it is really cumbersome. Rather
              than topology cleaning, am I am able to somehow throw out
              the zero area elements from the call to
              LinearTriInterpolator() method, and still use the
              triangulation in Matplotlib? My other alternative is to
              use something other than trapezoidal map algorithm, but
              this is just not computationally efficient.<br>
              <br>
              I've reproduced the following example that illustrates the
              problem in a small code snippet. Any suggestions?<br>
              <br>
              Thanks,<br>
              <br>
              Pat Prodanovic<br>
              <br>
              # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-<br>
              import matplotlib.tri as mtri<br>
              import numpy as np<br>
              <br>
              # manually construct an invalid triangulation having a
              zero area element<br>
              x = np.array([0.0, 1.0, 1.0, 1.0, 2.0])<br>
              y = np.array([1.0, 0.0, 2.0, 1.0, 1.0])<br>
              z = np.zeros(5)<br>
              <br>
              triangles = np.array( [[0, 1, 2], [1, 3, 2], [1, 4, 2],
              [0, 4, 1]])<br>
              <br>
              # create a Matplotlib Triangulation<br>
              triang = mtri.Triangulation(x,y,triangl<wbr>es)<br>
              <br>
              # to perform the linear interpolation<br>
              interpolator = mtri.LinearTriInterpolator(tri<wbr>ang, z)<br>
              m_z = interpolator(1.0, 1.0)<br>
              # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-<wbr>+-+-+-+-+-<br>
              <br>
              ______________________________<wbr>_________________<br>
              Matplotlib-users mailing list<br>
              <a moz-do-not-send="true"
                href="mailto:Matplotlib-users@python.org"
                target="_blank">Matplotlib-users@python.org</a><br>
              <a moz-do-not-send="true"
                href="https://mail.python.org/mailman/listinfo/matplotlib-users"
                rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/matplotlib-users</a><br>
            </blockquote>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
  </body>
</html>