OpenSceneGraph/src/osgVolume/Locator.cpp

285 lines
7.7 KiB
C++
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgVolume/Locator>
#include <osg/io_utils>
#include <osg/Notify>
#include <osg/FrontFace>
#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,
minX, minY, minZ, 1.0);
_inverse.invert(_transform);
locatorModified();
}
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;
}
bool Locator::computeLocalBounds(Locator& /*source*/, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const
{
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());
}
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;
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;
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(;
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());
}
return true;
}
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);
Fixed a range of issues reported by cppcheck: [examples/osgphotoalbum/PhotoArchive.cpp:56]: (error) Memory leak: fileIndentifier [examples/osgphotoalbum/PhotoArchive.cpp:257]: (error) Deallocating a deallocated pointer: newData [examples/osgphotoalbum/PhotoArchive.cpp:318]: (error) Deallocating a deallocated pointer: newData [src/osg/ImageUtils.cpp:116]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:307]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:312]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:367]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:399]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:400]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:482]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:483]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:484]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:519]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/ImageUtils.cpp:536]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:71]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:74]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:77]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:82]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:102]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:107]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:599]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:600]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:601]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:602]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:603]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:604]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:605]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osg/OcclusionQueryNode.cpp:606]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:134]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:135]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:136]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:137]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:139]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:177]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:178]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:195]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:198]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:203]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:205]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/ExternalFileWriter.cpp:253]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers. [src/osgDB/InputStream.cpp:553]: (error) Memory leak: data [src/osgDB/OutputStream.cpp:393]: (error) Memory leak: data [src/osgPlugins/Inventor/ConvertToInventor.cpp:656]: (error) Mismatching allocation and deallocation: tmpArray [src/osgPlugins/Inventor/ReaderWriterIV.cpp:237]: (error) Common realloc mistake: 'buf' nulled but not freed upon failure [src/osgPlugins/OpenFlight/expGeometryRecords.cpp:167]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers. [src/osgPlugins/OpenFlight/expGeometryRecords.cpp:373]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers. [src/osgPlugins/cfg/CameraConfig.cpp:635]: (error) Unusual pointer arithmetic [src/osgPlugins/freetype/FreeTypeLibrary.cpp:122]: (error) Memory leak: buffer [src/osgPlugins/geo/ReaderWriterGEO.cpp:210]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 211 [src/osgPlugins/geo/ReaderWriterGEO.cpp:227]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 228 [src/osgPlugins/geo/ReaderWriterGEO.cpp:903]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 904 [src/osgPlugins/geo/osgGeoNodes.h:180]: (error) Memory leak: geoHeaderGeo::intVars [src/osgPlugins/geo/osgGeoNodes.h:181]: (error) Memory leak: geoHeaderGeo::useVars [src/osgPlugins/geo/osgGeoNodes.h:182]: (error) Memory leak: geoHeaderGeo::extVars [src/osgPlugins/md2/ReaderWriterMD2.cpp:180]: (error) Memory leak: mapbase [src/osgPlugins/md2/ReaderWriterMD2.cpp:166]: (error) Resource leak: file_fd [src/osgPlugins/pic/ReaderWriterPIC.cpp:152]: (error) Mismatching allocation and deallocation: tmpbuf [src/osgPlugins/pic/ReaderWriterPIC.cpp:153]: (error) Mismatching allocation and deallocation: buffer [src/osgPlugins/ply/plyfile.cpp:843]: (error) Memory leak: plyfile [src/osgPlugins/pvr/ReaderWriterPVR.cpp:179]: (error) Memory leak: imageData [src/osgPlugins/shp/ESRIShapeParser.cpp:29]: (error) Resource leak: fd [src/osgPlugins/shp/XBaseParser.cpp:96]: (error) Resource leak: fd [src/osgPlugins/zip/unzip.cpp:3158]: (error) Possible null pointer dereference: s - otherwise it is redundant to check if s is null at line 3159 [src/osgPlugins/zip/unzip.cpp:4155]: (error) Dangerous usage of 'rd' (strncpy doesn't always 0-terminate it) [src/osgShadow/MinimalCullBoundsShadowMap.cpp:334]: (error) Possible null pointer dereference: rl - otherwise it is redundant to check if rl is null at line 331 [src/osgViewer/ScreenCaptureHandler.cpp:617]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 611 [src/osgViewer/ScreenCaptureHandler.cpp:632]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 626 [src/osgVolume/Locator.cpp:209]: (error) Dangerous iterator usage after erase()-method. [src/osgVolume/RayTracedTechnique.cpp:274]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259 [src/osgVolume/RayTracedTechnique.cpp:275]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259 [src/osgWrappers/serializers/osg/ShaderBinary.cpp:28]: (error) Mismatching allocation and deallocation: data
2011-06-21 03:15:53 +08:00
return;
}
}
}
void Locator::locatorModified()
{
for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin();
itr != _locatorCallbacks.end();
++itr)
{
(*itr)->locatorModified(this);
}
}
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));
float volume = (xAxis^yAxis)*zAxis;
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);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TransformLocatorCallback
//
TransformLocatorCallback::TransformLocatorCallback(osg::MatrixTransform* transform):
_transform(transform)
{}
void TransformLocatorCallback::locatorModified(Locator* locator)
{
if (_transform.valid())
{
locator->applyAppropriateFrontFace(_transform->getOrCreateStateSet());
_transform->setMatrix(locator->getTransform());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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()));
}