Example #1
2
int main(int argc, char **argv) {

  // Determine what the output image should be
  char filename[14];
  if (argc > 1) {
    if (strcmp(argv[1], "reference") == 0) {
      strcpy(filename, "reference.png");
    }
    else if (strcmp(argv[1], "custom") == 0) {
      strcpy(filename, "custom.png");
    }
    else if (strcmp(argv[1], "fancy") == 0) {
      strcpy(filename, "fancy.png");
    }
    else {
      fprintf(stderr, "Incorrect argument recieved\nUsage: %s reference.png | custom.png\n", argv[0]);
      return 1;
    }
  }
  else { // No argument
    fprintf(stderr, "No argument recieved\nUsage: %s reference.png | custom.png\n", argv[0]);
    return 2;
  }

  // Prepare the reference scene
  if (strcmp(filename, "reference.png") == 0) {
    setUpReference();
  } else if (strcmp(filename, "custom.png") == 0) {
    setUpCustom();
  } else if (strcmp(filename, "fancy.png") == 0) {
    printf("fancy should run in around 5-10 minutes.\n");
    setUpFancy();
  }

  // Camera position
  cameraPos = point_new(0, 0, 0);

  // Prepare PNG data array
  char imageData[width*height*3];
  int imagePos = 0;

  intersect_t intersect;
  color_t color;

  // Calculate the color of each pixel
  for (float j = 0; j < height; j++) {
    for (float i = 0; i < width; i++) {
      if (strcmp(filename, "reference.png") == 0) {
        color = getPixel(i, j, &intersect);
      } else if (strcmp(filename, "custom.png") == 0) {
        color = getAntialiasedPixel(i, j, &intersect, 2);
      } else {
        color = getAntialiasedPixel(i, j, &intersect, 5); // Really ramp it up
      }
      imageData[imagePos++] = color.r;
      imageData[imagePos++] = color.g;
      imageData[imagePos++] = color.b;
    }
  }

  // Write image out to PNG
  stbi_write_png(filename, width, height, 3, imageData, width*3);

  return 0;
}
Example #2
0
static void tri_test(void)
{
  mesh* m;
  point pts[3];
  ment v[3];
  ment e;
  unsigned i;
  double A;
  double l[3];
  double s;
  m = mesh_new();
  pts[0] = point_new(-1,0,0);
  pts[1] = point_new( 1,0,0);
  pts[2] = point_new( 0,my_sqrt(3),0);
  for (i = 0; i < 3; ++i)
    v[i] = ment_new(m, VERTEX, 0);
  for (i = 0; i < 3; ++i)
    mesh_set_point(m, v[i], pts[i]);
  e = ment_new(m, TRIANGLE, v);
  A = triangle_area(ment_triangle(m, e));
  debug("area: %e\n", A);
  s = 0;
  for (i = 0; i < 3; ++i) {
    mesh_down(m, e, EDGE, i, v);
    l[i] = line_len(verts_line(m, v));
    debug("length: %e\n", l[i]);
    s += l[i] * l[i];
  }
  s /= 3;
  debug("condition bound: %e\n", A / s);
  debug("quality: %e\n", ment_quality(m, e));
  mesh_free(m);
}
Example #3
0
static void tet_test(void)
{
  mesh* m;
  point pts[4];
  ment v[4];
  ment e;
  unsigned i;
  double V;
  double A[4];
  double s;
  m = mesh_new();
  pts[0] = point_new(-1, 0, -1.0 / my_sqrt(2));
  pts[1] = point_new( 1, 0, -1.0 / my_sqrt(2));
  pts[2] = point_new( 0,-1,  1.0 / my_sqrt(2));
  pts[3] = point_new( 0, 1,  1.0 / my_sqrt(2));
  for (i = 0; i < 4; ++i)
    v[i] = ment_new(m, VERTEX, 0);
  for (i = 0; i < 4; ++i)
    mesh_set_point(m, v[i], pts[i]);
  e = ment_new(m, TET, v);
  V = tet_volume(ment_tet(m, e));
  debug("volume: %e\n", V);
  s = 0;
  for (i = 0; i < 4; ++i) {
    mesh_down(m, e, TRIANGLE, i, v);
    A[i] = triangle_area(verts_triangle(m, v));
    debug("area: %e\n", A[i]);
    s += A[i] * A[i];
  }
  s /= 4;
  s = my_pow(s, 3.0 / 4.0);
  debug("condition bound: %.10e\n", V / s);
  debug("quality: %e\n", ment_quality(m, e));
  mesh_free(m);
}
Example #4
0
int main (int argc, char **argv)
{
  point **ptoarr = malloc(4 * sizeof(point *));
  *ptoarr = point_new(0.0, 0.0);
  *(ptoarr + 1) = point_new(1.0, 0.0);
  *(ptoarr + 2) = point_new(0.0, 0.0);
  ptoarr[3] = point_new(1.0, 0.0);
  path *p = path_new(ptoarr, 4);
  path_show(p);
  printf("The distance is %lf\n", path_length(p));
  path_append(p, point_new(2.0, 2.0));
  path_show(p);
  return 0;
}
Example #5
0
bool ray_intersects_triangle(ray_t ray, triangle_t *triangle, intersect_t *intersect) {
  float A = triangle->points[0].x - triangle->points[1].x;
  float B = triangle->points[0].y - triangle->points[1].y;
  float C = triangle->points[0].z - triangle->points[1].z;
  float D = triangle->points[0].x - triangle->points[2].x;
  float E = triangle->points[0].y - triangle->points[2].y;
  float F = triangle->points[0].z - triangle->points[2].z;
  float G = ray.direction.x;
  float H = ray.direction.y;
  float I = ray.direction.z;
  float J = triangle->points[0].x - ray.point.x;
  float K = triangle->points[0].y - ray.point.y;
  float L = triangle->points[0].z - ray.point.z;
  float M = A * (E * I - H * F) + B * (G * F - D * I) + C * (D * H - E * G);
  float t = -(F * (A * K - J * B) + E * (J * C - A * L) + D * (B * L - K * C)) / M;
  if (t < 0) return false;
  float gamma = (I * (A * K - J * B) + H * (J * C - A * L) + G * (B * L - K * C)) / M;
  if (gamma < 0 || gamma > 1) return false;
  float beta = (J * (E * I - H * F) + K * (G * F - D * I) + L * (D * H - E * G)) / M;
  if (beta < 0 || beta > 1 - gamma) return false;

  if (t < intersect->t || intersect->t == -1) {
    intersect->point = point_new(ray.point.x + ray.direction.x * t,
                                 ray.point.y + ray.direction.y * t,
                                 ray.point.z + ray.direction.z * t);
    intersect->t = t;
    intersect->object = triangle;
    intersect->geomType = GeomTypeTriangle;
    return true;
  }

  return false;
}
Example #6
0
color_t getAntialiasedPixel(float x, float y, intersect_t *intersect, float antialiasLevel) {
  float antialiasLevelSquared = antialiasLevel * antialiasLevel;
  float precalculatedOffsetPart = 1.0 / (2.0 * antialiasLevel);
  point_t pos;
  vec_t rayDirection;
  ray_t ray;
  color_t color;
  float avgR = 0, avgG = 0, avgB = 0;
  for (int j = 0; j < antialiasLevel; j++) {
    for (int i = 0; i < antialiasLevel; i++) {
      float xOff = i / antialiasLevel + precalculatedOffsetPart;
      float yOff = j / antialiasLevel + precalculatedOffsetPart;
      float xPos = (x + xOff) * 2 / width - 1;
      float yPos = -((y + yOff) * 2 / height - 1);
      pos = point_new(xPos, yPos, -2);
      rayDirection = vec_normalize(point_direction(cameraPos, pos));
      ray = ray_new(cameraPos, rayDirection);
      color = shootRay(ray, intersect, 0);
      avgR += (float)color.r / (float)antialiasLevelSquared;
      avgG += (float)color.g / (float)antialiasLevelSquared;
      avgB += (float)color.b / (float)antialiasLevelSquared;
    }
  }

  return rgb((char)avgR, (char)avgG, (char)avgB);
}
Example #7
0
int main(int argc, char *argv[]) {
  Point myPoint = point_new(0.0, 0.0);
  Vector unitX = vector_new(1.0, 0.0);
  Vector unitY = vector_new(0.0, 1.0);

  printf("%s + %s + %s = ", point_toString(myPoint),
                            vector_toString(unitX),
                            vector_toString(unitY) );

  point_addVector(myPoint, unitX);
  point_addVector(myPoint, unitY);

  printf("As a tuple: %s\n", tuple_toString((Tuple)myPoint));
  printf("As a point: %s\n", point_toString(myPoint));

  printf( "After scaling unitX by -0.5: %s\n",
	  vector_toString(vector_scale(unitX, -0.5))
	);
  printf("unitX has length %lf\n", vector_length(unitX));

  vector_delete(unitX);
  vector_delete(unitY);
  point_delete(myPoint);

  return 0;
}
Example #8
0
int		solve_map(t_map *map, t_list *list)
{
	int			x;
	int			y;
	t_etris		*tetri;

	if (list == NULL)
		return (1);
	y = 0;
	tetri = (t_etris *)(list->content);
	while (y < map->size - tetri->height + 1)
	{
		x = 0;
		while (x < map->size - tetri->width + 1)
		{
			if (place(tetri, map, x, y))
			{
				if (solve_map(map, list->next))
					return (1);
				else
					set_piece(tetri, map, point_new(x, y), '.');
			}
			x++;
		}
		y++;
	}
	return (0);
}
Example #9
0
Triangle*
triangle_new(Point *p1, Point *p2, Point *p3) 
{
	Triangle *t;
	t = (Triangle*)malloc(sizeof(Triangle));
	t->p1 = (Point*)malloc(sizeof(Point));
	t->p2 = (Point*)malloc(sizeof(Point));
	t->p3 = (Point*)malloc(sizeof(Point));
	
	memcpy(
		t->p1,
		p1,
		sizeof(Point)
	);
	memcpy(
		t->p2,
		p2,
		sizeof(Point)
	);
	memcpy(
		t->p3,
		p3,
		sizeof(Point)
	);
	t->centroid = point_new((p1->x + p2->x + p3->x) / 3,
												 (p1->y + p2->y + p3->y) / 3); 
	return t;
}
Example #10
0
castle_t *castle_new_ints(player_t *owner, int num_walls, int num_towers, int x, int y)
{
	castle_t *castle = castle_new(owner, num_walls, num_towers);

	castle->primary_location = point_new(x, y);

	return castle;
}
Example #11
0
static void _test_point(void)
{
    int x, y;
    const void *clazz;
    void *point;

    const struct _propinfo props[] = {
        { MUME_TYPE_STRING, "name",
          _POINT_PROP_NAME, MUME_PROP_READWRITE },
        { MUME_TYPE_STRING, "coord",
          _POINT_PROP_COORD, MUME_PROP_READWRITE },
    };

    const struct _property props1[] = {
        { "name", "hello" },
        { "coord", "10, 20" },
    };

    const struct _property props2[] = {
        { "name", "another name" },
        { "coord", "-1050, -1" },
    };

    _test_properties(point_class(), props, COUNT_OF(props));
    test_assert(mume_class_of(mume_object_class()) == mume_meta_class());
    test_assert(mume_class_of(mume_meta_class()) == mume_meta_class());
    test_assert(mume_super_class(mume_object_class()) == mume_object_class());
    test_assert(mume_super_class(mume_meta_class()) == mume_object_class());
    point = point_new("hello", 10, 20);
    test_assert(strcmp(point_get_name(point), "hello") == 0);
    _test_get_properties(point, props1, COUNT_OF(props1));
    point_get_coord(point, &x, &y);
    test_assert(10 == x && 20 == y);
    point_move(point, -10, 30);
    point_get_coord(point, &x, &y);
    test_assert(-10 == x && 30 == y);
    _test_set_properties(point, props2, COUNT_OF(props2));
    _test_get_properties(point, props2, COUNT_OF(props2));
    draw_result = 0;
    point_draw(point);
    test_assert(DRAW_POINT == draw_result);
    clazz = mume_class_of(point);
    test_assert(point_class() == clazz);
    test_assert(0 == strcmp(mume_class_name(clazz), "point"));
    test_assert(mume_object_class() == mume_super_class(clazz));
    test_assert(mume_size_of(point) == sizeof(struct _point));
    test_assert(mume_size_of(point_class()) == sizeof(struct _point_class));
    test_assert(mume_is_a(point, point_class()));
    test_assert(mume_is_of(point, point_class()));
    test_assert(!mume_is_a(point, mume_object_class()));
    test_assert(mume_is_of(point, mume_object_class()));
    test_assert(mume_is_a(point_class(), point_meta_class()));
    test_assert(mume_is_of(point_class(), mume_object_class()));
    test_assert(mume_is_of(point_meta_class(), mume_meta_class()));
    test_assert(mume_is_of(point_class(), mume_meta_class()));
    mume_delete(point);
}
Example #12
0
color_t getPixel(float x, float y, intersect_t *intersect) {
  intersect->t = -1;
  x = (x + 0.5) * 2 / width - 1;
  y = -((y + 0.5) * 2 / height - 1);
  point_t pixelPos = point_new(x, y, -2);
  vec_t rayDirection = vec_normalize(point_direction(cameraPos, pixelPos));
  ray_t ray = ray_new(cameraPos, rayDirection);
  return shootRay(ray, intersect, 0);
}
Example #13
0
Portal *portal_new(void) {
	Portal *portal;
	if ( (portal = malloc(sizeof *portal)) == NULL ) {
		fprintf(stderr,"Probleme de création de portal\n");
		exit(EXIT_FAILURE);
	}
	portal->portail = point_new(0, 0, CELL_SIZE / 2.3);
	portal->actif = 0;
	return portal;
}
Example #14
0
struct affine_point pointmul(const struct affine_point *p,
			     const gcry_mpi_t exp, 
			     const struct domain_params *dp)
{
  struct affine_point r = point_new();
  int n = gcry_mpi_get_nbits(exp);
  while (n) {
    point_double(&r, dp);
    if (gcry_mpi_test_bit(exp, --n))
      point_add(&r, p, dp);
  }
  assert(point_on_curve(&r, dp));
  return r;
}
Example #15
0
int main(int argc, char *argv[]) {
  Point myPoint = point_new(0.0, 0.0);
  Vector unitX = vector_new(1.0, 0.0);
  Vector unitY = vector_new(0.0, 1.0);

  printf("%s + %s + %s = ", point_toString(myPoint),
                            vector_toString(unitX),
                            vector_toString(unitY) );

  point_addVector(&myPoint, unitX);
  point_addVector(&myPoint, unitY);

  printf("%s\n", point_toString(myPoint));

  return 0;
}
Example #16
0
struct affine_point jacobian_to_affine(const struct jacobian_point *p,
				       const struct domain_params *dp)
{
  struct affine_point r = point_new();
  if (gcry_mpi_cmp_ui(p->z, 0)) {
    gcry_mpi_t h;
    h = gcry_mpi_snew(0);
    gcry_mpi_invm(h, p->z, dp->m);
    gcry_mpi_mulm(r.y, h, h, dp->m);
    gcry_mpi_mulm(r.x, p->x, r.y, dp->m);
    gcry_mpi_mulm(r.y, r.y, h, dp->m);
    gcry_mpi_mulm(r.y, r.y, p->y, dp->m);
    gcry_mpi_release(h);
  }
  return r;
}
Example #17
0
/**
 * Insert point to the tree and return point pointer
 *
 * quadtree_insert(quadtree_t *tree, double x, double y, void *key, bool update)
 * @return *point
 */
