azrafe7 / hxGeomAlgo

Small collection of computational geometry algorithms in Haxe.
http://azrafe7.github.io/hxGeomAlgo/index.html
Other
159 stars 13 forks source link

compiler error #8

Closed sdrabb closed 9 years ago

sdrabb commented 9 years ago

hi i'm trying to convert to javascript you're work but it does'nt work. could you help me please?

here's my error: Invalid commandline class : SnoeyinkKeil should be hxGeomAlgo.SnoeyinkKeil ./SnoeyinkKeil.hx:23: characters 7-27 : Class not found : hxGeomAlgo.PolyTools

anyone could help me to compile the hxGeomAlgo in javascript please?

azrafe7 commented 9 years ago

I can compile here without issues for js too. If you can share more info about your process maybe we can find a solution:

In the meanwhile, try adding --macro include('hxGeomAlgo') at the end of your command line and see if it works.

sdrabb commented 9 years ago

thanks for you're answer.

to compile i wrote a file compile.hxml in hxGeomAlgo folder, that containing these 2 line:

-main SnoeyinkKeil -js test.js

and i compile with ubuntu terminal using that commands:

haxe compile.hxml

it's my procedure correct?

azrafe7 commented 9 years ago

it's my procedure correct?

Not exactly...

Put this Main.hx file (which is a simple test for a star-shaped polygon) in the src folder:

package;

import hxGeomAlgo.HxPoint;
import hxGeomAlgo.SnoeyinkKeil;

using hxGeomAlgo.PolyTools;

class Main {
    static function main():Void {
        var polyCoords:Array<Float> = [96, 3, 54, 41, 2, 7, 32, 64, 5, 121, 9, 120, 58, 82, 112, 115, 74, 61, 97, 3];
        var poly = polyCoords.toPointArray();

        var decomposition = SnoeyinkKeil.decomposePoly(poly);
        for (p in decomposition) {
            trace(p);
        #if js
            js.Browser.document.writeln('<tt>' + Std.string(p) + '</tt></br>');
        #end
        }
    }
}

Then run: haxe -cp src -main Main -js test.js

Done!

If you're still beginning with haxe this might seem a bit too much (but really you'll find it very simple once you get accustomed to it). So let's start with installing haxe 3.2, and let me know if you get stuck at any point.

sdrabb commented 9 years ago

i do youre procedure but i get that error:

hxGeomAlgo/SnoeyinkKeil.hx:142: characters 9-59 : Variable initialization must be a constant value hxGeomAlgo/SnoeyinkKeil.hx:144: characters 9-52 : Variable initialization must be a constant value

azrafe7 commented 9 years ago

That's because you're using an old version of the compiler, and you need haxe 3.2 or newer to use the lib.

sdrabb commented 9 years ago

you're right thank's, how i can delete the old one you know?

azrafe7 commented 9 years ago

It's probably safe to just remove the haxe folder and then run the installer script (which should set up the correct env variables), but I don't know for sure.

sdrabb commented 9 years ago

Thanks i've another question it's possibile embed You're library with my javascript program?

Il venerdì 9 ottobre 2015, Giuseppe Di Mauro notifications@github.com ha scritto:

It's probably safe to just remove the haxe folder and then run the installer script (which should set up the correct env variables), but I don't know for sure.

— Reply to this email directly or view it on GitHub https://github.com/azrafe7/hxGeomAlgo/issues/8#issuecomment-146982400.

sdrabb commented 9 years ago

Ok , i'ge got another question i've a javascrip

Il venerdì 9 ottobre 2015, Giuseppe Di Mauro <notifications@github.com javascript:_e(%7B%7D,'cvml','notifications@github.com');> ha scritto:

It's probably safe to just remove the haxe folder and then run the installer script (which should set up the correct env variables), but I don't know for sure.

— Reply to this email directly or view it on GitHub https://github.com/azrafe7/hxGeomAlgo/issues/8#issuecomment-146982400.

azrafe7 commented 9 years ago

