Trace image to Edge Shapes, and terrain generation

Well I got some values. The first shape doesn't seem to be empty though the vertexCount says there are 192 elements in it(Though the image is only 32x32 pixels and not very complex). Also it doesn't give me the dereferenceable iterator until the tenth iteration.
Sorry for the double post. I was thinking and maybe it would be better if I tried to make my own implementation of this. I went through your code though and couldn't understand much of it. Could you give me a few guidelines/an outline of what I should be looking into/investigating/figuring out to put together something like this?


Thanks.
Making your own implementation is not a bad idea. However, I would strongly suggest at least keeping the core of the tracing algorithm from my code, because it's quite detailed and took me some time to get right. I'd recommend rewriting things like the parameters to the functions, the way the list of shapes is returned, the vertex-culling routine, and perhaps (very carefully) cleaning up the core algorithm.

I still don't know what's happening in your current code, although I had mistakenly thought your error was a compile-time error, not a run-time one. Perhaps some careful debugging will lead you to the problem, and it might even be an easy fix. If you #define DEBUG at the top of the edge-tracing routine's header, it will print out a bunch of debug data as it runs, which should help you track down any issues inside that routine; although your problem may lie elsewhere. Perhaps using a very simple test image would be a good idea too, say a white rectangle on black.
I tried with a simple square and it still gave me a huge vertex count (-11000 or so). I tried with #define DEBUG on top of the traceImage header but nothing seemed to change on my output log(I use MVS 2008). I'll rewrite the stuff you suggested and let's see what happens.
I'd like to export the shapes and vertices that this code generates into a file so I could port it to AS3 and use it as a level.

I need to record the data from the generation algorithm as it generates the level each frame using some form of lists, and then output that data back into a txt file when the user presses 'e'? Anyone have suggestions on a good way to do this?
Alright; I've written some code that I think may export all of the shapes and vertices (reusing the same staticBody variables) and opened the TerrainTracer source-code and Box2D-source code in Visual C++ 2008 Express.

In TerrainTracer.h under the traceMyImage function
Code:
   if(write = true)
   {
   ofstream out; //declare out as variable that holds text file   
   out.open("sample_terrain.txt");

   //Should only be written once   
   out << "var staticBodyDef:b2BodyDef = new b2BodyDef();" << endl;
   out << "var staticBodyShape:b2PolygonDef = new b2PolygonDef();" << endl;
   out << "staticBodyShape.friction = 0.5;" << endl;
   out << "staticBodyShape.restitution = 0.3;" << endl;
   out << "staticBodyShape.density = 0.0;" << endl;
   
   out.close(); //close text file

      for(unsigned int i=0;i<shapes.size();++i){
         writeVertices(shapes[i]);
      }
   }


New function in ImageTracer.h
Code:
void writeVertices(Shape& shape)
{
   ofstream out;
   out.open("sample_terrain.txt");
   out << endl << "staticBodyDef.position.Set(" << shape.x << " / RATIO , " << shape.y << " / RATIO );" << endl; //write x/y positions

   VertexIterator it=shape.begin();
   
   int a = 0;
   while(true){
      out << "b2Vec2(staticBodyShape.vertices[" << a << "]).Set( " << it->pos[0] << " / RATIO, " << it->pos[1] << " / RATIO);" << endl;
      if( it == shape.end() ) {
         break;
      }
      ++a;
      ++it;
   }
   
   out << "var staticBody = _world.CreateBody( staticBodyDef );" << endl;
   out << "staticBody.createShape( staticBodyShape );" << endl;
   out << "staticBody.setMassFromShapes();" << endl;
   out.close();
}


I've included fstream in both ImageTracer.h and TerrainTracer.h
I can compile the base testbed from the Box2D source code and it runs just fine, but when I compile the TerrainTracer.h and ImageTracer.h I get 29 errors and 13 warnings. All of the errors and warnings are listed below.
Code:
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(148) : error C2220: warning treated as error - no 'object' file generated
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(148) : warning C4127: conditional expression is constant
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(214) : error C2668: 'fabs' : ambiguous call to overloaded function
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(557): could be 'long double fabs(long double)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(509): or       'float fabs(float)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(119): or       'double fabs(double)'
4>        while trying to match the argument list '(int)'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(215) : error C2668: 'fabs' : ambiguous call to overloaded function
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(557): could be 'long double fabs(long double)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(509): or       'float fabs(float)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(119): or       'double fabs(double)'
4>        while trying to match the argument list '(int)'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(216) : error C2668: 'fabs' : ambiguous call to overloaded function
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(557): could be 'long double fabs(long double)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(509): or       'float fabs(float)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(119): or       'double fabs(double)'
4>        while trying to match the argument list '(int)'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(217) : error C2668: 'fabs' : ambiguous call to overloaded function
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(557): could be 'long double fabs(long double)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(509): or       'float fabs(float)'
4>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\math.h(119): or       'double fabs(double)'
4>        while trying to match the argument list '(int)'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(373) : warning C4127: conditional expression is constant
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(537) : warning C4127: conditional expression is constant
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(586) : error C2039: 'x' : is not a member of 'std::list<_Ty>'
4>        with
4>        [
4>            _Ty=Vertex
4>        ]
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(586) : error C2039: 'y' : is not a member of 'std::list<_Ty>'
4>        with
4>        [
4>            _Ty=Vertex
4>        ]
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(591) : warning C4127: conditional expression is constant
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/tga_image.h(135) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/tga_image.h(148) : warning C4244: '=' : conversion from 'int' to 'unsigned char', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/tga_image.h(150) : warning C4244: '=' : conversion from 'int' to 'unsigned char', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/tga_image.h(154) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/tga_image.h(189) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(103) : error C3861: 'clock': identifier not found
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(183) : warning C4267: '+=' : conversion from 'size_t' to 'int', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(228) : error C2065: 'b2EdgeChainDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(228) : error C2146: syntax error : missing ';' before identifier 'edgeDef'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(228) : error C2065: 'edgeDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(229) : error C2065: 'edgeDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(229) : error C2228: left of '.isALoop' must have class/struct/union
4>        type is ''unknown-type''
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(233) : warning C4244: 'argument' : conversion from 'double' to 'float32', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(233) : warning C4244: 'argument' : conversion from 'double' to 'float32', possible loss of data
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(238) : error C2065: 'edgeDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(238) : error C2228: left of '.vertexCount' must have class/struct/union
4>        type is ''unknown-type''
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(239) : error C2065: 'edgeDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(239) : error C2228: left of '.vertices' must have class/struct/union
4>        type is ''unknown-type''
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(241) : error C2065: 'edgeDef' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(289) : error C2065: 'b2EdgeShape' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(289) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(289) : error C2065: 'b2EdgeShape' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(289) : error C2059: syntax error : ')'
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(290) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(291) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(291) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(291) : error C2227: left of '->GetNextEdge' must point to class/struct/union/generic type
4>        type is ''unknown-type''
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(292) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(292) : error C2065: 'edge' : undeclared identifier
4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer.h(292) : fatal error C1903: unable to recover from previous error(s); stopping compilation


