POV-Ray : Newsgroups : povray.general : dynamic memory possible? Server Time
5 Nov 2024 09:23:08 EST (-0500)
  dynamic memory possible? (Message 1 to 10 of 16)  
Goto Latest 10 Messages Next 6 Messages >>>
From: Gunnar
Subject: dynamic memory possible?
Date: 4 Jun 2007 06:30:02
Message: <web.4663e8a8e799405a678ea4f80@news.povray.org>
Hello there,
I hope you understand my question, it's directly translated from German...
;)

I have a file with some coordinates I want to store in an array. Since I
don't know how to store in an array that was not defined before I do this:
--------------------------------------
#fopen fsbsp "124440_ACR.txt" read
#declare fs_count = 0;
#while(defined(fsbsp))
  #read (fsbsp,utm_x,utm_y,utm_z)
  #declare fs_count = fs_count+1;
#end
#debug concat("fs_count     ", str(fs_count, 0,0),"n")

#declare a_pos = array[fs_count];

#fopen a_pos_f "124440_ACR.txt" read
#declare fs_count2 = 0;
#while(defined(a_pos_f))
  #read (a_pos_f,utm_x,utm_y,utm_z)
  #declare a_pos[fs_count2] = <utm_x,utm_y,utm_z>;
  #declare fs_count2 = fs_count2 + 1;
