CMS 3D CMS Logo

IgSoCrystalHit.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Inventor/interface/IgSoCrystalHit.h"
00004 #include <Inventor/nodes/SoVertexProperty.h>
00005 #include <Inventor/nodes/SoIndexedFaceSet.h>
00006 #include <Inventor/nodes/SoIndexedLineSet.h>
00007 #include <Inventor/nodes/SoShapeHints.h>
00008 
00009 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00010 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00011 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00012 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00013 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00014 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00015  
00016 SO_KIT_SOURCE (IgSoCrystalHit);
00017 
00018 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00019 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00020 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00021 
00022 void
00023 IgSoCrystalHit::initClass (void)
00024 { SO_KIT_INIT_CLASS (IgSoCrystalHit, IgSoShapeKit, "IgSoShapeKit"); }
00025 
00026 IgSoCrystalHit::IgSoCrystalHit (void)
00027 {
00028     SO_KIT_CONSTRUCTOR (IgSoCrystalHit);
00029     SO_KIT_ADD_FIELD (energy,           (0.1));
00030     SO_KIT_ADD_FIELD (minimumEnergy,    (0.001));  // 1 MeV 
00031     SO_KIT_ADD_FIELD (scale,            (1.0));
00032     SO_KIT_ADD_FIELD (relativeWidth,    (0.85));
00033  
00034     SO_KIT_ADD_FIELD (drawCrystal,      (TRUE));
00035     SO_KIT_ADD_FIELD (drawHit,          (TRUE));
00036     SO_KIT_ADD_FIELD (showLines,        (FALSE));
00037  
00038     SO_KIT_ADD_FIELD (front1,           (0.5,  0.5, 0.0));   // initialise to unit box in +z
00039     SO_KIT_ADD_FIELD (front2,           (-0.5,  0.5, 0.0));
00040     SO_KIT_ADD_FIELD (front3,           (-0.5, -0.5, 0.0));
00041     SO_KIT_ADD_FIELD (front4,           (0.5, -0.5, 0.0));
00042 
00043     SO_KIT_ADD_FIELD (back1,            (0.5,  0.5, 1.0));
00044     SO_KIT_ADD_FIELD (back2,            (-0.5,  0.5, 1.0));
00045     SO_KIT_ADD_FIELD (back3,            (-0.5, -0.5, 1.0));
00046     SO_KIT_ADD_FIELD (back4,            (0.5, -0.5, 1.0));
00047 
00048     SO_KIT_ADD_CATALOG_ENTRY (shapeHints, SoShapeHints, FALSE, separator,\x0, TRUE);
00049     SO_KIT_ADD_CATALOG_ENTRY (faces,  SoIndexedFaceSet, FALSE, separator,\x0, TRUE);
00050     SO_KIT_ADD_CATALOG_ENTRY (lines,  SoIndexedLineSet, FALSE, separator,\x0, TRUE);
00051     SO_KIT_INIT_INSTANCE ();
00052     setUpConnections (true, true);
00053 }
00054 
00055 void
00056 IgSoCrystalHit::refresh (void)
00057 {
00058     if (energy.getValue () < 0
00059         || scale.getValue () < 0
00060         || relativeWidth.getValue () < 0
00061         || relativeWidth.getValue () > 1)
00062     {
00063         setPart ("shapeHints", NULL);
00064         setPart ("faces", NULL);
00065         setPart ("lines", NULL);
00066         return;
00067     }
00068     
00069     SoShapeHints *shapeHints = new SoShapeHints;
00070     shapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00071     shapeHints->shapeType = SoShapeHints::SOLID;
00072     shapeHints->faceType = SoShapeHints::CONVEX;
00073 
00074     SoIndexedFaceSet *faces = new SoIndexedFaceSet;
00075 
00076     // Crystal part of shape
00077     SbVec3f vertices [16];
00078     vertices [0] = front1.getValue ();
00079     vertices [1] = front2.getValue ();
00080     vertices [2] = front3.getValue ();
00081     vertices [3] = front4.getValue ();
00082 
00083     vertices [4] = back1.getValue ();
00084     vertices [5] = back2.getValue ();
00085     vertices [6] = back3.getValue ();
00086     vertices [7] = back4.getValue ();
00087 
00088     // Energy part
00089 
00090     // First find shape of crystal shrunk laterally according to
00091     // relativeWidth.  Take half off each side.  Then calculate
00092     // shrunken front and back planes coplanar with the original front
00093     // and back.  Move a tiny distance away along crystal axis from
00094     // the back face of the crystal so line around base is not lost.
00095     // Finally make the length of the hit proportional to the energy.
00096     float eScale = scale.getValue () * energy.getValue ();
00097     float delta = .5 * (1. + relativeWidth.getValue ());
00098     float epsilon = 0.0001;
00099 
00100     SbVec3f eFront1 = front1.getValue () + delta * (front3.getValue () - front1.getValue ());
00101     SbVec3f eFront2 = front2.getValue () + delta * (front4.getValue () - front2.getValue ());
00102     SbVec3f eFront3 = front3.getValue () + delta * (front1.getValue () - front3.getValue ());
00103     SbVec3f eFront4 = front4.getValue () + delta * (front2.getValue () - front4.getValue ());
00104         
00105     SbVec3f eBack1  = back1.getValue ()  + delta * (back3.getValue ()  - back1.getValue ());
00106     SbVec3f eBack2  = back2.getValue ()  + delta * (back4.getValue ()  - back2.getValue ());
00107     SbVec3f eBack3  = back3.getValue ()  + delta * (back1.getValue ()  - back3.getValue ());
00108     SbVec3f eBack4  = back4.getValue ()  + delta * (back2.getValue ()  - back4.getValue ());
00109 
00110     vertices [8]    = eBack1 + (eBack1 - eFront1) * epsilon;
00111     vertices [9]    = eBack2 + (eBack2 - eFront2) * epsilon;
00112     vertices [10]   = eBack3 + (eBack3 - eFront3) * epsilon;
00113     vertices [11]   = eBack4 + (eBack4 - eFront4) * epsilon;
00114     vertices [12]   = vertices [8]  + (eBack1 - eFront1) * eScale;
00115     vertices [13]   = vertices [9]  + (eBack2 - eFront2) * eScale;
00116     vertices [14]   = vertices [10] + (eBack3 - eFront3) * eScale;
00117     vertices [15]   = vertices [11] + (eBack4 - eFront4) * eScale;
00118 
00119     // Order vertices in each face so that right-handed normal points
00120     // outwards.  Order indices for composite (crystal+bump) face set.
00121     // 11 faces = (2 trapezoids * 6 faces) - (1 hidden one where they
00122     // join)
00123 
00124     static const int indices [11*5]  = {
00125         3,     2,    1,    0,   SO_END_FACE_INDEX,
00126         4,     5,    6,    7,   SO_END_FACE_INDEX,
00127         5,     1,    2,    6,   SO_END_FACE_INDEX,
00128         2,     3,    7,    6,   SO_END_FACE_INDEX,
00129         7,     3,    0,    4,   SO_END_FACE_INDEX,
00130         1,     5,    4,    0,   SO_END_FACE_INDEX, // end of crystal vertices: 6*5
00131         12,   13,   14,   15,   SO_END_FACE_INDEX,
00132         9,    10,   14,   13,   SO_END_FACE_INDEX,
00133         10,   11,   15,   14,   SO_END_FACE_INDEX,
00134         11,    8,   12,   15,   SO_END_FACE_INDEX,
00135         8,     9,   13,   12,   SO_END_FACE_INDEX
00136     };
00137                                  
00138     // Decide whether hit should be drawn based on user's desire and energy threshold
00139     SbBool drawHitPart
00140         = (drawHit.getValue ()
00141            && energy.getValue () >= minimumEnergy.getValue ());
00142 
00143     // Draw crystal and/or hit if requested
00144     SoVertexProperty *vtx = new SoVertexProperty;
00145     if (drawCrystal.getValue ())
00146     {
00147         if (drawHitPart)
00148         {
00149             // draw crystal and hit
00150             vtx->vertex.setValues (0, 16, vertices); 
00151             faces->coordIndex.setValues (0, 11*5, indices);
00152         }
00153         else
00154         {
00155             // draw only crystal
00156             vtx->vertex.setValues (0, 8, vertices); 
00157             faces->coordIndex.setValues (0, 6*5, indices);
00158         }
00159     }
00160     else if (drawHitPart)
00161     {
00162         // draw only hit
00163         vtx->vertex.setValues (0, 8, &vertices[8]); 
00164         faces->coordIndex.setValues (0, 6*5, indices);
00165     }
00166 
00167     faces->vertexProperty = vtx;
00168 
00169     // set parts
00170     setPart ("shapeHints",      shapeHints);
00171     setPart ("faces",           faces);
00172 
00173     if (showLines.getValue () == true)
00174     {
00175         SoIndexedLineSet *lines = new SoIndexedLineSet;
00176         if (drawCrystal.getValue ())
00177         {
00178             if (drawHitPart)
00179             {
00180                 lines->coordIndex.setValues (0, 11*5, indices);
00181             }
00182             else
00183             {
00184                 lines->coordIndex.setValues (0, 6*5, indices);
00185             }
00186         }
00187         else if (drawHitPart)
00188         {
00189             lines->coordIndex.setValues (0, 6*5, indices);
00190         }
00191         lines->vertexProperty = vtx;
00192         setPart ("lines", lines);
00193     }
00194     else
00195     {
00196         setPart ("lines", NULL);
00197     }
00198 }

Generated on Tue Jun 9 17:38:45 2009 for CMSSW by  doxygen 1.5.4