using System;
using System.Collections.Generic;
using System.Linq;
using GenesisWinForm;
using GenesisWinForm.G3DObject.G3DCore;
//using System.Threading.Tasks;
using GenesisWinForm.MathG3D;
using OpenTK;
public static class CollapseVertexFunction
{
/// <summary>
/// 计算边的坍塌cost
/// </summary>
public static double ComputeEdgeCollapseCost(Vertex u, Vertex v)
{
// if we collapse edge uv by moving u to v then how
// much different will the model change, i.e. the “error”.
double edgelength = (v.position - u.position).Length;
double curvature = 0;
// find the “sides” triangles that are on the edge uv
List<TriangleP> sides = new List<TriangleP>();
for (int i = 0; i < u.Face.Count; i++)
{
if (u.Face[i].HasVertex(v.id))
{
sides.Add(u.Face[i]);
}
}
// use the triangle facing most away from the sides
// to determine our curvature term
for (int i = 0; i < u.Face.Count; i++)
{
double mincurv = 1;
for (int j = 0; j < sides.Count; j++)
{
// use dot product of face normals.
double dotprod = Vector3d.Dot(u.Face[i].normal, sides[j].normal);
mincurv = Math.Min(mincurv, (1 - dotprod) / 2.0f);
}
curvature = Math.Max(curvature, mincurv);
}
Debug.Log("curvature" + curvature);
return edgelength * curvature;
}
/// <summary>
/// 通过顶点计算坍塌cost
/// </summary>
public static void ComputeEdgeCostAtVertex(Vertex v)
{
if (v.neighbor.Count == 0)
{
v.collapse = null;
v.cost = -0.01f;
return;
}
v.cost = 1000000;
v.collapse = null;
// search all neighboring edges for “least cost” edge
for (int i = 0; i < v.neighbor.Count; i++)
{
double c;
c = ComputeEdgeCollapseCost(v, v.neighbor[i]);
if (c < v.cost)
{
v.collapse = v.neighbor[i];
v.cost = c;
}
}
}
#region
/// <summary>
/// 通过三个顶点查找一个三角形
/// </summary>
//public static int FindTriangleByVertexs(Vertex v1, Vertex v2, Vertex v3)
//{
// TriangleP t = new TriangleP(0);
// for (int i = 0; i < v1.Face.Count; i++)
// {
// for (int j = 0; j < v2.Face.Count; j++)
// {
// for (int k = 0; k < v3.Face.Count; k++)
// {
// if (v1.Face[i] == v2.Face[j] && v2.Face[j] == v3.Face[k])
// {
// return v1.Face[i].id;
// }
// }
// }
// }
// return t.id;
//}
//public static bool DeleteFace(G3DCore3DObjectOptimize _mesh, TriangleP _triangleP)
//{
// _mesh.DeleteTriangleP(_triangleP.id);
// return true;
//}
//public static bool DeleteVertexs(G3DCore3DObjectOptimize _mesh, Vertex _v1)
//{
// #region
// //Vertex v1 = _v1;
// //Vertex v2 = _v1.collapse;
// //List<Vertex> v3v4 = FindVertexsHavingSameSide(v1, v2);
// //Vertex v3 = v3v4[0];
// //Vertex v4 = v3v4[1];
// //Vertex v5;//v1v3共边的另一个点,需要更新v1v3v5面的neighbor
// //Vertex v6;//v1v4共边的另一个点,需要更新v1v4v6面的neighbor
// //Vertex v7;//v2v3共边的另一个点,需要更新v2v3v7面的neighbor
// //Vertex v8;//v2v4共边的另一个点,需要更新v2v4v7面的neighbor
// //List<Vertex> V2V5 = FindVertexsHavingSameSide(v1, v3);
// //List<Vertex> V2V6 = FindVertexsHavingSameSide(v1, v4);
// //List<Vertex> V7V1 = FindVertexsHavingSameSide(v2, v3);
// //List<Vertex> V8V1 = FindVertexsHavingSameSide(v2, v4);
// v5
// //if (V2V5[0] == v2)
// //{
// // v5 = V2V5[1];
// //}
// //else
// //{
// // v5 = V2V5[0];
// //}
// v6
// //if (V2V6[0] == v2)
// //{
// // v6 = V2V6[1];
// //}
// //else
// //{
// //