/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: trop-2d-primitives.h 9298 2009-09-02 23:10:56Z gawrilow $
*/

#ifndef _POLYMAKE_TROP_2D_PRIMITIVES_H
#define _POLYMAKE_TROP_2D_PRIMITIVES_H

#include <Rational.h>
#include <Vector.h>
#include <Matrix.h>
#include <Set.h>

using namespace polymake;

void tropically_dehomogenize(Matrix<Rational>& M) {
   const int d(M.cols());
   for (int i=M.rows()-1; i>=0; --i) {
      for (int j=d-1; j>=0; --j)
      M(i,j)-=M(i,0);
   }
}

// for the following primitive it is assumed that the vectors are 2d
int right(const Vector<Rational>& a, const Vector<Rational>& b) { // a is right of b == 1
   return sign(a[0]-b[0]);
}

int y_above(const Vector<Rational>& a, const Vector<Rational>& b) { // a has higher y-coordinate than b == 1
   return sign(a[1]-b[1]);
}

int ray_above(const Vector<Rational>& a, const Vector<Rational>& b) { // a has higher (1,1)-ray than b == 1
   return sign(a[1]-a[0]+b[0]-b[1]);
}

int segment_below(const Vector<Rational>& a, const Vector<Rational>& b, const Vector<Rational>& c) {
   // b and c both right of a, line segment from a to b below line segment from a to c == 1
   int ba_ray_above=ray_above(b,a), ca_ray_above=ray_above(c,a);
   if (ba_ray_above>=0 && ca_ray_above>=0) {
      int bc_right=right(b,c);
      if (bc_right==0)
	 return y_above(b,c);
      else
	 return bc_right;
   }
   if (ba_ray_above<0 && ca_ray_above<0) {
      int bc_y_above=y_above(b,c);
      if (bc_y_above==0)
	 return right(b,c);
      else
	 return bc_y_above;
   }
   if (ba_ray_above>=0)
      return -1;
   else
      return 1;
}

int segment_above(const Vector<Rational>& a, const Vector<Rational>& b, const Vector<Rational>& c) {
   // b and c both left of a, line segment from a to b above line segment from a to c == 1
   int ba_y_above=y_above(b,a), ca_y_above=y_above(c,a);
   if (ba_y_above>=0 && ca_y_above>=0) {
      int bc_right=right(b,c);
      if (bc_right==0)
	 return y_above(b,c);
      else
	 return bc_right;
   }
   if (ba_y_above<0 && ca_y_above<0) {
      int bc_ray_above=ray_above(b,c);
      if (bc_ray_above==0)
	 return -right(b,c);
      else
	 return bc_ray_above;
   }
   if (ba_y_above>=0)
      return 1;
   else
      return -1;
}

#endif // _POLYMAKE_TROP_2D_PRIMITIVES_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
