| 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,92 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 |
| SbLineProjector |
| 47 |
| |
| 48 |
| Author(s) : Howard Look |
| 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/projectors/SbLineProjector.h> |
| 55 |
#include <Inventor/errors/SoDebugError.h> |
| 56 |
#include <cstdio> |
| 57 |
|
| 58 |
//////////////////////////////////////////////////////////////////////// |
| 59 |
// |
| 60 |
// Description: |
| 61 |
// Constructors |
| 62 |
// |
| 63 |
// Use: public |
| 64 |
// |
| 65 |
//////////////////////////////////////////////////////////////////////// |
| 66 |
|
| 67 |
SbLineProjector::SbLineProjector() |
| 68 |
{ |
| 69 |
setLine(SbLine(SbVec3f(0,0,0), SbVec3f(0,1,0))); |
| 70 |
} |
| 71 |
|
| 72 |
//////////////////////////////////////////////////////////////////////// |
| 73 |
// |
| 74 |
// Description: |
| 75 |
// Returns an instance that is a copy of this instance. The caller |
| 76 |
// is responsible for deleting the copy when done. |
| 77 |
// |
| 78 |
// Use: public, virtual |
| 79 |
// |
| 80 |
|
| 81 |
SbProjector * |
| 82 |
SbLineProjector::copy() const |
| 83 |
// |
| 84 |
//////////////////////////////////////////////////////////////////////// |
| 85 |
{ |
| 86 |
SbLineProjector *newProjector = new SbLineProjector; |
| 87 |
|
| 88 |
(*newProjector) = (*this); |
| 89 |
|
| 90 |
return newProjector; |
| 91 |
} |
| 92 |
//////////////////////////////////////////////////////////////////////// |
| 93 |
// |
| 94 |
// Description: |
| 95 |
// Misc set routines. |
| 96 |
// |
| 97 |
// Use: public |
| 98 |
// |
| 99 |
//////////////////////////////////////////////////////////////////////// |
| 100 |
|
| 101 |
void |
| 102 |
SbLineProjector::setLine(const SbLine &l) |
| 103 |
{ |
| 104 |
line = l; |
| 105 |
} |
| 106 |
|
| 107 |
//////////////////////////////////////////////////////////////////////// |
| 108 |
// |
| 109 |
// Description: |
| 110 |
// Project onto this projector. |
| 111 |
// |
| 112 |
// Originally this method created a working line by projecting the input |
| 113 |
// point into the scene, then transforming that line into workspace. |
| 114 |
// Afterwards, the point on the projector's line closest to the workingLine |
| 115 |
// was used as the result. |
| 116 |
// |
| 117 |
// This technique provides good results when the mouse is on the line, |
| 118 |
// but when it leaves the line, the closest point is not optimal. |
| 119 |
// |
| 120 |
// The new method first transforms the projector's line into screen space, |
| 121 |
// then finds the nearest point on the screenSpaceLine to the input point. |
| 122 |
// It then uses this new screen space line and then performs the projection |
| 123 |
// as it used to. |
| 124 |
// |
| 125 |
// The result is that the screen point which we project into the scene |
| 126 |
// is now guaranteed to lie on the working line. |
| 127 |
// |
| 128 |
// In short, first we find the closest point in screen space, then |
| 129 |
// we project that point back into work space. |
| 130 |
// |
| 131 |
// Use: public |
| 132 |
|
| 133 |
|
| 134 |
SbVec3f |
| 135 |
SbLineProjector::project(const SbVec2f &point) |
| 136 |
// |
| 137 |
//////////////////////////////////////////////////////////////////////// |
| 138 |
{ |
| 139 |
// Convert two line points to world space |
| 140 |
SbLine worldLine; |
| 141 |
workingToWorld.multLineMatrix( line, worldLine ); |
| 142 |
SbVec3f wldPt1 = worldLine.getPosition(); |
| 143 |
SbVec3f wldDir = worldLine.getDirection(); |
| 144 |
SbVec3f wldPt2 = wldPt1 + wldDir; |
| 145 |
|
| 146 |
// Convert two line points to normalized screen space. |
| 147 |
SbVec3f nrmScnPt1, nrmScnPt2; |
| 148 |
viewVol.projectToScreen( wldPt1, nrmScnPt1 ); |
| 149 |
viewVol.projectToScreen( wldPt2, nrmScnPt2 ); |
| 150 |
|
| 151 |
// Convert two line points and input point |
| 152 |
// to viewPlane space, a screen space that's got view plane's aspect ratio: |
| 153 |
float vvW = (viewVol.getWidth() == 0.0) ? 1 : viewVol.getWidth(); |
| 154 |
float vvH = (viewVol.getHeight() == 0.0) ? 1 : viewVol.getHeight(); |
| 155 |
SbVec3f vpPt1( nrmScnPt1[0] * vvW, nrmScnPt1[1] * vvH, 0); |
| 156 |
SbVec3f vpPt2( nrmScnPt2[0] * vvW, nrmScnPt2[1] * vvH, 0); |
| 157 |
SbVec3f vpInPoint( point[0] * vvW, point[1] * vvH, 0); |
| 158 |
|
| 159 |
// Create the viewPlaneLine -- our line expressed in viewPlane space: |
| 160 |
SbLine viewPlaneLine( vpPt1, vpPt2 ); |
| 161 |
|
| 162 |
// In viewplane space, find the closest point on our line to the cursor. |
| 163 |
SbVec3f vpClosestPt = viewPlaneLine.getClosestPoint( vpInPoint ); |
| 164 |
vpClosestPt.setValue( vpClosestPt[0], vpClosestPt[1], 0 ); |
| 165 |
|
| 166 |
// If we've got a perspective view, we may need to clamp the point we |
| 167 |
// choose so that it's not too close to the vanishing point. |
| 168 |
// Otherwise we'll just use our vpClosestPt |
| 169 |
|
| 170 |
SbVec3f vpClampedPt = vpClosestPt; |
| 171 |
|
| 172 |
if ( viewVol.getProjectionType() == SbViewVolume::PERSPECTIVE ) { |
| 173 |
|
| 174 |
// Find the vanishing point of our line in viewPlane space: |
| 175 |
// Convert the direction of our line from world space into space |
| 176 |
// after the affine matrix (i.e. just before the projection matrix) |
| 177 |
SbMatrix vvAffine, vvProj; |
| 178 |
viewVol.getMatrices( vvAffine, vvProj ); |
| 179 |
SbVec3f postAffineDir; |
| 180 |
vvAffine.multDirMatrix( wldDir, postAffineDir ); |
| 181 |
|
| 182 |
// If the direction of the line is parallel to the view plane, |
| 183 |
// then the z component of postAffineDir is 0. |
| 184 |
// In this case, we will not need to clamp our point and moreover, |
| 185 |
// if we try we'll wind up dividing by zero pretty soon. |
| 186 |
if ( postAffineDir[2] != 0.0 ) { |
| 187 |
|
| 188 |
// If we send a line out from (0,0,0) into the viewVolume towards |
| 189 |
// postAffineDir, it will vanish at the same point as any other line |
| 190 |
// parallel to this direction. Also, all points along this line |
| 191 |
// will project to the same point on the near (or far) plane. |
| 192 |
// So a line connecting (0,0,0) and the point at postAffineDir will |
| 193 |
// intersect the near plane at the vanishing point. Transforming |
| 194 |
// any point on this line by vvProj will yield the same x,y result |
| 195 |
// and the z component will vary with depth. |
| 196 |
// So multiply the postAffineDir as a vector through the projection |
| 197 |
// matrix and use the x,y for the vanishing point. |
| 198 |
SbVec3f projVanish; |
| 199 |
vvProj.multVecMatrix( postAffineDir, projVanish ); |
| 200 |
|
| 201 |
// Convert from [-1,1] range to [0,1] range for normalized coords. |
| 202 |
SbVec3f nrmScnVanish; |
| 203 |
nrmScnVanish[0] = (1.0 + projVanish[0]) * 0.5; |
| 204 |
nrmScnVanish[1] = (1.0 + projVanish[1]) * 0.5; |
| 205 |
|
| 206 |
// Finally, get the vanishing point in viewPlane coords: |
| 207 |
SbVec3f vpVanish( nrmScnVanish[0] * vvW, nrmScnVanish[1] * vvH, 0 ); |
| 208 |
|
| 209 |
#if 0 |
| 210 |
// Check that the vanishing point is correct: |
| 211 |
// Project nrmScnVanish on the plane to see if it goes along wldDir: |
| 212 |
SbVec2f nrmScnVanish2( nrmScnVanish[0], nrmScnVanish[1] ); |
| 213 |
SbLine vanishWorldLine; |
| 214 |
viewVol.projectPointToLine( nrmScnVanish2, vanishWorldLine ); |
| 215 |
SbVec3f test = vanishWorldLine.getDirection(); |
| 216 |
fprintf(stderr,"wldDir = %f %f %f\n",wldDir[0],wldDir[1],wldDir[2]); |
| 217 |
fprintf(stderr,"checkDir = %f %f %f\n", test[0], test[1],test[2]); |
| 218 |
#endif |
| 219 |
|
| 220 |
// The points vpPt1 and vpPt2 define the line in viewPlane space. |
| 221 |
// We can't go on the other side of the vanishing point from these |
| 222 |
// defining points in screen space or the point will be undefined when |
| 223 |
// we cast it into world space. |
| 224 |
// So clamp our selected point to lie on vpPt1's side of the vanishing |
| 225 |
// point. Since points near the vanishing point will also be incredibly |
| 226 |
// far away, introduce an (arbitrary) metric, VANISH_DELTA. |
| 227 |
// Our selection must be more than VANISH_DELTA times the average of |
| 228 |
// viewVolumeHeight and viewVolumeWidth from the vanishing point. |
| 229 |
#define VANISH_DELTA .01 |
| 230 |
float vanishSafetyDist = VANISH_DELTA * .5 * (vvW + vvH); |
| 231 |
#undef VANISH_DELTA |
| 232 |
// Make pt0, the point from which we measure distances along vpLine. |
| 233 |
// It will be one extra unit away from vpVanish than safetyDist |
| 234 |
SbVec3f pt0 = viewPlaneLine.getPosition(); |
| 235 |
pt0.setValue( pt0[0], pt0[1], 0 ); |
| 236 |
SbVec3f pt0ToVanishDir = vpVanish - pt0; |
| 237 |
pt0ToVanishDir.normalize(); |
| 238 |
float pt0ToVanishDist = vanishSafetyDist + 1.0; |
| 239 |
pt0 = vpVanish - pt0ToVanishDist * pt0ToVanishDir; |
| 240 |
|
| 241 |
// Get vector and dist from pt0 to vpClosestPt |
| 242 |
SbVec3f pt0ToClosest = vpClosestPt - pt0; |
| 243 |
float pt0ToClosestDist = pt0ToClosest.length(); |
| 244 |
|
| 245 |
// If vpClosestPt is too far from pt0, clamp it: |
| 246 |
float clampDist = pt0ToVanishDist - vanishSafetyDist; |
| 247 |
if ( (pt0ToClosestDist > clampDist) |
| 248 |
&& (pt0ToClosest.dot(pt0ToVanishDir) > 0.0) ) { |
| 249 |
vpClampedPt = pt0 + clampDist * pt0ToVanishDir; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// Convert result back into normalized screen space: |
| 255 |
SbVec2f nrmScnClampedPt( vpClampedPt[0] / vvW, vpClampedPt[1] / vvH); |
| 256 |
|
| 257 |
// Create a line in working space by projecting our point into the scene: |
| 258 |
SbVec3f result, whoCares; |
| 259 |
SbLine workingLine = getWorkingLine( nrmScnClampedPt ); |
| 260 |
|
| 261 |
// Find point on the projector line closest to workingLine |
| 262 |
if (! line.getClosestPoints(workingLine, result, whoCares)) { |
| 263 |
#ifdef DEBUG |
| 264 |
SoDebugError::post("SbLineProjector::project", |
| 265 |
"Couldn't get closest point"); |
| 266 |
#endif |
| 267 |
} |
| 268 |
|
| 269 |
return result; |
| 270 |
} |
| 271 |
|
| 272 |
//////////////////////////////////////////////////////////////////////// |
| 273 |
// |
| 274 |
// Description: |
| 275 |
// Get the vector based on one or two mouse positions. |
| 276 |
// |
| 277 |
// Use: public |
| 278 |
// |
| 279 |
//////////////////////////////////////////////////////////////////////// |
| 280 |
|
| 281 |
SbVec3f |
| 282 |
SbLineProjector::getVector(const SbVec2f &m1, const SbVec2f &m2) |
| 283 |
{ |
| 284 |
SbVec3f p1 = project(m1); |
| 285 |
SbVec3f p2 = project(m2); |
| 286 |
|
| 287 |
lastPoint = p2; |
| 288 |
return p2 - p1; |
| 289 |
} |
| 290 |
|
| 291 |
SbVec3f |
| 292 |
SbLineProjector::getVector(const SbVec2f &m) |
| 293 |
{ |
| 294 |
SbVec3f p = project(m); |
| 295 |
SbVec3f result = p - lastPoint; |
| 296 |
lastPoint = p; |
| 297 |
return result; |
| 298 |
} |
| 299 |
|