| 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * Further, this software is distributed without any warranty that it is |
| 16 | * free of the rightful claim of any third person regarding infringement |
| 17 | * or the like. Any license provided herein, whether implied or |
| 18 | * otherwise, applies only to this software file. Patent licenses, if |
| 19 | * any, provided herein do not apply to combinations of this program with |
| 20 | * other software, or any other product whatsoever. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 | * Mountain View, CA 94043, or: |
| 28 | * |
| 29 | * http://www.sgi.com |
| 30 | * |
| 31 | * For further information regarding this notice, see: |
| 32 | * |
| 33 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision: 1.1.1.1 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoPickedPoint |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | #include <Inventor/SoPath.h> |
| 55 | #include <Inventor/SoPickedPoint.h> |
| 56 | #include <Inventor/actions/SoGetMatrixAction.h> |
| 57 | #include <Inventor/details/SoDetail.h> |
| 58 | #include <Inventor/elements/SoModelMatrixElement.h> |
| 59 | #include <Inventor/elements/SoPickStyleElement.h> |
| 60 | #include <Inventor/elements/SoTextureMatrixElement.h> |
| 61 | #include <Inventor/elements/SoViewportRegionElement.h> |
| 62 | #include <Inventor/errors/SoDebugError.h> |
| 63 | |
| 64 | SoGetMatrixAction *SoPickedPoint::matrixAction = NULL; |
| 65 | |
| 66 | //////////////////////////////////////////////////////////////////////// |
| 67 | // |
| 68 | // Description: |
| 69 | // Constructor - internal since instances are created only by the |
| 70 | // SoRayPickAction. |
| 71 | // |
| 72 | // Use: internal |
| 73 | |
| 74 | SoPickedPoint::SoPickedPoint(const SoPath *_path, SoState *_state, |
| 75 | const SbVec3f &objSpacePoint) |
| 76 | // |
| 77 | //////////////////////////////////////////////////////////////////////// |
| 78 | { |
| 79 | int i, n; |
| 80 | |
| 81 | // Make a copy of the path since it most likely comes from the |
| 82 | // current traversal path in an action, which will be changed soon. |
| 83 | path = _path->copy(); |
| 84 | path->ref(); |
| 85 | |
| 86 | // Save state so we can get matrices when we need them later |
| 87 | state = _state; |
| 88 | |
| 89 | // Transform the object space point by the current modeling matrix |
| 90 | // to get the world space point |
| 91 | SoModelMatrixElement::get(state).multVecMatrix(objSpacePoint, worldPoint); |
| 92 | |
| 93 | // Make room in the detail list for one detail per node in the |
| 94 | // path. Set all the detail pointers to NULL. |
| 95 | n = ((const SoFullPath *) path)->getLength(); |
| 96 | details.set(n - 1, NULL); // Allocates space |
| 97 | for (i = n - 2; i >= 0; --i) |
| 98 | details.set(i, NULL); |
| 99 | |
| 100 | // Initialize material index to 0, the most common value |
| 101 | materialIndex = 0; |
| 102 | |
| 103 | // Set on-geometry flag based on current pick style |
| 104 | onGeometry = (SoPickStyleElement::get(state) != |
| 105 | SoPickStyleElement::BOUNDING_BOX); |
| 106 | |
| 107 | // Save the viewportRegion, we'll need it later: |
| 108 | vpRegion = SoViewportRegionElement::get(state); |
| 109 | } |
| 110 | |
| 111 | //////////////////////////////////////////////////////////////////////// |
| 112 | // |
| 113 | // Description: |
| 114 | // Copy constructor. |
| 115 | // |
| 116 | // Use: public |
| 117 | |
| 118 | SoPickedPoint::SoPickedPoint(const SoPickedPoint &pp) |
| 119 | // |
| 120 | //////////////////////////////////////////////////////////////////////// |
| 121 | { |
| 122 | worldPoint = pp.worldPoint; |
| 123 | worldNormal = pp.worldNormal; |
| 124 | imageTexCoords = pp.imageTexCoords; |
| 125 | materialIndex = pp.materialIndex; |
| 126 | path = pp.path; |
| 127 | onGeometry = pp.onGeometry; |
| 128 | vpRegion = pp.vpRegion; |
| 129 | |
| 130 | // Copy the details - note that the copy() method for SoDetailList |
| 131 | // makes copies of the detail instances. This has to be done |
| 132 | // because the instances are deleted when the list is deleted. |
| 133 | details = pp.details; |
| 134 | |
| 135 | // Ref the path, since we unref it when we are deleted |
| 136 | path->ref(); |
| 137 | } |
| 138 | |
| 139 | //////////////////////////////////////////////////////////////////////// |
| 140 | // |
| 141 | // Description: |
| 142 | // Destructor |
| 143 | // |
| 144 | // Use: public |
| 145 | |
| 146 | SoPickedPoint::~SoPickedPoint() |
| 147 | // |
| 148 | //////////////////////////////////////////////////////////////////////// |
| 149 | { |
| 150 | // Free up path |
| 151 | path->unref(); |
| 152 | |
| 153 | // The SoDetailList destructor deletes all details in the list, so |
| 154 | // we don't have to do it here |
| 155 | } |
| 156 | |
| 157 | //////////////////////////////////////////////////////////////////////// |
| 158 | // |
| 159 | // Description: |
| 160 | // Returns an instance that is a copy of this instance. The caller |
| 161 | // is responsible for deleting the copy when done. |
| 162 | // |
| 163 | // Use: public |
| 164 | |
| 165 | SoPickedPoint * |
| 166 | SoPickedPoint::copy() const |
| 167 | // |
| 168 | //////////////////////////////////////////////////////////////////////// |
| 169 | { |
| 170 | SoPickedPoint *newCopy = new SoPickedPoint(*this); |
| 171 | return newCopy; |
| 172 | } |
| 173 | |
| 174 | //////////////////////////////////////////////////////////////////////// |
| 175 | // |
| 176 | // Description: |
| 177 | // Returns the detail that corresponds to the given node in the path. |
| 178 | // |
| 179 | // Use: public |
| 180 | |
| 181 | const SoDetail * |
| 182 | SoPickedPoint::getDetail(const SoNode *node) const |
| 183 | // |
| 184 | //////////////////////////////////////////////////////////////////////// |
| 185 | { |
| 186 | int index; |
| 187 | |
| 188 | // Test for default case, corresponding to tail of path |
| 189 | if (node == NULL) |
| 190 | index = ((const SoFullPath *) path)->getLength() - 1; |
| 191 | |
| 192 | else |
| 193 | index = getNodeIndex(node); |
| 194 | |
| 195 | return details[index]; |
| 196 | } |
| 197 | |
| 198 | //////////////////////////////////////////////////////////////////////// |
| 199 | // |
| 200 | // Description: |
| 201 | // Returns the transformation matrix to go from the object space |
| 202 | // corresponding to the given node in the path to world space. |
| 203 | // |
| 204 | // Use: public |
| 205 | |
| 206 | SbMatrix |
| 207 | SoPickedPoint::getObjectToWorld(const SoNode *node) const |
| 208 | // |
| 209 | //////////////////////////////////////////////////////////////////////// |
| 210 | { |
| 211 | getMatrix(node); |
| 212 | return matrixAction->getMatrix(); |
| 213 | } |
| 214 | |
| 215 | //////////////////////////////////////////////////////////////////////// |
| 216 | // |
| 217 | // Description: |
| 218 | // Returns the transformation matrix to go from world space to the |
| 219 | // object space corresponding to the given node in the path. |
| 220 | // |
| 221 | // Use: public |
| 222 | |
| 223 | SbMatrix |
| 224 | SoPickedPoint::getWorldToObject(const SoNode *node) const |
| 225 | // |
| 226 | //////////////////////////////////////////////////////////////////////// |
| 227 | { |
| 228 | getMatrix(node); |
| 229 | return matrixAction->getInverse(); |
| 230 | } |
| 231 | |
| 232 | //////////////////////////////////////////////////////////////////////// |
| 233 | // |
| 234 | // Description: |
| 235 | // Returns the transformation matrix to go from the object space |
| 236 | // corresponding to the given node in the path to image space. |
| 237 | // |
| 238 | // Use: public |
| 239 | |
| 240 | SbMatrix |
| 241 | SoPickedPoint::getObjectToImage(const SoNode *node) const |
| 242 | // |
| 243 | //////////////////////////////////////////////////////////////////////// |
| 244 | { |
| 245 | getMatrix(node); |
| 246 | return matrixAction->getTextureMatrix(); |
| 247 | } |
| 248 | |
| 249 | //////////////////////////////////////////////////////////////////////// |
| 250 | // |
| 251 | // Description: |
| 252 | // Returns the transformation matrix to go from image space to the |
| 253 | // object space corresponding to the given node in the path. |
| 254 | // |
| 255 | // Use: public |
| 256 | |
| 257 | SbMatrix |
| 258 | SoPickedPoint::getImageToObject(const SoNode *node) const |
| 259 | // |
| 260 | //////////////////////////////////////////////////////////////////////// |
| 261 | { |
| 262 | getMatrix(node); |
| 263 | return matrixAction->getTextureInverse(); |
| 264 | } |
| 265 | |
| 266 | //////////////////////////////////////////////////////////////////////// |
| 267 | // |
| 268 | // Description: |
| 269 | // Returns the intersection point in object space that corresponds |
| 270 | // to the given node in the path. |
| 271 | // |
| 272 | // Use: public |
| 273 | |
| 274 | SbVec3f |
| 275 | SoPickedPoint::getObjectPoint(const SoNode *node) const |
| 276 | // |
| 277 | //////////////////////////////////////////////////////////////////////// |
| 278 | { |
| 279 | SbVec3f v; |
| 280 | |
| 281 | getWorldToObject(node).multVecMatrix(worldPoint, v); |
| 282 | |
| 283 | return v; |
| 284 | } |
| 285 | |
| 286 | //////////////////////////////////////////////////////////////////////// |
| 287 | // |
| 288 | // Description: |
| 289 | // Returns the surface normal in object space that corresponds to |
| 290 | // the given node in the path. |
| 291 | // |
| 292 | // Use: public |
| 293 | |
| 294 | SbVec3f |
| 295 | SoPickedPoint::getObjectNormal(const SoNode *node) const |
| 296 | // |
| 297 | //////////////////////////////////////////////////////////////////////// |
| 298 | { |
| 299 | SbVec3f v; |
| 300 | |
| 301 | getWorldToObject(node).multDirMatrix(worldNormal, v); |
| 302 | v.normalize(); |
| 303 | |
| 304 | return v; |
| 305 | } |
| 306 | |
| 307 | //////////////////////////////////////////////////////////////////////// |
| 308 | // |
| 309 | // Description: |
| 310 | // Returns the texture coordinates in object space that corresponds |
| 311 | // to the given node in the path. |
| 312 | // |
| 313 | // Use: public |
| 314 | |
| 315 | SbVec4f |
| 316 | SoPickedPoint::getObjectTextureCoords(const SoNode *node) const |
| 317 | // |
| 318 | //////////////////////////////////////////////////////////////////////// |
| 319 | { |
| 320 | return multVecMatrix4(getImageToObject(node), imageTexCoords); |
| 321 | } |
| 322 | |
| 323 | //////////////////////////////////////////////////////////////////////// |
| 324 | // |
| 325 | // Description: |
| 326 | // Sets the object-space normal. |
| 327 | // |
| 328 | // Use: extender |
| 329 | |
| 330 | void |
| 331 | SoPickedPoint::setObjectNormal(const SbVec3f &normal) |
| 332 | // |
| 333 | //////////////////////////////////////////////////////////////////////// |
| 334 | { |
| 335 | // Transform the object space normal by the current modeling |
| 336 | // matrix o get the world space normal. Use the inverse transpose |
| 337 | // of the odel matrix so that normals are not scaled incorrectly. |
| 338 | SbMatrix normalMatrix = |
| 339 | SoModelMatrixElement::get(state).inverse().transpose(); |
| 340 | |
| 341 | normalMatrix.multDirMatrix(normal, worldNormal); |
| 342 | worldNormal.normalize(); |
| 343 | } |
| 344 | |
| 345 | //////////////////////////////////////////////////////////////////////// |
| 346 | // |
| 347 | // Description: |
| 348 | // Sets the object-space texture coordinates. |
| 349 | // |
| 350 | // Use: extender |
| 351 | |
| 352 | void |
| 353 | SoPickedPoint::setObjectTextureCoords(const SbVec4f &texCoords) |
| 354 | // |
| 355 | //////////////////////////////////////////////////////////////////////// |
| 356 | { |
| 357 | // Transform the object space coords by the current texture matrix |
| 358 | // to get the image space texture coords |
| 359 | imageTexCoords = multVecMatrix4(SoTextureMatrixElement::get(state), |
| 360 | texCoords); |
| 361 | } |
| 362 | |
| 363 | //////////////////////////////////////////////////////////////////////// |
| 364 | // |
| 365 | // Description: |
| 366 | // Sets the detail corresponding to the given node in the path. |
| 367 | // NULL may be passed to remove a detail. |
| 368 | // |
| 369 | // Use: extender |
| 370 | |
| 371 | void |
| 372 | SoPickedPoint::setDetail(SoDetail *detail, SoNode *node) |
| 373 | // |
| 374 | //////////////////////////////////////////////////////////////////////// |
| 375 | { |
| 376 | int i; |
| 377 | |
| 378 | // Find node in path |
| 379 | i = getNodeIndex(node); |
| 380 | |
| 381 | #ifdef DEBUG |
| 382 | if (i < 0) |
| 383 | SoDebugError::post("SoPickedPoint::setDetail", |
| 384 | "Node %#x is not found in path", node); |
| 385 | #endif /* DEBUG */ |
| 386 | |
| 387 | details.set(i, detail); |
| 388 | } |
| 389 | |
| 390 | //////////////////////////////////////////////////////////////////////// |
| 391 | // |
| 392 | // Description: |
| 393 | // Returns index in path of given node, or -1 if not found. |
| 394 | // |
| 395 | // Use: private |
| 396 | |
| 397 | int |
| 398 | SoPickedPoint::getNodeIndex(const SoNode *node) const |
| 399 | // |
| 400 | //////////////////////////////////////////////////////////////////////// |
| 401 | { |
| 402 | int i; |
| 403 | |
| 404 | // Search from bottom up for node in path, since details are |
| 405 | // usually examined near the bottom |
| 406 | for (i = ((const SoFullPath *) path)->getLength() - 1; i >= 0; i--) |
| 407 | if (path->getNode(i) == node) |
| 408 | return i; |
| 409 | |
| 410 | #ifdef DEBUG |
| 411 | SoDebugError::post("SoPickedPoint::getNodeIndex", |
| 412 | "Node %#x is not found in path", node); |
| 413 | #endif /* DEBUG */ |
| 414 | |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | //////////////////////////////////////////////////////////////////////// |
| 419 | // |
| 420 | // Description: |
| 421 | // Applies given instance of SoGetMatrixAction to path to node. |
| 422 | // |
| 423 | // Use: private |
| 424 | |
| 425 | void |
| 426 | SoPickedPoint::getMatrix(const SoNode *node) const |
| 427 | // |
| 428 | //////////////////////////////////////////////////////////////////////// |
| 429 | { |
| 430 | SoPath *xfPath; |
| 431 | |
| 432 | // Construct a path from the root down to this node. Use the given |
| 433 | // path if it's the same |
| 434 | if (node == NULL || node == ((SoFullPath *)path)->getTail()) |
| 435 | xfPath = path; |
| 436 | |
| 437 | else { |
| 438 | int index = getNodeIndex(node); |
| 439 | xfPath = path->copy(0, index + 1); |
| 440 | xfPath->ref(); |
| 441 | } |
| 442 | |
| 443 | // Create an action instance if necessary, then apply it to the path |
| 444 | if (matrixAction == NULL) |
| 445 | matrixAction = new SoGetMatrixAction(vpRegion); |
| 446 | matrixAction->apply(xfPath); |
| 447 | |
| 448 | if (xfPath != path) |
| 449 | xfPath->unref(); |
| 450 | } |
| 451 | |
| 452 | //////////////////////////////////////////////////////////////////////// |
| 453 | // |
| 454 | // Description: |
| 455 | // Multiplies SbVec4f by matrix - for transforming texture coords. |
| 456 | // |
| 457 | // Use: private, static |
| 458 | |
| 459 | SbVec4f |
| 460 | SoPickedPoint::multVecMatrix4(const SbMatrix &m, const SbVec4f v) |
| 461 | // |
| 462 | //////////////////////////////////////////////////////////////////////// |
| 463 | { |
| 464 | int i; |
| 465 | SbVec4f v2; |
| 466 | |
| 467 | for (i = 0; i < 4; i++) |
| 468 | v2[i] = (v[0] * m[0][i] + |
| 469 | v[1] * m[1][i] + |
| 470 | v[2] * m[2][i] + |
| 471 | v[3] * m[3][i]); |
| 472 | |
| 473 | return v2; |
| 474 | } |