Message 1 of 4
Using Matrix Transformation to Map Between Tetrahedrons
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm posting a smaller sample of a larger Add In I'd like to create.
In this example, the script is expecting the user to select a body defined as a unit tetrahedron (i.e. a regular tetrahedron whose points all lie on the surface of a sphere with a radius of 1). Ideally, since it expects the position of these points, it should be able to use a matrix transform to map the body to another arbitrary tetrahedron.
In this script, the four source vertices and the four target vertices are known, and I need to get the transformation matrix that maps the source to the target. Hopefully this is a simple misunderstanding of the matrix algebra needed to solve for the transformation matrix. Any thoughts would be greatly appreciated.
import adsk.core, adsk.fusion, adsk.cam, traceback, pathlib
from math import sqrt
SOURCE_TETRAHEDRON = [
sqrt(8/9), -sqrt(2/9), -sqrt(2/9), 0.0,
0.0 , -sqrt(2/3), sqrt(2/3), 0.0,
-1/3 , -1/3 , -1/3 , 1.0,
1.0 , 1.0 , 1.0 , 1.0,
]
TARGET_TETRAHEDRON = [
2.0, 3.0, 6.0, 4.0,
2.0, 3.0, 0.0, 2.0,
2.0, 3.0, 9.0, 0.0,
1.0, 1.0, 1.0, 1.0,
]
INVERSE_SOURCE = adsk.core.Matrix3D.create()
INVERSE_SOURCE.setWithArray(SOURCE_TETRAHEDRON)
INVERSE_SOURCE.invert()
TRANSFORMATION_MATRIX = adsk.core.Matrix3D.create()
TRANSFORMATION_MATRIX.setWithArray(TARGET_TETRAHEDRON)
TRANSFORMATION_MATRIX.transformBy(INVERSE_SOURCE)
class ScriptConfig:
def __init__(self):
self.pwd = pathlib.Path(__file__).parent.resolve()
self.app = adsk.core.Application.get()
self.ui = self.app.userInterface
self.design = adsk.fusion.Design.cast(self.app.activeProduct)
self.brep_manager = adsk.fusion.TemporaryBRepManager.get()
def run(context):
try:
config = ScriptConfig()
selected_entity = config.ui.selectEntity("Select Tetrahedron", "Bodies").entity
selected_body = adsk.fusion.BRepBody.cast(selected_entity)
new_base_feature = config.design.activeComponent.features.baseFeatures.add()
new_base_feature.name = "TransformTetrahedron"
new_base_feature.startEdit()
new_body = config.brep_manager.copy(selected_body)
config.brep_manager.transform(new_body, TRANSFORMATION_MATRIX)
config.design.activeComponent.bRepBodies.add(new_body, new_base_feature)
new_base_feature.finishEdit()
except:
if config.ui:
config.app.log('Failed:\n{}'.format(traceback.format_exc()))