public bool DrawPolygon(IRaster pRaster, ExOriPara pExOriPara, InOriPara pInOriPara, IFeatureClass pFeatureClass, int nID) { try { Point2D[,] ptResult = null; ClsGetCameraView CGetCameraView = new ClsGetCameraView(); if (CGetCameraView.ImageReprojectionRange(pRaster, out ptResult, pExOriPara, pInOriPara, 50)) { IPointCollection pPointCollection = new PolygonClass(); for (int j = 0; j < ptResult.GetLength(1); j++) { if (ptResult[0, j] == null) { continue; } IPoint pPoint1 = new PointClass(); pPoint1.X = ptResult[0, j].X; pPoint1.Y = ptResult[0, j].Y; pPointCollection.AddPoint(pPoint1); } for (int k = ptResult.GetLength(1) - 1; k >= 0; k--) { if (ptResult[1, k] == null) { continue; } IPoint pPoint2 = new PointClass(); pPoint2.X = ptResult[1, k].X; pPoint2.Y = ptResult[1, k].Y; pPointCollection.AddPoint(pPoint2); } IFeature pFeatureTemp = pFeatureClass.CreateFeature(); IPolygon pPolygon = new PolygonClass(); pPolygon = pPointCollection as IPolygon; pPolygon.Close(); pFeatureTemp.Shape = pPolygon as IGeometry; pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("PointID"), nID); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("X"), pExOriPara.pos.X); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("Y"), pExOriPara.pos.Y); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("Z"), pExOriPara.pos.Z); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("omg"), pExOriPara.ori.omg); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("phi"), pExOriPara.ori.phi); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("kap"), pExOriPara.ori.kap); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("width"), pInOriPara.nW); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("height"), pInOriPara.nH); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("fx"), pInOriPara.dfx); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("fy"), pInOriPara.dfy); pFeatureTemp.set_Value(pFeatureTemp.Fields.FindField("f"), pInOriPara.df); pFeatureTemp.Store(); } return(true); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } }
private void drawRaster(IRaster pRaster, ExOriPara pExOriPara, InOriPara pInOriPara) { IRaster pRasterResult; IRasterLayer pRasterLayer = new RasterLayerClass(); ClsGetCameraView CGetCameraView = new ClsGetCameraView(); if (CGetCameraView.ImageReprojectionRange(pRaster, out pRasterResult, pExOriPara, pInOriPara.df, pInOriPara.dx, pInOriPara.dy, pInOriPara.nW, pInOriPara.nH, 50)) { pRasterLayer.CreateFromRaster(pRasterResult); pMapControl.AddLayer(pRasterLayer as ILayer); pMapControl.Refresh(); } }
public bool GetCamOri(Ex_OriPara exOriCamL, IFeatureClass pFeatureClass, int nID) { try { ExOriPara pExOriPara = new ExOriPara(); InOriPara pInOriPara = new InOriPara(); pExOriPara.ori.omg = exOriCamL.ori.omg; pExOriPara.ori.phi = exOriCamL.ori.phi; pExOriPara.ori.kap = exOriCamL.ori.kap; pExOriPara.pos.X = exOriCamL.pos.X; pExOriPara.pos.Y = exOriCamL.pos.Y; pExOriPara.pos.Z = exOriCamL.pos.Z; pInOriPara.df = 1226.23; pInOriPara.dfx = 512; pInOriPara.dfy = 512; pInOriPara.nW = 1024; pInOriPara.nH = 1024; //pInOriPara.dfx = 50; //pInOriPara.dfy = 50; //pInOriPara.nW = 100; //pInOriPara.nH = 100; //pInOriPara.df = CView.PointInformationList[i].f; //pInOriPara.dfx = CView.PointInformationList[i].fx; //pInOriPara.dfy = CView.PointInformationList[i].fy; //pInOriPara.nW = CView.PointInformationList[i].width; //pInOriPara.nH = CView.PointInformationList[i].height; /////////////////////////////////////////////////////////// //非逐点计算 //DrawPolygon(pRasterDem, pExOriPara, pInOriPara, pFeatureClass, nID); //////////////////////////////////////////////////////////////// //逐点计算 drawRaster(pRasterDem, pExOriPara, pInOriPara); } catch (SystemException e) { return(false); } return(true); }
public bool ImageReprojectionRange(IRaster pSrcRaster, out Point2D[,] ptResult, ExOriPara exori, InOriPara pInOri, int nGeoRange) { double dbFocus = pInOri.df; double fx = pInOri.dfx; double fy = pInOri.dfy; int nImgWidth = pInOri.nW; int nImgHeight = pInOri.nH; ptResult = null; Pt2i ptSubImgLeftTop = new Pt2i(); double[,] dbSubDemData = getSubDem(pSrcRaster, exori.pos, nGeoRange, ref ptSubImgLeftTop); if (dbSubDemData == null) { return(false); } IRasterProps pProps = pSrcRaster as IRasterProps; int nDemHeight = pProps.Height; int nDemWidth = pProps.Width; IRaster2 pRaster2 = pSrcRaster as IRaster2; double dbNoDataValue = getNoDataValue(pProps.NoDataValue); //object pNodata = pProps.NoDataValue; //double value=((double[])pNodata)[0]; //double dbNoDataValue =double.Parse((float[]pNodata)[0].ToString()); //double dbNoDataValue = Convert.ToDouble(((double[]))[0]); //object dbNoDataValue = Convert.ToDouble(pProps.NoDataValue.ToString()); //object nodatavalue = pProps.NoDataValue; //Type type=pProps.NoDataValue.GetType(); //double dbNoDataValue = type.IsArray ? nodatavalue[0] as double : nodatavalue as double; //初始步长 double dbInitialStep = 0; int nGridW = nDemWidth / 10; int nGridH = nDemHeight / 10; int nCount = 0; double dbHeightAvg = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { double dDemZ = double.NaN; double dbGeoX = pRaster2.ToMapX(i * nGridW); double dbGeoY = pRaster2.ToMapY(j * nGridH); if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, dbGeoX, dbGeoY, ref dDemZ) && Math.Abs(dDemZ - dbNoDataValue) >= 10e-10) { nCount++; dbHeightAvg += dDemZ; } } } if (nCount != 0) { dbHeightAvg /= nCount; } double dbCurCameraZ = 0; //IRaster2 pRaster2 = pSrcRaster as IRaster2; //int nCameraImgX = pRaster2.ToPixelColumn(exori.pos.X);//dXCumulate - pDem.m_PtOrigin.X) / dXRdem; //int nCameraImgY = pRaster2.ToPixelRow(exori.pos.Y); // (dYCumulate - pDem.m_PtOrigin.Y) / dYRdem; //if (nCameraImgY >= 0 || nCameraImgY < nDemHeight || nCameraImgX >= 0 || nCameraImgX < nDemWidth) //{ if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, exori.pos.X, exori.pos.Y, ref dbCurCameraZ) && Math.Abs(dbCurCameraZ - dbNoDataValue) >= 10e-10) { dbHeightAvg = dbCurCameraZ; } //} dbInitialStep = Math.Abs(dbHeightAvg - exori.pos.Z) / 2; Point2D ptLeftTop = new Point2D(0, 0); double[] dbResolution = new double[2]; dbResolution[0] = pProps.MeanCellSize().X; dbResolution[1] = pProps.MeanCellSize().Y; int[] nSize = new int[2]; nSize[0] = nDemWidth; nSize[1] = nDemHeight; //pDstRaster = CreateRaster(ptLeftTop, dbResolution, nSize); //pDstRaster = createRasterWithoutData(ptLeftTop, dbResolution[0], nSize, "testOutput.tif"); // byte[,] dbData = new byte[nDemWidth, nDemHeight]; //double demGrayVal=double.NaN; //Pt3d demXYZ; //byte byteGray; //byte bt = (byte)255; //Pt2d pt; Matrix matA, matB; matA = new Matrix(2, 2); matB = new Matrix(2, 1); Matrix matR; matR = new Matrix(3, 3); //Matrix matX; double[] a = new double[4]; double[] b = new double[2]; OPK2RMat(exori.ori, ref matR); //求解相关系数 double a1 = matR.getNum(0, 0); double a2 = matR.getNum(0, 1); double a3 = matR.getNum(0, 2); double b1 = matR.getNum(1, 0); double b2 = matR.getNum(1, 1); double b3 = matR.getNum(1, 2); double c1 = matR.getNum(2, 0); double c2 = matR.getNum(2, 1); double c3 = matR.getNum(2, 2); double[] dbCoef = new double[] { a1, a2, a3, b1, b2, b3, c1, c2, c3 }; //double X, Y, Z, Z1; // Z = 0.0; //double dthreshold = 0.01; //for (int i = 0; i < nDemWidth; i++) //{ // for (int j = 0; j < nDemHeight; j++) // { // dbData[i, j] = bt; // } //} ptResult = new Point2D[2, nImgWidth]; int nSearchStep = 50; for (int j = 0; j < nImgWidth + nSearchStep - 1; j += nSearchStep) { if (j >= nImgWidth) { j = nImgWidth - 1; } //最下面的点 Point2D ptBottom = null, ptTop = null; Point2D ptLastTime = null; if (getPixelIsCoverd(pSrcRaster, dbSubDemData, ptSubImgLeftTop, j, nImgHeight - 1, out ptBottom, exori, dbCoef, dbFocus, fx, fy, dbInitialStep)) { int nposy = 0; int nStep = nImgHeight - 1; while (nStep > 1 && nposy >= 0 && nposy < nImgHeight) { nStep /= 2; if (getPixelIsCoverd(pSrcRaster, dbSubDemData, ptSubImgLeftTop, j, nposy, out ptTop, exori, dbCoef, dbFocus, fx, fy, dbInitialStep)) { if (ptLastTime == null) { ptLastTime = new Point2D(); } ptLastTime.X = ptTop.X; ptLastTime.Y = ptTop.Y; nposy -= nStep; } else { nposy += nStep; } } } //加入链表 ptResult[0, j] = ptLastTime; ptResult[1, j] = ptBottom; } //write block data //if (!WriteToRaster(pDstRaster, dbData, ptLeftTop)) // return false; //if (!writeBlockDataToFile(ptLeftTop, dbData, nSize, pDstRaster)) // return false; return(true); }
//计算某个像素点是否可见,只适用于东北天坐标,即Z轴向上 private bool getPixelIsCoverd(IRaster pRaster, double[,] dbSubDemData, Pt2i ptSubImgLeftTop, int posx, int posy, out Point2D ptGeoCovered, ExOriPara exOriPara, double[] dbCoef, double dbFocus, double fx, double fy, double dbInitialStep) { ptGeoCovered = null; if (pRaster == null) { return(false); } bool bFlag = false; IRasterProps pProps = pRaster as IRasterProps; int nDemHeight = pProps.Height; int nDemWidth = pProps.Width; double dbNoDataValue = getNoDataValue(pProps.NoDataValue);// Convert.ToDouble(((object[])pProps.NoDataValue)[0]); double xPix, yPix; xPix = posx - fx; // nImgWidth / 2; //行列号转摄影测量像平面坐标 yPix = fy - posy; //nImgHeight / 2 double dVecX = dbCoef[0] * xPix + dbCoef[1] * yPix - dbCoef[2] * dbFocus; double dVecY = dbCoef[3] * xPix + dbCoef[4] * yPix - dbCoef[5] * dbFocus; double dVecZ = dbCoef[6] * xPix + dbCoef[7] * yPix - dbCoef[8] * dbFocus; double dVecDis = Math.Sqrt(dVecX * dVecX + dVecY * dVecY + dVecZ * dVecZ); double dRatioX = dVecX / dVecDis; double dRatioY = dVecY / dVecDis; double dRatioZ = dVecZ / dVecDis; //迭代步长 double dDemZ = 0.0; double dXCumulate = exOriPara.pos.X; double dYCumulate = exOriPara.pos.Y; double dZCumulate = exOriPara.pos.Z; double dStep = dbInitialStep; int nDemW = 0; int nDemH = 0; #region int nIter = 0; while (true) { nIter++; #region 如果迭代次数太多,则取最后一次的结果,避免无限循环 if (nIter > 10000) { if (true == GetGeoZ(pRaster, dbSubDemData, ptSubImgLeftTop, dXCumulate, dYCumulate, ref dDemZ)) { ptGeoCovered = new Point2D(); ptGeoCovered.X = dXCumulate; ptGeoCovered.Y = dYCumulate; bFlag = true; } break; } #endregion #region 由于存在“回头探测”的现象,因此要判断是否已经过头,此判断仅适用于Z值朝上(东北天坐标)的DEM if ((dXCumulate - exOriPara.pos.X) / dRatioX < 0) { break; } #endregion //正常迭代 dXCumulate = dXCumulate + dStep * dRatioX; dYCumulate = dYCumulate + dStep * dRatioY; dZCumulate = dZCumulate + dStep * dRatioZ; IRaster2 pRaster2 = pRaster as IRaster2; nDemW = pRaster2.ToPixelColumn(dXCumulate); //dXCumulate - pDem.m_PtOrigin.X) / dXRdem; nDemH = pRaster2.ToPixelRow(dYCumulate); // (dYCumulate - pDem.m_PtOrigin.Y) / dYRdem; if (nDemH < 0 || nDemH >= nDemHeight || nDemW < 0 || nDemW >= nDemWidth) { break; } if (false == GetGeoZ(pRaster, dbSubDemData, ptSubImgLeftTop, dXCumulate, dYCumulate, ref dDemZ)) { continue; } if (Math.Abs(dDemZ - dbNoDataValue) <= 10e-10) { //“回头探测”时遇到无效值,认为可以终止 if (dStep < 0) { break; } else { continue; } } //if (Math.Abs(dDemZ - dbNoDataValue) < 1) //{ // continue; //} //double dZMinus = dDemZ - dZCumulate; double dZMinus = dZCumulate - dDemZ; //表示已经到了地形点上 if (Math.Abs(dZMinus) < 0.01) { if (true == GetGeoZ(pRaster, dbSubDemData, ptSubImgLeftTop, dXCumulate, dYCumulate, ref dDemZ)) { ptGeoCovered = new Point2D(); ptGeoCovered.X = dXCumulate; ptGeoCovered.Y = dYCumulate; bFlag = true; } break; } //表示已经穿过DEM,需要进行“回头探测” if (dZMinus < 0) { dStep = -Math.Abs(0.1 * dZMinus); } else { dStep = Math.Abs(dStep); } //if (Math.Abs(dZMinus) < 0.5) //{ // dStep = 0.3; //} //if (Math.Abs(dZMinus) < 0.2) //{ // dStep = 0.1; //} //if (Math.Abs(dZMinus) < 0.1) //{ // dStep = 0.03; //} //if (Math.Abs(dZMinus) < 0.05) //{ // if (false == GetGeoZ(pRaster, dbSubDemData, ptSubImgLeftTop, dXCumulate, dYCumulate, ref dDemZ)) // { // break; // } // ptGeoCovered = new Point2D(); // ptGeoCovered.X = dXCumulate; // ptGeoCovered.Y = dYCumulate; // bFlag = true; // break; //} } #endregion return(bFlag); }
//逐点算法 public bool ImageReprojectionRange(IRaster pSrcRaster, out IRaster pDstRaster, ExOriPara exori, double dbFocus, double fx, double fy, int nImgWidth, int nImgHeight, int nGeoRange) { pDstRaster = null; Pt2i ptSubImgLeftTop = new Pt2i(); double[,] dbSubDemData = getSubDem(pSrcRaster, exori.pos, nGeoRange, ref ptSubImgLeftTop); if (dbSubDemData == null) { return(false); } IRasterProps pProps = pSrcRaster as IRasterProps; int nDemHeight = pProps.Height; int nDemWidth = pProps.Width; IRaster2 pRaster2 = pSrcRaster as IRaster2; double dbNoDataValue = getNoDataValue(pProps.NoDataValue); //object pNodata = pProps.NoDataValue; //double value=((double[])pNodata)[0]; //double dbNoDataValue =double.Parse((float[]pNodata)[0].ToString()); //double dbNoDataValue = Convert.ToDouble(((double[]))[0]); //object dbNoDataValue = Convert.ToDouble(pProps.NoDataValue.ToString()); //object nodatavalue = pProps.NoDataValue; //Type type=pProps.NoDataValue.GetType(); //double dbNoDataValue = type.IsArray ? nodatavalue[0] as double : nodatavalue as double; //初始步长 double dbInitialStep = 0; int nGridW = nDemWidth / 10; int nGridH = nDemHeight / 10; int nCount = 0; double dbHeightAvg = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { double dDemZ = double.NaN; double dbGeoX = pRaster2.ToMapX(i * nGridW); double dbGeoY = pRaster2.ToMapY(j * nGridH); if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, dbGeoX, dbGeoY, ref dDemZ) && Math.Abs(dDemZ - dbNoDataValue) >= 10e-10) { nCount++; dbHeightAvg += dDemZ; } } } if (nCount != 0) { dbHeightAvg /= nCount; } else { return(false); } double dbCurCameraZ = 0; //IRaster2 pRaster2 = pSrcRaster as IRaster2; //int nCameraImgX = pRaster2.ToPixelColumn(exori.pos.X);//dXCumulate - pDem.m_PtOrigin.X) / dXRdem; //int nCameraImgY = pRaster2.ToPixelRow(exori.pos.Y); // (dYCumulate - pDem.m_PtOrigin.Y) / dYRdem; //if (nCameraImgY >= 0 || nCameraImgY < nDemHeight || nCameraImgX >= 0 || nCameraImgX < nDemWidth) //{ if (GetGeoZ(pSrcRaster, dbSubDemData, ptSubImgLeftTop, exori.pos.X, exori.pos.Y, ref dbCurCameraZ) && Math.Abs(dbCurCameraZ - dbNoDataValue) >= 10e-10) { dbHeightAvg = dbCurCameraZ; } //} dbInitialStep = Math.Abs(dbHeightAvg - exori.pos.Z) / 2; Point2D ptLeftTop = new Point2D(0, 0); double[] dbResolution = new double[2]; dbResolution[0] = pProps.MeanCellSize().X; dbResolution[1] = pProps.MeanCellSize().Y; int[] nSize = new int[2]; nSize[0] = nDemWidth; nSize[1] = nDemHeight; //pDstRaster = CreateRaster(ptLeftTop, dbResolution, nSize); //pDstRaster = createRasterWithoutData(ptLeftTop, dbResolution[0], nSize, "testOutput.tif"); // byte[,] dbData = new byte[nDemWidth, nDemHeight]; //double demGrayVal=double.NaN; //Pt3d demXYZ; //byte byteGray; //byte bt = (byte)255; //Pt2d pt; Matrix matA, matB; matA = new Matrix(2, 2); matB = new Matrix(2, 1); Matrix matR; matR = new Matrix(3, 3); //Matrix matX; double[] a = new double[4]; double[] b = new double[2]; OPK2RMat(exori.ori, ref matR); //求解相关系数 double a1 = matR.getNum(0, 0); double a2 = matR.getNum(0, 1); double a3 = matR.getNum(0, 2); double b1 = matR.getNum(1, 0); double b2 = matR.getNum(1, 1); double b3 = matR.getNum(1, 2); double c1 = matR.getNum(2, 0); double c2 = matR.getNum(2, 1); double c3 = matR.getNum(2, 2); double[] dbCoef = new double[] { a1, a2, a3, b1, b2, b3, c1, c2, c3 }; //double X, Y, Z, Z1; // Z = 0.0; //double dthreshold = 0.01; //初始化数组 //int[,] dbData = new int[nDemWidth, nDemHeight]; //for (int i = 0; i < nDemWidth; i++) //{ // for (int j = 0; j < nDemHeight; j++) // { // dbData[i, j] = 0; // } //} try { //生成RASTER Point2D pt2dTopLeft = new Point2D(); pt2dTopLeft.X = pProps.Extent.UpperLeft.X; pt2dTopLeft.Y = pProps.Extent.UpperLeft.Y; pDstRaster = CreateRaster(pt2dTopLeft, dbResolution, nSize); if (pDstRaster == null) { return(false); } //得到数组 IPnt pBlockSize = new DblPntClass(); pBlockSize.X = nDemWidth; pBlockSize.Y = nDemHeight; IPnt pntLeftTop = new DblPntClass(); pntLeftTop.SetCoords(ptLeftTop.X, ptLeftTop.Y); IPixelBlock pPixelBlock = pDstRaster.CreatePixelBlock(pBlockSize); IPixelBlock3 pPixelBlock3 = pPixelBlock as IPixelBlock3; pDstRaster.Read(pntLeftTop, pPixelBlock); System.Array pixels; pixels = (System.Array)pPixelBlock3.get_PixelData(0); //初始化 object oValue = getValidType(pDstRaster as IRasterProps, 0); for (int i = 0; i < nDemHeight; i++) { for (int j = 0; j < nDemWidth; j++) { pixels.SetValue(oValue, j, i); } } //逐像素判断是否可见 IRaster2 pSrcRaster2 = pSrcRaster as IRaster2; oValue = getValidType(pDstRaster as IRasterProps, 255); for (int i = 0; i < nImgHeight; i++) { for (int j = 0; j < nImgWidth; j++) { Point2D ptCurrent = null; if (getPixelIsCoverd(pSrcRaster, dbSubDemData, ptSubImgLeftTop, j, i, out ptCurrent, exori, dbCoef, dbFocus, fx, fy, dbInitialStep)) { int nCol = int.MaxValue, nRow = int.MaxValue; pSrcRaster2.MapToPixel(ptCurrent.X, ptCurrent.Y, out nCol, out nRow); if (nCol >= nDemWidth || nCol < 0 || nRow < 0 || nRow >= nDemHeight) { continue; } pixels.SetValue(oValue, nCol, nRow); //pixels[nCol, nRow] = 255; } } } pPixelBlock3.set_PixelData(0, (System.Array)pixels); //修改数据 IRasterEdit pRasterEdit = pDstRaster as IRasterEdit; pRasterEdit.Write(pntLeftTop, pPixelBlock); pRasterEdit.Refresh(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); return(false); } return(true); }