Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > SDK Help
  New Posts New Posts
  FAQ FAQ  Forum Search

Generator too slow [SOLVED]

Author
Message
  Topic Search Topic Search
Rui Batista View Drop Down
Member
Member


Joined: 2002 Nov 30
Location: Portugal
Online Status: Offline
Posts: 1265
Direct Link To This Post Topic: Generator too slow [SOLVED]
    Posted: 2015 Apr 19 at 11:15am

User Information:

Cinema 4D Version:   15 
Platform:      Mac OSX  ; 
Language(s):     C++  ;   

---------

I created a generator object that generates a spline.
First I created it in python but, since it was resulting in a very slow generation of the spline (it is a very complex calculation), I decided to code it in C++.
But it is still too slow since it is calculation the spline everytime.
Is there any way to only re-generate the spline if the parameters were changed?
Back to Top
kuroyume0161 View Drop Down
Member
Member
Avatar

Joined: 2002 Oct 29
Location: United States
Online Status: Offline
Posts: 3665
Direct Link To This Post Posted: 2015 Apr 19 at 1:36pm
At least for GetVirtualObjects(), you can use GetAndCheckHierarchyClone() to check if the object needs to be recreated or not (if it is dirty or not).  If not, the returned clone can be returned.  Not sure if this applies with GetContour().

	// Check if needs rebuild and get cloned hierarchy of input objects
Bool dirty = FALSE;
mainop = op->GetAndCheckHierarchyClone(hh, child, HIERARCHYCLONEFLAGS_ASPOLY, &dirty, NULL, TRUE);
// - if !dirty then object is already cached and doesn't need to be rebuilt
if (!dirty) return mainop;

As for 'complex calculation', anything (and I literally mean *ANYTHING*) that can be done once or preprocessed should not be done every time you do this complex calculation.  I would need to see your calculation code in order to offer places to do this - but you should be able to see easily where calculations are invariant and could be done beforehand and then used to enhance speed and reduce complexity.  For instance, if you are dividing real numbers with a real number (say, Q) repeatedly, take the inverse of that real number (1/Q) and multiply.  Division is slower than multiplication in floats.  Avoid Sqrt() whenever possible - again, any calculation to a particular value that is being done more than once (esp. in loops) needs to be done outside of the loop or just once into a variable and reused.


Edited by kuroyume0161 - 2015 Apr 19 at 1:43pm
Back to Top
Rui Batista View Drop Down
Member
Member


Joined: 2002 Nov 30
Location: Portugal
Online Status: Offline
Posts: 1265
Direct Link To This Post Posted: 2015 Apr 19 at 3:37pm
I placed this in my code and it didn't work. It crashed Cinema 4D :-(

BaseObject* ArborSkeleton::GetVirtualObjects(BaseObject* op, HierarchyHelp* hh)
{

     Bool dirty=false;

     BaseObject* mainop=op->GetAndCheckHierarchyClone(hh, op, HIERARCHYCLONEFLAGS_ASPOLY, &dirty, NULL, true);
     if (!dirty)     return mainop;

     BaseDocument* doc=op->GetDocument();
     return (BaseObject*) Generate_Arbor(op,doc);
}
Back to Top
kuroyume0161 View Drop Down
Member
Member
Avatar

Joined: 2002 Oct 29
Location: United States
Online Status: Offline
Posts: 3665
Direct Link To This Post Posted: 2015 Apr 19 at 3:45pm
If your generated object is a spline, you should use HIERARCHCLONEFLAGS_ASSPLINE.  Also note that you pass the child of the generator object, not the generator itself (op):

	// Get first input object
BaseObject* child = op->GetDown();
if (!child) return NULL;

// Generate clones of input objects
// NOTE: 'mainop' is the original returned with the generated objects

// Check if needs rebuild and get cloned hierarchy of input objects
Bool dirty = FALSE;
BaseObject* mainop = op->GetAndCheckHierarchyClone(hh, child, HIERARCHYCLONEFLAGS_ASSPLINE, &dirty, NULL, TRUE);
// - if !dirty then object is already cached and doesn't need to be rebuilt
if (!dirty) return mainop;
if (!mainop) return NULL;


Edited by kuroyume0161 - 2015 Apr 19 at 3:46pm
Back to Top
Rui Batista View Drop Down
Member
Member


Joined: 2002 Nov 30
Location: Portugal
Online Status: Offline
Posts: 1265
Direct Link To This Post Posted: 2015 Apr 19 at 3:55pm
This object has no child. It is simply an object that generates a spline.
Back to Top
kuroyume0161 View Drop Down
Member
Member
Avatar

Joined: 2002 Oct 29
Location: United States
Online Status: Offline
Posts: 3665
Direct Link To This Post Posted: 2015 Apr 19 at 4:18pm
That ain't gonna work then. :(

You will need to do this first in GetVirtualObjects():

Bool dirty = op->CheckCache(hh) || op->IsDirty(DIRTYFLAGS_DATA);
if (!dirty) return op->GetCache(hh);

This should stop continuous rebuilding.

Back to Top
Rui Batista View Drop Down
Member
Member


Joined: 2002 Nov 30
Location: Portugal
Online Status: Offline
Posts: 1265
Direct Link To This Post Posted: 2015 Apr 19 at 11:58pm
YES!!! It worked just fine :-)
Now I just have to optimize my code, anyway :-)
Back to Top
S_Bach View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2011 Jun 27
Online Status: Offline
Posts: 1379
Direct Link To This Post Posted: 2015 Apr 20 at 6:52am
Hello,

I just want to add that an example on how to use the cache in GetVirtualObjects() can be found in the SDK Rounded Tube plugin.

If you want to create a Python ObjectData generator plugin you can take advantage of the optimized Cache, see “Optimize Cache”.

Best wishes,
Sebastian
SDK Support Engineer
Back to Top
Rui Batista View Drop Down
Member
Member


Joined: 2002 Nov 30
Location: Portugal
Online Status: Offline
Posts: 1265
Direct Link To This Post Posted: 2015 Apr 20 at 7:16am
Well, now that I have it working in C++ (it was already working in python), I guess I will keep on developing this one in C++.
And it is already caching quite well :-)
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.109 seconds.