raytracing

问题描述 投票:0回答:1

I've combined multiple stl files to one big mesh and I'm now trying to remove all faces that are on the inside.

I got a hint to use ambient occlusion and remove all faces that are below a certain brightness threshold.

So I started using libigl which has an ambient occlusion function. I came up with this code:

import igl
import numpy as np
from meshplot import plot, subplot, interact
import os
import math

v, f = igl.read_triangle_mesh("cube.stl")
print("Vertices: ", len(v))
print("Faces: ", len(f))

n = igl.per_vertex_normals(v, f)
ao = igl.ambient_occlusion(v, f, v, n, 5)
newV = []
newF = []
z = 0.8 #brightness threshold
for x in range(3,len(v),3):
    if ao[x]<=z or ao[x]<=z or ao[x]<=z: #adds the faces with its verteces with brightness above z to 'newV' and 'newF'
        #each face has 3 verteces
        newV.append(v[x-3]) #verex1
        newV.append(v[x-2]) #verex2
        newV.append(v[x-1]) #verex3
        newF.append(f[int(x/3)-1]) #face
plot(np.array(newV),np.array(newF))

When using this code on a 20*20*20mm cube which is a combination of 8 10*10*10mm cubes i get this result

It unfortunately detects more than just the outside faces as bright.

I combine the original stl files with this code:

import numpy
from stl import mesh

data = []
for x in range(2):
    for y in range(2):
        for z in range(2):
            #standart.stl is a 10 by 10 by 10 mm cube
            mesh_instance = mesh.Mesh.from_file('standart.stl')
            mesh_instance.translate((x*10,y*10,z*10))
            data.append(mesh_instance.data)

combined = mesh.Mesh(numpy.concatenate(data))
combined.save('out.stl')

(It just places 8 10*10*10mm cubes next to eachother)

You can find one combined stl here (it's the 20*20*20mm cube)

So do you know why the ambient occlusion function detects the inside faces as bright (or how i could tweak it so that it only detects the outside faces as bright) or anything that would help me detect the inside or outside faces so i could keep or remove them, that in the end i would be left with the outside faces only?

python stl mesh raytracing libigl
1个回答
0
投票

A different approach might be to do the following:

  1. Start with your original mesh
  2. Calculate the convex hull of [1] -> this produces a second mesh
  3. For all faces in [1], remove the face from the mesh if any of its vertices are not in [2]
  4. Your original mesh now only has faces that are on the "outside".

There are a number of algorithms for computing the convex hull, notably Chan's algorithm and Quickhull. There are also python libraries with implementations of these algorithms, like SciPy.

© www.soinside.com 2019 - 2024. All rights reserved.