How to get a list the visible vertices and segments of a mesh

2024/10/6 9:33:43

I work on pose estimation of a 3d objects. I am using CAD model of that object to generate all the possible hypothesis of its pose. I am using pyopengl to render the view of the object from a specific POV. Can anyone explain how to get a list of all the visible edges?

So I use face culling to eliminate the occluded faces, but I don't know how to pass the visible edges(indices and segments) to other python functions.

If there are any other approaches (not using OpenGL), I would really appreciate it.

So I want to get the drawn edges in the The rendered image:

img

I don't really want the image to be displayed. In summary, I have a CAD model, and I want a function that can return the visible segments out of a specific POV.

Thanks

Answer

Face culling

This works only for single convex strict winding rule mesh without holes!

The idea is that sign of dot product of 2 vectors will tell you if the vectors are opposite or not. So if we have a normal pointing out and view direction their dot should be negative for faces turned towards camera/viewer.

As you do not want to render just select visible planar faces/edges you can do this on CPU side entirely. What you need is to have your mesh in form of planar faces (does not matter if triangles,quads or whatever) so let assume triangles (for more points you just add them to _face but for computation still use only v0,v1,v2) ... Each face should have the vertexes and normal.

struct _face{double v0[3],v1[3],v2[3],n[3];};List<_face> mesh;

Now the vertexes v0,v1,v2 you already have. All of them should be ordered in strict winding rule. That means if you look at any face from outside the points should form only CW (clockwise) loop (or only CCW (counter-clockwise) loop). To compute normal you simply exploit cross product which returns vector perpendicular to both operands:

n = cross(v1-v0,v2-v1) // cross product
n = n / |n|            // optional normalize to unit vector

If you need the vector math see

  • Understanding 4x4 homogenous transform matrices

On the bottom is how to compute this... Also the whole answer you will need for the camera direction so read it...

Now if your mesh has strict winding rule than all the computed normals are pointing out of mesh (or inwards depends on your coordinate system, CW/CCW and order of operands in cross product). Let assume they all pointing out (if not just negate normal).

In case you do not have strict winding rule compute avg point of your mesh (sum all vertexes and divide by their count) this will be the center c of your object. Now just compute

dot(n,(v0+v1+v2)/3 - c)

and if not positive negate the n. This will repair your normals (you can also reverse the v0,v1,v2 to repair the mesh.

Now the camera and mesh usually has its own 4x4 transform matrix. one transfroms from mesh LCS (local coordinate system) to GCS ("world" global coordinate system) and the other from GCS to camera LCS (screen). We do not need projections for this as we do not render ... So what we need to do for each face is:

  1. convert n to GCS

  2. compute dot(n,camera_view_direction)

    where camera_view_direction is GCS vector pointing in view direction. You can take it from direct camera matrix directly. It is usually the Z axis vector (in OpenGL Perspective view it is -Z). Beware camera matrix used for rendering is inverse matrix so if the case either compute inverse first or transpose it as we do not need the offset anyway ...

  3. decide if face visible from the sign of #2

Again all the math is explained in the link above...

In case you do not have mesh matrix (does not have changing position or orientation) you can assume its matrix is unit one which means GCS = mesh LCS so no need for transformations.

In some cases there is no camera and only mesh matrix (I suspect your case) then it is similar you just ignore the camera transforms and use (0,0,-1) or (0,0,+1) as view direction.

Also see this:

  • Understanding lighting in OpenGL

It should shine some light on the normals topic.

https://en.xdnf.cn/q/120469.html

Related Q&A

Python function calls the wrong method/target

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if Im trying to call the method to create/change the LEDs, it ends up in the wrong method. He…

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…

How to optimize this Pandas code to run faster

I have this code to create a swarmplot from data from a DataFrame:df = pd.DataFrame({"Refined__Some_ID":some_id_list,"Refined_Age":age_list,"Name":name_list …

i was creating a REST api using flask and while i was about to test it on postman I saw that error

File "c:\Users\kally\rest\code\app.py", line 3, in <module>from flask_jwt import JWTFile "C:\Users\kally\AppData\Roaming\Python\Python310\site-packages\flask_jwt\__init__.py",…

Web scrape get drop-down menu data python

I am trying to get a list of all countries in the webpage https://www.nexmo.com/products/sms. I see the list is displayed in the drop-down. After inspecting the page, I tried the following code but I m…

TypeError(unsupported operand type(s) for ** or pow(): str and int,)

import mathA = input("Enter Wright in KG PLease :") B = input("Enter Height in Meters Please :")while (any(x.isalpha() for x in A)):print("No Letters Please")A = input(&qu…

How can I do assignment in a List Comprehension? [duplicate]

This question already has answers here:How can I do assignments in a list comprehension?(8 answers)Closed 1 year ago.Generally, whenever I do a for loop in python, I try to convert it into a list comp…

KeyError: column_name

I am writing a python code, it should read the values of columns but I am getting the KeyError: column_name error. Can anyone please tell me how to fix this issue. import numpy as np from sklearn.clust…

Find starting and ending indices of list chunks satisfying given condition

I am trying to find the start and stop indices of chunks of positive numbers in a list.cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] For the given example input, the desired o…