How to get the Vertex Normal on mesh?

Hi;
In this sample I cant get the normal on mesh face, now I’m try to get the normal at mesh vertex but always failed.

static bool meshclosetpoint(ON_Mesh* m_mesh, ON_3dPoint po, ON_3dVector& nor, ON_3dPoint& poi_cl)
{
	bool rc=false;
	ON_MESH_POINT point;
	ON_3dVector normal = ON_UNSET_POINT;
	m_mesh->GetClosestPoint(po, &point);
	poi_cl = point.m_P;
	if (point.m_ci.m_type == ON_COMPONENT_INDEX::mesh_face || point.m_ci.m_type == ON_COMPONENT_INDEX::meshtop_edge)
	{
		if (0 != point.m_mesh && 0 <= point.m_ci.m_index && point.m_ci.m_index < point.m_mesh->FaceCount())
		{
			if (point.m_mesh->HasFaceNormals())
				normal = point.m_mesh->m_FN[point.m_ci.m_index];
			else
			{
				if (point.m_mesh->HasDoublePrecisionVertices())
					point.m_mesh->m_F[point.m_ci.m_index].ComputeFaceNormal(point.m_mesh->DoublePrecisionVertices().Array(), normal);
				else
					point.m_mesh->m_F[point.m_ci.m_index].ComputeFaceNormal(point.m_mesh->m_V.Array(), normal);
			}
			if (normal.IsValid())
			{
				nor = normal;
				rc=true;
			}
		}
		else  //not work
		{
			ON_Mesh dupe_mesh( *point.m_mesh );
			dupe_mesh.ComputeVertexNormals();
			nor = dupe_mesh.m_N[point.m_ci.m_index];
			rc=true;
		}
	}
	return rc;
};

How cant I solve it?

Moved to Developer category.

Hi @pythonuser,

How about this?

static bool TestPythonUser(
  ON_Mesh* mesh, 
  ON_3dPoint point, 
  ON_3dPoint& face_point,
  ON_3dVector& face_normal
)
{
  face_point = ON_3dPoint::UnsetPoint;
  face_normal = ON_3dVector::UnsetVector;

  if (nullptr == mesh)
    return false;

  ON_MESH_POINT mp;
  bool rc = mesh->GetClosestPoint(point, &mp);
  if (rc)
  {
    face_point = mp.m_P;
    if (mp.m_ci.m_type == ON_COMPONENT_INDEX::mesh_face)
    {
      if (!mesh->HasFaceNormals())
        mesh->ComputeFaceNormals();
      face_normal = mesh->m_FN[mp.m_face_index];
    }
  }
  return rc;
}

– Dale

Hi @dale
When mp.m_ci.m_type == ON_COMPONENT_INDEX::mesh_face, it run very good, but when face_point = mesh->mesh->Vertex(i), it not run :joy:

@pythonuser - this is true. Perhaps you should return the vertex normal in his case.

– Dale