[SOLVED] Calculate Distance Between Cylinder and Line (3D Math Problem)

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
User avatar
gbroques
Posts: 167
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

[SOLVED] Calculate Distance Between Cylinder and Line (3D Math Problem)

Post by gbroques »

Hi all.

I'm struggling with a 3D math problem and need some help.

The problem is as follows:
  • There's a line in 3D space defined by two points, p1 and p2 (see Front View screenshot).
  • This line represents the bottom edge of a flat metal bar, welded onto a pipe.
  • There's a pipe centered around the origin with a known radius, r (see Top View screenshot) .
  • I want to calculate the distance, x, between the pipe centered around the origin, and the line, so that I know the width of the flat metal bar.
How would I calculate this?

Maybe there's some other solution that would you propose given different known facts?

Using Python code in FreeCAD or describing a solution in terms of mathematics (vectors, planes, etc.) would be great!

Let me know if my question is not clear enough! :)
distance-between-line-and-cylinder-front.png
distance-between-line-and-cylinder-front.png (94.79 KiB) Viewed 1572 times
distance-between-line-and-cylinder-top-view.png
distance-between-line-and-cylinder-top-view.png (82.39 KiB) Viewed 1572 times
Last edited by gbroques on Fri Oct 15, 2021 4:20 pm, edited 1 time in total.
User avatar
gbroques
Posts: 167
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: Calculate Distance Between Cylinder and Line (3D Math Problem)

Post by gbroques »

I just wanted to follow up on this and let people know I figured it out.

Given 2 points (or Vectors), P1 and P2.

PNx, PNy, PNz notation is used to describe the x, y, and z components of each point, where N is 1 or 2.

STEP 1

I formed the equation of a 3D line:

Code: Select all

Line3D(t) = P1 + t (P2 - P1)
Where t is some scalar between 0 and 1.

STEP 2

Next find a value for t, where z = 0.

Code: Select all

t = -P1z / (P2z - P1z)
STEP 3

Finally, use t from above and x-values from points to find the x-coordiate value for the point on the 3D line where z = 0.

(This comes from the equation of the 3D line, but only condsering the x-coordinate.)

Code: Select all

x = P1x + t * (P2x - P1x)
STEP 4

In my case this yields a negative x-coordinate value for the point on the line where z = 0.

Take the absolute value this point, and subtract r, to get the distance between the line and cylinder centered about the origin.

Code: Select all

d = abs(x) - r
References
top-view-x-r.png
top-view-x-r.png (56.88 KiB) Viewed 1427 times
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: [SOLVED] Calculate Distance Between Cylinder and Line (3D Math Problem)

Post by mario52 »

Hi

then Vector_API

and DraftVecUtils.py "C:\FreeCAD\Mod\Draft\DraftVecUtils.py"

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply