如何找到转换后的ModelVisual3D的变换point3d坐标

时间:2009-09-07 20:30:59

标签: .net wpf 3d transform

我想点击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's autozoom的评论,我应该:

  

从每个MeshGeometry3D向上走,然后停下来应用它沿途找到的每个Transform&gt;对象

但我没有成功。任何人都有办法到达MeshGeometry3D的ModelVisual3D?

或者更快的方式来接触那些转化过的Point3D?


添加hittest代码:

    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应该有转换的DependencyProperty?

2 个答案:

答案 0 :(得分:0)

这可以帮助您获得所需内容 - Transform3DGroup.Transform

它允许您使用TransformGroups变换转换一个点或一组点,在这种情况下,要使用的Transform3DGroup包含在GeometryModel3D.Transform属性中。

答案 1 :(得分:0)

在执行hitTesting时,您可以在

中找到ModelVisual3D的Transformation矩阵
Transform3D transformOnModelVisual3D = rayMeshResult.VisualHit.Transform;

这对应于设置的变换:

<ModelVisual3D x:Name="yellowTriangle">
 <ModelVisual3D.Transform>
  <RotateTransform3D>
    <RotateTransform3D.Rotation>
     <AxisAngleRotation3D Angle="60" Axis="0 1 0"/>
    </RotateTransform3D.Rotation>
  </RotateTransform3D>
 </ModelVisual3D.Transform>
....
</ModelVisual3D>

可以在

中找到另一个转换
Transform3D transformOnGeometryModel3D = rayMeshResult.ModelHit.Transform;

对应于:     ...

<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="-2" OffsetY="1" OffsetZ="-1"/>
   </Transform3DGroup>
  </GeometryModel3D.Transform>
 </GeometryModel3D>
</ModelVisual3D.Content>