Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Angle Calculation between vectors


Recommended Posts

Greetings everyone,

In the code which is below I am doing angle calculating ( more like checking if the angle is 180 or 90 degree)
Why?
When I am doing  rough zig-zag motion I have problems with chips at the walls ( check pic below)
image.thumb.png.d97d1c8cd8cf1c9e97f297063a537c0f.png

So what I am trying to do is to check if I have 90 degree angles and convert the to arc, just to avoid nasty long chips.
I know I can do this kind of turning with normal operation, but I want to do something different.
Code which will convert  corners to arcs is ready now the only issue is checking the angles.

If you have any advice it would be nice, if you see mistake in the code please tell me and guide me on the right path,

information's about how to calculate dot product and magnitude product I got it from :
https://en.wikipedia.org/wiki/Dot_product
https://en.wikipedia.org/wiki/Cross_product

Other then that there is an issue with acos, this Sh** always wants to be between -1 and 1.....

 

pcalculate_angle
      v1x = (prv_x$ - x$) * 2      # x-component of vector v1
      v1z = prv_z$ - z$            # z-component of vector v1
      v2x = (nextx$ - x$) * 2      # x-component of vector v2
      v2z = nextz$ - z$            # z-component of vector v2
      
      # Calculate dot product and magnitude product of the vectors
      if v2z = 0 | v1z = 0, v2z = 1, v1z = 1          # Check if either v2z or v1z is zero and set them to 1 to avoid division by zero
      dot_product = (v1x * v2x) + (v1z * v2z)         # Calculate the dot product of v1 and v2
      if dot_product = 0, dot_product = 1             # If the dot product is zero, set it to 1 to avoid division by zero

      v1_magnitude_squared = v1x * v1x + v1z * v1z    # Calculate the squared magnitude of v1
      v2_magnitude_squared = v2x * v2x + v2z * v2z    # Calculate the squared magnitude of v2
      v1_magnitude = sqrt(v1_magnitude_squared)       # Calculate the magnitude of v1
      v2_magnitude = sqrt(v2_magnitude_squared)       # Calculate the magnitude of v2
      angle_rad_1 = dot_product / (v1_magnitude * v2_magnitude)  # Calculate the cosine of the angle between v1 and v2
      angle_rad = acos(angle_rad_1)  # Calculate the angle between v1 and v2 in radians
    
      # Convert angle from radians to degrees
      angle_deg = angle_rad * 180.0 / pi$
       "//", *angle_rad, "//", e$
      
      # Check if dot product and magnitude product are within valid range
      if dot_product >= m_one & dot_product <= 1 & v1_magnitude > 0 & v2_magnitude > 0,
        [
        # Calculate angle between the vectors in radians
        angle_rad_1 = dot_product / (v1_magnitude * v2_magnitude)
        angle_rad = acos(angle_rad_1)
        #"//", *angle_rad, "//", e$
        # Convert angle from radians to degrees
        angle_deg = angle_rad * 180.0 / pi$
        ]  
      else,
        [
        # Calculate the angle using atan2 for orthogonal vectors
        if v2z = 0 | v1z = 0, v2z = 1, v1z = 1    # Check if either v2z or v1z is zero and set them to 1 to avoid division by zero
        angle_rad_1 = v2x / v2z                   # Calculate the tangent of the angle between v1 and v2
        angle_rad_2 = acos(angle_rad_1)           # Calculate the angle between v1 and v2 in radians
        angle_rad_3 = v1x / v1z                   # Calculate the tangent of the angle between v1 and v2
        angle_rad_4 = acos(angle_rad_3)           # Calculate the angle between v1 and v2 in radians
        angle_rad = angle_rad_2 - angle_rad_4     # Calculate the angle between v1 and v2 in radians
        #"//", *angle_rad, "//", e$
        # Ensure the angle is within the range [0, 2*pi)
        if angle_rad < 0, angle_rad = angle_rad + 2 * pi$
        # Convert the angle from radians to degrees
        angle_deg = angle_rad * 180.0 / pi$
        #*angle_rad, e$
        #*angle_deg, e$
        ]

 for now output looks like this:

image.thumb.png.4a598d8ef2daa071a281a3250b333a90.png

Link to comment
Share on other sites

If you want to know how alike two 3D vectors are, normalize them, then dot them.  If the dot product is 0 (or very close to it, remember not to trust floating point numbers) then the vectors are orthogonal.  If the dot product is -1 then they are 180 degree apart.

