-
Notifications
You must be signed in to change notification settings - Fork 64
Adds nd vector graphic #1034
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
Open
apasarkar
wants to merge
5
commits into
ndwidget
Choose a base branch
from
add_ndvector
base: ndwidget
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adds nd vector graphic #1034
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe96606
Includes nd vector code that works
apasarkar 2b1f79c
Faster position assignment, no more for loop
apasarkar d508a32
Batched computations for vector set function
apasarkar a87ed4c
Formatting updates
apasarkar f49b569
Includes improved annotations and changes ordering of the data slice …
apasarkar 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 hidden or 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 |
|---|---|---|
|
|
@@ -82,11 +82,8 @@ def set_value(self, graphic, value: np.ndarray): | |
| else: | ||
| self._positions[:] = value | ||
|
|
||
| for i in range(self._positions.shape[0]): | ||
| # only need to update the translation vector | ||
| graphic.world_object.instance_buffer.data["matrix"][i][3, 0:3] = ( | ||
| self._positions[i] | ||
| ) | ||
| # Only need to update the translation vector | ||
| graphic.world_object.instance_buffer.data["matrix"][:, 3, 0:3] = self._positions[:] | ||
|
|
||
| graphic.world_object.instance_buffer.update_full() | ||
|
|
||
|
|
@@ -171,15 +168,164 @@ def set_value(self, graphic, value: np.ndarray): | |
| # vector determines the size of the vector | ||
| magnitudes = np.linalg.norm(self._directions, axis=1, ord=2) | ||
|
|
||
| for i in range(self._directions.shape[0]): | ||
| # for i in range(self._directions.shape[0]): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove stale code |
||
| # get quaternion to rotate vector to new direction | ||
| rotation = la.quat_from_vecs(self.init_direction, self._directions[i]) | ||
| # get the new transform | ||
| transform = la.mat_compose(graphic.positions[i], rotation, magnitudes[i]) | ||
| # set the buffer | ||
| graphic.world_object.instance_buffer.data["matrix"][i] = transform.T | ||
| rotation = quat_from_vecs(self.init_direction, self._directions[:]) | ||
| # get the new transform | ||
| transform = mat_compose(graphic.positions[:], rotation, magnitudes[:]) | ||
| # set the buffer | ||
| graphic.world_object.instance_buffer.data["matrix"][:] = transform.transpose(0, 2, 1) | ||
|
|
||
| graphic.world_object.instance_buffer.update_full() | ||
|
|
||
| event = GraphicFeatureEvent(type="directions", info={"value": value}) | ||
| self._call_event_handlers(event) | ||
|
|
||
|
|
||
|
|
||
| def quat_from_vecs(source, target, out=None, dtype=None) -> np.ndarray: | ||
| source = np.asarray(source, dtype=float) | ||
| if source.ndim == 1: | ||
| source = source[None, :] | ||
| target = np.asarray(target, dtype=float) | ||
| if target.ndim == 1: | ||
| target = target[None, :] | ||
|
|
||
| num_vecs = target.shape[0] | ||
| result_shape = (num_vecs, 4) | ||
| if out is None: | ||
| out = np.empty(result_shape, dtype=dtype) | ||
|
|
||
| axis = np.cross(source, target) # (num_pts, 3) | ||
| axis_norm = np.linalg.norm(axis, axis=-1) # (num_pts,) | ||
| angle = np.arctan2(axis_norm, np.sum(source * target, axis=-1)) # (num_pts,) | ||
|
|
||
| # Handle degenerate case: source and target are parallel (axis is zero vector). | ||
| # Pick any axis orthogonal to source as a replacement. | ||
| use_fallback = axis_norm == 0 | ||
| if np.any(use_fallback): | ||
| t = np.broadcast_to(source, (num_vecs, 3))[use_fallback] | ||
|
|
||
| # Better case split: | ||
| y_zero = t[:, 1] == 0 | ||
| z_zero = t[:, 2] == 0 | ||
| neither_zero = ~y_zero & ~z_zero | ||
|
|
||
| fb = np.empty((y_zero.shape[0], 3), dtype=float) | ||
| fb[y_zero] = (0., 1., 0.) | ||
| fb[~y_zero & z_zero] = (0., 0., 1.) | ||
| fb[neither_zero, 0] = 0. | ||
| fb[neither_zero, 1] = -t[neither_zero, 2] | ||
| fb[neither_zero, 2] = t[neither_zero, 1] | ||
|
|
||
| axis[use_fallback] = fb | ||
|
|
||
| return quat_from_axis_angle(axis, angle, out=out) | ||
|
|
||
|
|
||
| def quat_from_axis_angle(axis, angle, out=None, dtype=None) -> np.ndarray: | ||
| """Quaternion from axis-angle pair. | ||
|
|
||
| Create a quaternion representing the rotation of an given angle | ||
| about a given unit vector | ||
|
|
||
| Parameters | ||
| ---------- | ||
| axis : ndarray, [3] | ||
| Unit vector | ||
| angle : number | ||
| The angle (in radians) to rotate about axis | ||
| out : ndarray, optional | ||
| A location into which the result is stored. If provided, it | ||
| must have a shape that the inputs broadcast to. If not provided or | ||
| None, a freshly-allocated array is returned. A tuple must have | ||
| length equal to the number of outputs. | ||
| dtype : data-type, optional | ||
| Overrides the data type of the result. | ||
|
|
||
| Returns | ||
| ------- | ||
| ndarray, [4] | ||
| Quaternion. | ||
| """ | ||
|
|
||
| axis = np.asarray(axis, dtype=float) | ||
| angle = np.asarray(angle, dtype=float) | ||
|
|
||
| if out is None: | ||
| out_shape = np.broadcast_shapes(axis.shape[:-1], angle.shape) | ||
| out = np.empty((*out_shape, 4), dtype=dtype) | ||
|
|
||
| # result should be independent of the length of the given axis | ||
| lengths_shape = (*axis.shape[:-1], 1) | ||
| axis = axis / np.linalg.norm(axis, axis=-1).reshape(lengths_shape) | ||
|
|
||
| out[..., :3] = axis * np.sin(angle / 2).reshape(lengths_shape) | ||
| out[..., 3] = np.cos(angle / 2) | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| def mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None) -> np.ndarray: | ||
| """ | ||
| Compose transformation matrices given translation vectors, quaternions, | ||
| and scaling vectors. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| translation : ndarray, [3] or [num_vectors, 3] | ||
| rotation : ndarray, [4] or [num_vectors, 4] | ||
| scaling : ndarray, [3] or [num_vectors, 3] | ||
|
|
||
| Returns | ||
| ------- | ||
| ndarray, [num_vectors, 4, 4] | ||
| """ | ||
| rotation = np.asarray(rotation, dtype=float) | ||
| translation = np.asarray(translation, dtype=float) | ||
| scaling = np.asarray(scaling, dtype=float) | ||
|
|
||
| if rotation.ndim == 1: | ||
| rotation = rotation[None, :] | ||
| if translation.ndim == 1: | ||
| translation = translation[None, :] | ||
| if scaling.ndim == 0: | ||
| scaling = np.full((1, 3), scaling) | ||
| elif scaling.ndim == 1 and scaling.shape[0] == 3: | ||
| scaling = scaling[None, :] | ||
| elif scaling.ndim == 1: | ||
| scaling = scaling[:, None] * np.ones(3) | ||
|
|
||
| num_vectors = max(rotation.shape[0], translation.shape[0], scaling.shape[0]) | ||
|
|
||
| if out is None: | ||
| out = np.zeros((num_vectors, 4, 4), dtype=dtype) | ||
| else: | ||
| out[..., :, :] = 0 | ||
|
|
||
| x, y, z, w = rotation[:, 0], rotation[:, 1], rotation[:, 2], rotation[:, 3] | ||
|
|
||
| x2, y2, z2 = x + x, y + y, z + z | ||
| xx, xy, xz = x * x2, x * y2, x * z2 | ||
| yy, yz, zz = y * y2, y * z2, z * z2 | ||
| wx, wy, wz = w * x2, w * y2, w * z2 | ||
|
|
||
| sx, sy, sz = scaling[:, 0], scaling[:, 1], scaling[:, 2] | ||
|
|
||
|
|
||
| out[:, 0, 0] = (1 - (yy + zz)) * sx | ||
| out[:, 1, 0] = (xy + wz) * sx | ||
| out[:, 2, 0] = (xz - wy) * sx | ||
|
|
||
| out[:, 0, 1] = (xy - wz) * sy | ||
| out[:, 1, 1] = (1 - (xx + zz)) * sy | ||
| out[:, 2, 1] = (yz + wx) * sy | ||
|
|
||
| out[:, 0, 2] = (xz + wy) * sz | ||
| out[:, 1, 2] = (yz - wx) * sz | ||
| out[:, 2, 2] = (1 - (xx + yy)) * sz | ||
|
|
||
| out[:, 0:3, 3] = translation | ||
| out[:, 3, 3] = 1 | ||
|
|
||
| return out | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.