cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

Fix workplane cylinder center when generated using a custom direction

Open kmarchais opened this issue 1 year ago • 7 comments

Fixes #1591.

  • This PR fixes the following bug when a cylinder generated from a workplane with a direction different than the default one (0, 0, 1) was not centered at the right position.
>>> cq.Shape.centerOfMass(cq.Workplane("XY").cylinder(radius=1, height=10, direct=cq.Vector(1, 0, 0)).val()) 
Vector: (5.0, 0.0, -5.0)
>>> cq.Shape.centerOfMass(cq.Workplane("XY").cylinder(radius=1, height=10, direct=cq.Vector(0, 1, 0)).val()) 
Vector: (0.0, 5.0, -5.0)
>>> cq.Shape.centerOfMass(cq.Workplane("XY").cylinder(radius=1, height=10, direct=cq.Vector(0, 0, 1)).val()) 
Vector: (0.0, 0.0, 0.0)
  • This PR also brings the possibility to center the cylinder when it is generated using any custom direction.

A test has been added to check these two points. It fails before the fix and passes after the fix.

The case where centered != (True, True, True) with a direction different than one along the x, y, or z axes is not managed. What would we like to do in this case?

kmarchais avatar May 25 '24 22:05 kmarchais

Codecov Report

Attention: Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.33%. Comparing base (f29f2d6) to head (0c2b482). Report is 45 commits behind head on master.

Files with missing lines Patch % Lines
cadquery/cq.py 50.00% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1593      +/-   ##
==========================================
+ Coverage   94.86%   95.33%   +0.47%     
==========================================
  Files          28       27       -1     
  Lines        6226     6774     +548     
  Branches     1261     1010     -251     
==========================================
+ Hits         5906     6458     +552     
  Misses        193      193              
+ Partials      127      123       -4     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 25 '24 23:05 codecov[bot]

The case where centered != (True, True, True) with a direction different than one along the x, y, or z axes is not managed. What would we like to do in this case?

Something like the following should handle it.

        
        if isinstance(centered, bool):
            centered = (centered, centered, centered)

        offset = Vector()
        plane = Plane((0, 0, 0), normal=direct)
        if not centered[0]:
            offset += plane.toWorldCoords((radius, 0, 0))
        if not centered[1]:
            offset += plane.toWorldCoords((0, radius, 0))
        if centered[2]:
            offset += plane.toWorldCoords((0, 0, -height / 2))

        s = Solid.makeCylinder(radius, height, offset, direct, angle)

        # We want a cylinder for each point on the workplane
        return self.eachpoint(lambda loc: s.moved(loc), True, combine, clean)

lorenzncode avatar Jun 01 '24 19:06 lorenzncode

Thanks, I prefer this than what I did but then it requires to be careful about the axis to set for centering.

To center a cylinder along its height but directed along the y axis, it requires to put the third component of centered to True as the following example show.

c1 = cq.Shape.centerOfMass(
    cq.Workplane("XY").cylinder(
        radius=1,
        height=10,
        direct=cq.Vector(0, 1, 0),
        centered=(False, False, True),
    ).val()
) 
print(c1)
c2 = cq.Shape.centerOfMass(
    cq.Workplane("XY").cylinder(
        radius=1,
        height=10,
        direct=cq.Vector(0, 1, 0),
        centered=(False, True, False),
    ).val()
)
print(c2)

Results in

Vector: (1.0, 0.0, 1.0)
Vector: (0.0, 5.0, 1.0)

Is it the expected behavior?

kmarchais avatar Jun 02 '24 12:06 kmarchais

res = cq.Workplane("XY").cylinder(
        radius=1,
        height=10,
        direct=cq.Vector(0, 1, 0),
        centered=(False, False, True),
).val()

results in this: afbeelding

res2 = cq.Workplane("XY").cylinder(
        radius=1,
        height=10,
        direct=cq.Vector(0, 1, 0),
        centered=(False, True, False),
).val()

