2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
|
2009-01-05 19:26:26 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2009-01-05 19:26:26 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2009-01-05 19:26:26 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-01-05 19:26:26 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgVolume/Locator>
|
2009-08-28 03:25:23 +08:00
|
|
|
#include <osg/io_utils>
|
|
|
|
#include <osg/Notify>
|
2016-11-11 21:54:15 +08:00
|
|
|
#include <osg/FrontFace>
|
2009-01-05 19:26:26 +08:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using namespace osgVolume;
|
|
|
|
|
|
|
|
void Locator::setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ)
|
|
|
|
{
|
|
|
|
_transform.set(maxX-minX, 0.0, 0.0, 0.0,
|
|
|
|
0.0, maxY-minY, 0.0, 0.0,
|
|
|
|
0.0, 0.0, maxZ-minZ, 0.0,
|
2012-03-22 01:36:20 +08:00
|
|
|
minX, minY, minZ, 1.0);
|
2009-01-05 19:26:26 +08:00
|
|
|
|
|
|
|
_inverse.invert(_transform);
|
2009-07-03 13:25:08 +08:00
|
|
|
|
|
|
|
locatorModified();
|
2009-01-05 19:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Locator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const
|
|
|
|
{
|
|
|
|
world = local * _transform;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Locator::convertModelToLocal(const osg::Vec3d& world, osg::Vec3d& local) const
|
|
|
|
{
|
|
|
|
local = world * _inverse;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:00:43 +08:00
|
|
|
bool Locator::computeLocalBounds(Locator& /*source*/, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const
|
2009-01-05 19:26:26 +08:00
|
|
|
{
|
|
|
|
typedef std::list<osg::Vec3d> Corners;
|
|
|
|
Corners corners;
|
|
|
|
|
|
|
|
osg::Vec3d cornerNDC;
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,1.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,0.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,0.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,1.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,1.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (corners.empty()) return false;
|
|
|
|
|
|
|
|
|
|
|
|
for(Corners::iterator itr = corners.begin();
|
|
|
|
itr != corners.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x());
|
|
|
|
bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y());
|
|
|
|
bottomLeft.z() = osg::minimum( bottomLeft.z(), itr->z());
|
|
|
|
topRight.x() = osg::maximum( topRight.x(), itr->x());
|
|
|
|
topRight.y() = osg::maximum( topRight.y(), itr->y());
|
|
|
|
topRight.z() = osg::maximum( topRight.z(), itr->z());
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-01-05 19:26:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Locator::computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const
|
|
|
|
{
|
2013-08-22 21:39:36 +08:00
|
|
|
OSG_INFO<<"Locator::computeLocalBounds"<<std::endl;
|
2009-08-28 03:25:23 +08:00
|
|
|
|
2009-01-05 19:26:26 +08:00
|
|
|
typedef std::list<osg::Vec3d> Corners;
|
|
|
|
Corners corners;
|
|
|
|
|
|
|
|
osg::Vec3d cornerNDC;
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,1.0,0.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,0.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,0.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(0.0,1.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (convertLocalToModel(osg::Vec3d(1.0,1.0,1.0), cornerNDC))
|
|
|
|
{
|
|
|
|
corners.push_back(cornerNDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (corners.empty()) return false;
|
|
|
|
|
2009-08-28 03:25:23 +08:00
|
|
|
Corners::iterator itr = corners.begin();
|
|
|
|
|
|
|
|
bottomLeft.x() = topRight.x() = itr->x();
|
|
|
|
bottomLeft.y() = topRight.y() = itr->y();
|
|
|
|
bottomLeft.z() = topRight.z() = itr->z();
|
|
|
|
|
|
|
|
++itr;
|
|
|
|
|
|
|
|
for(;
|
2009-01-05 19:26:26 +08:00
|
|
|
itr != corners.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x());
|
|
|
|
bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y());
|
|
|
|
bottomLeft.z() = osg::minimum( bottomLeft.z(), itr->z());
|
|
|
|
topRight.x() = osg::maximum( topRight.x(), itr->x());
|
|
|
|
topRight.y() = osg::maximum( topRight.y(), itr->y());
|
|
|
|
topRight.z() = osg::maximum( topRight.z(), itr->z());
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-01-05 19:26:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-03 13:25:08 +08:00
|
|
|
|
|
|
|
void Locator::addCallback(LocatorCallback* callback)
|
|
|
|
{
|
|
|
|
// check if callback is already attached, if so just return early
|
|
|
|
for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin();
|
|
|
|
itr != _locatorCallbacks.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (*itr == callback)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback is not attached so now attach it.
|
|
|
|
_locatorCallbacks.push_back(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Locator::removeCallback(LocatorCallback* callback)
|
|
|
|
{
|
|
|
|
// checl if callback is attached, if so erase it.
|
|
|
|
for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin();
|
|
|
|
itr != _locatorCallbacks.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if (*itr == callback)
|
|
|
|
{
|
|
|
|
_locatorCallbacks.erase(itr);
|
2011-06-21 03:15:53 +08:00
|
|
|
return;
|
2009-07-03 13:25:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Locator::locatorModified()
|
|
|
|
{
|
|
|
|
for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin();
|
|
|
|
itr != _locatorCallbacks.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->locatorModified(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-12-07 03:31:12 +08:00
|
|
|
|
2016-11-11 21:54:15 +08:00
|
|
|
bool Locator::inverted() const
|
|
|
|
{
|
|
|
|
osg::Vec3d xAxis(_transform(0,0), _transform(1,0), _transform(2,0));
|
|
|
|
osg::Vec3d yAxis(_transform(0,1), _transform(1,1), _transform(2,1));
|
|
|
|
osg::Vec3d zAxis(_transform(0,2), _transform(1,2), _transform(2,2));
|
2016-11-12 01:16:40 +08:00
|
|
|
double volume = (xAxis^yAxis)*zAxis;
|
2016-11-11 21:54:15 +08:00
|
|
|
return volume<0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Locator::applyAppropriateFrontFace(osg::StateSet* ss) const
|
|
|
|
{
|
|
|
|
osg::StateAttribute* sa = ss->getAttribute(osg::StateAttribute::FRONTFACE);
|
|
|
|
osg::FrontFace* ff = dynamic_cast<osg::FrontFace*>(sa);
|
|
|
|
if (!ff)
|
|
|
|
{
|
|
|
|
ff = new osg::FrontFace;
|
|
|
|
ss->setAttribute(ff);
|
|
|
|
}
|
|
|
|
ff->setMode( inverted() ? osg::FrontFace::CLOCKWISE : osg::FrontFace::COUNTER_CLOCKWISE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-07 03:31:12 +08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TransformLocatorCallback
|
|
|
|
//
|
|
|
|
TransformLocatorCallback::TransformLocatorCallback(osg::MatrixTransform* transform):
|
|
|
|
_transform(transform)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void TransformLocatorCallback::locatorModified(Locator* locator)
|
|
|
|
{
|
2016-11-11 21:54:15 +08:00
|
|
|
if (_transform.valid())
|
|
|
|
{
|
|
|
|
locator->applyAppropriateFrontFace(_transform->getOrCreateStateSet());
|
|
|
|
|
|
|
|
_transform->setMatrix(locator->getTransform());
|
|
|
|
}
|
2013-12-07 03:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TexGenLocatorCallback
|
|
|
|
//
|
|
|
|
TexGenLocatorCallback::TexGenLocatorCallback(osg::TexGen* texgen, Locator* geometryLocator, Locator* imageLocator):
|
|
|
|
_texgen(texgen),
|
|
|
|
_geometryLocator(geometryLocator),
|
|
|
|
_imageLocator(imageLocator) {}
|
|
|
|
|
|
|
|
void TexGenLocatorCallback::locatorModified(Locator*)
|
|
|
|
{
|
|
|
|
if (!_texgen || !_geometryLocator || !_imageLocator) return;
|
|
|
|
|
|
|
|
_texgen->setPlanesFromMatrix(
|
|
|
|
_geometryLocator->getTransform() *
|
|
|
|
osg::Matrix::inverse(_imageLocator->getTransform()));
|
|
|
|
}
|