Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_cube 5 #define tools_sg_cube 6 7 #include "node" 8 #include "render_gstos" 9 10 #include "sf" 11 #include "pick_action" 12 #include "bbox_action" 13 14 namespace tools { 15 namespace sg { 16 17 class cube : public node, public render_gstos 18 TOOLS_NODE(cube,tools::sg::cube,node) 19 public: 20 sf<float> width; 21 sf<float> height; 22 sf<float> depth; 23 public: 24 virtual const desc_fields& node_desc_fields( 25 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::cub 26 static const desc_fields s_v(parent::node_ 27 TOOLS_ARG_FIELD_DESC(width), 28 TOOLS_ARG_FIELD_DESC(height), 29 TOOLS_ARG_FIELD_DESC(depth) 30 ); 31 return s_v; 32 } 33 private: 34 void add_fields(){ 35 add_field(&width); 36 add_field(&height); 37 add_field(&depth); 38 } 39 protected: //render_gstos 40 virtual void visit(gstos_add& a_visitor,draw 41 visit<gstos_add>(a_visitor,a_style); 42 } 43 public: 44 virtual void render(render_action& a_action) 45 if(gstos_render(a_action)) return; 46 // immediate rendering : 47 const state& state = a_action.state(); 48 bool draw_edges = false; 49 if(state.m_draw_type==draw_filled) draw_ed 50 51 if(draw_edges) { 52 a_action.color4f(0,0,0,1); //if lighten, 53 a_action.line_width(1); 54 visit(a_action,draw_lines); 55 a_action.set_polygon_offset(true); 56 a_action.color4f(state.m_color); 57 a_action.line_width(state.m_line_width); 58 } 59 60 visit(a_action,state.m_draw_type); 61 if(draw_edges) a_action.set_polygon_offset 62 } 63 64 virtual void pick(pick_action& a_action) { 65 const state& state = a_action.state(); 66 if(a_action.stop_at_first()){ 67 visit(a_action,state.m_draw_type); 68 if(a_action.done()) a_action.set_node(th 69 } else { 70 a_action.set_done(false); 71 a_action.zs().clear(); 72 a_action.ws().clear(); 73 visit(a_action,state.m_draw_type); 74 if(a_action.done()) { 75 a_action.add_pick(*this,a_action.zs(), 76 a_action.set_done(false); 77 } 78 } 79 } 80 virtual void bbox(bbox_action& a_action) { 81 const state& state = a_action.state(); 82 visit(a_action,state.m_draw_type); 83 } 84 public: 85 cube() 86 :parent() 87 ,render_gstos() 88 ,width(1.0f) 89 ,height(1.0f) 90 ,depth(1.0f) 91 { 92 add_fields(); 93 } 94 virtual ~cube(){} 95 public: 96 cube(const cube& a_from) 97 :parent(a_from) 98 ,render_gstos(a_from) 99 ,width(a_from.width) 100 ,height(a_from.height) 101 ,depth(a_from.depth) 102 { 103 add_fields(); 104 } 105 cube& operator=(const cube& a_from){ 106 parent::operator=(a_from); 107 render_gstos::operator=(a_from); 108 109 width = a_from.width; 110 height = a_from.height; 111 depth = a_from.depth; 112 113 return *this; 114 } 115 protected: 116 void _faces(float front[],float back[], // 117 float right[],float left[], 118 float top[],float bottom[]){ 119 120 float wh = width.value()*0.5f; 121 float hh = height.value()*0.5f; 122 float dh = depth.value()*0.5f; 123 124 front[0] = wh;front[ 1] = -hh;front[ 2] = 125 front[3] = wh;front[ 4] = hh;front[ 5] = 126 front[6] = -wh;front[ 7] = hh;front[ 8] = 127 front[9] = -wh;front[10] = -hh;front[11] = 128 129 back[0] = wh;back[ 1] = -hh;back[ 2] = -d 130 back[3] = -wh;back[ 4] = -hh;back[ 5] = -d 131 back[6] = -wh;back[ 7] = hh;back[ 8] = -d 132 back[9] = wh;back[10] = hh;back[11] = -d 133 134 right[0] = wh;right[ 1] = -hh;right[ 2] = 135 right[3] = wh;right[ 4] = -hh;right[ 5] = 136 right[6] = wh;right[ 7] = hh;right[ 8] = 137 right[9] = wh;right[10] = hh;right[11] = 138 139 left[0] = -wh;left[ 1] = -hh;left[ 2] = d 140 left[3] = -wh;left[ 4] = hh;left[ 5] = d 141 left[6] = -wh;left[ 7] = hh;left[ 8] = -d 142 left[9] = -wh;left[10] = -hh;left[11] = -d 143 144 top[0] = wh;top[ 1] = hh;top[ 2] = dh; 145 top[3] = wh;top[ 4] = hh;top[ 5] = -dh; 146 top[6] = -wh;top[ 7] = hh;top[ 8] = -dh; 147 top[9] = -wh;top[10] = hh;top[11] = dh; 148 149 bottom[0] = wh;bottom[ 1] = -hh;bottom[ 2 150 bottom[3] = -wh;bottom[ 4] = -hh;bottom[ 5 151 bottom[6] = -wh;bottom[ 7] = -hh;bottom[ 8 152 bottom[9] = wh;bottom[10] = -hh;bottom[11 153 } 154 155 void _tris(float tris[],float nms[]){ //[108 156 float front[12]; 157 float back[12]; 158 float right[12]; 159 float left[12]; 160 float top[12]; 161 float bottom[12]; 162 163 _faces(front,back,right,left,top,bottom); 164 165 ///////////////////// 166 tris[0] = front[0]; 167 tris[1] = front[1]; 168 tris[2] = front[2]; 169 170 tris[3] = front[3]; 171 tris[4] = front[4]; 172 tris[5] = front[5]; 173 174 tris[6] = front[6]; 175 tris[7] = front[7]; 176 tris[8] = front[8]; 177 // 178 tris[9] = front[6]; 179 tris[10] = front[7]; 180 tris[11] = front[8]; 181 182 tris[12] = front[9]; 183 tris[13] = front[10]; 184 tris[14] = front[11]; 185 186 tris[15] = front[0]; 187 tris[16] = front[1]; 188 tris[17] = front[2]; 189 190 ///////////////////// 191 tris[18] = back[0]; 192 tris[19] = back[1]; 193 tris[20] = back[2]; 194 195 tris[21] = back[3]; 196 tris[22] = back[4]; 197 tris[23] = back[5]; 198 199 tris[24] = back[6]; 200 tris[25] = back[7]; 201 tris[26] = back[8]; 202 // 203 tris[27] = back[6]; 204 tris[28] = back[7]; 205 tris[29] = back[8]; 206 207 tris[30] = back[9]; 208 tris[31] = back[10]; 209 tris[32] = back[11]; 210 211 tris[33] = back[0]; 212 tris[34] = back[1]; 213 tris[35] = back[2]; 214 215 ///////////////////// 216 tris[36] = right[0]; 217 tris[37] = right[1]; 218 tris[38] = right[2]; 219 220 tris[39] = right[3]; 221 tris[40] = right[4]; 222 tris[41] = right[5]; 223 224 tris[42] = right[6]; 225 tris[43] = right[7]; 226 tris[44] = right[8]; 227 // 228 tris[45] = right[6]; 229 tris[46] = right[7]; 230 tris[47] = right[8]; 231 232 tris[48] = right[9]; 233 tris[49] = right[10]; 234 tris[50] = right[11]; 235 236 tris[51] = right[0]; 237 tris[52] = right[1]; 238 tris[53] = right[2]; 239 240 ///////////////////// 241 tris[54] = left[0]; 242 tris[55] = left[1]; 243 tris[56] = left[2]; 244 245 tris[57] = left[3]; 246 tris[58] = left[4]; 247 tris[59] = left[5]; 248 249 tris[60] = left[6]; 250 tris[61] = left[7]; 251 tris[62] = left[8]; 252 // 253 tris[63] = left[6]; 254 tris[64] = left[7]; 255 tris[65] = left[8]; 256 257 tris[66] = left[9]; 258 tris[67] = left[10]; 259 tris[68] = left[11]; 260 261 tris[69] = left[0]; 262 tris[70] = left[1]; 263 tris[71] = left[2]; 264 265 ///////////////////// 266 tris[72] = top[0]; 267 tris[73] = top[1]; 268 tris[74] = top[2]; 269 270 tris[75] = top[3]; 271 tris[76] = top[4]; 272 tris[77] = top[5]; 273 274 tris[78] = top[6]; 275 tris[79] = top[7]; 276 tris[80] = top[8]; 277 // 278 tris[81] = top[6]; 279 tris[82] = top[7]; 280 tris[83] = top[8]; 281 282 tris[84] = top[9]; 283 tris[85] = top[10]; 284 tris[86] = top[11]; 285 286 tris[87] = top[0]; 287 tris[88] = top[1]; 288 tris[89] = top[2]; 289 290 ///////////////////// 291 tris[90] = bottom[0]; 292 tris[91] = bottom[1]; 293 tris[92] = bottom[2]; 294 295 tris[93] = bottom[3]; 296 tris[94] = bottom[4]; 297 tris[95] = bottom[5]; 298 299 tris[96] = bottom[6]; 300 tris[97] = bottom[7]; 301 tris[98] = bottom[8]; 302 // 303 tris[99] = bottom[6]; 304 tris[100] = bottom[7]; 305 tris[101] = bottom[8]; 306 307 tris[102] = bottom[9]; 308 tris[103] = bottom[10]; 309 tris[104] = bottom[11]; 310 311 tris[105] = bottom[0]; 312 tris[106] = bottom[1]; 313 tris[107] = bottom[2]; 314 ///////////////////// 315 316 ///////////////////// front 317 nms[0] = 0; 318 nms[1] = 0; 319 nms[2] = 1; 320 321 nms[3] = 0; 322 nms[4] = 0; 323 nms[5] = 1; 324 325 nms[6] = 0; 326 nms[7] = 0; 327 nms[8] = 1; 328 // 329 nms[9] = 0; 330 nms[10] = 0; 331 nms[11] = 1; 332 333 nms[12] = 0; 334 nms[13] = 0; 335 nms[14] = 1; 336 337 nms[15] = 0; 338 nms[16] = 0; 339 nms[17] = 1; 340 341 ///////////////////// back 342 nms[18] = 0; 343 nms[19] = 0; 344 nms[20] = -1; 345 346 nms[21] = 0; 347 nms[22] = 0; 348 nms[23] = -1; 349 350 nms[24] = 0; 351 nms[25] = 0; 352 nms[26] = -1; 353 // 354 nms[27] = 0; 355 nms[28] = 0; 356 nms[29] = -1; 357 358 nms[30] = 0; 359 nms[31] = 0; 360 nms[32] = -1; 361 362 nms[33] = 0; 363 nms[34] = 0; 364 nms[35] = -1; 365 366 ///////////////////// right 367 nms[36] = 1; 368 nms[37] = 0; 369 nms[38] = 0; 370 371 nms[39] = 1; 372 nms[40] = 0; 373 nms[41] = 0; 374 375 nms[42] = 1; 376 nms[43] = 0; 377 nms[44] = 0; 378 // 379 nms[45] = 1; 380 nms[46] = 0; 381 nms[47] = 0; 382 383 nms[48] = 1; 384 nms[49] = 0; 385 nms[50] = 0; 386 387 nms[51] = 1; 388 nms[52] = 0; 389 nms[53] = 0; 390 391 ///////////////////// left 392 nms[54] = -1; 393 nms[55] = 0; 394 nms[56] = 0; 395 396 nms[57] = -1; 397 nms[58] = 0; 398 nms[59] = 0; 399 400 nms[60] = -1; 401 nms[61] = 0; 402 nms[62] = 0; 403 // 404 nms[63] = -1; 405 nms[64] = 0; 406 nms[65] = 0; 407 408 nms[66] = -1; 409 nms[67] = 0; 410 nms[68] = 0; 411 412 nms[69] = -1; 413 nms[70] = 0; 414 nms[71] = 0; 415 416 ///////////////////// top 417 nms[72] = 0; 418 nms[73] = 1; 419 nms[74] = 0; 420 421 nms[75] = 0; 422 nms[76] = 1; 423 nms[77] = 0; 424 425 nms[78] = 0; 426 nms[79] = 1; 427 nms[80] = 0; 428 // 429 nms[81] = 0; 430 nms[82] = 1; 431 nms[83] = 0; 432 433 nms[84] = 0; 434 nms[85] = 1; 435 nms[86] = 0; 436 437 nms[87] = 0; 438 nms[88] = 1; 439 nms[89] = 0; 440 441 ///////////////////// bottom 442 nms[90] = 0; 443 nms[91] = -1; 444 nms[92] = 0; 445 446 nms[93] = 0; 447 nms[94] = -1; 448 nms[95] = 0; 449 450 nms[96] = 0; 451 nms[97] = -1; 452 nms[98] = 0; 453 // 454 nms[99] = 0; 455 nms[100] = -1; 456 nms[101] = 0; 457 458 nms[102] = 0; 459 nms[103] = -1; 460 nms[104] = 0; 461 462 nms[105] = 0; 463 nms[106] = -1; 464 nms[107] = 0; 465 ///////////////////// 466 } 467 468 void _lines(float lines[]) { //[144] 469 float front[12]; 470 float back[12]; 471 float right[12]; 472 float left[12]; 473 float top[12]; 474 float bottom[12]; 475 476 _faces(front,back,right,left,top,bottom); 477 478 //4 segs * 2 points * 3 coords * 6 faces. 479 480 //4*2*3*6 = 24*6 = 144 481 482 lines[0] = front[0]; 483 lines[1] = front[1]; 484 lines[2] = front[2]; 485 lines[3] = front[3]; 486 lines[4] = front[4]; 487 lines[5] = front[5]; 488 489 lines[6] = front[3]; 490 lines[7] = front[4]; 491 lines[8] = front[5]; 492 lines[9] = front[6]; 493 lines[10] = front[7]; 494 lines[11] = front[8]; 495 496 lines[12] = front[6]; 497 lines[13] = front[7]; 498 lines[14] = front[8]; 499 lines[15] = front[9]; 500 lines[16] = front[10]; 501 lines[17] = front[11]; 502 503 lines[18] = front[9]; 504 lines[19] = front[10]; 505 lines[20] = front[11]; 506 lines[21] = front[0]; 507 lines[22] = front[1]; 508 lines[23] = front[2]; 509 510 lines[24] = back[0]; 511 lines[25] = back[1]; 512 lines[26] = back[2]; 513 lines[27] = back[3]; 514 lines[28] = back[4]; 515 lines[29] = back[5]; 516 517 lines[30] = back[3]; 518 lines[31] = back[4]; 519 lines[32] = back[5]; 520 lines[33] = back[6]; 521 lines[34] = back[7]; 522 lines[35] = back[8]; 523 524 lines[36] = back[6]; 525 lines[37] = back[7]; 526 lines[38] = back[8]; 527 lines[39] = back[9]; 528 lines[40] = back[10]; 529 lines[41] = back[11]; 530 531 lines[42] = back[9]; 532 lines[43] = back[10]; 533 lines[44] = back[11]; 534 lines[45] = back[0]; 535 lines[46] = back[1]; 536 lines[47] = back[2]; 537 538 lines[48] = left[0]; 539 lines[49] = left[1]; 540 lines[50] = left[2]; 541 lines[51] = left[3]; 542 lines[52] = left[4]; 543 lines[53] = left[5]; 544 545 lines[54] = left[3]; 546 lines[55] = left[4]; 547 lines[56] = left[5]; 548 lines[57] = left[6]; 549 lines[58] = left[7]; 550 lines[59] = left[8]; 551 552 lines[60] = left[6]; 553 lines[61] = left[7]; 554 lines[62] = left[8]; 555 lines[63] = left[9]; 556 lines[64] = left[10]; 557 lines[65] = left[11]; 558 559 lines[66] = left[9]; 560 lines[67] = left[10]; 561 lines[68] = left[11]; 562 lines[69] = left[0]; 563 lines[70] = left[1]; 564 lines[71] = left[2]; 565 566 lines[72] = right[0]; 567 lines[73] = right[1]; 568 lines[74] = right[2]; 569 lines[75] = right[3]; 570 lines[76] = right[4]; 571 lines[77] = right[5]; 572 573 lines[78] = right[3]; 574 lines[79] = right[4]; 575 lines[80] = right[5]; 576 lines[81] = right[6]; 577 lines[82] = right[7]; 578 lines[83] = right[8]; 579 580 lines[84] = right[6]; 581 lines[85] = right[7]; 582 lines[86] = right[8]; 583 lines[87] = right[9]; 584 lines[88] = right[10]; 585 lines[89] = right[11]; 586 587 lines[90] = right[9]; 588 lines[91] = right[10]; 589 lines[92] = right[11]; 590 lines[93] = right[0]; 591 lines[94] = right[1]; 592 lines[95] = right[2]; 593 594 lines[96] = top[0]; 595 lines[97] = top[1]; 596 lines[98] = top[2]; 597 lines[99] = top[3]; 598 lines[100] = top[4]; 599 lines[101] = top[5]; 600 601 lines[102] = top[3]; 602 lines[103] = top[4]; 603 lines[104] = top[5]; 604 lines[105] = top[6]; 605 lines[106] = top[7]; 606 lines[107] = top[8]; 607 608 lines[108] = top[6]; 609 lines[109] = top[7]; 610 lines[110] = top[8]; 611 lines[111] = top[9]; 612 lines[112] = top[10]; 613 lines[113] = top[11]; 614 615 lines[114] = top[9]; 616 lines[115] = top[10]; 617 lines[116] = top[11]; 618 lines[117] = top[0]; 619 lines[118] = top[1]; 620 lines[119] = top[2]; 621 622 lines[120] = bottom[0]; 623 lines[121] = bottom[1]; 624 lines[122] = bottom[2]; 625 lines[123] = bottom[3]; 626 lines[124] = bottom[4]; 627 lines[125] = bottom[5]; 628 629 lines[126] = bottom[3]; 630 lines[127] = bottom[4]; 631 lines[128] = bottom[5]; 632 lines[129] = bottom[6]; 633 lines[130] = bottom[7]; 634 lines[131] = bottom[8]; 635 636 lines[132] = bottom[6]; 637 lines[133] = bottom[7]; 638 lines[134] = bottom[8]; 639 lines[135] = bottom[9]; 640 lines[136] = bottom[10]; 641 lines[137] = bottom[11]; 642 643 lines[138] = bottom[9]; 644 lines[139] = bottom[10]; 645 lines[140] = bottom[11]; 646 lines[141] = bottom[0]; 647 lines[142] = bottom[1]; 648 lines[143] = bottom[2]; 649 } 650 651 void _points(float points[]) { //[24] 652 float wh = width.value()*0.5f; 653 float hh = height.value()*0.5f; 654 float dh = depth.value()*0.5f; 655 656 points[0] = wh;points[ 1] = -hh;points[ 2 657 points[3] = wh;points[ 4] = hh;points[ 5 658 points[6] = -wh;points[ 7] = hh;points[ 8 659 points[9] = -wh;points[10] = -hh;points[11 660 661 points[12+0] = wh;points[12+ 1] = -hh;poi 662 points[12+3] = -wh;points[12+ 4] = -hh;poi 663 points[12+6] = -wh;points[12+ 7] = hh;poi 664 points[12+9] = wh;points[12+10] = hh;poi 665 } 666 667 template <class T> 668 void visit(T& a_visitor,draw_type a_style){ 669 if(a_style==draw_points) { 670 float points[24]; 671 _points(points); 672 a_visitor.add_points(24,points); 673 674 } else if(a_style==draw_lines) { 675 float lines[144]; //segments 676 _lines(lines); 677 a_visitor.add_lines(144,lines); 678 679 } else if(a_style==draw_filled) { 680 float tris[108];float nms[108]; 681 _tris(tris,nms); 682 a_visitor.add_triangles_normal(108,tris, 683 } 684 } 685 }; 686 687 }} 688 689 #endif