How to write a 3D image from a 3x3 matrix written in fortran 90? - image

I'm trying to write a 3D image in fortran 90.
The code for the object I want in the image:
Here is a code of a cube in fortran:
PROGRAM myimage
integer xmax,ymax,zmax
parameter (xmax=10,ymax=10,zmax=10)
INTEGER mytable(1:xmax,1:ymax,1:zmax)
do 1 i1=1,xmax
do 2 i2=1,ymax
do 3 i3=1,zmax
mytable(i1,i2,i3)=0
if ((i1.ge.3).and.(i1.le.6).and.(i2.ge.3).and.(i2.le.6).and.(i3.ge.3).and.(i3.le.6)) then
mytable(i1,i2,i3)=1
endif
3 continue
2 continue
1 continue
end
The type of image I'd like to get:
The type of image I want is like this :
The cube would be my pixels mytable=1 and around it there would be pixels : mytable=0
What I tried:
I first tried to write a code to make the image directly in fortran, but it turned out that the image issued was not a 3D image as I wanted (see Appendix 1).
The question:
Could you explain me how to view that type of object in 3D please ?
For instance, following the comment of Vladimir F, I downloaded Paraview.
I found this question that is quite similar to where I stand now.
But I don't understand what exactly I have to write in the file, if I choose to write it in the format UCD. I didn't find explanations on the internet and the link that is brought in the question there does not work.
Appendix 1:
Here is a code for a 2D image and for the 3D image I tried to code.
I first wrote a 2D image that works. I tried to generalize it to 3D. I'd like to view the object where mytable=1 in 3D.
subroutine image2d(mytable,xmax,ymax,zmax)
integer xmax,ymax,zmax,mytable(1:xmax,1:ymax,1:zmax)
character*15 fname
WRITE(fname,'(a)')'myimage2d.ppm'
open (100,file=fname,form='formatted')
write(100,'(a)') 'P3'
write(100,*) '#'
write(100,*) xmax,ymax
write(100,*) 2
do 10 i10=1,xmax
do 20 i20=1,ymax
if (mytable(i10,i20,5).eq.0) then
write(100,*) '2 2 2'
else if (mytable(i10,i20,5).eq.1) then
write(100,*) '0 0 0'
end if
20 continue
10 continue
end
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine image3d(mytable,xmax,ymax,zmax)
integer xmax,ymax,zmax,mytable(1:xmax,1:ymax,1:zmax)
character*15 fname
WRITE(fname,'(a)')'myimage3d.ppm'
open (200,file=fname,form='formatted')
write(200,'(a)') 'P3'
write(200,*) '#'
write(200,*) xmax,ymax,zmax
write(200,*) 3
do 11 i10=1,xmax
do 21 i20=1,ymax
do 31 i30=1,ymax
if (mytable(i10,i20,i30).eq.0) then
write(200,*) '2 2 2'
else if (mytable(i10,i20,i30).eq.1) then
write(200,*) '0 1 2'
end if
31 continue
21 continue
11 continue
end

Related

how to limit 00 to 99 counter to 60

For a simple starter project I was putting together a 2-7segement display 00 to 99 counter coded on sketch.
//The line below is the array containing all the binary numbers for the digits on a SSD from 0 to 9
const int number[11] = {0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000};
I believe that my solution is either to change this part of the code or add another line, I'm just unsure.
Any advice?
I have tried adding another line to set one of the displays to stop at 6 but it didn't compile with the rest of the code.

Encapsulated poscript max line limit

