package vector3

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file vector3.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
(* Copyright (c) 2022, Francois Berenger
 * Tsuda laboratory, Graduate School of Frontier Sciences,
 * The University of Tokyo, Japan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *)

type t = { x : float ;
           y : float ;
           z : float }

(* constructors ------------------------------------------------------------ *)

let origin =
  { x = 0. ;
    y = 0. ;
    z = 0. }

let make x y z =
  { x = x ;
    y = y ;
    z = z }

let of_triplet (x, y, z) =
  { x = x ;
    y = y ;
    z = z }

let of_string str =
  try Scanf.sscanf str "%f %f %f"
        (fun x y z -> { x = x ;
                        y = y ;
                        z = z })
  with _ -> invalid_arg ("Vector3.of_string: " ^ str)

(* to other types ---------------------------------------------------------- *)

let to_triplet v =
  (v.x, v.y, v.z)

let to_string v =
  Printf.sprintf "%f %f %f" v.x v.y v.z

(* operations -------------------------------------------------------------- *)

(* tests' variables *)
(*$inject
  let tiny = 0.000001 ;;
  let unit_square = make 1. 1. 0. ;;
  let unit_cube = make 1. 1. 1. ;;
  let ox, oy, oz = (make 1. 0. 0.,
                    make 0. 1. 0.,
                    make 0. 0. 1.) ;;
*)

(*$T dot
  dot unit_cube unit_cube = 3.
*)
let dot v1 v2 =
  v1.x *. v2.x +.
  v1.y *. v2.y +.
  v1.z *. v2.z

(*$T cross
  cross ox oy = oz
*)
let cross v1 v2 =
  { x = v1.y *. v2.z -. v1.z *. v2.y ;
    y = v1.z *. v2.x -. v1.x *. v2.z ;
    z = v1.x *. v2.y -. v1.y *. v2.x }

(*$T diff
  diff unit_cube unit_cube = origin
*)
let diff v1 v2 =
  { x = v1.x -. v2.x ;
    y = v1.y -. v2.y ;
    z = v1.z -. v2.z }

(*$T add
  add ox (add oy oz) = unit_cube
*)
let add v1 v2 =
  { x = v1.x +. v2.x ;
    y = v1.y +. v2.y ;
    z = v1.z +. v2.z }

(*$T mag2
  mag2 unit_cube = 3.
*)
let mag2 v =
  v.x *. v.x +.
  v.y *. v.y +.
  v.z *. v.z

(*$T mag
  mag unit_cube = sqrt 3.
*)
let mag v =
  sqrt (mag2 v)

(*$T angle
  cmp_float ~epsilon:tiny (atan 1.) (angle ox unit_square)
*)
let angle v1 v2 =
  acos ((dot v1 v2) /.
        ((mag v1) *. (mag v2)))

(*$T dist
   dist unit_cube origin = sqrt 3.
*)
let dist v1 v2 =
  mag (diff v1 v2)

(*$T div
  div unit_cube 2. = make 0.5 0.5 0.5
*)
let div v s =
  { x = v.x /. s ;
    y = v.y /. s ;
    z = v.z /. s }

(*$T mult
   mult unit_cube 2. = make 2. 2. 2.
*)
let mult v s =
  { x = v.x *. s ;
    y = v.y *. s ;
    z = v.z *. s }

(*$T normalize
   cmp_float ~epsilon:tiny (mag (normalize unit_cube)) 1.
*)
let normalize v =
  div v (mag v)

(*$T div
  neg unit_cube = make (-1.) (-1.) (-1.)
*)
let neg v =
  { x = -.v.x ;
    y = -.v.y ;
    z = -.v.z }