| 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 |
| SoTimeCounter |
| 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/SoTimeCounter.h> |
| 56 |
|
| 57 |
SO_ENGINE_SOURCE(SoTimeCounter); |
| 58 |
|
| 59 |
//////////////////////////////////////////////////////////////////////// |
| 60 |
// |
| 61 |
// Description: |
| 62 |
// Constructor |
| 63 |
// |
| 64 |
// Use: public |
| 65 |
|
| 66 |
SoTimeCounter::SoTimeCounter() |
| 67 |
// |
| 68 |
//////////////////////////////////////////////////////////////////////// |
| 69 |
{ |
| 70 |
SO_ENGINE_CONSTRUCTOR(SoTimeCounter); |
| 71 |
SO_ENGINE_ADD_INPUT(min, (0)); |
| 72 |
SO_ENGINE_ADD_INPUT(max, (1)); |
| 73 |
SO_ENGINE_ADD_INPUT(step, (1)); |
| 74 |
SO_ENGINE_ADD_INPUT(on, (TRUE)); |
| 75 |
SO_ENGINE_ADD_INPUT(frequency, (1)); |
| 76 |
SO_ENGINE_ADD_INPUT(duty, (1.0)); |
| 77 |
SO_ENGINE_ADD_INPUT(timeIn, (SbTime::zero())); |
| 78 |
SO_ENGINE_ADD_INPUT(syncIn, ()); |
| 79 |
SO_ENGINE_ADD_INPUT(reset, (0)); |
| 80 |
SO_ENGINE_ADD_OUTPUT(output, SoSFShort); |
| 81 |
SO_ENGINE_ADD_OUTPUT(syncOut, SoSFTrigger); |
| 82 |
|
| 83 |
state = ON; |
| 84 |
todo = RECALC|SYNC; |
| 85 |
stages = NULL; |
| 86 |
curStage = -1; |
| 87 |
prevStage = -1; |
| 88 |
isBuiltIn = TRUE; |
| 89 |
|
| 90 |
// default connection for time source |
| 91 |
timeIn.connectFrom(SoDB::getGlobalField("realTime")); |
| 92 |
} |
| 93 |
|
| 94 |
//////////////////////////////////////////////////////////////////////// |
| 95 |
// |
| 96 |
// Description: |
| 97 |
// Destructor |
| 98 |
// |
| 99 |
// Use: private |
| 100 |
|
| 101 |
SoTimeCounter::~SoTimeCounter() |
| 102 |
// |
| 103 |
//////////////////////////////////////////////////////////////////////// |
| 104 |
{ |
| 105 |
delete [] stages; |
| 106 |
} |
| 107 |
|
| 108 |
//////////////////////////////////////////////////////////////////////// |
| 109 |
// |
| 110 |
// Description: |
| 111 |
// Writes instance to SoOutput. Takes care of not writing out |
| 112 |
// connection to realTime that is created in constructor. |
| 113 |
// |
| 114 |
// Use: private |
| 115 |
|
| 116 |
void |
| 117 |
SoTimeCounter::writeInstance(SoOutput *out) |
| 118 |
// |
| 119 |
//////////////////////////////////////////////////////////////////////// |
| 120 |
{ |
| 121 |
// If connected to realTime, disable connection to prevent |
| 122 |
// connection from being written and mark timeIn as default to |
| 123 |
// prevent value from being written |
| 124 |
SbBool timeInWasDefault = timeIn.isDefault(); |
| 125 |
SbBool timeInConnectionEnabled = timeIn.isConnectionEnabled(); |
| 126 |
SoField *timeInConnection = NULL; |
| 127 |
timeIn.getConnectedField(timeInConnection); |
| 128 |
if (timeInConnection == SoDB::getGlobalField("realTime")) { |
| 129 |
timeIn.enableConnection(FALSE); |
| 130 |
timeIn.setDefault(TRUE); |
| 131 |
} |
| 132 |
SoEngine::writeInstance(out); |
| 133 |
|
| 134 |
timeIn.enableConnection(timeInConnectionEnabled); |
| 135 |
timeIn.setDefault(timeInWasDefault); |
| 136 |
} |
| 137 |
|
| 138 |
//////////////////////////////////////////////////////////////////////// |
| 139 |
// |
| 140 |
// Description: |
| 141 |
// Reads stuff into instance. Works around a problem with old |
| 142 |
// files that contain explicit references to the default |
| 143 |
// connection to realTime. |
| 144 |
// |
| 145 |
// Use: private |
| 146 |
|
| 147 |
SbBool |
| 148 |
SoTimeCounter::readInstance(SoInput *in, unsigned short flags) |
| 149 |
// |
| 150 |
//////////////////////////////////////////////////////////////////////// |
| 151 |
{ |
| 152 |
SbBool result = SoEngine::readInstance(in, flags); |
| 153 |
todo |= RESET; |
| 154 |
SoField *timeInConnection = NULL; |
| 155 |
timeIn.getConnectedField(timeInConnection); |
| 156 |
if (timeInConnection == SoDB::getGlobalField("realTime")) { |
| 157 |
timeIn.disconnect(); |
| 158 |
timeIn.connectFrom(timeInConnection); |
| 159 |
} |
| 160 |
|
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
//////////////////////////////////////////////////////////////////////// |
| 165 |
// |
| 166 |
// Description: |
| 167 |
// Detects when the inputs changed. Notifications are only propogated |
| 168 |
// when the output is going to change; periodic clock-ticks are suppressed |
| 169 |
// while waiting for the next stage |
| 170 |
// |
| 171 |
// Use: private |
| 172 |
|
| 173 |
void |
| 174 |
SoTimeCounter::inputChanged(SoField *whichInput) |
| 175 |
// |
| 176 |
//////////////////////////////////////////////////////////////////////// |
| 177 |
{ |
| 178 |
// get current time value |
| 179 |
SbTime now = timeIn.getValue(); |
| 180 |
|
| 181 |
SbBool enable = FALSE; |
| 182 |
SbBool syncEnable = !!(todo&SYNC); |
| 183 |
if (whichInput == &min || |
| 184 |
whichInput == &max || |
| 185 |
whichInput == &step || |
| 186 |
whichInput == &frequency || |
| 187 |
whichInput == &duty) { |
| 188 |
todo |= RECALC; |
| 189 |
} |
| 190 |
else if (whichInput == &reset) { |
| 191 |
todo |= RESET; |
| 192 |
} |
| 193 |
else if (whichInput == &syncIn) { |
| 194 |
todo |= SYNC; |
| 195 |
syncEnable = TRUE; |
| 196 |
} |
| 197 |
else if (whichInput == &on) { |
| 198 |
if (!on.getValue() && state != PAUSED) { |
| 199 |
todo |= PAUSE; |
| 200 |
todo &= ~UNPAUSE; |
| 201 |
} |
| 202 |
else if (on.getValue() && state == PAUSED) { |
| 203 |
todo |= UNPAUSE; |
| 204 |
todo &= ~PAUSE; |
| 205 |
} |
| 206 |
} |
| 207 |
else if (whichInput == &timeIn && on.getValue() && stages != NULL) { |
| 208 |
SbTime off = now-baseTime; |
| 209 |
if (off >= stages[curStage+1].offset || off < stages[curStage].offset) { |
| 210 |
enable = TRUE; |
| 211 |
if (off > stages[nStages].offset) |
| 212 |
syncEnable = TRUE; |
| 213 |
} |
| 214 |
} |
| 215 |
if (todo) enable = TRUE; |
| 216 |
|
| 217 |
output.enable(enable); |
| 218 |
syncOut.enable(syncEnable); |
| 219 |
|
| 220 |
#ifdef DEVELOP |
| 221 |
printf("TimeCounter::inputChanged %s state=%s on=%d reset=%d enable=%d syncEnable=%d todo=", |
| 222 |
whichInput==&min ? "min " : |
| 223 |
whichInput==&max ? "max " : |
| 224 |
whichInput==&step ? "step " : |
| 225 |
whichInput==&reset ? "reset " : |
| 226 |
whichInput==&syncIn ? "syncIn" : |
| 227 |
whichInput==&on ? "on " : |
| 228 |
whichInput==&timeIn ? "------" : |
| 229 |
" ... ", |
| 230 |
state==ON ? "ON " : |
| 231 |
state==PAUSED ? "PAUSED" : |
| 232 |
"??????", |
| 233 |
on.getValue(), |
| 234 |
reset.getValue(), |
| 235 |
enable, |
| 236 |
syncEnable); |
| 237 |
if (todo) { |
| 238 |
int t=todo; |
| 239 |
if (todo&RECALC) printf("RECALC,"), t &= ~RECALC; |
| 240 |
if (todo&RESET) printf("RESET,"), t &= ~RESET; |
| 241 |
if (todo&SYNC) printf("SYNC,"), t &= ~SYNC; |
| 242 |
if (todo&PAUSE) printf("PAUSE,"), t &= ~PAUSE; |
| 243 |
if (todo&UNPAUSE) printf("UNPAUSE,"), t &= ~UNPAUSE; |
| 244 |
if (t) printf("????"); |
| 245 |
} |
| 246 |
else printf("0"); |
| 247 |
printf("\n"); |
| 248 |
#endif |
| 249 |
|
| 250 |
/*********** |
| 251 |
** |
| 252 |
** first do the things queued up in the "todo" bitmask |
| 253 |
*/ |
| 254 |
|
| 255 |
// recalculate timing/counting info |
| 256 |
if (todo&RECALC) { |
| 257 |
|
| 258 |
// get values |
| 259 |
int mn = min.getValue(); |
| 260 |
int mx = max.getValue(); |
| 261 |
int stp = step.getValue(); |
| 262 |
if (stp == 0) stp = 1; |
| 263 |
if (mn > mx) { int t = mn; mn=mx; mx=t; } |
| 264 |
|
| 265 |
int nDuty = duty.getNum(); |
| 266 |
period = 1/frequency.getValue(); |
| 267 |
|
| 268 |
// reallocate stage array |
| 269 |
delete [] stages; |
| 270 |
nStages = 1+abs((mx-mn)/stp); |
| 271 |
stages = new struct Stage[nStages + 1]; // sentinnel on end |
| 272 |
|
| 273 |
// make sure curStage isn't out of range |
| 274 |
while (curStage >= nStages) curStage -= nStages; |
| 275 |
|
| 276 |
// fill stage values and duties (non normalized) |
| 277 |
int i, val; |
| 278 |
for (i=0, val=stp>0?mn:mx; i<nStages; i++, val+=stp) { |
| 279 |
stages[i].val = val; |
| 280 |
stages[i].duty = (i < nDuty) ? duty[i] : 1.0; |
| 281 |
} |
| 282 |
|
| 283 |
// normalize duties |
| 284 |
double total; |
| 285 |
for (total=0, i=0; i<nStages; i++) total += stages[i].duty; |
| 286 |
for (i=0; i<nStages; i++) stages[i].duty /= total; |
| 287 |
|
| 288 |
// set offsets |
| 289 |
stages[0].offset = 0.0; |
| 290 |
for (i=1; i<nStages; i++) { |
| 291 |
stages[i].offset = stages[i-1].offset + (stages[i-1].duty * period); |
| 292 |
} |
| 293 |
stages[nStages].offset = period; // sentinnel value |
| 294 |
|
| 295 |
#ifdef DEVELOP |
| 296 |
// debugging output |
| 297 |
printf("TimeCounter::evaluate RECALC min=%d max=%d step=%d " |
| 298 |
"nStages=%d freq=%g period=%g\n", |
| 299 |
mn, mx, stp, nStages, frequency.getValue(), period); |
| 300 |
for (i=0; i<nStages; i++) { |
| 301 |
printf("\tstage %d:\tval=%d\tduty=%g\toffset=%g\n", |
| 302 |
i, stages[i].val, stages[i].duty, stages[i].offset.getValue()); |
| 303 |
} |
| 304 |
#endif |
| 305 |
} |
| 306 |
|
| 307 |
// adjust the base time to reset the cycle |
| 308 |
if (todo&SYNC) { |
| 309 |
|
| 310 |
curStage = 0; |
| 311 |
baseTime = now; |
| 312 |
pauseOffset = 0.0; // in case we're currently paused |
| 313 |
|
| 314 |
#ifdef DEVELOP |
| 315 |
printf("TimeCounter::evaluate SYNC now=%s baseTime=%s\n", |
| 316 |
now.format("%s.%i").getString(), |
| 317 |
baseTime.format("%s.%i").getString()); |
| 318 |
#endif |
| 319 |
} |
| 320 |
|
| 321 |
// adjust the base time to reset the cycle |
| 322 |
if (todo&RESET) { |
| 323 |
// get inputs |
| 324 |
const int r = reset.getValue(); |
| 325 |
int stp = step.getValue(); |
| 326 |
if (stp==0) stp = 1; |
| 327 |
|
| 328 |
// convert input value to stage index |
| 329 |
curStage = (r-stages[0].val)/stp; |
| 330 |
if (curStage < 0) |
| 331 |
curStage = 0; |
| 332 |
else if (curStage >= nStages) |
| 333 |
curStage = nStages-1; |
| 334 |
|
| 335 |
// adjust base time so that requested stage is starting now. |
| 336 |
baseTime = now - stages[curStage].offset; |
| 337 |
pauseOffset = stages[curStage].offset; // in case we're currently paused |
| 338 |
|
| 339 |
#ifdef DEVELOP |
| 340 |
printf("TimeCounter::evaluate RESET reset=%d curStage=%d " |
| 341 |
"val=%d offset=%s now=%s baseTime=%s\n", |
| 342 |
r, curStage, stages[curStage].val, |
| 343 |
stages[curStage].offset.format("%s.%i").getString(), |
| 344 |
now.format("%s.%i").getString(), |
| 345 |
baseTime.format("%s.%i").getString()); |
| 346 |
#endif |
| 347 |
} |
| 348 |
|
| 349 |
// when initiating a pause, remember the offset into the cycle |
| 350 |
if (todo&PAUSE) { |
| 351 |
pauseOffset = now - baseTime; |
| 352 |
state = PAUSED; |
| 353 |
#ifdef DEVELOP |
| 354 |
printf("TimeCounter::evaluate PAUSE now=%s baseTime=%s pauseOffset=%s\n", |
| 355 |
now.format("%s.%i").getString(), |
| 356 |
baseTime.format("%s.%i").getString(), |
| 357 |
pauseOffset.format("%s.%i").getString()); |
| 358 |
#endif |
| 359 |
} |
| 360 |
|
| 361 |
// to stop a pause, adjust the base time so that the saved offset |
| 362 |
// is maintained |
| 363 |
if (todo&UNPAUSE) { |
| 364 |
baseTime = now - pauseOffset; |
| 365 |
state = ON; |
| 366 |
#ifdef DEVELOP |
| 367 |
printf("TimeCounter::evaluate UNPAUSE now=%s pauseOffset=%s " |
| 368 |
"baseTime=%s\n", |
| 369 |
now.format("%s.%i").getString(), |
| 370 |
pauseOffset.format("%s.%i").getString(), |
| 371 |
baseTime.format("%s.%i").getString()); |
| 372 |
#endif |
| 373 |
} |
| 374 |
|
| 375 |
todo = 0; |
| 376 |
/* |
| 377 |
** done with the things queued up in the "todo" bitmask |
| 378 |
** |
| 379 |
************/ |
| 380 |
} |
| 381 |
|
| 382 |
//////////////////////////////////////////////////////////////////////// |
| 383 |
// |
| 384 |
// Description: |
| 385 |
// Evaluation routine |
| 386 |
// |
| 387 |
// Use: private |
| 388 |
|
| 389 |
void |
| 390 |
SoTimeCounter::evaluate() |
| 391 |
// |
| 392 |
//////////////////////////////////////////////////////////////////////// |
| 393 |
{ |
| 394 |
int i; |
| 395 |
// get current time value |
| 396 |
SbTime now = timeIn.getValue(); |
| 397 |
|
| 398 |
// find out the current segment |
| 399 |
if (state==ON) { |
| 400 |
|
| 401 |
// move baseTime to the start of the current cycle |
| 402 |
while (now - baseTime >= period) baseTime += period; |
| 403 |
while (baseTime - now > period) baseTime -= period; |
| 404 |
|
| 405 |
// search for the offset |
| 406 |
SbTime offset = now - baseTime; |
| 407 |
for (i=nStages-1; i; i--) |
| 408 |
if (offset >= stages[i].offset) |
| 409 |
break; |
| 410 |
|
| 411 |
curStage = i; |
| 412 |
} |
| 413 |
|
| 414 |
#ifdef DEVELOP |
| 415 |
printf("TimeCounter::evaluate state=%s now=%s baseTime=%s offset=%s " |
| 416 |
"curStage=%d output=%d\n", |
| 417 |
state==ON ? "ON " : |
| 418 |
state==PAUSED ? "PAUSED" : "??????", |
| 419 |
now.format("%s.%i").getString(), |
| 420 |
baseTime.format("%s.%i").getString(), |
| 421 |
(now-baseTime).format("%s.%i").getString(), |
| 422 |
curStage, |
| 423 |
stages[curStage].val); |
| 424 |
#endif |
| 425 |
SO_ENGINE_OUTPUT(output, SoSFShort, setValue(stages[curStage].val)); |
| 426 |
prevStage = curStage; |
| 427 |
} |