Yes, it's definitely possible to use from a pure js side. Let me cook up an example...

Also, I've edited my code snippet above!

sdrabb commented 9 years ago

That's great You're very kind thanks! i wait You're example

azrafe7 commented 9 years ago

Hey @sdrabb, see this gist (https://gist.github.com/azrafe7/7fc1214cae8c4fb18597). It contains three files

It's very basic, and you'll probably need to inspect the hxGeomAlgo var in the inspector from time to time to better understand how the functions are exposed. But it should give you a rough idea of how to use it.

Let me know if you have any problems.

sdrabb commented 9 years ago

thank's a lot... i have compiled your library updating haxe to 3.2.0..

now i'll try your example, if i've a problem I will let you know.

at the moment i've tryied your keil algorith on my map editor and it worked great! :+1:

azrafe7 commented 9 years ago

Closing this now, as the original problem has been fixed (older compiler).

Feel free to open another one if you have issues with the lib, or need help.

sdrabb commented 9 years ago

Ok very kind thank

Il sabato 10 ottobre 2015, Giuseppe Di Mauro notifications@github.com ha scritto:

Closing this now, as the original problem has been fixed (older compiler).

Feel free to open another one if you have issues with the lib, or need help.

— Reply to this email directly or view it on GitHub https://github.com/azrafe7/hxGeomAlgo/issues/8#issuecomment-147125987.

sdrabb commented 9 years ago

what are the commands to compile alla the library like your example?

azrafe7 commented 9 years ago

Something like:

haxe -cp src -js bin/hxGeomAlgo.js --macro include('hxGeomAlgo')

but notice that currently you need to add @:expose on top of the classes you want to be visible from the js side (see http://haxe.org/manual/target-javascript-expose.html). For example open SnoeinkKeil.hx and change the class declaration to

@:expose
class SnoeyinkKeil {

(I'm in the middle of adding this, but have not yet figured out how to do it for enums, so have not pushed it yet to github.)

sdrabb commented 9 years ago

why sometimes when i put the coordinate on decompoly function i get that error?

here my function:

     function generateConvexArea(points){
    var roomPoints = points.slice();
   var polyCoords = [];

   for(i=0; i < roomPoints.length ; i++){

  polyCoords.push(roomPoints[i].x);
  polyCoords.push(roomPoints[i].y);

 }

  var hxGeomAlgo = window.hxGeomAlgo;

  var poly = hxGeomAlgo.PolyTools.toPointArray(polyCoords);
  var decomposition = hxGeomAlgo.SnoeyinkKeil.decomposePoly(poly);

   return JSON.stringify(decomposition);

  }

ehere the error: Uncaught TypeError: Cannot read property 'y' of undefined.

then sometime it work why?

i solve it using https://mpen.ca/406/bayazit

changing my function to :+1:

  function generateConvexArea(points){
  var roomPoints = points.slice();
  var polyCoords = [];

  for(i=0; i < roomPoints.length ; i++){

   polyCoords.push(roomPoints[i].x);
   polyCoords.push(roomPoints[i].y);

  }

 var hxGeomAlgo = window.hxGeomAlgo;

  var poly = hxGeomAlgo.PolyTools.toPointArray(polyCoords);
 var decomposition = hxGeomAlgo.Bayazit.decomposePoly(poly);

    return JSON.stringify(decomposition);

 }
azrafe7 commented 9 years ago

Can you open the console (F12 on Chrome and Firefox) and report where the error is happening?

In general it means that the examined point is null, but I'd need a full reproducible test to investigate further, if you could provide it I will take a look.

azrafe7 commented 9 years ago

Also, if you can share just the coords of the poly which is not working with SnoeyinkKeil, maybe I can reproduce with that.

sdrabb commented 9 years ago

the line of error is:

hxGeomAlgo.js:1617 Uncaught TypeError: Cannot read property 'y' of undefined

hxGeomAlgo_PolyTools.isLeft = function(p,a,b) {
   return (a.x - p.x) * (b.y - p.y) - (b.x - p.x) * (a.y - p.y) > 0;   <-------- that line
 };

the coords of the polygon are:

[32, 14, 36, 14, 36, 20, 44, 20, 44, 10, 42, 8, 32, 8, 32, 10, 32, 12]

hxGeomAlgo.js:1617 Uncaught TypeError: Cannot read property 'y' of undefined

the same poly with Bayazit work.

sdrabb commented 9 years ago

And i've got another question: Once i've decomposed my polygon is there a function in the library to get the 'Convex CUT' that the algorithm have generated?

azrafe7 commented 9 years ago

Your poly seems to work fine with haxe, but maybe I'm missing something. Still investigating...

Anyway there could be issues with collinear or duplicate points. You can try running your poly through RamerDouglasPeucker/VisvalingamWhyatt simplification function before decomposing it?

As in:

    var poly = hxGeomAlgo.PolyTools.toPointArray(polyCoords);
    poly = hxGeomAlgo.RamerDouglasPeucker.simplify(poly);

    var decomposition = hxGeomAlgo.SnoeyinkKeil.decomposePoly(poly);

and it should work (hopefully).

Some of the algos in the lib are very picky about the input, so the user has to clean it up before passing it around.

About the convex cut: that's exactly what SnoewyinkKeil/Bayazit decomposition do, return an array of convex polygons (actually Array of Array of HxPoint). So you should be good to go.

Mind to share your use case? (as maybe there's some shortcut I could advice you to take)

sdrabb commented 9 years ago

with your suggest work thank's!

usecase

i'm build a map and i need that's cut and convexArea of the sigle room that in my program is a polygon.

azrafe7 commented 9 years ago

Ah, so it was about collinear points in the end!?

Can you post the coords of that image (including the collinear points)? (I could figure them out - i know -, but if you already have them... ;)

azrafe7 commented 9 years ago

Ok, the poly is more or less [1,2,5,2,5,1,8,1,11,1,11,3,15,3,15,6,8,6,1,6] (with some scale applied).

But then the convex decomposition could very well be something like unwanted_decomp

In your specific case I think you need to come up with a way to separate rooms, and then apply the decomposition.

sdrabb commented 9 years ago

thank's a lot the rooms are separated yet... :+1:

sdrabb commented 9 years ago

i ask you only if in your library there is a function that return the cut

azrafe7 commented 9 years ago

Sorry I don't fully understand you. What you're meaning with "the cut"?

sdrabb commented 9 years ago

with cut i mean the 2 point that the algorithm use to divide the polygon in convex area

azrafe7 commented 9 years ago

So you mean splitting a polygon with a line and returning the separated shapes/polys?!

In that case: no, sorry, nothing like that in the lib at the moment (maybe in the future - I don't know). Perhaps you can try to look into nape (http://napephys.com/), which I think is really robust and has a cut function too (along with many poly decomposition methods), or try to roll your own algorithm taking ideas from stackoverflow and the like.

azrafe7 commented 9 years ago

Well, maybe using the Tess2 package and boolean ops would not be too hard to achieve something like that, but it really comes up to your specific use case and if you can work around it without some unnecessary computations.

(Meaning: maybe storing the rooms in a different way would simplify things from the beginning, no?! :stuck_out_tongue_winking_eye:)

azrafe7 commented 9 years ago

Thinking about it, should be pretty easy to cut a polygon by using Tess2.difference(). The only thing to watch out for is that you have to slightly expand the cutting line making it into a poly.

So if you want to cut using the line [0, 50, 150, 50], you actually pass in something like [0, 50, 150, 50, 150, 50.1, 0, 50.1].

sdrabb commented 9 years ago

thank's... there is a via to consider the collinear points with SnoeyinkKeil?

Bayazit consider that points or i mistake?

in short i want that in my convexarea arrays there are the green points that are collinear:

schermata del 2015-10-17 19 14 20

it's possible?

azrafe7 commented 9 years ago

Since collinear points don't contribute to the simplification you can remove them from the input and add them back to the output, no?!