On 09/10/2015 at 12:06, xxxxxxxx wrote:
The code was posted in C++ so I don't know if it will help you or not:
https://plugincafe.maxon.net/topic/6483/6972_uv-mirror-hv&KW=Frontal&PN=1
This is what I have in my C++ notes:
//This code creates the equivalent to the flat mapping button in the BP UV options
#define SIGDIG 5.0 //Define a value macro to be used several times later on
static Real sign(Real n)
{
if( n > 0.0 ) return 1.0; //Returns zero by default
if( n < 0.0 ) return -1.0; //Otherwise Returns 1.0 or -1.0
return 0.0;
}
static Real TrimDecimal(Real num, Real digits)
{
Real n;
n = num * Pow((Real)10.0, digits);
n = sign(n) * Abs(Floor(n + 0.5));
return n / Pow((Real)10.0, digits);
}
Bool FrontalMapUVs(PolygonObject *op, Matrix mg)
{
if( !op ) return false;
LONG numFaces = op->GetPolygonCount();
LONG numVerts = op->GetPointCount();
Vector *pVerts = op->GetPointW();
CPolygon *pPolys = op->GetPolygonW();
UVWTag *pUVTag = (UVWTag * )op->MakeVariableTag(Tuvw, numFaces);
if( !pUVTag ) return false;
UVWHandle pUVHndl = pUVTag->GetDataAddressW();
if( !pUVHndl ) return false;
LONG ndx;
Real lenxinv, lenyinv, lenzinv;
Vector mapCenter, mapSize, vMin, vMax;
vMin.x = 100000.0;
vMin.y = 100000.0;
vMin.z = 100000.0;
vMax.x = -100000.0;
vMax.y = -100000.0;
vMax.z = -100000.0;
for(ndx=0; ndx<numVerts; ndx++)
{
Vector pt = pVerts[ndx] * mg;
if( pt.x < vMin.x ) vMin.x = pt.x;
if( pt.x > vMax.x ) vMax.x = pt.x;
if( pt.y < vMin.y ) vMin.y = pt.y;
if( pt.y > vMax.y ) vMax.y = pt.y;
if( pt.z < vMin.z ) vMin.z = pt.z;
if( pt.z > vMax.z ) vMax.z = pt.z;
}
mapSize.x = Abs(vMax.x - vMin.x);
mapSize.y = Abs(vMax.y - vMin.y);
mapSize.z = Abs(vMax.z - vMin.z);
mapCenter.x = vMin.x+(mapSize.x*0.5);
mapCenter.y = vMin.y+(mapSize.y*0.5);
mapCenter.z = vMin.z+(mapSize.z*0.5);
if (mapSize.x!=0.0) lenxinv = 1.0/mapSize.x; else lenxinv = 0.0;
if (mapSize.y!=0.0) lenyinv = 1.0/mapSize.y; else lenyinv = 0.0;
if (mapSize.z!=0.0) lenzinv = 1.0/mapSize.z; else lenzinv = 0.0;
// Walk the list of polygons and map the UVs
for(ndx=0; ndx<numFaces; ndx++)
{
UVWStruct uvw;
pUVTag->Get(pUVHndl, ndx, uvw);
Vector pt_a = (pVerts[pPolys[ndx].a] - mapCenter) + mg.off;
Vector pt_b = (pVerts[pPolys[ndx].b] - mapCenter) + mg.off;
Vector pt_c = (pVerts[pPolys[ndx].c] - mapCenter) + mg.off;
Vector pt_d = (pVerts[pPolys[ndx].d] - mapCenter) + mg.off;
uvw.a.x = TrimDecimal((pt_a.x*lenxinv)+0.5, SIGDIG);
uvw.a.y = TrimDecimal((-pt_a.y*lenyinv)+0.5, SIGDIG);
uvw.b.x = TrimDecimal((pt_b.x*lenxinv)+0.5, SIGDIG);
uvw.b.y = TrimDecimal((-pt_b.y*lenyinv)+0.5, SIGDIG);
uvw.c.x = TrimDecimal((pt_c.x*lenxinv)+0.5, SIGDIG);
uvw.c.y = TrimDecimal((-pt_c.y*lenyinv)+0.5, SIGDIG);
uvw.d.x = TrimDecimal((pt_d.x*lenxinv)+0.5, SIGDIG);
uvw.d.y = TrimDecimal((-pt_d.y*lenyinv)+0.5, SIGDIG);
pUVTag->Set(pUVHndl, ndx, uvw);
}
return true;
}
-ScottA