00001
00003
00004
00005
00006
00007
00008
00010
00011 #include "VP1GuideLineSystems/VP1CartesianGrid.h"
00012 #include "VP1GuideLineSystems/VP1Floor.h"
00013 #include "CLHEP/Units/SystemOfUnits.h"
00014 #include <Inventor/nodes/SoSeparator.h>
00015 #include <Inventor/nodes/SoVertexProperty.h>
00016 #include <Inventor/nodes/SoLineSet.h>
00017 #include <Inventor/SbColor4f.h>
00018
00019
00020 class VP1CartesianGrid::Imp {
00021 public:
00022 Imp(VP1CartesianGrid *,
00023 SoSeparator * attachsep);
00024 VP1CartesianGrid * theclass;
00025 SoSeparator * attachSep;
00026
00027 bool shown;
00028 SbColor4f colourAndTransp;
00029 double extent;
00030 double spacing;
00031
00032 SoSeparator * sep;
00033
00034 void rebuild3DObjects();
00035 void updateColour();
00036 };
00037
00038
00039 VP1CartesianGrid::VP1CartesianGrid(SoSeparator * attachsep,
00040 IVP1System * sys,QObject * parent)
00041 : QObject(parent), VP1HelperClassBase(sys,"VP1CartesianGrid"), d(new Imp(this,attachsep))
00042 {
00043 }
00044
00045
00046 VP1CartesianGrid::~VP1CartesianGrid()
00047 {
00048 setShown(false);
00049 if (d->sep)
00050 d->sep->unref();
00051 d->attachSep->unref();
00052 delete d;
00053 }
00054
00055
00056 VP1CartesianGrid::Imp::Imp(VP1CartesianGrid *tc,SoSeparator * as)
00057 : theclass(tc), attachSep(as), shown(false),
00058 colourAndTransp(SbColor4f(1,1,1,1)),extent(10), spacing(1), sep(0)
00059 {
00060 attachSep->ref();
00061 }
00062
00063
00064 void VP1CartesianGrid::Imp::rebuild3DObjects()
00065 {
00066 theclass->messageVerbose("(Re)building 3D objects");
00067
00068 if (sep) {
00069 sep->removeAllChildren();
00070 } else {
00071 sep = new SoSeparator;
00072 sep->ref();
00073 }
00074
00075 const bool save = sep->enableNotify(false);
00076
00077 int nmax; double distmax;
00078 if (!VP1Floor::calcParsFromExtentAndSpacing( theclass, extent, spacing, 40, nmax, distmax )) {
00079 nmax = 10;
00080 distmax = 10*m;
00081 theclass->message("ERROR: Problems calculating nmax/distmax.");
00082 }
00083
00084 SoVertexProperty * grid_cartesian_vertices = new SoVertexProperty();
00085
00086 int ivert(0);
00087 int nsublines(0);
00088
00089
00090 for (int ix = -nmax; ix<=nmax; ++ix)
00091 for (int iz = -nmax; iz<=nmax; ++iz) {
00092 double x = ix*spacing;
00093 double z = iz*spacing;
00094 grid_cartesian_vertices->vertex.set1Value(ivert++,x,-distmax,z);
00095 grid_cartesian_vertices->vertex.set1Value(ivert++,x,+distmax,z);
00096 ++nsublines;
00097 }
00098
00099
00100 for (int iy = -nmax; iy<=nmax; ++iy) {
00101 double y = iy*spacing;
00102 for (int ix = -nmax; ix<=nmax; ++ix) {
00103 double x = ix*spacing;
00104 grid_cartesian_vertices->vertex.set1Value(ivert++,x,y,-distmax);
00105 grid_cartesian_vertices->vertex.set1Value(ivert++,x,y,+distmax);
00106 ++nsublines;
00107 }
00108 for (int iz = -nmax; iz<=nmax; ++iz) {
00109 double z = iz*spacing;
00110 grid_cartesian_vertices->vertex.set1Value(ivert++,-distmax,y,z);
00111 grid_cartesian_vertices->vertex.set1Value(ivert++,+distmax,y,z);
00112 ++nsublines;
00113 }
00114 }
00115
00116 grid_cartesian_vertices->materialBinding=SoMaterialBinding::OVERALL;
00117 grid_cartesian_vertices->normalBinding=SoNormalBinding::OVERALL;
00118 SoLineSet * line = new SoLineSet();
00119 line->numVertices.enableNotify(FALSE);
00120 line->numVertices.setNum(nsublines);
00121 for (int i=0;i<nsublines;++i)
00122 line->numVertices.set1Value(i,2);
00123 line->vertexProperty = grid_cartesian_vertices;
00124 line->numVertices.enableNotify(TRUE);
00125 line->numVertices.touch();
00126
00127 sep->addChild(line);
00128 updateColour();
00129
00130 if (save) {
00131 sep->enableNotify(true);
00132 sep->touch();
00133 }
00134
00135 }
00136
00137
00138 void VP1CartesianGrid::Imp::updateColour()
00139 {
00140 theclass->messageVerbose("Updating packed colour");
00141 if (!sep||sep->getNumChildren()<1)
00142 return;
00143 SoNode * n = sep->getChild(0);
00144 if (!n||n->getTypeId()!=SoLineSet::getClassTypeId())
00145 return;
00146 SoLineSet * line = static_cast<SoLineSet*>(n);
00147 SoVertexProperty * vertices = static_cast<SoVertexProperty *>(line->vertexProperty.getValue());
00148 if (!vertices)
00149 return;
00150 vertices->orderedRGBA = colourAndTransp.getPackedValue();
00151 }
00152
00153
00154 void VP1CartesianGrid::setShown(bool b)
00155 {
00156 messageVerbose("Signal received: setShown("+str(b)+")");
00157 if (d->shown==b)
00158 return;
00159 d->shown=b;
00160 if (d->shown) {
00161 d->rebuild3DObjects();
00162 if (d->attachSep->findChild(d->sep)<0)
00163 d->attachSep->addChild(d->sep);
00164 } else {
00165 if (d->sep&&d->attachSep->findChild(d->sep)>=0)
00166 d->attachSep->removeChild(d->sep);
00167 }
00168 }
00169
00170
00171 void VP1CartesianGrid::setColourAndTransp(const SbColor4f&ct)
00172 {
00173 messageVerbose("Signal received in setColourAndTransp slot.");
00174 if (d->colourAndTransp==ct)
00175 return;
00176 d->colourAndTransp=ct;
00177 if (d->shown)
00178 d->updateColour();
00179 }
00180
00181
00182 void VP1CartesianGrid::setExtent(const double& e)
00183 {
00184 messageVerbose("Signal received: setExtent("+str(e)+")");
00185 if (d->extent==e)
00186 return;
00187 d->extent=e;
00188 if (d->shown)
00189 d->rebuild3DObjects();
00190 }
00191
00192
00193 void VP1CartesianGrid::setSpacing(const double&s)
00194 {
00195 messageVerbose("Signal received: setSpacing("+str(s)+")");
00196 if (d->spacing==s)
00197 return;
00198 d->spacing=s;
00199 if (d->shown)
00200 d->rebuild3DObjects();
00201 }