3D MeshFace Normals Calculation Question ...

Everything related to 3D programming
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

3D MeshFace Normals Calculation Question ...

Post by marc_256 »

Hi,

I made a new procedure for creating a 3D threads.
it works fine, but I have sometimes problems with the NORMALS of the faces.
So, there are faces with the wrong normal direction.

Image

To debug my program I want to check and visualize the direction of the normals on my 3D editor,
I want to draw the normals on the screen to be perpendicular to the MeshFace ...
See image (https://en.wikipedia.org/wiki/Normal_(g ... onal_space) plane with blue arrows

Solutions:

1) Normal vector data 1:
- PosX, PosY, PosZ, AngleX, AngleY, AngleZ
with a fixed length of 1 (normalized vector)

2) Normal vector data 2:
- P1_PosX, P1_PosY, P1_PosZ, P2_PosX, P2_PosY, P2_PosZ


- With very small faces the normals with length of 1 will be ok.
With larger faces I don't see the very small normal vectors on the screen.

So, I want to use/store the second solution,

What do I have till now:
Triangle Face Data => Vertex1 P(X, Y, Z), Vertex2 P(X, Y, Z), Vertex3 P(X, Y, Z)
Normal Data P1 (vector origin) => Face center C(X, Y, Z)
Normal Length => example 100

Question:
How can I calculate the normal Data of P2(X, Y, Z)

What I need now are the 3 Angles of the Normal vector
(AngleX, AngleY, AngleZ)
But how can I calculate these with the 3 face vertices ?


I found this: https://github.com/gametutorials/tutori ... 3DMath.cpp

For me !@#$%^&*() :oops:


Thanks,
Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: 3D MeshFace Normals Calculation Question ...

Post by pf shadoko »

NormalizeMesh()
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: 3D MeshFace Normals Calculation Question ...

Post by threedslider »

Use the correct of cross product for normal :shock:

See the math here :
https://math.libretexts.org/Bookshelves ... ss_Product
Last edited by threedslider on Thu Jan 02, 2025 7:25 pm, edited 1 time in total.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: 3D MeshFace Normals Calculation Question ...

Post by minimy »

Hello, the best and easy solution is from pf shadoko.
But the math procedure can be some thing like this:

Code: Select all


global normal.vector3
 Procedure   CalculateTriangleNormal(x1.f, y1.f, z1.f, x2.f, y2.f, z2.f, x3.f, y3.f, z3.f)
    ;devuelve la normal del triangulo
    Protected.f ABx, ABy, ABz, ACx, ACy, ACz
    ABx = x2 - x1
    ABy = y2 - y1
    ABz = z2 - z1
    ACx = x3 - x1
    ACy = y3 - y1
    ACz = z3 - z1
  
    ; Calcula el producto cruz de AB y AC
    normal\x = ABy * ACz - ABz * ACy
    normal\y = ABz * ACx - ABx * ACz
    normal\z = ABx * ACy - ABy * ACx
  
    ; Normaliza el vector resultante
    Protected.f length
    length = Sqr(normal\x * normal\x + normal\y * normal\y + normal\z * normal\z)
    If length <> 0
      normal\x = normal\x / length
      normal\y = normal\y / length
      normal\z = normal\z / length
    EndIf
  
  EndProcedure
If translation=Error: reply="Sorry, Im Spanish": Endif
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: 3D MeshFace Normals Calculation Question ...

Post by threedslider »

@minimy : fixing some thing in normalize :mrgreen:

Here is the code : length = 1.0 / Sqr(normal\x * normal\x + normal\y * normal\y + normal\z * normal\z)

Otherwise it may be wrong result in condition :wink:
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 3D MeshFace Normals Calculation Question ...

Post by marc_256 »

Hello,

Thanks for the tips and help,
To debug my program I want to check and visualize the direction of the normals on my 3D editor,
I want to draw the normals on the screen to be perpendicular to the MeshFace ...
I don't use OGRE in my 3D viewer/editor, I use my data to 3D converter and LineXY()
so, NormalizeMesh() is not the solution here.

thanks minimy for the snipper,
and threedslider for the correction.


Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: 3D MeshFace Normals Calculation Question ...

Post by threedslider »

I have a wrong on my side for normalize :oops:

If the normal is the range from -1.0 to +1.0, so it is wrong for me because I confuse the word normalize and normalization xD

Here the code for test :

Code: Select all

x.f = 5.0
y.f = 10.0
z.f = 3.0

x1.f = 20.0
y1.f = 30.0
z1.f = 13.0

n_x.f = y * z1 - z * y1
n_y.f = z * x1 - z1 * x
n_z.f = x * y1 - y * x1

length.f = Sqr(n_x*n_x + n_y*n_y + n_z*n_z)

If length > 0
  
  n_x = n_x / length
  n_y = n_y / length
  n_z = n_z / length
  
  Debug n_x
  Debug n_y
  Debug n_z
EndIf
So the normal vector is between -1.0 to +1.0, it is correct for @minimy :shock:

What I am noob xD

Sorry @marc_256 :lol:
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 3D MeshFace Normals Calculation Question ...

Post by marc_256 »

Hi,

Just to give the actual status of my program.

I know, I'm repeating myself ...
The longer I work with 3D math, the more I respect the developers of 3D software.

This given snipper is not working for my application.


*Let me explain first ...

My CNC/LATHE mesh:
Mostly cylindrical part locked in a lathe head or locked between points.
We can remove material with a tool on the outside or drilling/turning inside the part.

