32 #ifdef G4VIS_BUILD_OI_DRIVER
37 #include <Inventor/SoPath.h>
38 #include <Inventor/SoNodeKitPath.h>
39 #include <Inventor/nodes/SoCoordinate3.h>
40 #include <Inventor/nodes/SoCoordinate4.h>
41 #include <Inventor/nodes/SoSeparator.h>
42 #include <Inventor/nodes/SoDrawStyle.h>
43 #include <Inventor/nodes/SoLightModel.h>
44 #include <Inventor/nodes/SoMaterial.h>
45 #include <Inventor/nodes/SoLineSet.h>
46 #include <Inventor/nodes/SoCube.h>
47 #include <Inventor/nodes/SoFont.h>
48 #include <Inventor/nodes/SoText2.h>
49 #include <Inventor/nodes/SoFaceSet.h>
50 #include <Inventor/nodes/SoNormal.h>
51 #include <Inventor/nodes/SoNormalBinding.h>
52 #include <Inventor/nodes/SoComplexity.h>
53 #include <Inventor/nodes/SoNurbsSurface.h>
54 #include <Inventor/nodes/SoTranslation.h>
55 #include <Inventor/nodes/SoTransform.h>
56 #include <Inventor/nodes/SoResetTransform.h>
57 #include <Inventor/nodes/SoMatrixTransform.h>
59 #define USE_SOPOLYHEDRON
61 #ifndef USE_SOPOLYHEDRON
102 G4int G4OpenInventorSceneHandler::fSceneIdCount = 0;
104 G4OpenInventorSceneHandler::G4OpenInventorSceneHandler (
G4OpenInventor& system,
110 ,fCurrentSeparator(0)
111 ,fModelingSolid(false)
112 ,fReducedWireFrame(true)
114 ,fPreviewAndFull(true)
119 fRoot =
new SoSeparator;
121 fRoot->setName(
"Root");
123 fDetectorRoot =
new SoSeparator;
124 fDetectorRoot->setName(
"StaticRoot");
125 fRoot->addChild(fDetectorRoot);
127 fTransientRoot =
new SoSeparator;
128 fTransientRoot->setName(
"TransientRoot");
129 fRoot->addChild(fTransientRoot);
131 fCurrentSeparator = fTransientRoot;
134 G4OpenInventorSceneHandler::~G4OpenInventorSceneHandler ()
137 fStyleCache->unref();
140 void G4OpenInventorSceneHandler::ClearStore ()
142 fDetectorRoot->removeAllChildren();
143 fSeparatorMap.clear();
145 fTransientRoot->removeAllChildren();
148 void G4OpenInventorSceneHandler::ClearTransientStore ()
150 fTransientRoot->removeAllChildren();
156 void G4OpenInventorSceneHandler::PreAddSolid
163 GeneratePrerequisites();
169 void G4OpenInventorSceneHandler::BeginPrimitives
176 if (!fProcessingSolid) {
177 GeneratePrerequisites();
184 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polyline& line)
187 static G4bool warned =
false;
191 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polyline&)",
193 "2D polylines not implemented. Ignored.");
205 G4int nPoints = line.size();
206 SbVec3f* pCoords =
new SbVec3f[nPoints];
208 for (
G4int iPoint = 0; iPoint < nPoints ; iPoint++) {
209 pCoords[iPoint].setValue((
float)line[iPoint].
x(),
210 (
float)line[iPoint].
y(),
211 (
float)line[iPoint].
z());
217 SoCoordinate3 *polyCoords =
new SoCoordinate3;
218 polyCoords->point.setValues(0,nPoints,pCoords);
219 fCurrentSeparator->addChild(polyCoords);
224 SoDrawStyle* drawStyle = fStyleCache->getLineStyle();
225 fCurrentSeparator->addChild(drawStyle);
230 if (fpViewer->GetViewParameters().IsPicking()) LoadAtts(line, pLine);
233 pLine->numVertices.setValues(0,1,(
const long *)&nPoints);
235 pLine->numVertices.setValues(0,1,&nPoints);
238 fCurrentSeparator->addChild(pLine);
243 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polymarker& polymarker)
246 static G4bool warned =
false;
250 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polymarker&)",
252 "2D polymarkers not implemented. Ignored.");
264 G4int pointn = polymarker.size();
265 if(pointn<=0)
return;
267 SbVec3f* points =
new SbVec3f[pointn];
268 for (
G4int iPoint = 0; iPoint < pointn ; iPoint++) {
269 points[iPoint].setValue((
float)polymarker[iPoint].
x(),
270 (
float)polymarker[iPoint].
y(),
271 (
float)polymarker[iPoint].
z());
274 SoCoordinate3* coordinate3 =
new SoCoordinate3;
275 coordinate3->point.setValues(0,pointn,points);
276 fCurrentSeparator->addChild(coordinate3);
278 MarkerSizeType sizeType;
279 G4double screenSize = GetMarkerSize (polymarker, sizeType);
292 markerSet->numPoints = pointn;
295 if (fpViewer->GetViewParameters().IsPicking())
296 LoadAtts(polymarker, markerSet);
303 if (screenSize <= 5.) {
304 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
305 }
else if (screenSize <= 7.) {
306 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
308 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
312 if (screenSize <= 5.) {
314 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
316 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_5_5;
318 }
else if (screenSize <= 7.) {
320 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
322 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_7_7;
326 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
328 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_9_9;
333 if (screenSize <= 5.) {
335 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_5_5;
337 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_5_5;
339 }
else if (screenSize <= 7.) {
341 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_7_7;
343 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_7_7;
347 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_9_9;
349 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_9_9;
353 fCurrentSeparator->addChild(markerSet);
360 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Text& text)
363 static G4bool warned =
false;
367 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Text&)",
369 "2D text not implemented. Ignored.");
381 const G4Colour&
c = GetTextColour (text);
383 fStyleCache->getMaterial((
float)c.GetRed(),
386 (float)(1-c.GetAlpha()));
387 fCurrentSeparator->addChild(material);
389 MarkerSizeType sizeType;
390 G4double size = GetMarkerSize (text, sizeType);
405 SoFont *g4Font =
new SoFont();
407 fCurrentSeparator->addChild(g4Font);
412 SoText2 *g4String =
new SoText2();
413 g4String->string.setValue(text.
GetText());
414 g4String->spacing = 2.0;
420 g4String->justification = SoText2::CENTER;
break;
424 fCurrentSeparator->addChild(g4String);
430 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Circle& circle) {
431 AddCircleSquare(G4OICircle, circle);
437 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Square& square) {
438 AddCircleSquare(G4OISquare, square);
441 void G4OpenInventorSceneHandler::AddCircleSquare
442 (G4OIMarker markerType,
const G4VMarker& marker)
445 static G4bool warned =
false;
449 (
"G4OpenInventorSceneHandler::AddCircleSquare",
451 "2D circles and squares not implemented. Ignored.");
463 MarkerSizeType sizeType;
464 G4double screenSize = GetMarkerSize (marker, sizeType);
479 SbVec3f* points =
new SbVec3f[1];
480 points[0].setValue((
float)centre.
x(),
483 SoCoordinate3* coordinate3 =
new SoCoordinate3;
484 coordinate3->point.setValues(0,1,points);
485 fCurrentSeparator->addChild(coordinate3);
488 markerSet->numPoints = 1;
491 if (fpViewer->GetViewParameters().IsPicking()) LoadAtts(marker, markerSet);
494 switch (markerType) {
496 if (screenSize <= 5.) {
498 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
500 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_5_5;
502 }
else if (screenSize <= 7.) {
504 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
506 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_7_7;
510 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
512 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_9_9;
517 if (screenSize <= 5.) {
519 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_5_5;
521 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_5_5;
523 }
else if (screenSize <= 7.) {
525 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_7_7;
527 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_7_7;
531 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_9_9;
533 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_9_9;
538 fCurrentSeparator->addChild(markerSet);
546 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polyhedron& polyhedron)
551 static G4bool warned =
false;
555 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polyhedron&)",
557 "2D polyhedra not implemented. Ignored.");
572 if (fpViewer->GetViewParameters().IsPicking())
573 LoadAtts(polyhedron, soPolyhedron);
575 SbString name =
"Non-geometry";
582 soPolyhedron->setName(sbName);
583 soPolyhedron->
solid.setValue(fModelingSolid);
585 fCurrentSeparator->addChild(soPolyhedron);
592 void G4OpenInventorSceneHandler::AddPrimitive (
const G4NURBS& nurb) {
595 static G4bool warned =
false;
599 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4NURBS&)",
601 "2D NURBS not implemented. Ignored.");
613 G4float *u_knot_array, *u_knot_array_ptr;
616 while (u_iterator.pick (u_knot_array_ptr++)){}
618 G4float *v_knot_array, *v_knot_array_ptr;
621 while (v_iterator.pick (v_knot_array_ptr++)){}
623 G4float *ctrl_pnt_array, *ctrl_pnt_array_ptr;
624 ctrl_pnt_array = ctrl_pnt_array_ptr =
627 while (c_p_iterator.pick (ctrl_pnt_array_ptr++)){}
629 SoSeparator *surfSep =
new SoSeparator();
634 SoComplexity *complexity =
new SoComplexity;
635 SoCoordinate4 *ctlPts =
new SoCoordinate4;
636 SoNurbsSurface *oi_nurb =
new SoNurbsSurface;
638 complexity->value = (float)0.6;
640 SbVec4f* points =
new SbVec4f[nPoints];
641 for (
G4int iPoint = 0; iPoint < nPoints ; iPoint++) {
642 points[iPoint].setValue(
643 ctrl_pnt_array[iPoint*4 + 0],
644 ctrl_pnt_array[iPoint*4 + 1],
645 ctrl_pnt_array[iPoint*4 + 2],
646 ctrl_pnt_array[iPoint*4 + 3]);
648 ctlPts->point.setValues (0,nPoints,points);
654 surfSep->addChild(complexity);
655 surfSep->addChild(ctlPts);
656 surfSep->addChild(oi_nurb);
658 fCurrentSeparator->addChild(surfSep);
663 delete [] u_knot_array;
664 delete [] v_knot_array;
665 delete [] ctrl_pnt_array;
669 void G4OpenInventorSceneHandler::GeneratePrerequisites()
700 typedef std::vector<PVNodeID>
PVPath;
718 PVPath::const_reverse_iterator ri;
720 ri = ++drawnPVPath.rbegin();
721 if (ri != drawnPVPath.rend()) {
723 MotherVolume = ri->GetPhysicalVolume()->GetLogicalVolume();
733 SoSeparator* previewSeparator =
734 (SoSeparator*) detectorTreeKit->getPart(
"previewSeparator",
TRUE);
735 previewSeparator->renderCaching = SoSeparator::OFF;
737 SoSeparator* fullSeparator =
738 (SoSeparator*) detectorTreeKit->getPart(
"fullSeparator",
TRUE);
739 fullSeparator->renderCaching = SoSeparator::OFF;
748 fpViewer->GetApplicableVisAttributes (fpVisAttribs);
753 const double green = g4Col.
GetGreen ();
755 double transparency = 1 - g4Col.
GetAlpha();
759 GetDrawingStyle(pApplicableVisAttribs);
760 switch (drawing_style) {
762 fModelingSolid =
false;
767 fModelingSolid =
true;
771 SoMaterial* material =
772 fStyleCache->getMaterial((
float)red,
775 (
float)transparency);
776 detectorTreeKit->setPart(
"appearance.material",material);
778 SoLightModel* lightModel =
779 fModelingSolid ? fStyleCache->getLightModelPhong() :
780 fStyleCache->getLightModelBaseColor();
781 detectorTreeKit->setPart(
"appearance.lightModel",lightModel);
785 fSeparatorMap[pCurrentLV] = fullSeparator;
790 if (fSeparatorMap.find(MotherVolume) != fSeparatorMap.end()) {
793 fSeparatorMap[MotherVolume]->addChild(detectorTreeKit);
800 "ERROR: G4OpenInventorSceneHandler::GeneratePrerequisites: Mother "
801 << ri->GetPhysicalVolume()->GetName()
802 <<
':' << ri->GetCopyNo()
803 <<
" not previously encountered."
804 "\nShouldn't happen! Please report to visualization coordinator."
809 fDetectorRoot->addChild(detectorTreeKit);
813 fDetectorRoot->addChild(detectorTreeKit);
816 fCurrentSeparator = previewSeparator;
822 if (fSeparatorMap.find(MotherVolume) != fSeparatorMap.end()) {
823 fCurrentSeparator = fSeparatorMap[MotherVolume];
829 G4cout <<
"ERROR: G4OpenInventorSceneHandler::PreAddSolid: Mother "
830 << ri->GetPhysicalVolume()->GetName()
831 <<
':' << ri->GetCopyNo()
832 <<
" not previously encountered."
833 "\nShouldn't happen! Please report to visualization coordinator."
836 fCurrentSeparator = fDetectorRoot;
839 fCurrentSeparator = fDetectorRoot;
846 if (fReadyForTransients) {
847 fCurrentSeparator = fTransientRoot;
849 fCurrentSeparator = fDetectorRoot;
854 void G4OpenInventorSceneHandler::AddProperties(
const G4VisAttributes* visAtts)
858 fpViewer->GetApplicableVisAttributes (visAtts);
862 const double red = g4Col.
GetRed ();
863 const double green = g4Col.
GetGreen ();
864 const double blue = g4Col.
GetBlue ();
865 double transparency = 1 - g4Col.
GetAlpha();
869 GetDrawingStyle(pApplicableVisAttribs);
870 switch (drawing_style) {
872 fModelingSolid =
false;
877 fModelingSolid =
true;
882 G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pApplicableVisAttribs);
883 fReducedWireFrame = !isAuxEdgeVisible;
885 SoMaterial* material =
886 fStyleCache->getMaterial((
float)red,
889 (
float)transparency);
890 fCurrentSeparator->addChild(material);
892 SoLightModel* lightModel =
893 fModelingSolid ? fStyleCache->getLightModelPhong() :
894 fStyleCache->getLightModelBaseColor();
895 fCurrentSeparator->addChild(lightModel);
898 void G4OpenInventorSceneHandler::AddTransform(
const G4Point3D& translation)
902 fCurrentSeparator->addChild(fStyleCache->getResetTransform());
904 SoMatrixTransform* matrixTransform =
new SoMatrixTransform;
905 G4OpenInventorTransform3D oiTran
907 SbMatrix* sbMatrix = oiTran.GetSbMatrix();
909 const G4Vector3D scale = fpViewer->GetViewParameters().GetScaleFactor();
912 (SbVec3f((
float)scale.
x(),(float)scale.
y(),(float)scale.
z()));
913 sbMatrix->multRight(sbScale);
915 matrixTransform->matrix.setValue(*sbMatrix);
917 fCurrentSeparator->addChild(matrixTransform);