using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class SimilarityCulling
{
private static List<triangle> Lites = new List<triangle>();
//private static List<Vector3> newLites = new List<Vector3>();
private static List<int> CloseList = new List<int>();
private static int MaxCount = 0;
private static int loop = 0;
public static float watchMaxSize = 12f;
public const int OneOfMaxPoint = 3;
public static List<Vector3> GetDisposePoints(ref List<Vector3> points)
{
if (points.Count % OneOfMaxPoint != 0)
MaxCount = points.Count - points.Count % OneOfMaxPoint;
loop = points.Count / OneOfMaxPoint;
for (int i = 0; i < loop; i++)
{
triangle _triangle = new triangle(points[i * OneOfMaxPoint], points[i * OneOfMaxPoint + 1], points[i * OneOfMaxPoint + 2], i);
Lites.Add(_triangle);
}
foreach (var item in Lites)
{
int returnNum = item.watch(watchMaxSize);
if (returnNum == -1)
continue;
Debug.Log(returnNum * 3 + 1 + "可以被移除");
CloseList.Add(returnNum);
}
Debug.Log("一波检测完毕");
CloseList.Reverse();
foreach (var item in CloseList)
{
points.RemoveAt(item * OneOfMaxPoint + 1);
}
if (CloseList.Count > 0)
{
Debug.Log("开始新的一波检测");
CloseList.Clear();
Lites.Clear();
GetDisposePoints(ref points);
}
else
{
Debug.Log("所有近似点已剔除!");
}
return points;
}
}
class triangle
{
Vector3 a;
Vector3 b;
Vector3 c;
int index;
public triangle(Vector3 a, Vector3 b, Vector3 c, int index)
{
this.a = a;
this.b = b;
this.c = c;
this.index = index;
}
public int watch(float watchMax)
{
float ground = Vector3.Distance(c, a);
float lineAb = Vector3.Distance(a, b);
float lineBc = Vector3.Distance(b, c);
float A_POW = Mathf.Pow(lineAb, 2);
float B_POW = Mathf.Pow(lineBc, 2);
float ground_POW = Mathf.Pow(ground, 2);
float X = (ground_POW - A_POW - B_POW) / ground * 2;
float z_long = ground + lineAb + lineBc;
float M_ = (Mathf.Sqrt((z_long - ground) * (z_long - lineAb) * (z_long - lineBc)) * 2) / ground;
float f = Mathf.Sqrt(Mathf.Abs(A_POW - X * X));
return index = M_ < SimilarityCulling.watchMaxSize ? index : -1;
}
}当前轨迹服务器计算后 会返回N个点
而实际我们只需要几个点便可生成路径 而且便与路径的动态编辑
so 剔除掉那些相识的非关键点
using System.Collections.Generic;
using UnityEngine;
using System;
public static class SimilarityCulling
{
private static List<triangle> Lites = new List<triangle>();
//private static List<Vector3> newLites = new List<Vector3>();
private static List<int> CloseList = new List<int>();
private static int MaxCount = 0;
private static int loop = 0;
public static float watchMaxSize = 12f;
public const int OneOfMaxPoint = 3;
public static List<Vector3> GetDisposePoints(ref List<Vector3> points)
{
if (points.Count % OneOfMaxPoint != 0)
MaxCount = points.Count - points.Count % OneOfMaxPoint;
loop = points.Count / OneOfMaxPoint;
for (int i = 0; i < loop; i++)
{
triangle _triangle = new triangle(points[i * OneOfMaxPoint], points[i * OneOfMaxPoint + 1], points[i * OneOfMaxPoint + 2], i);
Lites.Add(_triangle);
}
foreach (var item in Lites)
{
int returnNum = item.watch(watchMaxSize);
if (returnNum == -1)
continue;
Debug.Log(returnNum * 3 + 1 + "可以被移除");
CloseList.Add(returnNum);
}
Debug.Log("一波检测完毕");
CloseList.Reverse();
foreach (var item in CloseList)
{
points.RemoveAt(item * OneOfMaxPoint + 1);
}
if (CloseList.Count > 0)
{
Debug.Log("开始新的一波检测");
CloseList.Clear();
Lites.Clear();
GetDisposePoints(ref points);
}
else
{
Debug.Log("所有近似点已剔除!");
}
return points;
}
}
class triangle
{
Vector3 a;
Vector3 b;
Vector3 c;
int index;
public triangle(Vector3 a, Vector3 b, Vector3 c, int index)
{
this.a = a;
this.b = b;
this.c = c;
this.index = index;
}
public int watch(float watchMax)
{
float ground = Vector3.Distance(c, a);
float lineAb = Vector3.Distance(a, b);
float lineBc = Vector3.Distance(b, c);
float A_POW = Mathf.Pow(lineAb, 2);
float B_POW = Mathf.Pow(lineBc, 2);
float ground_POW = Mathf.Pow(ground, 2);
float X = (ground_POW - A_POW - B_POW) / ground * 2;
float z_long = ground + lineAb + lineBc;
float M_ = (Mathf.Sqrt((z_long - ground) * (z_long - lineAb) * (z_long - lineBc)) * 2) / ground;
float f = Mathf.Sqrt(Mathf.Abs(A_POW - X * X));
return index = M_ < SimilarityCulling.watchMaxSize ? index : -1;
}
}当前轨迹服务器计算后 会返回N个点


so 剔除掉那些相识的非关键点
