changeset 9:8b265e711351

added button "Attach NIfTI 3D model" in Orthanc Explorer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 18 Jul 2023 17:05:23 +0200
parents d1267c6c33e1
children 549ba7ada071
files Sources/OrthancExplorer.js
diffstat 1 files changed, 111 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Sources/OrthancExplorer.js	Tue Jul 18 15:58:21 2023 +0200
+++ b/Sources/OrthancExplorer.js	Tue Jul 18 17:05:23 2023 +0200
@@ -153,8 +153,6 @@
   $('#stl-viewer-series').remove();
   $('#stl-generate-rtstruct-series').remove();
 
-  // Test whether this is a whole-slide image by check the SOP Class
-  // UID of one instance of the series
   GetResource('/series/' + seriesId, function(series) {
     if (series['Instances'].length == 1) {
       var instanceId = series['Instances'][0];
@@ -184,8 +182,6 @@
   $('#stl-viewer-instance').remove();
   $('#stl-generate-rtstruct-instance').remove();
 
-  // Test whether this is a whole-slide image by check the SOP Class
-  // UID of one instance of the series
   $.ajax({
     url: '/instances/' + instanceId + '/metadata/SopClassUid',
     success: function(sopClassUid) {
@@ -201,3 +197,114 @@
     }
   });
 });
+
+
+$('#study').live('pagebeforeshow', function() {
+  if (${HAS_CREATE_DICOM_STL}) {
+    var studyId = $.mobile.pageData.uuid;
+
+    $('#stl-attach-nifti-study').remove();
+
+    var b = $('<a>')
+        .attr('id', 'stl-attach-nifti-study')
+        .attr('data-role', 'button')
+        .attr('href', '#')
+        .attr('data-icon', 'search')
+        .attr('data-theme', 'e')
+        .text('Attach NIfTI 3D model')
+        .button();
+
+    b.insertAfter($('#study-info'));
+    b.click(function() {
+
+      var options = $('<ul>')
+          .attr('data-divider-theme', 'd')
+          .attr('data-role', 'listview');
+
+      var upload = $('<input>')
+          .attr('type', 'file')
+          .attr('id', 'stl-attach-nifti-study-upload')
+          .attr('data-theme', 'a');
+
+      options.append($('<li>').text('Choose the NIfTI file:'));
+      options.append($('<li>').append(upload));
+      options.append($('<li>').text('Resolution:'));
+      options.append($('<li>').append($('<select>')
+                                      .attr('id', 'stl-attach-nifti-study-resolution')
+                                      .attr('data-theme', 'a')
+                                      .append($('<option>').attr('value', '256').text('256'))
+                                      .append($('<option>').attr('value', '128').text('128'))
+                                      .append($('<option>').attr('value', '512').text('512'))));
+      options.append($('<li>')
+                     .append($('<input>')
+                             .attr('id', 'stl-attach-nifti-study-smooth')
+                             .attr('type', 'checkbox')
+                             .attr('data-theme', 'a')
+                             .attr('checked', ''))
+                     .append($('<label>')
+                             .attr('for', 'stl-attach-nifti-study-smooth')
+                             .text('Smooth volume')));
+
+      options.append($('<li>').append(
+        $('<a>')
+          .attr('href', '#')
+          .attr('rel', 'close').attr('data-theme', 'b')
+          .text('Generate')
+          .click(function(e) {
+            e.preventDefault();
+
+            var fileInput = document.getElementById('stl-attach-nifti-study-upload');
+            var resolution = $('#stl-attach-nifti-study-resolution').val();
+            var smooth = $('#stl-attach-nifti-study-smooth').is(':checked');
+
+            if (fileInput.files.length == 0) {
+              alert('No NIfTI file was selected');
+              return;
+            }
+
+            reader = new FileReader();
+            reader.onload = function() {
+
+              // https://github.com/axios/axios/issues/513
+              var nifti = reader.result;
+              var niftiBase64 = btoa(new Uint8Array(nifti).reduce((data, byte) => data + String.fromCharCode(byte), ''));
+
+              $.ajax({
+                url: '../stl/encode-nifti',
+                type: 'POST',
+                data: JSON.stringify({
+                  'Nifti' : 'data:application/octet-stream;base64,' + niftiBase64,
+                  'ParentStudy' : studyId,
+                  'Smooth' : smooth,
+                  'Resolution' : parseInt(resolution, 10)
+                }),
+                dataType: 'json',
+                success: function(s) {
+                  $.mobile.changePage('#series?uuid=' + s.ParentSeries, {
+                    allowSamePageTransition: true
+                  });
+                },
+                error: function() {
+                  alert('Error while generating the 3D model');
+                }
+              });
+
+            };
+
+            reader.readAsArrayBuffer(fileInput.files[0]);
+          })));
+
+      // Launch the dialog
+      $('#dialog').simpledialog2({
+        mode: 'blank',
+        animate: false,
+        headerText: 'Generate 3D model',
+        headerClose: true,
+        forceInput: false,
+        width: '100%',
+        blankContent: options
+      });
+
+    });
+  }
+});