Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Goodmorning,
I'm a bit rusty with regard to the recurrence in the classes, I have serched about this but I only find binary tree or list. Then, I ask here for help.

I have to create a class "cube".

Class cube has two member fuctions, "analyze", "inizialize". Has characterized by 8 points and a pointer to a list of 8 cubes objects (itself) that I call subcubes.

The functions are these:

1) Inizialize: provide to generate list of subcubes, started from 8 points and an integer that indicate level of tree. Take in input 8 points and an integer (that indicates the level of explosion of subcube) and give in output list of subcubes relative to 8 points in input.

2) Analyze: Verify if keep or remove the current cube. If (YES), this function will called recursively for all subcubes of the current cube. If (NO) current cube deleted from list.

Initially I have a list of 8 cube objects, on which will be launched the function analyze, those of interest eventually expanded into 8 subcubes, started from 8 point of current cube..and so on...

I draw into this scheme the situation, where P1, .., P8 are points, SC are subcubes and C are cubes (pratically cube abd subcube are the same object):

<img src='http://img401.imageshack.us/img401/6450/03u1.jpg' border='0'/>
Uploaded with ImageShack.us

How can I do this? Please help me!
Posted
Updated 22-Oct-13 22:20pm
v2

I don't know what exactly you are having problems with. I would declare the sub-cubes as a vector of pointers to cubes, for example:

C++
class Cube
{
    Initialize (int depth);
    ...
    vector<cube*> m_subCubes;
};

The Initialize function is called recursively, propagating depth - 1 to the next level. If depth is == 0 it returns immediately. Otherwise it creates the sub-cube objects and calls Initialize (depth - 1).

Same for your Analyze function.

Don't forget to delete the created sub-cubes in the destructor of your Cube class!
 
Share this answer
 
Comments
Domus1919 22-Oct-13 4:25am    
Thanks for your time..I understand..
nv3 22-Oct-13 5:19am    
You are welcome.
Domus1919 23-Oct-13 4:21am    
I have update my question, more clear situation...can you help me?
nv3 23-Oct-13 4:39am    
Help you with what exactly? Write the code? Or do you have a specific question. The general outline I described in my solution still applies.
Domus1919 23-Oct-13 4:43am    
Okay..in your opinion I can do this with that sintax...thanks...I have dubts, and then ask..
Your recursive data structure is a tree[^], and yes, you may use std::vector as 'array of objects'.
 
Share this answer
 
Comments
Domus1919 23-Oct-13 4:21am    
I have update my question, more clear situation...can you help me?
First of all, there is no need to characterize a cube with 8 points. This is 24 degrees of freedom, while all solid 3D bodies are defined by 6 degrees of freedom, for a given shape (do I even have to explain that?) and, to define the shape of the cube, you need one more degree of freedom, its size. So, 7 scalar parameters fully define the size, orientation and location of a cube in 3D. You model is wrong (excessive) in the very beginning.

Now, why do you need a "sub-cube" class. If the semantic of this object is the cube, it should have the same class, Cube. If the "sub-cube" is not a cube, you should not call it a "cube". Again, wrong element of your model.

So, before discussing your method and "recursion" (what is "recursive object", by the way? I can imagine something like that, but you have to define it), before containers of class instances, you need to fix your model. And then you will need to explain your concerns properly.

—SA
 
Share this answer
 
Comments
Domus1919 22-Oct-13 3:34am    
Subcube it's Cube class, the same of Cube, but intern at the actual cube. I need to define 8 points because I'm work on points, not with cubes...cube it's the astraction...I need to create a class Cube defined by 8 points and that contain a pointer to itself..the recursion it's that...or not? I ask for a help here...because I don't understand how do this...
Sergey Alexandrovich Kryukov 22-Oct-13 11:14am    
Okay, you confirmed again that your approach is inconsistent. You cannot work on points, because they don't truly define a cube. For example, if you got those eight points, they may be not the cube apexes, so it may need a check up, but floating-point numbers are approximate, so how? From the other hand, if your get, say, 7 points, you can always find out the 8-th one. This approach is inconsistent.

And it's enough to use the same class for cube and sub-cube. You need to create a derived class only when you need to add some members or override some methods. Consider this: any, absolutely any instance of Cube can play the role of sub-cube.

So, again, instead of protecting your model (how can it be helpful?) fix it.

—SA
Domus1919 22-Oct-13 11:30am    
Sergey, I understand your opinion, but you don't understand me. I need to use point because I work with point (use it for backproject on a binary image and verify if point is in or out silhouette) than, "cube" it's only an astraction to identify a zone of the 3d space that I investigate. Initial cube are the full space, that I divide in 8 subcube and exam their vertices: if are intersting, I create 8 sub-subcubes in that zone.

Sorry if I don't tell this before. Now, after I explain this situation, why my approch it's inconsistent? It's wrong operate in this way? (Piramid way).
Sergey Alexandrovich Kryukov 22-Oct-13 11:45am    
I'm not sure if you understand. I'm not saying you should not use those 8 points. I say that you should not use them to define the cube, you should calculate them from initialization data of the cube. That is exactly because the cube is the abstraction to identify a zone of the 3D space, as you put it so accurately and correctly. And you only confirmed the idea that the "Subcube" should be exact same type as "Cube". If you create an instance with different initialization parameters (smaller, at different location), it does not make it a different type. Hopefully you can see the problem with your approach now.
—SA
Domus1919 23-Oct-13 4:21am    
I have update my question now, more clear situation...can you help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900