WPF中点的误差矩阵变换

时间:2018-03-21 15:17:16

标签: c# wpf

我正在尝试转换3D模型,但似乎失败了,我正在尝试研究是什么原因。

我有一段代码,它生成一个3D坐标和一个矩阵,并将它们相乘,然后在WPF中我尝试执行相同的操作,但似乎得到了不同的值。

using System;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;

public partial class CompanyDetails : System.Web.UI.Page
{
    int Companyid = 0;
    string cmdName = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Companyid = Convert.ToInt32(Request.QueryString["CompanyID"]);
            cmdName = Request.QueryString["CommandType"];
            Session["something"] = Companyid;
        }
            if (cmdName == "Details")
            {
                BindTextBoxvalues();
            }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        string x = Session["something"].ToString();
        try
        {
            if (SaveButton.Text == "Save")
            {
                SqlCommand cmd = new SqlCommand();
                String mycon = "Data Source=.; Initial Catalog=something; Integrated Security=True";
                SqlConnection con = new SqlConnection(mycon);
                cmd = new SqlCommand("spInsertCompany", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Name.Text;
                cmd.Parameters.Add("@CompanyCode", SqlDbType.VarChar).Value = CompanyCode.Text;
                cmd.Parameters.Add("@LegalName", SqlDbType.VarChar).Value = LegalName.Text;
                cmd.Parameters.Add("@TaxID", SqlDbType.Int).Value = TaxID.Text;
                cmd.Parameters.Add("@BusinessPhone", SqlDbType.VarChar).Value = BusinessPhone.Text;
                cmd.Parameters.Add("@Extension", SqlDbType.VarChar).Value = Extension.Text;
                cmd.Parameters.Add("@FaxNumber", SqlDbType.VarChar).Value = FaxNumber.Text;
                cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description.Value;
                bool isstatus = IsActiveCheckBox.Checked;
                cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Convert.ToInt32(isstatus);
                con.Open();
                cmd.Connection = con;
                cmd.ExecuteNonQuery();
                Response.Write("<script language='javascript'>window.alert('Saved Successfully.');window.location='Company.aspx';</script>");
            }
            else if (SaveButton.Text == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                String mycon = "Data Source=.; Initial Catalog=something; Integrated Security=True";
                SqlConnection con = new SqlConnection(mycon);
                con.Open();
                cmd = new SqlCommand("spUpdateCompany", con);
                cmd.CommandType = CommandType.StoredProcedure;
                int a = Convert.ToInt32(Companyid);
              // I need the value here but it is being updated to zero here.
                cmd.Parameters.Add("@CompanyID", SqlDbType.Int).Value = Companyid;
                cmd.Parameters.Add("@CompanyName", SqlDbType.VarChar).Value = Name.Text;
                cmd.Parameters.Add("@CompanyCode", SqlDbType.VarChar).Value = CompanyCode.Text;
                cmd.Parameters.Add("@BusinessPhone", SqlDbType.VarChar).Value = BusinessPhone.Text;
                cmd.Parameters.Add("@Extension", SqlDbType.VarChar).Value = Extension.Text;
                cmd.Parameters.Add("@FaxNumber", SqlDbType.VarChar).Value = FaxNumber.Text;
                cmd.Parameters.Add("@TaxID", SqlDbType.Int).Value = TaxID.Text;
                cmd.Parameters.Add("@LegalName", SqlDbType.VarChar).Value = LegalName.Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
                Response.Write("<script language='javascript'>window.alert('Updated Successfully.');window.location='Company.aspx';</script>");
            }
        }
        catch (SqlException ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message",
                "alert('Oops!! following error occured : " + ex.Message.ToString() + "');", true);
        }

    }
    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Company.aspx");
    }
    private void BindTextBoxvalues()
    {
        SaveButton.Text = "Update";
        string constr = "Data Source=.; Initial Catalog=something; Integrated Security=True";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("select * from Company where CompanyID=" + Companyid, con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        Name.Text = dt.Rows[0][1].ToString();
        CompanyCode.Text = dt.Rows[0][2].ToString();
        LegalName.Text = dt.Rows[0][15].ToString();
        TaxID.Text = dt.Rows[0][14].ToString();
        BusinessPhone.Text = dt.Rows[0][3].ToString();
        Extension.Text = dt.Rows[0][13].ToString();
        FaxNumber.Text = dt.Rows[0][12].ToString();
        Description.Value = dt.Rows[0][4].ToString();
        IsActiveCheckBox.Checked = Convert.ToBoolean(dt.Rows[0][11]);
    }
}

B的输出如下:

Point3D a = new Point3D(-0.552284, 0.904515, -2.987232);
var matrix1 = new Matrix3D()
        {
            M11 = 1.660177,
            M22 = 1.660177,
            M33 = 1.660177,
            M14 = 0.989035,
            M24 = -0.743628,
            M34 = 4.506998,
            M44 = 1.0000
        };
Point3D b = matrix1.Transform(a);

然而,当我在一些随机在线计算器上进行矩阵乘法时:

0.0670127994101071
-0.109751653602916
0.362463476775451

    1.660177 0.000000 0.000000 0.989035
    0.000000 1.660177 0.000000 -0.743628
    0.000000 0.000000 1.660177 4.506998
    0.000000 0.000000 0.000000 1.000000

我明白了:

-0.552284
0.904515
-2.987232
1

我做错了吗?这也适用于我尝试移动整个3D模型时使其不可见但尽管它应该是正确的。

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。出于某种原因,命名(至少对我来说)是非常混乱和索引,但基本上必须用相应的OffsetX,OffsetY,OffsetZ替换所有MX4。

Point3D a = new Point3D(-0.552284, 0.904515, -2.987232);
var matrix1 = new Matrix3D()
        {
            M11 = 1.660177,
            M22 = 1.660177,
            M33 = 1.660177,
            OffsetX = 0.989035,
            OffsetY  = -0.743628,
            OffsetZ   = 4.506998,
            M44 = 1.0000
        };
Point3D b = matrix1.Transform(a);