CMS 3D CMS Logo

IgSoGrid.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Inventor/interface/IgSoGrid.h"
00004 #include <Inventor/nodes/SoText2.h>
00005 #include <Inventor/nodes/SoFont.h>
00006 #include <Inventor/nodes/SoIndexedLineSet.h>
00007 #include <Inventor/nodes/SoSeparator.h>
00008 #include <Inventor/nodes/SoTranslation.h>
00009 
00010 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00011 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00012 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00013 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00014 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00015 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00016 
00017 SO_KIT_SOURCE (IgSoGrid);
00018 
00019 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00020 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00021 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00022 
00023 void
00024 IgSoGrid::initClass (void)
00025 { SO_KIT_INIT_CLASS (IgSoGrid, IgSoShapeKit, "IgSoShapeKit"); }
00026 
00027 
00028 IgSoGrid::IgSoGrid (void)
00029 {    
00030     SO_KIT_CONSTRUCTOR (IgSoGrid);
00031     SO_KIT_ADD_FIELD (maxX,             (5.0f));
00032     SO_KIT_ADD_FIELD (maxZ,             (5.0f));
00033 
00034     SO_KIT_ADD_CATALOG_ENTRY (xLabels,          SoSeparator,    FALSE, separator,\x0, TRUE);
00035     SO_KIT_ADD_CATALOG_ENTRY (zLabels,          SoSeparator,    FALSE, separator,\x0, TRUE);
00036     SO_KIT_ADD_CATALOG_ENTRY (grid,             SoIndexedLineSet, FALSE, separator,\x0, TRUE);
00037     SO_KIT_ADD_CATALOG_ENTRY (font,             SoFont,         FALSE, separator,\x0, TRUE);
00038     SO_KIT_ADD_CATALOG_ENTRY (labels,           SoSeparator,    FALSE, separator,\x0, TRUE);
00039 
00040     SO_KIT_INIT_INSTANCE ();
00041     setUpConnections (true, true);
00042 }
00043 
00044 void
00045 IgSoGrid::refresh (void)
00046 {
00047     SoSeparator         *xLabels        = new SoSeparator;
00048     SoSeparator         *zLabels        = new SoSeparator;
00049     SoIndexedLineSet    *grid           = new SoIndexedLineSet;
00050     SoFont              *font           = new SoFont;
00051 
00052     SoVertexProperty    *vtx            = new SoVertexProperty;
00053     SoText2             *text;
00054     SoMFInt32           coords;
00055 
00056     float               xMax = maxZ.getValue ();
00057     float               zMax = maxX.getValue ();
00058     
00059     int                 vertex = 5;
00060     int                 coord = 0;
00061 
00062     vtx->vertex.set1Value (0, SbVec3f (-zMax, 0, -xMax));
00063     vtx->vertex.set1Value (1, SbVec3f (-zMax, 0,  xMax));
00064     vtx->vertex.set1Value (2, SbVec3f ( zMax, 0,  xMax));
00065     vtx->vertex.set1Value (3, SbVec3f ( zMax, 0, -xMax));
00066     vtx->vertex.set1Value (4, SbVec3f (-zMax, 0, -xMax));
00067 
00068     for (; coord < vertex; coord++)
00069         coords.set1Value (coord, coord);
00070     coords.set1Value (coord++, SO_END_LINE_INDEX);
00071         
00072     //  set up co-ords for grid lines at unit intervals in x and z
00073     float x = -zMax;
00074     float z = ceil (-xMax);
00075 
00076     SoTranslation       *xStart = new SoTranslation;
00077     SoTranslation       *xTranslation = new SoTranslation;
00078     int                 tick;
00079     
00080     int ticks = int (2 * abs (int (x)) + 1);
00081 
00082     xStart->translation = SbVec3f (x, 0, -z);
00083     xTranslation->translation = SbVec3f (1.0, 0.0, 0.0);
00084     xLabels->addChild (xStart);
00085 
00086     for (tick = 0; tick < ticks; tick++, x++)
00087     {
00088         vtx->vertex.set1Value (vertex, SbVec3f (x, 0, -z));
00089         coords.set1Value (coord++, vertex++);
00090         vtx->vertex.set1Value (vertex, SbVec3f (x, 0, z));
00091         coords.set1Value (coord++, vertex++);
00092         coords.set1Value (coord++, SO_END_LINE_INDEX);
00093 
00094         text = new SoText2;
00095         text->justification = SoText2::CENTER;
00096         char textLabel [10];
00097         sprintf (textLabel, "%.0f", x);
00098         text->string = textLabel;
00099         xLabels->addChild (text);
00100         xLabels->addChild (xTranslation);
00101     }
00102 
00103     // now x
00104     x = -zMax;
00105     z = ceil (-xMax);
00106     if (z == -xMax)
00107         z += 1; // remove those end regions
00108 
00109     SoTranslation       *yStart = new SoTranslation;
00110     SoTranslation       *yTranslation = new SoTranslation;
00111 
00112     yStart->translation = SbVec3f (-x, 0, z);
00113     yTranslation->translation = SbVec3f (0, 0, 1);
00114     zLabels->addChild (yStart);
00115 
00116     ticks = int (2 * abs (int (z)) + 1);
00117     for (tick = 0; tick < ticks; tick++, z++)
00118     {
00119         vtx->vertex.set1Value (vertex, SbVec3f (-x, 0, z));
00120         coords.set1Value (coord++, vertex++);
00121         vtx->vertex.set1Value (vertex, SbVec3f (x, 0, z));
00122         coords.set1Value (coord++, vertex++);
00123         coords.set1Value (coord++, SO_END_LINE_INDEX);
00124 
00125         text = new SoText2;
00126         text->justification = SoText2::CENTER;
00127         char textLabel [10];
00128         sprintf (textLabel, "%.0f", z);
00129         text->string = textLabel;
00130         zLabels->addChild (text);
00131         zLabels->addChild (yTranslation);
00132     }
00133 
00134     grid->vertexProperty = vtx;
00135     grid->coordIndex = coords;
00136         
00137     // set parts
00138     setPart ("xLabels",         xLabels);
00139     setPart ("zLabels",         zLabels);
00140     setPart ("grid",            grid);
00141     setPart ("font",            font);
00142 }
00143 

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