697{
699 if (!currentNode) {
700 static G4bool first =
true;
701 if (first) {
702 first = false;
703 G4Exception(
"G4Qt3DSceneHandler::AddPrimitive(const G4Polyhedron&)",
705 "No available node!");
706 }
707 return;
708 }
709
711
713
714
715
716
717
718
719
720
721 std::vector<G4Point3D> vertices;
722 std::vector<G4Normal3D> normals;
723
724
725
726 typedef std::pair<G4Point3D,G4Point3D> Line;
727 std::vector<Line> lines;
728 auto insertIfNew = [&lines](const Line& newLine) {
729
730
731
732
733
734
735
736
737 lines.push_back(newLine);
738 };
739
742 do {
747 notLastFace = polyhedron.
GetNextFacet(nEdges, vertex, edgeFlag, normal);
748 vertices.push_back(vertex[0]);
749 vertices.push_back(vertex[1]);
750 vertices.push_back(vertex[2]);
751 normals.push_back(normal[0]);
752 normals.push_back(normal[1]);
753 normals.push_back(normal[2]);
754 if(isAuxilaryEdgeVisible||edgeFlag[0]>0)insertIfNew(Line(vertex[0],vertex[1]));
755 if(isAuxilaryEdgeVisible||edgeFlag[1]>0)insertIfNew(Line(vertex[1],vertex[2]));
756 if (nEdges == 3) {
757
758
759 if(isAuxilaryEdgeVisible||edgeFlag[2]>0)insertIfNew(Line(vertex[2],vertex[0]));
760 } else if (nEdges == 4) {
761
762
763 vertices.push_back(vertex[2]);
764 vertices.push_back(vertex[3]);
765 vertices.push_back(vertex[0]);
766 normals.push_back(normal[2]);
767 normals.push_back(normal[3]);
768 normals.push_back(normal[0]);
769 if(isAuxilaryEdgeVisible||edgeFlag[2]>0)insertIfNew(Line(vertex[2],vertex[3]));
770 if(isAuxilaryEdgeVisible||edgeFlag[3]>0)insertIfNew(Line(vertex[3],vertex[0]));
771 } else {
773 << "ERROR: polyhedron face with unexpected number of edges (" << nEdges << ')'
776 return;
777 }
778 } while (notLastFace);
779 const auto nVerts = vertices.size();
780 const auto nLines = lines.size();
781
782
783
785 transform->setObjectName("transform");
786
787 Qt3DCore::QEntity* wireframeEntity = nullptr;
788 Qt3DCore::QEntity* surfaceEntity = nullptr;
789 static G4int errorCount = 0;
791 switch (drawing_style) {
793 wireframeEntity = new Qt3DCore::QEntity(currentNode);
794 wireframeEntity->addComponent(transform);
795 break;
797 wireframeEntity = new Qt3DCore::QEntity(currentNode);
798 wireframeEntity->addComponent(transform);
799 surfaceEntity = new Qt3DCore::QEntity(currentNode);
800 surfaceEntity->addComponent(transform);
801 break;
803 surfaceEntity = new Qt3DCore::QEntity(currentNode);
804 surfaceEntity->addComponent(transform);
805 break;
807 wireframeEntity = new Qt3DCore::QEntity(currentNode);
808 wireframeEntity->addComponent(transform);
809 surfaceEntity = new Qt3DCore::QEntity(currentNode);
810 surfaceEntity->addComponent(transform);
811 break;
813
814 if (errorCount == 0) {
815 ++errorCount;
816 G4warn <<
"WARNING: Qt3D: cloud drawing not implemented" <<
G4endl;
817 }
818 return;
819 break;
820 }
821
822 const auto vertexByteSize = 3*
sizeof(
PRECISION);
823
824 Qt3DRender::QGeometry* vertexGeometry = nullptr;
825 Qt3DRender::QGeometry* lineGeometry = nullptr;
826
827 Qt3DRender::QAttribute* positionAtt = nullptr;
828 Qt3DRender::QAttribute* normalAtt = nullptr;
829 Qt3DRender::QAttribute* lineAtt = nullptr;
830
831 Qt3DRender::QBuffer* vertexBuffer = nullptr;
835
836
837
838 QByteArray vertexByteArray;
839 const auto vertexBufferByteSize = 2*nVerts*vertexByteSize;
840 vertexByteArray.resize((
G4int)vertexBufferByteSize);
841 auto vertexBufferArray =
reinterpret_cast<PRECISION*
>(vertexByteArray.data());
843 for (std::size_t i = 0; i < nVerts; ++i) {
844 vertexBufferArray[i1++] = vertices[i].x();
845 vertexBufferArray[i1++] = vertices[i].y();
846 vertexBufferArray[i1++] = vertices[i].z();
847 vertexBufferArray[i1++] = normals[i].x();
848 vertexBufferArray[i1++] = normals[i].y();
849 vertexBufferArray[i1++] = normals[i].z();
850 }
851
852 vertexGeometry = new Qt3DRender::QGeometry();
853 vertexGeometry->setObjectName("vertexGeometry");
854 vertexBuffer = new Qt3DRender::QBuffer(vertexGeometry);
855 vertexBuffer->setObjectName("Vertex buffer");
856 vertexBuffer->setData(vertexByteArray);
857
858
859 positionAtt = new Qt3DRender::QAttribute;
860 positionAtt->setObjectName("Position attribute");
861 positionAtt->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
862 positionAtt->setBuffer(vertexBuffer);
863 positionAtt->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
864 positionAtt->setVertexBaseType(
BASETYPE);
865 positionAtt->setVertexSize(3);
866 positionAtt->setCount((
G4int)nVerts);
867 positionAtt->setByteOffset(0);
868 positionAtt->setByteStride(2*vertexByteSize);
869
870
871 normalAtt = new Qt3DRender::QAttribute;
872 normalAtt->setObjectName("Normal attribute");
873 normalAtt->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
874 normalAtt->setBuffer(vertexBuffer);
875 normalAtt->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
876 normalAtt->setVertexBaseType(
BASETYPE);
877 normalAtt->setVertexSize(3);
878 normalAtt->setCount((
G4int)nVerts);
879 normalAtt->setByteOffset(vertexByteSize);
880 normalAtt->setByteStride(2*vertexByteSize);
881 }
882
883 Qt3DRender::QBuffer* lineBuffer = nullptr;
887
888
889 QByteArray lineByteArray;
890 const auto lineBufferByteSize = 2*nLines*vertexByteSize;
891 lineByteArray.resize((
G4int)lineBufferByteSize);
892 auto lineBufferArray =
reinterpret_cast<PRECISION*
>(lineByteArray.data());
894 for (const auto& line: lines) {
895 lineBufferArray[i2++] = line.first.x();
896 lineBufferArray[i2++] = line.first.y();
897 lineBufferArray[i2++] = line.first.z();
898 lineBufferArray[i2++] = line.second.x();
899 lineBufferArray[i2++] = line.second.y();
900 lineBufferArray[i2++] = line.second.z();
901 }
902
903 lineGeometry = new Qt3DRender::QGeometry();
904 lineGeometry->setObjectName("lineGeometry");
905 lineBuffer = new Qt3DRender::QBuffer(lineGeometry);
906 lineBuffer->setObjectName("Line buffer");
907 lineBuffer->setData(lineByteArray);
908
909
910 lineAtt = new Qt3DRender::QAttribute;
911 lineAtt->setObjectName("Position attribute");
912 lineAtt->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
913 lineAtt->setBuffer(lineBuffer);
914 lineAtt->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
915 lineAtt->setVertexBaseType(
BASETYPE);
916 lineAtt->setVertexSize(3);
917 lineAtt->setCount((
G4int)nLines);
918 lineAtt->setByteOffset(0);
919 lineAtt->setByteStride(vertexByteSize);
920 }
921
922
923
925 Qt3DExtras::QDiffuseSpecularMaterial* material;
926 Qt3DRender::QGeometryRenderer* renderer;
927 switch (drawing_style) {
928
930
931 lineGeometry->addAttribute(lineAtt);
932
933 material = new Qt3DExtras::QDiffuseSpecularMaterial();
934 material->setObjectName("materialForWireframe");
936 material->setShininess(0.);
937 material->setSpecular(0.);
938 wireframeEntity->addComponent(material);
939
940 renderer = new Qt3DRender::QGeometryRenderer;
941 renderer->setObjectName("polyhedronWireframeRenderer");
942 renderer->setGeometry(lineGeometry);
943 renderer->setVertexCount(2*(
G4int)nLines);
944 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
945 wireframeEntity->addComponent(renderer);
946
947 break;
948
950
951
952
953 vertexGeometry->addAttribute(positionAtt);
954 vertexGeometry->addAttribute(normalAtt);
955
956 material = new Qt3DExtras::QDiffuseSpecularMaterial();
957 material->setObjectName("materialForHiddenLines");
958 material->setAmbient(Qt::white);
959 material->setShininess(0.);
960 material->setSpecular(0.);
961 surfaceEntity->addComponent(material);
962
963 renderer = new Qt3DRender::QGeometryRenderer;
964 renderer->setObjectName("polyhedronSurfaceRenderer");
965 renderer->setGeometry(vertexGeometry);
966 renderer->setVertexCount((
G4int)nVerts);
967 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
968 surfaceEntity->addComponent(renderer);
969
970
971
972 lineGeometry->addAttribute(lineAtt);
973
974 material = new Qt3DExtras::QDiffuseSpecularMaterial();
975 material->setObjectName("materialForWireFrame");
977 material->setShininess(0.);
978 material->setSpecular(0.);
979 wireframeEntity->addComponent(material);
980
981 renderer = new Qt3DRender::QGeometryRenderer;
982 renderer->setObjectName("polyhedronWireframeRenderer");
983 renderer->setGeometry(lineGeometry);
984 renderer->setVertexCount(2*(
G4int)nLines);
985 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
986 wireframeEntity->addComponent(renderer);
987
988 break;
989
991
992 vertexGeometry->addAttribute(positionAtt);
993 vertexGeometry->addAttribute(normalAtt);
994
995 material = new Qt3DExtras::QDiffuseSpecularMaterial();
996 material->setObjectName("materialForSurface");
998 if (colour.GetAlpha() < 1.) material->setAlphaBlendingEnabled(true);
999 surfaceEntity->addComponent(material);
1000
1001 renderer = new Qt3DRender::QGeometryRenderer;
1002 renderer->setObjectName("polyhedronSurfaceRenderer");
1003 renderer->setGeometry(vertexGeometry);
1004 renderer->setVertexCount((
G4int)nVerts);
1005 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
1006 surfaceEntity->addComponent(renderer);
1007
1008 break;
1009
1011
1012
1013
1014 vertexGeometry->addAttribute(positionAtt);
1015 vertexGeometry->addAttribute(normalAtt);
1016
1017 material = new Qt3DExtras::QDiffuseSpecularMaterial();
1018 material->setObjectName("materialForSurface");
1020 if (colour.GetAlpha() < 1.) material->setAlphaBlendingEnabled(true);
1021 surfaceEntity->addComponent(material);
1022
1023 renderer = new Qt3DRender::QGeometryRenderer;
1024 renderer->setObjectName("polyhedronSurfaceRenderer");
1025 renderer->setGeometry(vertexGeometry);
1026 renderer->setVertexCount((
G4int)nVerts);
1027 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
1028 surfaceEntity->addComponent(renderer);
1029
1030
1031
1032 lineGeometry->addAttribute(lineAtt);
1033
1034 material = new Qt3DExtras::QDiffuseSpecularMaterial();
1035 material->setObjectName("materialForWireframe");
1037 material->setShininess(0.);
1038 material->setSpecular(0.);
1039 wireframeEntity->addComponent(material);
1040
1041 renderer = new Qt3DRender::QGeometryRenderer;
1042 renderer->setObjectName("polyhedronSurfaceRenderer");
1043 renderer->setGeometry(lineGeometry);
1044 renderer->setVertexCount(2*(
G4int)nLines);
1045 renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);
1046 wireframeEntity->addComponent(renderer);
1047
1048 break;
1049
1051
1052 break;
1053 }
1054}
virtual G4String GetCurrentTag() const
G4ViewParameters::DrawingStyle GetDrawingStyle(const G4VisAttributes *)
G4bool IsAuxEdgeVisible() const
G4int GetNoFacets() const
G4bool GetNextFacet(G4int &n, G4Point3D *nodes, G4int *edgeFlags=nullptr, G4Normal3D *normals=nullptr) const