-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add WarpBatch - array process warp with single callback #651
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# %% | ||
import numpy as np | ||
from manifold3d import * | ||
|
||
# Creates a classic torus knot, defined as a string wrapping periodically | ||
# around the surface of an imaginary donut. If p and q have a common | ||
# factor then you will get multiple separate, interwoven knots. This is | ||
# an example of using the warp() method, thus avoiding any direct | ||
# handling of triangles. | ||
|
||
|
||
def run(): | ||
# The number of times the thread passes through the donut hole. | ||
p = 1 | ||
# The number of times the thread circles the donut. | ||
q = 3 | ||
# Radius of the interior of the imaginary donut. | ||
majorRadius = 25 | ||
# Radius of the small cross-section of the imaginary donut. | ||
minorRadius = 10 | ||
# Radius of the small cross-section of the actual object. | ||
threadRadius = 3.75 | ||
# Number of linear segments making up the threadRadius circle. Default is | ||
# getCircularSegments(threadRadius). | ||
circularSegments = -1 | ||
# Number of segments along the length of the knot. Default makes roughly | ||
# square facets. | ||
linearSegments = -1 | ||
|
||
# These default values recreate Matlab Knot by Emmett Lalish: | ||
# https://www.thingiverse.com/thing:7080 | ||
|
||
kLoops = np.gcd(p, q) | ||
pk = p / kLoops | ||
qk = q / kLoops | ||
n = ( | ||
circularSegments | ||
if circularSegments > 2 | ||
else get_circular_segments(threadRadius) | ||
) | ||
m = linearSegments if linearSegments > 2 else n * qk * majorRadius / threadRadius | ||
|
||
offset = 2 | ||
circle = CrossSection.circle(1, n).translate([offset, 0]) | ||
|
||
def ax_rotate(x, theta): | ||
a, b = (x + 1) % 3, (x + 2) % 3 | ||
s, c = np.sin(theta), np.cos(theta) | ||
m = np.zeros((len(theta), 4, 4), dtype=np.float32) | ||
m[:, a, a], m[:, a, b] = c, s | ||
m[:, b, a], m[:, b, b] = -s, c | ||
m[:, x, x], m[:, 3, 3] = 1, 1 | ||
return m | ||
|
||
def func(pts): | ||
npts = pts.shape[0] | ||
x, y, z = pts[:, 0], pts[:, 1], pts[:, 2] | ||
psi = qk * np.arctan2(x, y) | ||
theta = psi * pk / qk | ||
x1 = np.sqrt(x * x + y * y) | ||
phi = np.arctan2(x1 - offset, z) | ||
|
||
v = np.zeros((npts, 4), dtype=np.float32) | ||
v[:, 0] = threadRadius * np.cos(phi) | ||
v[:, 2] = threadRadius * np.sin(phi) | ||
v[:, 3] = 1 | ||
r = majorRadius + minorRadius * np.cos(theta) | ||
|
||
m1 = ax_rotate(0, -np.arctan2(pk * minorRadius, qk * r)) | ||
m1[:, 3, 0] = minorRadius | ||
m2 = ax_rotate(1, theta) | ||
m2[:, 3, 0] = majorRadius | ||
m3 = ax_rotate(2, psi) | ||
|
||
v = v[:, None, :] @ (m1 @ m2 @ m3) | ||
return v[:, 0, :3] | ||
|
||
def func_single(v): | ||
pts = np.array(v)[None, :] | ||
pts = func(pts) | ||
return tuple(pts[0]) | ||
|
||
return circle.revolve(int(m)).warp_batch(func) | ||
# return circle.revolve(int(m)).warp(func_single) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this line the deep copy, or the for loops following?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copy it up front, then use it to update in-place after the warp func. Mainly because it allocates all the right vector sizes in one simple line of code. Are you concerned with the wasted data-copy doing it this way? I could optimize it if you think the complexity is worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine, I guess Emmett is just not sure about where the deep copy comment belongs to (the single line or including the loop after it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it kind of looked like it was just a one-line deep copy followed by a loop deep copy - I'm still not quite sure why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I think I understand now. It's all good.