#end
--------------------------------------
Here's what this does:
- opens the file and "counts" the rows
- closes the file (#while(defined(...)))
- defines the array
- opens the file again
- reads and stores values
- closes the file again

Is there any possibility to do this without opening and reading the file
twice? I know Java can do dynamic memory (if that's what it is called), but
POV?

Thanks in advance
Gunnar


Post a reply to this message

From: Penelope20k
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 07:32:22
Message: <4663f846@news.povray.org>
you should simply answer this question.


Why did you open the same file under 2 differents name ?





news:web.4663e8a8e799405a678ea4f80@news.povray.org...
> Hello there,
> I hope you understand my question, it's directly translated from German...
> ;)
>
> I have a file with some coordinates I want to store in an array. Since I
> don't know how to store in an array that was not defined before I do this:
> --------------------------------------
> #fopen fsbsp "124440_ACR.txt" read
> #declare fs_count = 0;
> #while(defined(fsbsp))
>   #read (fsbsp,utm_x,utm_y,utm_z)
>   #declare fs_count = fs_count+1;
> #end
> #debug concat("fs_count     ", str(fs_count, 0,0),"n")
>
> #declare a_pos = array[fs_count];
>
> #fopen a_pos_f "124440_ACR.txt" read
> #declare fs_count2 = 0;
> #while(defined(a_pos_f))
>   #read (a_pos_f,utm_x,utm_y,utm_z)
>   #declare a_pos[fs_count2] = <utm_x,utm_y,utm_z>;
>   #declare fs_count2 = fs_count2 + 1;
> #end
> --------------------------------------
> Here's what this does:
> - opens the file and "counts" the rows
> - closes the file (#while(defined(...)))
> - defines the array
> - opens the file again
> - reads and stores values
> - closes the file again
>
> Is there any possibility to do this without opening and reading the file
> twice? I know Java can do dynamic memory (if that's what it is called),
but
> POV?
>
> Thanks in advance
> Gunnar
>
>
>


Post a reply to this message

From: Gunnar
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 07:45:02
Message: <web.4663fa51752edf44678ea4f80@news.povray.org>
"Penelope20k" <pen### [at] caramailfr> wrote:
> you should simply answer this question.
>
>
> Why did you open the same file under 2 differents name ?
>

You're right, I did that. But I don't think that matters... does it?


Post a reply to this message

From: Jim Charter
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 11:49:49
Message: <4664349d$1@news.povray.org>
Gunnar wrote:

> Is there any possibility to do this without opening and reading the file
> twice? I know Java can do dynamic memory (if that's what it is called), but
> POV?
>
In arrays.inc there is a macro to resize an array.  So you can define 
you array initially to take the first item then resize it with each 
sucessful read from your file.


Post a reply to this message

From: Warp
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 12:34:14
Message: <46643f05@news.povray.org>
Jim Charter <jrc### [at] msncom> wrote:
> In arrays.inc there is a macro to resize an array.  So you can define 
> you array initially to take the first item then resize it with each 
> sucessful read from your file.

  This is not a good solution, especially if the amount of items is large.

  The resizing is done by creating a second array of the new size and
then copying the elements of the first array into the second. If you do
this for all n elements in the input you'll end up doing n resizes and
n+(n-1)+(n-2)+...+1 copy operations, which grows proportional to the
square of n. (For example if the input had 1000 elements it would end up
making 500500 copy operations. That's half million.)

-- 
                                                          - Warp


Post a reply to this message

From: digitalseraphim
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 12:54:54
Message: <466443de$1@news.povray.org>
Warp wrote:
> Jim Charter <jrc### [at] msncom> wrote:
>> In arrays.inc there is a macro to resize an array.  So you can define 
>> you array initially to take the first item then resize it with each 
>> sucessful read from your file.
> 
>   This is not a good solution, especially if the amount of items is large.
> 
>   The resizing is done by creating a second array of the new size and
> then copying the elements of the first array into the second. If you do
> this for all n elements in the input you'll end up doing n resizes and
> n+(n-1)+(n-2)+...+1 copy operations, which grows proportional to the
> square of n. (For example if the input had 1000 elements it would end up
> making 500500 copy operations. That's half million.)
> 

but what you can do, is start out with a reasonable guess and increase 
it from there (by jumps).  You may end up wasting some space by growing 
the array too large, but it would work.

-ds


Post a reply to this message

From: Jim Charter
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 13:02:25
Message: <466445a1$1@news.povray.org>
Warp wrote:
> Jim Charter <jrc### [at] msncom> wrote:
> 
>>In arrays.inc there is a macro to resize an array.  So you can define 
>>you array initially to take the first item then resize it with each 
>>sucessful read from your file.
> 
> 
>   This is not a good solution, especially if the amount of items is large.
> 
>   The resizing is done by creating a second array of the new size and
> then copying the elements of the first array into the second. If you do
> this for all n elements in the input you'll end up doing n resizes and
> n+(n-1)+(n-2)+...+1 copy operations, which grows proportional to the
> square of n. (For example if the input had 1000 elements it would end up
> making 500500 copy operations. That's half million.)
> 
<shrug> I know ... but that's his call. And even a slow old 1 gh machine 
can still polish off a half a million copies pretty fast can't it? Also 
the coding is awkward with the array index, the loop control and the 
array dimension to resize to, being offset.  Personally I would probably 
print the array declaration into an include file, externally in a 
program such as Python.  But we really don't know what his purposes are.


Post a reply to this message

From: Bill Pragnell
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 15:20:02
Message: <web.466464d5752edf447e595fbb0@news.povray.org>
Jim Charter <jrc### [at] msncom> wrote:
> <shrug> I know ... but that's his call. And even a slow old 1 gh machine
> can still polish off a half a million copies pretty fast can't it? Also
> the coding is awkward with the array index, the loop control and the
> array dimension to resize to, being offset.  Personally I would probably
> print the array declaration into an include file, externally in a
> program such as Python.  But we really don't know what his purposes are.

If the data in the file isn't changing very often it might be worth running
a loop to count the lines, then write a new data file with the array size
as a header (maybe from a macro or a separate scene file). Then subsequent
test renders etc with the same data can read the header and create an array
with the correct size.

But maybe that's not convenient... :-)


Post a reply to this message

From: Jim Charter
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 15:57:25
Message: <46646ea5$1@news.povray.org>
Bill Pragnell wrote:

> 
> If the data in the file isn't changing very often it might be worth running
> a loop to count the lines, then write a new data file with the array size
> as a header (maybe from a macro or a separate scene file). Then subsequent
> test renders etc with the same data can read the header and create an array
> with the correct size.
> 
> But maybe that's not convenient... :-)
> 
> 

Yeah, not very self-contained though.  I preliminary counting read is of 
course what he is already doing and wants to improve on.  Probably the 
best solution would be to write the array declaration to an include file 
right from POV. But there are lots of solutions, my guess is he is just 
casting around for options, so hence my original answer to his question.


Post a reply to this message

From: Alain
Subject: Re: dynamic memory possible?
Date: 4 Jun 2007 19:01:39
Message: <466499d3$1@news.povray.org>
Gunnar nous apporta ses lumieres en ce 2007/06/04 07:41:
> "Penelope20k" <pen### [at] caramailfr> wrote:
>> you should simply answer this question.
>>
>>
>> Why did you open the same file under 2 differents name ?
>>
> 
> You're right, I did that. But I don't think that matters... does it?
> 
> 
Not realy, it just use some more memory.

Alain


Post a reply to this message

Goto Latest 10 Messages Next 6 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.