Q: Thinking Particles



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 23/10/2006 at 18:13, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   9.603 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
    Hey Guys,
    I've started writing a plugin for Thinking Particles. I'm having a difficult time right now figuring out how to allocate the same number of objects as the total number of particles. I originally thought I would just create an array of null objects == to the NumParticles(), but array indices must be constant. I don't want to allocate an arbitrary number of null objects hoping that it will be larger than the number of particles in any scene that the plugin is used (a huge waste of memory). So in one scene if the total number of particles will be 150, I would like to generate 150 null objects. In another scene if the number of particles is 1000, I would like to create 1000 null objects and so on. I hope this makes sense. Any help is always appreciated.
    Josh



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 24/10/2006 at 03:47, xxxxxxxx wrote:

    I am not sure if I understand your problem, but usally you can define the size of an array like this:

      
    Real *mydata = NULL;  
    if(!mydata) mydata = bNew Real[datacount];  
    

    mydata is then your array of size of datacount.

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 24/10/2006 at 08:48, xxxxxxxx wrote:

    Hi Matthias,
    Thanks for the quick reply. But, from this code wouldn't datacount need to be initialized to a specific number? i.e datacount = 150. What I am trying to do is the following:
    This assumes that the TP_MasterSystem tpms object has already been created.
    datacount = tpms->NumParticles();
    This way the array I allocate will be the same number as that of the particles in the current scene. But this will yield compilation errors. Thanks in advance for any help.
    Josh



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 24/10/2006 at 09:41, xxxxxxxx wrote:

    Yes, it would need to be initialized to a specific number. But I don't see why this would raise compiler errors:

    LONG datacount = tpms->NumParticles();  
    Real* mydata = bNew Real[datacount];  
    if (!mydata) ; //Couldn't allocate the Real array
    

    Worst case, do this instead for allocation of the Real array:

    Real* mydata = static_cast<Real*>(GeAlloc(sizeof(Real)*datacount));
    

    If you are allocating an array of BaseObjects, I think you want to go a different route (you can't allocate an array of them directly as far as I know). You'll need to allocate an array of pointers to BaseObject and fill the pointers with allocated BaseObjects in a loop:

    BaseObject** nullArray = bNew BaseObject*[datacount];  
    if (nullArray)  
    {  
       Bool error = FALSE;  
       LONG k;  
       for (k = 0; k < datacount; k++)  
       {  
          nullArray[k] = BaseObject::Alloc(Onull);  
          if (!nullArray[k])  
          {  
            error = TRUE;  
            break; //with Error  
          }  
       }  
       if (error) return FALSE;  
       for (k = 0; k < datacount; k++)  
       {  
          doc->InsertObject(nullArray[k]);  
       }  
    }
    

    Note that I'm doing the insertion separate from the allocation so that there won't be 'dangling' objects in the document that need to be removed in case of failure.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 24/10/2006 at 10:44, xxxxxxxx wrote:

    Thanks Robert! You're always a ton of help. I'll try this later tonight and see how it works out. Once again, Thanks.
    Josh



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 25/10/2006 at 04:25, xxxxxxxx wrote:

    One may be of topic question.
    Why to use bNew and bDelete if one can create simple template class that will manage all this stuff for you?
    DynamicArray from C4D SDK for example.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 25/10/2006 at 05:43, xxxxxxxx wrote:

    You mean GeDynamicArray. Remember that it is only available in R9.603+ (which would explain why I didn't mention it - any support for earlier versions will have to revert to the regular method or you'd have to design your own dynamic array template class). Plus, all of that work will only be useful if the array is definitely going to be 'dynamic' - overkill if it will remain the same size indefinitely.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 25/10/2006 at 12:00, xxxxxxxx wrote:

    That is why I have written "for example".
    The array do not need to be dynamic, it can be static too.
    If one simple and small template class is used for static arrays then the probability for memory leaks will be much smaller.
    Of course this is the question of programming ideology...



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 25/10/2006 at 15:03, xxxxxxxx wrote:

    Quote: Originally posted by Remotion on 25 October 2006
    >
    > * * *
    >
    > One may be of topic question.
    > Why to use bNew and bDelete if one can create simple template class that will manage all this stuff for you?
    > DynamicArray from C4D SDK for example.
    >
    >
    >
    > * * *

    yeah, that's probably the more elegant solution, hadn't thought about this.

    cheers,
    Matthias


Log in to reply