I think that I'm exceeding EPS max line limits:
I'm generating an eps programmatically that consists of a grid of pictures.
My EPS has this structure:
%!PS-Adobe-3.0 EPSF-3.0
.
.
%%BeginProlog
%%EndProlog
%%Page: 1 1
%%Begin Raster Image. Index: 0
save
449 2576 translate
0 rotate
-282 -304 translate
[1 0 0 1 0 0] concat
0 0 translate
[1 0 0 1 0 0] concat
0 0 translate
userdict begin
DisplayImage
0 0
564 608
12
564 608
0
0
FBDBB9FBDCBCFDDBBAFFD8B2FFD7A9FED4A1FCD29CFDD09EFED0A2FFD0A6FFCDA3FFCBA0FFCBA0...
EED79CEBD09CEDD19EEED2A1EFD3A3F0D4A5F0D4A6F0D4A7F1D4A4F3D4A0F3D49F
end
restore
%%End Raster Image
%%Begin Raster Image. Index: 1
.
.
end
restore
%%End Raster Image
%%Begin Raster Image. Index: 2
etc
So the thing is if I write up to 4 images to the EPS, everything works fine, but when I try to write a 5th, the eps won't open on any EPS viewer including Adobe Illustrator (the operation cannot complete because of an unknown error).
I tried using different images to make sure that the particular images were ok and I got the same result, as long as I'm writing 4 images (105825 lines file) everything works. But when I use 5 (132253 lines file) it fails.
Is it possible that I'm exceeding the maximum line limit for EPS?
This are the files in question if you'd like to analyze them:
the one that works - >https://files.fm/u/bfn2d32m and the one that doesn't ->
https://files.fm/u/4gbybr3y
There's no 'line limit' in PostScript or EPS, so you can't be hitting that.
When I run your file through Ghostscript it throws an error /undefined in yImage (I'd suggest you debug PostScript using a proper PostScript interpreter, not Adobe Illustrator).
This suggests to me that one of your images is using more data than you have supplied, so the interpreter runs off the end of the data, consuming parts of the program, until it has read sufficient bytes from currentfile to satisfy the request. At that point is starts processing the file as PostScript again, but the file pointer now points to the 'yImage' of the next 'DisplayImage'. Since you haven't defined a 'yImage' key, naturally this gives you an 'undefined' error.
From your description, this would seem likely to be the 4th image, since adding the 5th throws the error. Note that if your program terminates without supplying enough data (so the interpreter reaches EOF) then the data supplied will be drawn. So it might look like your 4th image is correct even when it isn't, provided it isn't followed by any further program code.
A style note; PostScript is a stack-based language, so normally one would proceed by pushing values onto the stack and reading them from there, instead of executing the 'token' operator.
So your input would be more like :
0 0
564 608
12
564 608
0
0
DisplayImage
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
...
And the DisplayImage code would be :
/DisplayImage
{
%
% Display a DirectClass or PseudoClass image.
%
% Parameters:
% x & y translation.
% x & y scale.
% label pointsize.
% image label.
% image columns & rows.
% class: 0-DirectClass or 1-PseudoClass.
% compression: 0-none or 1-RunlengthEncoded.
% hex color packets.
%
gsave
/buffer 512 string def
/byte 1 string def
/color_packet 3 string def
/pixels 768 string def
/compression exch def
/class exch def
/rows exch def
/columns exch def
/pointsize exch def
scale
translate
This avoids you having to use token at all for the scale and translate operations for instance.

Suggestions for data extraction Data in fortran

I use F95/90 and IBM compiler. I am trying to extract the numerical values from block and write in a file. I am facing a strange error in the output which I cannot understand. Every time I execute the program it skips the loop between 'Beta' and 'END'. I am trying to read and store the values.
The number of lines inside the Alpha- and Beta loops are not fixed. So a simple 'do loop' is of no use to me. I tried the 'do while' loop and also 'if-else' but it still skips the 'Beta' part.
Alpha Singles Amplitudes
15 3 23 4 -0.186952
15 3 26 4 0.599918
15 3 31 4 0.105048
15 3 23 4 0.186952
Beta Singles Amplitudes
15 3 23 4 0.186952
15 3 26 4 -0.599918
15 3 31 4 -0.105048
15 3 23 4 -0.186952
END `
The simple short code is :
program test_read
implicit none
integer::nop,a,b,c,d,e,i,j,k,l,m,ios
double precision::r,t,rr
character::dummy*300
character*15::du1,du2,du3
open (unit=10, file="1.txt", status='old',form='formatted')
100 read(10,'(a100)')dummy
if (dummy(1:3)=='END') goto 200
if(dummy(2:14)=='Alpha Singles') then
i=0
160 read(10,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r
do while(du1.ne.' Bet')
write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')'AS',du1,b,du2,c,du3,d,du4,e,r
goto 160
end do
elseif (dummy(2:14)=='Beta Singles') then
170 read(10,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r
if((du1=='END'))then
stop
else
write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')'BS',du1,b,du2,c,du3,d,du4,e,r
goto 170
end if
end if
goto 100
200 print*,'This is the end'
end program test_read
Your program never gets out of the loop which checks for Beta because when your while loop exits, it has already read the line with Beta. It then goes to 100 which reads the next line after Beta, so you never actually see Beta Singles. Try the following
character(len=2):: tag
read(10,'(a100)')dummy
do while (dummy(1:3).ne.'END')
if (dummy(2:14)=='Alpha Singles') then
tag = 'AS'
else if (dummy(2:14)=='Beta Singles') then
tag = 'BS'
else
read(dummy,'(a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')du1,b,du2,c,du3,d,du4,e,r
write(*,'(a2,a4,i2,a6,i1,a4,i2,a6,i1,f12.6)')tag,du1,b,du2,c,du3,d,du4,e,r
end if
read(10, '(a100)') dummy
end do
print*,'This is the end'

Checking if pixel belongs to an image

I have written the following function that find if a pixel belongs to an image in matlab.
At the beginning, I wanted to test it as if a number in a set belongs to a vector like the following:
function traverse_pixels(img)
for i:1:length(img)
c(i) = img(i)
end
But, when I run the following commands for example, I get the error shown at the end:
>> A = [ 34 565 456 535 34 54 5 5 4532 434 2345 234 32332434];
>> traverse_pixels(A);
??? Error: File: traverse_pixels.m Line: 2 Column: 6
Unexpected MATLAB operator.
Why is that? How can I fix the problem?
Thanks.
There is a syntax error in the head of your for loop, it's supposed to be:
for i = 1:length(img)
Also, to check if an array contains a specific value you could use:
A = [1 2 3]
if sum(A==2)>0
disp('there is at least one 2 in A')
end
This should be faster since no for loop is included.
for i = 1:length(image)
silly error, not : , it is =

measurement converter for selecting unit

I'm seeing quite a few measurement conversion relating gems, but I haven't been able to find one that will select the best/closest unit.
For example
If I give the gem the measurement of
9 inches + 6 inches
I'm trying to get the result
1 foot, 3 inches
The conversion tools I've seen, I'd have to tell the convertor to try to convert to feet, and then decide which is the most appropriate measurement.
Not sure how complicated you wanted to get but for your example I did this:
def plain_english_conversion(inches)
divmod_output = inches.divmod(12)
puts "#{divmod_output[0]} ft, #{divmod_output[1]} in"
end
puts "15 "
plain_english_conversion(15)
puts "37 "
plain_english_conversion(37)
With the output of:
15
1 ft, 3 in
37
3 ft, 1 in
Certainly I restricted it to feet/inches but you can abstract it out if necessary (inches & yards, feet & yards, etc.)

Resources