|
5m
楼主 |
发表于 2013-8-30 01:02:55
|
只看该作者
本帖最后由 panhao1 于 2013-8-30 01:08 编辑
RhinoC# Script案例
private void RunScript(Mesh x, double y, ref object A, ref object B)
{ try{
List<int> mapping = new List<int> ();
List<Point3d> ps = new List<Point3d>();
List<int> MapCount = new List<int> ();
ps.Add(x.Vertices[0]);mapping.Add(0);MapCount.Add(1);
for(int j = 1;j < x.Vertices.Count;j++) {
bool sign = true;
for(int i = 0;i < ps.Count;i++){
if(ps.DistanceTo((Point3d) x.Vertices[j]) < y){
sign = false;mapping.Add(i);
ps += (Point3d) x.Vertices[j];//这样直接加是错误的。应该是做个index备份。不然就会导致新的vertex失效
//添加 ps/=2;(懒人的修复方法)。
MapCount++;
break;}
}
if(sign){mapping.Add(ps.Count);ps.Add(x.Vertices[j]);MapCount.Add(1);}
}
for(int i = 0;i < ps.Count;i++){
ps /= MapCount;//注释掉(懒人修复方法)
}
Mesh mesh = new Mesh();
for(int i = 0;i < ps.Count;i++){
mesh.Vertices.Add(ps);
}
for(int i = 0;i < x.Faces.Count;i++){
if(x.Faces.IsQuad){
int p1 = mapping[x.Faces.A];
int p2 = mapping[x.Faces.B];
int p3 = mapping[x.Faces.C];
int p4 = mapping[x.Faces.D];
if( noRepeat(p1, p2, p3, p4)){
mesh.Faces.AddFace(p1, p2, p3, p4);
}
}
if(x.Faces.IsTriangle){
int p1 = mapping[x.Faces.A];
int p2 = mapping[x.Faces.B];
int p3 = mapping[x.Faces.C];
if( noRepeat(p1, p2, p3)){
mesh.Faces.AddFace(p1, p2, p3);
}
}
}
mesh.Normals.ComputeNormals();
mesh.Compact();
A = mesh;B = MapCount;
}catch(Exception ex){
Print(ex.ToString());
}
}
//<Custom additional code>
bool noRepeat(int a1, int a2, int a3, int a4){
if(a1 == a2)return false;
else if(a1 == a3)return false;
else if(a1 == a4)return false;
else if(a2 == a3)return false;
else if(a2 == a4)return false;
else if(a3 == a4)return false;
else return true;
}
bool noRepeat(int a1, int a2, int a3){
if(a1 == a2)return false;
else if(a1 == a3)return false;
else if(a2 == a3)return false;
else return true;
}
|
|