| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgVolume/Locator> |
|---|
| 15 | #include <osg/io_utils> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | #include <list> |
|---|
| 19 | |
|---|
| 20 | using namespace osgVolume; |
|---|
| 21 | |
|---|
| 22 | void Locator::setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ) |
|---|
| 23 | { |
|---|
| 24 | _transform.set(maxX-minX, 0.0, 0.0, 0.0, |
|---|
| 25 | 0.0, maxY-minY, 0.0, 0.0, |
|---|
| 26 | 0.0, 0.0, maxZ-minZ, 0.0, |
|---|
| 27 | minX, minY, minZ, 1.0); |
|---|
| 28 | |
|---|
| 29 | _inverse.invert(_transform); |
|---|
| 30 | |
|---|
| 31 | locatorModified(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | bool Locator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const |
|---|
| 35 | { |
|---|
| 36 | world = local * _transform; |
|---|
| 37 | return true; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | bool Locator::convertModelToLocal(const osg::Vec3d& world, osg::Vec3d& local) const |
|---|
| 41 | { |
|---|
| 42 | local = world * _inverse; |
|---|
| 43 | return true; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | bool Locator::computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const |
|---|
| 47 | { |
|---|
| 48 | typedef std::list<osg::Vec3d> Corners; |
|---|
| 49 | Corners corners; |
|---|
| 50 | |
|---|
| 51 | osg::Vec3d cornerNDC; |
|---|
| 52 | if (convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), cornerNDC)) |
|---|
| 53 | { |
|---|
| 54 | corners.push_back(cornerNDC); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | if (convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), cornerNDC)) |
|---|
| 58 | { |
|---|
| 59 | corners.push_back(cornerNDC); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | if (convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), cornerNDC)) |
|---|
| 63 | { |
|---|
| 64 | corners.push_back(cornerNDC); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | if (convertLocalToModel(osg::Vec3d(1.0,1.0,0.0), cornerNDC)) |
|---|
| 68 | { |
|---|
| 69 | corners.push_back(cornerNDC); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | if (convertLocalToModel(osg::Vec3d(0.0,0.0,1.0), cornerNDC)) |
|---|
| 73 | { |
|---|
| 74 | corners.push_back(cornerNDC); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if (convertLocalToModel(osg::Vec3d(1.0,0.0,1.0), cornerNDC)) |
|---|
| 78 | { |
|---|
| 79 | corners.push_back(cornerNDC); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | if (convertLocalToModel(osg::Vec3d(0.0,1.0,1.0), cornerNDC)) |
|---|
| 83 | { |
|---|
| 84 | corners.push_back(cornerNDC); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | if (convertLocalToModel(osg::Vec3d(1.0,1.0,1.0), cornerNDC)) |
|---|
| 88 | { |
|---|
| 89 | corners.push_back(cornerNDC); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (corners.empty()) return false; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | for(Corners::iterator itr = corners.begin(); |
|---|
| 96 | itr != corners.end(); |
|---|
| 97 | ++itr) |
|---|
| 98 | { |
|---|
| 99 | bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x()); |
|---|
| 100 | bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y()); |
|---|
| 101 | bottomLeft.z() = osg::minimum( bottomLeft.z(), itr->z()); |
|---|
| 102 | topRight.x() = osg::maximum( topRight.x(), itr->x()); |
|---|
| 103 | topRight.y() = osg::maximum( topRight.y(), itr->y()); |
|---|
| 104 | topRight.z() = osg::maximum( topRight.z(), itr->z()); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | return true; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | bool Locator::computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const |
|---|
| 111 | { |
|---|
| 112 | OSG_NOTICE<<"Locator::computeLocalBounds"<<std::endl; |
|---|
| 113 | |
|---|
| 114 | typedef std::list<osg::Vec3d> Corners; |
|---|
| 115 | Corners corners; |
|---|
| 116 | |
|---|
| 117 | osg::Vec3d cornerNDC; |
|---|
| 118 | if (convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), cornerNDC)) |
|---|
| 119 | { |
|---|
| 120 | corners.push_back(cornerNDC); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | if (convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), cornerNDC)) |
|---|
| 124 | { |
|---|
| 125 | corners.push_back(cornerNDC); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | if (convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), cornerNDC)) |
|---|
| 129 | { |
|---|
| 130 | corners.push_back(cornerNDC); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (convertLocalToModel(osg::Vec3d(1.0,1.0,0.0), cornerNDC)) |
|---|
| 134 | { |
|---|
| 135 | corners.push_back(cornerNDC); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | if (convertLocalToModel(osg::Vec3d(0.0,0.0,1.0), cornerNDC)) |
|---|
| 139 | { |
|---|
| 140 | corners.push_back(cornerNDC); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | if (convertLocalToModel(osg::Vec3d(1.0,0.0,1.0), cornerNDC)) |
|---|
| 144 | { |
|---|
| 145 | corners.push_back(cornerNDC); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | if (convertLocalToModel(osg::Vec3d(0.0,1.0,1.0), cornerNDC)) |
|---|
| 149 | { |
|---|
| 150 | corners.push_back(cornerNDC); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | if (convertLocalToModel(osg::Vec3d(1.0,1.0,1.0), cornerNDC)) |
|---|
| 154 | { |
|---|
| 155 | corners.push_back(cornerNDC); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if (corners.empty()) return false; |
|---|
| 159 | |
|---|
| 160 | Corners::iterator itr = corners.begin(); |
|---|
| 161 | |
|---|
| 162 | bottomLeft.x() = topRight.x() = itr->x(); |
|---|
| 163 | bottomLeft.y() = topRight.y() = itr->y(); |
|---|
| 164 | bottomLeft.z() = topRight.z() = itr->z(); |
|---|
| 165 | |
|---|
| 166 | ++itr; |
|---|
| 167 | |
|---|
| 168 | for(; |
|---|
| 169 | itr != corners.end(); |
|---|
| 170 | ++itr) |
|---|
| 171 | { |
|---|
| 172 | bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x()); |
|---|
| 173 | bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y()); |
|---|
| 174 | bottomLeft.z() = osg::minimum( bottomLeft.z(), itr->z()); |
|---|
| 175 | topRight.x() = osg::maximum( topRight.x(), itr->x()); |
|---|
| 176 | topRight.y() = osg::maximum( topRight.y(), itr->y()); |
|---|
| 177 | topRight.z() = osg::maximum( topRight.z(), itr->z()); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | return true; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | void Locator::addCallback(LocatorCallback* callback) |
|---|
| 184 | { |
|---|
| 185 | |
|---|
| 186 | for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin(); |
|---|
| 187 | itr != _locatorCallbacks.end(); |
|---|
| 188 | ++itr) |
|---|
| 189 | { |
|---|
| 190 | if (*itr == callback) |
|---|
| 191 | { |
|---|
| 192 | return; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | _locatorCallbacks.push_back(callback); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | void Locator::removeCallback(LocatorCallback* callback) |
|---|
| 201 | { |
|---|
| 202 | |
|---|
| 203 | for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin(); |
|---|
| 204 | itr != _locatorCallbacks.end(); |
|---|
| 205 | ++itr) |
|---|
| 206 | { |
|---|
| 207 | if (*itr == callback) |
|---|
| 208 | { |
|---|
| 209 | _locatorCallbacks.erase(itr); |
|---|
| 210 | return; |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | void Locator::locatorModified() |
|---|
| 216 | { |
|---|
| 217 | for(LocatorCallbacks::iterator itr = _locatorCallbacks.begin(); |
|---|
| 218 | itr != _locatorCallbacks.end(); |
|---|
| 219 | ++itr) |
|---|
| 220 | { |
|---|
| 221 | (*itr)->locatorModified(this); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | } |
|---|