-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools_parse.c
119 lines (104 loc) · 2.76 KB
/
tools_parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tools_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmanzini <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/09 14:15:16 by mpauw #+# #+# */
/* Updated: 2018/11/27 16:43:13 by mpauw ### ########.fr */
/* */
/* ************************************************************************** */
#include "rt.h"
/*
** Updates values in a vector by reading from a String.
*/
void update_vector(t_3v *vector, char *line)
{
double *tmp;
int i;
if (!(tmp = (double *)malloc(3 * sizeof(double))))
error(0);
get_doubles_from_line(tmp, line, 3);
i = 0;
while (i < 3)
{
(vector->v)[i] = tmp[i];
i++;
}
free(tmp);
}
void update_vector_xyz(t_3v *vector, char *line)
{
double *tmp;
if (!(tmp = (double *)malloc(3 * sizeof(double))))
error(0);
get_doubles_from_line(tmp, line, 3);
(vector->v)[0] = tmp[2];
(vector->v)[1] = tmp[0];
(vector->v)[2] = tmp[1];
free(tmp);
}
/*
** Get a size amount of doubles from a String.
*/
void get_doubles_from_line(double *v, char *line, int size)
{
char **values_str;
int i;
values_str = ft_strsplit((line), ' ');
i = 0;
while (*(values_str + i))
i++;
if (i != size)
s_error("Not the right amount of vector values");
i = -1;
while (++i < size)
v[i] = ft_atod(values_str[i]);
ft_free_array((void **)values_str);
}
/*
** Get a size amount of int from a String.
*/
void get_int_from_line(int *v, char *line, int size)
{
char **values_str;
int i;
values_str = ft_strsplit((line), ' ');
i = 0;
while (*(values_str + i))
i++;
if (i != size)
s_error("Not the right amount of vector values");
i = -1;
while (++i < size)
v[i] = ft_atoi(values_str[i]);
ft_free_array((void **)values_str);
}
/*
** Get a String of vector values.
*/
char *get_vector_string(t_3v v, int precision)
{
char *s;
char *v0;
char *v1;
char *v2;
int size;
v0 = ft_dtoa(v.v[0], precision);
v1 = ft_dtoa(v.v[1], precision);
v2 = ft_dtoa(v.v[2], precision);
size = ft_strlen(v0) + ft_strlen(v1) + ft_strlen(v2) + 3;
if (!(s = (char *)malloc(sizeof(char) * size)))
error(1);
ft_bzero(s, size);
s = ft_strcat(s, v0);
s = ft_strcat(s, " ");
s = ft_strcat(s, v1);
s = ft_strcat(s, " ");
s = ft_strcat(s, v2);
free(v0);
free(v1);
free(v2);
return (s);
}