| 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 |
// Some code for setting up and dealing with the profile window. |
| 39 |
// |
| 40 |
|
| 41 |
#include <cstdio> |
| 42 |
#include <unistd.h> |
| 43 |
#include <cmath> |
| 44 |
#include <cstdlib> |
| 45 |
|
| 46 |
#include <Inventor/Xt/SoXt.h> |
| 47 |
|
| 48 |
#include <Inventor/SoDB.h> |
| 49 |
#include <Inventor/nodes/SoSeparator.h> |
| 50 |
#include <Inventor/nodes/SoLightModel.h> |
| 51 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 52 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 53 |
#include <Inventor/nodes/SoMaterial.h> |
| 54 |
|
| 55 |
#include <Xm/Text.h> |
| 56 |
|
| 57 |
#include "LineManip.h" |
| 58 |
#include "GeneralizedCylinder.h" |
| 59 |
|
| 60 |
// This file defines: |
| 61 |
// |
| 62 |
// char *myGridBuffer |
| 63 |
// char *myProfileGridBuffer |
| 64 |
// char *myGridXYBuffer |
| 65 |
// |
| 66 |
#include "MyGrids.h" |
| 67 |
|
| 68 |
// |
| 69 |
// The line manipulator. |
| 70 |
// |
| 71 |
static LineManip2 *profileLineManip = NULL; |
| 72 |
static LineManip2 *crossSectionLineManip = NULL; |
| 73 |
static LineManip2 *spineLineManip = NULL; |
| 74 |
static LineManip2 *twistLineManip = NULL; |
| 75 |
|
| 76 |
// |
| 77 |
// This is called when the mouse leaves a line manip window. It turns |
| 78 |
// off that line manip's hilights. |
| 79 |
// |
| 80 |
static void |
| 81 |
leaveLineManipWindow(Widget, XtPointer mydata, XEvent *, Boolean *cont) |
| 82 |
{ |
| 83 |
((LineManip2 *)mydata)->removeHilights(); |
| 84 |
|
| 85 |
*cont = TRUE; // Continue dispatching this event |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
SoSeparator * |
| 90 |
createLineGraphCommon( SoPerspectiveCamera *&c, LineManip2 *&theManip, |
| 91 |
Widget theWidget, char *gridFileName, |
| 92 |
char *gridDfltBuf ) |
| 93 |
{ |
| 94 |
SoSeparator *result = new SoSeparator; |
| 95 |
result->ref(); |
| 96 |
|
| 97 |
//!!! |
| 98 |
// UGLY HACK! TEMPORARY BUG FIX! |
| 99 |
// There is some kind of memory stomping bug going on here. |
| 100 |
// If you call this twice, something weird happens with the old version. |
| 101 |
// The old line manip, which should just disappear when the old scene |
| 102 |
// gets unref'ed, apparently retains a reference to the old coordinate |
| 103 |
// node. Anyhow, I'm not sure what's wrong, but referencing the |
| 104 |
// whole scene graph an extra stops the core dump. |
| 105 |
// But it should cause a pretty bad memory leak. |
| 106 |
// It saves a whole scene graph it should get rid of, each time a new |
| 107 |
// generalized cylinder is created or selected. |
| 108 |
//XXX |
| 109 |
//??? |
| 110 |
//!!! |
| 111 |
result->ref(); |
| 112 |
//!!! |
| 113 |
//??? |
| 114 |
//XXX |
| 115 |
|
| 116 |
c = new SoPerspectiveCamera; |
| 117 |
result->addChild(c); |
| 118 |
|
| 119 |
c->nearDistance = 0.5; |
| 120 |
c->farDistance = 10.0; |
| 121 |
c->focalDistance = 5.0; |
| 122 |
c->heightAngle = 1.04; |
| 123 |
|
| 124 |
// Axes and labels (appear underneath the line manipulator) |
| 125 |
SoInput in; |
| 126 |
SoSeparator *g1; |
| 127 |
if (gridFileName != NULL && in.openFile(gridFileName, TRUE) != FALSE) |
| 128 |
g1 = SoDB::readAll(&in); |
| 129 |
else |
| 130 |
g1 = NULL; |
| 131 |
|
| 132 |
if (g1 == NULL && gridDfltBuf != NULL ) { |
| 133 |
// If you couldn't find the file, use the default buffer... |
| 134 |
g1 = new SoSeparator; |
| 135 |
SoInput in; |
| 136 |
in.setBuffer((void *) gridDfltBuf, (size_t)strlen(gridDfltBuf)); |
| 137 |
SoNode *n; |
| 138 |
while ((SoDB::read(&in,n) != FALSE) && (n!=NULL)) |
| 139 |
g1->addChild(n); |
| 140 |
} |
| 141 |
if (g1 != NULL) |
| 142 |
{ |
| 143 |
result->addChild(g1); |
| 144 |
} |
| 145 |
|
| 146 |
SoSeparator *manipGroup = new SoSeparator; |
| 147 |
result->addChild(manipGroup); |
| 148 |
|
| 149 |
SoLightModel *lm = new SoLightModel; |
| 150 |
manipGroup->addChild(lm); |
| 151 |
lm->model = SoLightModel::BASE_COLOR; |
| 152 |
|
| 153 |
SoMaterial *m = new SoMaterial; |
| 154 |
m->diffuseColor.setValue(0.0, 0.8, 0.8); |
| 155 |
manipGroup->addChild(m); |
| 156 |
|
| 157 |
// Remove event handler for old manip, if there was one. |
| 158 |
if ( theManip != NULL ) |
| 159 |
XtRemoveEventHandler(theWidget, LeaveWindowMask, |
| 160 |
FALSE, leaveLineManipWindow, (XtPointer)theManip); |
| 161 |
|
| 162 |
theManip = new LineManip2; |
| 163 |
theManip->setHilightSize(0.1); |
| 164 |
manipGroup->addChild(theManip); |
| 165 |
|
| 166 |
// Add event handler for new manip. |
| 167 |
XtAddEventHandler(theWidget, LeaveWindowMask, |
| 168 |
FALSE, leaveLineManipWindow, (XtPointer)theManip); |
| 169 |
|
| 170 |
result->unrefNoDelete(); |
| 171 |
|
| 172 |
return result; |
| 173 |
} |
| 174 |
|
| 175 |
// |
| 176 |
// This creates the profile's scene graph and sets up the callback so |
| 177 |
// the line manipulator turns off its hilights when the mouse leaves |
| 178 |
// the profile window. |
| 179 |
// |
| 180 |
SoNode * |
| 181 |
createProfileGraph(Widget profileWidget, GeneralizedCylinder *surface) |
| 182 |
{ |
| 183 |
SoPerspectiveCamera *c; |
| 184 |
|
| 185 |
SoSeparator *topNode = createLineGraphCommon( c, profileLineManip, |
| 186 |
profileWidget, "ProfileGrid.iv", myProfileGridBuffer ); |
| 187 |
c->position.setValue(3.0, 0.0, 5.0); |
| 188 |
|
| 189 |
// Get the coordinate node from the surface node... |
| 190 |
SoCoordinate3 *c3 = NULL; |
| 191 |
if (surface) |
| 192 |
c3 = SO_GET_PART( surface, "profileCoords", SoCoordinate3 ); |
| 193 |
profileLineManip->setCoordinate3( c3 ); |
| 194 |
|
| 195 |
return topNode; |
| 196 |
} |
| 197 |
|
| 198 |
SoNode * |
| 199 |
createCrossSectionGraph(Widget crossSectionWidget, GeneralizedCylinder *surface) |
| 200 |
{ |
| 201 |
SoPerspectiveCamera *c; |
| 202 |
|
| 203 |
SoSeparator *topNode = createLineGraphCommon( c, crossSectionLineManip, |
| 204 |
crossSectionWidget, "GridXY.iv", myGridXYBuffer ); |
| 205 |
c->position.setValue(0.0, 5.0, 0.0); |
| 206 |
c->orientation.setValue(SbVec3f(1,0,0), -1.57079 ); |
| 207 |
|
| 208 |
crossSectionLineManip->setPlaneNormal(SbVec3f( 0, 1, 0 ) ); |
| 209 |
|
| 210 |
// Get the coordinate node from the surface node... |
| 211 |
SoCoordinate3 *c3 = NULL; |
| 212 |
if (surface) |
| 213 |
c3 = SO_GET_PART( surface, "crossSectionCoords", SoCoordinate3 ); |
| 214 |
crossSectionLineManip->setCoordinate3( c3 ); |
| 215 |
|
| 216 |
return topNode; |
| 217 |
} |
| 218 |
SoNode * |
| 219 |
createSpineGraph(Widget spineWidget, GeneralizedCylinder *surface) |
| 220 |
{ |
| 221 |
SoPerspectiveCamera *c; |
| 222 |
|
| 223 |
SoSeparator *topNode = createLineGraphCommon( c, spineLineManip, |
| 224 |
spineWidget, "Grid.iv", myGridBuffer ); |
| 225 |
c->position.setValue(0.0, 0.0, 5.0); |
| 226 |
|
| 227 |
// Get the coordinate node from the surface node... |
| 228 |
SoCoordinate3 *c3 = NULL; |
| 229 |
if (surface) |
| 230 |
c3 = SO_GET_PART( surface, "spineCoords", SoCoordinate3 ); |
| 231 |
spineLineManip->setCoordinate3( c3 ); |
| 232 |
|
| 233 |
return topNode; |
| 234 |
} |
| 235 |
SoNode * |
| 236 |
createTwistGraph(Widget twistWidget, GeneralizedCylinder *surface) |
| 237 |
{ |
| 238 |
SoPerspectiveCamera *c; |
| 239 |
|
| 240 |
SoSeparator *topNode = createLineGraphCommon( c, twistLineManip, |
| 241 |
twistWidget, "Grid.iv", myGridBuffer ); |
| 242 |
c->position.setValue(0.0, 0.0, 5.0); |
| 243 |
|
| 244 |
// Get the coordinate node from the surface node... |
| 245 |
SoCoordinate3 *c3 = NULL; |
| 246 |
if (surface) |
| 247 |
c3 = SO_GET_PART( surface, "twistCoords", SoCoordinate3 ); |
| 248 |
twistLineManip->setCoordinate3( c3 ); |
| 249 |
|
| 250 |
return topNode; |
| 251 |
} |
| 252 |
|
| 253 |
// |
| 254 |
// Called to get rid of all the points added so far. |
| 255 |
// |
| 256 |
void |
| 257 |
clearProfilePoints() |
| 258 |
{ |
| 259 |
SoCoordinate3 *coord = profileLineManip->getCoordinate3(); |
| 260 |
coord->point.deleteValues(0); |
| 261 |
} |
| 262 |
|
| 263 |
void |
| 264 |
clearCrossSectionPoints() |
| 265 |
{ |
| 266 |
SoCoordinate3 *coord = crossSectionLineManip->getCoordinate3(); |
| 267 |
coord->point.deleteValues(0); |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
clearSpinePoints() |
| 272 |
{ |
| 273 |
SoCoordinate3 *coord = spineLineManip->getCoordinate3(); |
| 274 |
coord->point.deleteValues(0); |
| 275 |
} |
| 276 |
void |
| 277 |
clearTwistPoints() |
| 278 |
{ |
| 279 |
SoCoordinate3 *coord = twistLineManip->getCoordinate3(); |
| 280 |
coord->point.deleteValues(0); |
| 281 |
} |
| 282 |
void |
| 283 |
makeReverseSection() |
| 284 |
{ |
| 285 |
SoCoordinate3 *coord = crossSectionLineManip->getCoordinate3(); |
| 286 |
|
| 287 |
int numCoords = coord->point.getNum(); |
| 288 |
|
| 289 |
SbVec3f temp; |
| 290 |
|
| 291 |
for (int first = 0, last = numCoords-1; |
| 292 |
first < last; |
| 293 |
first++, last-- ){ |
| 294 |
|
| 295 |
temp = coord->point[first]; |
| 296 |
coord->point.set1Value(first, coord->point[last]); |
| 297 |
coord->point.set1Value(last, temp); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
void |
| 302 |
makeCircularSection() |
| 303 |
{ |
| 304 |
SoCoordinate3 *coord = crossSectionLineManip->getCoordinate3(); |
| 305 |
coord->point.enableNotify(FALSE); |
| 306 |
coord->point.deleteValues(0); |
| 307 |
|
| 308 |
int numSegments = 35; |
| 309 |
float maxAngle = 2 * 3.14159; |
| 310 |
|
| 311 |
coord->point.insertSpace(0,numSegments); |
| 312 |
|
| 313 |
SbVec3f newPoint(0,0,0); |
| 314 |
float theta; |
| 315 |
for (int i = 0; i < numSegments; i++ ) { |
| 316 |
theta = ((float)i/(float)(numSegments-1)) * maxAngle; |
| 317 |
newPoint[0] = cos( theta ); |
| 318 |
newPoint[2] = sin( theta ); |
| 319 |
coord->point.set1Value(i,newPoint); |
| 320 |
} |
| 321 |
coord->point.enableNotify(TRUE); |
| 322 |
coord->point.touch(); |
| 323 |
} |
| 324 |
|
| 325 |
void |
| 326 |
makeSemiCircularSpine() |
| 327 |
{ |
| 328 |
SoCoordinate3 *coord = spineLineManip->getCoordinate3(); |
| 329 |
coord->point.enableNotify(FALSE); |
| 330 |
coord->point.deleteValues(0); |
| 331 |
|
| 332 |
int numSegments = 50; |
| 333 |
float maxAngle = 3.14159; |
| 334 |
|
| 335 |
coord->point.insertSpace(0,numSegments); |
| 336 |
|
| 337 |
SbVec3f newPoint(0,0,0); |
| 338 |
float theta; |
| 339 |
for (int i = 0; i < numSegments; i++ ) { |
| 340 |
theta = ((float)i / (float)(numSegments-1)) * maxAngle; |
| 341 |
newPoint[0] = cos( theta ); |
| 342 |
newPoint[1] = sin( theta ); |
| 343 |
coord->point.set1Value(i,newPoint); |
| 344 |
} |
| 345 |
coord->point.enableNotify(TRUE); |
| 346 |
coord->point.touch(); |
| 347 |
} |
| 348 |
void |
| 349 |
makeCircularSpine() |
| 350 |
{ |
| 351 |
SoCoordinate3 *coord = spineLineManip->getCoordinate3(); |
| 352 |
coord->point.enableNotify(FALSE); |
| 353 |
coord->point.deleteValues(0); |
| 354 |
|
| 355 |
int numSegments = 50; |
| 356 |
float maxAngle = 2 * 3.14159; |
| 357 |
|
| 358 |
coord->point.insertSpace(0,numSegments); |
| 359 |
|
| 360 |
SbVec3f newPoint(0,0,0); |
| 361 |
float theta; |
| 362 |
for (int i = 0; i < numSegments; i++ ) { |
| 363 |
theta = ((float)i/(float)(numSegments-1)) * maxAngle; |
| 364 |
newPoint[0] = cos( theta ); |
| 365 |
newPoint[1] = sin( theta ); |
| 366 |
coord->point.set1Value(i,newPoint); |
| 367 |
} |
| 368 |
coord->point.enableNotify(TRUE); |
| 369 |
coord->point.touch(); |
| 370 |
} |