r/OpenPythonSCAD • • Oct 15 '24

Is an inverse() function supported to invert a handle?

I typically follow the pattern of translating + rotating a component during assembly to do touch up work, and perform inverse movement to put the component back to its original position to prep for printing on the base plate.

I have been utilizing handles for this a lot.

The pain point I ran into: I really could use numpy's np.linalg.inv() to calculate the inverse eigen vector to restore a component back to its original position.

Questions:

  • Is there a way to import libraries I install from pip into pythonscad's run time? I see a File > Show Library Folder. Is that where I can place dependencies? It is still clunky for numpy since it has a chain of py files I would need to copy over.
  • Is there a built-in matrix inverse operator I can use right now?
3 Upvotes

26 comments sorted by

View all comments

Show parent comment

1

u/gadget3D Oct 21 '24

yes, 1st statement is correct

2nd is not

restore = align(moved_sq_3d, baseplate.origin)

is not

restore = align(moved_sq_3d, baseplate.origin, moved_sq_3d.origin)

obj.align(A,B=1) is equal to

obj * (A/B) /// operators are matrix operators here

2

u/rebuyer10110 Oct 22 '24

You are right, my mistake. I am still wrapping my head around this.

What is the full documentation on arguments to align? I am having trouble wrapping my head around what it means when I am passing only one 4x4 matrix to align instead of two. Does it take the object's origin as an implicit argument in this case?

1

u/rebuyer10110 Oct 22 '24

obj.align(A,B=1) is equal to

obj * (A/B) /// operators are matrix operators here

What happens if it is obj.align(A=1, B) ?

1

u/gadget3D Oct 22 '24

Np, its hard to get that into the head initially, but once you realize, it makes much more sense

IMHO there might not be a need to really understand the internals, just 1st handle is the handle on the object, which is settled already, 2nd handle is the object being attached. this might help.

Sorry, there is very little documentation on align yet, generally I am very bad in documenting and realizing the key facts which others need to comprehend. WillAdams is working on a WIKI FYI

If you pass only one argument to align, second argument is 1, which translates to unity matrix, and this is the untransformed origin of the being-attached object

if you want to do use like obj.align(A=1,B), you need to provide an unity matrix in 1sr argument,

which is

obj.align(baseplate.origin, B) # from previous post

or more simply:

obj.divmatrix(B) # from previous post

Hope that helps

1

u/rebuyer10110 Oct 23 '24

Is it correct to say align(A, B) is ALWAYS performing divmatrix, which is performing A/B (aka, A * B_inverse)?

1

u/gadget3D Oct 23 '24

its always performing multmatrix. if B argument given, its performing "*B_inverse" in addition

2

u/rebuyer10110 Oct 25 '24 edited Oct 25 '24

Thank you. I played with it some more, and I understand it much better now.

Here's the "pattern" I had mentally.

(1) I would position a shape to origin at various points to enact some modification.

(2) At the end I want to undo all the transformation so the modified piece is in the same position as initially at the start of the function.

I do this since openscad (and pythonscad) built-in function often operate around the origin (e.g., things like mirroring and rotations).

With this, I was able to utilize align() to achieve my natural flow.

EDIT: Added a second option of saving handles to the object so I can do a one-step restore.

def reposition_jig(jig_vanilla, punch_screw_hole=False, hole_diam=3, head_diam=BOLT_HEAD_DIAMETER, nut_diam=HEX_NUT_MAX_DIAMETER):

    # identity matrix
    IDENTITY = lambda: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]

    # Do some positioning to get my shape into a "neutral" position. Save the (modified) origin handle as a restore_point.
    res = jig_vanilla.back(20).right(50).up(0.8)
    restore_point = res.origin
    res.restore_point = IDENTITY()  # Identity matrix for performing instant jump later.

    # Position the shape again to pre-align on 2 axis where I would drill a hole right at the (x=0, y=0, z=0).
    # hole should be 6.5mm apart towards the target, from center of existing drill alignment. Center to center.
    # Save this handle as well.
    res = res.right(5.5).back(12.8).right(6.5)
    existing_alignment = res.origin

    # The actual hole I want to drill is just 7mm to the left.
    res = res.right(7)
    hole_pos = res.origin

    # Drilling the hole. I want it in a weird way where the top is bigger to fit the m3 bolt head, and actual thru-hole is 3mm diameter. Under the jig there's a partial hole to snuggly fit a m3 bolt nut.
    # 5mm diam head, 3mm diam bolt shaft.
    drill = cylinder(r=hole_diam/2, h=100, center=True, fn=30)
    bolt_head = cylinder(r=head_diam/2, h=50, fn=30)
    nut_hole = cylinder(r=nut_diam/2, h=50, fn=6).roty(180).down(1.8)

    total_drill = (drill | bolt_head | nut_hole).down(1.35)
    res = res - (total_drill)

    # restore to original position.
    toggleOption = 1
    
    if toggleOption == 1:
      # Option 1: Go in reverse in a chained matrix operation.
      res = align(res, existing_alignment, hole_pos)
      res = align(res, restore_point, existing_alignment)
    else:
      # Option 2: do an instant jump, since handle as part of object property follow object's transforms once assigned!
      res = align(res, IDENTITY(), res.restore_point)

    return res

3

u/rebuyer10110 Oct 25 '24

u/WillAdams any interest in enriching the existing align() documentation (reddit wiki) with an example like this? Feel free to simplify it a bit as needed.

1

u/WillAdams Oct 25 '24

Yes, I've added you as a "wiki contributor" --- let me know if you have any difficulties with doing this.

1

u/rebuyer10110 Oct 30 '24

Thanks. I added a section. Please feel free to review.

1

u/gadget3D Oct 25 '24

you can easily translate an object with

moved = object + [x, y, z ]

this is even shorter than using right, back and top ....

even '-' for translating is possible since today

do scaling with * respectively

1

u/rebuyer10110 Oct 25 '24

I actually have slight preference for front/back/left/right/up/down semantic actually :)

It's easier on the eye when skimming the code quickly.

And in practice, I only do it "once" for important positions. Afterwards all other transformations would be done with handles.

For me the biggest game changer has been align() / multmatrix() / divmatrix().

It took me some time to understand it well. But well worth the effort :)