point_t*
quadtree_insert(quadtree_t *tree, double x, double y, void *key, bool update) {
  point_t *point;
  point_t *point_return=NULL;
  if(!(point = point_new(x, y, key))) return NULL;
  if(!node_contains_(tree->root, point)) {
    point_free(point, tree->key_free);
    return NULL;
  }
  point_return = insert_(tree, tree->root, point, update);
  if(point_return != point) {
    if(point)
      point_free(point, tree->key_free);
    return point_return;
  }
  tree->length++;
  return point_return;
}
Example #18
0
GSList *read_star_list(char *filename, double *maxx, double *maxy)
{
	static char buffer[1024];
	GSList *ret = NULL;
	FILE *fin;
	double x, y;
	struct point *s;

	if (!strcmp(filename, "-"))
		fin = stdin;
	else
		fin = fopen(filename, "r");

	if (fin == NULL)
		return NULL;

	while (fgets(buffer, 1024, fin)) {
		if (!isdigit(buffer[0]))
			continue;

		if (sscanf(buffer, "%lf %lf", &x, &y) == 2) {
			s = point_new (x, y);

			if (maxx && (x > *maxx))
				*maxx = x;

			if (maxy && (y > *maxy))
				*maxy = y;

			ret = g_slist_prepend (ret, s);
		}
	}
	fclose(fin);

	return g_slist_reverse (ret);
}
Example #19
0
int main(int argc, char *argv[]) {
	if (argc == 1) {
		puts("Use: level_edit <level_folder>");
		return 1;
	}

	qw_screen(800, 600, 0, "Projekt Defense");
	
	/* get background image from level folder */
	char lvl_bg[128] = {0};
	strcpy(lvl_bg, argv[1]);
	strcat(lvl_bg, "/background.png");
	qw_image background = qw_loadimage(lvl_bg);
	

	int max_points = 128,
	    points_i = 0;
	SDL_Point *points = malloc(sizeof(SDL_Point) * max_points);
	
	while (qw_running()) {
		qw_drawimage(background);
		
		qw_color(200, 100, 120, 255);
		qw_fillrect(qw_mousex - 2, qw_mousey - 2, 4, 4);
		
		/* waypoint placement */
		if (qw_mousedown(SDL_BUTTON_LEFT)) {
			if (points_i == 0) {
				points[points_i++] = point_new(qw_mousex, qw_mousey);
			} else {
				/* if not the first point placed: check if current point is to close to the last one */
				SDL_Point np = point_new(qw_mousex, qw_mousey);
				
				if (point_distance(points[points_i - 1], np) > 7.f)
					points[points_i++] = np;
			}
			
			/* we need more points? */
			if (points_i == max_points) {
				max_points += 128;
				points = realloc(points, sizeof(SDL_Point) * max_points);
			}
		}
		
		if (qw_keydown(QW_KEY(E))) {
			if (points_i > 1) {
				export_points(argv[1], points, points_i);
				puts("Exported!");
				qw_quit();
			}
		}

		qw_color(100, 120, 200, 255);
		SDL_RenderDrawLines(qw_renderer, points, points_i);

		qw_redraw();
		if (qw_keydown(QW_KEY(ESCAPE))) {
			qw_quit();
		}
	}
	
	free(points);
	qw_destroyimage(background);
	
	return 0;
}
Example #20
0
Point*
triangle_circumcenter(Triangle *t)
{
	return point_new(triangle_circumcenter_x(t), triangle_circumcenter_y(t));
}
Example #21
0
void main_loop(void)
{
	Doubly_linked_node *iterator;
	int direction, dx, dy, count;
	int x, y;

	uint32_t last_time;
	uint32_t current_time;
	uint32_t ellapsed_time;
	uint32_t start_time;

	Point *save_eye;
	float speed;

	last_time = SDL_GetTicks();
	while (!conf->key[SDLK_ESCAPE] && !conf->quit)
	{
		start_time = SDL_GetTicks(); 
		save_eye = point_new(conf->eye->x, conf->eye->y, conf->eye->z);
		speed  = (conf->free_fly) ? 5 : 0.5;

		update_event();

		if (!conf->viewMode && conf->key[SDLK_F1])
		{
			conf->key[SDLK_F1] = 0;
			conf->free_fly = !conf->free_fly;
			if (!conf->free_fly)
			{
				save_eye->x = CELL_SIZE / 2;
				save_eye->y = CELL_SIZE / 2;
				save_eye->z = CHARACTER_SIZE;
			}
			else {
				conf->jump_duration = 0;
			}
		}

		if (!conf->viewMode && (conf->key[SDLK_LSHIFT] || conf->key[SDLK_LALT]))
		{
			if ( conf->jump_duration == 0 ) { /* j'ai du mal a courir quand je saute */
				speed = (conf->free_fly) ? 7.51337 : 0.91337;
			}
		}

		if (!conf->viewMode &&  conf->key[SDLK_LCTRL])
		{
			speed = (conf->free_fly) ? 0.1 : 0.3;
		}

		if (conf->key[SDLK_F2])
		{
			conf->key[SDLK_F2] = 0;
			conf->time = !conf->time;
		}

		if (conf->key[SDLK_F3])
		{
			conf->key[SDLK_F3] = 0;
			conf->display = !conf->display;
		}

		if (!conf->viewMode &&  conf->key[SDLK_F4])
		{
			conf->key[SDLK_F4] = 0;
			conf->quadTreeView = !conf->quadTreeView;
		}

		if (conf->key[SDLK_F5])
		{
			conf->key[SDLK_F5] = 0;
			if (!strcmp(conf->music, "music/music2.mp3"))
			{
				conf->music = "music/music.mp3";
				music_new();
			} else if (!strcmp(conf->music, "music/music.mp3")) {
				conf->music = "music/music2.mp3";
				music_new();
			}
		}
		
		if (!conf->viewMode && (conf->key[SDLK_UP] || conf->key[SDLK_z]))
		{
			forward_move(save_eye, speed);
		}

		if (!conf->viewMode && (conf->key[SDLK_DOWN] || conf->key[SDLK_s]))
		{
			backward_move(save_eye, speed);
		}

		if (!conf->viewMode && (conf->key[SDLK_RIGHT] || conf->key[SDLK_d]))
		{
			right_move(save_eye, speed);
		}

		if (!conf->viewMode && (conf->key[SDLK_LEFT] || conf->key[SDLK_q]))
		{
			left_move(save_eye, speed);
		}

		if (!conf->viewMode && conf->key[SDLK_a])
		{
			conf->key[SDLK_a] = 0;
			conf->theta += 180;
		}

		if (!conf->viewMode && conf->key[SDLK_r])
		{
			conf->key[SDLK_r] = 0;
			conf->life = MAX_HEALTH;
			save_eye->x = CELL_SIZE / 2;
			save_eye->y = CELL_SIZE / 2;
			save_eye->z = CHARACTER_SIZE;
			
			portals->bleu->actif = 0;
			portals->orange->actif = 0;
			conf->timer = SDL_GetTicks();
		}

		if (conf->key[SDLK_KP2] || (conf->key[SDLK_n]))
		{
			if (conf->free_fly) {
				save_eye->z -= 3;
			} else if (conf->viewMode) {
				save_eye->z -= 10;
			}
		}

		if (conf->key[SDLK_KP8] || (conf->key[SDLK_SPACE]))
		{
			if (!conf->free_fly && !conf->viewMode)
			{
				conf->key[SDLK_KP8] = 0;
				conf->key[SDLK_SPACE] = 0;
				if ( !conf->viewMode && conf->jump_duration == 0 ) {
					conf->jump_duration = 120;
				}
			} 
			else if (conf->free_fly){
				save_eye->z += 3;
			} else {
				save_eye->z += 10;
			}
		}
		
		conf->eye->z += jump(save_eye);
		
		if ( conf->key[SDLK_p] ) {
			conf->key[SDLK_p] = 0;
			if(Mix_PausedMusic() == 1) {
				Mix_ResumeMusic();
			}
			else {
				Mix_PauseMusic();
			}
		}
		
		if ( conf->key[SDLK_PLUS] || conf->key[SDLK_m] || conf->key[SDLK_KP_PLUS] ) {
			change_volume(CHANG_VOL);
		}

		if ( conf->key[SDLK_MINUS] || conf->key[SDLK_l] || conf->key[SDLK_KP_MINUS] ) {
			change_volume(-CHANG_VOL);
		}
		
		if (!conf->mousebutton[SDL_BUTTON_LEFT] && !conf->mousebutton[SDL_BUTTON_RIGHT])
		{
			conf->shoot = 0;
		}

		if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_LEFT])
		{
			conf->mousebutton[SDL_BUTTON_LEFT] = 0;
			conf->mousebutton[SDL_BUTTON_RIGHT] = 0;
			conf->shoot = 1;
		}

		if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_RIGHT])
		{
			conf->mousebutton[SDL_BUTTON_LEFT] = 0;
			conf->mousebutton[SDL_BUTTON_RIGHT] = 0;
			conf->shoot = 2;
		}
		
		if (!conf->viewMode && portals->orange->actif && portals->bleu->actif ) {
			if ( abs(save_eye->x - portals->bleu->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->bleu->portail->y) < TRIGGER_DISTANCE) {
				save_eye->x = portals->orange->portail->x - (sin(portals->orange->rotation) * PUSH_DISTANCE);
				save_eye->y = portals->orange->portail->y + (cos(portals->orange->rotation) * PUSH_DISTANCE);
				conf->theta += 180 + ( portals->orange->rotation - portals->bleu->rotation);
			}
			else if ( abs(save_eye->x - portals->orange->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->orange->portail->y) < TRIGGER_DISTANCE) {
				save_eye->x = portals->bleu->portail->x - (sin(portals->bleu->rotation) * PUSH_DISTANCE);
				save_eye->y = portals->bleu->portail->y + (cos(portals->bleu->rotation) * PUSH_DISTANCE);
				conf->theta += 180 + ( portals->bleu->rotation - portals->orange->rotation);
			}
		}

		if (COORD((int)(save_eye->x / CELL_SIZE), (int)(save_eye->y / CELL_SIZE))
				!= COORD((int)((conf->eye)->x / CELL_SIZE), (int)((conf->eye)->y / CELL_SIZE)))
		{
			iterator = mwl->last;
			while (1)
			{
				if ((iterator->object)->type == MOVING_WALL)
				{
					x = (((int)((iterator->object)->anchor)->x ) / CELL_SIZE);
					y = (((int)((iterator->object)->anchor)->y ) / CELL_SIZE);

					count = 0;
					direction = rand() % 4;
					while (count < 4)
					{	
					 	dx = 0;
					 	dy = 0;
					 	switch (direction)
					 	{
					 		case 0:
					 			dx = 1;
					 		break;
					 		case 1:
					 			dy = 1;
					 		break;
					 		case 2:
					 			dx = -1;
					 		break;
					 		default:
					 			dy = -1;
					 		break;
					 	}
					 	if(COORD((x+dx),(y+dy)) == COORD(((int)save_eye->x / CELL_SIZE), ((int)save_eye->y / CELL_SIZE))
					 		|| !IS_PLAYABLE(COORD((x+dx),(y+dy)))
					 		|| (dx == 1 && END_RIGHT(COORD(x,y)))
					 		|| (dy == 1 && END_TOP(COORD(x,y)))
					 		|| (dx == -1 && END_LEFT(COORD(x,y)))
					 		|| (dy == -1 && END_BOTTOM(COORD(x,y)))
					 	) {
					 		direction = (direction + 1) % 4;
					 		++count;
					 	} else {
					 		if(laby->matrix[COORD(x,y)] == SPIKES_MW) {
					 			laby->matrix[COORD(x,y)] = SPIKES;
					 		} else if (laby->matrix[COORD(x,y)] == MOVING_WALL) {
					 			laby->matrix[COORD(x,y)] = PASS;
					 		}

							if(laby->matrix[COORD((x+dx),(y+dy))] == PASS)
							{
								laby->matrix[COORD((x+dx),(y+dy))] = MOVING_WALL;
							} else if (laby->matrix[COORD((x+dx),(y+dy))] == SPIKES) {
								laby->matrix[COORD((x+dx),(y+dy))] = SPIKES_MW;
							}

							((iterator->object)->anchor)->x += (dx * CELL_SIZE);
							((iterator->object)->anchor)->y += (dy * CELL_SIZE);
							count = 4;
						}
					}
				}

				if (iterator->next != NULL)
				{
					iterator = iterator->next;
				} else {
					break;
				}
			}
		}

		/*fprintf(stderr, "%d %d\n", conf->mousex - SCREEN_MID_WIDTH, conf->mousey - SCREEN_MID_HEIGHT);*/
		if (!conf->viewMode && (( save_eye->x > 2 && save_eye->y > 2 
			&& save_eye->x < (CELL_SIZE * WIDTH) - 2 
			&& save_eye->y < (CELL_SIZE * HEIGHT) - 2 
			/*&& save_eye->z <= CHARACTER_SIZE 
			&& save_eye->z > CHARACTER_SIZE - 3*/
			&& IS_PLAYABLE(COORD((int)(save_eye->x / CELL_SIZE),(int)(save_eye->y / CELL_SIZE))) 
			&& IS_PLAYABLE(COORD((int)((save_eye->x + 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) 
			&& IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y + 2) / CELL_SIZE))) 
			&& IS_PLAYABLE(COORD((int)((save_eye->x - 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) 
			&& IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y - 2) / CELL_SIZE))) 
			) || conf->free_fly == 1
		)) {
			conf->eye->x = save_eye->x;
			conf->eye->y = save_eye->y;
			conf->eye->z = save_eye->z;
		}

		/* Mouse motion */
		if(!conf->viewMode)
		{
			conf->theta -= (conf->mousex - SCREEN_MID_WIDTH) * SENSITIVITY;
			conf->phi -= (conf->mousey - SCREEN_MID_HEIGHT) * SENSITIVITY;
			SDL_WarpMouse(SCREEN_MID_WIDTH, SCREEN_MID_HEIGHT); 
			modify_direction();
			change_center();
		} else {
			conf->eye->x = conf->center->x + CELL_SIZE * WIDTH * cos(conf->theta * M_PI / 180);
			conf->eye->y = conf->center->y + CELL_SIZE * HEIGHT * sin(conf->theta * M_PI / 180);
			conf->theta += 0.5;
			if (conf->theta >= 360)
			{
				conf->theta = 0;
			}
		}

		if (conf->life <= 0)
		{
			fprintf(stderr,"                          .,---.\n");
			fprintf(stderr,"                        ,/XM#MMMX;,\n");
			fprintf(stderr,"                      -%%##########M%%,\n");
			fprintf(stderr,"                     -@######%%  $###@=\n");
			fprintf(stderr,"      .,--,         -H#######$   $###M:\n");
			fprintf(stderr,"   ,;$M###MMX;     .;##########$;HM###X=\n");
			fprintf(stderr," ,/@##########H=      ;################+\n");
			fprintf(stderr,"-+#############M/,      %%##############+\n");
			fprintf(stderr,"%%M###############=      /##############:\n");
			fprintf(stderr,"H################      .M#############;.\n");
			fprintf(stderr,"@###############M      ,@###########M:.\n");
			fprintf(stderr,"X################,      -$=X#######@:\n");
			fprintf(stderr,"/@##################%%-     +######$-\n");
			fprintf(stderr,".;##################X     .X#####+,\n");
			fprintf(stderr," .;H################/     -X####+.\n");
			fprintf(stderr,"   ,;X##############,       .MM/\n");
			fprintf(stderr,"      ,:+$H@M#######M#$-    .$$=\n");
			fprintf(stderr,"           .,-=;+$@###X:    ;/=.\n");
			fprintf(stderr,"                  .,/X$;   .::,\n");
			fprintf(stderr,"                      .,    ..\n");
			fprintf(stderr,"Haw Haw ! You lose !\n");
			conf->quit = 1;
		}

		if (IS_EXIT(COORD((int)(conf->eye->x / CELL_SIZE), (int)(conf->eye->y / CELL_SIZE))) && !conf->free_fly)
		{
			if (!conf->win)
			{
				fprintf(stderr, "\n                         #,\n");
				fprintf(stderr, "                        ###\n");
				fprintf(stderr, "                       ## ##\n");
				fprintf(stderr, "                      ##  ##\n");
				fprintf(stderr, "                       ####\n");
				fprintf(stderr, "                         |\n");
				fprintf(stderr, "                        #####\n");
				fprintf(stderr, "                       ######\n");
				fprintf(stderr, "                       ##  ##\n");
				fprintf(stderr, "                       ##  ##\n");
				fprintf(stderr, "                       ##  ##\n");
				fprintf(stderr, "                       ##  ##########\n");
				fprintf(stderr, "                       ##  #############\n");
				fprintf(stderr, "                  #######  ###############\n");
				fprintf(stderr, "              #############################\n");
				fprintf(stderr, "        .###################################\n");
				fprintf(stderr, "       #####################################;\n");
				fprintf(stderr, "       ##                                 ##.\n");
				fprintf(stderr, "       ##                                 ##\n");
				fprintf(stderr, "       #####################################\n");
				fprintf(stderr, "       ##                                 ##\n");
				fprintf(stderr, "       ##                                 ##\n");
				fprintf(stderr, "       ##                                 ###\n");
				fprintf(stderr, "    #####                                 #####\n");
				fprintf(stderr, "   ### ##################################### ###\n");
				fprintf(stderr, "  ###  ##                                 ##  ###\n");
				fprintf(stderr, "  ##   ## ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ##   ##\n");
				fprintf(stderr, "   ##  #####################################  ##\n");
				fprintf(stderr, "    ##                                       ##\n");
				fprintf(stderr, "     ####                                 ####\n");
				fprintf(stderr, "       ######                         ######\n");
				fprintf(stderr, "          ###############################\n");
				fprintf(stderr, "The cake was not a lie !\n");
			}
			if (strcmp(conf->music, "music/music3.mp3")) {
				conf->music = "music/music3.mp3";
				music_new();
			}
			conf->win = 1;
		}

		/* Display with FPS care */
		current_time = SDL_GetTicks();
		ellapsed_time = current_time - last_time;
		last_time = current_time;

		display();

		ellapsed_time = SDL_GetTicks() - start_time;
		if (ellapsed_time < 25)
		{
			SDL_Delay(25 - ellapsed_time);
		}

		point_free(save_eye);
	}
}
Example #22
0
void setUpCustom() {
  // Light
  lights[numLights++] = light_new(point_new(0, 2, -6), 0.5);

  // Reflective material
  color_t color = rgb(0, 0, 0);
  material_t refl = material_new(color, 1);

  // Red material
  color = rgb(231, 76, 60);
  material_t red = material_new(color, 0);

  // Green material
  color = rgb(46, 204, 113);
  material_t green = material_new(color, 0);

  // Purple material
  color = rgb(155, 89, 182);
  material_t purple = material_new(color, 0);

  // White material
  color = rgb(255, 255, 255);
  material_t white = material_new(color, 0);

  // Spheres
  point_t s = point_new(-1, -1, -8);
  spheres[numSpheres++] = sphere_new(s, 1, refl);

  s = point_new(1, -1, -7);
  spheres[numSpheres++] = sphere_new(s, 1, purple);

  // White back wall
  point_t a = point_new(-2.5, -2, -10);
  point_t b = point_new(2.5, -2, -10);
  point_t c = point_new(-2.5, 2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(2.5, -2, -10);
  b = point_new(2.5, 2, -10);
  c = point_new(-2.5, 2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);

  // White ceiling (it has a hole in it...)
  a = point_new(-2.5, 2, -2);
  b = point_new(2.5, 2, -2);
  c = point_new(2.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, 2, -2);
  b = point_new(2.5, 2, -5.5);
  c = point_new(-2.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, 2, -10);
  b = point_new(2.5, 2, -6.5);
  c = point_new(-2.5, 2, -6.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, 2, -10);
  b = point_new(2.5, 2, -10);
  c = point_new(2.5, 2, -6.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, 2, -6.5);
  b = point_new(-0.5, 2, -6.5);
  c = point_new(-0.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, 2, -6.5);
  b = point_new(-0.5, 2, -5.5);
  c = point_new(-2.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(0.5, 2, -6.5);
  b = point_new(2.5, 2, -6.5);
  c = point_new(2.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(0.5, 2, -6.5);
  b = point_new(2.5, 2, -5.5);
  c = point_new(0.5, 2, -5.5);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);

  // White floor
  a = point_new(-2.5, -2, -2);
  b = point_new(2.5, -2, -2);
  c = point_new(2.5, -2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-2.5, -2, -2);
  b = point_new(2.5, -2, -10);
  c = point_new(-2.5, -2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);

  // Red left wall
  a = point_new(-2.5, -2, -2);
  b = point_new(-2.5, -2, -10);
  c = point_new(-2.5, 2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, red);
  a = point_new(-2.5, -2, -2);
  b = point_new(-2.5, 2, -10);
  c = point_new(-2.5, 2, -2);
  triangles[numTriangles++] = triangle_new3(a, b, c, red);

  // Green right wall
  a = point_new(2.5, -2, -2);
  b = point_new(2.5, -2, -10);
  c = point_new(2.5, 2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, green);
  a = point_new(2.5, -2, -2);
  b = point_new(2.5, 2, -10);
  c = point_new(2.5, 2, -2);
  triangles[numTriangles++] = triangle_new3(a, b, c, green);
}
Example #23
0
/* Graham's Scan
 * Given a set of points on the plane, Graham's scan computes their convex hull.
 * The algorithm works in three phases: 
 * 1. Find an extreme point.
 *    This point will be the pivot, is guaranteed to be on the hull,
 *    and is chosen to be the point with largest y coordinate. 
 * 2. Sort the points in order of increasing angle about the pivot.
 *    We end up with a star-shaped polygon (one in which one special point,
 *    in this case the pivot, can "see" the whole polygon). 
 * 3. Build the hull, by marching around the star-shaped poly, adding edges
 *    when we make a left turn, and back-tracking when we make a right turn. 
 */
static int grahamscan(polygon_t *chull, point_list_t *points)
{
  int i;
  real_t dx, dy, ymin;
  real_t *ang;
  point_t *p, *q;
  point_t *v1, *v2, *v3;
  dlink_t *ax, *bx, *cx, *x, *y, *tmp;
	
  assert(chull);
  assert(points);
  assert(point_list_get_count(points) >= 3);
	
  ang = (real_t *)malloc(point_list_get_count(points) * sizeof(real_t));
  assert(ang);

  // Find an extreme point
  // Preparing the polygon, and
  // Find the point with minimum value of y-coordinate
  for (ax = NULL, x = points->tail->next; x != points->head; x = x->next) {
    p = (point_t *)x->object;
    y = dlink_new();
    point_inc_ref(p);
    y->object = (void *)p;
    dlist_insert(y, chull);
    if (ax == NULL || point_get_y(p) < ymin) {
      ax = y;
      ymin = point_get_y(p);
    }
  }
  dlink_cutoff(ax);
  dlist_dec_count(chull);
  dlist_push(ax, chull);

  // Sort the points in order of increasing angle about the pivot.
  p = (point_t *)ax->object;
  //point_dump(p);
  for (i = 0, x = ax->next; x != chull->head; x = x->next) {
    q = (point_t *)x->object;
    dx = point_get_x(q) - point_get_x(p);
    dy = point_get_y(q) - point_get_y(p);
    ang[i] = arctan2r(dy, dx);
    x->spare = (void *)&(ang[i]);
    i++;
    //point_dump(q);
    //printf("ang: %lf\n", ang[i-1]);
  }

  for (x = ax->next; x->next != chull->head; x = x->next) {
    for (y = x->next; y != chull->head; y = y->next) {
      if (*((real_t *)y->spare) < *((real_t *)x->spare)) {
	dlink_exchange(x, y);
	tmp = x, x = y, y = tmp;
      }
    }
  }
  //point_dump((point_t *)c->object);

  v1 = point_new();
  v2 = point_new();
  v3 = point_new();

  cx = chull->tail->next->next->next;
  while (cx != chull->head) {
    bx = cx->prev;
    ax = bx->prev;

    point_subtract(v1, (point_t *)ax->object, (point_t *)bx->object);
    point_subtract(v2, (point_t *)cx->object, (point_t *)bx->object);
    point_xproduct(v3, v1, v2);

    // Convex ?
    if (point_get_z(v3) < 0) {
      cx = cx->next;
    } else {
      dlink_cutoff(bx);
      dlist_dec_count(chull);
      point_destroy((point_t *)bx->object);
      dlink_destroy(bx);
    }
  }

  for (x = chull->tail->next; x != chull->head; x = x->next)
    x->spare = NULL;

  point_destroy(v3);
  point_destroy(v2);
  point_destroy(v1);

  free(ang);

  return dlist_get_count(chull);
}
Example #24
0
/*
 * Quick-Hull
 * Here's an algorithm that deserves its name.
 * It's a fast way to compute the convex hull of a set of points on the plane.
 * It shares a few similarities with its namesake, quick-sort: 
 * - it is recursive.
 * - each recursive step partitions data into several groups.
 *
 * The partitioning step does all the work. The basic idea is as follows:
 * 1. We are given a some points, 
 *    and line segment AB which we know is a chord of the convex hull. 
 * 2. Among the given points, find the one which is farthest from AB.
 *    Let's call this point C.
 * 3. The points inside the triangle ABC cannot be on the hull.
 *    Put them in set s0.
 * 4. Put the points which lie outside edge AC in set s1,
 * and points outside edge BC in set s2.
 *
 * Once the partitioning is done, we recursively invoke quick-hull on sets s1 and s2.
 * The algorithm works fast on random sets of points 
 * because step 3 of the partition typically discards a large fraction of the points. 
 */
static int quickhull_grouping(dlist_t *group)
{
  dlink_t *x, *ax, *bx, *cx, *next;
  dlist_t *s1, *s2;
  point_t *v1, *v2, *v3;
  real_t area;

  assert(group);

  if (dlist_get_count(group) <= 2)
    return dlist_get_count(group);

  v1 = point_new();
  v2 = point_new();
  v3 = point_new();

  // Find the point with maximum parallelogram's area
  ax = dlist_pop(group);
  bx = dlist_extract(group);
  cx = NULL;

  point_subtract(v2, (point_t *)(bx->object), (point_t *)(ax->object));
  for (x = group->tail->next; x != group->head; x = x->next) {
    point_subtract(v1, (point_t *)(x->object), (point_t *)(ax->object));
    point_xproduct(v3, v1, v2);
    if (cx == NULL || point_get_z(v3) > area) {
      cx = x;
      area = point_get_z(v3);
    }
  }
  dlink_cutoff(cx);
  dlist_dec_count(group);

  // S1 grouping
  s1 = dlist_new();
  dlist_insert(ax, s1);
  point_subtract(v2, (point_t *)(cx->object), (point_t *)(ax->object));
  for (x = group->tail->next; x != group->head; ) {
    point_subtract(v1, (point_t *)(x->object), (point_t *)(ax->object));
    point_xproduct(v3, v1, v2);
    if (point_get_z(v3) > 0) {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(group);
      dlist_insert(x, s1);
      x = next;
    } else x = x->next;
  }
  dlist_insert(cx, s1);
  quickhull_grouping(s1);
  assert(cx == s1->head->prev);

  // S2 grouping and pop out others
  cx = dlist_extract(s1);
  s2 = dlist_new();
  dlist_insert(cx, s2);
  point_subtract(v2, (point_t *)bx->object, (point_t *)cx->object);
  for (x = group->tail->next; x != group->head;) {
    point_subtract(v1, (point_t *)x->object, (point_t *)cx->object);
    point_xproduct(v3, v1, v2);
    if (point_get_z(v3) > 0) {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(group);
      dlist_insert(x, s2);
      x = next;
    } else {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(group);
      point_destroy((point_t *)x->object);
      dlink_destroy(x);
      x = next;
    }
  }
  dlist_insert(bx, s2);
  quickhull_grouping(s2);
  assert(bx == s2->head->prev);

  assert(dlist_get_count(group) == 0);
  //assert(group->count == 0);

  // Merge s1 and s2 into group
  while (dlist_get_count(s1) > 0) {
    x = dlist_pop(s1);
    dlist_insert(x, group);
  }

  while (dlist_get_count(s2) > 0) {
    x = dlist_pop(s2);
    dlist_insert(x, group);
  }

  dlist_destroy(s2);
  dlist_destroy(s1);

  point_destroy(v3);
  point_destroy(v2);
  point_destroy(v1);

  return dlist_get_count(group);
}
Example #25
0
void setUpReference() {
  // Light
  lights[numLights++] = light_new(point_new(3, 5, -15), 0);

  // Reflective material
  color_t color = rgb(0, 0, 0);
  material_t refl = material_new(color, 1);

  // Red material
  color = rgb(255, 0, 0);
  material_t red = material_new(color, 0);

  // Blue material
  color = rgb(0, 0, 255);
  material_t blue = material_new(color, 0);

  // White material
  color = rgb(255, 255, 255);
  material_t white = material_new(color, 0);

  // Spheres
  point_t s = point_new(0, 0, -16);
  spheres[numSpheres++] = sphere_new(s, 2, refl);

  s = point_new(3, -1, -14);
  spheres[numSpheres++] = sphere_new(s, 1, refl);

  s = point_new(-3, -1, -14);
  spheres[numSpheres++] = sphere_new(s, 1, red);

  // Back blue wall
  point_t a = point_new(-8, -2, -20);
  point_t b = point_new(8, -2, -20);
  point_t c = point_new(8, 10, -20);
  triangles[numTriangles++] = triangle_new3(a, b, c, blue);
  a = point_new(-8, -2, -20);
  b = point_new(8, 10, -20);
  c = point_new(-8, 10, -20);
  triangles[numTriangles++] = triangle_new3(a, b, c, blue);

  // White floor
  a = point_new(-8, -2, -20);
  b = point_new(8, -2, -10);
  c = point_new(8, -2, -20);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);
  a = point_new(-8, -2, -20);
  b = point_new(-8, -2, -10);
  c = point_new(8, -2, -10);
  triangles[numTriangles++] = triangle_new3(a, b, c, white);

  // Right red wall
  a = point_new(8, -2, -20);
  b = point_new(8, -2, -10);
  c = point_new(8, 10, -20);
  triangles[numTriangles++] = triangle_new3(a, b, c, red);
}
Example #26
0
int INPUT(struct Map_info *In, char *column, char *scol, char *wheresql)
{
    struct quadruple *point;
    double x, y, z, w, nz = 0., sm;
    double c1, c2, c3, c4, c5, c6, nsg;
    int i, j, k = 0, a, irev, cfmask;
    int ddisk = 0;
    double deltx, delty, deltz;
    int first_time = 1;
    CELL *cellmask;
    const char *mapsetm;
    char buf[500];
    int cat, intval;
    struct field_info *Fi;
    dbDriver *Driver;
    dbCatValArray cvarr, sarray;
    int nrec, nrec1, ctype, sctype;
    struct line_pnts *Points;
    struct line_cats *Cats;

    OUTRANGE = 0;
    NPOINT = 0;
    dmin = dmin * dmin;

    /* Read attributes */
    db_CatValArray_init(&cvarr);
    if (scol != NULL)
	db_CatValArray_init(&sarray);
    Fi = Vect_get_field(In, 1);
    if (Fi == NULL)
	G_fatal_error(_("Unable to get layer info for vector map"));

    Driver = db_start_driver_open_database(Fi->driver, Fi->database);
    if (Driver == NULL)
	G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
		      Fi->database, Fi->driver);

    nrec =
	db_select_CatValArray(Driver, Fi->table, Fi->key, column, wheresql,
			      &cvarr);
    ctype = cvarr.ctype;
    G_debug(3, "nrec = %d", nrec);

    if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
	G_fatal_error(_("Column type of wcolumn is not supported (must be integer or double)"));

    if (nrec < 0)
	G_fatal_error(_("Unable to select data from table"));
    G_message("%d records selected from table", nrec);

    if (scol != NULL) {

	nrec1 =
	    db_select_CatValArray(Driver, Fi->table, Fi->key, scol, wheresql,
				  &sarray);
	sctype = cvarr.ctype;

	if (sctype == -1)
	    G_fatal_error(_("Cannot read column type of smooth column"));
	if (sctype == DB_C_TYPE_DATETIME)
	    G_fatal_error
		(_("Column type of smooth column (datetime) is not supported"));
	if (sctype != DB_C_TYPE_INT && sctype != DB_C_TYPE_DOUBLE)
	    G_fatal_error(_("Column type of smooth column is not supported (must be integer or double)"));
    }

    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    Vect_rewind(In);

    while (1) {
	int ival, type, ret;

	if (-1 == (type = Vect_read_next_line(In, Points, Cats)))
	    G_fatal_error(_("Unable to read vector map"));

	if (type == -2)
	    break;		/* EOF */

	if (!(type & GV_POINTS))
	    continue;

	Vect_cat_get(Cats, 1, &cat);
	if (cat < 0) {
	    G_warning(_("Point without category"));
	    continue;
	}

	x = Points->x[0];
	y = Points->y[0];
	z = Points->z[0];

	if (ctype == DB_C_TYPE_INT) {
	    ret = db_CatValArray_get_value_int(&cvarr, cat, &ival);
	    w = ival;
	}
	else {			/* DB_C_TYPE_DOUBLE */
	    ret = db_CatValArray_get_value_double(&cvarr, cat, &w);
	}

	if (ret != DB_OK) {
	    if (wheresql != NULL)
		/* G_message(_("Database record for cat %d not used due to SQL statement")); */
		/* do nothing in this case to not confuse user. Or implement second cat list */
		;
	    else
		G_warning(_("No record for category %d in table <%s>"), cat,
			  Fi->table);
	    continue;
	}

	if (rsm == -1 && scol != NULL) {

	    if (sctype == DB_C_TYPE_INT) {
		ret = db_CatValArray_get_value_int(&sarray, cat, &intval);
		sm = intval;
	    }
	    else {		/* DB_C_TYPE_DOUBLE */
		ret = db_CatValArray_get_value_double(&sarray, cat, &sm);
	    }
	}


	G_debug(3, "%f %f %f %f", x, y, z, w);

	k++;
	w = w * wmult;
	z = z * zmult;
	c1 = x - ((struct octdata *)(root->data))->x_orig;
	c2 = ((struct octdata *)(root->data))->x_orig +
	    ((struct octdata *)(root->data))->n_cols * ew_res - x;
	c3 = y - ((struct octdata *)(root->data))->y_orig;
	c4 = ((struct octdata *)(root->data))->y_orig +
	    ((struct octdata *)(root->data))->n_rows * ns_res - y;
	c5 = z - ((struct octdata *)(root->data))->z_orig;
	c6 = ((struct octdata *)(root->data))->z_orig +
	    ((struct octdata *)(root->data))->n_levs * tb_res - z;

	if (!
	    ((c1 >= 0) && (c2 >= 0) && (c3 >= 0) && (c4 >= 0) && (c5 >= 0) &&
	     (c6 >= 0))) {
	    if (!OUTRANGE) {
		G_warning(_("Some points outside of region -- will ignore..."));
	    }
	    OUTRANGE++;
	}
	else {
	    if (!(point = point_new(x, y, z, w, sm))) {
		clean();
		G_fatal_error(_("Cannot allocate memory for point"));
	    }

	    a = OT_insert_oct(point, root);
	    if (a == 0) {
		NPOINT++;
	    }
	    if (a < 0) {
		G_warning(_("Can't insert %lf,%lf,%lf,%lf,%lf a=%d"), x, y, z,
			  w, sm, a);
		return -1;
	    }

	    if (first_time) {
		first_time = 0;
		xmin = x;
		ymin = y;
		zmin = z;
		wmin = w;
		xmax = x;
		ymax = y;
		zmax = z;
		wmax = w;
	    }

	    xmin = amin1(xmin, x);
	    ymin = amin1(ymin, y);
	    zmin = amin1(zmin, z);
	    wmin = amin1(wmin, w);
	    xmax = amax1(xmax, x);
	    ymax = amax1(ymax, y);
	    zmax = amax1(zmax, z);
	    wmax = amax1(wmax, w);
	}
    }				/* while */

    db_CatValArray_free(&cvarr);

    c1 = xmin - ((struct octdata *)(root->data))->x_orig;
    c2 = ((struct octdata *)(root->data))->x_orig +
	((struct octdata *)(root->data))->n_cols * ew_res - xmax;
    c3 = ymin - ((struct octdata *)(root->data))->y_orig;
    c4 = ((struct octdata *)(root->data))->y_orig +
	((struct octdata *)(root->data))->n_rows * ns_res - ymax;
    c5 = zmin - ((struct octdata *)(root->data))->z_orig;
    c6 = ((struct octdata *)(root->data))->z_orig +
	((struct octdata *)(root->data))->n_levs * tb_res - zmax;

    if ((c1 > 5 * ew_res) || (c2 > 5 * ew_res) ||
	(c3 > 5 * ns_res) || (c4 > 5 * ns_res) ||
	(c5 > 5 * tb_res) || (c6 > 5 * tb_res)) {
	static int once = 0;

	if (!once) {
	    once = 1;
	    G_warning(_("Strip exists with insufficient data"));
	}
    }

    nz = wmin;
    totsegm = translate_oct(root, ((struct octdata *)(root->data))->x_orig,
			    ((struct octdata *)(root->data))->y_orig,
			    ((struct octdata *)(root->data))->z_orig, nz);
    if (!totsegm) {
	clean();
	G_fatal_error(_("Zero segments!"));
    }

    ((struct octdata *)(root->data))->x_orig = 0;
    ((struct octdata *)(root->data))->y_orig = 0;
    ((struct octdata *)(root->data))->z_orig = 0;	/* was commented out */

    if (outz != NULL)
	ddisk += disk;
    if (gradient != NULL)
	ddisk += disk;
    if (aspect1 != NULL)
	ddisk += disk;
    if (ncurv != NULL)
	ddisk += disk;
    if (gcurv != NULL)
	ddisk += disk;
    if (mcurv != NULL)
	ddisk += disk;

    G_message
	("Processing all selected output files will require %d bytes of disk space for temp files",
	 ddisk);

    /*
       fprintf(stderr,"xmin=%lf,xmax=%lf,ymin=%lf,ymax=%lf,zmin=%lf,zmax=%lf,wmin=%lf,wmax=%lf\n",xmin,xmax,ymin,ymax,zmin,zmax,wmin,wmax);
     */

    fprintf(stderr, "\n");
    if (OUTRANGE > 0)
	G_warning
	    (_("There are points outside specified 2D/3D region--ignored %d points (total points: %d)"),
	     OUTRANGE, k);
    if (NPOINT > 0)
	G_warning
	    (_("Points are more dense than specified 'DMIN'--ignored %d points (remain %d)"),
	     NPOINT, k - NPOINT);
    NPOINT = k - NPOINT - NPT - OUTRANGE;
    if (NPOINT < KMIN) {
	if (NPOINT != 0) {
	    G_warning
		(_("%d points given for interpolation (after thinning) is less than given NPMIN=%d"),
		 NPOINT, KMIN);
	    KMIN = NPOINT;
	}
	else {
	    fprintf(stderr, "ERROR: zero points in the given region!\n");
	    return -1;
	}
    }
    if (NPOINT > KMAXPOINTS && KMIN <= KMAX) {
	fprintf(stderr,
		"ERROR: segmentation parameters set to invalid values: npmin = %d, segmax = %d \n",
		KMIN, KMAX);
	fprintf(stderr,
		"for smooth connection of segments, npmin > segmax (see manual) \n");
	return -1;
    }

    if (NPOINT < KMAXPOINTS && KMAX != KMAXPOINTS)
	G_warning
	    (_("There is less than %d points for interpolation, no segmentation is necessary, to run the program faster, set segmax=%d (see manual)"),
	     KMAXPOINTS, KMAXPOINTS);

    deltx = xmax - xmin;
    delty = ymax - ymin;
    deltz = zmax - zmin;
    nsg = (double)NPOINT / (double)KMIN;
    dnorm = deltx * delty * deltz / nsg;
    nsg = 3.0;
    nsg = 1. / nsg;
    dnorm = pow(dnorm, nsg);
    /* DEBUG
       if (fd4 != NULL)
       fprintf (fd4, "deltx,delty %f %f \n", deltx, delty);
     */
    nsizc = current_region.cols;	/* ((int)(deltx/ew_res))+1;  */
    nsizr = current_region.rows;	/* ((int)(delty/ns_res))+1;   */
    NPT = k;
    x0utm = 0.;
    y0utm = 0.;
    z0utm = 0.;

  /** create a bitmap mask from given raster map **/
    if (maskmap != NULL) {
	mapsetm = G_find_raster2(maskmap, "");
	if (!mapsetm) {
	    clean();
	    G_fatal_error(_("Mask raster map [%s] not found"), maskmap);
	}
	bitmask = BM_create(nsizc, nsizr);
	cellmask = Rast_allocate_c_buf();
	cfmask = Rast_open_old(maskmap, mapsetm);
	for (i = 0; i < nsizr; i++) {
	    irev = nsizr - i - 1;
	    Rast_get_c_row(cfmask, cellmask, i);
	    for (j = 0; j < nsizc; j++) {
		if ((cellmask[j] == 0) || Rast_is_c_null_value(&cellmask[j]))
		    BM_set(bitmask, j, irev, 0);
		else
		    BM_set(bitmask, j, irev, 1);
	    }
	}
	G_message(_("Bitmap mask created"));
    }

    return 1;
}
Example #27
0
static int quickhull(polygon_t *chull, point_list_t *points)
{
  real_t ymin, ymax, yval;
  point_t *p, *v1, *v2, *v3;
  dlink_t *ax, *bx, *x, *y, *next;
  dlist_t *right_group, *left_group;

  assert(chull);
  assert(points);

  // Allocate the structure element of convex hull
  for (x = points->tail->next; x != points->head; x = x->next) {
    p = (point_t *)x->object;
    point_inc_ref(p);
    y = dlink_new();
    y->object = (void *)p;
    dlist_insert(y, chull);
  }

  // find the extreme points along y-axis
  ax = NULL;
  bx = NULL;
  for (x = chull->tail->next; x != chull->head; x = x->next) {
    yval = point_get_y((point_t *)(x->object));
    if (ax == NULL || yval < ymin) {
      ax = x;
      ymin = yval;
    }
    if (bx == NULL || yval > ymax) {
      bx = x;
      ymax = yval;
    }
  }
  dlink_cutoff(ax);
  dlist_dec_count(chull);
  dlink_cutoff(bx);
  dlist_dec_count(chull);
  //point_dump((point_t *)ax->object);
  //point_dump((point_t *)bx->object);

  v1 = point_new();
  v2 = point_new();
  v3 = point_new();

  //printf("for right section\n");
  right_group= dlist_new();
  dlist_insert(ax, right_group);
  point_subtract(v2, (point_t *)bx->object, (point_t *)ax->object);
  for (x = chull->tail->next; x != chull->head;) {
    //point_dump((point_t *)x->object);
    point_subtract(v1, (point_t *)x->object, (point_t *)ax->object);
    point_xproduct(v3, v1, v2);
    if (point_get_z(v3) > 0) {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(chull);
      dlist_insert(x, right_group);
      //printf(" ");
      //point_dump((point_t *)x->object);
      x = next;
    } else x = x->next;
  }
  dlist_insert(bx, right_group);
  quickhull_grouping(right_group);

  //printf("for left section\n");
  ax = dlist_pop(right_group);
  bx = dlist_extract(right_group);
  //point_dump((point_t *)ax->object);
  //point_dump((point_t *)bx->object);

  left_group = dlist_new();
  dlist_insert(bx, left_group);
  point_subtract(v2, (point_t *)ax->object, (point_t *)bx->object);
  for (x = chull->tail->next; x != chull->head; ) {
    point_subtract(v1, (point_t *)x->object, (point_t *)bx->object);
    point_xproduct(v3, v1, v2);
    if (point_get_z(v3) > 0) {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(chull);
      dlist_insert(x, left_group);
      //point_dump((point_t *)x->object);
      x = next;
    } else {
      next = x->next;
      dlink_cutoff(x);
      dlist_dec_count(chull);
      point_destroy((point_t *)x->object);
      dlink_destroy(x);
      x = next;
    }
  }
  dlist_insert(ax, left_group);
  quickhull_grouping(left_group);

  ax = dlist_extract(left_group);
  bx = dlist_pop(left_group);

  dlist_insert(ax, chull);
  while (dlist_get_count(right_group) > 0) {
    x = dlist_pop(right_group);
    dlist_insert(x, chull);
  }
  dlist_insert(bx, chull);

  while (dlist_get_count(left_group) > 0) {
    x = dlist_pop(left_group);
    dlist_insert(x, chull);
  }

  dlist_destroy(left_group);
  dlist_destroy(right_group);

  point_destroy(v3);
  point_destroy(v2);
  point_destroy(v1);

  return dlist_get_count(chull);
}
Example #28
0
real_t convexhull_compute_weak_diameter(line_t *diameter, polygon_t *chull)
{
  real_t low, high, dist;
  real_t temp, temp1, temp2, temp3, temp4;
  dlink_t *ax, *bx, *ax_prev, *bx_next;
  point_t *a, *b, p, *u, *v, *r;

  assert(diameter);
  assert(chull);

  r = point_new();
  u = NULL;
  v = NULL;
  dist = 0;

  for (ax = chull->tail->next; ax->next != chull->head; ax = ax->next) {
    if (ax->prev == chull->tail) ax_prev = chull->head->prev;
    else ax_prev = ax->prev;
    a = (point_t *)ax->object;

    for (bx = ax->next; bx != chull->head; bx = bx->next) {
      if (bx->next == chull->head) bx_next = chull->tail->next;
      else bx_next = bx->next;
      b = (point_t *)bx->object;

      point_subtract(&p, b, a);
      low = point_dotproduct(a, &p);
      high = point_dotproduct(b, &p);

      if (low > high) {
	temp = low;
	low = high;
	high = temp;
      }

      temp1 = point_dotproduct((point_t *)ax->next->object, &p);
      temp2 = point_dotproduct((point_t *)ax_prev->object, &p);
      temp3 = point_dotproduct((point_t *)bx_next->object, &p);
      temp4 = point_dotproduct((point_t *)bx->prev->object, &p);

      if ((low <= temp1 && temp1 <= high) &&
	  (low <= temp2 && temp2 <= high) &&
	  (low <= temp3 && temp3 <= high) &&
	  (low <= temp4 && temp4 <= high)) {
	temp = get_distance_of_p2p(a, b);
	/*
	circle(p->x, p->y, 10, 255, 0, 0);
	circle(q->x, q->y, 10, 255, 0, 0);
	keyhit();
	circle(p->x, p->y, 10, 0, 0, 0);
	circle(q->x, q->y, 10, 0, 0, 0);
	*/
	if (temp > dist) {
	  u = a; v = b;
	  dist = temp;
	}
      }
    }
  }

  line_set_endpoints(diameter, u, v);

  return dist;
}