simplex-solver 3.2.2__cp39-abi3-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simplex_solver-3.2.2.dist-info/DELVEWHEEL +2 -0
- simplex_solver-3.2.2.dist-info/METADATA +94 -0
- simplex_solver-3.2.2.dist-info/RECORD +84 -0
- simplex_solver-3.2.2.dist-info/WHEEL +5 -0
- simplex_solver.libs/.load-order-simplex_solver-3.2.2 +1 -0
- simplex_solver.libs/msvcp140-a4c2229bdc2a2a630acdc095b4d86008.dll +0 -0
- simplexui/__init__.py +86 -0
- simplexui/_version.py +1 -0
- simplexui/channelBox.py +867 -0
- simplexui/comboCheckDialog.py +396 -0
- simplexui/commands/__init__.py +16 -0
- simplexui/commands/alembicCommon.py +1148 -0
- simplexui/commands/alembic_walker.py +299 -0
- simplexui/commands/applyCorrectives.py +452 -0
- simplexui/commands/correctiveInterface.py +259 -0
- simplexui/commands/expandedExport.py +434 -0
- simplexui/commands/hdf5Convert.py +62 -0
- simplexui/commands/mayaCorrectiveInterface.py +159 -0
- simplexui/commands/mayatonumpy.py +416 -0
- simplexui/commands/mesh.py +1085 -0
- simplexui/commands/poseblendlib.py +168 -0
- simplexui/commands/reorderSimplexPoints.py +102 -0
- simplexui/commands/replaceDefinitionJson.py +74 -0
- simplexui/commands/rigidAlign.py +97 -0
- simplexui/commands/unsubdivide.py +1222 -0
- simplexui/commands/uvTransfer.py +976 -0
- simplexui/dragFilter.py +334 -0
- simplexui/falloffDialog.py +504 -0
- simplexui/img/ChefHead.png +0 -0
- simplexui/img/frozen.png +0 -0
- simplexui/interface/__init__.py +30 -0
- simplexui/interface/dummyInterface.py +1313 -0
- simplexui/interface/mayaInterface.py +2294 -0
- simplexui/interfaceModel.py +655 -0
- simplexui/interfaceModelTrees.py +456 -0
- simplexui/items/__init__.py +50 -0
- simplexui/items/accessor.py +145 -0
- simplexui/items/combo.py +645 -0
- simplexui/items/dragItem.py +44 -0
- simplexui/items/falloff.py +748 -0
- simplexui/items/group.py +267 -0
- simplexui/items/progression.py +613 -0
- simplexui/items/shape.py +384 -0
- simplexui/items/simplex.py +1734 -0
- simplexui/items/slider.py +632 -0
- simplexui/items/stack.py +147 -0
- simplexui/items/traversal.py +780 -0
- simplexui/items/treeItem.py +441 -0
- simplexui/main.pyw +22 -0
- simplexui/menu/__init__.py +73 -0
- simplexui/menu/dummyPlugins/__init__.py +16 -0
- simplexui/menu/genericPlugins/__init__.py +16 -0
- simplexui/menu/genericPlugins/_builtins.py +270 -0
- simplexui/menu/genericPlugins/checkPossibleCombos.py +48 -0
- simplexui/menu/genericPlugins/exportSplit.py +67 -0
- simplexui/menu/genericPlugins/showFalloffs.py +26 -0
- simplexui/menu/genericPlugins/showTraversals.py +26 -0
- simplexui/menu/genericPlugins/simplexUvTransfer.py +106 -0
- simplexui/menu/genericPlugins/unsubdivide.py +72 -0
- simplexui/menu/mayaPlugins/__init__.py +16 -0
- simplexui/menu/mayaPlugins/exportOther.py +53 -0
- simplexui/menu/mayaPlugins/extractProgressives.py +68 -0
- simplexui/menu/mayaPlugins/freezeCombo.py +247 -0
- simplexui/menu/mayaPlugins/generateShapeIncrementals.py +99 -0
- simplexui/menu/mayaPlugins/importObjs.py +199 -0
- simplexui/menu/mayaPlugins/linearizeTraversal.py +60 -0
- simplexui/menu/mayaPlugins/makeShelfBtn.py +67 -0
- simplexui/menu/mayaPlugins/relaxToSelection.py +76 -0
- simplexui/menu/mayaPlugins/reloadDefinition.py +44 -0
- simplexui/menu/mayaPlugins/snapToNeutral.py +71 -0
- simplexui/menu/mayaPlugins/softSelectToCluster.py +201 -0
- simplexui/menu/mayaPlugins/tweakMix.py +136 -0
- simplexui/menu/mayaPlugins/updateRestShape.py +113 -0
- simplexui/pysimplex.lib +0 -0
- simplexui/pysimplex.pyd +0 -0
- simplexui/simplexDialog.py +1547 -0
- simplexui/travCheckDialog.py +419 -0
- simplexui/traversalDialog.py +299 -0
- simplexui/ui/comboCheckDialog.ui +475 -0
- simplexui/ui/falloffDialog.ui +243 -0
- simplexui/ui/simplexDialog.ui +1458 -0
- simplexui/ui/travCheckDialog.ui +488 -0
- simplexui/ui/traversalDialog.ui +532 -0
- simplexui/utils.py +307 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
# I could have used the Scipy rotations library, but it doeosn't deal
|
|
6
|
+
# with ndim arrays of quaternions.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def positive_scalar(q: np.ndarray) -> np.ndarray:
|
|
10
|
+
"""Ensure the scalar value of an array of quaternions is positive"""
|
|
11
|
+
shape = q.shape
|
|
12
|
+
q = q.reshape((-1, 4))
|
|
13
|
+
q[q[:, 3] < 0] *= -1
|
|
14
|
+
return q.reshape(shape)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def outer(q: np.ndarray) -> np.ndarray:
|
|
18
|
+
"""Do the outer product of an array of quaternions with itself"""
|
|
19
|
+
return q[..., None] * q[..., None, :] # broadcast to do the outer products
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def inverse_quats(q: np.ndarray) -> np.ndarray:
|
|
23
|
+
"""Get the inverses of the given unit quaternions"""
|
|
24
|
+
ret = q.copy()
|
|
25
|
+
ret[..., :-1] *= -1
|
|
26
|
+
return ret
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def norm_quats(q: np.ndarray) -> np.ndarray:
|
|
30
|
+
"""Normalize the given quaternions and flip any whose scalar component
|
|
31
|
+
is negative
|
|
32
|
+
"""
|
|
33
|
+
return positive_scalar(q / np.linalg.norm(q, axis=-1, keepdims=True))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def mul_quats(q1, q2):
|
|
37
|
+
"""Quaternion multiplication of two arrays
|
|
38
|
+
|
|
39
|
+
Follows the convention that the right hand is applied "first"
|
|
40
|
+
Like row-major transformation matrices
|
|
41
|
+
"""
|
|
42
|
+
x1, y1, z1, w1 = q1[..., 0], q1[..., 1], q1[..., 2], q1[..., 3]
|
|
43
|
+
x2, y2, z2, w2 = q2[..., 0], q2[..., 1], q2[..., 2], q2[..., 3]
|
|
44
|
+
|
|
45
|
+
w = w2 * w1 - x2 * x1 - y2 * y1 - z2 * z1
|
|
46
|
+
x = w2 * x1 + x2 * w1 + y2 * z1 - z2 * y1
|
|
47
|
+
y = w2 * y1 - x2 * z1 + y2 * w1 + z2 * x1
|
|
48
|
+
z = w2 * z1 + x2 * y1 - y2 * x1 + z2 * w1
|
|
49
|
+
|
|
50
|
+
return np.stack((x, y, z, w), axis=-1)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def quaternion_pow(q: np.ndarray, n: np.ndarray):
|
|
54
|
+
"""Exponentiate unit quaternions
|
|
55
|
+
Could also be thought of as slerping from the unit quaternion to the
|
|
56
|
+
given quaternions
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
q (np.array[..., 4]): An array of quaternions
|
|
60
|
+
n (np.array[..., 0]): An array of weights broadcastable to the shape
|
|
61
|
+
of the quaternion array
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
np.array[..., 4]: An array of exponentiated quaternions
|
|
65
|
+
"""
|
|
66
|
+
w = np.clip(q[..., -1], -1.0, 1.0)
|
|
67
|
+
v = q[..., :-1]
|
|
68
|
+
|
|
69
|
+
theta = np.arccos(w)
|
|
70
|
+
sin_theta = np.sin(theta)
|
|
71
|
+
|
|
72
|
+
# Broadcast n to shape of theta
|
|
73
|
+
n = np.broadcast_to(n, theta.shape)
|
|
74
|
+
|
|
75
|
+
# Scale the rotation angle
|
|
76
|
+
new_theta = n * theta
|
|
77
|
+
new_w = np.cos(new_theta)
|
|
78
|
+
|
|
79
|
+
# Fall back to a linear approximation for small angles
|
|
80
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
81
|
+
scale = np.where(sin_theta > 1e-8, np.sin(new_theta) / sin_theta, n)
|
|
82
|
+
|
|
83
|
+
new_v = v * scale[..., np.newaxis]
|
|
84
|
+
|
|
85
|
+
result = np.concatenate([new_v, new_w[..., np.newaxis]], axis=-1)
|
|
86
|
+
return result
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def sum_weighted_quat_poses(
|
|
90
|
+
restPose: np.ndarray, # (N, 4) float
|
|
91
|
+
targets: np.ndarray, # (T, N, 4) float
|
|
92
|
+
weights: np.ndarray, # T float
|
|
93
|
+
levels: np.ndarray, # T int
|
|
94
|
+
) -> np.ndarray: # (N, 4) float
|
|
95
|
+
"""Perform a "weighted sum" of target poses of quaternions with multiple
|
|
96
|
+
levels. Each level is calculated individually, then multiplied in order
|
|
97
|
+
to get the final output.
|
|
98
|
+
|
|
99
|
+
This assumes Scalar (W) Last formatted quaternions
|
|
100
|
+
|
|
101
|
+
This uses some interesting math that I don't fully understand. But the idea
|
|
102
|
+
came from a NASA paper on the topic.
|
|
103
|
+
|
|
104
|
+
Apparently if you sum a bunch of the outer products of quaternions with
|
|
105
|
+
themselves, and then take the largest eigenvector of the resulting 4x4
|
|
106
|
+
matrix, that eigenvector is (in some sense) the average of those quaternions.
|
|
107
|
+
|
|
108
|
+
The naiive idea of an average is the sum divided by the count. So if you
|
|
109
|
+
multiply an average value by the count, you should get back the sum.
|
|
110
|
+
|
|
111
|
+
So if I take this "average" of quaternions and do a "multiplication" by the
|
|
112
|
+
count, I should get back something that looks like a "sum" of the quaternions
|
|
113
|
+
|
|
114
|
+
And to do "multiplication by the count", I just extrapolate the quaternion
|
|
115
|
+
by slerping it to that count.
|
|
116
|
+
|
|
117
|
+
This may not be "correct", but it sure seems to behave like I want it to.
|
|
118
|
+
And that's all that matters for this application.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
restPose (np.array[N, 4]): An array of quaternions
|
|
122
|
+
targets (np.array[T, N, 4]): An array of target poses of quaternions
|
|
123
|
+
weights (np.array[T]): The weight of each target
|
|
124
|
+
levels (np.array[T]): The level of each target
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
np.array[N, 4]: An array of quaternions
|
|
128
|
+
"""
|
|
129
|
+
targets = norm_quats(targets)
|
|
130
|
+
restPose = norm_quats(restPose)
|
|
131
|
+
|
|
132
|
+
# Get the existing levels, and how many poses are at each level
|
|
133
|
+
exlev, counts = np.unique(levels, return_counts=True)
|
|
134
|
+
s_exlev = np.argsort(exlev)
|
|
135
|
+
exlev = exlev[s_exlev]
|
|
136
|
+
counts = counts[s_exlev]
|
|
137
|
+
|
|
138
|
+
# Get the targets in the space of the rest pose
|
|
139
|
+
restInv = inverse_quats(restPose)
|
|
140
|
+
relTargets = mul_quats(targets, restInv[None])
|
|
141
|
+
|
|
142
|
+
# Apply the weights to all the targets
|
|
143
|
+
slerped = quaternion_pow(relTargets, weights[..., None])
|
|
144
|
+
|
|
145
|
+
# Get the outer product of all the quaternions
|
|
146
|
+
# and sum them by level
|
|
147
|
+
outs = outer(slerped)
|
|
148
|
+
levelEigs = np.zeros((exlev[-1] + 1, len(restPose), 4, 4))
|
|
149
|
+
np.add.at(levelEigs, levels, outs)
|
|
150
|
+
levelEigs = levelEigs[exlev]
|
|
151
|
+
|
|
152
|
+
# Get largest eigenvalue for each matrix
|
|
153
|
+
# This represents the *average* of each level
|
|
154
|
+
evals, evecs = np.linalg.eig(levelEigs)
|
|
155
|
+
mvals = np.argmax(np.abs(evals), axis=-1)[..., None, None]
|
|
156
|
+
averages = np.take_along_axis(evecs, mvals, axis=evecs.ndim - 1).squeeze(axis=-1)
|
|
157
|
+
averages = norm_quats(averages)
|
|
158
|
+
|
|
159
|
+
# Take each level's pose to the power of its count
|
|
160
|
+
sums = quaternion_pow(averages, counts[:, None])
|
|
161
|
+
sums = norm_quats(sums)
|
|
162
|
+
|
|
163
|
+
# Finally multiply the poses together on top of the rest pose
|
|
164
|
+
qret = restPose.copy()
|
|
165
|
+
for x in sums:
|
|
166
|
+
qret = mul_quats(x, qret)
|
|
167
|
+
|
|
168
|
+
return qret
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Copyright 2016, Blur Studio
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Simplex.
|
|
4
|
+
#
|
|
5
|
+
# Simplex is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Simplex is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with Simplex. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
"""Transfer shapes between mismatched models
|
|
19
|
+
|
|
20
|
+
Given a 1:1 point correspondence, transfer the shapes from one
|
|
21
|
+
geometry to another. Figuring out the correspondence is currently
|
|
22
|
+
outside the scope of this tool, though I may release one later.
|
|
23
|
+
|
|
24
|
+
The point correspondence should look like an unordered range, and
|
|
25
|
+
will be used as a numpy index to get the output values. It's also
|
|
26
|
+
possible to invert the range if you think you've got it backwards
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import json
|
|
32
|
+
|
|
33
|
+
import numpy as np
|
|
34
|
+
|
|
35
|
+
from .alembicCommon import buildSmpx, readSmpx
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def reorderSimplexPoints(
|
|
39
|
+
sourcePath: str, matchPath: str, outPath: str, invertMatch=False
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Transfer shape data from the sourcePath using the numpy int array
|
|
42
|
+
at matchPath to make the final output at outPath
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
sourcePath : str
|
|
47
|
+
The source .smpx file path
|
|
48
|
+
matchPath : str
|
|
49
|
+
The new vert order in numpy, or json format. The data should be an Nx2 array
|
|
50
|
+
of integers
|
|
51
|
+
outPath : str
|
|
52
|
+
The new output .smpx path
|
|
53
|
+
invertMatch : bool
|
|
54
|
+
Whether to directly apply the match from matchPath, or whether to invert it
|
|
55
|
+
|
|
56
|
+
Returns
|
|
57
|
+
-------
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
jsString, counts, verts, faces, uvs, uvFaces = readSmpx(sourcePath)
|
|
61
|
+
|
|
62
|
+
js = json.loads(jsString)
|
|
63
|
+
name = js["systemName"]
|
|
64
|
+
|
|
65
|
+
print("Loading Correspondence")
|
|
66
|
+
if matchPath.endswith(".json"):
|
|
67
|
+
with open(matchPath, "r") as f:
|
|
68
|
+
c = json.load(f)
|
|
69
|
+
c = np.array(c)
|
|
70
|
+
else:
|
|
71
|
+
c = np.load(matchPath)
|
|
72
|
+
|
|
73
|
+
c = c[c[:, 0].argsort()].T[1]
|
|
74
|
+
ci = c.argsort()
|
|
75
|
+
if invertMatch:
|
|
76
|
+
ci, c = c, ci
|
|
77
|
+
|
|
78
|
+
print("Reordering")
|
|
79
|
+
verts = verts[:, c, :]
|
|
80
|
+
faces = ci[faces]
|
|
81
|
+
|
|
82
|
+
buildSmpx(
|
|
83
|
+
outPath,
|
|
84
|
+
verts,
|
|
85
|
+
faces,
|
|
86
|
+
jsString,
|
|
87
|
+
name,
|
|
88
|
+
faceCounts=counts,
|
|
89
|
+
uvs=uvs,
|
|
90
|
+
uvFaces=uvFaces,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if __name__ == "__main__":
|
|
95
|
+
import os
|
|
96
|
+
|
|
97
|
+
base = r"K:\Departments\CharacterModeling\Library\Head\MaleHead_Standard\005"
|
|
98
|
+
_sourcePath = os.path.join(base, "HeadMaleStandard_High_Split_BadOrder.smpx")
|
|
99
|
+
_matchPath = os.path.join(base, "Reorder.np")
|
|
100
|
+
_outPath = os.path.join(base, "HeadMaleStandard_High_Split2.smpx")
|
|
101
|
+
|
|
102
|
+
reorderSimplexPoints(_sourcePath, _matchPath, _outPath)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Copyright 2016, Blur Studio
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Simplex.
|
|
4
|
+
#
|
|
5
|
+
# Simplex is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Simplex is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with Simplex. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
from .alembic_walker import AlembicWalker
|
|
20
|
+
|
|
21
|
+
from typing import TYPE_CHECKING
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from alembic.Abc import IProperty, OProperty
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ReplaceSmpxAttr(AlembicWalker):
|
|
28
|
+
@classmethod
|
|
29
|
+
def copy_property_data(
|
|
30
|
+
cls,
|
|
31
|
+
inProp: IProperty,
|
|
32
|
+
outProp: OProperty,
|
|
33
|
+
name: list[str],
|
|
34
|
+
objDepth: int,
|
|
35
|
+
propDepth: int,
|
|
36
|
+
*args,
|
|
37
|
+
**kwargs,
|
|
38
|
+
) -> None:
|
|
39
|
+
"""Copy the data from an input property to an output property
|
|
40
|
+
|
|
41
|
+
Override this method if you need to change the data that gets stored
|
|
42
|
+
on a specific property
|
|
43
|
+
|
|
44
|
+
Arguments:
|
|
45
|
+
inProp (iProperty): The input property to copy data from
|
|
46
|
+
outProp (oProperty): The output property to copy data to
|
|
47
|
+
name (list): The full path-name of the property being copied
|
|
48
|
+
objDepth (int): The depth in the *object* hierarchy that the parent
|
|
49
|
+
object of this property is
|
|
50
|
+
propDepth (int): The depth of the *property* hierarchy that this
|
|
51
|
+
property is
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
if name[-1] == 'simplex' and 'newjs' in kwargs:
|
|
55
|
+
if not inProp.isCompound():
|
|
56
|
+
AlembicWalker.copy_time_sampling(inProp, outProp, *args, **kwargs)
|
|
57
|
+
for _ in inProp.samples:
|
|
58
|
+
outProp.setValue(kwargs['newjs'])
|
|
59
|
+
else:
|
|
60
|
+
AlembicWalker.copy_property_data(
|
|
61
|
+
inProp, outProp, name, objDepth, propDepth, *args, **kwargs
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def replaceDefinitionJson(inSmpxPath: str, outSmpxPath: str, newJsonString: str):
|
|
66
|
+
"""Given a simplex file, make a copy of that file with the new json definition string
|
|
67
|
+
This is good for doing manual renames and edits of the json
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
inSmpxPath (str): The filepath to the existing simplex
|
|
71
|
+
outSmpxPath (str): The filepath save the new simplex
|
|
72
|
+
newJsonString (str): The json string that will replace the old one
|
|
73
|
+
"""
|
|
74
|
+
ReplaceSmpxAttr.copy_alembic(inSmpxPath, outSmpxPath, newjs=newJsonString)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Copyright 2016, Blur Studio
|
|
2
|
+
#
|
|
3
|
+
# This file is part of Simplex.
|
|
4
|
+
#
|
|
5
|
+
# Simplex is free software: you can redistribute it and/or modify
|
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
# (at your option) any later version.
|
|
9
|
+
#
|
|
10
|
+
# Simplex is distributed in the hope that it will be useful,
|
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
# GNU Lesser General Public License for more details.
|
|
14
|
+
#
|
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
# along with Simplex. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def rigidAlign(P, Q, iters: int = 10):
|
|
24
|
+
"""Rigidly align meshes with matching vert order by a least-squares error.
|
|
25
|
+
Uses a variation of an algorithm by Umeyama
|
|
26
|
+
Relevant links:
|
|
27
|
+
* https://gist.github.com/nh2/bc4e2981b0e213fefd4aaa33edfb3893 (this code)
|
|
28
|
+
* http://stackoverflow.com/a/32244818/263061 (solution with scale)
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
P : np.array
|
|
33
|
+
Static set of points
|
|
34
|
+
Q : np.array
|
|
35
|
+
Points to align with non-uniform scale
|
|
36
|
+
iters : int
|
|
37
|
+
The number of iterations (Defaults to 10)
|
|
38
|
+
|
|
39
|
+
Returns
|
|
40
|
+
-------
|
|
41
|
+
: np.array
|
|
42
|
+
The 4x4 transformation matrix that most closely aligns Q to P
|
|
43
|
+
"""
|
|
44
|
+
assert P.shape == Q.shape
|
|
45
|
+
|
|
46
|
+
n, dim = P.shape
|
|
47
|
+
assert dim == 3
|
|
48
|
+
|
|
49
|
+
if iters <= 1:
|
|
50
|
+
raise ValueError("Must run at least 1 iteration")
|
|
51
|
+
|
|
52
|
+
# Get the centroid of each object
|
|
53
|
+
Qm = Q.mean(axis=0)
|
|
54
|
+
Pm = P.mean(axis=0)
|
|
55
|
+
|
|
56
|
+
# Subtract out the centroid to get the basic aligned mesh
|
|
57
|
+
cP = P - Pm # centeredP
|
|
58
|
+
cQRaw = Q - Qm # centeredQ
|
|
59
|
+
|
|
60
|
+
cQ = cQRaw.copy()
|
|
61
|
+
cumulation = np.eye(3) # build an accumulator for the rotation
|
|
62
|
+
|
|
63
|
+
# Here, we find an approximate rotation and scaling, but only
|
|
64
|
+
# keep track of the accumulated rotations.
|
|
65
|
+
# Then we apply the non-uniform scale by comparing bounding boxes
|
|
66
|
+
# This way we don't get any shear in our matrix, and we relatively
|
|
67
|
+
# quickly walk our way towards a minimum
|
|
68
|
+
for _ in range(iters):
|
|
69
|
+
# Magic?
|
|
70
|
+
C = np.dot(cP.T, cQ) / n
|
|
71
|
+
V, S, W = np.linalg.svd(C)
|
|
72
|
+
|
|
73
|
+
# Handle negative scaling
|
|
74
|
+
d = (np.linalg.det(V) * np.linalg.det(W)) < 0.0
|
|
75
|
+
if d:
|
|
76
|
+
S[-1] = -S[-1]
|
|
77
|
+
V[:, -1] = -V[:, -1]
|
|
78
|
+
|
|
79
|
+
# build the rotation matrix for this iteration
|
|
80
|
+
# and add it to the accumulation
|
|
81
|
+
R = np.dot(V, W)
|
|
82
|
+
cumulation = np.dot(cumulation, R.T)
|
|
83
|
+
|
|
84
|
+
# Now apply the accumulated rotation to the raw point positions
|
|
85
|
+
# Then grab the non-uniform scaling from the bounding box
|
|
86
|
+
# And set up cQ for the next iteration
|
|
87
|
+
cQ = np.dot(cQRaw, cumulation)
|
|
88
|
+
sf = (cP.max(axis=0) - cP.min(axis=0)) / (cQ.max(axis=0) - cQ.min(axis=0))
|
|
89
|
+
cQ = cQ * sf
|
|
90
|
+
|
|
91
|
+
# Build the final transformation
|
|
92
|
+
csf = cumulation * sf
|
|
93
|
+
tran = Pm - Qm.dot(csf)
|
|
94
|
+
outMat = np.eye(4)
|
|
95
|
+
outMat[:3, :3] = csf
|
|
96
|
+
outMat[3, :3] = tran
|
|
97
|
+
return outMat
|