191 lines
4.3 KiB
C
191 lines
4.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: o_ :::::: ::: */
|
|
/* fdf.c :+: / :+::+: :+: */
|
|
/* +:+ > +:++:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
/* Created: 2024/12/06 11:07:30 by whaffman #+#+# #+#+# #+# #+# | */
|
|
/* Updated: 2024/12/06 11:19:30 by whaffman ### ### ### ### / \ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "fdf.h"
|
|
|
|
|
|
//# include "MLX42.h"
|
|
#define WRONG_LINE_LENGTH "Error: wrong line length. Expected %d, got %d\n"
|
|
#define MALLOC_ERROR "Error: malloc failed\n"
|
|
#define FILE_ERROR "Error: could not open file %s\n"
|
|
#define USAGE_ERROR "Usage: %s <filename>\n"
|
|
|
|
void rotate_x(t_point **points, double angle, int size)
|
|
{
|
|
int i;
|
|
int previous_y;
|
|
|
|
i = 0;
|
|
while (i < size)
|
|
{
|
|
previous_y = (*points)[i].y;
|
|
(*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle);
|
|
(*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle);
|
|
i++;
|
|
}
|
|
}
|
|
void rotate_y(t_point **points, double angle, int size)
|
|
{
|
|
int i;
|
|
int previous_x;
|
|
|
|
i = 0;
|
|
while (i < size)
|
|
{
|
|
previous_x = (*points)[i].x;
|
|
(*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle);
|
|
(*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle);
|
|
i++;
|
|
}
|
|
}
|
|
void rotate_z(t_point **points, double angle, int size)
|
|
{
|
|
int i;
|
|
int previous_x;
|
|
int previous_y;
|
|
|
|
i = 0;
|
|
while (i < size)
|
|
{
|
|
previous_x = (*points)[i].x;
|
|
previous_y = (*points)[i].y;
|
|
(*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle);
|
|
(*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int get_map_sizes(char *filename, t_map *map)
|
|
{
|
|
const int fd = open(filename, O_RDONLY);
|
|
char *line;
|
|
int width;
|
|
|
|
if (fd < 0)
|
|
return (ft_printf(FILE_ERROR, filename), 0);
|
|
width = 0;
|
|
while (true)
|
|
{
|
|
line = get_next_line(fd);
|
|
if (!line)
|
|
break ;
|
|
width = ft_count_words(line, ' ');
|
|
free(line);
|
|
if (map->width != 0 && map->width != width)
|
|
return (ft_printf(WRONG_LINE_LENGTH, map->width, width), 0);
|
|
else if (map->width == 0)
|
|
map->width = width;
|
|
map->height++;
|
|
}
|
|
return (close(fd), 1);
|
|
}
|
|
|
|
void free_line_and_split(char **line, char ***split)
|
|
{
|
|
char **split_start;
|
|
|
|
split_start = *split;
|
|
free(*line);
|
|
*line = NULL;
|
|
while (**split)
|
|
{
|
|
free(**split);
|
|
**split = NULL;
|
|
(*split)++;
|
|
}
|
|
free(split_start);
|
|
*split = NULL;
|
|
}
|
|
|
|
int read_map(char *filename, t_map *map)
|
|
{
|
|
int fd;
|
|
char *line;
|
|
int y;
|
|
int x;
|
|
char **split;
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
if (fd < 0)
|
|
return (ft_printf(FILE_ERROR, filename), 0);
|
|
y = map->height - 1;
|
|
while (true)
|
|
{
|
|
x = 0;
|
|
line = get_next_line(fd);
|
|
if (!line)
|
|
break ;
|
|
split = ft_split(line, ' ');
|
|
while (split[x])
|
|
{
|
|
map->points[y * map->width + x].x = x - map->width / 2;
|
|
map->points[y * map->width + x].y = map->height / 2 - y;
|
|
map->points[y * map->width + x].z = ft_atoi(split[x]);
|
|
x++;
|
|
}
|
|
free_line_and_split(&line, &split);
|
|
y--;
|
|
}
|
|
close(fd);
|
|
return (1);
|
|
}
|
|
|
|
void print_map(t_map *map)
|
|
{
|
|
int x;
|
|
int y;
|
|
|
|
y = 0;
|
|
while (y < map->height)
|
|
{
|
|
x = 0;
|
|
while (x < map->width)
|
|
{
|
|
printf("(%.2f, %.2f, %.2f)\n",
|
|
map->points[y * map->width + x].x,
|
|
map->points[y * map->width + x].y,
|
|
map->points[y * map->width + x].z);
|
|
x++;
|
|
}
|
|
ft_printf("\n");
|
|
y++;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
t_map *map;
|
|
|
|
if (argc != 2)
|
|
{
|
|
ft_printf(USAGE_ERROR, argv[0]);
|
|
return (EXIT_FAILURE);
|
|
}
|
|
map = malloc(sizeof(t_map));
|
|
if (!map)
|
|
return (ft_printf(MALLOC_ERROR), EXIT_FAILURE);
|
|
get_map_sizes(argv[1], map);
|
|
map->points = malloc(map->width * map->height * sizeof(t_point));
|
|
if (!map->points)
|
|
return (ft_printf(MALLOC_ERROR), EXIT_FAILURE);
|
|
if (!read_map(argv[1], map))
|
|
return (EXIT_FAILURE);
|
|
print_map(map);
|
|
rotate_x(&map->points, M_PI / 4, map->width * map->height);
|
|
rotate_z(&map->points, M_PI / 4, map->width * map->height);
|
|
print_map(map);
|
|
return (EXIT_SUCCESS);
|
|
}
|