I’d like to transform or project positions of the polygon points on a plane using simple orthographic projection - for UV mapping and get stucked . The code:

    CPolygonFaceRefArray lPolygons = lPolygonMesh.GetPolygons();

    for( long f=0; f < lPolygons.GetCount(); f++ )
    {
        PolygonFace lFace = lPolygons[ f ];
        CPointRefArray lPoints = lFace.GetPoints();

        Point lPoint1 = lPoints[ 0 ];
        Point lPoint2 = lPoints[ 1 ];
        Point lPoint3 = lPoints[ 2 ];

        CVector3 lPoint1Position = lPoint1.GetPosition();
        CVector3 lPoint2Position = lPoint2.GetPosition();
        CVector3 lPoint3Position = lPoint3.GetPosition();

        _LogValue2( f, L"--------------------------------------" );

        // Vector A->B, that becomes the X axis of the local plane.
        CVector3 lLocalX = lLocalX.Sub( lPoint1Position, lPoint2Position );
        lLocalX.NormalizeInPlace();

        // Vector A->C
        CVector3 lVector13 = lVector13.Sub( lPoint1Position, lPoint3Position );
        lVector13.NormalizeInPlace();

        // Determine the Z vector of the local plane:.
        CVector3 lLocalZ = lLocalZ.Cross( lLocalX, lVector13 );
        lLocalZ.NormalizeInPlace();

        // Determine Y vector of the local plane, assuming that it’s perpendicular to already known X and Z.
        CVector3 lLocalY = lLocalY.Cross( lLocalZ, lLocalX );
        lLocalY.NormalizeInPlace();

        // In result we got three axis of the local plane:
        // lLocalX.Dot( lLocalY ) == lLocalX.Dot( lLocalZ ) ==  lLocalY.Dot( lLocalZ ) == 0;

        /*
        Now use system of equations to determine the positions of all points in reference to the local plane.

        double GetBase(const double& a1, const double& b1, const double& c1, const double& a2, const double& b2, const double& c2, const double& a3, const double& b3, const double& c3 )
        {return (a1*b2*c3)+(a3*b1*c2)+(a2*b3*c1)-((a3*b2*c1)+(a1*b3*c2)+(a2*b1*c3));}

        double GetX(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
        {return ( (d1*b2*c3)+(d2*b1*c2)+(d2*b3*c1)-((d3*b2*c1)+(d1*b3*c2)+(d2*b1*c3)) ) / in_Base;}

        double GetY(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
        {return ( (a1*d2*c3)+(a3*d1*c2)+(a2*d3*c1)-((a3*d2*c1)+(a1*d3*c2)+(a2*b1*c3)) ) / in_Base;}

        double GetZ(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
        {return ( (a1*b2*d3)+(a3*b1*d2)+(a2*b3*d1)-((a3*b2*d1)+(a1*b3*d2)+(a2*b1*d3)) ) / in_Base;}
        */

        double fU; 
        double fV; 
        double fW;
        double fBase;

        // For each point, except the first one (lPoint1Position), which we aleady know.

        for( long p=1; p < lPoints.GetCount(); p++ )
        {
            Point lPoint = lPoints[ p ];
            CVector3 lPointPosition = lPoint.GetPosition();
            CVector3 lPointVector = lPointVector.Sub( lPoint1Position, lPointPosition );

            fBase = GetBase( 
                        lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(),
                        lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(),
                        lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ() ); 

            fU = GetX( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
                       lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
                       lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
                       ,fBase ); 

            fV = GetY( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
                       lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
                       lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
                       ,fBase ); 

            fW = GetZ( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
                       lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
                       lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
                       ,fBase ); 

            _LogRoundValue3( p, fU, fV, fW );
        }

        // Result for a cube with the center in 0,0,0 (global coords) is:
        // INFO : Values: 0, --------------------------------------
        // INFO : 1 = 0, 0, 0
        // INFO : 2 = 0, 1, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 1, --------------------------------------
        // INFO : 1 = 1, 1, 0
        // INFO : 2 = 1, 1, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 2, --------------------------------------
        // INFO : 1 = 1, 0, 0
        // INFO : 2 = 1, 1, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 3, --------------------------------------
        // INFO : 1 = 1, 0, 0
        // INFO : 2 = 1, 1, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 4, --------------------------------------
        // INFO : 1 = 1, 0, 0
        // INFO : 2 = 1, 1, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 5, --------------------------------------
        // INFO : 1 = 0, 0, 0
        // INFO : 2 = 0, 1, 0
        // INFO : 3 = 0, 1, 0

        // and for a rotated cube at the same place:
        // INFO : Values: 0, --------------------------------------
        // INFO : 1 = 0.027506, 0.033672, 0
        // INFO : 2 = 0.487751, 0.969746, 0
        // INFO : 3 = 0.460245, 1, 0
        // INFO : Values: 1, --------------------------------------
        // INFO : 1 = 1.15549, 0.753083, 0
        // INFO : 2 = 1.03428, 1, 0
        // INFO : 3 = -0.121205, 1, 0
        // INFO : Values: 2, --------------------------------------
        // INFO : 1 = 1, 0.04442, 0
        // INFO : 2 = 1, 1.04442, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 3, --------------------------------------
        // INFO : 1 = 1, -0.093859, 0
        // INFO : 2 = 1, 0.906141, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 4, --------------------------------------
        // INFO : 1 = 1, -0.04442, 0
        // INFO : 2 = 1, 0.95558, 0
        // INFO : 3 = 0, 1, 0
        // INFO : Values: 5, --------------------------------------
        // INFO : 1 = 0.027506, 0.09418, 0
        // INFO : 2 = -0.43274, 1.03025, 0
        // INFO : 3 = -0.460245, 1, 0
    }

It’s per poly projection only and I assume - which is not always the case, but it’s not important - that all polygon points are on the same local plane.

I started with determining the local plane which is fine and then using equation system trying to find the coordinates of point on the local plane. The results however is wrong.

The code uses the XSI’ API and the vector class documentation is here:

http://download.autodesk.com/global/docs/softimage2013/en_us/sdkguide/index.html?url=si_cpp/classXSI_1_1MATH_1_1CVector3.html,topicNumber=si_cpp_classXSI_1_1MATH_1_1CVector3_html

  • The first question is: is it a correct approach?
  • If it is, then what’s wrong with it?

…and yes I have read the http://en.wikipedia.org/wiki/3D_projection :)

I'd appreciate any suggestions.

有帮助吗?

解决方案

I'd suggest you compute the mapped coordinates using dot products:

fU = lPointVector.Dot(lLocalX);
fV = lPointVector.Dot(lLocalY);
fW = lPointVector.Dot(lLocalZ); // should be zero if points lie in plane

This will take you from 3D coordinates to 2D, which appears to be what you are asking. If not you should clarify your question.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top