THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/06/2010 at 11:30, xxxxxxxx wrote:
Ok, here is some verified working code that I've stripped out to be stand-alone callable routines...
//---------------------------------------------------------------------------------------
//***************************************************************************************
// GetVertexNormals()
//***************************************************************************************
//---------------------------------------------------------------------------------------
Vector *GetVertexNormals(PolygonObject *pObj)
{
LONG numVerts = pObj->GetPointCount();
LONG numPolys = pObj->GetPolygonCount();
const Vector *pVerts = pObj->GetPointR();
const CPolygon *pPolys = pObj->GetPolygonR();
// initialize neighbor class... this is used to compute m_SrcvNorms
Neighbor neighbor;
if( !neighbor.Init(numVerts, pPolys, numPolys, NULL) )
return NULL;
// Allocate array of Normal Vectors to fill in and return
Vector *pNormals = (Vector * )GeAlloc(numVerts * sizeof(Vector));
if( !pNormals ) return NULL;
// Determine a Normal for each vertex of mesh
LONG i, j, faceCnt, *pFaces = NULL;
Vector vNorm;
for(i=0; i<numVerts; i++)
{
vNorm = Vector(); // (re)intitialize
neighbor.GetPointPolys(i, &pFaces;, &faceCnt;);
if( faceCnt )
{
for(j=0; j<faceCnt; j++)
{
Vector n = CalcFaceNormal(pVerts, pPolys[pFaces[j]]);
vNorm += !n;
}
vNorm /= faceCnt; // average the normal(s)
pNormals[i] = !vNorm; // normalize and store
}
else
pNormals[i] = Vector(0.0,1.0,0.0); // default case = Up for any stray verts
}
return pNormals;
}
//---------------------------------------------------------------------------------------
//***************************************************************************************
// AddNormSplineObject()
//***************************************************************************************
//---------------------------------------------------------------------------------------
void AddNormSplineObject(PolygonObject *pObj, LONG splineLen)
{
Vector *pNormals = GetVertexNormals(pObj);
if( !pNormals ) return;
LONG numNorms = pObj->GetPointCount();
// allocate spline object large enough for all points
SplineObject *splineOp = SplineObject::Alloc(2*numNorms,Tlinear);
if( !splineOp || !splineOp->MakeVariableTag(Tsegment,numNorms) )
{
MessageDialog(String("Spline Object Allocation Failed"));
GeFree(pNormals);
return;
}
splineOp->SetName(String("NormSplines"));
Vector *padr = splineOp->GetPointW();
Segment *sadr = splineOp->GetSegmentW();
if( !padr || !sadr )
{
MessageDialog(String("Spline Object Data Setup Failed"));
GeFree(pNormals);
SplineObject::Free(splineOp);
return;
}
const Vector *pVerts = pObj->GetPointR();
Matrix mg = pObj->GetMgn();
LONG normCnt = 0;
LONG vertCnt = 0;
LONG i;
for(i=0; i<numNorms; i++)
{
sadr[normCnt].cnt = 2;
sadr[normCnt++].closed = false;
padr[vertCnt++] = pVerts[i] * mg;
padr[vertCnt++] = (pVerts[i] + (pNormals[i] * splineLen)) * mg;
}
splineOp->GetDataInstance()->SetBool(SPLINEOBJECT_CLOSED,false);
GeFree(pNormals); // done with this now
BaseDocument *pBaseDoc = pObj->GetDocument();
pBaseDoc->InsertObject(splineOp,NULL,NULL);
splineOp->Message(MSG_UPDATE);
}
...just cut/paste that into your code and call the AddNormSplineObject() routine with your PolygonObject. Once you've verified that it's working, you can either use it as-is, or reference it to figure out why yours is not working.