C#: creating mesh, invalid vertex indices

Hey there,

I’m trying to create a C# component, where you input two curves and divide them x times.
The Points on the curves should be the vertices for a quadrangular mesh.
Although the preview doesn’t look that bad i get an invalid mesh as output.

The exact statement is:
Mesh: ON_Mesh.m_F [4] .vi[ ] has invalid vertex indices.

Thanks for your help!

Lukertplugintest4.gh (5.6 KB)

The Code:

private void RunScript(Curve iCurve1, Curve iCurve2, int iSegments, ref object Mesh, ref object Vertices, ref object Validity)
{

List<Point3d> ptsCurve1 = new List<Point3d>();
List<Point3d> ptsCurve2 = new List<Point3d>();

Point3d ptCurve1 = new Point3d();
Point3d ptCurve2 = new Point3d();

double spacing = 1.0 / iSegments;
double t;

// divide curves
for (int i = 0; i < iSegments; i++)
{
  t = i * spacing;
  ptCurve1 = iCurve1.PointAtNormalizedLength(t);
  ptsCurve1.Add(ptCurve1);
  ptCurve2 = iCurve2.PointAtNormalizedLength(t);
  ptsCurve2.Add(ptCurve2);
}

// reverse list if indices are on the wrong side
if (ptsCurve1[0].DistanceTo(ptsCurve2[0]) >                                                                                           ptsCurve1[0].DistanceTo(ptsCurve2[iSegments - 1]))
{
  ptsCurve2.Reverse();
}

Mesh mesh = new Mesh();

for (int i = 0; i < iSegments; i++)
{
  mesh.Vertices.Add(ptsCurve1[i]);
  mesh.Vertices.Add(ptsCurve2[i]);
}

for (int i = 0; i < mesh.Vertices.Count; i += 2)
{
  mesh.Faces.AddFace(i, i + 1, i + 3, i + 2);
}

Mesh = mesh;
Vertices = mesh.Vertices;
Validity = GH_Format.FormatMeshValidity(mesh);

}

At first you’ve actually culled end points of your both curve’s division (when your segments integer is divided by 1.0 ) so when you make vertices from your segments integer it has two more extra point .
just subtract verticesCount from 2.0 for making faces .

List<Point3d> ptsCurve1 = new List<Point3d>();
List<Point3d> ptsCurve2 = new List<Point3d>();

Point3d ptCurve1 = new Point3d();
Point3d ptCurve2 = new Point3d();

double spacing = 1.0 / iSegments;
double t;

// divide curves
for (int i = 0; i < iSegments; i++)
{
  t = i * spacing;
  ptCurve1 = iCurve1.PointAtNormalizedLength(t);
  ptsCurve1.Add(ptCurve1);
  ptCurve2 = iCurve2.PointAtNormalizedLength(t);
  ptsCurve2.Add(ptCurve2);
}

// reverse list if indices are on the wrong side
if (ptsCurve1[0].DistanceTo(ptsCurve2[0]) > ptsCurve1[0].DistanceTo(ptsCurve2[iSegments - 1]))
{
  ptsCurve2.Reverse();
}


Mesh mesh = new Mesh();

for (int i = 0; i < iSegments; i++)
{
  mesh.Vertices.Add(ptsCurve1[i]);
  mesh.Vertices.Add(ptsCurve2[i]);
}

for (int i = 0; i < mesh.Vertices.Count - 2.0 ; i += 2)
{
  mesh.Faces.AddFace(i, i + 1, i + 3, i + 2);
}

Mesh = mesh;
Vertices = mesh.Vertices;
Validity = GH_Format.FormatMeshValidity(mesh);
1 Like

Thanks that helped!

Hi, you do not need to keep all those points. You can go directly to verts. Also @sagkoyeou fix leaves the last mesh face off:


Lastly there is some stuff you should typically do to meshes afterward for them to display well. Recomputing normals.

// orient curves
if (iCurve1.PointAtStart.DistanceTo(iCurve2.PointAtStart) > iCurve1.PointAtStart.DistanceTo(iCurve2.PointAtEnd))
{
iCurve2.Reverse();
}

// mesh
Mesh mesh = new Mesh();

// spacing
double spacing = 1.0 / iSegments;
double t;

// verts
for (int i = 0; i < iSegments + 1; i++)
{
  t = i * spacing;
  mesh.Vertices.Add(iCurve1.PointAtNormalizedLength(t));
  mesh.Vertices.Add(iCurve2.PointAtNormalizedLength(t));
}

// faces
for (int i = 0; i < iSegments * 2; i += 2)
{
  mesh.Faces.AddFace(i, i + 1, i + 3, i + 2);
}

// finishing
mesh.Normals.ComputeNormals();
mesh.FaceNormals.ComputeFaceNormals();
mesh.Compact();

// outputs
Mesh = mesh;
Vertices = mesh.Vertices;
Validity = GH_Format.FormatMeshValidity(mesh);


Mesh2Curves.gh (9.1 KB)