MathNet Ray3D座標系下轉換

duiduo發表於2021-01-04

將一個Ray3D物件在兩個笛卡爾座標下進行轉換初探。嘗試了用CoordinateSystem.Transform(Ray3D ray) 的方法,但沒有得到期待結果。這款還是覺得現在這塊還有問題。

            CoordinateSystem originCS = new CoordinateSystem();//初始一個笛卡爾座標系
            Console.WriteLine("originCS" + originCS);
            Angle angle = Angle.FromRadians(Math.PI/2);
            CoordinateSystem rotationCS = originCS.RotateCoordSysAroundVector(UnitVector3D.ZAxis, angle);
            Console.WriteLine("orginCS.RotateCoordSysAroundVector: " + rotationCS);
            CoordinateSystem cs3 = rotationCS.OffsetBy(new Vector3D(5, 5, 0)); //轉換後的座標系

//實際是兩個座標系下轉換Matrix
            CoordinateSystem mappingCS3ToOrigin = CoordinateSystem.CreateMappingCoordinateSystem(cs3, originCS);
            CoordinateSystem mappingOriginToCS3 = CoordinateSystem.CreateMappingCoordinateSystem(originCS, cs3);

//點在兩座標系下的轉換
            Point3D por = new Point3D(0, 8, 0);//origin中座標系下一點的座標
            Point3D tcs = por.TransformBy(mappingCS3ToOrigin); //CS下座標     
            Point3D tor = tcs.TransformBy(mappingOriginToCS3); //origin下座標 
            Console.Out.WriteLine("tcs=" + tcs.ToString() + " tor=" + tor.ToString());


//抓換射線
            Ray3D ray = new Ray3D(por, UnitVector3D.Create(-1, 1, 0));//origin
            Console.Out.WriteLine("ray=" + ray);
            Ray3D rayCS = new Ray3D(ray.ThroughPoint.TransformBy(mappingCS3ToOrigin),
                ray.Direction.TransformBy(mappingCS3ToOrigin)); //cs3下了
            Console.Out.WriteLine("rayCS=" + rayCS);
            Ray3D rayback = new Ray3D(rayCS.ThroughPoint.TransformBy(mappingOriginToCS3),
                rayCS.Direction.TransformBy(mappingOriginToCS3));
            Console.Out.WriteLine("rayback=" + rayback);//origin下了


//執行部分結果
tcs=(3, 5, 0) tor=(-8.88178419700125E-16, 8, 0)
ray=ThroughPoint: (0, 8, 0), Direction: (-0.707106781186548, 0.707106781186548, 0)
rayCS=ThroughPoint: (3, 5, 0), Direction: (0.707106781186548, 0.707106781186548, 0)
rayback=ThroughPoint: (-8.88178419700125E-16, 8, 0), Direction: (-0.707106781186548, 0.707106781186548, 0)

 

相關文章