/// <summary> /// 初始化帧到第一帧 /// </summary> private void modelInitReloadALLFrame() { GlobalVariableBackground.Instance.conManager.WriteLog("帧初始化开始"); if (vtkAllFrame.listFrame.Count == 0) { return; } lastIndex = 0; GlobalVariableBackground.Instance.FrameAllIndex = 0; //vtkAllFrame.ClearListFrame(); //vtkAllFrame.listFrame =new List<VTK_Frame>(); for (int i = 0; i < GlobalVariableBackground.Instance.modelCacheCount; i++) { modelFrameManager modelFrameManager = transform.GetChild(i).GetComponent <modelFrameManager>(); if (modelFrameManager == null) { transform.GetChild(i).gameObject.AddComponent <modelFrameManager>(); modelFrameManager.vtkframe = new VTK_Frame(); modelFrameManager.isCollider = vtkAllFrame.loadFile.dataType == Enums.DataType.Default; } else { if (modelFrameManager.isCutModelGO) { //transform.GetChild(i) 里面含有切割生成的对象 continue; } } VTK_Frame vtkframe = modelFrameManager.vtkframe; vtkframe.go = transform.GetChild(i).gameObject; //vtkAllFrame.listFrame.Add(vtkframe); Others.ThreadLoadFilePack pack = new Others.ThreadLoadFilePack(); //填充 线程数据包-》单帧 pack.vtkFrame = vtkframe; pack.vtkAllFrame = vtkAllFrame; DirectoryInfo folder = new DirectoryInfo(vtkAllFrame.loadFile.FileDir); FileInfo[] fileinfoArray; fileinfoArray = folder.GetFiles("*.qf"); Array.Sort(fileinfoArray, new FileNameSort()); //填充 线程数据包-》单一文件 pack.fileAddres = fileinfoArray[i]; // vtkframe.selfFrameIndex = i; VTKCManager m = new VTKCManager(); vtkframe.timeStartLession = DateTime.Now; ThreadPool.QueueUserWorkItem(new WaitCallback(m.DisposeData), pack as object); } //GlobalVariableBackground.Instance.conManager.WriteLog("帧初始化任务分发完毕"); }
/// <summary> /// 获取文件列表 /// </summary> public void VTKGetFileList(Structure_LoadFile structure_loadFile) { if (structure_loadFile.FileDir == null) { return; } List <Others.MaxAndMin> listm = new List <Others.MaxAndMin>(); if (structure_loadFile.dataType == Enums.DataType.Default) { StreamReader sr; string file_name = structure_loadFile.FileDir + @"\Arr.mm"; sr = File.OpenText(file_name); string strAboutAttMaxMin = sr.ReadLine(); sr.Close(); sr.Dispose(); string[] strTempArray = strAboutAttMaxMin.Trim().Split(' '); Others.MaxAndMin mn; for (int i = 0; i < strTempArray.Length;) { mn = new Others.MaxAndMin(); mn.Attname = strTempArray[i++]; mn.Dimension = Convert.ToInt32(strTempArray[i++]); mn.isPointAtt = strTempArray[i++].Contains("point"); mn.Min = Convert.ToSingle(strTempArray[i++]); mn.Max = Convert.ToSingle(strTempArray[i++]); listm.Add(mn); } } structure_loadFile.name = GetSameName(structure_loadFile.FileDir); string extensionName = "*.qf"; //switch (structure_loadFile.fileType) //{ // case Enums.FileType.Tecplot: // extensionName = "*.dat"; // break; // case Enums.FileType.VTK: // extensionName = "*.vtk"; // break; // default: // break; //} //创建所有帧对象 VTK_AllFrame AllFrame = new VTK_AllFrame(); AllFrame.point_MaxAndMin = listm; AllFrame.modelState = new Others.ModelState(); int pointAttIndex = 0; int cellAttIndex = 0; for (int i = 0; i < listm.Count; i++) { if (listm[i].Dimension == 1) { AllFrame.modelState.isUsecolor = true; AllFrame.modelState.isPointAtt = listm[i].isPointAtt; if (listm[i].isPointAtt) { AllFrame.modelState.attIndex = pointAttIndex; } else { AllFrame.modelState.attIndex = cellAttIndex; } AllFrame.modelState.maxminIndex = i; break; } else { if (listm[i].isPointAtt) { pointAttIndex++; } else { cellAttIndex++; } } } AllFrame.listFrame = new List <VTK_Frame>(); AllFrame.name = structure_loadFile.name; //创建 场景中的 数据模型载体 GameObject newNodel = new GameObject(); newNodel.name = structure_loadFile.name; newNodel.transform.parent = transform; //挂上 功能脚本 modelGameObject modelgo = newNodel.AddComponent <modelGameObject>(); modelgo.vtkAllFrame = AllFrame; AllFrame.loadFile = structure_loadFile; DirectoryInfo folder = new DirectoryInfo(structure_loadFile.FileDir); FileInfo[] fileinfoArray = folder.GetFiles(extensionName); Array.Sort(fileinfoArray, new FileNameSort()); AllFrame.FileCount = fileinfoArray.Length; GlobalVariableBackground.Instance.FrameALLCount = fileinfoArray.Length; //加载指定缓存数量的模型数据 for (int i = 0; i < GlobalVariableBackground.Instance.modelCacheCount; i++) { GameObject goModelFrame; //生成 线程数据包 goModelFrame = GameObject.Instantiate(GlobalVariableBackground.Instance.modelPerfab); goModelFrame.transform.parent = newNodel.transform; modelFrameManager modelManager = goModelFrame.AddComponent <modelFrameManager>(); goModelFrame.AddComponent <ClickGameObjectEvent>(); modelManager.modelState = AllFrame.modelState; Others.ThreadLoadFilePack pack = new Others.ThreadLoadFilePack(); //pack.strAboutArrMAxMin = strAboutAttMaxMin; //创建 单帧 VTK_Frame frame = new VTK_Frame(); frame.point_MaxAndMin = AllFrame.point_MaxAndMin; modelManager.vtkframe = frame; frame.go = goModelFrame; modelManager.isCollider = structure_loadFile.dataType == Enums.DataType.Default; pack.vtkAllFrame = AllFrame; //填充 线程数据包-》单帧 pack.vtkFrame = frame; //填充 线程数据包-》单一文件 pack.fileAddres = fileinfoArray[i]; int fileIndex = i; AllFrame.listFrame.Add(frame); goModelFrame.name = AllFrame.name + "#" + fileIndex; frame.selfFrameIndex = fileIndex; VTKCManager m = new VTKCManager(); frame.timeStartLession = DateTime.Now; m.DisposeData(pack); //Thread t = new Thread(m.DisposeData); //t.IsBackground = true; //t.Start(pack); //ThreadPool.QueueUserWorkItem(new WaitCallback(m.DisposeData), pack as object); } folder = null; fileinfoArray = null; }
private void modelInitReloadAllFrameByIndex(int indexFrame) { int min, max = 0; int half = GlobalVariableBackground.Instance.modelCacheCount / 2; if (indexFrame - half < 0) { min = 0; max = GlobalVariableBackground.Instance.modelCacheCount - 1; } else if (indexFrame + half > vtkAllFrame.FileCount) { min = vtkAllFrame.FileCount - 1 - GlobalVariableBackground.Instance.modelCacheCount; max = vtkAllFrame.FileCount - 1; } else { min = indexFrame - half; max = indexFrame + half; } for (int i = min; i <= max; i++) { int childIndex = i % GlobalVariableBackground.Instance.modelCacheCount; modelFrameManager modelFrameManager = transform.GetChild(childIndex).GetComponent <modelFrameManager>(); if (modelFrameManager == null) { transform.GetChild(childIndex).gameObject.AddComponent <modelFrameManager>(); modelFrameManager.vtkframe = new VTK_Frame(); modelFrameManager.isCollider = vtkAllFrame.loadFile.dataType == Enums.DataType.Default; } else { if (modelFrameManager.isCutModelGO) { //transform.GetChild(i) 里面含有切割生成的对象 continue; } } VTK_Frame vtkframe = modelFrameManager.vtkframe; vtkframe.go = transform.GetChild(childIndex).gameObject; Others.ThreadLoadFilePack pack = new Others.ThreadLoadFilePack(); pack.vtkFrame = vtkframe; pack.vtkAllFrame = vtkAllFrame; DirectoryInfo folder = new DirectoryInfo(vtkAllFrame.loadFile.FileDir); FileInfo[] fileinfoArray; fileinfoArray = folder.GetFiles("*.qf"); Array.Sort(fileinfoArray, new FileNameSort()); //填充 线程数据包-》单一文件 pack.fileAddres = fileinfoArray[i]; vtkframe.selfFrameIndex = i; VTKCManager m = new VTKCManager(); vtkframe.timeStartLession = DateTime.Now; if (vtkAllFrame.modelState.isSmooth && vtkAllFrame.loadFile.dataType == Enums.DataType.Water) { Others.ThreadSmoothModePack packsm = new Others.ThreadSmoothModePack(); packsm.fileAddres = pack.fileAddres; packsm.smoothCount = 20; packsm.vtkAllFrame = pack.vtkAllFrame; packsm.vtkFrame = pack.vtkFrame; m.SmoothDispose(packsm); //ThreadPool.QueueUserWorkItem(new WaitCallback(m.SmoothDispose), packsm as object); //Thread t = new Thread(m.SmoothDispose); //t.IsBackground = true; //t.Start(packsm); } else { ThreadPool.QueueUserWorkItem(new WaitCallback(m.DisposeData), pack as object); } } }
// Update is called once per frame void Update() { if (vtkframe != null && vtkframe.isLoadFinish) { vtkframe.isLoadFinish = false; BoxColliderChange(); //if (transform.parent.GetComponent<modelGameObject>().vtkAllFrame.loadFile.dataType == Enums.DataType.Default) //{ //} StartCoroutine("vtkLoadModel"); } if (isLoadModelFininsh) { isLoadModelFininsh = false; dtColorStart = System.DateTime.Now; //ToDrawColor(); StartCoroutine("ToDrawColor"); } if (isLoadColorFininsh) { isLoadColorFininsh = false; System.DateTime dtColorEnd = System.DateTime.Now; ToEnable(); // TestMothed(); name = name.Split('#')[0] + "#" + vtkframe.selfFrameIndex; vtkframe.timeLoadEnd = System.DateTime.Now; GlobalVariableBackground.Instance.conManager.WriteLog(vtkframe.selfFrameIndex + " Lession to threadStart is " + (vtkframe.timeLoadStart - vtkframe.timeStartLession).TotalMilliseconds + "ms, threadStart to threadEnd is " + (vtkframe.timeLoadQFEnd - vtkframe.timeLoadStart).TotalMilliseconds + "ms, threadEnd to Enable is " + (vtkframe.timeLoadEnd - vtkframe.timeLoadQFEnd).TotalMilliseconds + "ms, ColorStart To ColorEnd is " + (dtColorEnd - dtColorStart).TotalMilliseconds + "ms, AllTime is " + (vtkframe.timeLoadEnd - vtkframe.timeStartLession).TotalMilliseconds + "ms "); //float time = System.Convert.ToSingle((vtkframe.timeLoadEnd - vtkframe.timeLoadStart).TotalSeconds / (GlobalVariableBackground.Instance.modelCacheCount / 2)); //GlobalVariableBackground.Instance.conManager.timeIndex++; //GlobalVariableBackground.Instance.conManager.timeArray[GlobalVariableBackground.Instance.conManager.timeIndex % 5] = time; //float Sum = 0; //for (int i = 0; i < GlobalVariableBackground.Instance.conManager.timeArray.Length; i++) //{ // Sum += GlobalVariableBackground.Instance.conManager.timeArray[i]; //} //GlobalVariableBackground.Instance.conManager.timeFloat =Mathf.Max(2f, Sum / 5) ; // GlobalVariableBackground.Instance.conManager.WriteLog("加载完毕"); if (isCutModelGO) { modelGameObject model = transform.parent.GetComponent <modelGameObject>(); if (model != null) { modelFrameManager mf = model.vtkAllFrame.listFrame[vtkframe.selfFrameIndex % GlobalVariableBackground.Instance.modelCacheCount].go.GetComponent <modelFrameManager>(); mf.goHaveCutMode = true; mf.ToEnable(); } } vtkframe.isAllFinish = true; } }
/// <summary> /// 跟随显示选中网格或点 /// </summary> /// <param name="obj"></param> /// <returns></returns> private Others.PackUpObject PickUpObject(Others.PackUpObject obj) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //射线投射 if (Physics.Raycast(ray, out hit)) { if (hit.transform == null)//如果射线没有碰撞到Transform,return { obj.isMeshHave = false; return(obj); } if (obj == null) { obj = new Others.PackUpObject(); } if (obj.pickUpName.Equals(hit.transform.name) && obj.pickUpIndex == hit.triangleIndex && pickmode == Enums.PickMode.cell) { return(obj); } //获取hit.transform的MeshFilter组件 MeshFilter meshFilter = hit.transform.GetComponent <MeshFilter>(); if (meshFilter == null) { obj.isMeshHave = false; return(obj); } if (hit.transform.parent == null) { obj.isMeshHave = false; return(obj); } modelFrameManager modelChild = hit.transform.parent.GetComponent <modelFrameManager>(); if (modelChild == null) { obj.isMeshHave = false; return(obj); } if (!hit.transform.name.Contains("|")) { obj.isMeshHave = false; return(obj); } Mesh mesh = meshFilter.mesh; int[] triArray = mesh.triangles; Vector3[] vecArray = mesh.vertices; //CLoadTest.Cell_Out cell = modelChild.vtkframe.meshArray[index].cellArray[hit.triangleIndex]; //int number = cell.number; int hitIndex = hit.triangleIndex * 3; //AddLineArray(listVec, listTri, MakeQuad(vecArray[triArray[hitIndex]], vecArray[triArray[hitIndex + 1]], 0.001f)); //AddLineArray(listVec, listTri, MakeQuad(vecArray[triArray[hitIndex + 1]], vecArray[triArray[hitIndex + 2]], 0.001f)); //AddLineArray(listVec, listTri, MakeQuad(vecArray[triArray[hitIndex + 2]], vecArray[triArray[hitIndex]], 0.001f)); obj.AttIndex = modelChild.modelState.attIndex; switch (pickmode) { case Enums.PickMode.Point: Vector3 v1 = transform.TransformPoint(vecArray[triArray[hitIndex]]); Vector3 v2 = transform.TransformPoint(vecArray[triArray[hitIndex + 1]]); Vector3 v3 = transform.TransformPoint(vecArray[triArray[hitIndex + 2]]); float f1 = Vector3.Distance(v1, hit.point); float f2 = Vector3.Distance(v2, hit.point); float f3 = Vector3.Distance(v3, hit.point); Vector3 vec; if (f1 < f2 && f1 < f3) { vec = v1; obj.pointIndex = triArray[hitIndex]; } else if (f2 < f1 && f2 < f3) { vec = v2; obj.pointIndex = triArray[hitIndex + 1]; } else { vec = v3; obj.pointIndex = triArray[hitIndex + 2]; } Vector3[] vecTemp = GetPointCube(vec, 0.04f); obj.mesh.vertices = vecTemp; obj.mesh.triangles = pointTriTemp; obj.mesh.colors = colorPointArrayTemp; obj.mesh.RecalculateNormals(); obj.pickUpIndex = hit.triangleIndex; obj.pickUpName = hit.transform.name; obj.isMeshHave = true; obj.isPointMode = true; //GlobalVariableBackground.Instance.conManager.WriteLog("obj.pickUpName is " + obj.pickUpName + " obj.pickUpIndex is " + obj.pickUpIndex); int index = Convert.ToInt32(obj.pickUpName.Split('|')[1]); break; case Enums.PickMode.cell: //TODO:追加线颜色处理 AddLineArray3(listVec, listTri, MakeQuad(vecArray[triArray[hitIndex]], vecArray[triArray[hitIndex + 1]], vecArray[triArray[hitIndex + 2]], GlobalVariableBackground.Instance.Linewight * 1.5f)); Color[] colorArray = new Color[listVec.Count]; for (int i = 0; i < listVec.Count; i++) { //基佬紫 colorArray[i] = Color.HSVToRGB(304 / 360f, 1f, 1f); } //Mesh ms = new Mesh(); if (obj.mesh.vertices.Length > listVec.Count) { obj.mesh.triangles = listTri.ToArray(); obj.mesh.SetVertices(listVec); } else { obj.mesh.SetVertices(listVec); obj.mesh.triangles = listTri.ToArray(); } obj.mesh.colors = colorArray; obj.mesh.RecalculateNormals(); listVec.Clear(); listTri.Clear(); colorArray = null; obj.pickUpIndex = hit.triangleIndex; obj.pickUpName = hit.transform.name; obj.isMeshHave = true; obj.isPointMode = false; //GlobalVariableBackground.Instance.conManager.WriteLog("obj.pickUpName is " + obj.pickUpName + " obj.pickUpIndex is " + obj.pickUpIndex); break; default: break; } return(obj); } obj.isMeshHave = false; return(obj); }
/// <summary> /// 点击选中 /// </summary> private void PickUpClick() { if (pickUpObj.isMeshHave) { pickClick.lastFrameIndex = GlobalVariableBackground.Instance.FrameAllIndex; pickClick.isMeshHave = pickUpObj.isMeshHave; pickClick.pickUpIndex = pickUpObj.pickUpIndex; pickClick.pickUpName = pickUpObj.pickUpName; Vector3[] vec = pickUpObj.mesh.vertices; int[] tri = pickUpObj.mesh.triangles; if (pickClick.mesh.vertices.Length > vec.Length) { pickClick.mesh.triangles = tri; pickClick.mesh.vertices = vec; } else { pickClick.mesh.vertices = vec; pickClick.mesh.triangles = tri; } int colorLength = pickUpObj.mesh.colors.Length; pickClick.isPointMode = pickUpObj.isPointMode; pickClick.AttIndex = pickUpObj.AttIndex; pickClick.pointIndex = pickUpObj.pointIndex; Color[] color = new Color[colorLength]; for (int i = 0; i < color.Length; i++) { color[i] = Color.yellow; } pickClick.mesh.colors = color; pickClick.mesh.RecalculateNormals(); pickClick.mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 100000f); modelFrameManager mf = hit.transform.parent.GetComponent <modelFrameManager>(); if (mf != null) { if (mf.vtkframe.CellAttType.listAttCount.Count > 0 && pickmode == Enums.PickMode.cell) { if (!GlobalVariableBackground.Instance.TableObject.gameObject.activeSelf) { GlobalVariableBackground.Instance.TableObject.gameObject.SetActive(true); } } else if (mf.vtkframe.pointAttType.listAttCount.Count > 0 && pickmode == Enums.PickMode.Point) { if (!GlobalVariableBackground.Instance.TableObject.gameObject.activeSelf) { GlobalVariableBackground.Instance.TableObject.gameObject.SetActive(true); } } else { GlobalVariableBackground.Instance.TableObject.gameObject.SetActive(false); } GlobalVariableBackground.Instance.TableObject.ClearValue(); if (pickClick.MeshIndex >= 0 && pickClick.pointIndex >= 0) { pickClick.number = mf.vtkframe.meshArray[pickClick.MeshIndex].pointArray[pickClick.pointIndex].number; } } } }