So, it is impossible to create a normal vector with only the 3 points.
For now my procedure for creation of a lathe/mesh some times gives CW/CCW the points.
What you need as extra data is the direction of the normal (Not the normal data, only they are outside faces).
What I mean is, if you have only the 3 points, you can never know what is the inside/outside of the face in the mesh is.

Marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: 3D MeshFace Normals Calculation Question ...

Post by threedslider »

marc_256 wrote: Sat Jan 04, 2025 4:49 pm I know, I'm repeating myself ...
The longer I work with 3D math, the more I respect the developers of 3D software.
Yes :)

marc_256 wrote: Sat Jan 04, 2025 4:49 pm So, it is impossible to create a normal vector with only the 3 points.
What do you mean that ? this snippet is good for normal vector for 3 points (and working too)....

Maybe the most simple is to calculate a quad polygon as here :

Code: Select all

Structure vec3
  x.f
  y.f
  z.f
EndStructure


Dim vector.vec3(3)
normal.vec3

normal\x = 0.0
normal\y = 0.0
normal\z = 0.0

vector(0)\x = 5.0
vector(0)\y = 0.0
vector(0)\z = 0.0
vector(1)\x = 10.0
vector(1)\y = 0.0
vector(1)\z = 0.0
vector(2)\x = 10.0
vector(2)\y = 10.0
vector(2)\z = 0.0
vector(3)\x = 0.0
vector(3)\y = 10.0
vector(3)\z = 0.0


Procedure CalculateQuadNormal(Array vec.vec3(1))
  
  Shared normal.vec3
  
   For i=0 To 3
     
     j = (i+1)%4
       
    ; make a cross product
    normal\x = normal\x + ( vec(i)\y * vec(j)\z) - (vec(i)\z * vec(j)\y)
    normal\y = normal\y + ( vec(i)\z * vec(j)\x) - (vec(i)\x * vec(j)\z) 
    normal\z = normal\z + ( vec(i)\x * vec(j)\y) - (vec(i)\y * vec(j)\x) 
     
     ; Another to make a cross product
;     normal\x = normal\x + ( vec(i)\y - vec(j)\y) * (vec(i)\z + vec(j)\z)
;     normal\y = normal\y + ( vec(i)\z - vec(j)\z) * (vec(i)\x + vec(j)\x) 
;     normal\z = normal\z + ( vec(i)\x - vec(j)\x) * (vec(i)\y + vec(j)\y) 
    
    Next
    
    
   
  
    ;Normalize the vector
    Protected.f length
    length = Sqr(normal\x * normal\x + normal\y * normal\y + normal\z * normal\z)
    If length <> 0
      normal\x = normal\x / length
      normal\y = normal\y / length
      normal\z = normal\z / length
    EndIf
    
    Debug normal\x
    Debug normal\y
    Debug normal\z
    
  EndProcedure
  
  CalculateQuadNormal(vector())
Is it working for you ?
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: 3D MeshFace Normals Calculation Question ...

Post by threedslider »

marc_256 wrote: Sat Jan 04, 2025 4:49 pm For now my procedure for creation of a lathe/mesh some times gives CW/CCW the points.
What you need as extra data is the direction of the normal (Not the normal data, only they are outside faces).
What I mean is, if you have only the 3 points, you can never know what is the inside/outside of the face in the mesh is.
To know it what is the inside/outside of the face is all depend of your model but in general you can "flip" the normal to inside or to outside. as +1.0 or -1.0.
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 3D MeshFace Normals Calculation Question ...

Post by marc_256 »

OK, let start all over ...

Sorry, I didn't have the chance in my live to become a math guru, I only became a 2D/3D designer. 8)
So, I study a lot via YT and other websites and (gaming/math) forums.

What I understand till now, is that a [NORMAL] can have 2 directions.
1) [+1] = Face side to the camera (outside of the mesh), See Image below, Green NORMAL line
2) [-1] = Face side away from the camera (inside of the mesh), See Image below, Red NORMAL line

My [CreateVerticesFaces.pbi] procedure can now create VERTICES and FACES from my virtual lathe 3D part.
So, I lock 3 vertices for each FACE I create.

Depending of the FACE position, it can be an outside face (outside mesh) or an inside face (inside the mesh) by drilling inside the part.
I need to detect the correct NORMAL direction.
Till now my procedure only gives me the 3 vertices of a face and not the position of the face in the mesh.

The Normal CrossProduct calculation snipper is working very well.
Depending on what points you enter in the program,
for each NORMAL I can draw two NORMAL vectors, a good one (green) and a not usable one (red).

* I only need to find a method to auto correct the FACE direction in the MESH with my created FACE data.
Depending its position in the mesh... and this is the hardest part for now.


Thanks,
Marc

Image
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: 3D MeshFace Normals Calculation Question ...

Post by SMaag »

The direction of the normal depends on the triangle definition direction.
ClockWise order or CounterClockWise

You can check the Clockwise or CounterClockwise order by calculating the signed Area of the Triangle.

Code: Select all

double triangle_area ( double xa, double ya, double xb, double yb, double xc, 
  double yc )

//****************************************************************************
//
//  Purpose:
//    TRIANGLE_AREA computes the signed area of a triangle.
//  Licensing:
//    This code is distributed under the MIT license.
//
//  Modified:
//    06 May 2014
//  Author:
//    John Burkardt
//  Parameters:
//
//    Input, double XA, YA, XB, YB, XC, YC, the coordinates of
//    the vertices of the triangle, given in counterclockwise order.
//
//    Output, double TRIANGLE_AREA, the signed area of the triangle.
{
  double value;

  value = 0.5 * ( 
      ( xb - xa ) * ( yc - ya ) 
    - ( xc - xa ) * ( yb - ya ) );

  return value;
}
Post Reply