def remesh(meshfile,
           omeshfile,
           keep_normals=False,
           integrator=integrate.integrate,
           invert_normals=True,
           edge_protect=0):
    # Load pre-defined normals from a CSV (exported from MATLAB).
    # Use pstereo.get_normals(images, lights) to obtain normals using photometric
    # stereo.
    (vertices, normals, indices) = plyutils.readPLY(meshfile)

    # normalize the normals.
    albedos = np.linalg.norm(normals, axis=1, keepdims=True)
    normals = normals / albedos

    old_normals = np.array(normals)
    #print(old_normals[2000:2020,:])
    #print("OLD: ", old_normals[0, :], " keep_normals: ", keep_normals)

    #print(normals.shape)
    w = int(math.sqrt(normals.shape[0]))
    h = w
    normals = normals.reshape((w, h, 3))

    if invert_normals:
        normals[:, :, 2] = -normals[:, :, 2]

    if np.sum(normals[:, :, 2]) > 0:
        zflipped = False
    else:
        zflipped = True
    # Integrate normals into a heightfield.
    zfield = integrator(normals, zflipped=zflipped, edge_protect=edge_protect)

    # Some stats.
    print("zmax: ", np.max(zfield))
    print("zmin: ", np.min(zfield))
    #print(normals[30:32,30:32,:])

    # Create a mesh from the heightfield.
    mesh = z2mesh.z2mesh(zfield, -1.0, 1.0, -1.0, 1.0, flip=False)

    # Expand mesh components.
    new_vertices, new_normals, new_indices = mesh

    if keep_normals:
        #old_normals[:, :, 2] = -old_normals[:, :, 2]
        #old_normals[:, 2] = -old_normals[:, 2]
        new_normals = old_normals * albedos

    #print("NEW: ", new_normals[0, :])
    # Write the mesh to mesh file.
    plyutils.writePLY(omeshfile, new_vertices, -new_normals, new_indices)
Example #2
0
def remesh(meshfile, omeshfile, keep_normals=False):
    # Load pre-defined normals from a CSV (exported from MATLAB).
    # Use psereo.get_normals(images, lights) to obtain normals using photometric
    # stereo.
    (vertices, normals, indices) = plyutils.readPLY(meshfile)
    old_normals = np.array(normals)
    print("OLD: ", old_normals[0, :])

    print(normals.shape)
    w = int(math.sqrt(normals.shape[0]))
    h = w
    normals = normals.reshape((w,h,3))
    #normals[:, :, 2] = -normals[:, :, 2]

    # Integrate normals into a heightfield.
    zfield = integrate.integrate(normals, 0.0)

    # Some stats.
    print("zmax: ", np.max(zfield))
    print("zmin: ", np.min(zfield))

    # Create a mesh from the heightfield.
    mesh = z2mesh.z2mesh(zfield, -1.0, 1.0, -1.0, 1.0)

    # Expand mesh components.
    new_vertices, new_normals, new_indices = mesh
    new_vertices[:, 2] = -new_vertices[:, 2]

    if keep_normals:
        #old_normals[:, :, 2] = -old_normals[:, :, 2]
        #old_normals[:, 2] = -old_normals[:, 2]
        new_normals = old_normals

    print("NEW: ", new_normals[0, :])
    # Write the mesh to mesh file.
    plyutils.writePLY(omeshfile, new_vertices, new_normals, new_indices)
    normals[:, :, 1] = -normals[:, :, 1]
    normals[:, :, 0] = -normals[:, :, 0]
    normals = normals.reshape((W * H, 3))

    print("normals: ", np.sum(normals[:, 2]))

    return new_vertices, normals, new_indices


if __name__ == "__main__":
    directory = sys.argv[1]
    if len(sys.argv) > 2:
        directive = sys.argv[2]
    else:
        directive = None

    os.system("mitsuba " + directory + "/originals/normals-scene.xml -o " +
              directory + "/originals/normals.exr -DsampleCount=128")

    colors = loadEXR(directory + "/originals/normals.exr", ("R", "G", "B"))

    normals = 2 * colors - 1
    normals = np.transpose(normals, [1, 2, 0])
    print(normals.shape)
    W, H, _ = normals.shape

    mesh = normalsToMesh(normals, directive=directive)

    # Write the mesh to mesh file.
    plyutils.writePLY(directory + "/originals/targetmesh.ply", *mesh)
assert (False)
# Load pre-defined normals from a CSV (exported from MATLAB).
# Use psereo.get_normals(images, lights) to obtain normals using photometric
# stereo.
normals, W, H = pstereo.load_from_csv(sys.argv[1] + "/normal_field")

normals[:, :, 2] = -normals[:, :, 2]

# Integrate normals into a heightfield.
zfield = integrate(normals, 0.0)

# Some stats.
print("zmax: ", np.max(zfield))
print("zmin: ", np.min(zfield))

# Create a mesh from the heightfield.
mesh = z2mesh.z2mesh(-zfield, -1.0, 1.0, -1.0, 1.0)

new_vertices, new_normals, new_indices = mesh

if "--keep-normals" in sys.argv:
    normals = np.flip(normals, axis=1)
    normals[:, :, 2] = -normals[:, :, 2]
    normals = -normals
    normals = normals.reshape((W * H, 3))
else:
    normals = new_normals

# Write the mesh to mesh file.
plyutils.writePLY(sys.argv[2], new_vertices, normals, new_indices)
    z = frankot.project_surface(mx, my)

    # Slice the first quarter
    z = z[:sz[0] / 2, :sz[1] / 2]
    assert (z.shape[0] == W and z.shape[1] == H)

    # Readjust z values to fit the average.
    newMeanZ = z.mean(axis=0).mean(axis=0)
    oldMeanZ = mean

    z2 = (oldMeanZ - newMeanZ) + z

    return z2 / (W / 2)


"""# Readjust the points.
vertices[:, 2] = z2.reshape((W*H, 1));

num_samples = np.zeros(normals[:, :, 1]);
# Recompute the normals.
# Loop through indices and update the normals.

# first, put the normals back into W*H form
normals = normals.reshape((W*H, 3));
normalcounts = np.zeros(W*H);

for t in indices:
    n = cross(vertices[t[0]] - vertices[t[1]], vertices[t[2]] - vertices[t[1]]);
    n.normalize();
    nc0 = normalcounts[t[0]];
    nc1 = normalcounts[t[1]];