/***************************************************************
* Function: applyTranslation()
*
* 'gridSVect': number of snapping segments along each direction
* 'gridUnitLegnth': actual length represented by each segment,
*  only one component of 'gridSVect' is supposed to be non-zero
*
***************************************************************/
void CAVEGroupEditGeodeWireframe::applyTranslation(const osg::Vec3s &gridSVect, const float &gridUnitLegnth,
							const string &gridUnitLegnthInfo)
{
    mMoveSwitch->setAllChildrenOn();

    /* move to other direction: clear all children of 'mMoveSwitch' except child[0], rebuild offset tree */
    if ((mMoveSVect.x() * gridSVect.x() + mMoveSVect.y() * gridSVect.y() + mMoveSVect.z() * gridSVect.z()) <= 0)
    {
	unsigned int numChildren = mMoveSwitch->getNumChildren();
	if (numChildren > 1)
	{
	    mMoveSwitch->removeChildren(1, numChildren - 1);
	    MatrixTransVector::iterator itrMatTrans = mMoveMatTransVector.begin();
	    mMoveMatTransVector.erase(itrMatTrans + 1, itrMatTrans + numChildren);
	}
	mMoveSVect = Vec3s(0, 0, 0);
    }

    /* decide unit offset vector and start/end index of children under 'mMoveSwitch' */
    Vec3 gridOffsetVect;
    short idxStart = 0, idxEnd = 0;
    if (gridSVect.x() != 0)
    {
	idxStart = mMoveSVect.x();
	idxEnd = gridSVect.x();
	if (gridSVect.x() > 0) gridOffsetVect = Vec3(gridUnitLegnth, 0, 0);
	else gridOffsetVect = Vec3(-gridUnitLegnth, 0, 0);
    }
    else if (gridSVect.y() != 0)
    {
	idxStart = mMoveSVect.y();
	idxEnd = gridSVect.y();
	if (gridSVect.y() > 0) gridOffsetVect = Vec3(0, gridUnitLegnth, 0);
	else gridOffsetVect = Vec3(0, -gridUnitLegnth, 0);
    }
    else if (gridSVect.z() != 0)
    {
	idxStart = mMoveSVect.z();
	idxEnd = gridSVect.z();
	if (gridSVect.z() > 0) gridOffsetVect = Vec3(0, 0, gridUnitLegnth);
	else gridOffsetVect = Vec3(0, 0, -gridUnitLegnth);
    } 
    idxStart = idxStart > 0 ? idxStart: -idxStart;
    idxEnd = idxEnd > 0 ? idxEnd : -idxEnd;

    /* update the first wireframe with global translation and rotation */
    if (idxStart == 0) mMoveMatTransVector[0]->setMatrix(mBoundBoxScaleMat * mAccRootMat);

    /* create or remove a sequence of extra children under 'mMoveSwitch' */
    if (idxStart < idxEnd)
    {
	for (short i = idxStart + 1; i <= idxEnd; i++)
	{
	    Matrixd transMat;
	    transMat.makeTranslate(gridOffsetVect * i);
	    MatrixTransform *moveTrans = new MatrixTransform;
	    CAVEGeodeEditWireframeMove *moveGeode = new CAVEGeodeEditWireframeMove;
	    mMoveSwitch->addChild(moveTrans);
	    mMoveMatTransVector.push_back(moveTrans);
	    moveTrans->addChild(moveGeode);
	    moveTrans->setMatrix(mBoundBoxScaleMat * mAccRootMat * transMat);
	}
    }
    else if (idxStart > idxEnd)
    {
	mMoveSwitch->removeChildren(idxEnd + 1, idxStart - idxEnd);
	MatrixTransVector::iterator itrMatTrans = mMoveMatTransVector.begin();
	itrMatTrans += idxEnd + 1;
	mMoveMatTransVector.erase(itrMatTrans, itrMatTrans + (idxStart - idxEnd));
    }

    mMoveSVect = gridSVect;

    if (!mPrimaryFlag) return;

    /* update info text if 'this' wireframe is primary */
    mEditInfoTextSwitch->setAllChildrenOn();

    char info[128];
    sprintf(info, "Offset = %3.2f m\nSnapping = ", gridUnitLegnth * idxEnd);
    mEditInfoText->setText(info + gridUnitLegnthInfo);
}
/***************************************************************
* Function: applyRotation()
*
* 'axisSVect': rotational snapping values around each axis,
*  for instance, Vec3(2, 0, 0) means rotation around X-axis by
*  two times of 'gridUnitAngle'
*  only one component of 'axisIntVect' is supposed to be non-zero
*
***************************************************************/
void CAVEGroupEditGeodeWireframe::applyRotation(const osg::Vec3s &axisSVect, const float &gridUnitAngle,
						const string &gridUnitAngleInfo)
{
    if (!mPrimaryFlag) return;

    mRotateSwitch->setAllChildrenOn();

    /* rotate in other direction: clear all children of 'mRotateSwitch' except child[0], rebuild offset tree */
    if ((mRotateSVect.x() * axisSVect.x() + mRotateSVect.y() * axisSVect.y() + mRotateSVect.z() * axisSVect.z()) <= 0)
    {
	unsigned int numChildren = mRotateSwitch->getNumChildren();
	if (numChildren > 1)
	{
	    mRotateSwitch->removeChildren(1, numChildren - 1);
	    MatrixTransVector::iterator itrMatTrans = mRotateMatTransVector.begin();
	    mRotateMatTransVector.erase(itrMatTrans + 1, itrMatTrans + numChildren);
	}
	mRotateSVect = Vec3s(0, 0, 0);
    }

    /* decide unit rotation quat and start/end index of children under 'mRotateSwitch' */
    Vec3 gridRotationAxis;
    short idxStart = 0, idxEnd = 0;
    if (axisSVect.x() != 0)
    {
	idxStart = mRotateSVect.x();
	idxEnd = axisSVect.x();
	if (axisSVect.x() > 0) gridRotationAxis = Vec3(1, 0, 0);
	else gridRotationAxis = Vec3(-1, 0, 0);
    }
    else if (axisSVect.y() != 0)
    {
	idxStart = mRotateSVect.y();
	idxEnd = axisSVect.y();
	if (axisSVect.y() > 0) gridRotationAxis = Vec3(0, 1, 0);
	else gridRotationAxis = Vec3(0, -1, 0);
    }
    else if (axisSVect.z() != 0)
    {
	idxStart = mRotateSVect.z();
	idxEnd = axisSVect.z();
	if (axisSVect.z() > 0) gridRotationAxis = Vec3(0, 0, 1);
	else gridRotationAxis = Vec3(0, 0, -1);
    }
    idxStart = idxStart > 0 ? idxStart: -idxStart;
    idxEnd = idxEnd > 0 ? idxEnd : -idxEnd;

    /* create or remove a sequence of extra children under 'mRotateSwitch' */
    if (idxStart < idxEnd)
    {
	for (short i = idxStart + 1; i <= idxEnd; i++)
	{
	    Matrixd rotMat;
	    rotMat.makeRotate(gridUnitAngle * i, gridRotationAxis);
	    MatrixTransform *rotateTrans = new MatrixTransform;
	    CAVEGeodeEditWireframeRotate *rotateGeode = new CAVEGeodeEditWireframeRotate;
	    mRotateSwitch->addChild(rotateTrans);
	    mRotateMatTransVector.push_back(rotateTrans);
	    rotateTrans->addChild(rotateGeode);
	    rotateTrans->setMatrix(mBoundSphereScaleMat * rotMat);
	}
    }
    else if (idxStart > idxEnd)
    {
	mRotateSwitch->removeChildren(idxEnd + 1, idxStart - idxEnd);
	MatrixTransVector::iterator itrMatTrans = mRotateMatTransVector.begin();
	itrMatTrans += idxEnd + 1;
	mRotateMatTransVector.erase(itrMatTrans, itrMatTrans + (idxStart - idxEnd));
    }

    mRotateSVect = axisSVect;

    /* update info text if 'this' wireframe is primary */
    mEditInfoTextSwitch->setAllChildrenOn();

    char info[128];
    const float gridUnitAngleDegree = gridUnitAngle * 180 / M_PI;
    sprintf(info, "Angle = %3.2f\nSnapping = ", gridUnitAngleDegree * idxEnd);
    mEditInfoText->setText(info + gridUnitAngleInfo);
}
Beispiel #3
0
static bool
buildBoxData( const osg::Vec3& halfExtents, const osg::Vec3s& subdivisions, osg::Geometry* geom )
{
    if( ( subdivisions.x() <= 0. ) || ( subdivisions.y() <= 0. ) || ( subdivisions.z() <= 0. ) )
    {
        osg::notify( osg::WARN ) << "osgwTools: makeBox: Invalid subdivisions." << std::endl;
        return( false );
    }
    const unsigned short subX( (unsigned short)( subdivisions.x() ) );
    const unsigned short subY( (unsigned short)( subdivisions.y() ) );
    const unsigned short subZ( (unsigned short)( subdivisions.z() ) );

    const float xMin( -halfExtents[ 0 ] );
    const float xMax( halfExtents[ 0 ] );
    const float yMin( -halfExtents[ 1 ] );
    const float yMax( halfExtents[ 1 ] );
    const float zMin( -halfExtents[ 2 ] );
    const float zMax( halfExtents[ 2 ] );

    geom->setVertexArray( new osg::Vec3Array );
    geom->setNormalArray( new osg::Vec3Array );
    geom->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
    geom->setTexCoordArray( 0, new osg::Vec2Array );
    {
        osg::Vec4Array* osgC = new osg::Vec4Array;
        osgC->push_back( osg::Vec4( 1., 1., 1., 1. ) );
        geom->setColorArray( osgC );
        geom->setColorBinding( osg::Geometry::BIND_OVERALL );
    }

    // +x
    addPlaneData( osg::Vec3( xMax, yMin, zMin ),
        osg::Vec3( 0., yMax-yMin, 0. ), subY,
        osg::Vec3( 0., 0., zMax-zMin ), subZ,
        osg::Vec3( 1., 0., 0. ), geom );

    // -x
    addPlaneData( osg::Vec3( xMin, yMax, zMin ),
        osg::Vec3( 0., yMin-yMax, 0. ), subY,
        osg::Vec3( 0., 0., zMax-zMin ), subZ,
        osg::Vec3( -1., 0., 0. ), geom );

    // +y
    addPlaneData( osg::Vec3( xMax, yMax, zMin ),
        osg::Vec3( xMin-xMax, 0., 0. ), subX,
        osg::Vec3( 0., 0., zMax-zMin ), subZ,
        osg::Vec3( 0., 1., 0. ), geom );

    // -y
    addPlaneData( osg::Vec3( xMin, yMin, zMin ),
        osg::Vec3( xMax-xMin, 0., 0. ), subX,
        osg::Vec3( 0., 0., zMax-zMin ), subZ,
        osg::Vec3( 0., -1., 0. ), geom );

    // +z
    addPlaneData( osg::Vec3( xMin, yMin, zMax ),
        osg::Vec3( xMax-xMin, 0., 0. ), subX,
        osg::Vec3( 0., yMax-yMin, 0. ), subY,
        osg::Vec3( 0., 0., 1. ), geom );

    // -z
    addPlaneData( osg::Vec3( xMax, yMin, zMin ),
        osg::Vec3( xMin-xMax, 0., 0. ), subX,
        osg::Vec3( 0., yMax-yMin, 0. ), subY,
        osg::Vec3( 0., 0., -1. ), geom );

    return( true );
}