Link to comment
Share on other sites

Greetings @Zaffin_D

I did how you told me to do and the angles looks ok for now, except they are wrong :D
I went deep in this calculation and I just cant collect myself (doing the normal job and editing my post)

Can you check what I did wrong this time?
If you like I can share full code so you can test yourself if you like,


 

  v1x = (prv_x$ - x$) * 2      # x-component of vector v1
  v1z = prv_z$ - z$            # z-component of vector v1
  v2x = (nextx$ - x$) * 2      # x-component of vector v2
  v2z = nextz$ - z$            # z-component of vector v2

  
  v1_magnitude_squared = v1x * v1x + v1z * v1z
  v2_magnitude_squared = v2x * v2x + v2z * v2z
  v1_magnitude = sqrt(abs(v1_magnitude_squared))
  v2_magnitude = sqrt(abs(v2_magnitude_squared))

  # Normalize vectors
  v1_normalized_x = v1x / v1_magnitude
  v1_normalized_z = v1z / v1_magnitude
  v2_normalized_x = v2x / v2_magnitude
  v2_normalized_z = v2z / v2_magnitude

  # Calculate dot product with normalized vectors
  dot_product_normalized = (v1_normalized_x * v2_normalized_x) + (v1_normalized_z * v2_normalized_z)

  # Check if dot product and magnitude product are within valid range
  if dot_product_normalized >= m_one & dot_product_normalized <= 1 & v1_magnitude > 0 & v2_magnitude > 0,
    [
    # Calculate angle between the vectors in radians
    angle_rad = acos(dot_product_normalized)
    # Convert angle from radians to degrees
    angle_deg = angle_rad * 180.0 / pi$
    ]
  else,
    [
    # Calculate the angle using atan2 for orthogonal vectors
    if v2z = 0 | v1z = 0, v2z = 1, v1z = 1
    angle_rad_1 = v2x / v2z  # Calculate the tangent of the angle between v1 and v2
    angle_rad_2 = acos(angle_rad_1)  # Calculate the angle between v1 and v2 in radians
    angle_rad_3 = v1x / v1z  # Calculate the tangent of the angle between v1 and v2
    angle_rad_4 = acos(angle_rad_3)  # Calculate the angle between v1 and v2 in radians
    angle_rad = angle_rad_2 - angle_rad_4  # Calculate the angle between v1 and v2 in radians
    ]
    # Ensure the angle is within the range [0, 2*pi$)
      # Check if dot product is very close to zero
    if abs(dot_product_normalized) < angle_tolerance, angle_deg = 90.0 # Vectors are orthogonal
    else,
    [
    # Check if dot product is less than -1 + 1e-10  0.0000000001
    if dot_product_normalized < m_one + angle_tolerance, angle_deg = 180.0  # Vectors are 180 degrees apart
    ]
    while angle_rad >= 2 * pi$,
      [
      angle_rad = angle_rad - (2 * pi$)
      ]
      # Convert the angle from radians to degrees
      angle_deg = angle_rad * 180.0 / pi$

  # Initialize the direction variables with unknown direction
  #sdirection_x = sundirection
  #sdirection_z = sundirection

  # Determine the direction of the vector for x-axis
  if v1x < 0 & v2x > 0, sdirection_x = sudirection
  else,
    [
    if v1x > 0 & v2x < 0, sdirection_x = sddirection
    ]
  sdirection_x, e$  

  # Determine the direction of the vector for z-axis
  if v1z > 0 & v2z < 0, sdirection_z = sldirection   
  else,
    [
    if v1z < 0 & v2z > 0, sdirection_z = srdirection
    ]
  sdirection_z, e$  
  # Output the angle in degrees and the direction
  angle_deg, e$

image.png.c781601a57058b90e08a5881937bfcdb.png

 

Link to comment
Share on other sites
#v1x = (prv_x$ - x$) * 2     # x-component of vector v1
  v1x = (prv_x$ - x$)      # x-component of vector v1
  v1z = prv_z$ - z$            # z-component of vector v1
  #v2x = (nextx$ - x$) * 2      # x-component of vector v2
  v2x = (nextx$ - x$)      # x-component of vector v2
  v2z = nextZ$ - z$            # z-component of vector v2
  angle_deg = atan2(v1x,v1z), "v1_", *angle_deg, e$
  angle_deg = atan2(v2x,v2z), "v2_", *angle_deg, e$
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...