I assumed the reason it can't find the edge, edgeDef, b2EdgeShape was because Box2D wasn't imported into the file, so I added #include "Box2D.h" to both ImageTracer.h and TerrainTracer.h, but it didn't appear to have any impact. Are the errors related to this one: "error C2220: warning treated as error - no 'object' file generated"?

As for the error "4>c:\temp\box2d_v2.0.1\box2d_v2.0.1\box2d\examples\testbed\tests\TerrainTracer/ImageTracer.h(586) : error C2039: 'x' : is not a member of 'std::list<_Ty>'", I'm still trying to figure out how to get the X and Y variables from the shape, but I don't believe it's related to all the other errors in the base source code.

Anyone recognize the problem here?
A few of those warnings are just type-conversion issues in the tga loader that you can either ignore or rectify easily.
The fabs( ) errors appear to be my fault. They should be replaced with the integer version abs( ), as the argument being passed is an integer type.
As for the Box2D-related errors, are you using the SVN version of Box2D? I don't think Edge Shapes are in the release version yet, so you must get the latest code from the SVN repository.
I've only had a quick look, but the rest of the problems seem to stem from your attempts to access the vertex data of a shape. There are two ways you can approach this, either getting the data from the structures returned by the traceImage( ) function (which are completely independant of Box2D), or generating Box2D Edge Shapes and then getting the data from them. Whichever method you use, just check the corresponding structures and/or accessor functions for how to get at the data you need.
I got the latest SVN release and that fixed a lot of the errors. Changing fabs() to abs() fixed some more.

I don't believe I understand the shape declaration; I see the Vertex struct in ImageTracer.h holds two elements: the x and y position of each vertice in the shape. However, when recreating the shape, you have to specify a position for it in the world. If the vertices held in the Vertex struct are the literal x and y coordinates of the shape, then can I just do "staticBodyDef.position.Set(" 0 / RATIO , 0 / RATIO );" and the shapes will be created in the correct position because of the vertices not being declared from zero (meaning their current values contain the "offsets" from 0,0)?

Changing the "out << endl << "staticBodyDef.position.Set(" << shape.x << " / RATIO , " << shape.y << " / RATIO );" << endl;" to zeroes in place of shape.x and shape.y solved two more errors.

There are two left that I'm unsure of how to solve.
Code:
1>c:\box2d_svn_trunk\examples\testbed\tests\TerrainTracer/ImageTracer.h(148) : error C2220: warning treated as error - no 'object' file generated
1>c:\box2d_svn_trunk\examples\testbed\tests\TerrainTracer.h(103) : error C3861: 'clock': identifier not found


The clock error can be solved with the right import, correct? Do you know what library I would need for it?

I think the first "warning as error" can be ignored if I find the option in VC++2008 to disable it.

*Edit: "warning as error" option disabled, commented out fuction that calls "clock()".
Problem now arises when I go to terraintracer and attempt to load.

Code:
Debug Assertion Failed!

\Microsoft Visual Studio 9.0\VC\include\list
Line: 270

Expression: list iterator not decrementable

Looking for the cause of the error and how to fix it. I assume it's functionality that was removed with Visual C++ 2008 or perhaps never on this compiler. The image still loads and the code I added isn't being called yet, so I assume it's probably in the traceImage functions.
Hey bb_vb could I add you to instant messenger?
Sorry I didn't see your earlier reply. The "list iterator not decrementable" error is coming from the vertex culling routine I think. The problem is that in that function, I use the list (in a funky/hackish way) by assuming it's circular, which is the case under GCC, but is not the case in MSVC (so I just read on the 'net). Your only real option in this case is to not use that function (as I mention in my first post, it's a very hacked-up function as it is) and either do without that functionality or re-write it yourself in a better way. In terms of results, the vertex-culling effect is almost emulated (if not better) by using the technique of shrinking your source image in order to get a smoother/lower-detail trace, so perhaps just rely on that instead.

You can add me on MSN, although I basically don't use it any more. A better bet would just be to email me: bb_vb at hotmail dot com. That said, I have been known to be unresponsive there too, but I'll do my best 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=2169