Skip to content

triangle.triangulate output does not have segments specified #92

Description

@JulianKnodt

I'm computing a constrained delaunay triangulation of an input UV parameterization, and I want to exactly maintain all input edges, so I passed them all through the segments parameter. Currently though, I found that the output will have edges flipped as compared to the input mesh, and if I do not pass segments at all the output is identical as when I specify segments to preserve.

Here is code to load and output meshes, and the data I'm loading with it:

I'm not sure if my use is wrong, but any help would be appreciated.

import argparse
import math
import trimesh

import torch

import numpy as np

import triangle

def arguments():
  a = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
  )
  a.add_argument("-i", "--input", required=True, help="Input mesh")
  a.add_argument("--iters", type=int, default=10000, help="Optimization iterations")
  a.add_argument("--device", default="cpu", help="device to use")
  return a.parse_args()

def main():
  args = arguments()
  device = args.device
  print("Loading mesh")
  mesh = trimesh.load_mesh(args.input, process=False)
  uv = mesh.visual.uv

  faces = torch.tensor(mesh.faces, device=device)
  uniq_edges = []

  tri_m = trimesh.Trimesh(
    vertices=np.concatenate([uv, np.zeros_like(uv[:, :1])], axis=-1),
    faces=mesh.faces,
  )
  tri_m.export("tmp_in.obj")

  og_vi = {}
  for vi, p in enumerate(uv.tolist()):
    assert(tuple(p) not in og_vi)
    og_vi[tuple(p)] = vi

  og_edges = set()
  for vis in mesh.faces:
    assert(len(vis) == 3)
    for i, v in enumerate(vis):
      assert(v >= 0)
      n = vis[(i+1)%3]
      assert(n >= 0)
      og_edges.add(tuple(sorted([int(v),int(n)])))

  corners = [[0,1],[0,0],[1,1],[1,0]]
  t = triangle.triangulate({
    "vertices": uv.tolist() + corners,
    "segments": list(og_edges),
  })

  new_uv = t["vertices"]
  new_t = t["triangles"]
  is_valid_face = [0] * len(new_t)
  se = lambda i,j: (min(i, j), max(i,j))
  for ti, vis in enumerate(new_t):
    is_valid = True
    for i, v in enumerate(vis):
      vi = og_vi.get(tuple(new_uv[v]))
      ni = og_vi.get(tuple(new_uv[vis[(i+1) % 3]]))
      is_valid = is_valid and (vi is not None) and (ni is not None) and (se(vi, ni) in og_edges)
    is_valid_face[ti] = is_valid

  #new_t = [t for ti, t in enumerate(new_t) if is_valid_face[ti]]
  tri_m = trimesh.Trimesh(
    vertices=np.concatenate([new_uv, np.zeros_like(new_uv[:, :1])], axis=-1),
    faces=new_t,
  )
  tri_m.export("tmp.obj")
  exit()

if __name__ == "__main__": main()

dense_cube copy.obj.txt

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions