Ejemplo n.º 1
0
Archivo: frustum.cpp Proyecto: hglm/sre
void sreScissors::UpdateWithProjectedPoint(float x, float y, double z) {
    if (z >= - 1.001d) {
        // Beyond the near plane.
        z = maxd(- 1.0d, z);
        double depth = 0.5d * z + 0.5d;
        near = mind(depth, near);
        far = maxd(depth, far);
        left = minf(x, left);
        right = maxf(x, right);
        bottom = minf(y, bottom);
        top = maxf(y, top);
        return;
    }
    sreMessage(SRE_MESSAGE_WARNING,
        "Unexpected vertex in front of the near plane in UpdateWorldSpaceBoundingHull "
        "z = %lf", z);
    // In front of the near plane.
    near = 0;
    far = 1.0;
    // We know that the light volume intersects the frustum, so it must extend to
    // both sides of the near plane.
    // Assume it fills the whole viewport (not optimal).
    left = - 1.0f;
    right = 1.0f;
    bottom = - 1.0f;
    top = 1.0f;
}
Ejemplo n.º 2
0
bool UManArc::getSMRCLcmd(char * buf, int bufCnt, double maxDist)
{
  int i, n, m;
  double d, dd, da;
  char * p1;
  bool result = true;
  double v;
  double dist;
  //
  // turn is not allowed at zero speed
  if (fabs(vel) < 0.2)
    v = 0.25;
  else
    v = vel;
  //
  if (radius < 3.0)
  {
    dist = mind(maxDist, getDistance());
    snprintf(buf, bufCnt, "turnr %.3f %.3f @v%.2f @a%.3f :($drivendist > %.2f)",
             maxd(radius, 0.15), // use at least 15cm turn radius
             angle * 180.0 / M_PI, v,
             maxd(getMinAcc(), fabs(acc)), dist);
    n = strlen(buf);
  }
  else
  { // it is too unsafe to turn and finish on the implicit angle criteria
    // divide into 30 cm steps
    m = maxi(1, roundi(fabs(angle) / 0.05)); // about 3 deg steps
    d = radius * angle;
    dd = d / double(m);
    da = angle / double(m);
    n = 0;
    p1 = buf;
    dist = mind(maxDist, getDistance());
    for (i = 0; i < m; i++)
    { // one command takes a little less than 60 characters.
      // so there need to be at least 60 characters left in buffer
      result = ((bufCnt - n) > 60);
      if (not result)
      { // no more space for next command
        printf("*** UManArc::getSMRCLcmd: no space left (%d left) in buffer (size %d) for next drive command\n", bufCnt, n);
        break;
      }
      // space for next command line
      snprintf(p1, bufCnt - n, "turnr %.3f %.3f \"rad\" @v%.2f @a%.3f :($drivendist > %.2f)\n",
               radius, da, v, maxd(getMinAcc(), fabs(acc)), dd);
      n += strlen(p1);
      p1 = &buf[n];
      dist -= dd;
      if (dist < 0.0)
        break;
    }
    // remove laset '\n'
    if (n > 0)
      buf[n-1] = '\0';
  }
  return (n < bufCnt);
}
Ejemplo n.º 3
0
inline double range( double a, double b ) {
    long r = random();
    double rate = (double)r / (double)(0x7fffffff);
    double _a = mind(a,b);
    double _b = maxd(a,b);
    return _a + (_b-_a)*rate;
}
Ejemplo n.º 4
0
/*Sends a 8-byte credit back to the torus node LP that sent the message */
static void credit_send( nodes_state * s,
                         tw_bf * bf,
                         tw_lp * lp,
                         nodes_message * msg)
{
#if DEBUG
    //printf("\n (%lf) sending credit tmp_dir %d tmp_dim %d %lf ", tw_now(lp), msg->source_direction, msg->source_dim, s->params->credit_delay );
#endif
    bf->c1 = 0;
    tw_event * buf_e;
    nodes_message *m;
    tw_stime ts;
    int src_dir = msg->source_direction;
    int src_dim = msg->source_dim;

    msg->saved_available_time = s->next_credit_available_time[(2 * src_dim) + src_dir][0];
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] = maxd(s->next_credit_available_time[(2 * src_dim) + src_dir][0], tw_now(lp));
    ts =  s->params->credit_delay +
          tw_rand_exponential(lp->rng, s->params->credit_delay/1000);
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] += ts;

    //buf_e = tw_event_new( msg->sender_lp, s->next_credit_available_time[(2 * src_dim) + src_dir][0] - tw_now(lp), lp);
    //m = tw_event_data(buf_e);
    buf_e = model_net_method_event_new(msg->sender_node,
                                       s->next_credit_available_time[(2*src_dim) + src_dir][0] - tw_now(lp),
                                       lp, TORUS, (void**)&m, NULL);
    m->source_direction = msg->source_direction;
    m->source_dim = msg->source_dim;

    m->type = CREDIT;
    tw_event_send( buf_e );
}
Ejemplo n.º 5
0
void gline(float x1, float y1, float x2, float y2, float width, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
	#ifdef	OPENGL
		glColor4f(((float) r) / 255.0, ((float) g) / 255.0, ((float) b) / 255.0, ((float) a) / 255.0);
		//   c1x,c1y
		//  0,0......
		//   c4x,c4y  ........       c2x,c2y
		//                    ........ px,py
		//                       c3x,c3y
		float c1x, c1y, c1l, c2x, c2y, c2l, c3x, c3y, c3l, c4x, c4y, c4l;

		float px = x2 - x1;
		float py = y2 - y1;

		c1x = -py;
		c1y = px;
		c1l = hypotf(c1x, c1y);
		c1x = (c1x * width / c1l / 2.0) + x1;
		c1y = (c1y * width / c1l / 2.0) + y1;

		c2x = -py;
		c2y = px;
		c2l = hypotf(c2x, c2y);
		c2x = (c2x * width / c2l / 2.0) + px + x1;
		c2y = (c2y * width / c2l / 2.0) + py + y1;

		c3x = py;
		c3y = -px;
		c3l = hypotf(c3x, c3y);
		c3x = (c3x * width / c3l / 2.0) + px + x1;
		c3y = (c3y * width / c3l / 2.0) + py + y1;

		c4x = py;
		c4y = -px;
		c4l = hypotf(c4x, c4y);
		c4x = (c4x * width / c4l / 2.0) + x1;
		c4y = (c4y * width / c4l / 2.0) + y1;

		if (width == 4.0)
			printf("LINE: [%3.0f,%3.0f]->[%3.0f,%3.0f]->[%3.0f,%3.0f]->[%3.0f,%3.0f]\n", c1x, c1y, c2x, c2y, c3x, c3y, c4x, c4y);

		glVertex2f(c1x, c1y);
		glVertex2f(c2x, c2y);
		glVertex2f(c3x, c3y);
		glVertex2f(c4x, c4y);
	#else
	thickLineRGBA(Surf_Display,
		(x1 - viewPortL) * zoomFactor,
		(viewPortB - y1) * zoomFactor,
		(x2 - viewPortL) * zoomFactor,
		(viewPortB - y2) * zoomFactor,
		mind(maxd(width * zoomFactor, 1), 2),
		r, g, b, a
		);
	#endif
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
{
	FILE *stream = NULL;
	int i, j, r, count;
	bool flag;

	Matrix matrix = NULL;
	Vector rhs = NULL;
	VectorArray Lattice = NULL;
	Vector vector = NULL;

	LinearSystem initialsystem;
	ZSolveContext ctx;

	char *token;
	int memory;

	getopts(argc, argv);

	puts(FORTY_TWO_BANNER);

	if (OResume)
	{
		// START OF RESUME SECTION - READ FILES AND CREATE CONTEXT 

		strcat(BaseName, ".backup");
		stream = fopen(BaseName, "r");
		BaseName[BaseLength] = '\0';

		if (stream==NULL)
		{
			printf("Unable to open backup file %s.backup\n", BaseName);
			free(BaseName);
			exit(1);
		}

		// options
		if (fscanf(stream, "%d %d %d", &OVerbose, &OLogging, &OBackup)!=3 || OVerbose<0 || OVerbose>3 || OLogging<0 || OLogging>3 || OBackup<0)
		{
			fclose(stream);
			printf("Backup file %s.backup does not contain valid data.\n", BaseName);
			free(BaseName);
			exit(2);
		}

		// get context
		ctx = createZSolveContextFromBackup(stream, zsolveLogCallbackDefault, backupEvent);
		fclose(stream);

		// logfile
		if (OLogging>0)
		{
			strcat(BaseName, ".log");
			stream = fopen(BaseName, "a");
			BaseName[BaseLength] = '\0';
			if (stream==NULL)
			{
				printf("Unable to open log file %s.log\n", BaseName);
				free(BaseName);
				exit(1);
			}
			ctx->LogFile = LogFile = stream;
		}

		// END OF RESUME SECTION
	}
	else
	{
		// logfile
		if (OLogging>0)
		{
			strcat(BaseName, ".log");
			stream = fopen(BaseName, "w");
			BaseName[BaseLength] = '\0';
			if (stream==NULL)
			{
				printf("Unable to open log file %s.log\n", BaseName);
				free(BaseName);
				exit(1);
			}
			LogFile = stream;
		}
		// check for existance of solution files
		if (!OForce)
		{
			strcat(BaseName, ".zhom");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				fclose(stream);
				if (OVerbose>0)
					printf("%s.hom already exists! Use -f to force calculation.\n", BaseName);
				if (OLogging>0)
				{
					fprintf(LogFile, "%s.hom already exists! Use -f to force calculation.\n", BaseName);
					fclose(LogFile);
				}
				free(BaseName);
				exit(1);
			}
			strcat(BaseName, ".zinhom");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				fclose(stream);
				if (OVerbose>0)
					printf("%s.inhom already exists! Use -f to force calculation.\n", BaseName);
				if (LogFile)
				{
					fprintf(LogFile, "%s.inhom already exists! Use -f to force calculation.\n", BaseName);
					fclose(LogFile);
				}
				free(BaseName);
				exit(1);
			}
		}
	
		// matrix
		strcat(BaseName, ".mat");
		stream = fopen(BaseName, "r");
		BaseName[BaseLength] = '\0';

		if (stream == NULL)
		{
			stream = fopen(BaseName, "r");
			if (stream) {
				if (OVerbose>0)
					printf("Matrix file %s.mat not found, falling back to project file %s.\n\n", BaseName, BaseName);
				if (OLogging>0)
				{
					fprintf(LogFile, "Matrix file %s.mat not found, falling back to project file %s.\n\n", BaseName, BaseName);
					fclose(LogFile);
				}
			}
		}

		if (stream==NULL)
		{
			strcat(BaseName, ".lat");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream==NULL)
			{
				// lattice
				if (OVerbose>0)
					printf("Neither matrix file %s.mat nor lattice file %s.lat exists!\n", BaseName, BaseName);
				if (OLogging>0)
				{
					fprintf(LogFile, "Neither matrix file %s.mat nor lattice file %s.lat exists!\n", BaseName, BaseName);
					fclose(LogFile);
				}
				free(BaseName);
				exit(1);
			}
			else
			{
				// START OF LATTICE SECTION - READ FILES AND CREATE CONTEXT

				Lattice = readVectorArray(stream, false);
				fclose(stream);
				if (Lattice == NULL)
				{
					if (OVerbose>0)
						printf("Lattice file %s.lat does not contain valid data.\n", BaseName);
					if (OLogging>0)
					{
						fprintf(LogFile, "Lattice file %s.lat does not contain valid data.\n", BaseName);
						fclose(LogFile);
					}
					free(BaseName);
					exit(1);
				}
			
				// rhs
				if (ORightHandSide)
				{
					strcat(BaseName, ".rhs");
					stream = fopen(BaseName, "r");
					BaseName[BaseLength] = '\0';
					if (stream!=NULL)
					{
						fscanf(stream, "%d", &i);
						if (i!=1)
						{
							fclose(stream);
							printf("Height of RHS must be 1!\n");
							if (LogFile)
							{
								fprintf(LogFile, "Height of RHS must be 1!\n");
								fclose(LogFile);
							}
							deleteMatrix(matrix);
							free(BaseName);
							exit(1);
						}
						fscanf(stream, "%d", &i);
						while (i--)
						{
							if (fscanf(stream, "%d", &j) || j!=0)
							{
								printf("When reading from %s.lat, RHS file %s.rhs must contain a zero vector.\n", BaseName, BaseName);
								if (LogFile)
								{
									fprintf(LogFile, "When reading from %s.lat, RHS file %s.rhs must contain a zero vector.\n", BaseName, BaseName);
									fclose(LogFile);
								}
								deleteMatrix(matrix);
								free(BaseName);
								exit(1);
							}
						}
					}
				}

				// variable properties

				for (i=0; i<Lattice->Variables; i++)
				{
					Lattice->Properties[i].Column = i;
					Lattice->Properties[i].Lower = OHilbert ? 0 : -MAXINT;
					Lattice->Properties[i].Upper = MAXINT;
					Lattice->Properties[i].Free = (!OGraver && !OHilbert);
				}
			
				// read .rel
				strcat(BaseName, ".rel");
				stream = fopen(BaseName, "r");
				BaseName[BaseLength] = '\0';
				if (stream!=NULL)
				{
					token = NULL;
					memory = 0;
					flag = false;

					if (fscanf(stream, "%d %d", &r, &j)<2 || r != 1)
					{
						printf("RELATION file %s.rel must start with the dimensions.\n", BaseName);
						flag = true;
					}

					for (i=0; i<j; i++)
					{
						if (readTokenFromFile(stream, "0123456789=<>", &token, &memory) == 0)
						{
							printf("RELATION file %s.rel ends unexpectedly.\n", BaseName);
							flag = true;
						}
						else if (!strcmp(token, "<") || !strcmp(token, ">"))
						{
							printf("When reading from %s.lat, inequalities are not allowed.\n", BaseName);
							flag = true;
						}
						else if (strcmp(token, "="))
						{
							printf("Unknown token '%s' in RELATION file %s.rel.\n", token, BaseName);
							flag = true;
						}
					}
					free(token);
					fclose(stream);
	
					if (flag)
					{
						free(BaseName);
						exit(1);
					}
				}
			
				// read .sign
				strcat(BaseName, ".sign");
				stream = fopen(BaseName, "r");
				BaseName[BaseLength] = '\0';
				if (stream!=NULL)
				{
					token = NULL;
					memory = 0;
					flag = false;
	
					if (fscanf(stream, "%d %d", &r, &i)<2 || i != Lattice->Variables || r != 1)
					{
						printf("SIGN file %s.sign must start with '1 %d'.\n", BaseName, Lattice->Variables);
						flag = true;
					}
	
					for (i=0; i<Lattice->Variables; i++)
					{
						if (readTokenFromFile(stream, "0123456789-abcdefghijklmnopqrstuvwxyz", &token, &memory) == 0)
						{
							printf("SIGN file %s.sign ends unexpectedly.\n", BaseName);
							flag = true;
						}
						if (!strcmp(token, "0") || !strcmp(token, "free") || !strcmp(token, "f"))
						{
							Lattice->Properties[i].Upper = MAXINT;
							Lattice->Properties[i].Lower = -MAXINT;
							Lattice->Properties[i].Free = true;
						}
						else if (!strcmp(token, "1") || !strcmp(token, "hil") || !strcmp(token, "h"))
						{
							Lattice->Properties[i].Upper = MAXINT;
							Lattice->Properties[i].Lower = 0;
							Lattice->Properties[i].Free = false;
						}
						else if (!strcmp(token, "-1") || !strcmp(token, "-hil") || !strcmp(token, "-h"))
						{
							Lattice->Properties[i].Upper = 0;
							Lattice->Properties[i].Lower = -MAXINT;
							Lattice->Properties[i].Free = false;
						}
						else if (!strcmp(token, "2") || !strcmp(token, "graver") || !strcmp(token, "g"))
						{
							if (OHilbert)
							{
								printf("Input Error: Graver components for `hilbert' executable.\nInput Error: Use the `graver' executable instead.\n");
								flag = true;
							}
							else
							{
								Lattice->Properties[i].Upper = MAXINT;
								Lattice->Properties[i].Lower = -MAXINT;
								Lattice->Properties[i].Free = false;
							}
						}
						else
						{
							printf("Unknown token '%s' in SIGN file %s.sign.\n", token, BaseName);
							flag = true;
						}
					}
					free(token);
					fclose(stream);

					if (flag)
					{
						deleteVectorArray(Lattice);
						free(BaseName);
						exit(1);
					}
				}
			
				// read .ub
				strcat(BaseName, ".ub");
				stream = fopen(BaseName, "r");
				BaseName[BaseLength] = '\0';
				if (stream!=NULL)
				{
					token = NULL;
					memory = 0;
					flag = false;
		
					if (fscanf(stream, "%d %d", &r, &i)<2 || i != Lattice->Variables || r != 1)
					{
						printf("UPPER BOUNDS file %s.ub must start with '1 %d'.\n", BaseName, Lattice->Variables);
						flag = true;
					}
	
					for (i=0; i<Lattice->Variables; i++)
					{
						if (readTokenFromFile(stream, "0123456789*-", &token, &memory) == 0)
						{
							printf("UPPER BOUNDS file %s.ub ends unexpectedly.\n", BaseName);
							flag = true;
						}
						if (!strcmp(token, "*"))
							Lattice->Properties[i].Upper = MAXINT;
						else if (sscanf(token, "%d", &j) == 1)
						{
							if (Lattice->Properties[i].Free)
							{
								printf("Upper bound '%s' cannot be set for free variables.\n", token);
								flag = true;
							}
							else if (j>=0)
								Lattice->Properties[i].Upper = j;
							else
							{
								printf("Negative upper bound '%s' in UPPER BOUNDS file %s.ub.\n", token, BaseName);
								flag = true;
							}
						}
						else
						{
							printf("Unknown token '%s' in UPPER BOUNDS file %s.ub.\n", token, BaseName);
							flag = true;
						}
					}
					free(token);
					fclose(stream);
		
					if (flag)
					{
						deleteVectorArray(Lattice);
						free(BaseName);
						exit(1);
					}
				}

				// read .lb
				strcat(BaseName, ".lb");
				stream = fopen(BaseName, "r");
				BaseName[BaseLength] = '\0';
				if (stream!=NULL)
				{
					token = NULL;
					memory = 0;
					flag = false;
		
					if (fscanf(stream, "%d %d", &r, &i)<2 || i != Lattice->Variables || r != 1)
					{
						printf("LOWER BOUNDS file %s.lb must start with '1 %d'.\n", BaseName, Lattice->Variables);
						flag = true;
					}
	
					for (i=0; i<Lattice->Variables; i++)
					{
						if (readTokenFromFile(stream, "0123456789*-", &token, &memory) == 0)
						{
							printf("LOWER BOUNDS file %s.lb ends unexpectedly.\n", BaseName);
							flag = true;
						}
						if (!strcmp(token, "*"))
							Lattice->Properties[i].Lower = -MAXINT;
						else if (sscanf(token, "%d", &j) == 1)
						{
							if (Lattice->Properties[i].Free)
							{
								printf("Lower bound '%s' cannot be set for free variables.\n", token);
								flag = true;
							}
							else if (j<=0)
								Lattice->Properties[i].Lower = j;
							else
							{
								printf("Positive lower bound '%s' in LOWER BOUNDS file %s.lb.\n", token, BaseName);
								flag = true;
							}
						}
						else
						{
							printf("Unknown token '%s' in LOWER BOUNDS file %s.lb.\n", token, BaseName);
							flag = true;
						}
					}
					free(token);
					fclose(stream);
		
					if (flag)
					{
						deleteVectorArray(Lattice);
						free(BaseName);
						exit(1);
					}
				}
	
				ctx = createZSolveContextFromLattice(Lattice, LogFile, OLogging, OVerbose, zsolveLogCallbackDefault, backupEvent);
			
				// print lattice
				if (ctx->Verbosity>0)
				{
					printf("\nLattice to use:\n\n");
					printVectorArray(ctx->Lattice, false);
					printf("\n\n");
				}
				if (ctx->LogLevel>0)
				{
					fprintf(ctx->LogFile, "\nLattice to use:\n\n");
					fprintVectorArray(ctx->LogFile, ctx->Lattice, false);
					fprintf(ctx->LogFile, "\n\n");
				}

				// END OF LATTICE SECTION
			}
		}
		else
		{
			// START OF SYSTEM SECTION - READ FILES AND CREATE CONTEXT

			matrix = readMatrix(stream);
			fclose(stream);
			if (matrix==NULL)
			{
				printf("Matrix file %s does not contain valid data.\n", BaseName);
				if (LogFile)
				{
					fprintf(LogFile, "Matrix file %s does not contain valid data.\n", BaseName);
					fclose(LogFile);
				}
				free(BaseName);
				exit(1);
			}
	
			// rhs
			if (ORightHandSide)
			{
				strcat(BaseName, ".rhs");
				stream = fopen(BaseName, "r");
				BaseName[BaseLength] = '\0';
				if (stream!=NULL)
				{
					if (OGraver || OHilbert)
					{
						fclose(stream);
						printf("Input Error: No rhs file is allowed with --graver and --hilbert!\n");
						printf("Input Error: Please delete %s.rhs and rerun zsolve\n", BaseName);
						if (LogFile)
						{
							fprintf(LogFile, "Input Error: No rhs file is allowed with --graver and --hilbert!\n");
							fprintf(LogFile, "Input Error: Please delete %s.rhs and rerun zsolve\n", BaseName);
							fclose(LogFile);
						}
						deleteMatrix(matrix);
						free(BaseName);
						exit(1);
					}
					
					fscanf(stream, "%d", &i);
					if (i!=1)
					{
						fclose(stream);
						printf("Height of RHS must be 1!\n");
						if (LogFile)
						{
							fprintf(LogFile, "Height of RHS must be 1!\n");
							fclose(LogFile);
						}
						deleteMatrix(matrix);
						free(BaseName);
						exit(1);
					}
					fscanf(stream, "%d", &i);
					if (i!=matrix->Height)
					{
						fclose(stream);
						printf("Matrix height conflicts with width of rhs!\n");
						if (LogFile)
						{
							fprintf(LogFile, "Matrix height conflicts with width of rhs!\n");
							fclose(LogFile);
						}
						deleteMatrix(matrix);
						free(BaseName);
						exit(1);
					}
					rhs = readVector(stream, matrix->Height);
					fclose(stream);
					if (rhs==NULL)
					{
						printf("RHS file %s.rhs does not contain valid data.\n", BaseName);
						if (LogFile)
						{
							fprintf(LogFile, "RHS file %s.rhs does not contain valid data.\n", BaseName);
							fclose(LogFile);
						}
						deleteMatrix(matrix);
						free(BaseName);
						exit(1);
					}
				}
			}

			// fill with zeros
			if (rhs==NULL)
			{
				rhs = createVector(matrix->Height);
				for (i=0; i<matrix->Height; i++)
					rhs[i] = 0;
			}

			// create system
			initialsystem = createLinearSystem();
	
			setLinearSystemMatrix(initialsystem, matrix);
			deleteMatrix(matrix);

			setLinearSystemRHS(initialsystem, rhs);
			deleteVector(rhs);

			// default limits

			if (OGraver)
				setLinearSystemLimit(initialsystem, -1, -MAXINT, MAXINT, false);
			else if (OHilbert)
				setLinearSystemLimit(initialsystem, -1, 0, MAXINT, false);
			else
				setLinearSystemLimit(initialsystem, -1, -MAXINT, MAXINT, true);
	
			// default equation type

			setLinearSystemEquationType(initialsystem, -1, EQUATION_EQUAL, 0);

			// read .rel
			strcat(BaseName, ".rel");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				token = NULL;
				memory = 0;
				flag = false;

				if (fscanf(stream, "%d %d", &r, &i)<2 || i != initialsystem->Equations || r != 1)
				{
					printf("RELATION file %s.rel must start with '1 %d'.\n", BaseName, initialsystem->Equations);
					flag = true;
				}

				for (i=0; i<initialsystem->Equations; i++)
				{
					if (readTokenFromFile(stream, "0123456789=<>", &token, &memory) == 0)
					{
						printf("RELATION file %s.rel ends unexpectedly.\n", BaseName);
						flag = true;
					}
					if (!strcmp(token, "="))
						setLinearSystemEquationType(initialsystem, i, EQUATION_EQUAL, 0);
					// BUG: Not a real bug, but maybe misdefinition?? <= is not so hard to type :-)
					else if (!strcmp(token, "<"))
						setLinearSystemEquationType(initialsystem, i, EQUATION_LESSEREQUAL, 0);
					else if (!strcmp(token, ">"))
						setLinearSystemEquationType(initialsystem, i, EQUATION_GREATEREQUAL, 0);
					else
					{
						printf("Unknown token '%s' in RELATION file %s.rel.\n", token, BaseName);
						flag = true;
					}
				}
				free(token);
				fclose(stream);

				if (flag)
				{
					deleteLinearSystem(initialsystem);
					free(BaseName);
					exit(1);
				}
			}

			// read .sign
			strcat(BaseName, ".sign");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				token = NULL;
				memory = 0;
				flag = false;

				if (fscanf(stream, "%d %d", &r, &i)<2 || i != initialsystem->Variables || r != 1)
				{
					printf("SIGN file %s.sign must start with '1 %d'.\n", BaseName, initialsystem->Variables);
					flag = true;
				}

				for (i=0; i<initialsystem->Variables; i++)
				{
					if (readTokenFromFile(stream, "0123456789-abcdefghijklmnopqrstuvwxyz", &token, &memory) == 0)
					{
						printf("SIGN file %s.sign ends unexpectedly.\n", BaseName);
						flag = true;
					}
					if (!strcmp(token, "0") || !strcmp(token, "free") || !strcmp(token, "f"))
						setLinearSystemLimit(initialsystem, i, -MAXINT, MAXINT, true);
					else if (!strcmp(token, "1") || !strcmp(token, "hil") || !strcmp(token, "h"))
						setLinearSystemLimit(initialsystem, i, 0, MAXINT, false);
					else if (!strcmp(token, "-1") || !strcmp(token, "-hil") || !strcmp(token, "-h"))
						setLinearSystemLimit(initialsystem, i, -MAXINT, 0, false);
					else if (!strcmp(token, "2") || !strcmp(token, "graver") || !strcmp(token, "g"))
					{
						if (OHilbert)
						{
							if (!flag)
								printf("Input Error: Graver components for `hilbert' executable.\nInput Error: Use the `graver' executable instead.\n");
							flag = true;
						}
						else
							setLinearSystemLimit(initialsystem, i, -MAXINT, MAXINT, false);
					}
					else
					{
						printf("Unknown token '%s' in SIGN file %s.sign.\n", token, BaseName);
						flag = true;
					}
				}
				free(token);
				fclose(stream);

				if (flag)
				{
					deleteLinearSystem(initialsystem);
					free(BaseName);
					exit(1);
				}
			}

			// read .ub
			strcat(BaseName, ".ub");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				token = NULL;
				memory = 0;
				flag = false;
	
				if (fscanf(stream, "%d %d", &r, &i)<2 || i != initialsystem->Variables || r != 1)
				{
					printf("UPPER BOUNDS file %s.ub must start with '1 %d'.\n", BaseName, initialsystem->Variables);
					flag = true;
				}

				for (i=0; i<initialsystem->Variables; i++)
				{
					if (readTokenFromFile(stream, "0123456789*-", &token, &memory) == 0)
					{
						printf("UPPER BOUNDS file %s.ub ends unexpectedly.\n", BaseName);
						flag = true;
					}
					if (!strcmp(token, "*"))
						setLinearSystemBound(initialsystem, i, 'u', MAXINT);
					else if (sscanf(token, "%d", &j) == 1)
					{
						if (initialsystem->VarProperties[i].Free)
						{
							printf("Upper bound '%s' cannot be set for free variables.\n", token);
							flag = true;
						}
						else if (j>=0)
							setLinearSystemBound(initialsystem, i, 'u', j);
						else
						{
							printf("Negative upper bound '%s' in UPPER BOUNDS file %s.ub.\n", token, BaseName);
							flag = true;
						}
					}
					else
					{
						printf("Unknown token '%s' in UPPER BOUNDS file %s.ub.\n", token, BaseName);
						flag = true;
					}
				}
				free(token);
				fclose(stream);
	
				if (flag)
				{
					deleteLinearSystem(initialsystem);
					free(BaseName);
					exit(1);
				}
			}

			// read .lb
			strcat(BaseName, ".lb");
			stream = fopen(BaseName, "r");
			BaseName[BaseLength] = '\0';
			if (stream!=NULL)
			{
				token = NULL;
				memory = 0;
				flag = false;
	
				if (fscanf(stream, "%d %d", &r, &i)<2 || i != initialsystem->Variables || r != 1)
				{
					printf("LOWER BOUNDS file %s.lb must start with '1 %d'.\n", BaseName, initialsystem->Variables);
					flag = true;
				}

				for (i=0; i<initialsystem->Variables; i++)
				{
					if (readTokenFromFile(stream, "0123456789*-", &token, &memory) == 0)
					{
						printf("LOWER BOUNDS file %s.lb ends unexpectedly.\n", BaseName);
						flag = true;
					}
					if (!strcmp(token, "*"))
						setLinearSystemBound(initialsystem, i, 'l', -MAXINT);
					else if (sscanf(token, "%d", &j) == 1)
					{
						if (initialsystem->VarProperties[i].Free)
						{
							printf("Lower bound '%s' cannot be set for free variables.\n", token);
							flag = true;
						}
						else if (j<=0)
							setLinearSystemBound(initialsystem, i, 'l', j);
						else
						{
							printf("Positive lower bound '%s' in LOWER BOUNDS file %s.lb.\n", token, BaseName);
							flag = true;
						}
					}
					else
					{
						printf("Unknown token '%s' in LOWER BOUNDS file %s.lb.\n", token, BaseName);
						flag = true;
					}
				}
				free(token);
				fclose(stream);
	
				if (flag)
				{
					deleteLinearSystem(initialsystem);
					free(BaseName);
					exit(1);
				}
			}

			ctx = createZSolveContextFromSystem(initialsystem, LogFile, OLogging, OVerbose, zsolveLogCallbackDefault, backupEvent);
	
			// END OF SYSTEM SECTION
		}
	}

	// DEBUG
//	printVectorArray(ctx->Lattice, true);

	zsolveSystem(ctx, !OResume);

	if (OGraver)
	{
		printf("Writing %d vectors to graver file, with respect to symmetry.\n", ctx->Graver->Size);
		if (LogFile)
			fprintf(LogFile, "Writing %d vectors to graver file, with respect to symmetry.\n", ctx->Graver->Size);
		
		strcat(BaseName, ".gra");
		stream = fopen(BaseName, "w");
		BaseName[BaseLength] = '\0';
		if (stream)
		{
			fprintf(stream, "%d %d\n\n", ctx->Graver->Size, ctx->Graver->Variables);
			fprintVectorArray(stream, ctx->Graver, false);
			fclose(stream);
		}
	}
	else if (OHilbert)
	{
		strcat(BaseName, ".hil");
		stream = fopen(BaseName, "w");
		BaseName[BaseLength] = '\0';
		if (stream)
		{
			fprintf(stream, "%d %d\n\n", ctx->Homs->Size + ctx->Frees->Size, ctx->Homs->Variables);
			fprintVectorArray(stream, ctx->Homs, false);
			fprintf(stream, "\n");
			fprintVectorArray(stream, ctx->Frees, false);
			fclose(stream);
		}
	}
	else
	{
		strcat(BaseName, ".zinhom");
		stream = fopen(BaseName, "w");
		BaseName[BaseLength] = '\0';
		if (stream)
		{
			fprintf(stream, "%d %d\n\n", ctx->Inhoms->Size, ctx->Inhoms->Variables);
			fprintVectorArray(stream, ctx->Inhoms, false);
			fclose(stream);
		}

		strcat(BaseName, ".zhom");
		stream = fopen(BaseName, "w");
		BaseName[BaseLength] = '\0';
		if (stream)
		{
			fprintf(stream, "%d %d\n\n", ctx->Homs->Size, ctx->Homs->Variables);
			fprintVectorArray(stream, ctx->Homs, false);
			fclose(stream);
		}

		if (ctx->Frees->Size>0)
		{
			strcat(BaseName, ".zfree");
			stream = fopen(BaseName, "w");
			BaseName[BaseLength] = '\0';
			if (stream)
			{
				fprintf(stream, "%d %d\n\n", ctx->Frees->Size, ctx->Frees->Variables);
				fprintVectorArray(stream, ctx->Frees, false);
				fclose(stream);
			}
		}
	}


	printf("\n4ti2 Total Time: ");
	printCPUTime(maxd(getCPUTime() - ctx->AllTime, 0.0));
	printf("\n");
	if (LogFile) {
		fprintf(LogFile, "\n4ti2 Total Time: ");
		fprintCPUTime(LogFile, maxd(getCPUTime() - ctx->AllTime, 0.0));
		fprintf(LogFile, "\n");
	}

	deleteZSolveContext(ctx, true);

	if (BaseName!=NULL)
		free(BaseName);

	if (LogFile)
		fclose(LogFile);

	return EXIT_SUCCESS;
}
Ejemplo n.º 7
0
bool UResPassable::findTopOfRoad(ULaserPi * pip)
{
  bool result;
  int j;
  ULineSegment * seg;
  double d = 0.0; //, dl, dr;
  const int MAX_TOP_CNT = 10;
  double topHgt[MAX_TOP_CNT];
  int top[MAX_TOP_CNT];
  int topCnt;
  ULaserPoint * pt; //, *pl, *pr;
  const double TOP_LIMIT = sqr(0.5); // times line-fit variance
  const double MIN_TOP_LIMIT = 0.04; // distance (in x) from fit-line
  bool topValid;
  bool isOK;
  const double MAX_TOP_RANGE = 4.5; // laser range [m]
  const double TOP_DIST_FROM_END = 0.4; // meter
  double minTopHgt;
  double td;
  double topVarMin;
  UPosition topPos;

  // set edges and top
  result = (pip != NULL);
  if (result)
  {
    seg = pip->getSegment();
    result = (absd(pip->getTilt()) < lineFitZTiltLimit);
  }
  if (result)
  { // find center of road
    // minimum value for valid height
    minTopHgt = maxd(pip->getFitVariance() * TOP_LIMIT, MIN_TOP_LIMIT);
    // get first values to test
    pt = scan->getData(pip->getRight());
//    pl = pt + 4;
//    pr = pt - 4;
    // initialize top candidate array
    topCnt = 0;
    topHgt[topCnt] = -1.0;
    top[topCnt] = -1;
    // find center position candidates
    for (j = pip->getRight(); j <= pip->getLeft(); j++)
    { // set classification to passable
      pt->setQ(PQ_EASY);
      isOK = ((j > (pip->getRight() + 4)) and
          (j < (pip->getLeft() - 4)));
      if (isOK)
      { // valid data set
        d = seg->getXYsignedDistance(pt->pos);
//        dl = seg->getXYsignedDistance(pl->pos);
//        dr = seg->getXYsignedDistance(pr->pos);
        if ((d > topHgt[topCnt]) and //(d > dl) and (d > dr) and
             (pt->range < MAX_TOP_RANGE) and
             (d > minTopHgt))
        { // negative is closer (and higher)
          topHgt[topCnt] = d;
          top[topCnt] = j;
        }
        if ((d < 0.0) and (top[topCnt] >= 0))
        { // top has ended, and ready to find next
          if (topCnt < MAX_TOP_CNT - 1)
          {
            topCnt++;
            topHgt[topCnt] = -1.0;
            top[topCnt] = -1;
          }
        }
      }
      pt++;
//      pl++;
//      pr++;
    }
    // find best top - the one with the lowest variance
    // -- that is lowest variance in the area to the left of the top
    // -- (should possibly be the minimum or average of left and right side variance)
    if (top[topCnt] >= 0)
      topCnt++;
    topValid = (topCnt > 0);
    if (topValid)
    {
      topVarMin = 10.0;
      for (j = 0; j < topCnt; j++)
      {
        pt = scan->getData(top[j]);
        if (pt->varL < topVarMin)
        {
          topVarMin = pt->varL;
          topPos = pt->pos;
        }
      }
    }
    if (topValid)
    {
      td = seg->getPositionOnLine(&topPos);
      topValid = td > TOP_DIST_FROM_END;
    }
    if (topValid)
      topValid = td < (seg->length - TOP_DIST_FROM_END);
    if (not topValid)
      // use center position
      td = seg->length / 2.0;
    pip->setCenter(td, topValid);
    // and analyze edges
  }
  return result;
}
Ejemplo n.º 8
0
int UResPassable::makePassableIntervals2(const int rightLim, const int leftLim)
{
  int result = 0;
  int i, j;
  ULaserPoint *pr;
  ULaserPoint *pl; // pointer to left test point
  ULaserPoint *pll; // pointer to last left test point
  ULaserPoint *par;
  int piLeft, piRight; // left and right index
  int paRight, paLeft; // actual interval limits
  bool passable;
  double w, d;
  double varMin = 1.0;
  double varMin2 = 1.0;
  double varTest = 0.0;
  const double LASER_MEARUREMENT_VARIANCE = sqr(0.01); // in meter^2
  double varMinLim = LASER_MEARUREMENT_VARIANCE;
  double varStop = 1.0;
  UPosition p;
  // max allowed separation of measurements
  const double MAX_MEASUREMENT_INTERVAL_DIST = 0.8;
//  const double VAR_MIN2_LIMIT = 2; // minimum measurements for valid varince type 2 (no min limit)
  double d2p; // distance to previous measurement
  bool limHeight, limMaxVar, limEndDist, limInside, limMeasureDist;
  bool doContinue;
  //
  //
  // set default as not passable
  scan->setQ(rightLim, leftLim - 1, PQ_NOT);
  //
  pr = scan->getData(rightLim);
  if (lineSmoothSettings)
    varMinLim = LASER_MEARUREMENT_VARIANCE;
  else
    varMinLim = LASER_MEARUREMENT_VARIANCE * 2.0;
  pl = pr;
  i = rightLim;
  passable = false;
  piRight = 0;
  piLeft = -1; // just to avoid warnings
  while (i < leftLim)
  { // test for terrain structure
    if (not passable)
    { // look for start of passable using max limit
      passable = (pr->varL < lineFitVarLimit) and     // within variance limit ...
          (absd(pr->tilt) < lineFitXTiltLimit) and // and tilt OK
          (pr->pos.z < MAX_ALLOWED_HEIGHT) and // and not too high ...
          (pr->pos.z > MIN_ALLOWED_HEIGHT);    // or too low
      if (passable)
      { // right edge of a potential area is found
        piRight = i;
        varMinLim = LASER_MEARUREMENT_VARIANCE;
        varMin = maxd(pr->varL, varMinLim);
        varMin2 = varMin; // 1.0;
        // initialize looking for left side of interval
        piLeft = i;
        pl = scan->getData(i);
      }
    }
    //
    if (passable)
    {
      pll = pl;
      pl = scan->getData(i);
      // look for left side of interval using adaptive limit
      varTest = mind(varMin, lineFitVarLimit)  * lineFitEndpointDev;
      // save index for (adaptive) left limit
      paLeft = scan->getData(piLeft)->varToL;
      // get compensated distance for last measurement to the left of interval
      d = pr->distLeft - lineFitConvexOffset;
      // get distance to previous measurement
      if (pll->isValid() and pl->isValid())
        d2p = hypot(pl->pos.x - pll->pos.x, pl->pos.y - pll->pos.y);
      else
        d2p = 0.0;
      // debug
      //if (pr->pos.z >= MAX_ALLOWED_HEIGHT)
      //  pr->pos.z = pr->pos.z + 0.01;
      // debug end
      limHeight = (pr->pos.z < MAX_ALLOWED_HEIGHT) and (pr->pos.z > MIN_ALLOWED_HEIGHT);
      limMaxVar = pr->varL < lineFitVarLimit;
      limEndDist = sqr(d) < varTest;
      limInside  = paLeft <= leftLim;
      limMeasureDist = d2p < MAX_MEASUREMENT_INTERVAL_DIST;
      // debug
      doContinue = (limMeasureDist and limInside and limEndDist and limMaxVar and limHeight);
      if (not doContinue)
      {
        if (not limMeasureDist)
          iMeasureDist++;
        if (not limInside)
          iInside++;
        if (limEndDist)
          iEndDist++;
        if (limMaxVar)
          iMaxVar++;
        if (limHeight)
          iHeight++;
      }
      // debug end
      // test for end of passable interval
      if ((pr->varL < lineFitVarLimit) and  // max line-fit variance
           (sqr(d) < varTest) and      // fit of left-most point adapted
           (paLeft <= leftLim) and     // inside test interval
           (absd(pr->tilt) < lineFitXTiltLimit) and // line tilt OK
           (pr->pos.z < MAX_ALLOWED_HEIGHT) and // item height not too high ...
           (pr->pos.z > MIN_ALLOWED_HEIGHT) and
           (d2p < MAX_MEASUREMENT_INTERVAL_DIST)) // or too low
      { // still passable
        if (pr->isValid())
          piLeft = i; // left-most place to search for a right edge
        // adapt limit of line-fit
        if (pr->varL < varMin)
          varMin = maxd(pr->varL, varMinLim);
        if ((pr->varL < varMin2)) // and ((pr->varToL - i) > VAR_MIN2_LIMIT))
          varMin2 = pr->varL;
      }
      else
      { // (adaptive) left end of interval is found
        // look from here to the right for adaptive right limit
        paRight = piLeft;
        // get position of left side of interval
        p = scan->getData(paLeft)->pos;
        par = scan->getData(paRight); // first guess of adapted right edge
        w = 0.0; // reset width
        for (j = paRight; j >= piRight; j--)
        { // look for right edge with this variance.
          // calculate passable width
          w = p.dist(par->pos);
          // get distance relative to line compensated
          // to favor convex shaped road
          d = par->distRight - lineFitConvexOffset;
          varStop = par->varL;
          //
          limMaxVar = pr->varL < lineFitVarLimit;
          limEndDist = sqr(d) < varTest;
          doContinue = (limMeasureDist and limInside and limEndDist and limMaxVar and limHeight);
          if (not doContinue)
          {
            if (limEndDist)
              iEndDist++;
            if (limMaxVar)
              iMaxVar++;
          }
          //
          if ((par->varL <= lineFitVarLimit) and  // max line-fit variance limit
               (sqr(d) < varTest))// and           // last right-most point (adaptive)
            paRight = j;
          else
            // right edge is found
            break;
          // go further right
          par--;
        }
        // left is left side of last good interval
        // missing test for big (in-line) jumps in untested interval
        // from liLeft to paLeft
        pl = scan->getData(piLeft);
        for (j = piLeft + 1; j <= paLeft; j++)
        {
          pll = pl;
          pl = scan->getData(j);
          if (pll->isValid() and pl->isValid())
          { // test only if measurements are valid
            d2p = hypot(pl->pos.x - pll->pos.x, pl->pos.y - pll->pos.y);
            if (d2p >= MAX_MEASUREMENT_INTERVAL_DIST)
            { // stop before the jump
              paLeft = j - 1;
              break;
            }
          }
        }
        addPassableInterval(paRight, mini(paLeft, leftLim), varMin, varMin2);
        //
        result++;
        // look for new start
        i = paLeft;
        pr = scan->getData(i);
        passable = false;
        // an interval is found (or failed), but there may be another
        // to the right of the tested interval
        if ((paRight - piRight) > MIN_MEASUREMENTS_PER_PI)
        { // test this interval
          result += makePassableIntervals2(piRight, paRight - 1);
        }
        // continue with next interval
      }
    }
    i++;
    //    prOld = pr;
    pr++;
  }
  return result;
}
Ejemplo n.º 9
0
/* send a packet from one torus node to another torus node
 A packet can be up to 256 bytes on BG/L and BG/P and up to 512 bytes on BG/Q */
