| 1 |
#include "gplaut04.h" |
| 2 |
#include "axis.h" |
| 3 |
|
| 4 |
#define FONT_SIZE 12 |
| 5 |
|
| 6 |
////////////////////////////////////////////////////////////// |
| 7 |
// |
| 8 |
Axis::Axis() |
| 9 |
// |
| 10 |
////////////////////////////////////////////////////////////// |
| 11 |
{ |
| 12 |
maxValue = 1; |
| 13 |
minValue = -1; |
| 14 |
numOfTickers = 10; |
| 15 |
color.setValue(1,0,0); |
| 16 |
|
| 17 |
name = "Y"; |
| 18 |
|
| 19 |
double delta = (maxValue-minValue)/numOfTickers; |
| 20 |
for(int i = 0; i <= numOfTickers; ++i) |
| 21 |
{ |
| 22 |
tickers[i] = minValue + i * delta; |
| 23 |
} |
| 24 |
adjust = 0; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
////////////////////////////////////////////////////////////// |
| 29 |
// |
| 30 |
Axis::~Axis() |
| 31 |
// |
| 32 |
////////////////////////////////////////////////////////////// |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
////////////////////////////////////////////////////////////// |
| 38 |
// |
| 39 |
Axis::Axis(bool s3D, int ty, double mxValue, double miValue, |
| 40 |
int numTickers, const char* aName, int wh, SbColor clr) |
| 41 |
// |
| 42 |
////////////////////////////////////////////////////////////// |
| 43 |
{ |
| 44 |
show3D = s3D; |
| 45 |
set(ty, mxValue, miValue, numTickers, aName, wh, clr); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
////////////////////////////////////////////////////////////// |
| 50 |
// |
| 51 |
Axis::Axis(int ty, double mxValue, double miValue, int numTickers, |
| 52 |
const char* aName, int wh, SbColor clr) |
| 53 |
// |
| 54 |
////////////////////////////////////////////////////////////// |
| 55 |
{ |
| 56 |
show3D = false; |
| 57 |
set(ty, mxValue, miValue, numTickers, aName, wh, clr); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
////////////////////////////////////////////////////////////// |
| 62 |
// |
| 63 |
void |
| 64 |
Axis::set(int ty, double mxValue, double mnValue, int numTickers, |
| 65 |
const char* aName, int wh, SbColor clr) |
| 66 |
// |
| 67 |
////////////////////////////////////////////////////////////// |
| 68 |
{ |
| 69 |
type = ty; |
| 70 |
maxValue = mxValue; |
| 71 |
minValue = mnValue; |
| 72 |
numOfTickers = (numTickers<11) ? numTickers : 10; |
| 73 |
which = wh; |
| 74 |
color = clr; |
| 75 |
name = aName; |
| 76 |
|
| 77 |
double delta = (maxValue-minValue)/numOfTickers; |
| 78 |
for(int i = 0; i <= numOfTickers; ++i) |
| 79 |
{ |
| 80 |
tickers[i] = minValue + i * delta; |
| 81 |
} |
| 82 |
adjust = 0; |
| 83 |
if(type == COORD_AT_ORIGIN && (maxValue < 0 || minValue > 0)) |
| 84 |
{ |
| 85 |
if(minValue > 0) |
| 86 |
adjust = minValue; |
| 87 |
else if(maxValue < 0) |
| 88 |
adjust = maxValue; |
| 89 |
adjust *= 1.1 / (maxValue - minValue); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
////////////////////////////////////////////////////////////// |
| 95 |
// |
| 96 |
SoSeparator * |
| 97 |
Axis::createAxis() |
| 98 |
// |
| 99 |
////////////////////////////////////////////////////////////// |
| 100 |
{ |
| 101 |
SoSeparator * axis = new SoSeparator; |
| 102 |
axis->ref(); |
| 103 |
|
| 104 |
SoMaterial *mtl = new SoMaterial; |
| 105 |
mtl->diffuseColor.setValue(color); |
| 106 |
axis->addChild(mtl); |
| 107 |
|
| 108 |
SoCylinder *cyl = new SoCylinder; |
| 109 |
cyl->radius = 2.0/500.0; |
| 110 |
cyl->height = 2.2; |
| 111 |
if(type == COORD_AT_ORIGIN && (maxValue < 0 || minValue > 0)) |
| 112 |
{ |
| 113 |
SoTransform *xform = new SoTransform; |
| 114 |
float d = 0; |
| 115 |
cyl->height = 2 * (1.1 + adjust); |
| 116 |
if(minValue > 0) |
| 117 |
d = -adjust; |
| 118 |
else if(maxValue < 0) |
| 119 |
d = adjust; |
| 120 |
if (which == -1) //y |
| 121 |
xform->translation.setValue(0, -d, 0); |
| 122 |
else // x or z |
| 123 |
xform->translation.setValue(0, d, 0); |
| 124 |
axis->addChild(xform); |
| 125 |
} |
| 126 |
axis->addChild(cyl); |
| 127 |
|
| 128 |
float position; |
| 129 |
if(type == COORDORIGIN || type == COORD_AT_ORIGIN) |
| 130 |
{ |
| 131 |
axis->addChild(drawArrow()); |
| 132 |
position = 1.2 + adjust; |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
#ifdef TD_FONT |
| 137 |
position = 0; |
| 138 |
#else |
| 139 |
position = 1.1; |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
axis->addChild(drawAxisName(position)); |
| 144 |
|
| 145 |
// draw tickers |
| 146 |
if(numOfTickers>0) |
| 147 |
for(int i = 0; i <= numOfTickers; ++i) |
| 148 |
axis->addChild(drawTicker(-1.0+i*2.0/numOfTickers, 0.04)); |
| 149 |
|
| 150 |
// draw scales |
| 151 |
if(numOfTickers>0) |
| 152 |
for(int i = 0; i <= numOfTickers; ++i) |
| 153 |
axis->addChild(drawScale(-1.0+i*2.0/numOfTickers, tickers[i])); |
| 154 |
|
| 155 |
axis->unrefNoDelete(); |
| 156 |
return axis; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
////////////////////////////////////////////////////////////// |
| 162 |
// |
| 163 |
SoSeparator * |
| 164 |
Axis::drawAxisName(float pos) |
| 165 |
// |
| 166 |
////////////////////////////////////////////////////////////// |
| 167 |
{ |
| 168 |
SoSeparator *nameSep = new SoSeparator; |
| 169 |
|
| 170 |
SoFont *axFont = new SoFont; |
| 171 |
axFont->name.setValue("Helvetica"); |
| 172 |
axFont->size.setValue(FONT_SIZE); |
| 173 |
|
| 174 |
#ifdef TD_FONT |
| 175 |
SoText3 *axName = new SoText3; |
| 176 |
#else |
| 177 |
SoText2 *axName = new SoText2; |
| 178 |
#endif |
| 179 |
axName->string.setValue(name); |
| 180 |
|
| 181 |
SoTransform *tkXform = new SoTransform; |
| 182 |
if(type == LEFTBACK || type == LEFTAHEAD) |
| 183 |
{ |
| 184 |
if(which == -1) |
| 185 |
{ |
| 186 |
tkXform->translation.setValue(-0.4, pos, 0.00); |
| 187 |
tkXform->rotation.setValue(SbVec3f(0.0,0.0,1.0), M_PI_2); |
| 188 |
nameSep->addChild(tkXform); |
| 189 |
|
| 190 |
SoTransform *tkXform2 = new SoTransform; |
| 191 |
tkXform2->rotation.setValue(SbVec3f(1.0,0.0,0.0), M_PI); |
| 192 |
nameSep->addChild(tkXform2); |
| 193 |
} |
| 194 |
else if( which == 1) |
| 195 |
{ |
| 196 |
tkXform->translation.setValue(0.2, pos, 0.00); |
| 197 |
tkXform->rotation.setValue(SbVec3f(0.0,0.0,1.0),M_PI_2); |
| 198 |
nameSep->addChild(tkXform); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
tkXform->translation.setValue(-0.4, pos, 0.00); |
| 203 |
tkXform->rotation.setValue(SbVec3f(0.0,0.0,1.0),M_PI_2); |
| 204 |
nameSep->addChild(tkXform); |
| 205 |
} |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
if(which == -1) |
| 210 |
{ |
| 211 |
SoTransform *tkXform2 = new SoTransform; |
| 212 |
tkXform2->rotation.setValue(SbVec3f(0.0,0.0,1.0), M_PI); |
| 213 |
|
| 214 |
tkXform->translation.setValue(-0.2, -pos, 0.00); |
| 215 |
tkXform->rotation.setValue(SbVec3f(0.0,1.0,0.0), M_PI); |
| 216 |
nameSep->addChild(tkXform); |
| 217 |
|
| 218 |
nameSep->addChild(tkXform2); |
| 219 |
} |
| 220 |
else if( which == 1) |
| 221 |
{ |
| 222 |
tkXform->translation.setValue(0.0, pos, 0.00); |
| 223 |
tkXform->rotation.setValue(SbVec3f(0.0,0.0,1.0),M_PI_2); |
| 224 |
nameSep->addChild(tkXform); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
tkXform->translation.setValue(0.0, pos, 0.0); |
| 229 |
nameSep->addChild(tkXform); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
nameSep->addChild(axFont); |
| 234 |
nameSep->addChild(axName); |
| 235 |
|
| 236 |
return nameSep; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
////////////////////////////////////////////////////////////// |
| 241 |
// |
| 242 |
SoSeparator * |
| 243 |
Axis::drawTicker(float pos, float height) |
| 244 |
// |
| 245 |
////////////////////////////////////////////////////////////// |
| 246 |
{ |
| 247 |
SoSeparator *tkSep = new SoSeparator; |
| 248 |
SoCylinder *ticker = new SoCylinder; |
| 249 |
ticker->radius = height/10.0; |
| 250 |
ticker->height = height; |
| 251 |
|
| 252 |
SoTransform *tkXform = new SoTransform; |
| 253 |
|
| 254 |
if(which == -1) |
| 255 |
{ |
| 256 |
pos -= adjust; |
| 257 |
tkXform->translation.setValue(0.02, pos, 0.00); |
| 258 |
tkXform->rotation.setValue(SbVec3f(0.0,0.0,1.0),M_PI_2); |
| 259 |
} |
| 260 |
else if(which == 1) |
| 261 |
{ |
| 262 |
pos += adjust; |
| 263 |
if(type == LEFTBACK) |
| 264 |
tkXform->translation.setValue(0.00, pos, 0.02); |
| 265 |
else |
| 266 |
tkXform->translation.setValue(0.00, pos, -0.02); |
| 267 |
tkXform->rotation.setValue(SbVec3f(1.0,0.0,0.0),M_PI_2); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
pos += adjust; |
| 272 |
tkXform->translation.setValue(0.00, pos, -0.02); |
| 273 |
tkXform->rotation.setValue(SbVec3f(1.0,0.0,0.0),M_PI_2); |
| 274 |
} |
| 275 |
|
| 276 |
tkSep->addChild(tkXform); |
| 277 |
tkSep->addChild(ticker); |
| 278 |
return tkSep; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
////////////////////////////////////////////////////////////// |
| 283 |
// |
| 284 |
SoSeparator * |
| 285 |
Axis::drawScale(float pos, float scale) |
| 286 |
// |
| 287 |
////////////////////////////////////////////////////////////// |
| 288 |
{ |
| 289 |
SoSeparator * scaleSep = new SoSeparator; |
| 290 |
|
| 291 |
SoFont *sclFont = new SoFont; |
| 292 |
sclFont->name.setValue("Helvetica"); |
| 293 |
sclFont->size.setValue(FONT_SIZE); |
| 294 |
|
| 295 |
SoTransform *tkXform = new SoTransform; |
| 296 |
pos += adjust; |
| 297 |
if(which == -1) |
| 298 |
{ |
| 299 |
tkXform->translation.setValue(-0.35, -pos, 0.00); |
| 300 |
tkXform->rotation.setValue(SbVec3f(1.0,0.0,0.0),M_PI); |
| 301 |
} |
| 302 |
else if(which == 1) |
| 303 |
{ |
| 304 |
if(type == LEFTBACK) |
| 305 |
tkXform->translation.setValue(0.00, pos-0.05, -0.16); |
| 306 |
else |
| 307 |
tkXform->translation.setValue(0.00, pos-0.05, 0.08); |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
tkXform->translation.setValue(0, pos, 0.05); |
| 312 |
} |
| 313 |
|
| 314 |
char strScale[10]; |
| 315 |
sprintf(strScale, "%8.2e", scale); |
| 316 |
|
| 317 |
#ifdef TD_FONT |
| 318 |
SoText3 *axScale = new SoText3; |
| 319 |
#else |
| 320 |
SoText2 *axScale = new SoText2; |
| 321 |
#endif |
| 322 |
axScale->string.setValue(strScale); |
| 323 |
|
| 324 |
scaleSep->addChild(tkXform); |
| 325 |
scaleSep->addChild(sclFont); |
| 326 |
scaleSep->addChild(axScale); |
| 327 |
return scaleSep; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
////////////////////////////////////////////////////////////// |
| 332 |
// |
| 333 |
SoSeparator * |
| 334 |
Axis::drawArrow() |
| 335 |
// |
| 336 |
////////////////////////////////////////////////////////////// |
| 337 |
{ |
| 338 |
SoSeparator * arrSep = new SoSeparator; |
| 339 |
SoTransform * arrXform = new SoTransform; |
| 340 |
if( which == -1 ) |
| 341 |
{ |
| 342 |
arrXform->translation.setValue(0.0, -1.1-adjust, 0.0); |
| 343 |
arrXform->rotation.setValue(SbVec3f(1, 0, 0), M_PI); |
| 344 |
} |
| 345 |
else |
| 346 |
arrXform->translation.setValue(0.0, 1.1+adjust, 0.0); |
| 347 |
|
| 348 |
arrSep->addChild(arrXform); |
| 349 |
|
| 350 |
SoCone * arr = new SoCone; |
| 351 |
arr->bottomRadius = 0.01; |
| 352 |
arr->height =0.1; |
| 353 |
arrSep->addChild(arr); |
| 354 |
|
| 355 |
return arrSep; |
| 356 |
} |