31 #ifdef G4VIS_BUILD_OI_DRIVER
36 #include <Inventor/SoPath.h>
37 #include <Inventor/SoNodeKitPath.h>
38 #include <Inventor/nodes/SoCoordinate3.h>
39 #include <Inventor/nodes/SoCoordinate4.h>
40 #include <Inventor/nodes/SoSeparator.h>
41 #include <Inventor/nodes/SoDrawStyle.h>
42 #include <Inventor/nodes/SoLightModel.h>
43 #include <Inventor/nodes/SoMaterial.h>
44 #include <Inventor/nodes/SoLineSet.h>
45 #include <Inventor/nodes/SoCube.h>
46 #include <Inventor/nodes/SoFont.h>
47 #include <Inventor/nodes/SoText2.h>
48 #include <Inventor/nodes/SoFaceSet.h>
49 #include <Inventor/nodes/SoNormal.h>
50 #include <Inventor/nodes/SoNormalBinding.h>
51 #include <Inventor/nodes/SoComplexity.h>
52 #include <Inventor/nodes/SoTranslation.h>
53 #include <Inventor/nodes/SoTransform.h>
54 #include <Inventor/nodes/SoResetTransform.h>
55 #include <Inventor/nodes/SoMatrixTransform.h>
57 #define USE_SOPOLYHEDRON
59 #ifndef USE_SOPOLYHEDRON
99 G4int G4OpenInventorSceneHandler::fSceneIdCount = 0;
107 ,fCurrentSeparator(0)
108 ,fModelingSolid(
false)
109 ,fReducedWireFrame(
true)
111 ,fPreviewAndFull(
true)
116 fRoot =
new SoSeparator;
118 fRoot->setName(
"Root");
120 fDetectorRoot =
new SoSeparator;
121 fDetectorRoot->setName(
"StaticRoot");
122 fRoot->addChild(fDetectorRoot);
124 fTransientRoot =
new SoSeparator;
125 fTransientRoot->setName(
"TransientRoot");
126 fRoot->addChild(fTransientRoot);
128 fCurrentSeparator = fTransientRoot;
131 G4OpenInventorSceneHandler::~G4OpenInventorSceneHandler ()
134 fStyleCache->unref();
137 void G4OpenInventorSceneHandler::ClearStore ()
139 fDetectorRoot->removeAllChildren();
140 fSeparatorMap.clear();
142 fTransientRoot->removeAllChildren();
145 void G4OpenInventorSceneHandler::ClearTransientStore ()
147 fTransientRoot->removeAllChildren();
153 void G4OpenInventorSceneHandler::PreAddSolid
160 GeneratePrerequisites();
166 void G4OpenInventorSceneHandler::BeginPrimitives
173 if (!fProcessingSolid) {
174 GeneratePrerequisites();
181 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polyline& line)
184 static G4bool warned =
false;
188 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polyline&)",
190 "2D polylines not implemented. Ignored.");
202 G4int nPoints = line.size();
203 SbVec3f* pCoords =
new SbVec3f[nPoints];
205 for (
G4int iPoint = 0; iPoint < nPoints ; iPoint++) {
206 pCoords[iPoint].setValue((
float)line[iPoint].
x(),
207 (
float)line[iPoint].
y(),
208 (
float)line[iPoint].
z());
214 SoCoordinate3 *polyCoords =
new SoCoordinate3;
215 polyCoords->point.setValues(0,nPoints,pCoords);
216 fCurrentSeparator->addChild(polyCoords);
221 SoDrawStyle* drawStyle = fStyleCache->getLineStyle();
222 fCurrentSeparator->addChild(drawStyle);
227 if (fpViewer->GetViewParameters().IsPicking()) LoadAtts(line, pLine);
230 pLine->numVertices.setValues(0,1,(
const long *)&nPoints);
232 pLine->numVertices.setValues(0,1,&nPoints);
235 fCurrentSeparator->addChild(pLine);
240 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polymarker& polymarker)
243 static G4bool warned =
false;
247 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polymarker&)",
249 "2D polymarkers not implemented. Ignored.");
261 G4int pointn = polymarker.size();
262 if(pointn<=0)
return;
264 SbVec3f* points =
new SbVec3f[pointn];
265 for (
G4int iPoint = 0; iPoint < pointn ; iPoint++) {
266 points[iPoint].setValue((
float)polymarker[iPoint].
x(),
267 (
float)polymarker[iPoint].
y(),
268 (
float)polymarker[iPoint].
z());
271 SoCoordinate3* coordinate3 =
new SoCoordinate3;
272 coordinate3->point.setValues(0,pointn,points);
273 fCurrentSeparator->addChild(coordinate3);
275 MarkerSizeType sizeType;
276 G4double screenSize = GetMarkerSize (polymarker, sizeType);
289 markerSet->numPoints = pointn;
292 if (fpViewer->GetViewParameters().IsPicking())
293 LoadAtts(polymarker, markerSet);
300 if (screenSize <= 5.) {
301 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
302 }
else if (screenSize <= 7.) {
303 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
305 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
309 if (screenSize <= 5.) {
311 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
313 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_5_5;
315 }
else if (screenSize <= 7.) {
317 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
319 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_7_7;
323 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
325 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_9_9;
330 if (screenSize <= 5.) {
332 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_5_5;
334 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_5_5;
336 }
else if (screenSize <= 7.) {
338 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_7_7;
340 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_7_7;
344 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_9_9;
346 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_9_9;
350 fCurrentSeparator->addChild(markerSet);
357 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Text& text)
360 static G4bool warned =
false;
364 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Text&)",
366 "2D text not implemented. Ignored.");
378 const G4Colour&
c = GetTextColour (text);
380 fStyleCache->getMaterial((
float)c.GetRed(),
383 (float)(1-c.GetAlpha()));
384 fCurrentSeparator->addChild(material);
386 MarkerSizeType sizeType;
387 G4double size = GetMarkerSize (text, sizeType);
402 SoFont *g4Font =
new SoFont();
404 fCurrentSeparator->addChild(g4Font);
409 SoText2 *g4String =
new SoText2();
410 g4String->string.setValue(text.
GetText());
411 g4String->spacing = 2.0;
415 g4String->justification = SoText2::LEFT;
break;
417 g4String->justification = SoText2::CENTER;
break;
419 g4String->justification = SoText2::RIGHT;
break;
421 fCurrentSeparator->addChild(g4String);
427 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Circle& circle) {
428 AddCircleSquare(G4OICircle, circle);
434 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Square&
square) {
435 AddCircleSquare(G4OISquare, square);
438 void G4OpenInventorSceneHandler::AddCircleSquare
439 (G4OIMarker markerType,
const G4VMarker& marker)
442 static G4bool warned =
false;
446 (
"G4OpenInventorSceneHandler::AddCircleSquare",
448 "2D circles and squares not implemented. Ignored.");
460 MarkerSizeType sizeType;
461 G4double screenSize = GetMarkerSize (marker, sizeType);
476 SbVec3f* points =
new SbVec3f[1];
477 points[0].setValue((
float)centre.
x(),
480 SoCoordinate3* coordinate3 =
new SoCoordinate3;
481 coordinate3->point.setValues(0,1,points);
482 fCurrentSeparator->addChild(coordinate3);
485 markerSet->numPoints = 1;
488 if (fpViewer->GetViewParameters().IsPicking()) LoadAtts(marker, markerSet);
491 switch (markerType) {
493 if (screenSize <= 5.) {
495 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_5_5;
497 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_5_5;
499 }
else if (screenSize <= 7.) {
501 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_7_7;
503 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_7_7;
507 markerSet->markerIndex = SoMarkerSet::CIRCLE_FILLED_9_9;
509 markerSet->markerIndex = SoMarkerSet::CIRCLE_LINE_9_9;
514 if (screenSize <= 5.) {
516 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_5_5;
518 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_5_5;
520 }
else if (screenSize <= 7.) {
522 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_7_7;
524 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_7_7;
528 markerSet->markerIndex = SoMarkerSet::SQUARE_FILLED_9_9;
530 markerSet->markerIndex = SoMarkerSet::SQUARE_LINE_9_9;
535 fCurrentSeparator->addChild(markerSet);
543 void G4OpenInventorSceneHandler::AddPrimitive (
const G4Polyhedron& polyhedron)
548 static G4bool warned =
false;
552 (
"G4OpenInventorSceneHandler::AddPrimitive (const G4Polyhedron&)",
554 "2D polyhedra not implemented. Ignored.");
569 if (fpViewer->GetViewParameters().IsPicking())
570 LoadAtts(polyhedron, soPolyhedron);
572 SbString name =
"Non-geometry";
579 soPolyhedron->setName(sbName);
580 soPolyhedron->
solid.setValue(fModelingSolid);
582 fCurrentSeparator->addChild(soPolyhedron);
585 void G4OpenInventorSceneHandler::GeneratePrerequisites()
616 typedef std::vector<PVNodeID>
PVPath;
634 PVPath::const_reverse_iterator ri;
636 ri = ++drawnPVPath.rbegin();
637 if (ri != drawnPVPath.rend()) {
639 MotherVolume = ri->GetPhysicalVolume()->GetLogicalVolume();
649 SoSeparator* previewSeparator =
650 (SoSeparator*) detectorTreeKit->getPart(
"previewSeparator",
TRUE);
653 SoSeparator* fullSeparator =
654 (SoSeparator*) detectorTreeKit->getPart(
"fullSeparator",
TRUE);
664 fpViewer->GetApplicableVisAttributes (fpVisAttribs);
668 const double red = g4Col.
GetRed ();
669 const double green = g4Col.
GetGreen ();
670 const double blue = g4Col.
GetBlue ();
671 double transparency = 1 - g4Col.
GetAlpha();
675 GetDrawingStyle(pApplicableVisAttribs);
676 switch (drawing_style) {
678 fModelingSolid =
false;
683 fModelingSolid =
true;
686 fModelingSolid =
false;
690 SoMaterial* material =
691 fStyleCache->getMaterial((
float)red,
694 (
float)transparency);
695 detectorTreeKit->setPart(
"appearance.material",material);
697 SoLightModel* lightModel =
698 fModelingSolid ? fStyleCache->getLightModelPhong() :
699 fStyleCache->getLightModelBaseColor();
700 detectorTreeKit->setPart(
"appearance.lightModel",lightModel);
704 fSeparatorMap[pCurrentLV] = fullSeparator;
709 if (fSeparatorMap.find(MotherVolume) != fSeparatorMap.end()) {
712 fSeparatorMap[MotherVolume]->addChild(detectorTreeKit);
719 "ERROR: G4OpenInventorSceneHandler::GeneratePrerequisites: Mother "
720 << ri->GetPhysicalVolume()->GetName()
721 <<
':' << ri->GetCopyNo()
722 <<
" not previously encountered."
723 "\nShouldn't happen! Please report to visualization coordinator."
728 fDetectorRoot->addChild(detectorTreeKit);
732 fDetectorRoot->addChild(detectorTreeKit);
735 fCurrentSeparator = previewSeparator;
741 if (fSeparatorMap.find(MotherVolume) != fSeparatorMap.end()) {
742 fCurrentSeparator = fSeparatorMap[MotherVolume];
748 G4cout <<
"ERROR: G4OpenInventorSceneHandler::PreAddSolid: Mother "
749 << ri->GetPhysicalVolume()->GetName()
750 <<
':' << ri->GetCopyNo()
751 <<
" not previously encountered."
752 "\nShouldn't happen! Please report to visualization coordinator."
755 fCurrentSeparator = fDetectorRoot;
758 fCurrentSeparator = fDetectorRoot;
765 if (fReadyForTransients) {
766 fCurrentSeparator = fTransientRoot;
768 fCurrentSeparator = fDetectorRoot;
773 void G4OpenInventorSceneHandler::AddProperties(
const G4VisAttributes* visAtts)
777 fpViewer->GetApplicableVisAttributes (visAtts);
781 const double red = g4Col.
GetRed ();
782 const double green = g4Col.
GetGreen ();
783 const double blue = g4Col.
GetBlue ();
784 double transparency = 1 - g4Col.
GetAlpha();
788 GetDrawingStyle(pApplicableVisAttribs);
789 switch (drawing_style) {
791 fModelingSolid =
false;
796 fModelingSolid =
true;
799 fModelingSolid =
false;
804 G4bool isAuxEdgeVisible = GetAuxEdgeVisible (pApplicableVisAttribs);
805 fReducedWireFrame = !isAuxEdgeVisible;
807 SoMaterial* material =
808 fStyleCache->getMaterial((
float)red,
811 (
float)transparency);
812 fCurrentSeparator->addChild(material);
814 SoLightModel* lightModel =
815 fModelingSolid ? fStyleCache->getLightModelPhong() :
816 fStyleCache->getLightModelBaseColor();
817 fCurrentSeparator->addChild(lightModel);
820 void G4OpenInventorSceneHandler::AddTransform(
const G4Point3D& translation)
824 fCurrentSeparator->addChild(fStyleCache->getResetTransform());
826 SoMatrixTransform* matrixTransform =
new SoMatrixTransform;
827 G4OpenInventorTransform3D oiTran
829 SbMatrix* sbMatrix = oiTran.GetSbMatrix();
831 const G4Vector3D scale = fpViewer->GetViewParameters().GetScaleFactor();
834 (SbVec3f((
float)scale.
x(),(float)scale.
y(),(float)scale.
z()));
835 sbMatrix->multRight(sbScale);
837 matrixTransform->matrix.setValue(*sbMatrix);
839 fCurrentSeparator->addChild(matrixTransform);