如何查找转换后的 ModelVisual3D 的转换后的 point3d 坐标
我想单击 Viewport3D 中的模型并查找模型的 3D 坐标。我想要转换后的坐标。
假设你在 Viewport3D 中有这个:
<ModelVisual3D x:Name="yellowTriangle">
<ModelVisual3D.Content>
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="-1,0,0 0,1,0 1,0,0" TriangleIndices="0,2,1"/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="10" OffsetY="20" OffsetZ="-10"/>
<ScaleTransform3D ScaleX="1" ScaleY="1.5" ScaleZ="1"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="20" Axis="1 0 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</GeometryModel3D.Transform>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Yellow"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
使用 VisualTreehelper.HitTest 我可以获取 MeshGeometry3D 和 Point3D 位置。但是,我只能获取原始的 Point3D:-1,0,0 等。如何获取转换后的 Point3D?
根据 Ericsink 的 autozoom 评论,我应该:
从每个 MeshGeometry3D 沿着视觉树向上走,并停下来应用沿途找到的每个 Transform >对象
但我没有成功。任何人都有办法访问 MeshGeometry3D 的 ModelVisual3D 吗?
或者有一种更快的方法来达到那些转换后的 Point3D 的?
添加命中测试代码:
private void Viewport3D_MouseDown(object sender, MouseButtonEventArgs e)
{
RayMeshGeometry3DHitTestResult rayMeshResult = (RayMeshGeometry3DHitTestResult)
VisualTreeHelper.HitTest(mainViewport, e.GetPosition(mainViewport));
if (rayMeshResult.MeshHit is MeshGeometry3D)
{
MeshGeometry3D mesh = (MeshGeometry3D)rayMeshResult.MeshHit;
foreach (Point3D point3d in mesh.Positions)
{
MessageBox.Show("original point: " + point3d.ToString());
Point3D p = new Point3D();
// Using the Modelhit now because the meshhit doesn't contain a transform
Transform3D trans = rayMeshResult.ModelHit.Transform;
p = trans.Transform(point3d);
MessageBox.Show("transformed point: " + p.ToString());
}
}
}
我清理了命中测试代码,它与上面的“yellowTriangle”一起使用。该问题仅发生在我基于 Daniel Lehenbauer 扩展 Visual3d 的派生 Rectangle3D 代码中:“http://blogs.msdn.com/danlehen/archive/2005/10/16/481597.aspx"
这样的东西并没有给我正确的转换:
<primitive3D:Rectangle3D>
<primitive3D:Rectangle3D.Material>
<DiffuseMaterial Brush="Green"/>
</primitive3D:Rectangle3D.Material>
<primitive3D:Rectangle3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
<ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="10"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="30" Axis="0 1 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</primitive3D:Rectangle3D.Transform>
</primitive3D:Rectangle3D>
我想我的派生矩形3D 应该有转换的依赖属性?
I want to click on a model in a Viewport3D and find the 3D coordinates of a model. I want the transformed coordinates.
Suppose you have this in a Viewport3D:
<ModelVisual3D x:Name="yellowTriangle">
<ModelVisual3D.Content>
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="-1,0,0 0,1,0 1,0,0" TriangleIndices="0,2,1"/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="10" OffsetY="20" OffsetZ="-10"/>
<ScaleTransform3D ScaleX="1" ScaleY="1.5" ScaleZ="1"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="20" Axis="1 0 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</GeometryModel3D.Transform>
<GeometryModel3D.Material>
<DiffuseMaterial Brush="Yellow"/>
</GeometryModel3D.Material>
</GeometryModel3D>
</ModelVisual3D.Content>
Using a VisualTreehelper.HitTest I can get to the MeshGeometry3D and the Point3D Positions. However, I can only get the original Point3D's : -1,0,0 etc. How do I get the transformed Point3D's ?
According to Ericsink's autozoom remarks I should:
walk up the visual tree from every MeshGeometry3D and stop to apply every Transform >object it finds along the way
But I don't succeed. Anyone has a way to reach to the ModelVisual3D of a MeshGeometry3D ?
Or a faster way to reach those transformed Point3D's ?
Adding the hittest code:
private void Viewport3D_MouseDown(object sender, MouseButtonEventArgs e)
{
RayMeshGeometry3DHitTestResult rayMeshResult = (RayMeshGeometry3DHitTestResult)
VisualTreeHelper.HitTest(mainViewport, e.GetPosition(mainViewport));
if (rayMeshResult.MeshHit is MeshGeometry3D)
{
MeshGeometry3D mesh = (MeshGeometry3D)rayMeshResult.MeshHit;
foreach (Point3D point3d in mesh.Positions)
{
MessageBox.Show("original point: " + point3d.ToString());
Point3D p = new Point3D();
// Using the Modelhit now because the meshhit doesn't contain a transform
Transform3D trans = rayMeshResult.ModelHit.Transform;
p = trans.Transform(point3d);
MessageBox.Show("transformed point: " + p.ToString());
}
}
}
I cleaned up my hittest code and it works with the "yellowTriangle" above. The problem only occurs with my derived Rectangle3D code based on Daniel Lehenbauer's extending Visual3d: "http://blogs.msdn.com/danlehen/archive/2005/10/16/481597.aspx"
Something like this doesn't give me the correct transformations:
<primitive3D:Rectangle3D>
<primitive3D:Rectangle3D.Material>
<DiffuseMaterial Brush="Green"/>
</primitive3D:Rectangle3D.Material>
<primitive3D:Rectangle3D.Transform>
<Transform3DGroup>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
<ScaleTransform3D ScaleX="1" ScaleY="1" ScaleZ="10"/>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D Angle="30" Axis="0 1 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
<TranslateTransform3D OffsetX="0" OffsetY="0" OffsetZ="0"/>
</Transform3DGroup>
</primitive3D:Rectangle3D.Transform>
</primitive3D:Rectangle3D>
I guess my derived rectangle3D should have a DependencyProperty for the Transformations ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会帮助您获得所需的内容 - Transform3DGroup.Transform
它将允许您使用 TransformGroups 变换来变换一个点或一组点,在您的情况下,要使用的 Transform3DGroup 包含在 GeometryModel3D.Transform 属性中。
This may help you get what you want - Transform3DGroup.Transform
It will allow you to transform a point or set of points using the TransformGroups transformation, where in your case the Transform3DGroup to use is contained in the GeometryModel3D.Transform property.
中找到 ModelVisual3D 的 Transformation 矩阵,
在进行 hitTesting 时,您可以在This 对应于 Transforms set on:
中找到另一个变换
并且可以在对应于: :
...
When doing hitTesting, you can find the Transformation matrix of a ModelVisual3D in the
This corresponds with Transforms set on:
And another transform can be found in
corresponding with:
...