-
Notifications
You must be signed in to change notification settings - Fork 0
/
objMaker.ml
182 lines (167 loc) · 4.85 KB
/
objMaker.ml
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
let f_i = float_of_int
let s_i = string_of_int
let s_f = string_of_float
(*let lastHeight = ref 0.
let lastColor = ref (0.,0.,0.)*)
let rec pgcd n m =
if n > m then pgcd m n
else if n = 0 then m
else let r = m mod n in
pgcd r n
let getHeight (r1,g1,b1) =(* Printf.printf "%f,%f %u\n" x y interval; *)
(* try *)
let (r,g,b)=
(int_of_float (255.*.r1),
int_of_float (255.*.g1),
int_of_float (255.*.b1)) in
(*lastHeight :=*)float_of_int (ImageProcessing.getHeightFor (r,g,b))(*;
!lastHeight
with Not_found -> !lastHeight *)
let getColor (x,y) interval =(* Printf.printf "%f,%f %u\n" x y interval; *)
(* try *)
let (r,g,b)=ImageProcessing.getColorAt
(int_of_float ((f_i interval) *.x),int_of_float ((f_i interval) *.y )) in
(*lastColor :=*) (((f_i r)/.255.),((f_i g)/.255.),((f_i b)/.255.))(* ;
!lastColor
with Not_found -> !lastColor *)
let gH=getHeight
let gC=getColor
(* let gH (x,y) interval = (x-.y)*.(x+.y) *)
let pp =function i->i:=!i+1
module VertexMap =Map.Make (struct
type t = int
let compare = Pervasives.compare
end)
(*let adjust fin interval=
let gros = ref (fin/interval) in
while !gros*interval > fin do
print_string "Reduit";
gros := !gros -1;
done;
!gros
*)
let calc_intersection (w,h) interval =
let cx = w/interval
and cy = h/interval
and vmap = ref VertexMap.empty
and flist = ref []
and ul = ref 0
and ur = ref 0
and mc = ref 0
and dl = ref 0
and dr = ref 0
and i= ref 2 (* iterator *)
and color = ref (0.,0.,0.)
in
(* let count = (2*cx*cy -cy -cx +1)
in *)
(* c=0 r=0 up left*)
(* vlist := (0.,0.,(gH (0.,0.) interval))::!vlist; (*hackfix*) *)
color := (gC (0.,0.) interval);
vmap:=VertexMap.add 1 (
!color,
(0.,
(gH !color),
0.)
) !vmap; (*1*)
(* pp i : i initialisé à 2 *)
for c = 0 to cy do
(* c=c r=0 : up right*)
color := (gC (f_i c+.1.,0.) interval);
vmap:=VertexMap.add !i (
!color,
(f_i c +.1.,
(gH !color),
0.0)) !vmap;
pp i;
for r = 0 to cx do
ur := !i-1;
if c = 1 then
begin
ul := !i-3*(cx)-1-4+r;
dl := !i-3*(cx)-1-1+r;
end
else
(* c=0 r=r *)
if c = 0 then
begin
if r = 0 then
ul := !i-2
else
ul := !i-3;
(* down left *)
color := (gC (f_i c,f_i r+.1.) interval);
vmap:=VertexMap.add !i (
!color,
(f_i c,
(gH !color),
f_i r+.1.)) !vmap;
dl := !i;
pp i;
end
else
begin
ul := !i-2*(cx)-1-3;
dl := !i-2*(cx)-1-1;
end;
(* middle center *)
color := (gC (f_i c+.0.5,f_i r+.0.5) interval);
vmap:=VertexMap.add !i (
!color,
(f_i c+.0.5,
(gH !color),
f_i r+.0.5)) !vmap;
mc := !i;
pp i;
color :=(gC (f_i c+.1.,f_i r+.1.) interval);
(* down right *)
vmap:=VertexMap.add !i (
!color,
(f_i c+.1.,
(gH !color),
f_i r+.1.)) !vmap;
dr := !i;
pp i;
(* up *)
flist := (!ul,!ur,!mc)::!flist;
(* left *)
flist := (!ul,!mc,!dl)::!flist;
(* right *)
flist := (!ur,!mc,!dr)::!flist;
(* down *)
flist := (!dl,!mc,!dr)::!flist;
done
done;
(!vmap,!flist)
let pixel2coord (x,y,z) =
((float_of_int x)/.100.0,
(float_of_int y)/.100.0,
(float_of_int z)/.100.0)
let rec createCoordList = function
| [] -> []
| c::l -> (pixel2coord c)::(createCoordList l)
let createObj filename (vMap,fList) =
let file =
open_out_gen [Open_wronly; Open_creat; Open_trunc] 511 filename in
let vertices = ref "" in
let concatVertices i (color,(x,y,z))=
vertices := !vertices ^
"\nv "^(string_of_float x)^
" "^(string_of_float y)^
" "^(string_of_float z)
in
let rec concatFacets =
function
| [] -> "\n"
| (a,b,c):: l ->
concatFacets l ^
"\nf "^(string_of_int a)^
" "^(string_of_int b)^
" "^(string_of_int c)
in
VertexMap.iter concatVertices vMap;
output_string file ("# Debut du fichier"^
!vertices ^
(concatFacets fList)^
"\n# Fin du fichier "^filename);
close_out file