00001
00003
00004
00005
00006
00007
00008
00010
00011 #include "VP1GuideLineSystems/VP1Floor.h"
00012 #include "CLHEP/Units/SystemOfUnits.h"
00013 #include <Inventor/nodes/SoSeparator.h>
00014 #include <Inventor/nodes/SoVertexProperty.h>
00015 #include <Inventor/nodes/SoLineSet.h>
00016 #include <Inventor/SbColor4f.h>
00017
00018
00019 bool VP1Floor::calcParsFromExtentAndSpacing( VP1HelperClassBase*helper,const double& extent, const double& spacing, const int& nmaxlimit, int& nmax, double& distmax )
00020 {
00021 if (extent<0.0||spacing<=0.0)
00022 return false;
00023 nmax=static_cast<int>(extent/spacing+0.5);
00024 if (nmax<1)
00025 nmax =1;
00026 if (nmax>nmaxlimit) {
00027 helper->message("Too many lines requested. All will not be shown.");
00028 nmax=nmaxlimit;
00029 }
00030 distmax = nmax*spacing;
00031 return true;
00032 }
00033
00034
00035 class VP1Floor::Imp {
00036 public:
00037 Imp(VP1Floor *,
00038 SoSeparator * attachsep);
00039 VP1Floor * theclass;
00040 SoSeparator * attachSep;
00041
00042 bool shown;
00043 SbColor4f colourAndTransp;
00044 double extent;
00045 double spacing;
00046 double vertpos;
00047
00048 SoSeparator * sep;
00049
00050 void rebuild3DObjects();
00051 void updateColour();
00052 };
00053
00054
00055 VP1Floor::VP1Floor(SoSeparator * attachsep,
00056 IVP1System * sys,QObject * parent)
00057 : QObject(parent), VP1HelperClassBase(sys,"VP1Floor"), d(new Imp(this,attachsep))
00058 {
00059 }
00060
00061
00062 VP1Floor::~VP1Floor()
00063 {
00064 setShown(false);
00065 if (d->sep)
00066 d->sep->unref();
00067 d->attachSep->unref();
00068 delete d;
00069 }
00070
00071
00072 VP1Floor::Imp::Imp(VP1Floor *tc,SoSeparator * as)
00073 : theclass(tc), attachSep(as), shown(false),
00074 colourAndTransp(SbColor4f(1,1,1,1)),extent(10), spacing(1), vertpos(0), sep(0)
00075 {
00076 attachSep->ref();
00077 }
00078
00079
00080 void VP1Floor::Imp::rebuild3DObjects()
00081 {
00082 theclass->messageVerbose("(Re)building 3D objects");
00083
00084 if (sep) {
00085 sep->removeAllChildren();
00086 } else {
00087 sep = new SoSeparator;
00088 sep->ref();
00089 }
00090
00091 const bool save = sep->enableNotify(false);
00092
00093 int nmax; double distmax;
00094 if (!calcParsFromExtentAndSpacing( theclass, extent, spacing, VP1Floor::nMax(), nmax, distmax )) {
00095 nmax = 10;
00096 distmax = 10*m;
00097 theclass->message("ERROR: Problems calculating nmax/distmax.");
00098 }
00099
00100 SoVertexProperty * floor_vertices = new SoVertexProperty();
00101 int ivert(0);
00102 int nsublines(0);
00103 for (int ix = -nmax; ix<=nmax; ++ix) {
00104 double x = ix*spacing;
00105 floor_vertices->vertex.set1Value(ivert++,x,vertpos,-distmax);
00106 floor_vertices->vertex.set1Value(ivert++,x,vertpos,+distmax);
00107 ++nsublines;
00108 }
00109 for (int iz = -nmax; iz<=nmax; ++iz) {
00110 double z = iz*spacing;
00111 floor_vertices->vertex.set1Value(ivert++,-distmax,vertpos,z);
00112 floor_vertices->vertex.set1Value(ivert++,+distmax,vertpos,z);
00113 ++nsublines;
00114 }
00115
00116 floor_vertices->materialBinding=SoMaterialBinding::OVERALL;
00117 floor_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 = floor_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 VP1Floor::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 VP1Floor::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 VP1Floor::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 VP1Floor::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 VP1Floor::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 }
00202
00203
00204 void VP1Floor::setVerticalPosition(const double&p)
00205 {
00206 messageVerbose("Signal received: setVerticalPosition("+str(p)+")");
00207 if (d->vertpos==p)
00208 return;
00209 d->vertpos=p;
00210 if (d->shown)
00211 d->rebuild3DObjects();
00212 }