техническое

Jul 11, 2012 09:23




int MnCdAzr_Register()
{
int err = 0;
cvxCmdFunc(" ",(void*)MnCdAzr_Wrap,0.0);
cvxCmdCallback("MnCdAzr_Cb",(void*)MnCdAzr_Cb);
cvxCmdCallback("MnCdAzr_Init",(void*)MnCdAzr_Init);
cvxCmdCallback("MnCdAzr_Term",(void*)MnCdAzr_Term);
cvxCmdCallback("MnCdAzr_EO",(void*)MnCdAzr_EO);
cvxCmdCallback("MnCdAzr_Br1",(void*)MnCdAzr_Br1);
err = cvxCmdTemplate("MnCdAzr.t");
return(err);
}
int MnCdAzr_Wrap
(
 int idData     /* I: command input data */
 )
{
WRAP_INTRO;
res = MnCdAzr(idData);
WRAP_OUTTRO;
}
void MnCdAzr_Init
(
 int idData     /* I: command input data */
 )
{
INIT_INTRO;
proComponentManager* cmanager = proComponentManager::GetInstance();
cmanager->RefreshCurrentFile();
svxData data;
if ( cvxDataGet(idData,2,&data) )
{
// init first run
_cvxDataSetNum( idData, 3, .4 );
_cvxDataSetNum( idData, 4, 0 );
_cvxDataSetNum( idData, 5, 0 );
}
INIT_OUTTRO;
}
int MnCdAzr_Cb
(
 char *template_name,    /* I: name of active form */
 int field_id,             /* I: id of active field */
 int idData            /* I: index of form's VDATA object */
 )
{
CB_INTRO;
err = MnCdAzr( idData );
CB_OUTTRO;
}
int MnCdAzr_Term()
{
TERM_INTRO;
TERM_OUTTRO;
}
void MnCdAzr_EO(int idData, void* echo)
{
EO_INTRO;
MnCdAzr( idData );
EO_OUTTRO;
};
void MnCdAzr_Br1()
{
BR_INTRO;
BR_OUTTRO;
};
int MnCdAzr
(
 int idData     /* I: command input data */
 )
{
svxPoint pntCenter;
pntCenter.x = pntCenter.y = 0;
pntCenter.z= -30;
svxVector vecNormal;
vecNormal.x = vecNormal.y = 0;
vecNormal.z = 1;
vector arrNearest;
arrNearest.resize(5);
for (int i = 0; i < 5; i++)
arrNearest[i].z = 0;
arrNearest[0].x = 2;
arrNearest[0].y = 2;
arrNearest[0].z = -30;
arrNearest[1].x = 2;
arrNearest[1].y = 5;
arrNearest[1].z = -30;
arrNearest[2].x = -3;
arrNearest[2].y = 1;
arrNearest[2].z = -30;
arrNearest[3].x = -5;
arrNearest[3].y = -5;
arrNearest[3].z = -30;
arrNearest[4].x = 1;
arrNearest[4].y = -3;
arrNearest[4].z = -30;
vector arrIntersected;
GetIntersectNearestPoints(pntCenter, vecNormal, arrNearest, arrIntersected);
CMD_INTRO;
svxData tmpdata;
svxPoint pickpntUp;
svxPoint pickpntDown;
int upsurf;
if (cvxDataGetPnt(idData, 1, &pickpntUp))
goto EXIT;
if ( cvxDataGetPromote(idData, 1, &upsurf))
goto EXIT;
cvxDataGet(idData,1,&tmpdata);
if ( upsurf )
upsurf = tmpdata.idParent;
else
upsurf = tmpdata.idEntity;
int dnsurf;
if (cvxDataGetPnt(idData, 2, &pickpntDown))
goto EXIT;
if ( cvxDataGetPromote(idData, 2, &dnsurf))
goto EXIT;
cvxDataGet(idData,2,&tmpdata);
if ( dnsurf )
dnsurf = tmpdata.idParent;
else
dnsurf = tmpdata.idEntity;
double thickness;
if (cvxDataGetNum(idData,3,&thickness))
goto EXIT;
double opt1;
if (cvxDataGetNum(idData,4,&opt1))
goto EXIT;
double opt2;
if (cvxDataGetNum(idData,5,&opt2))
goto EXIT;
// gather all components and keep track of their position
int numComps =0, *Comps=NULL;
cvxPartInqComps(NULL,NULL,&numComps,&Comps);
if (globalPreviewMode)
{
if (sEcho)
{
svxPoint* arrPosition = NULL;
cvxMemAlloc(numComps * sizeof(svxPoint), (void**)&arrPosition);
BOOL* arrAllSamePlane = NULL;
cvxMemAlloc(numComps * sizeof(BOOL), (void**)&arrAllSamePlane);
svxPoint* arrEnd = NULL;
cvxMemAlloc(numComps * sizeof(svxPoint), (void**)&arrEnd);
double* arrLengths = NULL;
cvxMemAlloc(numComps * sizeof(double), (void**)&arrLengths);
svxVector* vecDirections = NULL;
cvxMemAlloc(numComps * sizeof(svxVector), (void**)&vecDirections);
int nFaceId;
//////////////////////////////////////////////////////////////////////////
// task A
//////////////////////////////////////////////////////////////////////////
double upick, vpick;
double dLength;
svxPoint pntEnd;
for (int i = 0; i < numComps; i++)
{
svxMatrix mat;
cvxEntMatrix(Comps[i], &mat);
arrPosition[i].x = mat.xt;
arrPosition[i].y = mat.yt;
arrPosition[i].z = mat.zt; 
svxPoint pntProjection;
svxVector vecNorm;
if (cvxFacePntProj(upsurf, &arrPosition[i], &upick, &vpick))
{
arrAllSamePlane[i] = FALSE;
continue;
}
cvxFaceEval(upsurf, upick, vpick, &pntProjection, &vecNorm);
if (cvxPntDist(&arrPosition[i], &pntProjection) > MIN_DIST)
{
arrAllSamePlane[i] = FALSE;
continue;
}
arrAllSamePlane[i] = TRUE;
if (!GetCutterParams(upsurf, &pntProjection, 0, dLength, &pntEnd, nFaceId))
continue;
arrEnd[i] = pntEnd;
arrLengths[i] = dLength;
vecDirections[i] = vecNorm;
}
//////////////////////////////////////////////////////////////////////////
// task B, C
//////////////////////////////////////////////////////////////////////////
double dOffset = AZURE_THICK;
double dAverageDist = GetAverageDist(arrEnd, numComps);
for (int i = 0; i < numComps; i++)
{
vector arrNearest;
for (int j = 0; j < numComps; j++)
{
if (i == j)
continue;
double dCurrDist = cvxPntDist(&arrEnd[i], &arrEnd[j]);
if (dCurrDist > dAverageDist * 2)
{
if (i == 8)
{
proEcho echo(sEcho);
// echo.Point(&arrEnd[j]);
}
continue;
}
svxPoint pntCenterLine;
pntCenterLine.x = (arrEnd[i].x + arrEnd[j].x) / 2;
pntCenterLine.y = (arrEnd[i].y + arrEnd[j].y) / 2;
pntCenterLine.z = (arrEnd[i].z + arrEnd[j].z) / 2;
arrNearest.push_back(pntCenterLine);
}
//SortPointsAround(arrEnd[i], vecDirections[i], arrNearest);
vector arrIsects;
proEcho echo(sEcho);
if (i == 15)
{
bG = TRUE;
int g = 6;
g= 8;
for (int k = 0; k < (int)arrNearest.size(); k++)
echo.Point(&arrNearest[k]);
}
else
bG = FALSE;
GetIntersectNearestPoints(arrEnd[i], vecDirections[i], arrNearest, arrIsects);
vector arrResults;
int nT= 15;
if (i == nT)
{
for (int k = 0; k < (int)arrIsects.size(); k++)
{
svxPoint rr1;
rr1.x = arrEnd[k].x;
rr1.y = arrEnd[k].y;
rr1.z = arrEnd[k].z + 2;
echo.Line(&rr1, &arrEnd[k]);
}
svxPoint rr;
rr.x = arrEnd[i].x;
rr.y = arrEnd[i].y;
rr.z = arrEnd[i].z + 18;
echo.Line(&rr, &arrEnd[i]);
}
continue;
echo.Point(&arrEnd[i]);
for (int k = 0; k < (int)arrIsects.size(); k++)
{
int kp1 = (k+1)%(int)arrIsects.size();
echo.Line(&arrIsects[k], &arrIsects[kp1]);
}
}
cvxMemFree((void**)&arrPosition);
cvxMemFree((void**)&arrAllSamePlane);
cvxMemFree((void**)&arrEnd);
cvxMemFree((void**)&arrLengths);
cvxMemFree((void**)&vecDirections);
}
}
else
{
// rename the item
int histop=cvxEntNew(0, VX_ENT_OP);
cvxPartHistRename(histop, "Azure Tool", NULL);
}
cvxMemFree( (void**) &Comps );
EXIT:;
CMD_OUTTRO;
};
BOOL GetCutterParams(int nIdSurfaceUp, svxPoint* pntPicked, int nIdSurfaceDown, double& dLength, svxPoint* pntSecond, int& nFaceId)
{
double u, v;
if (cvxFacePntProj(nIdSurfaceUp, pntPicked, &u, &v))
return -1;
svxPoint pntTemp;
svxVector vecNormal;
cvxFaceEval(nIdSurfaceUp, u, v, &pntTemp, &vecNormal);
vecNormal.x = -vecNormal.x;
vecNormal.y = -vecNormal.y;
vecNormal.z = -vecNormal.z;
svxVector vecMicroOffset = vecNormal;
cvxVecNormalize(&vecMicroOffset);
vecMicroOffset.x *= 0.01;
vecMicroOffset.y *= 0.01;
vecMicroOffset.z *= 0.01;
svxAxis vecAxis;
vecAxis.Dir = vecNormal;
vecAxis.Pnt = *pntPicked;
vecAxis.Pnt.x += vecMicroOffset.x;
vecAxis.Pnt.y += vecMicroOffset.y;
vecAxis.Pnt.z += vecMicroOffset.z;
svxPoint pntNearest;
if (cvxIsectRayPartVis(VX_TRIM_ALL, &vecAxis, &pntNearest, &nFaceId))
return FALSE;
if (pntSecond != NULL)
*pntSecond = pntNearest;
dLength = cvxPntDist(pntPicked, &pntNearest);
return TRUE;
}
double GetAverageDist(svxPoint* arrAllPoints, int nPointsCount)
{
double dTotalMinDist = 0;
for (int i = 0; i < nPointsCount - 1; i++)
{
double dCurrMinDist = 0;
for (int j = i + 1; j < nPointsCount; j++)
{
double dCurrDist = cvxPntDist(&arrAllPoints[i], &arrAllPoints[j]);
if ((dCurrDist < dCurrMinDist) || (j == i + 1))
dCurrMinDist = dCurrDist;
}
dTotalMinDist += dCurrMinDist;
}
return dTotalMinDist / nPointsCount;
}
IntersectResultType Intersect(svxPoint* pntStart1, svxPoint* pntEnd1, svxPoint* pntStart2, svxPoint* pntEnd2, svxPoint* pntResult)
{
pntResult->z = 0.0;
double dDenom = ((pntEnd2->y - pntStart2->y) * (pntEnd1->x - pntStart1->x)) - 
((pntEnd2->x - pntStart2->x) * (pntEnd1->y - pntStart1->y));
double dNume_a = ((pntEnd2->x - pntStart2->x) * (pntStart1->y - pntStart2->y)) -
((pntEnd2->y - pntStart2->y) * (pntStart1->x - pntStart2->x));
double dNume_b = ((pntEnd1->x - pntStart1->x) * (pntStart1->y - pntStart2->y)) -
((pntEnd1->y - pntStart1->y) * (pntStart1->x - pntStart2->x));
if (fabs(dDenom) < MIN_DBL)
{
if ((fabs(dNume_a) < MIN_DBL) && (fabs(dNume_b) < MIN_DBL))
return kCoincident;
else
return kParallel;
}
double dUa = dNume_a / dDenom;
double dUb = dNume_b / dDenom;
if ((dUa >= 0.0) && (dUa <= 1.0) && (dUb >= 0.0) && (dUb <= 1.0))
{
pntResult->x = pntStart1->x + dUa * (pntEnd1->x - pntStart1->x);
pntResult->y = pntStart1->y + dUa * (pntEnd1->y - pntStart1->y);
return kInteresecting;
}
return kNotInteresecting;
}
void SortPointsAround(const svxPoint& pntCenter, const svxVector& vecNormal, vector& arrNearest)
{
svxVector vecAxisX;
vector arrAngles;
svxVector vecCurrent;
vecAxisX.x = 1;
vecAxisX.y = 0;
vecAxisX.z = 0;
arrAngles.resize(arrNearest.size());
for (int i = 0; i < (int)arrNearest.size(); i++)
{
vecCurrent.x = arrNearest[i].x - pntCenter.x;
vecCurrent.y = arrNearest[i].y - pntCenter.y;
vecCurrent.z = arrNearest[i].z - pntCenter.z;
arrAngles[i] = GetVectorsAngle02PI(&vecAxisX, &vecCurrent, (svxVector*)&vecNormal);
}
for (int i = 0; i < (int)arrAngles.size() - 1; i++)
{
for (int j = i + 1; j < (int)arrAngles.size(); j++)
{
if (arrAngles[i] > arrAngles[j])
{
double dTemp = arrAngles[i];
arrAngles[i] = arrAngles[j];
arrAngles[j] = dTemp;
swap(arrNearest[i], arrNearest[j]);
}
}
}
}
void GetIntersectNearestPoints(const svxPoint& pntCenter, const svxVector& vecNormal, const vector& arrNearest, vector& arrIntersected)
{
set arrSideFlag;
vector arrThisSide;
vector arrAllIsects;
for (int i = 0; i < (int)arrNearest.size(); i++)
{
svxPoint& pntCurr = (svxPoint&)arrNearest[i];
svxVector vecCurrent;
svxMatrix matrPlane;
vecCurrent.x = pntCurr.x - pntCenter.x;
vecCurrent.y = pntCurr.y - pntCenter.y;
vecCurrent.z = pntCurr.z - pntCenter.z;
cvxVecNormalize(&vecCurrent);
cvxMatPntVec(&pntCurr, &vecCurrent, &matrPlane);
for (int j = 0; j < (int)arrNearest.size(); j++)
{
if (i == j)
continue;
svxPoint& pntCurr2 = (svxPoint&)arrNearest[j];
svxVector vecSecond;
vecSecond.x = pntCurr2.x - pntCenter.x;
vecSecond.y = pntCurr2.y - pntCenter.y;
vecSecond.z = pntCurr2.z - pntCenter.z;
svxMatrix matrRotation;
svxAxis vecAxis;
vecAxis.Pnt = pntCurr2;
vecAxis.Dir = vecNormal;
cvxMatRotate(&matrRotation, 90, &vecAxis);
cvxVecTransform(&matrRotation, &vecSecond);
cvxVecNormalize(&vecSecond);
vecAxis.Dir = vecSecond;
svxPoint pntIsect;
if (cvxIsectRayPlane(&vecAxis, &matrPlane, &pntIsect))
continue;
arrAllIsects.push_back(pntIsect);
}
}
for (int i = 0; i < (int)arrNearest.size(); i++)
{
svxPoint& pntCurr = (svxPoint&)arrNearest[i];
svxVector vecCurrent;
svxMatrix matrPlane;
vecCurrent.x = pntCurr.x - pntCenter.x;
vecCurrent.y = pntCurr.y - pntCenter.y;
vecCurrent.z = pntCurr.z - pntCenter.z;
cvxVecNormalize(&vecCurrent);
cvxMatPntVec(&pntCurr, &vecCurrent, &matrPlane);
BOOL bOutside = FALSE;
int j = 0;
while (j < (int)arrAllIsects.size())
{
svxPoint& pntCurr2 = (svxPoint&)arrAllIsects[j];
svxAxis vecCheked;
vecCheked.Dir.x = pntCurr2.x - pntCenter.x;
vecCheked.Dir.y = pntCurr2.y - pntCenter.y;
vecCheked.Dir.z = pntCurr2.z - pntCenter.z;
double dAngle = GetVectorsAngle02PI(&vecCheked.Dir, (svxVector*)&vecCurrent, (svxVector*)&vecNormal);
if ((dAngle >= 89.9999) && (dAngle <= 269.9999))
{
j++;
continue;
}
vecCheked.Pnt = pntCenter;
double dAllowDist = cvxPntDist((svxPoint*)&pntCenter, &pntCurr2); 
svxPoint pntIsect;
if (cvxIsectRayPlane(&vecCheked, &matrPlane, &pntIsect))
continue;
double dCurrD = cvxPntDist((svxPoint*)&pntCenter, &pntIsect);
if (dCurrD > dAllowDist - 0.1)
j++;
else
arrAllIsects.erase(arrAllIsects.begin() + j);
}
}
int i = 0;
while (i < arrAllIsects.size())
{
int j = i + 1;
while (j < arrAllIsects.size())
{
if (cvxPntDist(&arrAllIsects[i], &arrAllIsects[j]) < MIN_DIST)
arrAllIsects.erase(arrAllIsects.begin() + j);
else
j++;
}
i++;
}
copy(arrAllIsects.begin(), arrAllIsects.end(), back_inserter(arrIntersected));
return;
svxVector vecCurrent1;
svxVector vecCurrent2;
for (int i = 0; i < (int)arrNearest.size(); i++)
{
if (arrSideFlag.find(i) != arrSideFlag.end())
continue;
svxPoint& pnt1 = (svxPoint&)arrNearest[i];
int nNextCnt = i + 1;
int nNext = nNextCnt % arrNearest.size();
BOOL bFound = FALSE;
for (int j = 0; j < (int)arrNearest.size(); j++)
{
if (arrSideFlag.find(nNext) == arrSideFlag.end())
{
bFound = TRUE;
break;
}
nNextCnt++;
nNext = nNextCnt % arrNearest.size();
}
if (!bFound)
continue;
svxPoint& pnt2 = (svxPoint&)arrNearest[nNext];
vecCurrent1.x = pntCenter.x - pnt1.x;
vecCurrent1.y = pntCenter.y - pnt1.y;
vecCurrent1.z = pntCenter.z - pnt1.z;
vecCurrent2.x = pntCenter.x - pnt2.x;
vecCurrent2.y = pntCenter.y - pnt2.y;
vecCurrent2.z = pntCenter.z - pnt2.z;
cvxVecNormalize(&vecCurrent1);
cvxVecNormalize(&vecCurrent2);
svxMatrix matrPlane;
cvxMatPntVec(&pnt1, &vecCurrent1, &matrPlane);
svxVector vecCross;
cvxVecCross(&vecCurrent1, &vecCurrent2, &vecCross);
cvxVecNormalize(&vecCross);
svxAxis vecAxis;
vecAxis.Dir = vecCross;
vecAxis.Pnt = pnt2;
svxMatrix matrRotation;
svxVector vecCurrPerp2 = vecCurrent2;
vecAxis.Pnt = pnt2;
cvxMatRotate(&matrRotation, 90, &vecAxis);
cvxVecTransform(&matrRotation, &vecCurrPerp2);
cvxVecNormalize(&vecCurrPerp2);
svxAxis vecSecondLine;
vecSecondLine.Pnt = pnt2;
vecSecondLine.Dir = vecCurrPerp2;
svxPoint pntIsect;
if (cvxIsectRayPlane(&vecSecondLine, &matrPlane, &pntIsect))
continue;
arrIntersected.push_back(pntIsect);
}
}

Previous post Next post
Up