comparison RenderingPlugin/Sources/Plugin.cpp @ 1889:fe4befc9c2b0

rendering of rt-struct in plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 18 Jan 2022 17:52:43 +0100
parents 9bdce2c91620
children 6ce81914f7e4
comparison
equal deleted inserted replaced
1888:9bdce2c91620 1889:fe4befc9c2b0
29 29
30 #include <EmbeddedResources.h> 30 #include <EmbeddedResources.h>
31 31
32 #include <Images/Image.h> 32 #include <Images/Image.h>
33 #include <Images/ImageProcessing.h> 33 #include <Images/ImageProcessing.h>
34 #include <Images/PngWriter.h>
34 #include <Images/NumpyWriter.h> 35 #include <Images/NumpyWriter.h>
35 #include <Logging.h> 36 #include <Logging.h>
36 #include <SerializationToolbox.h> 37 #include <SerializationToolbox.h>
37 #include <Toolbox.h> 38 #include <Toolbox.h>
38 39
656 657
657 static void RenderRtStruct(OrthancPluginRestOutput* output, 658 static void RenderRtStruct(OrthancPluginRestOutput* output,
658 const char* url, 659 const char* url,
659 const OrthancPluginHttpRequest* request) 660 const OrthancPluginHttpRequest* request)
660 { 661 {
661 DicomStructureCache::Accessor accessor(DicomStructureCache::GetSingleton(), request->groups[0]); 662 DataAugmentationParameters dataAugmentation;
662
663 if (!accessor.IsValid())
664 {
665 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
666 }
667
668 std::string structureName; 663 std::string structureName;
669 std::string instanceId; 664 std::string instanceId;
665 bool compress = false;
670 666
671 for (uint32_t i = 0; i < request->getCount; i++) 667 for (uint32_t i = 0; i < request->getCount; i++)
672 { 668 {
673 std::string key(request->getKeys[i]); 669 std::string key(request->getKeys[i]);
674 std::string value(request->getValues[i]); 670 std::string value(request->getValues[i]);
675 671
676 if (key == "structure") 672 if (!dataAugmentation.ParseParameter(key, value))
677 { 673 {
678 structureName = value; 674 if (key == "structure")
679 } 675 {
680 else if (key == "instance") 676 structureName = value;
681 { 677 }
682 instanceId = value; 678 else if (key == "instance")
683 } 679 {
684 else 680 instanceId = value;
685 { 681 }
686 LOG(WARNING) << "Unsupported option: " << key; 682 else if (key == "compress")
683 {
684 compress = ParseBoolean(key, value);
685 }
686 else
687 {
688 LOG(WARNING) << "Unsupported option: " << key;
689 }
687 } 690 }
688 } 691 }
689 692
690 if (structureName.empty()) 693 if (structureName.empty())
691 { 694 {
696 if (instanceId.empty()) 699 if (instanceId.empty())
697 { 700 {
698 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, 701 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
699 "Missing option \"instance\" to provide the Orthanc identifier of the instance of interest"); 702 "Missing option \"instance\" to provide the Orthanc identifier of the instance of interest");
700 } 703 }
701 704
702 std::unique_ptr<OrthancStone::DicomInstanceParameters> parameters(GetInstanceParameters(instanceId)); 705 std::unique_ptr<OrthancStone::DicomInstanceParameters> parameters(GetInstanceParameters(instanceId));
703 706
704 size_t structureIndex;
705 bool found = false;
706 for (size_t i = 0; i < accessor.GetRtStruct().GetStructuresCount(); i++)
707 {
708 if (accessor.GetRtStruct().GetStructureName(i) == structureName)
709 {
710 structureIndex = i;
711 found = true;
712 break;
713 }
714 }
715
716 if (!found)
717 {
718 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
719 "Unknown structure name: " + structureName);
720 }
721
722 std::list< std::vector<OrthancStone::Vector> > polygons; 707 std::list< std::vector<OrthancStone::Vector> > polygons;
723 accessor.GetRtStruct().GetStructurePoints(polygons, structureIndex, parameters->GetSopInstanceUid()); 708
709 {
710 DicomStructureCache::Accessor accessor(DicomStructureCache::GetSingleton(), request->groups[0]);
711
712 if (!accessor.IsValid())
713 {
714 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem);
715 }
716
717 size_t structureIndex;
718 bool found = false;
719 for (size_t i = 0; i < accessor.GetRtStruct().GetStructuresCount(); i++)
720 {
721 if (accessor.GetRtStruct().GetStructureName(i) == structureName)
722 {
723 structureIndex = i;
724 found = true;
725 break;
726 }
727 }
728
729 if (!found)
730 {
731 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
732 "Unknown structure name: " + structureName);
733 }
734
735 accessor.GetRtStruct().GetStructurePoints(polygons, structureIndex, parameters->GetSopInstanceUid());
736 }
724 737
725 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, parameters->GetWidth(), parameters->GetHeight(), false); 738 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, parameters->GetWidth(), parameters->GetHeight(), false);
726 Orthanc::ImageProcessing::Set(tmp, 0); 739 Orthanc::ImageProcessing::Set(tmp, 0);
727 740
741 OrthancStone::AffineTransform2D transform = dataAugmentation.ComputeTransform(parameters->GetWidth(), parameters->GetHeight());
742
728 for (std::list< std::vector<OrthancStone::Vector> >::const_iterator 743 for (std::list< std::vector<OrthancStone::Vector> >::const_iterator
729 it = polygons.begin(); it != polygons.end(); ++it) 744 it = polygons.begin(); it != polygons.end(); ++it)
730 { 745 {
731 std::vector<Orthanc::ImageProcessing::ImagePoint> points; 746 std::vector<Orthanc::ImageProcessing::ImagePoint> points;
732 points.reserve(it->size()); 747 points.reserve(it->size());
733 748
734 for (size_t i = 0; i < it->size(); i++) 749 for (size_t i = 0; i < it->size(); i++)
735 { 750 {
736 double x, y; 751 double x, y;
737 parameters->GetGeometry().ProjectPoint(x, y, (*it) [i]); 752 parameters->GetGeometry().ProjectPoint(x, y, (*it) [i]);
753 x /= parameters->GetPixelSpacingX();
754 y /= parameters->GetPixelSpacingY();
755
756 transform.Apply(x, y);
757
738 points.push_back(Orthanc::ImageProcessing::ImagePoint(x, y)); 758 points.push_back(Orthanc::ImageProcessing::ImagePoint(x, y));
739 } 759 }
740 760
741 Orthanc::ImageProcessing::FillPolygon(tmp, points, 255); 761 Orthanc::ImageProcessing::FillPolygon(tmp, points, 255);
742 } 762 }
743 763
744 AnswerNumpyImage(output, tmp, true); 764 AnswerNumpyImage(output, tmp, compress);
745 } 765 }
746 766
747 767
748 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType, 768 OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
749 OrthancPluginResourceType resourceType, 769 OrthancPluginResourceType resourceType,