static void packet_send( nodes_state * s,
                         tw_bf * bf,
                         nodes_message * msg,
                         tw_lp * lp )
{
    bf->c2 = 0;
    bf->c1 = 0;
    int tmp_dir, tmp_dim;
    tw_stime ts;
    tw_event *e;
    nodes_message *m;
    tw_lpid dst_lp = msg->dest_lp;
    dimension_order_routing( s, &dst_lp, &tmp_dim, &tmp_dir );

    if(s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ] < s->params->buffer_size)
    {
        bf->c2 = 1;
        msg->saved_src_dir = tmp_dir;
        msg->saved_src_dim = tmp_dim;
        ts = tw_rand_exponential( lp->rng, s->params->head_delay/200.0 ) +
             s->params->head_delay;

//    For reverse computation
        msg->saved_available_time = s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0];

        s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] = maxd( s->next_link_available_time[ tmp_dir + ( tmp_dim * 2 )][0], tw_now(lp) );
        s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] += ts;

        //e = tw_event_new( dst_lp, s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] - tw_now(lp), lp );
        //m = tw_event_data( e );
        //memcpy(m, msg, torus_get_msg_sz() + msg->remote_event_size_bytes);
        void * m_data;
        e = model_net_method_event_new(dst_lp,
                                       s->next_link_available_time[tmp_dir+(tmp_dim*2)][0] - tw_now(lp),
                                       lp, TORUS, (void**)&m, &m_data);
        memcpy(m, msg, sizeof(nodes_message));
        if (msg->remote_event_size_bytes) {
            memcpy(m_data, model_net_method_get_edata(TORUS, msg),
                   msg->remote_event_size_bytes);
        }
        m->type = ARRIVAL;

        if(msg->packet_ID == TRACE)
            printf("\n lp %d packet %lld flit id %d being sent to %d after time %lf ", (int) lp->gid, msg->packet_ID, msg->chunk_id, (int)dst_lp, s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] - tw_now(lp));
        //Carry on the message info
        m->source_dim = tmp_dim;
        m->source_direction = tmp_dir;
        m->next_stop = dst_lp;
        m->sender_node = lp->gid;
        m->local_event_size_bytes = 0; /* We just deliver the local event here */

        tw_event_send( e );

        s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ]++;

        uint64_t num_chunks = msg->packet_size/s->params->chunk_size;

        if(msg->packet_size % s->params->chunk_size)
            num_chunks++;

        if(msg->chunk_id == num_chunks - 1)
        {
            bf->c1 = 1;
            /* Invoke an event on the sending server */
            if(msg->local_event_size_bytes > 0)
            {
                tw_event* e_new;
                nodes_message* m_new;
                void* local_event;
                ts = (1/s->params->link_bandwidth) * msg->local_event_size_bytes;
                e_new = tw_event_new(msg->sender_svr, ts, lp);
                m_new = tw_event_data(e_new);
                //local_event = (char*)msg;
                //local_event += torus_get_msg_sz() + msg->remote_event_size_bytes;
                local_event = (char*)model_net_method_get_edata(TORUS, msg) +
                              msg->remote_event_size_bytes;
                memcpy(m_new, local_event, msg->local_event_size_bytes);
                tw_event_send(e_new);
            }
        }
    } // end if
    else
    {
        printf("\n buffer overflown ");
        MPI_Finalize();
        exit(-1);
    }
}