function [I]=SimpleStructMask_OUH(StructData,CTData,StructNumber)
  %This function return a binary mask image the size of CTData. All mask
  %values will be zero expect for those who is insidde the stucture in
  %StructurData given by the index StructNumber
  
  %Check added to test if the contour is within the z-pos of any of the CT images
  %10-11-2014 /CaB
  
  %Corrected a small issue related to Master Plan (Nucletron). In
  %Nucletron, it has been seen at least once that two delineations can be
  %identical. In that case, they are both deleted, assuming that one of the
  %delineations is a deletion of the other
  %07-07-2022 /CaB 

  
  I=zeros(size(CTData{2}),'uint8');
  temp2DImage=zeros(size(CTData{2},1),size(CTData{2},2),'uint8');
  temp=StructData{2}{StructNumber,2};
  [x, y, z, M]=GetVoxelPositions_OUH(CTData);
  SliceThickness=z(1:end-1)-z(2:end);
  %SliceThickness=CTData{5}(1:end-1)-CTData{5}(2:end);
  test=find(abs((SliceThickness-SliceThickness(1))/SliceThickness(1))>0.001, 1);
  if isempty(test)
    SliceThickness=abs(SliceThickness(1))/10;
  else
    warning('SliceThickness is not constant, Exit');
    return;
  end

  %The code is not a simple loop over struct slices since more than one
  %structure might be defined for a given CT slice. Structures within
  %structures might also be present. In that case the outermost structure
  %defines the mask the next is then subtrcated the third (if exist) is
  %added an so on in a alternating patterne. Structure which is only partly
  %overlapping is not allowed, and a warning will be given.
  %The idea of the algortihm is as follows
  %1) Find images beloging to the same CT slice
  %2) Of those find the ones which is not contained within any of the other
  %contours
  %3) Make union of the contours from step 2
  %4) Add or subtract union to the mask image. Add if the structure is an
  %odd numer in nesting sequence (the outhermost the third...) otherwise
  %subtract
  %5) "Remove" the used images and go to step 2 if more structures for this
  %slice exist
  %6) Go to step if more structs exist 
  
  %rotate struct Dicom coordinates to match the coordinates in the image data cube
  %This is only really needed for images which are not aligned with the
  %main axis of the Dicom coordinate system fixed in the patient
  %Remember that the M matrix includes tranlastion which is in mm. The
  %struct data in temp are unfortunate in cm thus a conversion back to mm
  %and afterwards to cm is needed. This unfortune conversion is kept in
  %order to be backward compatible
  Minv=M^(-1);
  for i=1:size(temp,1)
    rotatedCoor=Minv*[10*temp{i}'; ones(1,size(temp{i},1))];
    temp{i}=rotatedCoor(1:3,:)'/10;
  end
  
  StructNum=1:size(temp,1); %These are the Struct numbers which is not "used" yet
  StructPos=zeros(1,size(temp,1)); %These are the corrsponding z positions of StructNum
  for i=1:size(temp,1)
    StructPos(i)=temp{i}(1,3);
  end 
  while ~isempty(StructNum)
    Pos=StructPos(1); %Position of first available struct
    
    ZIndexInImage= abs(CTData{5}-Pos)<SliceThickness/3;
    SamePos=find(abs(StructPos-Pos)<SliceThickness/3); %Other slices with same position as first slice
    DiffPos=find(abs(StructPos-Pos)>SliceThickness/3); %Negation of SamePos is used while removing structs from StructNum and StructPos
    %More contours with same z-position
    TempMask=zeros(size(CTData{2},1),size(CTData{2},2),size(SamePos,2),'uint8');
    for i=1:size(SamePos,2)
      Posn=StructNum(SamePos(i)); %Number of the i'th image in the original struct file
      TempMask(:,:,i)=roipoly(CTData{3},CTData{4},temp2DImage,temp{Posn}(:,1),temp{Posn}(:,2));  
      
    end
    %TempMask do now contain the mask of the struct with similar z-pos. These needs to be combined in the following
    
    %First, check whether two of the contours are identical. It should not
    %be possible but has occurred from Master Plan (Nucletron) at least
    %once. We will remove all delineations created as identical since it is
    %most likely a numerical problem related to a deletion of a previous
    %delineation. This method might not be correct in all situations, but a
    %decision is needed for this non-standard struct data
    IndenticalIndex=false(size(TempMask,3),1);
    for i=1:size(TempMask,3)
        for j=i+1:size(TempMask,3)
            temp_mask=TempMask(:,:,i)==TempMask(:,:,j);
            if sum(temp_mask(:))==size(TempMask,1)*size(TempMask,2)
                IndenticalIndex(i)=true;
                IndenticalIndex(j)=true;
            end
        end
    end
    TempMask=TempMask(:,:,~IndenticalIndex);
    AddContour=true;
    while ~isempty(TempMask)
      %Find those which is not contained in other structures
      NotContained=ones(1,size(TempMask,3));
      for i=1:size(TempMask,3)
        for j=1:size(TempMask,3)
          if i~=j
            %Check if exist pixel in i which is not in j
            %Find i volumes which is not contained in j. If such exist i is
            %not contained in j. Partial overlap is not seen as "nested" structures
            %they will be handled as two seperate structures with no
            %overlap (both added or subtracted depending on the "nesting"
            %level). Empty volumes should be seen as a seperate structure
            %with no overlap with any other structure
            if max(max(TempMask(:,:,i)))==1
              %Only do check if i is a non-empty mask
              TestValue=find(and(TempMask(:,:,i),~TempMask(:,:,j))==1,1);
              if isempty(TestValue)
                %i is located inside j
                NotContained(i)=0;  
              end
            end
          end
        end
      end
      %NotContained values equal to 1 is the countours not contained in
      %other contours
      if isempty(find(NotContained==1, 1))
        %This should be be possible to happen, but just in case :-)
        warning('Structure error. Not able to interpret structure. Exit');
        return;
      end
      %Create Union of those slices which is not contained in other structures
      UnionImage=zeros(size(CTData{2},1),size(CTData{2},2),'uint8');
      for i=find(NotContained==1)
        UnionImage=or(UnionImage,TempMask(:,:,i));
      end
      %Add or subtract from I. Subtraction is only used if structures within structures are present 
      %Check added to test if the contour is within any of the CT images
      %10-11-2014 /CaB
      if sum(ZIndexInImage)>0
        if AddContour
          I(:,:,ZIndexInImage)=or(I(:,:,ZIndexInImage),UnionImage);
        else
          I(:,:,ZIndexInImage)=and(I(:,:,ZIndexInImage),~UnionImage);
        end
      end
      %Change value of AddContours such that next level of nested
      %structures within structures is either added or subtracted as
      %appropriate
      AddContour=~AddContour;
      %Remove the images which was not contained in other images from
      %TempMask
      TempMask=TempMask(:,:,NotContained==0);
    end
    StructNum=StructNum(DiffPos);
    StructPos=StructPos(DiffPos);
  end  
end