| 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 |
| SoShuttle |
| 47 |
| |
| 48 |
| Author(s) : Ronen Barzel |
| 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/SoDB.h> |
| 55 |
#include <Inventor/engines/SoElapsedTime.h> |
| 56 |
#include <Inventor/engines/SoCalculator.h> |
| 57 |
#include <Inventor/engines/SoInterpolate.h> |
| 58 |
#include <Inventor/nodes/SoShuttle.h> |
| 59 |
#include <Inventor/sensors/SoFieldSensor.h> |
| 60 |
#include <Inventor/sensors/SoOneShotSensor.h> |
| 61 |
|
| 62 |
SO_NODE_SOURCE(SoShuttle); |
| 63 |
|
| 64 |
//////////////////////////////////////////////////////////////////////// |
| 65 |
// |
| 66 |
// Description: |
| 67 |
// Constructor |
| 68 |
// |
| 69 |
// Use: public |
| 70 |
|
| 71 |
SoShuttle::SoShuttle() |
| 72 |
// |
| 73 |
//////////////////////////////////////////////////////////////////////// |
| 74 |
{ |
| 75 |
SO_NODE_CONSTRUCTOR(SoShuttle); |
| 76 |
SO_NODE_ADD_FIELD(translation0, (SbVec3f(0,0,0))); |
| 77 |
SO_NODE_ADD_FIELD(translation1, (SbVec3f(0,0,0))); |
| 78 |
SO_NODE_ADD_FIELD(speed, (1)); |
| 79 |
SO_NODE_ADD_FIELD(on, (TRUE)); |
| 80 |
|
| 81 |
time = new SoElapsedTime; |
| 82 |
calc = new SoCalculator; |
| 83 |
interp = new SoInterpolateVec3f; |
| 84 |
time->ref(); |
| 85 |
calc->ref(); |
| 86 |
interp->ref(); |
| 87 |
|
| 88 |
// next make the sensors (when wiring the engines |
| 89 |
// together the notify method will try to schedule |
| 90 |
// the sensor, so make sure it exists). |
| 91 |
translationSensor = new SoOneShotSensor(translationSensorCB, this); |
| 92 |
translationSensor->setPriority(0); |
| 93 |
translation0Sensor = new SoFieldSensor(translationSensorCB, this); |
| 94 |
translation0Sensor->setPriority(0); |
| 95 |
translation0Sensor->attach(&translation0); |
| 96 |
translation1Sensor = new SoFieldSensor(translationSensorCB, this); |
| 97 |
translation1Sensor->setPriority(0); |
| 98 |
translation1Sensor->attach(&translation1); |
| 99 |
onSensor = new SoFieldSensor(onSensorCB, this); |
| 100 |
onSensor->setPriority(0); |
| 101 |
onSensor->attach(&on); |
| 102 |
|
| 103 |
time->on.connectFrom(&on); |
| 104 |
calc->a.connectFrom(&speed); |
| 105 |
calc->b.connectFrom(&time->timeOut); |
| 106 |
// input "c" to the calculation is a phase offset that is |
| 107 |
// set by the rotation sensor callback. |
| 108 |
calc->expression.setValue("oa = 0.5 + 0.5 * sin(a*b*2*M_PI + c)"); |
| 109 |
translationSensorCB(this, NULL); // init offset to the default translation |
| 110 |
interp->input0.connectFrom(&translation0); |
| 111 |
interp->input1.connectFrom(&translation1); |
| 112 |
interp->alpha.connectFrom(&calc->oa); |
| 113 |
translation.connectFrom(&interp->output); |
| 114 |
|
| 115 |
// keep the internal connection, so we can tell |
| 116 |
// later if somebody overrode it. |
| 117 |
translation.getConnectedEngine(internalConnection); |
| 118 |
|
| 119 |
isBuiltIn = TRUE; |
| 120 |
} |
| 121 |
|
| 122 |
//////////////////////////////////////////////////////////////////////// |
| 123 |
// |
| 124 |
// Description: |
| 125 |
// Destructor (necessary since inline destructor is too complex) |
| 126 |
// |
| 127 |
// Use: private |
| 128 |
|
| 129 |
SoShuttle::~SoShuttle() |
| 130 |
// |
| 131 |
//////////////////////////////////////////////////////////////////////// |
| 132 |
{ |
| 133 |
time->unref(); |
| 134 |
calc->unref(); |
| 135 |
interp->unref(); |
| 136 |
|
| 137 |
delete translationSensor; |
| 138 |
delete translation0Sensor; |
| 139 |
delete translation1Sensor; |
| 140 |
delete onSensor; |
| 141 |
} |
| 142 |
|
| 143 |
//////////////////////////////////////////////////////////////////////// |
| 144 |
// |
| 145 |
// Description: |
| 146 |
// Write action. Disables connections to internal engines |
| 147 |
// during the write. |
| 148 |
// |
| 149 |
// Use: protected |
| 150 |
|
| 151 |
void |
| 152 |
SoShuttle::write(SoWriteAction *action) |
| 153 |
// |
| 154 |
//////////////////////////////////////////////////////////////////////// |
| 155 |
{ |
| 156 |
SbBool save = translation.isConnectionEnabled(); |
| 157 |
SoEngineOutput *translationSource; |
| 158 |
|
| 159 |
// if the connection is to the internal engine, disable it |
| 160 |
if (translation.isConnected() && |
| 161 |
translation.getConnectedEngine(translationSource) && |
| 162 |
translationSource == internalConnection) { |
| 163 |
|
| 164 |
translation.enableConnection(FALSE); |
| 165 |
} |
| 166 |
|
| 167 |
SoTranslation::write(action); |
| 168 |
|
| 169 |
translation.enableConnection(save); |
| 170 |
} |
| 171 |
|
| 172 |
//////////////////////////////////////////////////////////////////////// |
| 173 |
// |
| 174 |
// Description: |
| 175 |
// Override notify to notice when the translation field is touched. |
| 176 |
// Can't use field sensor because we need to distinguish |
| 177 |
// our own touching of the field from a setValue by the client. |
| 178 |
// |
| 179 |
// Use: protected |
| 180 |
|
| 181 |
void |
| 182 |
SoShuttle::notify(SoNotList *list) |
| 183 |
// |
| 184 |
//////////////////////////////////////////////////////////////////////// |
| 185 |
{ |
| 186 |
SoNotRec *rec = list->getFirstRec(); |
| 187 |
|
| 188 |
// only interested in a field-to-container notification |
| 189 |
if (rec->getType() == SoNotRec::CONTAINER && |
| 190 |
rec->getBase() == this && |
| 191 |
list->getLastField() == &translation) { |
| 192 |
|
| 193 |
translationSensor->schedule(); |
| 194 |
} |
| 195 |
|
| 196 |
SoTranslation::notify(list); |
| 197 |
} |
| 198 |
|
| 199 |
//////////////////////////////////////////////////////////////////////// |
| 200 |
// |
| 201 |
// Description: |
| 202 |
// Callback is called when somebody sets the translation field. |
| 203 |
// |
| 204 |
// Use: protected |
| 205 |
|
| 206 |
void |
| 207 |
SoShuttle::translationSensorCB(void *data, SoSensor *) |
| 208 |
// |
| 209 |
//////////////////////////////////////////////////////////////////////// |
| 210 |
{ |
| 211 |
SoShuttle *shuttle = (SoShuttle *) data; |
| 212 |
SbVec3f v = shuttle->translation.getValue(); |
| 213 |
SbVec3f v0 = shuttle->translation0.getValue(); |
| 214 |
SbVec3f v1 = shuttle->translation1.getValue(); |
| 215 |
|
| 216 |
// find the value of the interpolant that would |
| 217 |
// line up with v. See where v projects onto the line |
| 218 |
// from v0 to v1 |
| 219 |
SbVec3f diff = v1 - v0; |
| 220 |
float len = diff.length(), alpha; |
| 221 |
if (len == 0) |
| 222 |
alpha = 0; |
| 223 |
else { |
| 224 |
alpha = (v - v0).dot(diff)/(len*len); |
| 225 |
if (alpha > 1) |
| 226 |
alpha = 1; |
| 227 |
if (alpha < 0) |
| 228 |
alpha = 0; |
| 229 |
} |
| 230 |
|
| 231 |
float a = shuttle->calc->a[0]; |
| 232 |
float b = shuttle->calc->b[0]; |
| 233 |
// invert the expression: alpha = .5 + .5 sin(a*b*2*PI + c) |
| 234 |
shuttle->calc->c.setValue(asin(2*alpha-1) - a*b*2*M_PI); |
| 235 |
} |
| 236 |
|
| 237 |
//////////////////////////////////////////////////////////////////////// |
| 238 |
// |
| 239 |
// Description: |
| 240 |
// Callback is called when somebody sets the on field |
| 241 |
// |
| 242 |
// Use: protected |
| 243 |
|
| 244 |
void |
| 245 |
SoShuttle::onSensorCB(void *data, SoSensor *) |
| 246 |
// |
| 247 |
//////////////////////////////////////////////////////////////////////// |
| 248 |
{ |
| 249 |
SoShuttle *shuttle = (SoShuttle *) data; |
| 250 |
|
| 251 |
if (shuttle->on.getValue()) { |
| 252 |
// if turned on, force the internal connection to be made and enabled, |
| 253 |
// no matter what sort of mucking has happened to it. |
| 254 |
shuttle->translation.enableConnection(TRUE); |
| 255 |
shuttle->translation.connectFrom(&shuttle->interp->output); |
| 256 |
shuttle->translation.getConnectedEngine(shuttle->internalConnection); |
| 257 |
} |
| 258 |
} |