• Advertisement
Sign in to follow this  

Unity [C++] custom iterators

This topic is 3638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a 3D array representing Z, Y and X like
vector< vector< vector<T> > > myArray
where T is some type. Now if I form an iterator
vector<T>::iterator xIter
xIter = myArray[z][y].begin();
I can loop over the x dimension of the array. Now if I have a function that iterates over x performing some action (say, gaussian blur in 1D) I'd define it something like
doBlur(vector<T>::iterator xIterBegin, vector<T>::iterator xIterEnd);
which would in place blur along a line along the x-axis. Based on responses to this thread, I can define a templated function to take *any* iterator. So, with all this in mind, I can see how to write my 1D blur function using templates, allowing me to perform a blur with multiple calls with different iterators, as long as I can pass in iterators that iterate over the x, y, z coordinates as appropriate. What I can't see is what these iterators should be. Thinking about an iterator for the y axis, perhaps something like
vector< vector<T> >::iterator yIter
yIter = myArray[z].begin();
but this doesn't quite work as dereferencing the iterator gives me vector<T> not T, as I'd like. How do I write a custom iterator to give me this functionality? Do I have to write a custom iterator or is there a simpler, clever, way to do it? Many TIA

Share this post


Link to post
Share on other sites
Advertisement
Something you seem to have overlooked when considering y-axis iteration is that there are many 'columns' down which you could iterate.
What you're trying to do is actually disregarde the 'column-identifier', which turns out to be the x-component, and this just isn't practical.

Side note:
You might wonder why you can disregard the y (or even z) components for iterating along the x-axis, the answer is that this largely down to the way that you've set up your nested vectors such that the x-axis vector is the vector that actually contains the data.


So what can you do about this?
Well it turns out you have a few ways to solve this.

One way, which isn't entirely unreasonable I suppose, is to write custom iterators. This might make (more) sense if you wrapped those nested vectors into a new container class, call it CubeArray for example, then you can have a custom 3D iterator that you can access like normal: CubeArray<T>::iterator.

Writing iterators isn't hard, you just need to provide some technique to... well... iterate. Depending on the function that you provide your iterator can be a forwards iterator, backwards iterator, random access iterator etc.

If you're interested in writing STL compliant iterators then you need to adhere to their interface requirements. For example here you can find the details of a bi-directional iterator, if you provide a class with those functions then it's a fully compliant iterator.

However,
Other solutions certainly do exist; one way might be to condense your container down into a single vector:

vector<T> myArray

Note: You might actually gain some efficiency from a single vector, but that's not why I'm suggesting it.

You can still pretend this is a 3-dimensional array if you know the width, height and depth, although actually you don't need the depth for accessing elements:

myArray[(z * height + y) * width + x]

This now means that all mutable iterators will be of type vector<T>::iterator.

In order to allow the doBlur function to do its job it needs to be augmented with an offset argument; it must now also require that its iterators be random-access iterators (vector iterators are random access, so it's fine).

This offset argument (occasionally called the 'pitch', I think) is the offset from one element to the next.
So (if),
the x-axis components are contiguous, it's an offset of: 0,
the y-axis components are width distance apart, it's an offset of: width,
the z-axis components are width * height distance apart, it's an offset of: width * height.

To see this in action:

template <class RandomAccessIter>
void doBlur(RandomAccessIter xIterBegin, RandomAccessIter xIterEnd, int offset);

vector<T>::iterator yIterBegin = myArray.begin() + ((z1 * height + y1) * width + x1);
vector<T>::iterator yIterEnd = myArray.begin() + ((z2 * height + y2) * width + x2);
int offset = width;
doBlur(yIterBegin, yIterEnd, offset);


It would probably do to write a utility function to return the iterator at a given x, y, z position.

Share this post


Link to post
Share on other sites
Yes, there is a simpler way: use a more powerful (and more efficient) representation of the 3D array - boost::multi_array.

It holds things in a single allocated space (as far as I can tell from the documentation), and makes sure the array is "rectangular". Basically, it computes an index based on the dimensions and bounds of the array and indexes into a single vector. That avoids wasting lots of space (unused vector reserve capacity; redundant "per-row" vector object bookkeeping data).

It also provides a way to create "views" of parts of the array, in each dimension.


typedef boost::multi_array<Thing, 3> grid;
typedef typename grid::array_view<1>::type column;
grid myArray(boost::extents[5][5][5]); // or whatever size you wanted
typedef grid::index_range range;
grid::index_gen indices;
// Take 1D "core samples" in each direction, going through the middle of the cube.
column x22 = myArray[indices[range(0, 5)][2][2]];
column y22 = myArray[indices[2][range(0, 5)][2]];
column z22 = myArray[indices[2][2][range(0, 5)]];
// assuming we have template <typename Iterator> void blur(Iterator begin, Iterator end), blur each sample:
blur(x22.begin(), x22.end());
blur(y22.begin(), y22.end());
blur(z22.begin(), z22.end());



Reference.

Share this post


Link to post
Share on other sites
Of course boost::multi_array essentially does the same thing that I suggested, except it wraps everything up for you in a nice clean interface. [smile]

I'm not a huge user of the boost libraries so I tend to forget to recommend it's components. I'd suggest you go for the boost solution, unless, like me, you're prone to reinventing the wheel.

Share this post


Link to post
Share on other sites
I've done the "flatten 3D to 1D" thing many times, usually with bog standard arrays rather than vectors but the same thing. I'm not sure about the boost::multi_array being able to do exactly what I want but that's my fault for not being clear enough in my original post. Although the blur example describes my problem it also implied a "rectangular" array; this isn't the case. The number of elements in each vector can (and is) different depending on the dimensional coordinate: the first 2 'outer' vectors are rectangular but the last, innermost, vector can change in length.

Can boost::multi_array be formed to hold vector<T> objects with the
typedef typename grid::array_view<1>::type column
acting as my outer iterator? I think it should but am not sure - I've never used boost before. Also, what is the speed likely to be like compared to a custom iterator?

I'll also have a look at writing my own iterator as an intellectual exercise :)

Thanks for you help.

Share this post


Link to post
Share on other sites
Quote:
Original post by coordz
I've done the "flatten 3D to 1D" thing many times, usually with bog standard arrays rather than vectors but the same thing. I'm not sure about the boost::multi_array being able to do exactly what I want but that's my fault for not being clear enough in my original post. Although the blur example describes my problem it also implied a "rectangular" array; this isn't the case. The number of elements in each vector can (and is) different depending on the dimensional coordinate: the first 2 'outer' vectors are rectangular but the last, innermost, vector can change in length.


So, make a boost::multi_array<2, std::vector<T> >. No problem.

Except... what does it mean to iterate over Y and Z dimensions, then? Say I want to iterate over X = 3, Z = 1... what if I hit a vector (indexed by X) that doesn't have enough elements?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By bryandalo
      Good day,

      I just wanted to share our casual game that is available for android.

      Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.

      Download: https://play.google.com/store/apps/details?id=com.HellmodeGames.FlipEscape&hl=en
       
      Trailer: 
       
    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
  • Advertisement