Warning, /include/Geant4/tools/sg/cube is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_cube
0005 #define tools_sg_cube
0006
0007 #include "node"
0008 #include "render_gstos"
0009
0010 #include "sf"
0011 #include "pick_action"
0012 #include "bbox_action"
0013
0014 namespace tools {
0015 namespace sg {
0016
0017 class cube : public node, public render_gstos {
0018 TOOLS_NODE(cube,tools::sg::cube,node)
0019 public:
0020 sf<float> width;
0021 sf<float> height;
0022 sf<float> depth;
0023 public:
0024 virtual const desc_fields& node_desc_fields() const {
0025 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::cube)
0026 static const desc_fields s_v(parent::node_desc_fields(),3, //WARNING : take care of count.
0027 TOOLS_ARG_FIELD_DESC(width),
0028 TOOLS_ARG_FIELD_DESC(height),
0029 TOOLS_ARG_FIELD_DESC(depth)
0030 );
0031 return s_v;
0032 }
0033 private:
0034 void add_fields(){
0035 add_field(&width);
0036 add_field(&height);
0037 add_field(&depth);
0038 }
0039 protected: //render_gstos
0040 virtual void visit(gstos_add& a_visitor,draw_type a_style) {
0041 visit<gstos_add>(a_visitor,a_style);
0042 }
0043 public:
0044 virtual void render(render_action& a_action) {
0045 if(gstos_render(a_action)) return;
0046 // immediate rendering :
0047 const state& state = a_action.state();
0048 bool draw_edges = false;
0049 if(state.m_draw_type==draw_filled) draw_edges = state.m_GL_LIGHTING?false:true;
0050
0051 if(draw_edges) {
0052 a_action.color4f(0,0,0,1); //if lighten, then rendered grey.
0053 a_action.line_width(1);
0054 visit(a_action,draw_lines);
0055 a_action.set_polygon_offset(true);
0056 a_action.color4f(state.m_color);
0057 a_action.line_width(state.m_line_width);
0058 }
0059
0060 visit(a_action,state.m_draw_type);
0061 if(draw_edges) a_action.set_polygon_offset(state.m_GL_POLYGON_OFFSET_FILL);
0062 }
0063
0064 virtual void pick(pick_action& a_action) {
0065 const state& state = a_action.state();
0066 if(a_action.stop_at_first()){
0067 visit(a_action,state.m_draw_type);
0068 if(a_action.done()) a_action.set_node(this);
0069 } else {
0070 a_action.set_done(false);
0071 a_action.zs().clear();
0072 a_action.ws().clear();
0073 visit(a_action,state.m_draw_type);
0074 if(a_action.done()) {
0075 a_action.add_pick(*this,a_action.zs(),a_action.ws(),a_action.state());
0076 a_action.set_done(false);
0077 }
0078 }
0079 }
0080 virtual void bbox(bbox_action& a_action) {
0081 const state& state = a_action.state();
0082 visit(a_action,state.m_draw_type);
0083 }
0084 public:
0085 cube()
0086 :parent()
0087 ,render_gstos()
0088 ,width(1.0f)
0089 ,height(1.0f)
0090 ,depth(1.0f)
0091 {
0092 add_fields();
0093 }
0094 virtual ~cube(){}
0095 public:
0096 cube(const cube& a_from)
0097 :parent(a_from)
0098 ,render_gstos(a_from)
0099 ,width(a_from.width)
0100 ,height(a_from.height)
0101 ,depth(a_from.depth)
0102 {
0103 add_fields();
0104 }
0105 cube& operator=(const cube& a_from){
0106 parent::operator=(a_from);
0107 render_gstos::operator=(a_from);
0108
0109 width = a_from.width;
0110 height = a_from.height;
0111 depth = a_from.depth;
0112
0113 return *this;
0114 }
0115 protected:
0116 void _faces(float front[],float back[], //[12]
0117 float right[],float left[],
0118 float top[],float bottom[]){
0119
0120 float wh = width.value()*0.5f;
0121 float hh = height.value()*0.5f;
0122 float dh = depth.value()*0.5f;
0123
0124 front[0] = wh;front[ 1] = -hh;front[ 2] = dh;
0125 front[3] = wh;front[ 4] = hh;front[ 5] = dh;
0126 front[6] = -wh;front[ 7] = hh;front[ 8] = dh;
0127 front[9] = -wh;front[10] = -hh;front[11] = dh;
0128
0129 back[0] = wh;back[ 1] = -hh;back[ 2] = -dh;
0130 back[3] = -wh;back[ 4] = -hh;back[ 5] = -dh;
0131 back[6] = -wh;back[ 7] = hh;back[ 8] = -dh;
0132 back[9] = wh;back[10] = hh;back[11] = -dh;
0133
0134 right[0] = wh;right[ 1] = -hh;right[ 2] = dh;
0135 right[3] = wh;right[ 4] = -hh;right[ 5] = -dh;
0136 right[6] = wh;right[ 7] = hh;right[ 8] = -dh;
0137 right[9] = wh;right[10] = hh;right[11] = dh;
0138
0139 left[0] = -wh;left[ 1] = -hh;left[ 2] = dh;
0140 left[3] = -wh;left[ 4] = hh;left[ 5] = dh;
0141 left[6] = -wh;left[ 7] = hh;left[ 8] = -dh;
0142 left[9] = -wh;left[10] = -hh;left[11] = -dh;
0143
0144 top[0] = wh;top[ 1] = hh;top[ 2] = dh;
0145 top[3] = wh;top[ 4] = hh;top[ 5] = -dh;
0146 top[6] = -wh;top[ 7] = hh;top[ 8] = -dh;
0147 top[9] = -wh;top[10] = hh;top[11] = dh;
0148
0149 bottom[0] = wh;bottom[ 1] = -hh;bottom[ 2] = dh;
0150 bottom[3] = -wh;bottom[ 4] = -hh;bottom[ 5] = dh;
0151 bottom[6] = -wh;bottom[ 7] = -hh;bottom[ 8] = -dh;
0152 bottom[9] = wh;bottom[10] = -hh;bottom[11] = -dh;
0153 }
0154
0155 void _tris(float tris[],float nms[]){ //[108]
0156 float front[12];
0157 float back[12];
0158 float right[12];
0159 float left[12];
0160 float top[12];
0161 float bottom[12];
0162
0163 _faces(front,back,right,left,top,bottom);
0164
0165 /////////////////////
0166 tris[0] = front[0];
0167 tris[1] = front[1];
0168 tris[2] = front[2];
0169
0170 tris[3] = front[3];
0171 tris[4] = front[4];
0172 tris[5] = front[5];
0173
0174 tris[6] = front[6];
0175 tris[7] = front[7];
0176 tris[8] = front[8];
0177 //
0178 tris[9] = front[6];
0179 tris[10] = front[7];
0180 tris[11] = front[8];
0181
0182 tris[12] = front[9];
0183 tris[13] = front[10];
0184 tris[14] = front[11];
0185
0186 tris[15] = front[0];
0187 tris[16] = front[1];
0188 tris[17] = front[2];
0189
0190 /////////////////////
0191 tris[18] = back[0];
0192 tris[19] = back[1];
0193 tris[20] = back[2];
0194
0195 tris[21] = back[3];
0196 tris[22] = back[4];
0197 tris[23] = back[5];
0198
0199 tris[24] = back[6];
0200 tris[25] = back[7];
0201 tris[26] = back[8];
0202 //
0203 tris[27] = back[6];
0204 tris[28] = back[7];
0205 tris[29] = back[8];
0206
0207 tris[30] = back[9];
0208 tris[31] = back[10];
0209 tris[32] = back[11];
0210
0211 tris[33] = back[0];
0212 tris[34] = back[1];
0213 tris[35] = back[2];
0214
0215 /////////////////////
0216 tris[36] = right[0];
0217 tris[37] = right[1];
0218 tris[38] = right[2];
0219
0220 tris[39] = right[3];
0221 tris[40] = right[4];
0222 tris[41] = right[5];
0223
0224 tris[42] = right[6];
0225 tris[43] = right[7];
0226 tris[44] = right[8];
0227 //
0228 tris[45] = right[6];
0229 tris[46] = right[7];
0230 tris[47] = right[8];
0231
0232 tris[48] = right[9];
0233 tris[49] = right[10];
0234 tris[50] = right[11];
0235
0236 tris[51] = right[0];
0237 tris[52] = right[1];
0238 tris[53] = right[2];
0239
0240 /////////////////////
0241 tris[54] = left[0];
0242 tris[55] = left[1];
0243 tris[56] = left[2];
0244
0245 tris[57] = left[3];
0246 tris[58] = left[4];
0247 tris[59] = left[5];
0248
0249 tris[60] = left[6];
0250 tris[61] = left[7];
0251 tris[62] = left[8];
0252 //
0253 tris[63] = left[6];
0254 tris[64] = left[7];
0255 tris[65] = left[8];
0256
0257 tris[66] = left[9];
0258 tris[67] = left[10];
0259 tris[68] = left[11];
0260
0261 tris[69] = left[0];
0262 tris[70] = left[1];
0263 tris[71] = left[2];
0264
0265 /////////////////////
0266 tris[72] = top[0];
0267 tris[73] = top[1];
0268 tris[74] = top[2];
0269
0270 tris[75] = top[3];
0271 tris[76] = top[4];
0272 tris[77] = top[5];
0273
0274 tris[78] = top[6];
0275 tris[79] = top[7];
0276 tris[80] = top[8];
0277 //
0278 tris[81] = top[6];
0279 tris[82] = top[7];
0280 tris[83] = top[8];
0281
0282 tris[84] = top[9];
0283 tris[85] = top[10];
0284 tris[86] = top[11];
0285
0286 tris[87] = top[0];
0287 tris[88] = top[1];
0288 tris[89] = top[2];
0289
0290 /////////////////////
0291 tris[90] = bottom[0];
0292 tris[91] = bottom[1];
0293 tris[92] = bottom[2];
0294
0295 tris[93] = bottom[3];
0296 tris[94] = bottom[4];
0297 tris[95] = bottom[5];
0298
0299 tris[96] = bottom[6];
0300 tris[97] = bottom[7];
0301 tris[98] = bottom[8];
0302 //
0303 tris[99] = bottom[6];
0304 tris[100] = bottom[7];
0305 tris[101] = bottom[8];
0306
0307 tris[102] = bottom[9];
0308 tris[103] = bottom[10];
0309 tris[104] = bottom[11];
0310
0311 tris[105] = bottom[0];
0312 tris[106] = bottom[1];
0313 tris[107] = bottom[2];
0314 /////////////////////
0315
0316 ///////////////////// front
0317 nms[0] = 0;
0318 nms[1] = 0;
0319 nms[2] = 1;
0320
0321 nms[3] = 0;
0322 nms[4] = 0;
0323 nms[5] = 1;
0324
0325 nms[6] = 0;
0326 nms[7] = 0;
0327 nms[8] = 1;
0328 //
0329 nms[9] = 0;
0330 nms[10] = 0;
0331 nms[11] = 1;
0332
0333 nms[12] = 0;
0334 nms[13] = 0;
0335 nms[14] = 1;
0336
0337 nms[15] = 0;
0338 nms[16] = 0;
0339 nms[17] = 1;
0340
0341 ///////////////////// back
0342 nms[18] = 0;
0343 nms[19] = 0;
0344 nms[20] = -1;
0345
0346 nms[21] = 0;
0347 nms[22] = 0;
0348 nms[23] = -1;
0349
0350 nms[24] = 0;
0351 nms[25] = 0;
0352 nms[26] = -1;
0353 //
0354 nms[27] = 0;
0355 nms[28] = 0;
0356 nms[29] = -1;
0357
0358 nms[30] = 0;
0359 nms[31] = 0;
0360 nms[32] = -1;
0361
0362 nms[33] = 0;
0363 nms[34] = 0;
0364 nms[35] = -1;
0365
0366 ///////////////////// right
0367 nms[36] = 1;
0368 nms[37] = 0;
0369 nms[38] = 0;
0370
0371 nms[39] = 1;
0372 nms[40] = 0;
0373 nms[41] = 0;
0374
0375 nms[42] = 1;
0376 nms[43] = 0;
0377 nms[44] = 0;
0378 //
0379 nms[45] = 1;
0380 nms[46] = 0;
0381 nms[47] = 0;
0382
0383 nms[48] = 1;
0384 nms[49] = 0;
0385 nms[50] = 0;
0386
0387 nms[51] = 1;
0388 nms[52] = 0;
0389 nms[53] = 0;
0390
0391 ///////////////////// left
0392 nms[54] = -1;
0393 nms[55] = 0;
0394 nms[56] = 0;
0395
0396 nms[57] = -1;
0397 nms[58] = 0;
0398 nms[59] = 0;
0399
0400 nms[60] = -1;
0401 nms[61] = 0;
0402 nms[62] = 0;
0403 //
0404 nms[63] = -1;
0405 nms[64] = 0;
0406 nms[65] = 0;
0407
0408 nms[66] = -1;
0409 nms[67] = 0;
0410 nms[68] = 0;
0411
0412 nms[69] = -1;
0413 nms[70] = 0;
0414 nms[71] = 0;
0415
0416 ///////////////////// top
0417 nms[72] = 0;
0418 nms[73] = 1;
0419 nms[74] = 0;
0420
0421 nms[75] = 0;
0422 nms[76] = 1;
0423 nms[77] = 0;
0424
0425 nms[78] = 0;
0426 nms[79] = 1;
0427 nms[80] = 0;
0428 //
0429 nms[81] = 0;
0430 nms[82] = 1;
0431 nms[83] = 0;
0432
0433 nms[84] = 0;
0434 nms[85] = 1;
0435 nms[86] = 0;
0436
0437 nms[87] = 0;
0438 nms[88] = 1;
0439 nms[89] = 0;
0440
0441 ///////////////////// bottom
0442 nms[90] = 0;
0443 nms[91] = -1;
0444 nms[92] = 0;
0445
0446 nms[93] = 0;
0447 nms[94] = -1;
0448 nms[95] = 0;
0449
0450 nms[96] = 0;
0451 nms[97] = -1;
0452 nms[98] = 0;
0453 //
0454 nms[99] = 0;
0455 nms[100] = -1;
0456 nms[101] = 0;
0457
0458 nms[102] = 0;
0459 nms[103] = -1;
0460 nms[104] = 0;
0461
0462 nms[105] = 0;
0463 nms[106] = -1;
0464 nms[107] = 0;
0465 /////////////////////
0466 }
0467
0468 void _lines(float lines[]) { //[144]
0469 float front[12];
0470 float back[12];
0471 float right[12];
0472 float left[12];
0473 float top[12];
0474 float bottom[12];
0475
0476 _faces(front,back,right,left,top,bottom);
0477
0478 //4 segs * 2 points * 3 coords * 6 faces.
0479
0480 //4*2*3*6 = 24*6 = 144
0481
0482 lines[0] = front[0];
0483 lines[1] = front[1];
0484 lines[2] = front[2];
0485 lines[3] = front[3];
0486 lines[4] = front[4];
0487 lines[5] = front[5];
0488
0489 lines[6] = front[3];
0490 lines[7] = front[4];
0491 lines[8] = front[5];
0492 lines[9] = front[6];
0493 lines[10] = front[7];
0494 lines[11] = front[8];
0495
0496 lines[12] = front[6];
0497 lines[13] = front[7];
0498 lines[14] = front[8];
0499 lines[15] = front[9];
0500 lines[16] = front[10];
0501 lines[17] = front[11];
0502
0503 lines[18] = front[9];
0504 lines[19] = front[10];
0505 lines[20] = front[11];
0506 lines[21] = front[0];
0507 lines[22] = front[1];
0508 lines[23] = front[2];
0509
0510 lines[24] = back[0];
0511 lines[25] = back[1];
0512 lines[26] = back[2];
0513 lines[27] = back[3];
0514 lines[28] = back[4];
0515 lines[29] = back[5];
0516
0517 lines[30] = back[3];
0518 lines[31] = back[4];
0519 lines[32] = back[5];
0520 lines[33] = back[6];
0521 lines[34] = back[7];
0522 lines[35] = back[8];
0523
0524 lines[36] = back[6];
0525 lines[37] = back[7];
0526 lines[38] = back[8];
0527 lines[39] = back[9];
0528 lines[40] = back[10];
0529 lines[41] = back[11];
0530
0531 lines[42] = back[9];
0532 lines[43] = back[10];
0533 lines[44] = back[11];
0534 lines[45] = back[0];
0535 lines[46] = back[1];
0536 lines[47] = back[2];
0537
0538 lines[48] = left[0];
0539 lines[49] = left[1];
0540 lines[50] = left[2];
0541 lines[51] = left[3];
0542 lines[52] = left[4];
0543 lines[53] = left[5];
0544
0545 lines[54] = left[3];
0546 lines[55] = left[4];
0547 lines[56] = left[5];
0548 lines[57] = left[6];
0549 lines[58] = left[7];
0550 lines[59] = left[8];
0551
0552 lines[60] = left[6];
0553 lines[61] = left[7];
0554 lines[62] = left[8];
0555 lines[63] = left[9];
0556 lines[64] = left[10];
0557 lines[65] = left[11];
0558
0559 lines[66] = left[9];
0560 lines[67] = left[10];
0561 lines[68] = left[11];
0562 lines[69] = left[0];
0563 lines[70] = left[1];
0564 lines[71] = left[2];
0565
0566 lines[72] = right[0];
0567 lines[73] = right[1];
0568 lines[74] = right[2];
0569 lines[75] = right[3];
0570 lines[76] = right[4];
0571 lines[77] = right[5];
0572
0573 lines[78] = right[3];
0574 lines[79] = right[4];
0575 lines[80] = right[5];
0576 lines[81] = right[6];
0577 lines[82] = right[7];
0578 lines[83] = right[8];
0579
0580 lines[84] = right[6];
0581 lines[85] = right[7];
0582 lines[86] = right[8];
0583 lines[87] = right[9];
0584 lines[88] = right[10];
0585 lines[89] = right[11];
0586
0587 lines[90] = right[9];
0588 lines[91] = right[10];
0589 lines[92] = right[11];
0590 lines[93] = right[0];
0591 lines[94] = right[1];
0592 lines[95] = right[2];
0593
0594 lines[96] = top[0];
0595 lines[97] = top[1];
0596 lines[98] = top[2];
0597 lines[99] = top[3];
0598 lines[100] = top[4];
0599 lines[101] = top[5];
0600
0601 lines[102] = top[3];
0602 lines[103] = top[4];
0603 lines[104] = top[5];
0604 lines[105] = top[6];
0605 lines[106] = top[7];
0606 lines[107] = top[8];
0607
0608 lines[108] = top[6];
0609 lines[109] = top[7];
0610 lines[110] = top[8];
0611 lines[111] = top[9];
0612 lines[112] = top[10];
0613 lines[113] = top[11];
0614
0615 lines[114] = top[9];
0616 lines[115] = top[10];
0617 lines[116] = top[11];
0618 lines[117] = top[0];
0619 lines[118] = top[1];
0620 lines[119] = top[2];
0621
0622 lines[120] = bottom[0];
0623 lines[121] = bottom[1];
0624 lines[122] = bottom[2];
0625 lines[123] = bottom[3];
0626 lines[124] = bottom[4];
0627 lines[125] = bottom[5];
0628
0629 lines[126] = bottom[3];
0630 lines[127] = bottom[4];
0631 lines[128] = bottom[5];
0632 lines[129] = bottom[6];
0633 lines[130] = bottom[7];
0634 lines[131] = bottom[8];
0635
0636 lines[132] = bottom[6];
0637 lines[133] = bottom[7];
0638 lines[134] = bottom[8];
0639 lines[135] = bottom[9];
0640 lines[136] = bottom[10];
0641 lines[137] = bottom[11];
0642
0643 lines[138] = bottom[9];
0644 lines[139] = bottom[10];
0645 lines[140] = bottom[11];
0646 lines[141] = bottom[0];
0647 lines[142] = bottom[1];
0648 lines[143] = bottom[2];
0649 }
0650
0651 void _points(float points[]) { //[24]
0652 float wh = width.value()*0.5f;
0653 float hh = height.value()*0.5f;
0654 float dh = depth.value()*0.5f;
0655
0656 points[0] = wh;points[ 1] = -hh;points[ 2] = dh;
0657 points[3] = wh;points[ 4] = hh;points[ 5] = dh;
0658 points[6] = -wh;points[ 7] = hh;points[ 8] = dh;
0659 points[9] = -wh;points[10] = -hh;points[11] = dh;
0660
0661 points[12+0] = wh;points[12+ 1] = -hh;points[12+ 2] = -dh;
0662 points[12+3] = -wh;points[12+ 4] = -hh;points[12+ 5] = -dh;
0663 points[12+6] = -wh;points[12+ 7] = hh;points[12+ 8] = -dh;
0664 points[12+9] = wh;points[12+10] = hh;points[12+11] = -dh;
0665 }
0666
0667 template <class T>
0668 void visit(T& a_visitor,draw_type a_style){
0669 if(a_style==draw_points) {
0670 float points[24];
0671 _points(points);
0672 a_visitor.add_points(24,points);
0673
0674 } else if(a_style==draw_lines) {
0675 float lines[144]; //segments
0676 _lines(lines);
0677 a_visitor.add_lines(144,lines);
0678
0679 } else if(a_style==draw_filled) {
0680 float tris[108];float nms[108];
0681 _tris(tris,nms);
0682 a_visitor.add_triangles_normal(108,tris,nms);
0683 }
0684 }
0685 };
0686
0687 }}
0688
0689 #endif