and

in this: afbeelding

@kmarchais how did you actually generate the reference data for the tests? Visually the result is as expected AFAICT.

adam-urbanczyk avatar Jul 10 '24 19:07 adam-urbanczyk

The test was pretty straight forward in the previous implementation, testing all the possible combinations of centering and directions. With this implementation, I will need to take time to find how to manage it correctly.

Also I want to make sure, is the following the expected result?

>>> cq.Shape.centerOfMass(cq.Workplane("XY").cylinder(radius=10, height=40, direct=cq.Vector(1, 0, 0), centered=False).val())
Vector: (20, -10, 10)

kmarchais avatar Jul 11 '24 10:07 kmarchais

Looks OK, but the local coordinate system of the cylinder is ill-defined with only Z vector. Is that the point you are trying to make?

afbeelding

adam-urbanczyk avatar Jul 11 '24 16:07 adam-urbanczyk

I am just not used to this radius shift and wanted to make sure that this -10 in the y direction is fine. I will try to update the test soon.

kmarchais avatar Jul 12 '24 15:07 kmarchais

Hey @kmarchais any updates on this? Do you intend to finalize it?

adam-urbanczyk avatar Nov 09 '24 21:11 adam-urbanczyk

@adam-urbanczyk Thanks for the remainder. I'm on it.

kmarchais avatar Nov 18 '24 12:11 kmarchais

I could not find a simple way to test every combination of centered and direct parameters. I made it so that it checks different configurations:

  • Main axis directions (±X, ±Y, ±Z)
  • Centering configurations (all centered, none centered, mixed centering)
  • Verifies correct cylinder center position in each case

kmarchais avatar Nov 29 '24 09:11 kmarchais

Another idea is update the typing of the direct param

    direct: Union[Tuple[float, float, float], Vector] = Vector(0, 0, 1),

or = (0, 0, 1) See Plane normal: https://github.com/CadQuery/cadquery/blob/0c472ef741ef545174580e86190ad22294c45814/cadquery/occ_impl/geom.py#L553

lorenzncode avatar Dec 06 '24 01:12 lorenzncode

@lorenzncode it seems that your suggestions were applied. Are you OK with merging?

adam-urbanczyk avatar Dec 21 '24 22:12 adam-urbanczyk

@lorenzncode it seems that your suggestions were applied. Are you OK with merging?

I came across a couple of cases I'm not certain are correct.

  1. Compare box and cylinder for the following case. It seems the result is inconsistent. Why is the cylinder in negative Y? Shouldn't the bounding box of the cylinder be identical to box1? That is, both the box and cylinder in +Y.
import cadquery as cq

centered = (False, False, False)

r = 1
h = 10

cyl1 = (
    cq.Workplane()
    .cylinder(h, r, (1, 0, 0), centered=centered)
)

box1 = (
    cq.Workplane()
    .box(h, r * 2, r * 2, centered=centered)
)

image

  1. Here, I expected the second cylinder to be a YZ mirror of the first (bound box of cylinder to be in +Z).
import cadquery as cq

r = 1
h = 10
centered = (False, False, False)

cyl1 = (
    cq.Workplane()
    .cylinder(h, r, (1, 0, 0), centered=centered)
)

cyl2 = (
    cq.Workplane()
    .cylinder(h, r, (-1, 0, 0), centered=centered)
)

image

lorenzncode avatar Dec 22 '24 05:12 lorenzncode

I tested the snippet again here and get the same result. I suppose this is fine as is.

If I were to use this method I think I would choose only one of these centering options:

centered = (True, True, True)
# or 
centered = (True, True, False)

Anyway the default is (True, True, True).

lorenzncode avatar Dec 22 '24 16:12 lorenzncode

Alright, merging. Thanks @kmarchais !

adam-urbanczyk avatar Dec 24 '24 16:12 adam-urbanczyk